123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- #ifndef BOOST_MSM_BACK_DISPATCH_TABLE_H
- #define BOOST_MSM_BACK_DISPATCH_TABLE_H
- #include <cstdint>
- #include <utility>
- #include <boost/mpl/reverse_fold.hpp>
- #include <boost/mpl/greater.hpp>
- #include <boost/mpl/filter_view.hpp>
- #include <boost/mpl/pop_front.hpp>
- #include <boost/mpl/for_each.hpp>
- #include <boost/mpl/advance.hpp>
- #include <boost/type_traits/is_base_of.hpp>
- #include <boost/type_traits/is_same.hpp>
- #include <boost/msm/event_traits.hpp>
- #include <boost/msm/back/metafunctions.hpp>
- #include <boost/msm/back/common_types.hpp>
- BOOST_MPL_HAS_XXX_TRAIT_DEF(is_frow)
- namespace boost { namespace msm { namespace back
- {
- template <class Fsm,class Stt, class Event,class CompilePolicy>
- struct dispatch_table
- {
- private:
-
- typedef HandledEnum (*cell)(Fsm&, int,int,Event const&);
- typedef bool (*guard)(Fsm&, Event const&);
-
-
- template< typename Seq,typename AnEvent,typename State >
- struct chain_row
- {
- typedef State current_state_type;
- typedef AnEvent transition_event;
-
- struct execute_helper
- {
- template <class Sequence>
- static
- HandledEnum
- execute(Fsm& , int, int, Event& , ::boost::mpl::true_ const & )
- {
-
- return HANDLED_FALSE;
- }
- template <class Sequence>
- static
- HandledEnum
- execute(Fsm& fsm, int region_index , int state, Event& evt,
- ::boost::mpl::false_ const & )
- {
-
- typedef typename ::boost::mpl::front<Sequence>::type first_row;
- HandledEnum res = first_row::execute(fsm,region_index,state,evt);
- if (HANDLED_TRUE!=res && HANDLED_DEFERRED!=res)
- {
-
- HandledEnum sub_res =
- execute<typename ::boost::mpl::pop_front<Sequence>::type>(fsm,region_index,state,evt,
- ::boost::mpl::bool_<
- ::boost::mpl::empty<typename ::boost::mpl::pop_front<Sequence>::type>::type::value>());
-
- if ((HANDLED_FALSE==sub_res) && (HANDLED_GUARD_REJECT==res) )
- return HANDLED_GUARD_REJECT;
- else
- return sub_res;
- }
- return res;
- }
- };
-
- static HandledEnum execute(Fsm& fsm, int region_index, int state, Event& evt)
- {
-
- return execute_helper::template execute<Seq>(fsm,region_index,state,evt,
- ::boost::mpl::bool_< ::boost::mpl::empty<Seq>::type::value>());
- }
- };
-
- template< typename Entry >
- struct make_chain_row_from_map_entry
- {
-
-
- enum {number_frows = ::boost::mpl::count_if< typename Entry::second,has_is_frow< ::boost::mpl::placeholders::_1> >::value};
-
- template<class Sequence, int NumberToDelete>
- struct erase_first_rows
- {
- typedef typename ::boost::mpl::erase<
- typename Entry::second,
- typename ::boost::mpl::begin<Sequence>::type,
- typename ::boost::mpl::advance<
- typename ::boost::mpl::begin<Sequence>::type,
- ::boost::mpl::int_<NumberToDelete> >::type
- >::type type;
- };
-
- typedef typename ::boost::mpl::eval_if<
- typename ::boost::mpl::bool_< number_frows >= 2 >::type,
- erase_first_rows<typename Entry::second,number_frows-1>,
- ::boost::mpl::identity<typename Entry::second>
- >::type filtered_stt;
- typedef chain_row<filtered_stt,Event,
- typename Entry::first > type;
- };
-
- template <class Transition,class NewEvent>
- struct replace_event
- {
- typedef typename Transition::template replace_event<NewEvent>::type type;
- };
-
-
- template <class FrowTransition>
- struct change_frow_event
- {
- typedef typename ::boost::mpl::eval_if<
- typename has_is_frow<FrowTransition>::type,
- replace_event<FrowTransition,Event>,
- boost::mpl::identity<FrowTransition>
- >::type type;
- };
-
-
- typedef typename generate_state_set<Stt>::type state_list;
- BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size<state_list>::value));
- template <class Transition>
- struct convert_event_and_forward
- {
- static HandledEnum execute(Fsm& fsm, int region_index, int state, Event const& evt)
- {
- typename Transition::transition_event forwarded(evt);
- return Transition::execute(fsm,region_index,state,forwarded);
- }
- };
-
-
- struct init_cell
- {
- init_cell(dispatch_table* self_)
- : self(self_)
- {}
-
-
- template <class Transition>
- typename ::boost::disable_if<
- typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
- ,void>::type
- init_event_base_case(Transition const&, ::boost::mpl::true_ const &, ::boost::mpl::false_ const &) const
- {
- typedef typename create_stt<Fsm>::type stt;
- BOOST_STATIC_CONSTANT(int, state_id =
- (get_state_id<stt,typename Transition::current_state_type>::value));
-
- self->entries[state_id + 1] = reinterpret_cast<cell>(
- reinterpret_cast<std::uintptr_t>(&Transition::execute));
- }
- template <class Transition>
- typename ::boost::enable_if<
- typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
- ,void>::type
- init_event_base_case(Transition const&, ::boost::mpl::true_ const &, ::boost::mpl::false_ const &) const
- {
- self->entries[0] = reinterpret_cast<cell>(&Transition::execute);
- }
-
-
- template <class Transition>
- typename ::boost::disable_if<
- typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
- ,void>::type
- init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::true_ const &) const
- {
- typedef typename create_stt<Fsm>::type stt;
- BOOST_STATIC_CONSTANT(int, state_id =
- (get_state_id<stt,typename Transition::current_state_type>::value));
- self->entries[state_id+1] = &convert_event_and_forward<Transition>::execute;
- }
- template <class Transition>
- typename ::boost::enable_if<
- typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
- ,void>::type
- init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::true_ const &) const
- {
- self->entries[0] = &convert_event_and_forward<Transition>::execute;
- }
- template <class Transition>
- typename ::boost::disable_if<
- typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
- ,void>::type
- init_event_base_case(Transition const&, ::boost::mpl::true_ const &, ::boost::mpl::true_ const &) const
- {
- typedef typename create_stt<Fsm>::type stt;
- BOOST_STATIC_CONSTANT(int, state_id =
- (get_state_id<stt,typename Transition::current_state_type>::value));
- self->entries[state_id+1] = &convert_event_and_forward<Transition>::execute;
- }
- template <class Transition>
- typename ::boost::enable_if<
- typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
- ,void>::type
- init_event_base_case(Transition const&, ::boost::mpl::true_ const &, ::boost::mpl::true_ const &) const
- {
- self->entries[0] = &convert_event_and_forward<Transition>::execute;
- }
-
-
-
- template <class Transition>
- typename ::boost::disable_if<
- typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
- ,void>::type
- init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::false_ const &) const
- {
- typedef typename create_stt<Fsm>::type stt;
- BOOST_STATIC_CONSTANT(int, state_id =
- (get_state_id<stt,typename Transition::current_state_type>::value));
- self->entries[state_id+1] = &Transition::execute;
- }
- template <class Transition>
- typename ::boost::enable_if<
- typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
- ,void>::type
- init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::false_ const &) const
- {
- self->entries[0] = &Transition::execute;
- }
-
- template <class Transition>
- typename ::boost::enable_if<typename has_not_real_row_tag<Transition>::type,void >::type
- operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
- {
-
- }
- template <class Transition>
- typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
- operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
- {
-
- init_event_base_case(tr,
- ::boost::mpl::bool_<
- ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>(),
- ::boost::mpl::bool_<
- ::boost::msm::is_kleene_event<typename Transition::transition_event>::type::value>());
- }
-
- dispatch_table* self;
- };
-
-
-
- template <class EventType,class Enable=void>
- struct default_init_cell
- {
- default_init_cell(dispatch_table* self_,cell* tofill_entries_)
- : self(self_),tofill_entries(tofill_entries_)
- {}
- template <class State>
- typename ::boost::enable_if<typename has_state_delayed_event<State,Event>::type,void>::type
- operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<0> = 0)
- {
- typedef typename create_stt<Fsm>::type stt;
- BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
- cell call_no_transition = &Fsm::defer_transition;
- tofill_entries[state_id+1] = call_no_transition;
- }
- template <class State>
- typename ::boost::disable_if<
- typename ::boost::mpl::or_<
- typename has_state_delayed_event<State,Event>::type,
- typename ::boost::is_same<State,Fsm>::type
- >::type
- ,void >::type
- operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<1> = 0)
- {
- typedef typename create_stt<Fsm>::type stt;
- BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
- cell call_no_transition = &Fsm::call_no_transition;
- tofill_entries[state_id+1] = call_no_transition;
- }
-
- template <class State>
- typename ::boost::enable_if<
- typename ::boost::mpl::and_<
- typename ::boost::mpl::not_<typename has_state_delayed_event<State,Event>::type>::type,
- typename ::boost::is_same<State,Fsm>::type
- >::type
- ,void>::type
- operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<2> = 0)
- {
- cell call_no_transition = &Fsm::call_no_transition_internal;
- tofill_entries[0] = call_no_transition;
- }
- dispatch_table* self;
- cell* tofill_entries;
- };
-
- template <class EventType>
- struct default_init_cell<EventType,
- typename ::boost::enable_if<
- typename is_completion_event<EventType>::type>::type>
- {
- default_init_cell(dispatch_table* self_,cell* tofill_entries_)
- : self(self_),tofill_entries(tofill_entries_)
- {}
-
-
-
- template <class State>
- typename ::boost::disable_if<
- typename ::boost::is_same<State,Fsm>::type
- ,void>::type
- operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<0> = 0)
- {
- typedef typename create_stt<Fsm>::type stt;
- BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
- cell call_no_transition = &Fsm::default_eventless_transition;
- tofill_entries[state_id+1] = call_no_transition;
- }
- template <class State>
- typename ::boost::enable_if<
- typename ::boost::is_same<State,Fsm>::type
- ,void>::type
- operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<1> = 0)
- {
- cell call_no_transition = &Fsm::default_eventless_transition;
- tofill_entries[0] = call_no_transition;
- }
- dispatch_table* self;
- cell* tofill_entries;
- };
- public:
-
- dispatch_table()
- {
-
- ::boost::mpl::for_each<typename generate_state_set<Stt>::type,
- boost::msm::wrap< ::boost::mpl::placeholders::_1> >
- (default_init_cell<Event>(this,entries));
-
-
-
- typedef typename ::boost::mpl::reverse_fold<
-
- ::boost::mpl::filter_view
- <Stt, boost::mpl::or_<
- ::boost::is_base_of<transition_event< ::boost::mpl::placeholders::_>, Event>,
- ::boost::msm::is_kleene_event<transition_event< ::boost::mpl::placeholders::_> >
- >
- >,
-
- ::boost::mpl::map<>,
- ::boost::mpl::if_<
-
- ::boost::mpl::has_key< ::boost::mpl::placeholders::_1,
- transition_source_type< ::boost::mpl::placeholders::_2> >,
-
- ::boost::mpl::insert<
- ::boost::mpl::placeholders::_1,
- ::boost::mpl::pair<transition_source_type< ::boost::mpl::placeholders::_2>,
- ::boost::mpl::push_back<
- ::boost::mpl::at< ::boost::mpl::placeholders::_1,
- transition_source_type< ::boost::mpl::placeholders::_2> >,
- change_frow_event< ::boost::mpl::placeholders::_2 > >
- > >,
-
- ::boost::mpl::insert<
- ::boost::mpl::placeholders::_1,
- ::boost::mpl::pair<transition_source_type< ::boost::mpl::placeholders::_2>,
- make_vector< change_frow_event< ::boost::mpl::placeholders::_2> > > >
- >
- >::type map_of_row_seq;
-
- typedef typename ::boost::mpl::fold<
- map_of_row_seq,::boost::mpl::vector0<>,
- ::boost::mpl::if_<
- ::boost::mpl::greater< ::boost::mpl::size<
- ::boost::mpl::second< ::boost::mpl::placeholders::_2> >,
- ::boost::mpl::int_<1> >,
-
- ::boost::mpl::push_back< ::boost::mpl::placeholders::_1,
- make_chain_row_from_map_entry< ::boost::mpl::placeholders::_2> >,
-
- ::boost::mpl::push_back< ::boost::mpl::placeholders::_1,
- get_first_element_pair_second< ::boost::mpl::placeholders::_2> >
- > >::type chained_rows;
-
- ::boost::mpl::for_each<chained_rows>(init_cell(this));
- }
-
- static const dispatch_table& instance() {
- static dispatch_table table;
- return table;
- }
- public:
-
- cell entries[max_state+1];
- };
- }}}
- #endif
|