// Copyright 2008 Christophe Henry // henry UNDERSCORE christophe AT hotmail DOT com // This is an extended version of the state machine available in the boost::mpl library // Distributed under the same license as the original. // Copyright for the original version: // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed // under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_MSM_BACK_STATEMACHINE_H #define BOOST_MSM_BACK_STATEMACHINE_H #include #include #include #include #include #include #define BOOST_MPL_CFG_NO_PREPROCESSED_HEADERS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef BOOST_NO_RTTI #include #endif #include #include #include #include #include #include #include #include #include #include BOOST_MPL_HAS_XXX_TRAIT_DEF(accept_sig) BOOST_MPL_HAS_XXX_TRAIT_DEF(no_automatic_create) BOOST_MPL_HAS_XXX_TRAIT_DEF(non_forwarding_flag) BOOST_MPL_HAS_XXX_TRAIT_DEF(direct_entry) BOOST_MPL_HAS_XXX_TRAIT_DEF(initial_event) BOOST_MPL_HAS_XXX_TRAIT_DEF(do_serialize) #ifndef BOOST_MSM_CONSTRUCTOR_ARG_SIZE #define BOOST_MSM_CONSTRUCTOR_ARG_SIZE 5 // default max number of arguments for constructors #endif namespace boost { namespace msm { namespace back { // event used internally for wrapping a direct entry template struct direct_entry_event { typedef int direct_entry; typedef StateType active_state; direct_entry_event(Event const& evt):m_event(evt){} Event const& m_event; }; // This declares the statically-initialized dispatch_table instance. template const boost::msm::back::dispatch_table dispatch_table::instance; // library-containing class for state machines. Pass the actual FSM class as // the Concrete parameter. template class state_machine : public Derived { private: typedef boost::msm::back::state_machine library_sm; typedef ::boost::function< execute_return ()> transition_fct; typedef ::boost::function< execute_return () > deferred_fct; typedef std::deque deferred_events_queue_t; typedef std::queue events_queue_t; typedef bool (*flag_handler)(library_sm&); // all state machines are friend with each other to allow embedding any of them in another fsm template friend class boost::msm::back::state_machine; // helper to add, if needed, visitors to all states // version without visitors template struct visitor_fct_helper { public: visitor_fct_helper(){} void fill_visitors(int) { } template void insert(int,FCT) { } template void execute(int,VISITOR) { } }; // version with visitors template struct visitor_fct_helper >::type> { public: visitor_fct_helper():m_state_visitors(){} void fill_visitors(int number_of_states) { m_state_visitors.resize(number_of_states); } template void insert(int index,FCT fct) { m_state_visitors[index]=fct; } void execute(int index) { m_state_visitors[index](); } #define MSM_VISITOR_HELPER_EXECUTE_SUB(z, n, unused) ARG ## n vis ## n #define MSM_VISITOR_HELPER_EXECUTE(z, n, unused) \ template \ void execute(int index BOOST_PP_COMMA_IF(n) \ BOOST_PP_ENUM(n, MSM_VISITOR_HELPER_EXECUTE_SUB, ~ ) ) \ { \ m_state_visitors[index](BOOST_PP_ENUM_PARAMS(n,vis)); \ } BOOST_PP_REPEAT_FROM_TO(1,BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_VISITOR_HELPER_EXECUTE, ~) #undef MSM_VISITOR_HELPER_EXECUTE #undef MSM_VISITOR_HELPER_EXECUTE_SUB private: typedef typename StateType::accept_sig::type visitor_fct; typedef std::vector visitors; visitors m_state_visitors; }; template struct deferred_msg_queue_helper { }; template struct deferred_msg_queue_helper::type >::type> { public: deferred_msg_queue_helper():m_deferred_events_queue(){} deferred_events_queue_t m_deferred_events_queue; }; public: // tags typedef int composite_tag; // in case someone needs to know typedef HistoryPolicy history_policy; struct InitEvent { }; // flag handling struct Flag_AND { typedef std::logical_and type; }; struct Flag_OR { typedef std::logical_or type; }; typedef typename Derived::BaseAllStates BaseState; typedef Derived ConcreteSM; // if the front-end fsm provides an initial_event typedef, replace InitEvent by this one typedef typename ::boost::mpl::eval_if< typename has_initial_event::type, get_initial_event, ::boost::mpl::identity >::type fsm_initial_event; template struct exit_pt : public ExitPoint { // tags typedef ExitPoint wrapped_exit; typedef int pseudo_exit; typedef library_sm owner; typedef int no_automatic_create; typedef typename ExitPoint::event Event; typedef ::boost::function forwarding_function; // forward event to the higher-level FSM template void forward_event(ForwardEvent const& incomingEvent) { // use helper to forward or not ForwardHelper< ::boost::is_convertible::value>::helper(incomingEvent,m_forward); } void set_forward_fct(::boost::function fct) { m_forward = fct; } exit_pt():m_forward(){} // by assignments, we keep our forwarding functor unchanged as our containing SM did not change template exit_pt(RHS& rhs):m_forward(){} exit_pt& operator= (const exit_pt& ) { return *this; } private: forwarding_function m_forward; // using partial specialization instead of enable_if because of VC8 bug template struct ForwardHelper { template static void helper(ForwardEvent const& ,forwarding_function& ) { // Not our event, ignore } }; template struct ForwardHelper { template static void helper(ForwardEvent const& incomingEvent,forwarding_function& forward_fct) { // call if handler set, if not, this state is simply a terminate state if (forward_fct) forward_fct(incomingEvent); } }; }; template struct entry_pt : public EntryPoint { // tags typedef EntryPoint wrapped_entry; typedef int pseudo_entry; typedef library_sm owner; typedef int no_automatic_create; }; template struct direct : public EntryPoint { // tags typedef EntryPoint wrapped_entry; typedef int explicit_entry_state; typedef library_sm owner; typedef int no_automatic_create; }; typedef typename get_number_of_regions::type nr_regions; // Template used to form rows in the transition table template< typename ROW > struct row_ { //typedef typename ROW::Source T1; typedef typename make_entry::type T1; typedef typename make_exit::type T2; typedef typename ROW::Evt transition_event; // if the source is an exit pseudo state, then // current_state_type becomes the result of get_owner // meaning the containing SM from which the exit occurs typedef typename ::boost::mpl::eval_if< typename has_pseudo_exit::type, get_owner, ::boost::mpl::identity >::type current_state_type; // if Target is a sequence, then we have a fork and expect a sequence of explicit_entry // else if Target is an explicit_entry, next_state_type becomes the result of get_owner // meaning the containing SM if the row is "outside" the containing SM or else the explicit_entry state itself typedef typename ::boost::mpl::eval_if< typename ::boost::mpl::is_sequence::type, get_fork_owner, ::boost::mpl::eval_if< typename has_no_automatic_create::type, get_owner, ::boost::mpl::identity > >::type next_state_type; // if a guard condition is here, call it to check that the event is accepted static bool check_guard(library_sm& fsm,transition_event const& evt) { if ( ROW::guard_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list ) ) return true; return false; } // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) { BOOST_STATIC_CONSTANT(int, current_state = (get_state_id::type::value)); BOOST_STATIC_CONSTANT(int, next_state = (get_state_id::type::value)); BOOST_ASSERT(state == (current_state)); // if T1 is an exit pseudo state, then take the transition only if the pseudo exit state is active if (has_pseudo_exit::type::value && !is_exit_state_active >(fsm)) { return HANDLED_FALSE; } if (!check_guard(fsm,evt)) { // guard rejected the event, we stay in the current one return HANDLED_GUARD_REJECT; } // the guard condition has already been checked execute_exit (::boost::fusion::at_key(fsm.m_substate_list),evt,fsm); // then call the action method ROW::action_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list); // and finally the entry method of the new current state convert_event_and_execute_entry (::boost::fusion::at_key(fsm.m_substate_list),evt,fsm); fsm.m_states[region_index]=next_state; return HANDLED_TRUE; } }; // row having only a guard condition template< typename ROW > struct g_row_ { //typedef typename ROW::Source T1; typedef typename make_entry::type T1; typedef typename make_exit::type T2; typedef typename ROW::Evt transition_event; // if the source is an exit pseudo state, then // current_state_type becomes the result of get_owner // meaning the containing SM from which the exit occurs typedef typename ::boost::mpl::eval_if< typename has_pseudo_exit::type, get_owner, ::boost::mpl::identity >::type current_state_type; // if Target is a sequence, then we have a fork and expect a sequence of explicit_entry // else if Target is an explicit_entry, next_state_type becomes the result of get_owner // meaning the containing SM if the row is "outside" the containing SM or else the explicit_entry state itself typedef typename ::boost::mpl::eval_if< typename ::boost::mpl::is_sequence::type, get_fork_owner, ::boost::mpl::eval_if< typename has_no_automatic_create::type, get_owner, ::boost::mpl::identity > >::type next_state_type; // if a guard condition is defined, call it to check that the event is accepted static bool check_guard(library_sm& fsm,transition_event const& evt) { if ( ROW::guard_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list )) return true; return false; } // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) { BOOST_STATIC_CONSTANT(int, current_state = (get_state_id::type::value)); BOOST_STATIC_CONSTANT(int, next_state = (get_state_id::type::value)); BOOST_ASSERT(state == (current_state)); // if T1 is an exit pseudo state, then take the transition only if the pseudo exit state is active if (has_pseudo_exit::type::value && !is_exit_state_active >(fsm)) { return HANDLED_FALSE; } if (!check_guard(fsm,evt)) { // guard rejected the event, we stay in the current one return HANDLED_GUARD_REJECT; } // the guard condition has already been checked execute_exit (::boost::fusion::at_key(fsm.m_substate_list),evt,fsm); // and finally the entry method of the new current state convert_event_and_execute_entry (::boost::fusion::at_key(fsm.m_substate_list),evt,fsm); fsm.m_states[region_index]=next_state; return HANDLED_TRUE; } }; // row having only an action method template< typename ROW > struct a_row_ { //typedef typename ROW::Source T1; typedef typename make_entry::type T1; typedef typename make_exit::type T2; typedef typename ROW::Evt transition_event; // if the source is an exit pseudo state, then // current_state_type becomes the result of get_owner // meaning the containing SM from which the exit occurs typedef typename ::boost::mpl::eval_if< typename has_pseudo_exit::type, get_owner, ::boost::mpl::identity >::type current_state_type; // if Target is a sequence, then we have a fork and expect a sequence of explicit_entry // else if Target is an explicit_entry, next_state_type becomes the result of get_owner // meaning the containing SM if the row is "outside" the containing SM or else the explicit_entry state itself typedef typename ::boost::mpl::eval_if< typename ::boost::mpl::is_sequence::type, get_fork_owner, ::boost::mpl::eval_if< typename has_no_automatic_create::type, get_owner, ::boost::mpl::identity > >::type next_state_type; // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) { BOOST_STATIC_CONSTANT(int, current_state = (get_state_id::type::value)); BOOST_STATIC_CONSTANT(int, next_state = (get_state_id::type::value)); BOOST_ASSERT(state == (current_state)); // if T1 is an exit pseudo state, then take the transition only if the pseudo exit state is active if (has_pseudo_exit::type::value && !is_exit_state_active >(fsm)) { return HANDLED_FALSE; } // no need to check the guard condition // first call the exit method of the current state execute_exit (::boost::fusion::at_key(fsm.m_substate_list),evt,fsm); // then call the action method ROW::action_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list); // and finally the entry method of the new current state convert_event_and_execute_entry (::boost::fusion::at_key(fsm.m_substate_list),evt,fsm); fsm.m_states[region_index]=next_state; return HANDLED_TRUE; } }; // row having no guard condition or action, simply transitions template< typename ROW > struct _row_ { //typedef typename ROW::Source T1; typedef typename make_entry::type T1; typedef typename make_exit::type T2; typedef typename ROW::Evt transition_event; // if the source is an exit pseudo state, then // current_state_type becomes the result of get_owner // meaning the containing SM from which the exit occurs typedef typename ::boost::mpl::eval_if< typename has_pseudo_exit::type, get_owner, ::boost::mpl::identity >::type current_state_type; // if Target is a sequence, then we have a fork and expect a sequence of explicit_entry // else if Target is an explicit_entry, next_state_type becomes the result of get_owner // meaning the containing SM if the row is "outside" the containing SM or else the explicit_entry state itself typedef typename ::boost::mpl::eval_if< typename ::boost::mpl::is_sequence::type, get_fork_owner, ::boost::mpl::eval_if< typename has_no_automatic_create::type, get_owner, ::boost::mpl::identity > >::type next_state_type; // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) { BOOST_STATIC_CONSTANT(int, current_state = (get_state_id::type::value)); BOOST_STATIC_CONSTANT(int, next_state = (get_state_id::type::value)); BOOST_ASSERT(state == (current_state)); // if T1 is an exit pseudo state, then take the transition only if the pseudo exit state is active if (has_pseudo_exit::type::value && !is_exit_state_active >(fsm)) { return HANDLED_FALSE; } // first call the exit method of the current state execute_exit (::boost::fusion::at_key(fsm.m_substate_list),evt,fsm); // and finally the entry method of the new current state convert_event_and_execute_entry (::boost::fusion::at_key(fsm.m_substate_list),evt,fsm); fsm.m_states[region_index]=next_state; return HANDLED_TRUE; } }; // "i" rows are rows for internal transitions template< typename ROW > struct irow_ { typedef typename make_entry::type T1; typedef typename make_exit::type T2; typedef typename ROW::Evt transition_event; typedef typename ROW::Source current_state_type; typedef T2 next_state_type; // if a guard condition is here, call it to check that the event is accepted static bool check_guard(library_sm& fsm,transition_event const& evt) { if ( ROW::guard_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list)) return true; return false; } // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int , int state, transition_event const& evt) { BOOST_STATIC_CONSTANT(int, current_state = (get_state_id::type::value)); BOOST_ASSERT(state == (current_state)); if (!check_guard(fsm,evt)) { // guard rejected the event, we stay in the current one return HANDLED_GUARD_REJECT; } // call the action method ROW::action_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list); return HANDLED_TRUE; } }; // row having only a guard condition template< typename ROW > struct g_irow_ { typedef typename make_entry::type T1; typedef typename make_exit::type T2; typedef typename ROW::Evt transition_event; typedef typename ROW::Source current_state_type; typedef T2 next_state_type; // if a guard condition is defined, call it to check that the event is accepted static bool check_guard(library_sm& fsm,transition_event const& evt) { if ( ROW::guard_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list) ) return true; return false; } // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int , int state, transition_event const& evt) { BOOST_STATIC_CONSTANT(int, current_state = (get_state_id::type::value)); BOOST_ASSERT(state == (current_state)); if (!check_guard(fsm,evt)) { // guard rejected the event, we stay in the current one return HANDLED_GUARD_REJECT; } return HANDLED_TRUE; } }; // row having only an action method template< typename ROW > struct a_irow_ { typedef typename make_entry::type T1; typedef typename make_exit::type T2; typedef typename ROW::Evt transition_event; typedef typename ROW::Source current_state_type; typedef T2 next_state_type; // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) { BOOST_STATIC_CONSTANT(int, current_state = (get_state_id::type::value)); BOOST_ASSERT(state == (current_state)); // call the action method ROW::action_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list); return HANDLED_TRUE; } }; // row simply ignoring the event template< typename ROW > struct _irow_ { typedef typename make_entry::type T1; typedef typename make_exit::type T2; typedef typename ROW::Evt transition_event; typedef typename ROW::Source current_state_type; typedef T2 next_state_type; // Take the transition action and return the next state. static HandledEnum execute(library_sm& , int , int state, transition_event const& ) { BOOST_STATIC_CONSTANT(int, current_state = (get_state_id::type::value)); BOOST_ASSERT(state == (current_state)); return HANDLED_TRUE; } }; // transitions internal to this state machine (no substate involved) template< typename ROW, typename StateType > struct internal_ { typedef StateType current_state_type; typedef StateType next_state_type; typedef typename ROW::Evt transition_event; // if a guard condition is here, call it to check that the event is accepted static bool check_guard(library_sm& fsm,transition_event const& evt) { if ( ROW::guard_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list) ) return true; return false; } // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) { if (!check_guard(fsm,evt)) { // guard rejected the event, we stay in the current one return HANDLED_GUARD_REJECT; } // then call the action method ROW::action_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list); return HANDLED_TRUE; } }; template< typename ROW, typename StateType > struct a_internal_ { typedef StateType current_state_type; typedef StateType next_state_type; typedef typename ROW::Evt transition_event; // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) { // then call the action method ROW::action_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list); return HANDLED_TRUE; } }; template< typename ROW, typename StateType > struct g_internal_ { typedef StateType current_state_type; typedef StateType next_state_type; typedef typename ROW::Evt transition_event; // if a guard condition is here, call it to check that the event is accepted static bool check_guard(library_sm& fsm,transition_event const& evt) { if ( ROW::guard_call(fsm,evt, ::boost::fusion::at_key(fsm.m_substate_list), ::boost::fusion::at_key(fsm.m_substate_list), fsm.m_substate_list) ) return true; return false; } // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) { if (!check_guard(fsm,evt)) { // guard rejected the event, we stay in the current one return HANDLED_GUARD_REJECT; } return HANDLED_TRUE; } }; template< typename ROW, typename StateType > struct _internal_ { typedef StateType current_state_type; typedef StateType next_state_type; typedef typename ROW::Evt transition_event; static HandledEnum execute(library_sm& , int , int , transition_event const& ) { return HANDLED_TRUE; } }; // Template used to form forwarding rows in the transition table for every row of a composite SM template< typename T1 , class Evt > struct frow { typedef T1 current_state_type; typedef T1 next_state_type; typedef Evt transition_event; // Take the transition action and return the next state. static HandledEnum execute(library_sm& fsm, int region_index, int , transition_event const& evt) { execute_return res = (::boost::fusion::at_key(fsm.m_substate_list)).process_event(evt); fsm.m_states[region_index]=get_state_id::type::value; return res; } }; template struct create_backend_stt { }; template struct create_backend_stt { typedef g_row_ type; }; template struct create_backend_stt { typedef a_row_ type; }; template struct create_backend_stt<_row_tag,Transition,StateType> { typedef _row_ type; }; template struct create_backend_stt { typedef row_ type; }; // internal transitions template struct create_backend_stt { typedef g_irow_ type; }; template struct create_backend_stt { typedef a_irow_ type; }; template struct create_backend_stt { typedef irow_ type; }; template struct create_backend_stt<_irow_tag,Transition,StateType> { typedef _irow_ type; }; template struct create_backend_stt { typedef a_internal_ type; }; template struct create_backend_stt { typedef g_internal_ type; }; template struct create_backend_stt { typedef internal_ type; }; template struct create_backend_stt { typedef _internal_ type; }; template struct make_row_tag { typedef typename create_backend_stt::type type; }; // add to the stt the initial states which could be missing (if not being involved in a transition) template struct create_real_stt { //typedef typename BaseType::transition_table stt_simulated; typedef typename ::boost::mpl::fold< stt_simulated,mpl::vector0<>, ::boost::mpl::push_back< ::boost::mpl::placeholders::_1, make_row_tag< ::boost::mpl::placeholders::_2 , BaseType > > >::type type; }; template struct add_forwarding_row_helper { typedef typename generate_event_set::type all_events; typedef typename ::boost::mpl::fold< all_events, Intermediate, ::boost::mpl::push_back< ::boost::mpl::placeholders::_1, frow > >::type type; }; // gets the transition table from a composite and make from it a forwarding row template struct get_internal_transition_table { // first get the table of a composite typedef typename recursive_get_transition_table::type original_table; // add the internal events defined in the internal_transition_table // Note: these are added first because they must have a lesser prio // than the deeper transitions in the sub regions typedef typename StateType::internal_transition_table istt_simulated; typedef typename ::boost::mpl::fold< istt_simulated,::boost::mpl::vector0<>, ::boost::mpl::push_back< ::boost::mpl::placeholders::_1, make_row_tag< ::boost::mpl::placeholders::_2 , StateType> > >::type intermediate; // and add for every event a forwarding row typedef typename ::boost::mpl::eval_if< typename CompilePolicy::add_forwarding_rows, add_forwarding_row_helper, ::boost::mpl::identity >::type type; }; template struct get_internal_transition_table { typedef typename create_real_stt::type type; }; typedef typename create_real_stt::type real_transition_table; typedef typename create_stt::type stt; typedef typename get_initial_states::type initial_states; typedef typename generate_state_set::type state_list; typedef typename HistoryPolicy::template apply::type concrete_history; typedef typename ::boost::fusion::result_of::as_set::type substate_list; // extends the transition table with rows from composite states template struct extend_table { // add the init states //typedef typename create_stt::type stt; typedef typename Composite::stt Stt; // for every state, add its transition table (if any) // transformed as frow typedef typename ::boost::mpl::fold, get_internal_transition_table< ::boost::mpl::placeholders::_2, is_composite_state< ::boost::mpl::placeholders::_2> > > >::type type; }; // extend the table with tables from composite states typedef typename extend_table::type complete_table; // build a sequence of regions typedef typename get_regions_as_sequence::type seq_initial_states; // Member functions // start the state machine (calls entry of the initial state) void start() { // call on_entry on this SM (static_cast(this))->on_entry(fsm_initial_event(),*this); ::boost::mpl::for_each > (call_init(fsm_initial_event(),this)); // give a chance to handle an anonymous (eventless) transition handle_eventless_transitions_helper eventless_helper(this,true); eventless_helper.process_completion_event(); } // Main function used by clients of the derived FSM to make // transitions. Can also be called for internally (for example in an action method) generated events. template execute_return process_event(Event const& evt) { HandledEnum ret_handled=HANDLED_FALSE; // if the state machine has terminate or interrupt flags, check them, otherwise skip if (is_event_handling_blocked_helper ( ::boost::mpl::bool_::type::value>() ) ) return HANDLED_TRUE; // if a message queue is needed and processing is on the way if (!do_pre_msg_queue_helper (evt,::boost::mpl::bool_::type::value>()) ) { // wait for the end of current processing return HANDLED_TRUE; } else { // prepare the next deferred event for handling // if one defer is found in the SM, otherwise skip handle_defer_helper defer_helper(m_deferred_events_queue); defer_helper.do_pre_handle_deferred(); // process event HandledEnum handled = this->do_process_helper (evt,::boost::mpl::bool_::type::value>()); if (handled) { ret_handled = HANDLED_TRUE; } // process completion transitions BEFORE any other event in the pool (UML Standard 2.3 §15.3.14) handle_eventless_transitions_helper eventless_helper(this,(handled == HANDLED_TRUE)); eventless_helper.process_completion_event(); // after handling, take care of the deferred events defer_helper.do_post_handle_deferred(handled); // now check if some events were generated in a transition and was not handled // because of another processing, and if yes, start handling them do_post_msg_queue_helper(::boost::mpl::bool_::type::value>()); return ret_handled; } } // Getter that returns the current state of the FSM const int* current_state() const { return this->m_states; } template struct serialize_state { serialize_state(Archive& ar):ar_(ar){} template typename ::boost::enable_if< typename ::boost::mpl::or_< typename has_do_serialize::type, typename is_composite_state::type >::type ,void >::type operator()(T& t) const { ar_ & t; } template typename ::boost::disable_if< typename ::boost::mpl::or_< typename has_do_serialize::type, typename is_composite_state::type >::type ,void >::type operator()(T& t) const { // no state to serialize } Archive& ar_; }; template void serialize(Archive & ar, const unsigned int) { // invoke serialization of the base class (serialize_state(ar))(boost::serialization::base_object(*this)); // now our attributes ar & m_states; // queues cannot be serialized => skip ar & m_history; ar & m_event_processing; ar & m_is_included; // visitors cannot be serialized => skip ::boost::fusion::for_each(m_substate_list, serialize_state(ar)); } // linearly search for the state with the given id struct get_state_id_helper { get_state_id_helper(int id,const BaseState** res,const library_sm* self_): result_state(res),searched_id(id),self(self_) {} template void operator()(boost::msm::wrap const&) { // look for the state id until found BOOST_STATIC_CONSTANT(int, id = (get_state_id::value)); if (!*result_state && (id == searched_id)) { *result_state = &::boost::fusion::at_key(self->m_substate_list); } } const BaseState** result_state; int searched_id; const library_sm* self; }; // return the state whose id is passed or 0 if not found // caution if you need this, you probably need polymorphic states // complexity: O(number of states) const BaseState* get_state_by_id(int id) const { const BaseState* result_state=0; ::boost::mpl::for_each > (get_state_id_helper(id,&result_state,this)); return result_state; } // true if the sm is used in another sm bool is_contained() const { return m_is_included; } // get a state // as a pointer template typename ::boost::enable_if::type,State >::type get_state(::boost::msm::back::dummy<0> = 0) { return &(static_cast::type>::type > (::boost::fusion::at_key::type>(m_substate_list))); } // as a reference template typename ::boost::enable_if::type,State >::type get_state(::boost::msm::back::dummy<1> = 0) { return ::boost::fusion::at_key::type>(m_substate_list); } // checks if a flag is active using the BinaryOp as folding function template bool is_flag_active() { flag_handler* flags_entries = get_entries_for_flag(); return std::accumulate(m_states, m_states+nr_regions::value,false, ::boost::bind(typename BinaryOp::type(), ::boost::bind(::boost::apply(), ::boost::bind(::boost::msm::back::deref(), ::boost::bind(::boost::msm::back::plus2(), flags_entries, _2)), ::boost::ref(*this)), _1)); } // checks if a flag is active using no binary op if 1 region, or OR if > 1 regions template bool is_flag_active() { return FlagHelper1)>::helper(*this,get_entries_for_flag()); } // visit the currently active states (if these are defined as visitable // by implementing accept) void visit_current_states() { for (int i=0; i \ void visit_current_states(BOOST_PP_ENUM(n, MSM_VISIT_STATE_SUB, ~ ) ) \ { \ for (int i=0; i void defer_event(Event const& e) { // to call this function, you need either a state with a deferred_events typedef // or that the fsm provides the activate_deferred_events typedef BOOST_MPL_ASSERT(( has_fsm_deferred_events )); execute_return (library_sm::*pf) (Event const& evt)= &library_sm::process_event; Event temp (e); ::boost::function f= ::boost::bind(pf, this,temp); post_deferred_event(f); } protected: // interface for the derived class // helper used to fill the initial states struct init_states { init_states(int* const init):m_initial_states(init),m_index(-1){} // History initializer function object, used with mpl::for_each template void operator()(::boost::msm::wrap const&) { m_initial_states[++m_index]=get_state_id::type::value; } int* const m_initial_states; int m_index; }; public: struct update_state { update_state(substate_list& to_overwrite_):to_overwrite(&to_overwrite_){} template void operator()(StateType const& astate) const { ::boost::fusion::at_key(*to_overwrite)=astate; } substate_list* to_overwrite; }; template void set_states(Expr const& expr) { ::boost::fusion::for_each( ::boost::fusion::as_vector(FoldToList()(expr, boost::fusion::nil())),update_state(this->m_substate_list)); } // Construct with the default initial states state_machine() :Derived() ,m_events_queue() ,m_deferred_events_queue() ,m_history() ,m_event_processing(false) ,m_is_included(false) ,m_visitors() ,m_substate_list() { // initialize our list of states with the ones defined in Derived::initial_state ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap > (init_states(m_states)); m_history.set_initial_states(m_states); // create states fill_states(this); } template state_machine (Expr const& expr,typename ::boost::enable_if::type >::type* dummy=0) :Derived() ,m_events_queue() ,m_deferred_events_queue() ,m_history() ,m_event_processing(false) ,m_is_included(false) ,m_visitors() ,m_substate_list() { BOOST_MPL_ASSERT_MSG( ( ::boost::proto::matches::value), THE_STATES_EXPRESSION_PASSED_DOES_NOT_MATCH_GRAMMAR, (FoldToList)); // initialize our list of states with the ones defined in Derived::initial_state ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap > (init_states(m_states)); m_history.set_initial_states(m_states); // create states fill_states(this); set_states(expr); } // Construct with the default initial states and some default argument(s) #define MSM_CONSTRUCTOR_HELPER_EXECUTE_SUB(z, n, unused) ARG ## n t ## n #define MSM_CONSTRUCTOR_HELPER_EXECUTE(z, n, unused) \ template \ state_machine(BOOST_PP_ENUM(n, MSM_CONSTRUCTOR_HELPER_EXECUTE_SUB, ~ ), \ typename ::boost::disable_if::type >::type* dummy=0 ) \ :Derived(BOOST_PP_ENUM_PARAMS(n,t)) \ ,m_events_queue() \ ,m_deferred_events_queue() \ ,m_history() \ ,m_event_processing(false) \ ,m_is_included(false) \ ,m_visitors() \ ,m_substate_list() \ { \ ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap > \ (init_states(m_states)); \ m_history.set_initial_states(m_states); \ fill_states(this); \ } \ template \ state_machine(Expr const& expr,BOOST_PP_ENUM(n, MSM_CONSTRUCTOR_HELPER_EXECUTE_SUB, ~ ), \ typename ::boost::enable_if::type >::type* dummy=0 ) \ :Derived(BOOST_PP_ENUM_PARAMS(n,t)) \ ,m_events_queue() \ ,m_deferred_events_queue() \ ,m_history() \ ,m_event_processing(false) \ ,m_is_included(false) \ ,m_visitors() \ ,m_substate_list() \ { \ BOOST_MPL_ASSERT_MSG( \ ( ::boost::proto::matches::value), \ THE_STATES_EXPRESSION_PASSED_DOES_NOT_MATCH_GRAMMAR, \ (FoldToList)); \ ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap > \ (init_states(m_states)); \ m_history.set_initial_states(m_states); \ fill_states(this); \ set_states(expr); \ } BOOST_PP_REPEAT_FROM_TO(1,BOOST_PP_ADD(BOOST_MSM_CONSTRUCTOR_ARG_SIZE,1), MSM_CONSTRUCTOR_HELPER_EXECUTE, ~) #undef MSM_CONSTRUCTOR_HELPER_EXECUTE #undef MSM_CONSTRUCTOR_HELPER_EXECUTE_SUB // assignment operator using the copy policy to decide if non_copyable, shallow or deep copying is necessary library_sm& operator= (library_sm const& rhs) { if (this != &rhs) { Derived::operator=(rhs); // initialize our list of states with the ones defined in Derived::initial_state fill_states(this); do_copy(rhs); } return *this; } state_machine (library_sm const& rhs) : Derived(rhs) { if (this != &rhs) { // initialize our list of states with the ones defined in Derived::initial_state fill_states(this); do_copy(rhs); } } // the following 2 functions handle the terminate/interrupt states handling // if one of these states is found, the first one is used template bool is_event_handling_blocked_helper( ::boost::mpl::true_ const &) { // if the state machine is terminated, do not handle any event if (is_flag_active< ::boost::msm::TerminateFlag>()) return true; // if the state machine is interrupted, do not handle any event // unless the event is the end interrupt event if ( is_flag_active< ::boost::msm::InterruptedFlag>() && !is_flag_active< ::boost::msm::EndInterruptFlag >()) return true; return false; } // otherwise simple handling, no flag => continue template bool is_event_handling_blocked_helper( ::boost::mpl::false_ const &) { // no terminate/interrupt states detected return false; } // the following functions handle pre/post-process handling of a message queue template bool do_pre_msg_queue_helper(EventType const& evt, ::boost::mpl::true_ const &) { // no message queue needed return true; } template bool do_pre_msg_queue_helper(EventType const& evt, ::boost::mpl::false_ const &) { execute_return (library_sm::*pf) (EventType const& evt) = &library_sm::process_event; // if we are already processing an event if (m_event_processing) { // event has to be put into the queue transition_fct f = ::boost::bind(pf,this,evt); m_events_queue.m_events_queue.push(f); return false; } // event can be handled, processing m_event_processing = true; return true; } void do_post_msg_queue_helper( ::boost::mpl::true_ const &) { // no message queue needed } void do_post_msg_queue_helper( ::boost::mpl::false_ const &) { m_event_processing = false; process_message_queue(this); } // the following 2 functions handle the processing either with a try/catch protection or without template HandledEnum do_process_helper(EventType const& evt, ::boost::mpl::true_ const &) { return this->do_process_event(evt); } template HandledEnum do_process_helper(EventType const& evt, ::boost::mpl::false_ const &) { try { return this->do_process_event(evt); } catch (std::exception& e) { // give a chance to the concrete state machine to handle this->exception_caught(evt,*this,e); } return HANDLED_FALSE; } // handling of deferred events // if none is found in the SM, take the following empty main version template struct handle_defer_helper { handle_defer_helper(deferred_msg_queue_helper& ){} void do_pre_handle_deferred() { } void do_post_handle_deferred(HandledEnum) { } }; // otherwise the standard version handling the deferred events template struct handle_defer_helper ::type >::type> { handle_defer_helper(deferred_msg_queue_helper& a_queue): events_queue(a_queue),next_deferred_event(){} void do_pre_handle_deferred() { if (!events_queue.m_deferred_events_queue.empty()) { next_deferred_event = events_queue.m_deferred_events_queue.back(); events_queue.m_deferred_events_queue.pop_back(); } } void do_post_handle_deferred(HandledEnum handled) { if (((handled & HANDLED_DEFERRED) == HANDLED_DEFERRED) && next_deferred_event ) { // the event was already deferred, no reason to process another deferred event events_queue.m_deferred_events_queue.push_back(next_deferred_event); return; } else if (next_deferred_event) { next_deferred_event(); } } private: deferred_msg_queue_helper& events_queue; deferred_fct next_deferred_event; }; // handling of eventless transitions // if none is found in the SM, nothing to do template struct handle_eventless_transitions_helper { handle_eventless_transitions_helper(library_sm* , bool ){} void process_completion_event(){} }; // otherwise template struct handle_eventless_transitions_helper ::type >::type> { handle_eventless_transitions_helper(library_sm* self_, bool handled_):self(self_),handled(handled_){} void process_completion_event() { typedef typename ::boost::mpl::deref< typename ::boost::mpl::begin< typename find_completion_events::type >::type >::type first_completion_event; if (handled) { self->process_event(first_completion_event() ); } } private: library_sm* self; bool handled; }; template struct region_processing_helper { public: region_processing_helper(library_sm* self_,HandledEnum& result_) :self(self_),result(result_){} template void process(Event const& evt) { // use this table as if it came directly from the user typedef dispatch_table table; HandledEnum res = table::instance.entries[self->m_states[0]]( *self, 0, self->m_states[0], evt); result = (HandledEnum)((int)result | (int)res); } library_sm* self; HandledEnum& result; }; // version with visitors template struct region_processing_helper >::type> { private: // process event in one region template struct In { template static void process(Event const& evt,library_sm* self_,HandledEnum& result_) { // use this table as if it came directly from the user typedef dispatch_table table; HandledEnum res = table::instance.entries[self_->m_states[region_id::value]]( *self_, region_id::value , self_->m_states[region_id::value], evt); result_ = (HandledEnum)((int)result_ | (int)res); In< ::boost::mpl::int_ >::process(evt,self_,result_); } }; template struct In< ::boost::mpl::int_,Dummy> { // end of processing template static void process(Event const& evt,library_sm*,HandledEnum&){} }; public: region_processing_helper(library_sm* self_,HandledEnum& result_) :self(self_),result(result_){} template void process(Event const& evt) { // use this table as if it came directly from the user typedef dispatch_table table; In< ::boost::mpl::int_<0> >::process(evt,self,result); } library_sm* self; HandledEnum& result; }; // minimum event processing without exceptions, queues, etc. template HandledEnum do_process_event(Event const& evt) { HandledEnum handled = HANDLED_FALSE; // dispatch the event to every region region_processing_helper helper(this,handled); helper.process(evt); // if the event has not been handled and we have orthogonal zones, then // generate an error on every active state // for state machine states contained in other state machines, do not handle // but let the containing sm handle the error // completion events do not produce an error if (!handled && !is_contained() && !is_completion_event::type::value) { for (int i=0; ino_transition(evt,*this,this->m_states[i]); } } return handled; } // default row arguments for the compilers which accept this template bool no_guard(Event const&){return true;} template void no_action(Event const&){} #ifndef BOOST_NO_RTTI HandledEnum process_any_event( ::boost::any const& evt); #endif private: // composite accept implementation. First calls accept on the composite, then accept on all its active states. void composite_accept() { this->accept(); this->visit_current_states(); } #define MSM_COMPOSITE_ACCEPT_SUB(z, n, unused) ARG ## n vis ## n #define MSM_COMPOSITE_ACCEPT_EXECUTE(z, n, unused) \ template \ void composite_accept(BOOST_PP_ENUM(n, MSM_COMPOSITE_ACCEPT_SUB, ~ ) ) \ { \ this->accept(BOOST_PP_ENUM_PARAMS(n,vis)); \ this->visit_current_states(BOOST_PP_ENUM_PARAMS(n,vis)); \ } BOOST_PP_REPEAT_FROM_TO(1,BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_COMPOSITE_ACCEPT_EXECUTE, ~) #undef MSM_COMPOSITE_ACCEPT_EXECUTE #undef MSM_COMPOSITE_ACCEPT_SUB // helper used to call the init states at the start of the state machine template struct call_init { call_init(Event const& an_event,library_sm* self_): evt(an_event),self(self_){} template void operator()(boost::msm::wrap const&) { execute_entry(::boost::fusion::at_key(self->m_substate_list),evt,*self); } private: Event const& evt; library_sm* self; }; // helper for flag handling. Uses OR by default on orthogonal zones. template struct FlagHelper { static bool helper(library_sm& sm,flag_handler* ) { // by default we use OR to accumulate the flags return sm.is_flag_active(); } }; template struct FlagHelper { static bool helper(library_sm& sm,flag_handler* flags_entries) { // just one active state, so we can call operator[] with 0 return flags_entries[sm.current_state()[0]](sm); } }; // handling of flag // defines a true and false functions plus a forwarding one for composite states template struct FlagHandler { static bool flag_true(library_sm& ) { return true; } static bool flag_false(library_sm& ) { return false; } static bool forward(library_sm& fsm) { return ::boost::fusion::at_key(fsm.m_substate_list).template is_flag_active(); } }; template struct init_flags { private: // helper function, helps hiding the forward function for non-state machines states. template void helper (flag_handler* an_entry,int offset, ::boost::mpl::true_ const & ) { // composite => forward an_entry[offset] = &FlagHandler::forward; } template void helper (flag_handler* an_entry,int offset, ::boost::mpl::false_ const & ) { // default no flag an_entry[offset] = &FlagHandler::flag_false; } // attributes flag_handler* entries; public: init_flags(flag_handler* entries_) : entries(entries_) {} // Flags initializer function object, used with mpl::for_each template void operator()( ::boost::msm::wrap const& ) { typedef typename StateType::flag_list flags; typedef typename ::boost::mpl::contains::type found; typedef typename is_composite_state::type composite; BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::type::value)); if (found::type::value) { // the type defined the flag => true entries[state_id] = &FlagHandler::flag_true; } else { // false or forward typedef typename ::boost::mpl::and_< typename is_composite_state::type, typename ::boost::mpl::not_< typename has_non_forwarding_flag::type>::type >::type composite_no_forward; helper(entries,state_id,::boost::mpl::bool_()); } } }; // maintains for every flag a static array containing the flag value for every state template flag_handler* get_entries_for_flag() { BOOST_STATIC_CONSTANT(int, max_state = (mpl::size::value)); static flag_handler flags_entries[max_state]; // build a state list ::boost::mpl::for_each > (init_flags(flags_entries)); return flags_entries; } // helper used to create a state using the correct constructor template struct create_state_helper { static void set_sm(library_sm* ) { // state doesn't need its sm } }; // create a state requiring a pointer to the state machine template struct create_state_helper::type> { static void set_sm(library_sm* sm) { // create and set the fsm ::boost::fusion::at_key(sm->m_substate_list).set_sm_ptr(sm); } }; // main unspecialized helper class template struct visitor_args; #define MSM_VISITOR_ARGS_SUB(z, n, unused) BOOST_PP_CAT(_,BOOST_PP_ADD(n,1)) #define MSM_VISITOR_ARGS_TYPEDEF_SUB(z, n, unused) typename StateType::accept_sig::argument ## n #define MSM_VISITOR_ARGS_EXECUTE(z, n, unused) \ template \ struct visitor_args \ { \ template \ static typename enable_if_c::value,void >::type \ helper (library_sm* sm, \ int id,StateType& astate) \ { \ sm->m_visitors.insert(id, boost::bind(&StateType::accept, \ ::boost::ref(astate) BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_SUB, ~) )); \ } \ template \ static typename enable_if_c::value,void >::type \ helper (library_sm* sm, \ int id,StateType& astate) \ { \ void (StateType::*caccept)(BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_TYPEDEF_SUB, ~ ) ) \ = &StateType::composite_accept; \ sm->m_visitors.insert(id, boost::bind(caccept, \ ::boost::ref(astate) BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_SUB, ~) )); \ } \ }; BOOST_PP_REPEAT(BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_VISITOR_ARGS_EXECUTE, ~) #undef MSM_VISITOR_ARGS_EXECUTE #undef MSM_VISITOR_ARGS_SUB // the IBM compiler seems to have problems with nested classes // the same seems to apply to the Apple version of gcc 4.0.1 (just in case we do for < 4.1) // and also to MS VC < 8 #if defined (__IBMCPP__) || (defined (__APPLE_CC__) && (__GNUC__ == 4 && __GNUC_MINOR__ < 1)) || (defined(_MSC_VER) && (_MSC_VER < 1400)) public: #endif template void set_containing_sm(ContainingSM* sm) { m_is_included=true; ::boost::fusion::for_each(m_substate_list,add_state(this,sm)); } #if defined (__IBMCPP__) || (defined (__APPLE_CC__) && (__GNUC__ == 4 && __GNUC_MINOR__ < 1)) || (defined(_MSC_VER) && (_MSC_VER < 1400)) private: #endif // A function object for use with mpl::for_each that stuffs // states into the state list. template struct add_state { add_state(library_sm* self_,ContainingSM* sm) : self(self_),containing_sm(sm){} // State is a sub fsm with exit pseudo states and gets a pointer to this fsm, so it can build a callback template typename ::boost::enable_if< typename is_composite_state::type,void >::type new_state_helper(boost::msm::back::dummy<0> = 0) const { ::boost::fusion::at_key(self->m_substate_list).set_containing_sm(containing_sm); } // State is a sub fsm without exit pseudo states and does not get a callback to this fsm // or state is a normal state and needs nothing except creation template typename ::boost::enable_if< typename boost::mpl::and_::type>::type, typename boost::mpl::not_ ::type>::type >::type,void>::type new_state_helper( ::boost::msm::back::dummy<1> = 0) const { //nothing to do } // state is exit pseudo state and gets callback to target fsm template typename ::boost::enable_if::type,void >::type new_state_helper( ::boost::msm::back::dummy<2> = 0) const { execute_return (ContainingSM::*pf) (typename StateType::event const& evt)= &ContainingSM::process_event; ::boost::function fct = ::boost::bind(pf,containing_sm,_1); ::boost::fusion::at_key(self->m_substate_list).set_forward_fct(fct); } // for every defined state in the sm template void operator()( State const&) const { //create a new state with the defined id and type BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::value)); this->new_state_helper(), create_state_helper::set_sm(self); // create a visitor callback visitor_helper(state_id,::boost::fusion::at_key(self->m_substate_list), ::boost::mpl::bool_::type::value>()); } private: // support possible use of a visitor if accept_sig is defined template void visitor_helper(int id,StateType& astate, ::boost::mpl::true_ const & ) const { visitor_args:: template helper(self,id,astate); } template void visitor_helper(int ,StateType& , ::boost::mpl::false_ const &) const { // nothing to do } library_sm* self; ContainingSM* containing_sm; }; // helper used to copy every state if needed struct copy_helper { copy_helper(library_sm* sm): m_sm(sm){} template void operator()( ::boost::msm::wrap const& ) { BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::type::value)); // possibly also set the visitor visitor_helper(state_id); // and for states that keep a pointer to the fsm, reset the pointer create_state_helper::set_sm(m_sm); } template typename ::boost::enable_if::type,void >::type visitor_helper(int id) const { visitor_args::template helper (m_sm,id,::boost::fusion::at_key(m_sm->m_substate_list)); } template typename ::boost::disable_if::type,void >::type visitor_helper(int id) const { // nothing to do } library_sm* m_sm; }; // helper to copy the active states attribute template struct region_copy_helper { static void do_copy(library_sm* self_,library_sm const& rhs) { self_->m_states[region_id::value] = rhs.m_states[region_id::value]; region_copy_helper< ::boost::mpl::int_ >::do_copy(self_,rhs); } }; template struct region_copy_helper< ::boost::mpl::int_,Dummy> { // end of processing static void do_copy(library_sm*,library_sm const& ){} }; // copy functions for deep copy (no need of a 2nd version for NoCopy as noncopyable handles it) void do_copy (library_sm const& rhs, ::boost::msm::back::dummy<0> = 0) { // deep copy simply assigns the data region_copy_helper< ::boost::mpl::int_<0> >::do_copy(this,rhs); m_events_queue = rhs.m_events_queue; m_deferred_events_queue = rhs.m_deferred_events_queue; m_history = rhs.m_history; m_event_processing = rhs.m_event_processing; m_is_included = rhs.m_is_included; m_substate_list = rhs.m_substate_list; // except for the states themselves, which get duplicated ::boost::mpl::for_each > (copy_helper(this)); } // helper used to call the correct entry/exit method // unfortunately in O(number of states in the sub-sm) but should be better than a virtual call template struct entry_exit_helper { entry_exit_helper(int id,Event const& e,library_sm* self_): state_id(id),evt(e),self(self_){} // helper for entry actions template typename ::boost::enable_if::type helper( ::boost::msm::back::dummy<0> = 0) { BOOST_STATIC_CONSTANT(int, id = (get_state_id::value)); if (id == state_id) { execute_entry(::boost::fusion::at_key(self->m_substate_list),evt,*self); } } // helper for exit actions template typename boost::disable_if::type helper( ::boost::msm::back::dummy<1> = 0) { BOOST_STATIC_CONSTANT(int, id = (get_state_id::value)); if (id == state_id) { execute_exit(::boost::fusion::at_key(self->m_substate_list),evt,*self); } } // iterates through all states to find the one to be activated template void operator()( ::boost::msm::wrap const&) { entry_exit_helper::template helper< ::boost::mpl::bool_,State >(); } private: int state_id; Event const& evt; library_sm* self; }; // helper to start the fsm template struct region_start_helper { template static void do_start(library_sm* self_,Event const& incomingEvent) { //forward the event for handling by sub state machines ::boost::mpl::for_each > (entry_exit_helper(self_->m_states[region_id::value],incomingEvent,self_)); region_start_helper < ::boost::mpl::int_ >::do_start(self_,incomingEvent); } }; template struct region_start_helper< ::boost::mpl::int_,Dummy> { // end of processing template static void do_start(library_sm*,Event const& ){} }; // start for states machines which are themselves embedded in other state machines (composites) template void start(Event const& incomingEvent) { region_start_helper< ::boost::mpl::int_<0> >::do_start(this,incomingEvent); } // helper used to set the correct state as active state upon entry into a fsm struct direct_event_start_helper { direct_event_start_helper(library_sm* self_):self(self_){} // this variant is for the standard case, entry due to activation of the containing FSM template typename ::boost::disable_if::type,void>::type operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<0> = 0) { (static_cast(self))->on_entry(evt,fsm); self->start(evt); } // this variant is for the direct entry case (just one entry, not a sequence of entries) template typename ::boost::enable_if< typename ::boost::mpl::and_< typename ::boost::mpl::not_< typename is_pseudo_entry< typename EventType::active_state>::type >::type, typename ::boost::mpl::and_::type, typename ::boost::mpl::not_::type >::type >::type>::type,void >::type operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0) { (static_cast(self))->on_entry(evt,fsm); int state_id = get_state_id::value; BOOST_STATIC_ASSERT(EventType::active_state::zone_index >= 0); BOOST_STATIC_ASSERT(EventType::active_state::zone_index <= nr_regions::value); // just set the correct zone, the others will be default/history initialized self->m_states[EventType::active_state::zone_index] = state_id; self->start(evt.m_event); } // this variant is for the fork entry case (a sequence on entries) template typename ::boost::enable_if< typename ::boost::mpl::and_< typename ::boost::mpl::not_< typename is_pseudo_entry::type >::type, typename ::boost::mpl::and_::type, typename ::boost::mpl::is_sequence< typename EventType::active_state>::type >::type>::type,void >::type operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<2> = 0) { (static_cast(self))->on_entry(evt,fsm); ::boost::mpl::for_each > (fork_helper(self,evt)); // set the correct zones, the others (if any) will be default/history initialized self->start(evt.m_event); } // this variant is for the pseudo state entry case template typename ::boost::enable_if< typename is_pseudo_entry::type,void >::type operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<3> = 0) { // entry on the FSM (static_cast(self))->on_entry(evt,fsm); int state_id = get_state_id::value; // given region starts with the entry pseudo state as active state self->m_states[EventType::active_state::zone_index] = state_id; self->start(evt.m_event); // and we process the transition in the zone of the newly active state // (entry pseudo states are, according to UML, a state connecting 1 transition outside to 1 inside self->process_event(evt.m_event); } private: // helper for the fork case, does almost like the direct entry library_sm* self; template struct fork_helper { fork_helper(library_sm* self_,EventType const& evt_): helper_self(self_),helper_evt(evt_){} template void operator()( ::boost::msm::wrap const& ) { int state_id = get_state_id::value; BOOST_STATIC_ASSERT(StateType::zone_index >= 0); BOOST_STATIC_ASSERT(StateType::zone_index <= nr_regions::value); helper_self->m_states[StateType::zone_index] = state_id; } private: library_sm* helper_self; EventType const& helper_evt; }; }; // helper for entry template struct region_entry_exit_helper { template static void do_entry(library_sm* self_,Event const& incomingEvent) { self_->m_states[region_id::value] = self_->m_history.history_entry(incomingEvent)[region_id::value]; region_entry_exit_helper < ::boost::mpl::int_ >::do_entry(self_,incomingEvent); } template static void do_exit(library_sm* self_,Event const& incomingEvent) { ::boost::mpl::for_each > (entry_exit_helper(self_->m_states[region_id::value],incomingEvent,self_)); region_entry_exit_helper < ::boost::mpl::int_ >::do_exit(self_,incomingEvent); } }; template struct region_entry_exit_helper< ::boost::mpl::int_,Dummy> { // end of processing template static void do_entry(library_sm*,Event const& ){} template static void do_exit(library_sm*,Event const& ){} }; // entry/exit for states machines which are themselves embedded in other state machines (composites) template void do_entry(Event const& incomingEvent,FsmType& fsm) { // by default we activate the history/init states, can be overwritten by direct_event_start_helper region_entry_exit_helper< ::boost::mpl::int_<0> >::do_entry(this,incomingEvent); // block immediate handling of events m_event_processing = true; // if the event is generating a direct entry/fork, set the current state(s) to the direct state(s) direct_event_start_helper(this)(incomingEvent,fsm); // handle messages which were generated and blocked in the init calls m_event_processing = false; process_message_queue(this); } template void do_exit(Event const& incomingEvent,FsmType& fsm) { // first recursively exit the sub machines // forward the event for handling by sub state machines region_entry_exit_helper< ::boost::mpl::int_<0> >::do_exit(this,incomingEvent); // then call our own exit (static_cast(this))->on_exit(incomingEvent,fsm); // give the history a chance to handle this (or not). m_history.history_exit(this->m_states); } // the IBM and VC<8 compilers seem to have problems with the friend declaration of dispatch_table #if defined (__IBMCPP__) || (defined(_MSC_VER) && (_MSC_VER < 1400)) public: #endif // no transition for event. template static HandledEnum call_no_transition(library_sm& , int , int , Event const& ) { return HANDLED_FALSE; } // called for deferred events. Address set in the dispatch_table at init template static HandledEnum defer_transition(library_sm& fsm, int , int , Event const& e) { fsm.defer_event(e); return HANDLED_DEFERRED; } // called for completion events. Default address set in the dispatch_table at init // prevents no-transition detection for completion events template static HandledEnum default_eventless_transition(library_sm& fsm, int, int , Event const& e) { return HANDLED_FALSE; } #if defined (__IBMCPP__) || (defined(_MSC_VER) && (_MSC_VER < 1400)) private: #endif // puts a deferred event in the queue void post_deferred_event(deferred_fct& deferred) { m_deferred_events_queue.m_deferred_events_queue.push_front(deferred); } // removes one event from the message queue and processes it template typename ::boost::disable_if::type,void >::type process_message_queue(StateType*) { if (!m_events_queue.m_events_queue.empty()) { transition_fct to_call = m_events_queue.m_events_queue.front(); m_events_queue.m_events_queue.pop(); to_call(); } } template typename ::boost::enable_if::type,void >::type process_message_queue(StateType*) { // nothing to process } // calls the entry/exit or on_entry/on_exit depending on the state type // (avoids calling virtually) // variant for FSMs template static typename boost::enable_if::type,void >::type execute_entry(StateType& astate,EventType const& evt,FsmType& fsm,boost::msm::back::dummy<0> = 0) { // calls on_entry on the fsm then handles direct entries, fork, entry pseudo state astate.do_entry(evt,fsm); } // variant for states template static typename ::boost::disable_if< typename ::boost::mpl::or_::type, typename is_pseudo_exit::type >::type,void >::type execute_entry(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0) { // simple call to on_entry astate.on_entry(evt,fsm); } // variant for exit pseudo states template static typename ::boost::enable_if::type,void >::type execute_entry(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<2> = 0) { // calls on_entry on the state then forward the event to the transition which should be defined inside the // contained fsm astate.on_entry(evt,fsm); astate.forward_event(evt); } template static typename ::boost::enable_if::type,void >::type execute_exit(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<0> = 0) { astate.do_exit(evt,fsm); } template static typename ::boost::disable_if::type,void >::type execute_exit(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0) { // simple call to on_exit astate.on_exit(evt,fsm); } // helper allowing special handling of direct entries / fork template static typename ::boost::disable_if< typename ::boost::mpl::or_::type, ::boost::mpl::is_sequence >::type,void>::type convert_event_and_execute_entry(StateType& astate,EventType const& evt, FsmType& fsm, ::boost::msm::back::dummy<1> = 0) { // if the target is a normal state, do the standard entry handling execute_entry(astate,evt,fsm); } template static typename ::boost::enable_if< typename ::boost::mpl::or_::type, ::boost::mpl::is_sequence >::type,void >::type convert_event_and_execute_entry(StateType& astate,EventType const& evt, FsmType& fsm, ::boost::msm::back::dummy<0> = 0) { // for the direct entry, pack the event in a wrapper so that we handle it differently during fsm entry execute_entry(astate,msm::back::direct_entry_event(evt),fsm); } // creates all the states template void fill_states(ContainingSM* containing_sm=0) { BOOST_STATIC_CONSTANT(int, max_state = (mpl::size::value)); // allocate the place without reallocation m_visitors.fill_visitors(max_state); ::boost::fusion::for_each(m_substate_list,add_state(this,containing_sm)); } private: template struct msg_queue_helper { public: msg_queue_helper():m_events_queue(){} events_queue_t m_events_queue; }; template struct msg_queue_helper::type >::type> { }; template friend struct dispatch_table; // data members int m_states[nr_regions::value]; msg_queue_helper m_events_queue; deferred_msg_queue_helper m_deferred_events_queue; concrete_history m_history; bool m_event_processing; bool m_is_included; visitor_fct_helper m_visitors; substate_list m_substate_list; }; } } }// boost::msm::back #endif //BOOST_MSM_BACK_STATEMACHINE_H