Chris@16: /* Chris@16: * Copyright Andrey Semashev 2007 - 2013. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file locking_ptr.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 15.07.2009 Chris@16: * Chris@16: * This header is the Boost.Log library implementation, see the library documentation Chris@16: * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace aux { Chris@16: Chris@16: //! Shared lock object to support locking_ptr Chris@16: struct BOOST_LOG_NO_VTABLE locking_ptr_counter_base Chris@16: { Chris@16: unsigned int m_RefCounter; Chris@16: Chris@16: locking_ptr_counter_base() : m_RefCounter(0) Chris@16: { Chris@16: } Chris@16: Chris@16: virtual ~locking_ptr_counter_base() {} Chris@16: virtual void lock() = 0; Chris@16: virtual bool try_lock() = 0; Chris@16: virtual void unlock() = 0; Chris@16: Chris@16: private: Chris@16: locking_ptr_counter_base(locking_ptr_counter_base const&); Chris@16: locking_ptr_counter_base& operator= (locking_ptr_counter_base const&); Chris@16: }; Chris@16: Chris@16: struct try_lock_tag {}; Chris@16: BOOST_CONSTEXPR_OR_CONST try_lock_tag try_lock = {}; Chris@16: Chris@16: //! A pointer type that locks the backend until it's destroyed Chris@16: template< typename T > Chris@16: class locking_ptr Chris@16: { Chris@16: public: Chris@16: //! Pointed type Chris@16: typedef T element_type; Chris@16: Chris@16: private: Chris@16: //! The pointer to the backend Chris@16: shared_ptr< element_type > m_pElement; Chris@16: //! Reference to the shared lock control object Chris@16: locking_ptr_counter_base* m_pLock; Chris@16: Chris@16: public: Chris@16: //! Constructor Chris@16: locking_ptr(shared_ptr< element_type > const& p, locking_ptr_counter_base& l) Chris@16: : m_pElement(p), m_pLock(&l) Chris@16: { Chris@16: if (m_pLock->m_RefCounter == 0) Chris@16: m_pLock->lock(); Chris@16: ++m_pLock->m_RefCounter; Chris@16: } Chris@16: //! Constructor Chris@16: locking_ptr(shared_ptr< element_type > const& p, locking_ptr_counter_base& l, try_lock_tag const&) Chris@16: : m_pElement(p), m_pLock(&l) Chris@16: { Chris@16: if (m_pLock->m_RefCounter > 0 || m_pLock->try_lock()) Chris@16: { Chris@16: ++m_pLock->m_RefCounter; Chris@16: } Chris@16: else Chris@16: { Chris@16: m_pElement.reset(); Chris@16: m_pLock = NULL; Chris@16: } Chris@16: } Chris@16: //! Copy constructor Chris@16: locking_ptr(locking_ptr const& that) : m_pElement(that.m_pElement), m_pLock(that.m_pLock) Chris@16: { Chris@16: if (m_pLock) Chris@16: ++m_pLock->m_RefCounter; Chris@16: } Chris@16: //! Destructor Chris@16: ~locking_ptr() Chris@16: { Chris@16: if (m_pLock && --m_pLock->m_RefCounter == 0) Chris@16: m_pLock->unlock(); Chris@16: } Chris@16: Chris@16: //! Assignment Chris@16: locking_ptr& operator= (locking_ptr that) Chris@16: { Chris@16: this->swap(that); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //! Indirection Chris@16: element_type* operator-> () const { return m_pElement.get(); } Chris@16: //! Dereferencing Chris@16: element_type& operator* () const { return *m_pElement; } Chris@16: Chris@16: //! Accessor to the raw pointer Chris@16: element_type* get() const { return m_pElement.get(); } Chris@16: Chris@16: //! Checks for null pointer Chris@16: BOOST_EXPLICIT_OPERATOR_BOOL() Chris@16: //! Checks for null pointer Chris@16: bool operator! () const { return !m_pElement; } Chris@16: Chris@16: //! Swaps two pointers Chris@16: void swap(locking_ptr& that) Chris@16: { Chris@16: m_pElement.swap(that.m_pElement); Chris@16: register locking_ptr_counter_base* p = m_pLock; Chris@16: m_pLock = that.m_pLock; Chris@16: that.m_pLock = p; Chris@16: } Chris@16: }; Chris@16: Chris@16: //! Free raw pointer getter to assist generic programming Chris@16: template< typename T > Chris@16: inline T* get_pointer(locking_ptr< T > const& p) Chris@16: { Chris@16: return p.get(); Chris@16: } Chris@16: //! Free swap operation Chris@16: template< typename T > Chris@16: inline void swap(locking_ptr< T >& left, locking_ptr< T >& right) Chris@16: { Chris@16: left.swap(right); Chris@16: } Chris@16: Chris@16: } // namespace aux Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_