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