123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- #ifndef BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
- #define BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
- #include <cstddef>
- #include <boost/move/core.hpp>
- #include <boost/smart_ptr/shared_ptr.hpp>
- #include <boost/thread/lock_options.hpp>
- #include <boost/core/explicit_operator_bool.hpp>
- #include <boost/log/detail/config.hpp>
- #include <boost/log/detail/header.hpp>
- #ifdef BOOST_HAS_PRAGMA_ONCE
- #pragma once
- #endif
- namespace boost {
- BOOST_LOG_OPEN_NAMESPACE
- namespace aux {
- template< typename T, typename LockableT >
- class locking_ptr
- {
- typedef locking_ptr this_type;
- BOOST_COPYABLE_AND_MOVABLE_ALT(this_type)
- public:
-
- typedef T element_type;
- private:
-
- typedef LockableT lockable_type;
- private:
-
- shared_ptr< element_type > m_pElement;
-
- lockable_type* m_pLock;
- public:
-
- locking_ptr() BOOST_NOEXCEPT : m_pLock(NULL)
- {
- }
-
- locking_ptr(shared_ptr< element_type > const& p, lockable_type& l) : m_pElement(p), m_pLock(&l)
- {
- m_pLock->lock();
- }
-
- locking_ptr(shared_ptr< element_type > const& p, lockable_type& l, try_to_lock_t const&) : m_pElement(p), m_pLock(&l)
- {
- if (!m_pLock->try_lock())
- {
- m_pElement.reset();
- m_pLock = NULL;
- }
- }
-
- locking_ptr(locking_ptr const& that) : m_pElement(that.m_pElement), m_pLock(that.m_pLock)
- {
- if (m_pLock)
- m_pLock->lock();
- }
-
- locking_ptr(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT : m_pLock(that.m_pLock)
- {
- m_pElement.swap(that.m_pElement);
- that.m_pLock = NULL;
- }
-
- ~locking_ptr()
- {
- if (m_pLock)
- m_pLock->unlock();
- }
-
- locking_ptr& operator= (locking_ptr that) BOOST_NOEXCEPT
- {
- this->swap(that);
- return *this;
- }
-
- element_type* operator-> () const BOOST_NOEXCEPT { return m_pElement.get(); }
-
- element_type& operator* () const BOOST_NOEXCEPT { return *m_pElement; }
-
- element_type* get() const BOOST_NOEXCEPT { return m_pElement.get(); }
-
- BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
-
- bool operator! () const BOOST_NOEXCEPT { return !m_pElement; }
-
- void swap(locking_ptr& that) BOOST_NOEXCEPT
- {
- m_pElement.swap(that.m_pElement);
- lockable_type* p = m_pLock;
- m_pLock = that.m_pLock;
- that.m_pLock = p;
- }
- };
- template< typename T, typename LockableT >
- inline T* get_pointer(locking_ptr< T, LockableT > const& p) BOOST_NOEXCEPT
- {
- return p.get();
- }
- template< typename T, typename LockableT >
- inline void swap(locking_ptr< T, LockableT >& left, locking_ptr< T, LockableT >& right) BOOST_NOEXCEPT
- {
- left.swap(right);
- }
- }
- BOOST_LOG_CLOSE_NAMESPACE
- }
- #include <boost/log/detail/footer.hpp>
- #endif
|