2 * Copyright Andrey Semashev 2007 - 2015.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file locking_ptr.hpp
9 * \author Andrey Semashev
12 * This header is the Boost.Log library implementation, see the library documentation
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
16 #ifndef BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
20 #include <boost/move/core.hpp>
21 #include <boost/smart_ptr/shared_ptr.hpp>
22 #include <boost/thread/lock_options.hpp>
23 #include <boost/core/explicit_operator_bool.hpp>
24 #include <boost/log/detail/config.hpp>
25 #include <boost/log/detail/header.hpp>
27 #ifdef BOOST_HAS_PRAGMA_ONCE
33 BOOST_LOG_OPEN_NAMESPACE
37 //! A pointer type that locks the backend until it's destroyed
38 template< typename T, typename LockableT >
41 typedef locking_ptr this_type;
42 BOOST_COPYABLE_AND_MOVABLE_ALT(this_type)
46 typedef T element_type;
50 typedef LockableT lockable_type;
53 //! The pointer to the backend
54 shared_ptr< element_type > m_pElement;
55 //! Reference to the shared lock control object
56 lockable_type* m_pLock;
59 //! Default constructor
60 locking_ptr() BOOST_NOEXCEPT : m_pLock(NULL)
64 locking_ptr(shared_ptr< element_type > const& p, lockable_type& l) : m_pElement(p), m_pLock(&l)
69 locking_ptr(shared_ptr< element_type > const& p, lockable_type& l, try_to_lock_t const&) : m_pElement(p), m_pLock(&l)
71 if (!m_pLock->try_lock())
78 locking_ptr(locking_ptr const& that) : m_pElement(that.m_pElement), m_pLock(that.m_pLock)
84 locking_ptr(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT : m_pLock(that.m_pLock)
86 m_pElement.swap(that.m_pElement);
98 locking_ptr& operator= (locking_ptr that) BOOST_NOEXCEPT
105 element_type* operator-> () const BOOST_NOEXCEPT { return m_pElement.get(); }
107 element_type& operator* () const BOOST_NOEXCEPT { return *m_pElement; }
109 //! Accessor to the raw pointer
110 element_type* get() const BOOST_NOEXCEPT { return m_pElement.get(); }
112 //! Checks for null pointer
113 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
114 //! Checks for null pointer
115 bool operator! () const BOOST_NOEXCEPT { return !m_pElement; }
117 //! Swaps two pointers
118 void swap(locking_ptr& that) BOOST_NOEXCEPT
120 m_pElement.swap(that.m_pElement);
121 lockable_type* p = m_pLock;
122 m_pLock = that.m_pLock;
127 //! Free raw pointer getter to assist generic programming
128 template< typename T, typename LockableT >
129 inline T* get_pointer(locking_ptr< T, LockableT > const& p) BOOST_NOEXCEPT
133 //! Free swap operation
134 template< typename T, typename LockableT >
135 inline void swap(locking_ptr< T, LockableT >& left, locking_ptr< T, LockableT >& right) BOOST_NOEXCEPT
142 BOOST_LOG_CLOSE_NAMESPACE // namespace log
146 #include <boost/log/detail/footer.hpp>
148 #endif // BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_