Chris@16
|
1 #ifndef BOOST_STATECHART_DETAIL_STATE_BASE_HPP_INCLUDED
|
Chris@16
|
2 #define BOOST_STATECHART_DETAIL_STATE_BASE_HPP_INCLUDED
|
Chris@16
|
3 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
4 // Copyright 2002-2008 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/statechart/result.hpp>
|
Chris@16
|
12 #include <boost/statechart/event.hpp>
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/statechart/detail/counted_base.hpp>
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/intrusive_ptr.hpp>
|
Chris@16
|
17 #include <boost/noncopyable.hpp>
|
Chris@16
|
18 #include <boost/assert.hpp>
|
Chris@16
|
19 #include <boost/config.hpp> // BOOST_MSVC
|
Chris@16
|
20
|
Chris@16
|
21 #include <boost/detail/workaround.hpp>
|
Chris@16
|
22 #include <boost/detail/allocator_utilities.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 #ifdef BOOST_MSVC
|
Chris@16
|
25 # pragma warning( push )
|
Chris@16
|
26 # pragma warning( disable: 4702 ) // unreachable code (in release mode only)
|
Chris@16
|
27 #endif
|
Chris@16
|
28
|
Chris@16
|
29 #include <list>
|
Chris@16
|
30
|
Chris@16
|
31 #ifdef BOOST_MSVC
|
Chris@16
|
32 # pragma warning( pop )
|
Chris@16
|
33 #endif
|
Chris@16
|
34
|
Chris@16
|
35
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost
|
Chris@16
|
38 {
|
Chris@16
|
39 namespace statechart
|
Chris@16
|
40 {
|
Chris@16
|
41 namespace detail
|
Chris@16
|
42 {
|
Chris@16
|
43
|
Chris@16
|
44
|
Chris@16
|
45
|
Chris@16
|
46 template< class Allocator, class RttiPolicy >
|
Chris@16
|
47 class leaf_state;
|
Chris@16
|
48 template< class Allocator, class RttiPolicy >
|
Chris@16
|
49 class node_state_base;
|
Chris@16
|
50
|
Chris@16
|
51 typedef unsigned char orthogonal_position_type;
|
Chris@16
|
52
|
Chris@16
|
53
|
Chris@16
|
54
|
Chris@16
|
55 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
56 template< class Allocator, class RttiPolicy >
|
Chris@16
|
57 class state_base :
|
Chris@16
|
58 #ifndef NDEBUG
|
Chris@16
|
59 noncopyable,
|
Chris@16
|
60 #endif
|
Chris@16
|
61 public RttiPolicy::template rtti_base_type<
|
Chris@16
|
62 // Derived class objects will be created, handled and destroyed by exactly
|
Chris@16
|
63 // one thread --> locking is not necessary
|
Chris@16
|
64 counted_base< false > >
|
Chris@16
|
65 {
|
Chris@16
|
66 typedef typename RttiPolicy::template rtti_base_type<
|
Chris@16
|
67 counted_base< false > > base_type;
|
Chris@16
|
68
|
Chris@16
|
69 public:
|
Chris@16
|
70 //////////////////////////////////////////////////////////////////////////
|
Chris@16
|
71 void exit() {}
|
Chris@16
|
72
|
Chris@16
|
73 virtual const state_base * outer_state_ptr() const = 0;
|
Chris@16
|
74
|
Chris@16
|
75 protected:
|
Chris@16
|
76 //////////////////////////////////////////////////////////////////////////
|
Chris@16
|
77 state_base( typename RttiPolicy::id_provider_type idProvider ) :
|
Chris@16
|
78 base_type( idProvider ),
|
Chris@16
|
79 deferredEvents_( false )
|
Chris@16
|
80 {
|
Chris@16
|
81 }
|
Chris@16
|
82
|
Chris@16
|
83 #if BOOST_WORKAROUND( __GNUC__, BOOST_TESTED_AT( 4 ) )
|
Chris@16
|
84 // We make the destructor virtual for GCC because with this compiler there
|
Chris@16
|
85 // is currently no way to disable the "has virtual functions but
|
Chris@16
|
86 // non-virtual destructor" warning on a class by class basis. Although it
|
Chris@16
|
87 // can be done on the compiler command line with -Wno-non-virtual-dtor,
|
Chris@16
|
88 // this is undesirable as this would also suppress legitimate warnings for
|
Chris@16
|
89 // types that are not states.
|
Chris@16
|
90 virtual ~state_base() {}
|
Chris@16
|
91 #else
|
Chris@16
|
92 // This destructor is not virtual for performance reasons. The library
|
Chris@16
|
93 // ensures that a state object is never deleted through a state_base
|
Chris@16
|
94 // pointer but only through a pointer to the most-derived type.
|
Chris@16
|
95 ~state_base() {}
|
Chris@16
|
96 #endif
|
Chris@16
|
97
|
Chris@16
|
98 protected:
|
Chris@16
|
99 //////////////////////////////////////////////////////////////////////////
|
Chris@16
|
100 // The following declarations should be private.
|
Chris@16
|
101 // They are only protected because many compilers lack template friends.
|
Chris@16
|
102 //////////////////////////////////////////////////////////////////////////
|
Chris@16
|
103 void defer_event()
|
Chris@16
|
104 {
|
Chris@16
|
105 deferredEvents_ = true;
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 bool deferred_events() const
|
Chris@16
|
109 {
|
Chris@16
|
110 return deferredEvents_;
|
Chris@16
|
111 }
|
Chris@16
|
112
|
Chris@16
|
113 template< class Context >
|
Chris@16
|
114 void set_context( orthogonal_position_type position, Context * pContext )
|
Chris@16
|
115 {
|
Chris@16
|
116 pContext->add_inner_state( position, this );
|
Chris@16
|
117 }
|
Chris@16
|
118
|
Chris@16
|
119 public:
|
Chris@16
|
120 //////////////////////////////////////////////////////////////////////////
|
Chris@16
|
121 // The following declarations should be private.
|
Chris@16
|
122 // They are only public because many compilers lack template friends.
|
Chris@16
|
123 //////////////////////////////////////////////////////////////////////////
|
Chris@16
|
124 virtual detail::reaction_result react_impl(
|
Chris@16
|
125 const event_base & evt,
|
Chris@16
|
126 typename RttiPolicy::id_type eventType ) = 0;
|
Chris@16
|
127
|
Chris@16
|
128 typedef intrusive_ptr< node_state_base< Allocator, RttiPolicy > >
|
Chris@16
|
129 node_state_base_ptr_type;
|
Chris@16
|
130 typedef intrusive_ptr< leaf_state< Allocator, RttiPolicy > >
|
Chris@16
|
131 leaf_state_ptr_type;
|
Chris@16
|
132 typedef std::list<
|
Chris@16
|
133 leaf_state_ptr_type,
|
Chris@16
|
134 typename boost::detail::allocator::rebind_to<
|
Chris@16
|
135 Allocator, leaf_state_ptr_type >::type
|
Chris@16
|
136 > state_list_type;
|
Chris@16
|
137
|
Chris@16
|
138 virtual void remove_from_state_list(
|
Chris@16
|
139 typename state_list_type::iterator & statesEnd,
|
Chris@16
|
140 node_state_base_ptr_type & pOutermostUnstableState,
|
Chris@16
|
141 bool performFullExit ) = 0;
|
Chris@16
|
142
|
Chris@16
|
143 private:
|
Chris@16
|
144 //////////////////////////////////////////////////////////////////////////
|
Chris@16
|
145 bool deferredEvents_;
|
Chris@16
|
146 };
|
Chris@16
|
147
|
Chris@16
|
148
|
Chris@16
|
149
|
Chris@16
|
150 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
|
Chris@16
|
151 } // namespace detail
|
Chris@16
|
152 } // namespace statechart
|
Chris@16
|
153 #endif
|
Chris@16
|
154
|
Chris@16
|
155
|
Chris@16
|
156
|
Chris@16
|
157 template< class Allocator, class RttiPolicy >
|
Chris@16
|
158 inline void intrusive_ptr_add_ref(
|
Chris@16
|
159 const ::boost::statechart::detail::state_base< Allocator, RttiPolicy > * pBase )
|
Chris@16
|
160 {
|
Chris@16
|
161 pBase->add_ref();
|
Chris@16
|
162 }
|
Chris@16
|
163
|
Chris@16
|
164 template< class Allocator, class RttiPolicy >
|
Chris@16
|
165 inline void intrusive_ptr_release(
|
Chris@16
|
166 const ::boost::statechart::detail::state_base< Allocator, RttiPolicy > * pBase )
|
Chris@16
|
167 {
|
Chris@16
|
168 if ( pBase->release() )
|
Chris@16
|
169 {
|
Chris@16
|
170 // The state_base destructor is *not* virtual for performance reasons
|
Chris@16
|
171 // but intrusive_ptr< state_base > objects are nevertheless used to point
|
Chris@16
|
172 // to states. This assert ensures that such a pointer is never the last
|
Chris@16
|
173 // one referencing a state object.
|
Chris@16
|
174 BOOST_ASSERT( false );
|
Chris@16
|
175 }
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178
|
Chris@16
|
179
|
Chris@16
|
180 #ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
|
Chris@16
|
181 } // namespace detail
|
Chris@16
|
182 } // namespace statechart
|
Chris@16
|
183 #endif
|
Chris@16
|
184
|
Chris@16
|
185
|
Chris@16
|
186
|
Chris@16
|
187 } // namespace boost
|
Chris@16
|
188
|
Chris@16
|
189
|
Chris@16
|
190
|
Chris@16
|
191 #endif
|