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 7 #ifndef BOOST_XPRESSIVE_DETAIL_UTILITY_COUNTED_BASE_HPP_EAN_04_16_2006 8 #define BOOST_XPRESSIVE_DETAIL_UTILITY_COUNTED_BASE_HPP_EAN_04_16_2006 9 10 #include <boost/assert.hpp> 11 #include <boost/checked_delete.hpp> 12 #include <boost/detail/atomic_count.hpp> 13 14 namespace boost { namespace xpressive { namespace detail 15 { 16 template<typename Derived> 17 struct counted_base_access; 18 19 ////////////////////////////////////////////////////////////////////////////// 20 // counted_base 21 template<typename Derived> 22 struct counted_base 23 { use_countboost::xpressive::detail::counted_base24 long use_count() const 25 { 26 return this->count_; 27 } 28 29 protected: counted_baseboost::xpressive::detail::counted_base30 counted_base() 31 : count_(0) 32 { 33 } 34 counted_baseboost::xpressive::detail::counted_base35 counted_base(counted_base<Derived> const &) 36 : count_(0) 37 { 38 } 39 operator =boost::xpressive::detail::counted_base40 counted_base &operator =(counted_base<Derived> const &) 41 { 42 return *this; 43 } 44 45 private: 46 friend struct counted_base_access<Derived>; 47 mutable boost::detail::atomic_count count_; 48 }; 49 50 ////////////////////////////////////////////////////////////////////////////// 51 // counted_base_access 52 template<typename Derived> 53 struct counted_base_access 54 { add_refboost::xpressive::detail::counted_base_access55 static void add_ref(counted_base<Derived> const *that) 56 { 57 ++that->count_; 58 } 59 releaseboost::xpressive::detail::counted_base_access60 static void release(counted_base<Derived> const *that) 61 { 62 BOOST_ASSERT(0 < that->count_); 63 if(0 == --that->count_) 64 { 65 boost::checked_delete(static_cast<Derived const *>(that)); 66 } 67 } 68 }; 69 70 template<typename Derived> intrusive_ptr_add_ref(counted_base<Derived> const * that)71 inline void intrusive_ptr_add_ref(counted_base<Derived> const *that) 72 { 73 counted_base_access<Derived>::add_ref(that); 74 } 75 76 template<typename Derived> intrusive_ptr_release(counted_base<Derived> const * that)77 inline void intrusive_ptr_release(counted_base<Derived> const *that) 78 { 79 counted_base_access<Derived>::release(that); 80 } 81 82 }}} // namespace boost::xpressive::detail 83 84 #endif 85