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