// 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_FAVOR_COMPILE_TIME_H #define BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H #include #include #include #include #include #include #include #include #include #include namespace boost { namespace msm { namespace back { template struct process_any_event_helper { process_any_event_helper(msm::back::HandledEnum& res_,Fsm* self_,::boost::any any_event_): res(res_),self(self_),any_event(any_event_),finished(false){} template void operator()(boost::msm::wrap const&) { if ( ! finished && ::boost::any_cast(&any_event)!=0) { finished = true; res = self->process_event_internal(::boost::any_cast(any_event),false); } } private: msm::back::HandledEnum& res; Fsm* self; ::boost::any any_event; bool finished; }; #define BOOST_MSM_BACK_GENERATE_PROCESS_EVENT(fsmname) \ namespace boost { namespace msm { namespace back{ \ template<> \ ::boost::msm::back::HandledEnum fsmname::process_any_event( ::boost::any const& any_event) \ { \ typedef ::boost::msm::back::recursive_get_transition_table::type stt; \ typedef ::boost::msm::back::generate_event_set::type stt_events; \ typedef ::boost::msm::back::recursive_get_internal_transition_table::type istt; \ typedef ::boost::msm::back::generate_event_set::type >::type istt_events; \ typedef ::boost::msm::back::set_insert_range::type all_events; \ ::boost::msm::back::HandledEnum res= ::boost::msm::back::HANDLED_FALSE; \ ::boost::mpl::for_each > \ (::boost::msm::back::process_any_event_helper(res,this,any_event)); \ return res; \ } \ }}} struct favor_compile_time { typedef int compile_policy; typedef ::boost::mpl::false_ add_forwarding_rows; }; // Generates a singleton runtime lookup table that maps current state // to a function that makes the SM take its transition on the given // Event type. template struct dispatch_table < Fsm, Stt, Event, ::boost::msm::back::favor_compile_time> { private: // This is a table of these function pointers. typedef HandledEnum (*cell)(Fsm&, int,int,Event const&); typedef bool (*guard)(Fsm&, Event const&); // Compute the maximum state value in the sm so we know how big // to make the table typedef typename generate_state_set::type state_list; BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size::value)); struct chain_row { HandledEnum operator()(Fsm& fsm, int region,int state,Event const& evt) const { HandledEnum res = HANDLED_FALSE; typename std::deque::const_iterator it = one_state.begin(); while (it != one_state.end() && (res != HANDLED_TRUE && res != HANDLED_DEFERRED )) { HandledEnum handled = (*it)(fsm,region,state,evt); // reject is considered as erasing an error (HANDLED_FALSE) if ((HANDLED_FALSE==handled) && (HANDLED_GUARD_REJECT==res) ) res = HANDLED_GUARD_REJECT; else res = handled; ++it; } return res; } std::deque one_state; }; template static HandledEnum call_submachine(Fsm& fsm, int , int , Event const& evt) { return (fsm.template get_state()).process_any_event( ::boost::any(evt)); } // A function object for use with mpl::for_each that stuffs // transitions into cells. struct init_cell { init_cell(dispatch_table* self_) : self(self_) {} // version for transition event not base of our event template typename ::boost::disable_if< typename ::boost::is_same::type ,void>::type init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const { typedef typename create_stt::type stt; BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::value)); self->entries[state_id+1].one_state.push_front(reinterpret_cast(&Transition::execute)); } template typename ::boost::enable_if< typename ::boost::is_same::type ,void>::type init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const { self->entries[0].one_state.push_front(reinterpret_cast(&Transition::execute)); } // version for transition event base of our event template typename ::boost::disable_if< typename ::boost::is_same::type ,void>::type init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const { typedef typename create_stt::type stt; BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::value)); self->entries[state_id+1].one_state.push_front(&Transition::execute); } template typename ::boost::enable_if< typename ::boost::is_same::type ,void>::type init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const { self->entries[0].one_state.push_front(&Transition::execute); } // Cell initializer function object, used with mpl::for_each template typename ::boost::enable_if::type,void >::type operator()(Transition const&,boost::msm::back::dummy<0> = 0) const { // version for not real rows. No problem because irrelevant for process_event } template typename ::boost::disable_if::type,void >::type operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const { //only if the transition event is a base of our event is the reinterpret_case safe init_event_base_case(tr, ::boost::mpl::bool_< ::boost::is_base_of::type::value>() ); } dispatch_table* self; }; // Cell default-initializer function object, used with mpl::for_each // initializes with call_no_transition, defer_transition or default_eventless_transition // variant for non-anonymous transitions template struct default_init_cell { default_init_cell(dispatch_table* self_,chain_row* tofill_entries_) : self(self_),tofill_entries(tofill_entries_) {} template struct helper {}; template struct helper { template static void execute(boost::msm::wrap const&,chain_row* tofill) { typedef typename create_stt::type stt; BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::value)); cell call_no_transition = &Fsm::defer_transition; tofill[state_id+1].one_state.push_back(call_no_transition); } }; template struct helper { template static void execute(boost::msm::wrap const&,chain_row* tofill) { typedef typename create_stt::type stt; BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::value)); cell call_no_transition = &Fsm::defer_transition; tofill[state_id+1].one_state.push_back(call_no_transition); } }; template struct helper { template static typename ::boost::enable_if< typename ::boost::is_same::type ,void>::type execute(boost::msm::wrap const&,chain_row* tofill,boost::msm::back::dummy<0> = 0) { // for internal tables cell call_no_transition_internal = &Fsm::call_no_transition; tofill[0].one_state.push_front(call_no_transition_internal); } template static typename ::boost::disable_if< typename ::boost::is_same::type ,void>::type execute(boost::msm::wrap const&,chain_row* tofill,boost::msm::back::dummy<1> = 0) { typedef typename create_stt::type stt; BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::value)); cell call_no_transition = &call_submachine< State >; tofill[state_id+1].one_state.push_front(call_no_transition); } }; template struct helper { template static void execute(boost::msm::wrap const&,chain_row* tofill) { typedef typename create_stt::type stt; BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::value)); cell call_no_transition = &Fsm::call_no_transition; tofill[state_id+1].one_state.push_back(call_no_transition); } }; template void operator()(boost::msm::wrap const& s) { helper::type::value, is_composite_state::type::value>::execute(s,tofill_entries); } dispatch_table* self; chain_row* tofill_entries; }; // variant for anonymous transitions template struct default_init_cell::type>::type> { default_init_cell(dispatch_table* self_,chain_row* tofill_entries_) : self(self_),tofill_entries(tofill_entries_) {} // this event is a compound one (not a real one, just one for use in event-less transitions) // Note this event cannot be used as deferred! template void operator()(boost::msm::wrap const&) { typedef typename create_stt::type stt; BOOST_STATIC_CONSTANT(int, state_id = (get_state_id::value)); cell call_no_transition = &Fsm::default_eventless_transition; tofill_entries[state_id+1].one_state.push_back(call_no_transition); } dispatch_table* self; chain_row* tofill_entries; }; public: // initialize the dispatch table for a given Event and Fsm dispatch_table() { // Initialize cells for no transition ::boost::mpl::for_each< ::boost::mpl::filter_view< Stt, ::boost::is_base_of, Event> > > (init_cell(this)); ::boost::mpl::for_each< typename generate_state_set::type, boost::msm::wrap< ::boost::mpl::placeholders::_1> > (default_init_cell(this,entries)); } // The singleton instance. static const dispatch_table instance; public: // data members chain_row entries[max_state+1]; }; template const boost::msm::back::dispatch_table dispatch_table::instance; }}} // boost::msm::back #endif //BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H