Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file locking_ptr.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 15.07.2009
|
Chris@16
|
11 *
|
Chris@16
|
12 * This header is the Boost.Log library implementation, see the library documentation
|
Chris@16
|
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@101
|
19 #include <cstddef>
|
Chris@101
|
20 #include <boost/move/core.hpp>
|
Chris@16
|
21 #include <boost/smart_ptr/shared_ptr.hpp>
|
Chris@101
|
22 #include <boost/thread/lock_options.hpp>
|
Chris@16
|
23 #include <boost/log/detail/config.hpp>
|
Chris@16
|
24 #include <boost/utility/explicit_operator_bool.hpp>
|
Chris@16
|
25 #include <boost/log/detail/header.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
28 #pragma once
|
Chris@16
|
29 #endif
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost {
|
Chris@16
|
32
|
Chris@16
|
33 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
34
|
Chris@16
|
35 namespace aux {
|
Chris@16
|
36
|
Chris@16
|
37 //! A pointer type that locks the backend until it's destroyed
|
Chris@101
|
38 template< typename T, typename LockableT >
|
Chris@16
|
39 class locking_ptr
|
Chris@16
|
40 {
|
Chris@101
|
41 typedef locking_ptr this_type;
|
Chris@101
|
42 BOOST_COPYABLE_AND_MOVABLE_ALT(this_type)
|
Chris@101
|
43
|
Chris@16
|
44 public:
|
Chris@16
|
45 //! Pointed type
|
Chris@16
|
46 typedef T element_type;
|
Chris@16
|
47
|
Chris@16
|
48 private:
|
Chris@101
|
49 //! Lockable type
|
Chris@101
|
50 typedef LockableT lockable_type;
|
Chris@101
|
51
|
Chris@101
|
52 private:
|
Chris@16
|
53 //! The pointer to the backend
|
Chris@16
|
54 shared_ptr< element_type > m_pElement;
|
Chris@16
|
55 //! Reference to the shared lock control object
|
Chris@101
|
56 lockable_type* m_pLock;
|
Chris@16
|
57
|
Chris@16
|
58 public:
|
Chris@101
|
59 //! Default constructor
|
Chris@101
|
60 locking_ptr() BOOST_NOEXCEPT : m_pLock(NULL)
|
Chris@16
|
61 {
|
Chris@16
|
62 }
|
Chris@16
|
63 //! Constructor
|
Chris@101
|
64 locking_ptr(shared_ptr< element_type > const& p, lockable_type& l) : m_pElement(p), m_pLock(&l)
|
Chris@16
|
65 {
|
Chris@101
|
66 m_pLock->lock();
|
Chris@101
|
67 }
|
Chris@101
|
68 //! Constructor
|
Chris@101
|
69 locking_ptr(shared_ptr< element_type > const& p, lockable_type& l, try_to_lock_t const&) : m_pElement(p), m_pLock(&l)
|
Chris@101
|
70 {
|
Chris@101
|
71 if (!m_pLock->try_lock())
|
Chris@16
|
72 {
|
Chris@16
|
73 m_pElement.reset();
|
Chris@16
|
74 m_pLock = NULL;
|
Chris@16
|
75 }
|
Chris@16
|
76 }
|
Chris@16
|
77 //! Copy constructor
|
Chris@16
|
78 locking_ptr(locking_ptr const& that) : m_pElement(that.m_pElement), m_pLock(that.m_pLock)
|
Chris@16
|
79 {
|
Chris@16
|
80 if (m_pLock)
|
Chris@101
|
81 m_pLock->lock();
|
Chris@16
|
82 }
|
Chris@101
|
83 //! Move constructor
|
Chris@101
|
84 locking_ptr(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT : m_pLock(that.m_pLock)
|
Chris@101
|
85 {
|
Chris@101
|
86 m_pElement.swap(that.m_pElement);
|
Chris@101
|
87 that.m_pLock = NULL;
|
Chris@101
|
88 }
|
Chris@101
|
89
|
Chris@16
|
90 //! Destructor
|
Chris@16
|
91 ~locking_ptr()
|
Chris@16
|
92 {
|
Chris@101
|
93 if (m_pLock)
|
Chris@16
|
94 m_pLock->unlock();
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 //! Assignment
|
Chris@101
|
98 locking_ptr& operator= (locking_ptr that) BOOST_NOEXCEPT
|
Chris@16
|
99 {
|
Chris@16
|
100 this->swap(that);
|
Chris@16
|
101 return *this;
|
Chris@16
|
102 }
|
Chris@16
|
103
|
Chris@16
|
104 //! Indirection
|
Chris@101
|
105 element_type* operator-> () const BOOST_NOEXCEPT { return m_pElement.get(); }
|
Chris@16
|
106 //! Dereferencing
|
Chris@101
|
107 element_type& operator* () const BOOST_NOEXCEPT { return *m_pElement; }
|
Chris@16
|
108
|
Chris@16
|
109 //! Accessor to the raw pointer
|
Chris@101
|
110 element_type* get() const BOOST_NOEXCEPT { return m_pElement.get(); }
|
Chris@16
|
111
|
Chris@16
|
112 //! Checks for null pointer
|
Chris@101
|
113 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
|
Chris@16
|
114 //! Checks for null pointer
|
Chris@101
|
115 bool operator! () const BOOST_NOEXCEPT { return !m_pElement; }
|
Chris@16
|
116
|
Chris@16
|
117 //! Swaps two pointers
|
Chris@101
|
118 void swap(locking_ptr& that) BOOST_NOEXCEPT
|
Chris@16
|
119 {
|
Chris@16
|
120 m_pElement.swap(that.m_pElement);
|
Chris@101
|
121 lockable_type* p = m_pLock;
|
Chris@16
|
122 m_pLock = that.m_pLock;
|
Chris@16
|
123 that.m_pLock = p;
|
Chris@16
|
124 }
|
Chris@16
|
125 };
|
Chris@16
|
126
|
Chris@16
|
127 //! Free raw pointer getter to assist generic programming
|
Chris@101
|
128 template< typename T, typename LockableT >
|
Chris@101
|
129 inline T* get_pointer(locking_ptr< T, LockableT > const& p) BOOST_NOEXCEPT
|
Chris@16
|
130 {
|
Chris@16
|
131 return p.get();
|
Chris@16
|
132 }
|
Chris@16
|
133 //! Free swap operation
|
Chris@101
|
134 template< typename T, typename LockableT >
|
Chris@101
|
135 inline void swap(locking_ptr< T, LockableT >& left, locking_ptr< T, LockableT >& right) BOOST_NOEXCEPT
|
Chris@16
|
136 {
|
Chris@16
|
137 left.swap(right);
|
Chris@16
|
138 }
|
Chris@16
|
139
|
Chris@16
|
140 } // namespace aux
|
Chris@16
|
141
|
Chris@16
|
142 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
143
|
Chris@16
|
144 } // namespace boost
|
Chris@16
|
145
|
Chris@16
|
146 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
147
|
Chris@16
|
148 #endif // BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
|