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