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