| 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 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | #include <boost/detail/atomic_count.hpp> |
|---|
| 12 | #include <boost/config.hpp> // BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | namespace boost |
|---|
| 17 | { |
|---|
| 18 | namespace statechart |
|---|
| 19 | { |
|---|
| 20 | namespace detail |
|---|
| 21 | { |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | template< bool NeedsLocking > |
|---|
| 26 | struct count_base |
|---|
| 27 | { |
|---|
| 28 | count_base() : count_( 0 ) {} |
|---|
| 29 | mutable boost::detail::atomic_count count_; |
|---|
| 30 | }; |
|---|
| 31 | |
|---|
| 32 | template<> |
|---|
| 33 | struct count_base< false > |
|---|
| 34 | { |
|---|
| 35 | count_base() : count_( 0 ) {} |
|---|
| 36 | mutable long count_; |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 40 | template< bool NeedsLocking = true > |
|---|
| 41 | class counted_base : private count_base< NeedsLocking > |
|---|
| 42 | { |
|---|
| 43 | typedef count_base< NeedsLocking > base_type; |
|---|
| 44 | public: |
|---|
| 45 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 46 | bool ref_counted() const |
|---|
| 47 | { |
|---|
| 48 | return base_type::count_ != 0; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | long ref_count() const |
|---|
| 52 | { |
|---|
| 53 | return base_type::count_; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | protected: |
|---|
| 57 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 58 | counted_base() {} |
|---|
| 59 | ~counted_base() {} |
|---|
| 60 | |
|---|
| 61 | // do nothing copy implementation is intentional (the number of |
|---|
| 62 | // referencing pointers of the source and the destination is not changed |
|---|
| 63 | // through the copy operation) |
|---|
| 64 | counted_base( const counted_base & ) {} |
|---|
| 65 | counted_base & operator=( const counted_base & ) { return *this; } |
|---|
| 66 | |
|---|
| 67 | public: |
|---|
| 68 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 69 | // The following declarations should be private. |
|---|
| 70 | // They are only public because many compilers lack template friends. |
|---|
| 71 | ////////////////////////////////////////////////////////////////////////// |
|---|
| 72 | void add_ref() const |
|---|
| 73 | { |
|---|
| 74 | ++base_type::count_; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | bool release() const |
|---|
| 78 | { |
|---|
| 79 | BOOST_ASSERT( base_type::count_ > 0 ); |
|---|
| 80 | return --base_type::count_ == 0; |
|---|
| 81 | } |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | } // namespace detail |
|---|
| 87 | } // namespace statechart |
|---|
| 88 | } // namespace boost |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | #endif |
|---|