sl@0: #ifndef BOOST_STATECHART_SIMPLE_STATE_HPP_INCLUDED sl@0: #define BOOST_STATECHART_SIMPLE_STATE_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: 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: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include 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: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include // boost::polymorphic_downcast sl@0: sl@0: #include // std::size_t 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 T > sl@0: struct make_list : public mpl::eval_if< sl@0: mpl::is_sequence< T >, sl@0: mpl::identity< T >, sl@0: mpl::identity< mpl::list< T > > > {}; sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: template< class MostDerived, class Context, class InnerInitial > sl@0: struct simple_state_base_type sl@0: { sl@0: private: sl@0: typedef typename Context::outermost_context_base_type::allocator_type sl@0: allocator_type; sl@0: typedef typename Context::outermost_context_base_type::rtti_policy_type sl@0: rtti_policy_type; sl@0: typedef typename detail::make_list< InnerInitial >::type sl@0: inner_initial_list; sl@0: typedef typename mpl::size< inner_initial_list >::type sl@0: inner_initial_list_size; sl@0: sl@0: public: sl@0: typedef typename mpl::eval_if< sl@0: mpl::empty< inner_initial_list >, sl@0: mpl::identity< typename rtti_policy_type:: sl@0: template rtti_derived_type< MostDerived, leaf_state< sl@0: allocator_type, sl@0: rtti_policy_type > > >, sl@0: mpl::identity< typename rtti_policy_type:: sl@0: template rtti_derived_type< MostDerived, node_state< sl@0: inner_initial_list_size, sl@0: allocator_type, sl@0: rtti_policy_type > > > >::type type; sl@0: }; sl@0: sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: struct no_transition_function sl@0: { sl@0: template< class CommonContext > sl@0: void operator()( CommonContext & ) const {} sl@0: }; sl@0: sl@0: template< class TransitionContext, class Event > sl@0: class transition_function sl@0: { sl@0: public: sl@0: transition_function( sl@0: void ( TransitionContext::*pTransitionAction )( const Event & ), sl@0: const Event & evt sl@0: ) : sl@0: pTransitionAction_( pTransitionAction ), sl@0: evt_( evt ) sl@0: { sl@0: } sl@0: sl@0: template< class CommonContext > sl@0: void operator()( CommonContext & commonContext ) const sl@0: { sl@0: ( commonContext.template context< TransitionContext >() sl@0: .*pTransitionAction_ )( evt_ ); sl@0: } sl@0: sl@0: private: sl@0: void ( TransitionContext::*pTransitionAction_ )( const Event & ); sl@0: const Event & evt_; sl@0: }; sl@0: sl@0: sl@0: template< bool contextHasInheritedDeepHistory, bool contextHasDeepHistory > sl@0: struct deep_history_storer sl@0: { sl@0: template< class HistorizedState, class LeafState, class Context > sl@0: static void store_deep_history( Context & ) {} sl@0: }; sl@0: sl@0: template<> sl@0: struct deep_history_storer< true, false > sl@0: { sl@0: template< class HistorizedState, class LeafState, class Context > sl@0: static void store_deep_history( Context & ctx ) sl@0: { sl@0: ctx.template store_deep_history_impl< LeafState >(); sl@0: } sl@0: }; sl@0: sl@0: template<> sl@0: struct deep_history_storer< true, true > sl@0: { sl@0: template< class HistorizedState, class LeafState, class Context > sl@0: static void store_deep_history( Context & ctx ) sl@0: { sl@0: ctx.outermost_context_base().template store_deep_history< sl@0: HistorizedState, LeafState >(); sl@0: ctx.template store_deep_history_impl< LeafState >(); sl@0: } sl@0: }; sl@0: sl@0: sl@0: sl@0: } // namespace detail sl@0: sl@0: sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: enum history_mode sl@0: { sl@0: has_no_history, sl@0: has_shallow_history, sl@0: has_deep_history, sl@0: has_full_history // shallow & deep sl@0: }; sl@0: sl@0: sl@0: sl@0: ////////////////////////////////////////////////////////////////////////////// sl@0: template< class MostDerived, sl@0: class Context, sl@0: class InnerInitial = mpl::list<>, sl@0: history_mode historyMode = has_no_history > sl@0: class simple_state : public detail::simple_state_base_type< MostDerived, sl@0: typename Context::inner_context_type, InnerInitial >::type sl@0: { sl@0: typedef typename detail::simple_state_base_type< sl@0: MostDerived, typename Context::inner_context_type, sl@0: InnerInitial >::type base_type; sl@0: sl@0: public: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: typedef mpl::list<> reactions; sl@0: sl@0: typedef typename Context::inner_context_type context_type; sl@0: sl@0: template< detail::orthogonal_position_type innerOrthogonalPosition > sl@0: struct orthogonal sl@0: { sl@0: typedef mpl::integral_c< sl@0: detail::orthogonal_position_type, sl@0: innerOrthogonalPosition > inner_orthogonal_position; sl@0: typedef MostDerived inner_context_type; sl@0: }; sl@0: sl@0: typedef typename context_type::outermost_context_type sl@0: outermost_context_type; sl@0: sl@0: outermost_context_type & outermost_context() sl@0: { sl@0: // This assert fails when an attempt is made to access the state machine sl@0: // from a constructor of a state that is *not* a subtype of state<>. sl@0: // To correct this, derive from state<> instead of simple_state<>. sl@0: BOOST_ASSERT( get_pointer( pContext_ ) != 0 ); sl@0: return pContext_->outermost_context(); sl@0: } sl@0: sl@0: const outermost_context_type & outermost_context() const sl@0: { sl@0: // This assert fails when an attempt is made to access the state machine sl@0: // from a constructor of a state that is *not* a subtype of state<>. sl@0: // To correct this, derive from state<> instead of simple_state<>. sl@0: BOOST_ASSERT( get_pointer( pContext_ ) != 0 ); sl@0: return pContext_->outermost_context(); sl@0: } sl@0: sl@0: template< class OtherContext > sl@0: OtherContext & context() sl@0: { sl@0: typedef typename mpl::if_< sl@0: is_same< OtherContext, MostDerived >, sl@0: context_impl_this_context, sl@0: context_impl_other_context sl@0: >::type impl; sl@0: return impl::template context_impl< OtherContext >( *this ); sl@0: } sl@0: sl@0: template< class OtherContext > sl@0: const OtherContext & context() const sl@0: { sl@0: typedef typename mpl::if_< sl@0: is_same< OtherContext, MostDerived >, sl@0: context_impl_this_context, sl@0: context_impl_other_context sl@0: >::type impl; sl@0: return impl::template context_impl< OtherContext >( *this ); sl@0: } sl@0: sl@0: template< class Target > sl@0: Target state_cast() const sl@0: { sl@0: return outermost_context_base().template state_cast< Target >(); sl@0: } sl@0: sl@0: template< class Target > sl@0: Target state_downcast() const sl@0: { sl@0: return outermost_context_base().template state_downcast< Target >(); sl@0: } sl@0: sl@0: typedef typename context_type::state_base_type state_base_type; sl@0: typedef typename context_type::state_iterator state_iterator; sl@0: sl@0: state_iterator state_begin() const sl@0: { sl@0: return outermost_context_base().state_begin(); sl@0: } sl@0: sl@0: state_iterator state_end() const sl@0: { sl@0: return outermost_context_base().state_end(); sl@0: } sl@0: sl@0: sl@0: typedef typename context_type::event_base_ptr_type event_base_ptr_type; sl@0: sl@0: void post_event( const event_base_ptr_type & pEvent ) sl@0: { sl@0: outermost_context_base().post_event( pEvent ); sl@0: } sl@0: sl@0: void post_event( const event_base & evt ) sl@0: { sl@0: outermost_context_base().post_event( evt ); sl@0: } sl@0: sl@0: result discard_event() sl@0: { sl@0: return detail::result_utility::make_result( detail::do_discard_event ); sl@0: } sl@0: sl@0: result forward_event() sl@0: { sl@0: return detail::result_utility::make_result( detail::do_forward_event ); sl@0: } sl@0: sl@0: result defer_event() sl@0: { sl@0: this->state_base_type::defer_event(); sl@0: return detail::result_utility::make_result( detail::do_defer_event ); sl@0: } sl@0: sl@0: template< class DestinationState > sl@0: result transit() sl@0: { sl@0: return transit_impl< DestinationState, outermost_context_type >( sl@0: detail::no_transition_function() ); sl@0: } sl@0: sl@0: template< class DestinationState, class TransitionContext, class Event > sl@0: result transit( sl@0: void ( TransitionContext::*pTransitionAction )( const Event & ), sl@0: const Event & evt ) sl@0: { sl@0: return transit_impl< DestinationState, TransitionContext >( sl@0: detail::transition_function< TransitionContext, Event >( sl@0: pTransitionAction, evt ) ); sl@0: } sl@0: sl@0: result terminate() sl@0: { sl@0: outermost_context_base().terminate_as_reaction( *this ); sl@0: return detail::result_utility::make_result( detail::do_discard_event ); 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: outermost_context_base().template clear_shallow_history< sl@0: HistoryContext, orthogonalPosition >(); 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: outermost_context_base().template clear_deep_history< sl@0: HistoryContext, orthogonalPosition >(); sl@0: } sl@0: sl@0: protected: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: simple_state() : pContext_( 0 ) {} sl@0: sl@0: ~simple_state() sl@0: { sl@0: // As a result of a throwing derived class constructor, this destructor sl@0: // can be called before the context is set. sl@0: if ( get_pointer( pContext_ ) != 0 ) sl@0: { sl@0: if ( this->deferred_events() ) sl@0: { sl@0: outermost_context_base().release_events( this ); sl@0: } sl@0: sl@0: pContext_->remove_inner_state( orthogonal_position::value ); sl@0: } 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 typename Context::inner_orthogonal_position orthogonal_position; sl@0: sl@0: // If you receive a sl@0: // "use of undefined type 'boost::STATIC_ASSERTION_FAILURE'" or similar sl@0: // compiler error here then either this state resides in a non-existent sl@0: // orthogonal region of the outer state or the outer state does not have sl@0: // inner states. sl@0: BOOST_STATIC_ASSERT( ( mpl::less< sl@0: orthogonal_position, sl@0: typename context_type::no_of_orthogonal_regions >::value ) ); 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: sl@0: typedef typename context_type::event_base_type event_base_type; sl@0: typedef typename context_type::rtti_policy_type rtti_policy_type; sl@0: sl@0: typedef typename context_type::outermost_context_base_type sl@0: outermost_context_base_type; sl@0: typedef typename context_type::inner_context_ptr_type context_ptr_type; sl@0: typedef typename context_type::state_list_type state_list_type; sl@0: typedef intrusive_ptr< inner_context_type > inner_context_ptr_type; sl@0: typedef typename detail::make_list< InnerInitial >::type sl@0: inner_initial_list; sl@0: typedef typename mpl::size< inner_initial_list >::type sl@0: inner_initial_list_size; sl@0: typedef mpl::integral_c< sl@0: detail::orthogonal_position_type, sl@0: inner_initial_list_size::value > no_of_orthogonal_regions; sl@0: typedef typename mpl::push_front< sl@0: typename context_type::context_type_list, sl@0: context_type >::type context_type_list; sl@0: sl@0: // If you receive a sl@0: // "use of undefined type 'boost::STATIC_ASSERTION_FAILURE'" or similar sl@0: // compiler error here then the direct or indirect context of this state sl@0: // has deep history _and_ this state has two or more orthogonal regions. sl@0: // Boost.Statechart does not currently support deep history in a state whose sl@0: // direct or indirect inner states have two or more orthogonal regions. sl@0: // Please consult the documentation on how to work around this limitation. sl@0: BOOST_STATIC_ASSERT( ( mpl::or_< sl@0: mpl::less< sl@0: no_of_orthogonal_regions, sl@0: mpl::integral_c< detail::orthogonal_position_type, 2 > >, sl@0: mpl::not_< sl@0: typename context_type::inherited_deep_history > >::value ) ); sl@0: sl@0: typedef mpl::bool_< ( historyMode & has_shallow_history ) != 0 > sl@0: shallow_history; sl@0: typedef typename context_type::shallow_history stores_shallow_history; sl@0: sl@0: typedef mpl::bool_< ( historyMode & has_deep_history ) != 0 > sl@0: deep_history; sl@0: typedef typename mpl::or_< sl@0: deep_history, sl@0: typename context_type::inherited_deep_history sl@0: >::type inherited_deep_history; sl@0: typedef typename mpl::and_< sl@0: inherited_deep_history, sl@0: mpl::empty< inner_initial_list > >::type stores_deep_history; sl@0: sl@0: void * operator new( std::size_t size ) sl@0: { sl@0: return detail::allocate< MostDerived, sl@0: typename outermost_context_type::allocator_type >( size ); sl@0: } sl@0: sl@0: void operator delete( void * pState ) sl@0: { sl@0: detail::deallocate< MostDerived, sl@0: typename outermost_context_type::allocator_type >( pState ); sl@0: } sl@0: sl@0: outermost_context_base_type & outermost_context_base() sl@0: { sl@0: // This assert fails when an attempt is made to access the state machine sl@0: // from a constructor of a state that is *not* a subtype of state<>. sl@0: // To correct this, derive from state<> instead of simple_state<>. sl@0: BOOST_ASSERT( get_pointer( pContext_ ) != 0 ); sl@0: return pContext_->outermost_context_base(); sl@0: } sl@0: sl@0: const outermost_context_base_type & outermost_context_base() const sl@0: { sl@0: // This assert fails when an attempt is made to access the state machine sl@0: // from a constructor of a state that is *not* a subtype of state<>. sl@0: // To correct this, derive from state<> instead of simple_state<>. sl@0: BOOST_ASSERT( get_pointer( pContext_ ) != 0 ); sl@0: return pContext_->outermost_context_base(); sl@0: } sl@0: sl@0: virtual const state_base_type * outer_state_ptr() const sl@0: { sl@0: typedef typename mpl::if_< sl@0: is_same< outermost_context_type, context_type >, sl@0: outer_state_ptr_impl_outermost, sl@0: outer_state_ptr_impl_non_outermost sl@0: >::type impl; sl@0: return impl::outer_state_ptr_impl( *this ); sl@0: } sl@0: sl@0: virtual detail::reaction_result react_impl( sl@0: const event_base_type & evt, sl@0: typename rtti_policy_type::id_type eventType ) sl@0: { sl@0: typedef typename detail::make_list< sl@0: typename MostDerived::reactions >::type reaction_list; sl@0: detail::reaction_result reactionResult = sl@0: local_react< reaction_list >( evt, eventType ); sl@0: sl@0: // At this point we can only safely access pContext_ if the handler did sl@0: // not return do_discard_event! sl@0: switch ( reactionResult ) sl@0: { sl@0: case detail::do_forward_event: sl@0: // TODO: The following call to react_impl of our outer state should sl@0: // be made with a context_type:: prefix to call directly instead of sl@0: // virtually. For some reason the compiler complains... sl@0: reactionResult = pContext_->react_impl( evt, eventType ); sl@0: break; sl@0: case detail::do_defer_event: sl@0: outermost_context_base().defer_event( evt, this ); sl@0: break; sl@0: default: sl@0: break; sl@0: } sl@0: sl@0: return reactionResult; sl@0: } sl@0: sl@0: virtual void exit_impl( sl@0: typename base_type::direct_state_base_ptr_type & pSelf, sl@0: typename state_base_type::node_state_base_ptr_type & sl@0: pOutermostUnstableState, sl@0: bool performFullExit ) sl@0: { sl@0: inner_context_ptr_type pMostDerivedSelf = sl@0: polymorphic_downcast< MostDerived * >( this ); sl@0: pSelf = 0; sl@0: exit_impl( pMostDerivedSelf, pOutermostUnstableState, performFullExit ); sl@0: } sl@0: sl@0: void exit_impl( sl@0: inner_context_ptr_type & pSelf, sl@0: typename state_base_type::node_state_base_ptr_type & sl@0: pOutermostUnstableState, sl@0: bool performFullExit ) sl@0: { sl@0: switch ( this->ref_count() ) sl@0: { sl@0: case 2: sl@0: if ( get_pointer( pOutermostUnstableState ) == sl@0: static_cast< state_base_type * >( this ) ) sl@0: { sl@0: pContext_->set_outermost_unstable_state( sl@0: pOutermostUnstableState ); sl@0: // fall through to next case intended sl@0: } sl@0: else sl@0: { sl@0: break; sl@0: } sl@0: case 1: sl@0: { sl@0: if ( get_pointer( pOutermostUnstableState ) == 0 ) sl@0: { sl@0: pContext_->set_outermost_unstable_state( sl@0: pOutermostUnstableState ); sl@0: } sl@0: sl@0: if ( performFullExit ) sl@0: { sl@0: pSelf->exit(); sl@0: check_store_shallow_history< stores_shallow_history >(); sl@0: check_store_deep_history< stores_deep_history >(); sl@0: } sl@0: sl@0: context_ptr_type pContext = pContext_; sl@0: pSelf = 0; sl@0: pContext->exit_impl( sl@0: pContext, pOutermostUnstableState, performFullExit ); sl@0: break; sl@0: } sl@0: default: sl@0: break; sl@0: } sl@0: } 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 = this; sl@0: } sl@0: sl@0: template< class OtherContext > sl@0: const typename OtherContext::inner_context_ptr_type & context_ptr() const sl@0: { sl@0: typedef typename mpl::if_< sl@0: is_same< OtherContext, context_type >, sl@0: context_ptr_impl_my_context, sl@0: context_ptr_impl_other_context sl@0: >::type impl; sl@0: sl@0: return impl::template context_ptr_impl< OtherContext >( *this ); sl@0: } sl@0: sl@0: static void initial_deep_construct( sl@0: outermost_context_base_type & outermostContextBase ) sl@0: { sl@0: deep_construct( &outermostContextBase, outermostContextBase ); sl@0: } sl@0: sl@0: static void deep_construct( sl@0: const context_ptr_type & pContext, sl@0: outermost_context_base_type & outermostContextBase ) sl@0: { sl@0: const inner_context_ptr_type pInnerContext( sl@0: shallow_construct( pContext, outermostContextBase ) ); sl@0: deep_construct_inner< inner_initial_list >( sl@0: pInnerContext, outermostContextBase ); sl@0: } sl@0: sl@0: static inner_context_ptr_type shallow_construct( sl@0: const context_ptr_type & pContext, sl@0: outermost_context_base_type & outermostContextBase ) sl@0: { sl@0: const inner_context_ptr_type pInnerContext( new MostDerived ); sl@0: pInnerContext->set_context( pContext ); sl@0: outermostContextBase.add( pInnerContext ); sl@0: return pInnerContext; sl@0: } sl@0: sl@0: void set_context( const context_ptr_type & pContext ) sl@0: { sl@0: BOOST_ASSERT( get_pointer( pContext ) != 0 ); sl@0: pContext_ = pContext; sl@0: base_type::set_context( sl@0: orthogonal_position::value, get_pointer( pContext ) ); sl@0: } sl@0: sl@0: template< class InnerList > sl@0: static void deep_construct_inner( sl@0: const inner_context_ptr_type & pInnerContext, sl@0: outermost_context_base_type & outermostContextBase ) sl@0: { sl@0: typedef typename mpl::if_< sl@0: mpl::empty< InnerList >, sl@0: deep_construct_inner_impl_empty, sl@0: deep_construct_inner_impl_non_empty sl@0: >::type impl; sl@0: impl::template deep_construct_inner_impl< InnerList >( sl@0: pInnerContext, outermostContextBase ); sl@0: } sl@0: sl@0: template< class LeafState > sl@0: void store_deep_history_impl() sl@0: { sl@0: detail::deep_history_storer< sl@0: context_type::inherited_deep_history::value, sl@0: context_type::deep_history::value sl@0: >::template store_deep_history< MostDerived, LeafState >( sl@0: *pContext_ ); sl@0: } sl@0: sl@0: private: sl@0: ////////////////////////////////////////////////////////////////////////// sl@0: struct context_ptr_impl_other_context sl@0: { sl@0: template< class OtherContext, class State > sl@0: static const typename OtherContext::inner_context_ptr_type & sl@0: context_ptr_impl( const State & stt ) sl@0: { sl@0: // This assert fails when an attempt is made to access an outer sl@0: // context from a constructor of a state that is *not* a subtype of sl@0: // state<>. To correct this, derive from state<> instead of sl@0: // simple_state<>. sl@0: BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 ); sl@0: return stt.pContext_->template context_ptr< OtherContext >(); sl@0: } sl@0: }; sl@0: friend struct context_ptr_impl_other_context; sl@0: sl@0: struct context_ptr_impl_my_context sl@0: { sl@0: template< class OtherContext, class State > sl@0: static const typename OtherContext::inner_context_ptr_type & sl@0: context_ptr_impl( const State & stt ) sl@0: { sl@0: // This assert fails when an attempt is made to access an outer sl@0: // context from a constructor of a state that is *not* a subtype of sl@0: // state<>. To correct this, derive from state<> instead of sl@0: // simple_state<>. sl@0: BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 ); sl@0: return stt.pContext_; sl@0: } sl@0: }; sl@0: friend struct context_ptr_impl_my_context; sl@0: sl@0: struct context_impl_other_context sl@0: { sl@0: template< class OtherContext, class State > sl@0: static OtherContext & context_impl( State & stt ) sl@0: { sl@0: // This assert fails when an attempt is made to access an outer sl@0: // context from a constructor of a state that is *not* a subtype of sl@0: // state<>. To correct this, derive from state<> instead of sl@0: // simple_state<>. sl@0: BOOST_ASSERT( get_pointer( stt.pContext_ ) != 0 ); sl@0: return stt.pContext_->template context< OtherContext >(); sl@0: } sl@0: }; sl@0: friend struct context_impl_other_context; sl@0: sl@0: struct context_impl_this_context sl@0: { sl@0: template< class OtherContext, class State > sl@0: static OtherContext & context_impl( State & stt ) sl@0: { sl@0: return *polymorphic_downcast< MostDerived * >( &stt ); sl@0: } sl@0: }; sl@0: friend struct context_impl_this_context; sl@0: sl@0: template< class DestinationState, sl@0: class TransitionContext, sl@0: class TransitionAction > sl@0: result transit_impl( const TransitionAction & transitionAction ) sl@0: { sl@0: typedef typename mpl::find_if< sl@0: context_type_list, sl@0: mpl::contains< sl@0: typename DestinationState::context_type_list, sl@0: mpl::placeholders::_ > >::type common_context_iter; sl@0: typedef typename mpl::deref< common_context_iter >::type sl@0: common_context_type; sl@0: typedef typename mpl::distance< sl@0: typename mpl::begin< context_type_list >::type, sl@0: common_context_iter >::type termination_state_position; sl@0: typedef typename mpl::push_front< context_type_list, MostDerived >::type sl@0: possible_transition_contexts; sl@0: typedef typename mpl::at< sl@0: possible_transition_contexts, sl@0: termination_state_position >::type termination_state_type; sl@0: sl@0: termination_state_type & terminationState( sl@0: context< termination_state_type >() ); sl@0: const typename sl@0: common_context_type::inner_context_ptr_type pCommonContext( sl@0: terminationState.context_ptr< common_context_type >() ); sl@0: outermost_context_base_type & outermostContextBase( sl@0: pCommonContext->outermost_context_base() ); sl@0: sl@0: #ifdef BOOST_STATECHART_RELAX_TRANSITION_CONTEXT sl@0: typedef typename mpl::distance< sl@0: typename mpl::begin< possible_transition_contexts >::type, sl@0: typename mpl::find< sl@0: possible_transition_contexts, TransitionContext >::type sl@0: >::type proposed_transition_context_position; sl@0: sl@0: typedef typename mpl::plus< sl@0: termination_state_position, sl@0: mpl::long_< 1 > sl@0: >::type uml_transition_context_position; sl@0: sl@0: typedef typename mpl::deref< typename mpl::max_element< sl@0: mpl::list< sl@0: proposed_transition_context_position, sl@0: uml_transition_context_position >, sl@0: mpl::greater< mpl::placeholders::_, mpl::placeholders::_ > sl@0: >::type >::type real_transition_context_position; sl@0: sl@0: typedef typename mpl::at< sl@0: possible_transition_contexts, sl@0: real_transition_context_position >::type real_transition_context_type; sl@0: sl@0: #ifdef BOOST_MSVC sl@0: # pragma warning( push ) sl@0: # pragma warning( disable: 4127 ) // conditional expression is constant sl@0: #endif sl@0: if ( ( proposed_transition_context_position::value == 0 ) && sl@0: ( inner_initial_list_size::value == 0 ) ) sl@0: { sl@0: transitionAction( *polymorphic_downcast< MostDerived * >( this ) ); sl@0: outermostContextBase.terminate_as_part_of_transit( terminationState ); sl@0: } sl@0: else if ( proposed_transition_context_position::value >= sl@0: uml_transition_context_position::value ) sl@0: { sl@0: real_transition_context_type & transitionContext = sl@0: context< real_transition_context_type >(); sl@0: outermostContextBase.terminate_as_part_of_transit( terminationState ); sl@0: transitionAction( transitionContext ); sl@0: } sl@0: else sl@0: { sl@0: typename real_transition_context_type::inner_context_ptr_type sl@0: pTransitionContext = context_ptr< real_transition_context_type >(); sl@0: outermostContextBase.terminate_as_part_of_transit( sl@0: *pTransitionContext ); sl@0: transitionAction( *pTransitionContext ); sl@0: pTransitionContext = 0; sl@0: outermostContextBase.terminate_as_part_of_transit( terminationState ); sl@0: } sl@0: #ifdef BOOST_MSVC sl@0: # pragma warning( pop ) sl@0: #endif sl@0: #else sl@0: outermostContextBase.terminate_as_part_of_transit( terminationState ); sl@0: transitionAction( *pCommonContext ); sl@0: #endif sl@0: sl@0: typedef typename detail::make_context_list< sl@0: common_context_type, DestinationState >::type context_list_type; 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 make an invalid sl@0: // transition between different orthogonal regions. sl@0: BOOST_STATIC_ASSERT( ( mpl::equal_to< sl@0: typename termination_state_type::orthogonal_position, sl@0: typename mpl::front< context_list_type >::type::orthogonal_position sl@0: >::value ) ); sl@0: sl@0: detail::constructor< sl@0: context_list_type, outermost_context_base_type >::construct( sl@0: pCommonContext, outermostContextBase ); sl@0: sl@0: return detail::result_utility::make_result( detail::do_discard_event ); sl@0: } sl@0: sl@0: struct local_react_impl_non_empty sl@0: { sl@0: template< class ReactionList, class State > sl@0: static detail::reaction_result local_react_impl( sl@0: State & stt, sl@0: const event_base_type & evt, sl@0: typename rtti_policy_type::id_type eventType ) sl@0: { sl@0: detail::reaction_result reactionResult = sl@0: mpl::front< ReactionList >::type::react( sl@0: *polymorphic_downcast< MostDerived * >( &stt ), sl@0: evt, eventType ); sl@0: sl@0: if ( reactionResult == detail::no_reaction ) sl@0: { sl@0: reactionResult = stt.template local_react< sl@0: typename mpl::pop_front< ReactionList >::type >( sl@0: evt, eventType ); sl@0: } sl@0: sl@0: return reactionResult; sl@0: } sl@0: }; sl@0: friend struct local_react_impl_non_empty; sl@0: sl@0: struct local_react_impl_empty sl@0: { sl@0: template< class ReactionList, class State > sl@0: static detail::reaction_result local_react_impl( sl@0: State &, const event_base_type &, typename rtti_policy_type::id_type ) sl@0: { sl@0: return detail::do_forward_event; sl@0: } sl@0: }; sl@0: sl@0: template< class ReactionList > sl@0: detail::reaction_result local_react( sl@0: const event_base_type & evt, sl@0: typename rtti_policy_type::id_type eventType ) sl@0: { sl@0: typedef typename mpl::if_< sl@0: mpl::empty< ReactionList >, sl@0: local_react_impl_empty, sl@0: local_react_impl_non_empty sl@0: >::type impl; sl@0: return impl::template local_react_impl< ReactionList >( sl@0: *this, evt, eventType ); sl@0: } sl@0: sl@0: struct outer_state_ptr_impl_non_outermost sl@0: { sl@0: template< class State > sl@0: static const state_base_type * outer_state_ptr_impl( const State & stt ) sl@0: { sl@0: return get_pointer( stt.pContext_ ); sl@0: } sl@0: }; sl@0: friend struct outer_state_ptr_impl_non_outermost; sl@0: sl@0: struct outer_state_ptr_impl_outermost sl@0: { sl@0: template< class State > sl@0: static const state_base_type * outer_state_ptr_impl( const State & ) sl@0: { sl@0: return 0; sl@0: } sl@0: }; sl@0: sl@0: struct deep_construct_inner_impl_non_empty sl@0: { sl@0: template< class InnerList > sl@0: static void deep_construct_inner_impl( sl@0: const inner_context_ptr_type & pInnerContext, sl@0: outermost_context_base_type & outermostContextBase ) sl@0: { sl@0: typedef typename mpl::front< InnerList >::type current_inner; 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 there is a mismatch between the sl@0: // orthogonal position of a state and its position in the inner sl@0: // initial list of its outer state. sl@0: BOOST_STATIC_ASSERT( ( is_same< sl@0: current_inner, sl@0: typename mpl::at< sl@0: typename current_inner::context_type::inner_initial_list, sl@0: typename current_inner::orthogonal_position >::type >::value ) ); sl@0: sl@0: current_inner::deep_construct( pInnerContext, outermostContextBase ); sl@0: deep_construct_inner< typename mpl::pop_front< InnerList >::type >( sl@0: pInnerContext, outermostContextBase ); sl@0: } sl@0: }; sl@0: sl@0: struct deep_construct_inner_impl_empty sl@0: { sl@0: template< class InnerList > sl@0: static void deep_construct_inner_impl( sl@0: const inner_context_ptr_type &, outermost_context_base_type & ) {} sl@0: }; sl@0: sl@0: struct check_store_shallow_history_impl_no sl@0: { sl@0: template< class State > sl@0: static void check_store_shallow_history_impl( State & ) {} sl@0: }; sl@0: sl@0: struct check_store_shallow_history_impl_yes sl@0: { sl@0: template< class State > sl@0: static void check_store_shallow_history_impl( State & stt ) sl@0: { sl@0: stt.outermost_context_base().template store_shallow_history< sl@0: MostDerived >(); sl@0: } sl@0: }; sl@0: friend struct check_store_shallow_history_impl_yes; sl@0: sl@0: template< class StoreShallowHistory > sl@0: void check_store_shallow_history() sl@0: { sl@0: typedef typename mpl::if_< sl@0: StoreShallowHistory, sl@0: check_store_shallow_history_impl_yes, sl@0: check_store_shallow_history_impl_no sl@0: >::type impl; sl@0: impl::check_store_shallow_history_impl( *this ); sl@0: } sl@0: sl@0: struct check_store_deep_history_impl_no sl@0: { sl@0: template< class State > sl@0: static void check_store_deep_history_impl( State & ) {} sl@0: }; sl@0: sl@0: struct check_store_deep_history_impl_yes sl@0: { sl@0: template< class State > sl@0: static void check_store_deep_history_impl( State & stt ) sl@0: { sl@0: stt.store_deep_history_impl< MostDerived >(); sl@0: } sl@0: }; sl@0: friend struct check_store_deep_history_impl_yes; sl@0: sl@0: template< class StoreDeepHistory > sl@0: void check_store_deep_history() sl@0: { sl@0: typedef typename mpl::if_< sl@0: StoreDeepHistory, sl@0: check_store_deep_history_impl_yes, sl@0: check_store_deep_history_impl_no sl@0: >::type impl; sl@0: impl::check_store_deep_history_impl( *this ); sl@0: } sl@0: sl@0: sl@0: context_ptr_type pContext_; sl@0: }; sl@0: sl@0: sl@0: sl@0: #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP sl@0: } // namespace statechart sl@0: #endif sl@0: sl@0: sl@0: sl@0: template< class MostDerived, class Context, sl@0: class InnerInitial, history_mode historyMode > sl@0: inline void intrusive_ptr_release( const ::boost::statechart::simple_state< sl@0: MostDerived, Context, InnerInitial, historyMode > * pBase ) sl@0: { sl@0: if ( pBase->release() ) sl@0: { sl@0: // The cast is necessary because the simple_state destructor is non- sl@0: // virtual (and inaccessible from this context) sl@0: delete polymorphic_downcast< const MostDerived * >( pBase ); sl@0: } sl@0: } sl@0: sl@0: sl@0: sl@0: #ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP sl@0: } // namespace statechart sl@0: #endif sl@0: sl@0: sl@0: sl@0: } // namespace boost sl@0: sl@0: sl@0: sl@0: #endif