counted_base.hpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #ifndef BOOST_STATECHART_DETAIL_COUNTED_BASE_HPP_INCLUDED
  2. #define BOOST_STATECHART_DETAIL_COUNTED_BASE_HPP_INCLUDED
  3. //////////////////////////////////////////////////////////////////////////////
  4. // Copyright 2002-2006 Andreas Huber Doenni
  5. // Distributed under the Boost Software License, Version 1.0. (See accompany-
  6. // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //////////////////////////////////////////////////////////////////////////////
  8. #include <boost/detail/atomic_count.hpp>
  9. #include <boost/config.hpp> // BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  10. namespace boost
  11. {
  12. namespace statechart
  13. {
  14. namespace detail
  15. {
  16. template< bool NeedsLocking >
  17. struct count_base
  18. {
  19. count_base() : count_( 0 ) {}
  20. mutable boost::detail::atomic_count count_;
  21. };
  22. template<>
  23. struct count_base< false >
  24. {
  25. count_base() : count_( 0 ) {}
  26. mutable long count_;
  27. };
  28. //////////////////////////////////////////////////////////////////////////////
  29. template< bool NeedsLocking = true >
  30. class counted_base : private count_base< NeedsLocking >
  31. {
  32. typedef count_base< NeedsLocking > base_type;
  33. public:
  34. //////////////////////////////////////////////////////////////////////////
  35. bool ref_counted() const
  36. {
  37. return base_type::count_ != 0;
  38. }
  39. long ref_count() const
  40. {
  41. return base_type::count_;
  42. }
  43. protected:
  44. //////////////////////////////////////////////////////////////////////////
  45. counted_base() {}
  46. ~counted_base() {}
  47. // do nothing copy implementation is intentional (the number of
  48. // referencing pointers of the source and the destination is not changed
  49. // through the copy operation)
  50. counted_base( const counted_base & ) : base_type() {}
  51. counted_base & operator=( const counted_base & ) { return *this; }
  52. public:
  53. //////////////////////////////////////////////////////////////////////////
  54. // The following declarations should be private.
  55. // They are only public because many compilers lack template friends.
  56. //////////////////////////////////////////////////////////////////////////
  57. void add_ref() const
  58. {
  59. ++base_type::count_;
  60. }
  61. bool release() const
  62. {
  63. BOOST_ASSERT( base_type::count_ > 0 );
  64. return --base_type::count_ == 0;
  65. }
  66. };
  67. } // namespace detail
  68. } // namespace statechart
  69. } // namespace boost
  70. #endif