sl@0: #ifndef BOOST_STATECHART_STATE_MACHINE_HPP_INCLUDED sl@0: #define BOOST_STATECHART_STATE_MACHINE_HPP_INCLUDED sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: // Copyright 2002-2006 Andreas Huber Doenni sl@0: // Distributed under the Boost Software License, Version 1.0. (See accompany- sl@0: // ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include // boost::polymorphic_downcast sl@0: // BOOST_NO_EXCEPTIONS, BOOST_MSVC, BOOST_MSVC_STD_ITERATOR sl@0: #include sl@0: sl@0: #include sl@0: sl@0: #ifdef BOOST_MSVC sl@0: # pragma warning( push ) sl@0: # pragma warning( disable: 4702 ) // unreachable code (in release mode only) sl@0: #endif sl@0: sl@0: #include sl@0: sl@0: #ifdef BOOST_MSVC sl@0: # pragma warning( pop ) sl@0: #endif sl@0: sl@0: #include // std::allocator sl@0: #include // std::bad_cast sl@0: #include // std::less sl@0: #include sl@0: sl@0: sl@0: sl@0: #ifdef BOOST_MSVC sl@0: // We permanently turn off the following level 4 warnings because users will sl@0: // have to do so themselves anyway if we turn them back on sl@0: # pragma warning( disable: 4511 ) // copy constructor could not be generated sl@0: # pragma warning( disable: 4512 ) // assignment op could not be generated sl@0: #endif sl@0: sl@0: sl@0: sl@0: namespace boost sl@0: { sl@0: namespace statechart sl@0: { sl@0: namespace detail sl@0: { sl@0: sl@0: sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: template< class StateBaseType, class EventBaseType, class IdType > sl@0: class send_function sl@0: { sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: send_function( sl@0: StateBaseType & toState, sl@0: const EventBaseType & evt, sl@0: IdType eventType sl@0: ) : sl@0: toState_( toState ), evt_( evt ), eventType_( eventType ) sl@0: { sl@0: } sl@0: sl@0: result operator()() sl@0: { sl@0: return detail::result_utility::make_result( sl@0: toState_.react_impl( evt_, eventType_ ) ); sl@0: } sl@0: sl@0: private: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: StateBaseType & toState_; sl@0: const EventBaseType & evt_; sl@0: IdType eventType_; sl@0: }; sl@0: sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: struct state_cast_impl_pointer_target sl@0: { sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: template< class StateBaseType > sl@0: static const StateBaseType * deref_if_necessary( sl@0: const StateBaseType * pState ) sl@0: { sl@0: return pState; sl@0: } sl@0: sl@0: template< class Target, class IdType > sl@0: static IdType type_id() sl@0: { sl@0: Target p = 0; sl@0: return type_id_impl< IdType >( p ); sl@0: } sl@0: sl@0: static bool found( const void * pFound ) sl@0: { sl@0: return pFound != 0; sl@0: } sl@0: sl@0: template< class Target > sl@0: static Target not_found() sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: private: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: template< class IdType, class Type > sl@0: static IdType type_id_impl( const Type * ) sl@0: { sl@0: return Type::static_type(); sl@0: } sl@0: }; sl@0: sl@0: struct state_cast_impl_reference_target sl@0: { sl@0: template< class StateBaseType > sl@0: static const StateBaseType & deref_if_necessary( sl@0: const StateBaseType * pState ) sl@0: { sl@0: return *pState; sl@0: } sl@0: sl@0: template< class Target, class IdType > sl@0: static IdType type_id() sl@0: { sl@0: return remove_reference< Target >::type::static_type(); sl@0: } sl@0: sl@0: template< class Dummy > sl@0: static bool found( const Dummy & ) sl@0: { sl@0: return true; sl@0: } sl@0: sl@0: template< class Target > sl@0: static Target not_found() sl@0: { sl@0: throw std::bad_cast(); sl@0: } sl@0: }; sl@0: sl@0: template< class Target > sl@0: struct state_cast_impl : public mpl::if_< sl@0: is_pointer< Target >, sl@0: state_cast_impl_pointer_target, sl@0: state_cast_impl_reference_target sl@0: >::type {}; sl@0: sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: template< class RttiPolicy > sl@0: class history_key sl@0: { sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: template< class HistorizedState > sl@0: static history_key make_history_key() sl@0: { sl@0: return history_key( sl@0: HistorizedState::context_type::static_type(), sl@0: HistorizedState::orthogonal_position::value ); sl@0: } sl@0: sl@0: typename RttiPolicy::id_type history_context_type() const sl@0: { sl@0: return historyContextType_; sl@0: } sl@0: sl@0: friend bool operator<( sl@0: const history_key & left, const history_key & right ) sl@0: { sl@0: return sl@0: std::less< typename RttiPolicy::id_type >()( sl@0: left.historyContextType_, right.historyContextType_ ) || sl@0: ( ( left.historyContextType_ == right.historyContextType_ ) && sl@0: ( left.historizedOrthogonalRegion_ < sl@0: right.historizedOrthogonalRegion_ ) ); sl@0: } sl@0: sl@0: private: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: history_key( sl@0: typename RttiPolicy::id_type historyContextType, sl@0: orthogonal_position_type historizedOrthogonalRegion sl@0: ) : sl@0: historyContextType_( historyContextType ), sl@0: historizedOrthogonalRegion_( historizedOrthogonalRegion ) sl@0: { sl@0: } sl@0: sl@0: const typename RttiPolicy::id_type historyContextType_; sl@0: const orthogonal_position_type historizedOrthogonalRegion_; sl@0: }; sl@0: sl@0: sl@0: sl@0: } // namespace detail sl@0: sl@0: sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: template< class MostDerived, sl@0: class InitialState, sl@0: class Allocator = std::allocator< void >, sl@0: class ExceptionTranslator = null_exception_translator > sl@0: class state_machine : noncopyable sl@0: { sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: typedef Allocator allocator_type; sl@0: typedef detail::rtti_policy rtti_policy_type; sl@0: typedef event_base event_base_type; sl@0: typedef intrusive_ptr< const event_base_type > event_base_ptr_type; sl@0: sl@0: void initiate() sl@0: { sl@0: terminate(); sl@0: sl@0: { sl@0: terminator guard( *this ); sl@0: detail::result_utility::get_result( translator_( sl@0: initial_construct_function( *this ), sl@0: exception_event_handler( *this ) ) ); sl@0: guard.dismiss(); sl@0: } sl@0: sl@0: process_queued_events(); sl@0: } sl@0: sl@0: void terminate() sl@0: { sl@0: terminator guard( *this ); sl@0: detail::result_utility::get_result( translator_( sl@0: terminate_function( *this ), sl@0: exception_event_handler( *this ) ) ); sl@0: guard.dismiss(); sl@0: } sl@0: sl@0: bool terminated() const sl@0: { sl@0: return pOutermostState_ == 0; sl@0: } sl@0: sl@0: void process_event( const event_base_type & evt ) sl@0: { sl@0: send_event( evt ); sl@0: process_queued_events(); sl@0: } sl@0: sl@0: template< class Target > sl@0: Target state_cast() const sl@0: { sl@0: typedef detail::state_cast_impl< Target > impl; sl@0: sl@0: for ( typename state_list_type::const_iterator pCurrentLeafState = sl@0: currentStates_.begin(); sl@0: pCurrentLeafState != currentStatesEnd_; sl@0: ++pCurrentLeafState ) sl@0: { sl@0: const state_base_type * pCurrentState( sl@0: get_pointer( *pCurrentLeafState ) ); sl@0: sl@0: while ( pCurrentState != 0 ) sl@0: { sl@0: // The unnecessary try/catch overhead for pointer targets is sl@0: // typically small compared to the cycles dynamic_cast needs sl@0: #ifndef BOOST_NO_EXCEPTIONS sl@0: try sl@0: #endif sl@0: { sl@0: Target result = dynamic_cast< Target >( sl@0: impl::deref_if_necessary( pCurrentState ) ); sl@0: sl@0: if ( impl::found( result ) ) sl@0: { sl@0: return result; sl@0: } sl@0: } sl@0: #ifndef BOOST_NO_EXCEPTIONS sl@0: // Intentionally swallow std::bad_cast exceptions. We'll throw one sl@0: // ourselves when we fail to find a state that can be cast to Target sl@0: catch ( const std::bad_cast & ) {} sl@0: #endif sl@0: sl@0: pCurrentState = pCurrentState->outer_state_ptr(); sl@0: } sl@0: } sl@0: sl@0: return impl::template not_found< Target >(); sl@0: } sl@0: sl@0: template< class Target > sl@0: Target state_downcast() const sl@0: { sl@0: typedef detail::state_cast_impl< Target > impl; sl@0: sl@0: typename rtti_policy_type::id_type targetType = sl@0: impl::template type_id< Target, rtti_policy_type::id_type >(); sl@0: sl@0: for ( typename state_list_type::const_iterator pCurrentLeafState = sl@0: currentStates_.begin(); sl@0: pCurrentLeafState != currentStatesEnd_; sl@0: ++pCurrentLeafState ) sl@0: { sl@0: const state_base_type * pCurrentState( sl@0: get_pointer( *pCurrentLeafState ) ); sl@0: sl@0: while ( pCurrentState != 0 ) sl@0: { sl@0: if ( pCurrentState->dynamic_type() == targetType ) sl@0: { sl@0: return static_cast< Target >( sl@0: impl::deref_if_necessary( pCurrentState ) ); sl@0: } sl@0: sl@0: pCurrentState = pCurrentState->outer_state_ptr(); sl@0: } sl@0: } sl@0: sl@0: return impl::template not_found< Target >(); sl@0: } sl@0: sl@0: typedef detail::state_base< allocator_type, rtti_policy_type > sl@0: state_base_type; sl@0: sl@0: class state_iterator : public std::iterator< sl@0: std::forward_iterator_tag, sl@0: state_base_type, std::ptrdiff_t sl@0: #ifndef BOOST_MSVC_STD_ITERATOR sl@0: , const state_base_type *, const state_base_type & sl@0: #endif sl@0: > sl@0: { sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////// sl@0: explicit state_iterator( sl@0: typename state_base_type::state_list_type::const_iterator sl@0: baseIterator sl@0: ) : baseIterator_( baseIterator ) {} sl@0: sl@0: const state_base_type & operator*() const { return **baseIterator_; } sl@0: const state_base_type * operator->() const sl@0: { sl@0: return &**baseIterator_; sl@0: } sl@0: sl@0: state_iterator & operator++() { ++baseIterator_; return *this; } sl@0: state_iterator operator++( int ) sl@0: { sl@0: return state_iterator( baseIterator_++ ); sl@0: } sl@0: sl@0: bool operator==( const state_iterator & right ) const sl@0: { sl@0: return baseIterator_ == right.baseIterator_; sl@0: } sl@0: bool operator!=( const state_iterator & right ) const sl@0: { sl@0: return !( *this == right ); sl@0: } sl@0: sl@0: private: sl@0: typename state_base_type::state_list_type::const_iterator sl@0: baseIterator_; sl@0: }; sl@0: sl@0: state_iterator state_begin() const sl@0: { sl@0: return state_iterator( currentStates_.begin() ); sl@0: } sl@0: sl@0: state_iterator state_end() const sl@0: { sl@0: return state_iterator( currentStatesEnd_ ); sl@0: } sl@0: sl@0: void unconsumed_event( const event_base & ) {} sl@0: sl@0: protected: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: state_machine() : sl@0: currentStatesEnd_( currentStates_.end() ), sl@0: pOutermostState_( 0 ), sl@0: isInnermostCommonOuter_( false ), sl@0: performFullExit_( true ) sl@0: { sl@0: } sl@0: sl@0: // This destructor was only made virtual so that that sl@0: // polymorphic_downcast can be used to cast to MostDerived. sl@0: virtual ~state_machine() sl@0: { sl@0: terminate_impl( false ); sl@0: } sl@0: sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: // The following declarations should be protected. sl@0: // They are only public because many compilers lack template friends. sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: void post_event( const event_base_ptr_type & pEvent ) sl@0: { sl@0: BOOST_ASSERT( get_pointer( pEvent ) != 0 ); sl@0: eventQueue_.push_back( pEvent ); sl@0: } sl@0: sl@0: void post_event( const event_base & evt ) sl@0: { sl@0: post_event( evt.intrusive_from_this() ); sl@0: } sl@0: sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: // The following declarations should be private. sl@0: // They are only public because many compilers lack template friends. sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: typedef MostDerived inner_context_type; sl@0: typedef mpl::integral_c< detail::orthogonal_position_type, 0 > sl@0: inner_orthogonal_position; sl@0: typedef mpl::integral_c< detail::orthogonal_position_type, 1 > sl@0: no_of_orthogonal_regions; sl@0: sl@0: typedef MostDerived outermost_context_type; sl@0: typedef state_machine outermost_context_base_type; sl@0: typedef state_machine * inner_context_ptr_type; sl@0: typedef typename state_base_type::node_state_base_ptr_type sl@0: node_state_base_ptr_type; sl@0: typedef typename state_base_type::leaf_state_ptr_type leaf_state_ptr_type; sl@0: typedef typename state_base_type::state_list_type state_list_type; sl@0: sl@0: typedef mpl::clear< mpl::list<> >::type context_type_list; sl@0: sl@0: typedef mpl::bool_< false > shallow_history; sl@0: typedef mpl::bool_< false > deep_history; sl@0: typedef mpl::bool_< false > inherited_deep_history; sl@0: sl@0: detail::reaction_result react_impl( sl@0: const event_base_type &, sl@0: typename rtti_policy_type::id_type ) sl@0: { sl@0: return detail::do_forward_event; sl@0: } sl@0: sl@0: void exit_impl( sl@0: inner_context_ptr_type &, sl@0: typename state_base_type::node_state_base_ptr_type &, sl@0: bool ) {} sl@0: sl@0: void set_outermost_unstable_state( sl@0: typename state_base_type::node_state_base_ptr_type & sl@0: pOutermostUnstableState ) sl@0: { sl@0: pOutermostUnstableState = 0; sl@0: } sl@0: sl@0: // Returns a reference to the context identified by the template sl@0: // parameter. This can either be _this_ object or one of its direct or sl@0: // indirect contexts. sl@0: template< class Context > sl@0: Context & context() sl@0: { sl@0: // As we are in the outermost context here, only this object can be sl@0: // returned. sl@0: return *polymorphic_downcast< MostDerived * >( this ); sl@0: } sl@0: sl@0: template< class Context > sl@0: const Context & context() const sl@0: { sl@0: // As we are in the outermost context here, only this object can be sl@0: // returned. sl@0: return *polymorphic_downcast< const MostDerived * >( this ); sl@0: } sl@0: sl@0: outermost_context_type & outermost_context() sl@0: { sl@0: return *polymorphic_downcast< MostDerived * >( this ); sl@0: } sl@0: sl@0: const outermost_context_type & outermost_context() const sl@0: { sl@0: return *polymorphic_downcast< const MostDerived * >( this ); sl@0: } sl@0: sl@0: outermost_context_base_type & outermost_context_base() sl@0: { sl@0: return *this; sl@0: } sl@0: sl@0: const outermost_context_base_type & outermost_context_base() const sl@0: { sl@0: return *this; sl@0: } sl@0: sl@0: void terminate_as_reaction( state_base_type & theState ) sl@0: { sl@0: terminate_impl( theState, performFullExit_ ); sl@0: pOutermostUnstableState_ = 0; sl@0: } sl@0: sl@0: void terminate_as_part_of_transit( state_base_type & theState ) sl@0: { sl@0: terminate_impl( theState, performFullExit_ ); sl@0: isInnermostCommonOuter_ = true; sl@0: } sl@0: sl@0: void terminate_as_part_of_transit( state_machine & ) sl@0: { sl@0: terminate_impl( *pOutermostState_, performFullExit_ ); sl@0: isInnermostCommonOuter_ = true; sl@0: } sl@0: sl@0: sl@0: template< class State > sl@0: void add( const intrusive_ptr< State > & pState ) sl@0: { sl@0: // The second dummy argument is necessary because the call to the sl@0: // overloaded function add_impl would otherwise be ambiguous. sl@0: node_state_base_ptr_type pNewOutermostUnstableStateCandidate = sl@0: add_impl( pState, *pState ); sl@0: sl@0: if ( isInnermostCommonOuter_ || sl@0: is_in_highest_orthogonal_region< State >() && sl@0: ( get_pointer( pOutermostUnstableState_ ) == sl@0: pState->State::outer_state_ptr() ) ) sl@0: { sl@0: isInnermostCommonOuter_ = false; sl@0: pOutermostUnstableState_ = pNewOutermostUnstableStateCandidate; sl@0: } sl@0: } sl@0: sl@0: sl@0: void add_inner_state( sl@0: detail::orthogonal_position_type position, sl@0: state_base_type * pOutermostState ) sl@0: { sl@0: BOOST_ASSERT( position == 0 ); sl@0: detail::avoid_unused_warning( position ); sl@0: pOutermostState_ = pOutermostState; sl@0: } sl@0: sl@0: void remove_inner_state( detail::orthogonal_position_type position ) sl@0: { sl@0: BOOST_ASSERT( position == 0 ); sl@0: detail::avoid_unused_warning( position ); sl@0: pOutermostState_ = 0; sl@0: } sl@0: sl@0: sl@0: void defer_event( sl@0: const event_base_type & evt, sl@0: const state_base_type * pForState ) sl@0: { sl@0: deferredMap_[ pForState ].push_back( evt.intrusive_from_this() ); sl@0: } sl@0: sl@0: void release_events( const state_base_type * pForState ) sl@0: { sl@0: const typename deferred_map_type::iterator pFound = sl@0: deferredMap_.find( pForState ); sl@0: sl@0: // We are not guaranteed to find an entry because a state is marked for sl@0: // having deferred events _before_ the event is actually deferred. An sl@0: // exception might be thrown during deferral. sl@0: if ( pFound != deferredMap_.end() ) sl@0: { sl@0: eventQueue_.splice( eventQueue_.end(), pFound->second ); sl@0: deferredMap_.erase( pFound ); sl@0: } sl@0: } sl@0: sl@0: sl@0: template< class HistorizedState > sl@0: void store_shallow_history() sl@0: { sl@0: // 5.2.10.6 declares that reinterpret_casting a function pointer to a sl@0: // different function pointer and back must yield the same value. The sl@0: // following reinterpret_cast is the first half of such a sequence. sl@0: store_history_impl( sl@0: shallowHistoryMap_, sl@0: history_key_type::make_history_key< HistorizedState >(), sl@0: reinterpret_cast< void (*)() >( &HistorizedState::deep_construct ) ); sl@0: } sl@0: sl@0: template< sl@0: class HistoryContext, sl@0: detail::orthogonal_position_type orthogonalPosition > sl@0: void clear_shallow_history() sl@0: { sl@0: // If you receive a sl@0: // "use of undefined type 'boost::STATIC_ASSERTION_FAILURE'" or sl@0: // similar compiler error here then you tried to clear shallow history sl@0: // for a state that does not have shallow history. That is, the state sl@0: // does not pass either statechart::has_shallow_history or sl@0: // statechart::has_full_history to its base class template. sl@0: BOOST_STATIC_ASSERT( HistoryContext::shallow_history::value ); sl@0: sl@0: typedef typename mpl::at_c< sl@0: typename HistoryContext::inner_initial_list, sl@0: orthogonalPosition >::type historized_state; sl@0: sl@0: store_history_impl( sl@0: shallowHistoryMap_, sl@0: history_key_type::make_history_key< historized_state >(), sl@0: 0 ); sl@0: } sl@0: sl@0: template< class DefaultState > sl@0: void construct_with_shallow_history( sl@0: const typename DefaultState::context_ptr_type & pContext ) sl@0: { sl@0: construct_with_history_impl< DefaultState >( sl@0: shallowHistoryMap_, pContext ); sl@0: } sl@0: sl@0: sl@0: template< class HistorizedState, class LeafState > sl@0: void store_deep_history() sl@0: { sl@0: typedef typename detail::make_context_list< sl@0: typename HistorizedState::context_type, sl@0: LeafState >::type history_context_list; sl@0: typedef detail::constructor< sl@0: history_context_list, outermost_context_base_type > constructor_type; sl@0: // 5.2.10.6 declares that reinterpret_casting a function pointer to a sl@0: // different function pointer and back must yield the same value. The sl@0: // following reinterpret_cast is the first half of such a sequence. sl@0: store_history_impl( sl@0: deepHistoryMap_, sl@0: history_key_type::make_history_key< HistorizedState >(), sl@0: reinterpret_cast< void (*)() >( &constructor_type::construct ) ); sl@0: } sl@0: sl@0: template< sl@0: class HistoryContext, sl@0: detail::orthogonal_position_type orthogonalPosition > sl@0: void clear_deep_history() sl@0: { sl@0: // If you receive a sl@0: // "use of undefined type 'boost::STATIC_ASSERTION_FAILURE'" or sl@0: // similar compiler error here then you tried to clear deep history for sl@0: // a state that does not have deep history. That is, the state does not sl@0: // pass either statechart::has_deep_history or sl@0: // statechart::has_full_history to its base class template sl@0: BOOST_STATIC_ASSERT( HistoryContext::deep_history::value ); sl@0: sl@0: typedef typename mpl::at_c< sl@0: typename HistoryContext::inner_initial_list, sl@0: orthogonalPosition >::type historized_state; sl@0: sl@0: store_history_impl( sl@0: deepHistoryMap_, sl@0: history_key_type::make_history_key< historized_state >(), sl@0: 0 ); sl@0: } sl@0: sl@0: template< class DefaultState > sl@0: void construct_with_deep_history( sl@0: const typename DefaultState::context_ptr_type & pContext ) sl@0: { sl@0: construct_with_history_impl< DefaultState >( sl@0: deepHistoryMap_, pContext ); sl@0: } sl@0: sl@0: private: // implementation sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: void initial_construct() sl@0: { sl@0: InitialState::initial_deep_construct( sl@0: *polymorphic_downcast< MostDerived * >( this ) ); sl@0: } sl@0: sl@0: class initial_construct_function sl@0: { sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////// sl@0: initial_construct_function( state_machine & machine ) : sl@0: machine_( machine ) sl@0: { sl@0: } sl@0: sl@0: result operator()() sl@0: { sl@0: machine_.initial_construct(); sl@0: return detail::result_utility::make_result( sl@0: detail::do_discard_event ); // there is nothing to be consumed sl@0: } sl@0: sl@0: private: sl@0: ////////////////////////////////////////////////////////////////////// sl@0: state_machine & machine_; sl@0: }; sl@0: friend class initial_construct_function; sl@0: sl@0: class terminate_function sl@0: { sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////// sl@0: terminate_function( state_machine & machine ) : machine_( machine ) {} sl@0: sl@0: result operator()() sl@0: { sl@0: machine_.terminate_impl( true ); sl@0: return detail::result_utility::make_result( sl@0: detail::do_discard_event ); // there is nothing to be consumed sl@0: } sl@0: sl@0: private: sl@0: ////////////////////////////////////////////////////////////////////// sl@0: state_machine & machine_; sl@0: }; sl@0: friend class terminate_function; sl@0: sl@0: template< class ExceptionEvent > sl@0: detail::reaction_result handle_exception_event( sl@0: const ExceptionEvent & exceptionEvent, sl@0: state_base_type * pCurrentState ) sl@0: { sl@0: if ( terminated() ) sl@0: { sl@0: // there is no state that could handle the exception -> bail out sl@0: throw; sl@0: } sl@0: sl@0: // If we are stable, an event handler has thrown. sl@0: // Otherwise, either a state constructor, a transition action or an exit sl@0: // function has thrown and the state machine is now in an invalid state. sl@0: // This situation can be resolved by the exception event handler sl@0: // function by orderly transiting to another state or terminating. sl@0: // As a result of this, the machine must not be unstable when this sl@0: // function is left. sl@0: state_base_type * const pOutermostUnstableState = sl@0: get_pointer( pOutermostUnstableState_ ); sl@0: state_base_type * const pHandlingState = pOutermostUnstableState == 0 ? sl@0: pCurrentState : pOutermostUnstableState; sl@0: sl@0: BOOST_ASSERT( pHandlingState != 0 ); sl@0: sl@0: // Setting a member variable to a special value for the duration of a sl@0: // call surely looks like a kludge (normally it should be a parameter of sl@0: // the call). However, in this case it is unavoidable because the call sl@0: // below could result in a call to user code where passing through an sl@0: // additional bool parameter is not acceptable. sl@0: performFullExit_ = false; sl@0: const detail::reaction_result reactionResult = pHandlingState->react_impl( sl@0: exceptionEvent, exceptionEvent.dynamic_type() ); sl@0: // If the above call throws then performFullExit_ will obviously not be sl@0: // set back to true. In this case the termination triggered by the sl@0: // scope guard further up in the call stack will take care of this. sl@0: performFullExit_ = true; sl@0: sl@0: if ( ( reactionResult != detail::do_discard_event ) || sl@0: ( get_pointer( pOutermostUnstableState_ ) != 0 ) ) sl@0: { sl@0: throw; sl@0: } sl@0: sl@0: return detail::do_discard_event; sl@0: } sl@0: sl@0: class exception_event_handler sl@0: { sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////// sl@0: exception_event_handler( sl@0: state_machine & machine, sl@0: state_base_type * pCurrentState = 0 sl@0: ) : sl@0: machine_( machine ), sl@0: pCurrentState_( pCurrentState ) sl@0: { sl@0: } sl@0: sl@0: template< class ExceptionEvent > sl@0: result operator()( sl@0: const ExceptionEvent & exceptionEvent ) sl@0: { sl@0: return detail::result_utility::make_result( sl@0: machine_.handle_exception_event( sl@0: exceptionEvent, pCurrentState_ ) ); sl@0: } sl@0: sl@0: private: sl@0: ////////////////////////////////////////////////////////////////////// sl@0: state_machine & machine_; sl@0: state_base_type * pCurrentState_; sl@0: }; sl@0: friend class exception_event_handler; sl@0: sl@0: class terminator sl@0: { sl@0: public: sl@0: terminator( state_machine & machine ) : sl@0: machine_( machine ), dismissed_( false ) {} sl@0: ~terminator() sl@0: { sl@0: if ( !dismissed_ ) { machine_.terminate_impl( false ); } sl@0: } sl@0: void dismiss() { dismissed_ = true; } sl@0: sl@0: private: sl@0: state_machine & machine_; sl@0: bool dismissed_; sl@0: }; sl@0: friend class terminator; sl@0: sl@0: sl@0: void send_event( const event_base_type & evt ) sl@0: { sl@0: terminator guard( *this ); sl@0: BOOST_ASSERT( get_pointer( pOutermostUnstableState_ ) == 0 ); sl@0: const typename rtti_policy_type::id_type eventType = evt.dynamic_type(); sl@0: detail::reaction_result reactionResult = detail::do_forward_event; sl@0: sl@0: for ( sl@0: typename state_list_type::iterator pState = currentStates_.begin(); sl@0: ( reactionResult == detail::do_forward_event ) && sl@0: ( pState != currentStatesEnd_ ); sl@0: ++pState ) sl@0: { sl@0: // CAUTION: The following statement could modify our state list! sl@0: // We must not continue iterating if the event was consumed sl@0: reactionResult = detail::result_utility::get_result( translator_( sl@0: detail::send_function< sl@0: state_base_type, event_base_type, rtti_policy_type::id_type >( sl@0: **pState, evt, eventType ), sl@0: exception_event_handler( *this, get_pointer( *pState ) ) ) ); sl@0: } sl@0: sl@0: guard.dismiss(); sl@0: sl@0: if ( reactionResult == detail::do_forward_event ) sl@0: { sl@0: polymorphic_downcast< MostDerived * >( this )->unconsumed_event( evt ); sl@0: } sl@0: } sl@0: sl@0: sl@0: void process_queued_events() sl@0: { sl@0: while ( !eventQueue_.empty() ) sl@0: { sl@0: const event_base_ptr_type pCurrentEvent( eventQueue_.front() ); sl@0: eventQueue_.pop_front(); sl@0: send_event( *pCurrentEvent ); sl@0: } sl@0: } sl@0: sl@0: sl@0: void terminate_impl( bool performFullExit ) sl@0: { sl@0: performFullExit_ = true; sl@0: sl@0: if ( !terminated() ) sl@0: { sl@0: // this also empties deferredMap_ sl@0: terminate_impl( *pOutermostState_, performFullExit ); sl@0: } sl@0: sl@0: eventQueue_.clear(); sl@0: shallowHistoryMap_.clear(); sl@0: deepHistoryMap_.clear(); sl@0: } sl@0: sl@0: void terminate_impl( state_base_type & theState, bool performFullExit ) sl@0: { sl@0: isInnermostCommonOuter_ = false; sl@0: sl@0: // If pOutermostUnstableState_ == 0, we know for sure that sl@0: // currentStates_.size() > 0, otherwise theState couldn't be alive any sl@0: // more sl@0: if ( get_pointer( pOutermostUnstableState_ ) != 0 ) sl@0: { sl@0: theState.remove_from_state_list( sl@0: currentStatesEnd_, pOutermostUnstableState_, performFullExit ); sl@0: } sl@0: // Optimization: We want to find out whether currentStates_ has size 1 sl@0: // and if yes use the optimized implementation below. Since sl@0: // list<>::size() is implemented quite inefficiently in some std libs sl@0: // it is best to just decrement the currentStatesEnd_ here and sl@0: // increment it again, if the test failed. sl@0: else if ( currentStates_.begin() == --currentStatesEnd_ ) sl@0: { sl@0: // The machine is stable and there is exactly one innermost state. sl@0: // The following optimization is only correct for a stable machine sl@0: // without orthogonal regions. sl@0: leaf_state_ptr_type & pState = *currentStatesEnd_; sl@0: pState->exit_impl( sl@0: pState, pOutermostUnstableState_, performFullExit ); sl@0: } sl@0: else sl@0: { sl@0: BOOST_ASSERT( currentStates_.size() > 1 ); sl@0: // The machine is stable and there are multiple innermost states sl@0: theState.remove_from_state_list( sl@0: ++currentStatesEnd_, pOutermostUnstableState_, performFullExit ); sl@0: } sl@0: } sl@0: sl@0: sl@0: node_state_base_ptr_type add_impl( sl@0: const leaf_state_ptr_type & pState, sl@0: detail::leaf_state< allocator_type, rtti_policy_type > & ) sl@0: { sl@0: if ( currentStatesEnd_ == currentStates_.end() ) sl@0: { sl@0: pState->set_list_position( sl@0: currentStates_.insert( currentStatesEnd_, pState ) ); sl@0: } sl@0: else sl@0: { sl@0: *currentStatesEnd_ = pState; sl@0: pState->set_list_position( currentStatesEnd_ ); sl@0: ++currentStatesEnd_; sl@0: } sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: node_state_base_ptr_type add_impl( sl@0: const node_state_base_ptr_type & pState, sl@0: state_base_type & ) sl@0: { sl@0: return pState; sl@0: } sl@0: sl@0: template< class State > sl@0: static bool is_in_highest_orthogonal_region() sl@0: { sl@0: return mpl::equal_to< sl@0: typename State::orthogonal_position, sl@0: mpl::minus< sl@0: typename State::context_type::no_of_orthogonal_regions, sl@0: mpl::integral_c< detail::orthogonal_position_type, 1 > > sl@0: >::value; sl@0: } sl@0: sl@0: sl@0: typedef detail::history_key< rtti_policy_type > history_key_type; sl@0: sl@0: typedef std::map< sl@0: history_key_type, void (*)(), sl@0: std::less< history_key_type >, sl@0: typename boost::detail::allocator::rebind_to< sl@0: allocator_type, std::pair< const history_key_type, void (*)() > sl@0: >::type sl@0: > history_map_type; sl@0: sl@0: void store_history_impl( sl@0: history_map_type & historyMap, sl@0: const history_key_type & historyId, sl@0: void (*pConstructFunction)() ) sl@0: { sl@0: historyMap[ historyId ] = pConstructFunction; sl@0: } sl@0: sl@0: template< class DefaultState > sl@0: void construct_with_history_impl( sl@0: history_map_type & historyMap, sl@0: const typename DefaultState::context_ptr_type & pContext ) sl@0: { sl@0: typename history_map_type::iterator pFoundSlot = historyMap.find( sl@0: history_key_type::make_history_key< DefaultState >() ); sl@0: sl@0: if ( ( pFoundSlot == historyMap.end() ) || ( pFoundSlot->second == 0 ) ) sl@0: { sl@0: // We have never entered this state before or history was cleared sl@0: DefaultState::deep_construct( sl@0: pContext, *polymorphic_downcast< MostDerived * >( this ) ); sl@0: } sl@0: else sl@0: { sl@0: typedef void construct_function( sl@0: const typename DefaultState::context_ptr_type &, sl@0: typename DefaultState::outermost_context_base_type & ); sl@0: // 5.2.10.6 declares that reinterpret_casting a function pointer to a sl@0: // different function pointer and back must yield the same value. The sl@0: // following reinterpret_cast is the second half of such a sequence. sl@0: construct_function * const pConstructFunction = sl@0: reinterpret_cast< construct_function * >( pFoundSlot->second ); sl@0: (*pConstructFunction)( sl@0: pContext, *polymorphic_downcast< MostDerived * >( this ) ); sl@0: } sl@0: } sl@0: sl@0: typedef std::list< sl@0: event_base_ptr_type, sl@0: typename boost::detail::allocator::rebind_to< sl@0: allocator_type, event_base_ptr_type >::type sl@0: > event_queue_type; sl@0: sl@0: typedef std::map< sl@0: const state_base_type *, event_queue_type, sl@0: std::less< const state_base_type * >, sl@0: typename boost::detail::allocator::rebind_to< sl@0: allocator_type, sl@0: std::pair< const state_base_type * const, event_queue_type > sl@0: >::type sl@0: > deferred_map_type; sl@0: sl@0: sl@0: event_queue_type eventQueue_; sl@0: deferred_map_type deferredMap_; sl@0: state_list_type currentStates_; sl@0: typename state_list_type::iterator currentStatesEnd_; sl@0: state_base_type * pOutermostState_; sl@0: bool isInnermostCommonOuter_; sl@0: node_state_base_ptr_type pOutermostUnstableState_; sl@0: ExceptionTranslator translator_; sl@0: bool performFullExit_; sl@0: history_map_type shallowHistoryMap_; sl@0: history_map_type deepHistoryMap_; sl@0: }; sl@0: sl@0: sl@0: sl@0: } // namespace statechart sl@0: } // namespace boost sl@0: sl@0: sl@0: sl@0: #endif