counted_base.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //////////////////////////////////////////////////////////////////////////////
  2. // (c) Copyright Andreas Huber Doenni 2002-2005, Eric Niebler 2006
  3. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  4. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. //////////////////////////////////////////////////////////////////////////////
  6. #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_COUNTED_BASE_HPP_EAN_04_16_2006
  7. #define BOOST_XPRESSIVE_DETAIL_UTILITY_COUNTED_BASE_HPP_EAN_04_16_2006
  8. #include <boost/assert.hpp>
  9. #include <boost/checked_delete.hpp>
  10. #include <boost/detail/atomic_count.hpp>
  11. namespace boost { namespace xpressive { namespace detail
  12. {
  13. template<typename Derived>
  14. struct counted_base_access;
  15. //////////////////////////////////////////////////////////////////////////////
  16. // counted_base
  17. template<typename Derived>
  18. struct counted_base
  19. {
  20. long use_count() const
  21. {
  22. return this->count_;
  23. }
  24. protected:
  25. counted_base()
  26. : count_(0)
  27. {
  28. }
  29. counted_base(counted_base<Derived> const &)
  30. : count_(0)
  31. {
  32. }
  33. counted_base &operator =(counted_base<Derived> const &)
  34. {
  35. return *this;
  36. }
  37. private:
  38. friend struct counted_base_access<Derived>;
  39. mutable boost::detail::atomic_count count_;
  40. };
  41. //////////////////////////////////////////////////////////////////////////////
  42. // counted_base_access
  43. template<typename Derived>
  44. struct counted_base_access
  45. {
  46. static void add_ref(counted_base<Derived> const *that)
  47. {
  48. ++that->count_;
  49. }
  50. static void release(counted_base<Derived> const *that)
  51. {
  52. BOOST_ASSERT(0 < that->count_);
  53. if(0 == --that->count_)
  54. {
  55. boost::checked_delete(static_cast<Derived const *>(that));
  56. }
  57. }
  58. };
  59. template<typename Derived>
  60. inline void intrusive_ptr_add_ref(counted_base<Derived> const *that)
  61. {
  62. counted_base_access<Derived>::add_ref(that);
  63. }
  64. template<typename Derived>
  65. inline void intrusive_ptr_release(counted_base<Derived> const *that)
  66. {
  67. counted_base_access<Derived>::release(that);
  68. }
  69. }}} // namespace boost::xpressive::detail
  70. #endif