intrusive_ref_counter.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 intrusive_ref_counter.hpp
  9. * \author Andrey Semashev
  10. * \date 12.03.2009
  11. *
  12. * This header contains a reference counter class for \c intrusive_ptr.
  13. */
  14. #ifndef BOOST_SMART_PTR_INTRUSIVE_REF_COUNTER_HPP_INCLUDED_
  15. #define BOOST_SMART_PTR_INTRUSIVE_REF_COUNTER_HPP_INCLUDED_
  16. #include <boost/config.hpp>
  17. #include <boost/smart_ptr/detail/atomic_count.hpp>
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. #pragma once
  20. #endif
  21. #if defined(_MSC_VER)
  22. #pragma warning(push)
  23. // This is a bogus MSVC warning, which is flagged by friend declarations of intrusive_ptr_add_ref and intrusive_ptr_release in intrusive_ref_counter:
  24. // 'name' : the inline specifier cannot be used when a friend declaration refers to a specialization of a function template
  25. // Note that there is no inline specifier in the declarations.
  26. #pragma warning(disable: 4396)
  27. #endif
  28. namespace boost {
  29. namespace sp_adl_block {
  30. /*!
  31. * \brief Thread unsafe reference counter policy for \c intrusive_ref_counter
  32. *
  33. * The policy instructs the \c intrusive_ref_counter base class to implement
  34. * a reference counter suitable for single threaded use only. Pointers to the same
  35. * object with this kind of reference counter must not be used by different threads.
  36. */
  37. struct thread_unsafe_counter
  38. {
  39. typedef unsigned int type;
  40. static unsigned int load(unsigned int const& counter) BOOST_NOEXCEPT
  41. {
  42. return counter;
  43. }
  44. static void increment(unsigned int& counter) BOOST_NOEXCEPT
  45. {
  46. ++counter;
  47. }
  48. static unsigned int decrement(unsigned int& counter) BOOST_NOEXCEPT
  49. {
  50. return --counter;
  51. }
  52. };
  53. /*!
  54. * \brief Thread safe reference counter policy for \c intrusive_ref_counter
  55. *
  56. * The policy instructs the \c intrusive_ref_counter base class to implement
  57. * a thread-safe reference counter, if the target platform supports multithreading.
  58. */
  59. struct thread_safe_counter
  60. {
  61. typedef boost::detail::atomic_count type;
  62. static unsigned int load(boost::detail::atomic_count const& counter) BOOST_NOEXCEPT
  63. {
  64. return static_cast< unsigned int >(static_cast< long >(counter));
  65. }
  66. static void increment(boost::detail::atomic_count& counter) BOOST_NOEXCEPT
  67. {
  68. ++counter;
  69. }
  70. static unsigned int decrement(boost::detail::atomic_count& counter) BOOST_NOEXCEPT
  71. {
  72. return --counter;
  73. }
  74. };
  75. template< typename DerivedT, typename CounterPolicyT = thread_safe_counter >
  76. class intrusive_ref_counter;
  77. template< typename DerivedT, typename CounterPolicyT >
  78. void intrusive_ptr_add_ref(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) BOOST_NOEXCEPT;
  79. template< typename DerivedT, typename CounterPolicyT >
  80. void intrusive_ptr_release(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) BOOST_NOEXCEPT;
  81. /*!
  82. * \brief A reference counter base class
  83. *
  84. * This base class can be used with user-defined classes to add support
  85. * for \c intrusive_ptr. The class contains a reference counter defined by the \c CounterPolicyT.
  86. * Upon releasing the last \c intrusive_ptr referencing the object
  87. * derived from the \c intrusive_ref_counter class, operator \c delete
  88. * is automatically called on the pointer to the object.
  89. *
  90. * The other template parameter, \c DerivedT, is the user's class that derives from \c intrusive_ref_counter.
  91. */
  92. template< typename DerivedT, typename CounterPolicyT >
  93. class intrusive_ref_counter
  94. {
  95. private:
  96. //! Reference counter type
  97. typedef typename CounterPolicyT::type counter_type;
  98. //! Reference counter
  99. mutable counter_type m_ref_counter;
  100. public:
  101. /*!
  102. * Default constructor
  103. *
  104. * \post <tt>use_count() == 0</tt>
  105. */
  106. intrusive_ref_counter() BOOST_NOEXCEPT : m_ref_counter(0)
  107. {
  108. }
  109. /*!
  110. * Copy constructor
  111. *
  112. * \post <tt>use_count() == 0</tt>
  113. */
  114. intrusive_ref_counter(intrusive_ref_counter const&) BOOST_NOEXCEPT : m_ref_counter(0)
  115. {
  116. }
  117. /*!
  118. * Assignment
  119. *
  120. * \post The reference counter is not modified after assignment
  121. */
  122. intrusive_ref_counter& operator= (intrusive_ref_counter const&) BOOST_NOEXCEPT { return *this; }
  123. /*!
  124. * \return The reference counter
  125. */
  126. unsigned int use_count() const BOOST_NOEXCEPT
  127. {
  128. return CounterPolicyT::load(m_ref_counter);
  129. }
  130. protected:
  131. /*!
  132. * Destructor
  133. */
  134. BOOST_DEFAULTED_FUNCTION(~intrusive_ref_counter(), {})
  135. friend void intrusive_ptr_add_ref< DerivedT, CounterPolicyT >(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) BOOST_NOEXCEPT;
  136. friend void intrusive_ptr_release< DerivedT, CounterPolicyT >(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) BOOST_NOEXCEPT;
  137. };
  138. template< typename DerivedT, typename CounterPolicyT >
  139. inline void intrusive_ptr_add_ref(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) BOOST_NOEXCEPT
  140. {
  141. CounterPolicyT::increment(p->m_ref_counter);
  142. }
  143. template< typename DerivedT, typename CounterPolicyT >
  144. inline void intrusive_ptr_release(const intrusive_ref_counter< DerivedT, CounterPolicyT >* p) BOOST_NOEXCEPT
  145. {
  146. if (CounterPolicyT::decrement(p->m_ref_counter) == 0)
  147. delete static_cast< const DerivedT* >(p);
  148. }
  149. } // namespace sp_adl_block
  150. using sp_adl_block::intrusive_ref_counter;
  151. using sp_adl_block::thread_unsafe_counter;
  152. using sp_adl_block::thread_safe_counter;
  153. } // namespace boost
  154. #if defined(_MSC_VER)
  155. #pragma warning(pop)
  156. #endif
  157. #endif // BOOST_SMART_PTR_INTRUSIVE_REF_COUNTER_HPP_INCLUDED_