locking_ptr.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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)
  6. */
  7. /*!
  8. * \file locking_ptr.hpp
  9. * \author Andrey Semashev
  10. * \date 15.07.2009
  11. *
  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.
  14. */
  15. #ifndef BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_
  17. #include <boost/smart_ptr/shared_ptr.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/utility/explicit_operator_bool.hpp>
  20. #include <boost/log/detail/header.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. BOOST_LOG_OPEN_NAMESPACE
  26. namespace aux {
  27. //! Shared lock object to support locking_ptr
  28. struct BOOST_LOG_NO_VTABLE locking_ptr_counter_base
  29. {
  30. unsigned int m_RefCounter;
  31. locking_ptr_counter_base() : m_RefCounter(0)
  32. {
  33. }
  34. virtual ~locking_ptr_counter_base() {}
  35. virtual void lock() = 0;
  36. virtual bool try_lock() = 0;
  37. virtual void unlock() = 0;
  38. private:
  39. locking_ptr_counter_base(locking_ptr_counter_base const&);
  40. locking_ptr_counter_base& operator= (locking_ptr_counter_base const&);
  41. };
  42. struct try_lock_tag {};
  43. BOOST_CONSTEXPR_OR_CONST try_lock_tag try_lock = {};
  44. //! A pointer type that locks the backend until it's destroyed
  45. template< typename T >
  46. class locking_ptr
  47. {
  48. public:
  49. //! Pointed type
  50. typedef T element_type;
  51. private:
  52. //! The pointer to the backend
  53. shared_ptr< element_type > m_pElement;
  54. //! Reference to the shared lock control object
  55. locking_ptr_counter_base* m_pLock;
  56. public:
  57. //! Constructor
  58. locking_ptr(shared_ptr< element_type > const& p, locking_ptr_counter_base& l)
  59. : m_pElement(p), m_pLock(&l)
  60. {
  61. if (m_pLock->m_RefCounter == 0)
  62. m_pLock->lock();
  63. ++m_pLock->m_RefCounter;
  64. }
  65. //! Constructor
  66. locking_ptr(shared_ptr< element_type > const& p, locking_ptr_counter_base& l, try_lock_tag const&)
  67. : m_pElement(p), m_pLock(&l)
  68. {
  69. if (m_pLock->m_RefCounter > 0 || m_pLock->try_lock())
  70. {
  71. ++m_pLock->m_RefCounter;
  72. }
  73. else
  74. {
  75. m_pElement.reset();
  76. m_pLock = NULL;
  77. }
  78. }
  79. //! Copy constructor
  80. locking_ptr(locking_ptr const& that) : m_pElement(that.m_pElement), m_pLock(that.m_pLock)
  81. {
  82. if (m_pLock)
  83. ++m_pLock->m_RefCounter;
  84. }
  85. //! Destructor
  86. ~locking_ptr()
  87. {
  88. if (m_pLock && --m_pLock->m_RefCounter == 0)
  89. m_pLock->unlock();
  90. }
  91. //! Assignment
  92. locking_ptr& operator= (locking_ptr that)
  93. {
  94. this->swap(that);
  95. return *this;
  96. }
  97. //! Indirection
  98. element_type* operator-> () const { return m_pElement.get(); }
  99. //! Dereferencing
  100. element_type& operator* () const { return *m_pElement; }
  101. //! Accessor to the raw pointer
  102. element_type* get() const { return m_pElement.get(); }
  103. //! Checks for null pointer
  104. BOOST_EXPLICIT_OPERATOR_BOOL()
  105. //! Checks for null pointer
  106. bool operator! () const { return !m_pElement; }
  107. //! Swaps two pointers
  108. void swap(locking_ptr& that)
  109. {
  110. m_pElement.swap(that.m_pElement);
  111. register locking_ptr_counter_base* p = m_pLock;
  112. m_pLock = that.m_pLock;
  113. that.m_pLock = p;
  114. }
  115. };
  116. //! Free raw pointer getter to assist generic programming
  117. template< typename T >
  118. inline T* get_pointer(locking_ptr< T > const& p)
  119. {
  120. return p.get();
  121. }
  122. //! Free swap operation
  123. template< typename T >
  124. inline void swap(locking_ptr< T >& left, locking_ptr< T >& right)
  125. {
  126. left.swap(right);
  127. }
  128. } // namespace aux
  129. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  130. } // namespace boost
  131. #include <boost/log/detail/footer.hpp>
  132. #endif // BOOST_LOG_DETAIL_LOCKING_PTR_HPP_INCLUDED_