dispatch_table.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. // Copyright 2008 Christophe Henry
  2. // henry UNDERSCORE christophe AT hotmail DOT com
  3. // This is an extended version of the state machine available in the boost::mpl library
  4. // Distributed under the same license as the original.
  5. // Copyright for the original version:
  6. // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
  7. // under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_MSM_BACK_DISPATCH_TABLE_H
  11. #define BOOST_MSM_BACK_DISPATCH_TABLE_H
  12. #include <utility>
  13. #include <boost/mpl/reverse_fold.hpp>
  14. #include <boost/mpl/greater.hpp>
  15. #include <boost/mpl/filter_view.hpp>
  16. #include <boost/mpl/pop_front.hpp>
  17. #include <boost/mpl/for_each.hpp>
  18. #include <boost/mpl/advance.hpp>
  19. #include <boost/type_traits/is_base_of.hpp>
  20. #include <boost/type_traits/is_same.hpp>
  21. #include <boost/msm/event_traits.hpp>
  22. #include <boost/msm/back/metafunctions.hpp>
  23. #include <boost/msm/back/common_types.hpp>
  24. BOOST_MPL_HAS_XXX_TRAIT_DEF(is_frow)
  25. namespace boost { namespace msm { namespace back
  26. {
  27. // Generates a singleton runtime lookup table that maps current state
  28. // to a function that makes the SM take its transition on the given
  29. // Event type.
  30. template <class Fsm,class Stt, class Event,class CompilePolicy>
  31. struct dispatch_table
  32. {
  33. private:
  34. // This is a table of these function pointers.
  35. typedef HandledEnum (*cell)(Fsm&, int,int,Event const&);
  36. typedef bool (*guard)(Fsm&, Event const&);
  37. // class used to build a chain (or sequence) of transitions for a given event and start state
  38. // (like an UML diamond). Allows transition conflicts.
  39. template< typename Seq,typename AnEvent,typename State >
  40. struct chain_row
  41. {
  42. typedef State current_state_type;
  43. typedef AnEvent transition_event;
  44. // helper for building a disable/enable_if-controlled execute function
  45. struct execute_helper
  46. {
  47. template <class Sequence>
  48. static
  49. HandledEnum
  50. execute(Fsm& , int, int, Event const& , ::boost::mpl::true_ const & )
  51. {
  52. // if at least one guard rejected, this will be ignored, otherwise will generate an error
  53. return HANDLED_FALSE;
  54. }
  55. template <class Sequence>
  56. static
  57. HandledEnum
  58. execute(Fsm& fsm, int region_index , int state, Event const& evt,
  59. ::boost::mpl::false_ const & )
  60. {
  61. // try the first guard
  62. typedef typename ::boost::mpl::front<Sequence>::type first_row;
  63. HandledEnum res = first_row::execute(fsm,region_index,state,evt);
  64. if (HANDLED_TRUE!=res && HANDLED_DEFERRED!=res)
  65. {
  66. // if the first rejected, move on to the next one
  67. HandledEnum sub_res =
  68. execute<typename ::boost::mpl::pop_front<Sequence>::type>(fsm,region_index,state,evt,
  69. ::boost::mpl::bool_<
  70. ::boost::mpl::empty<typename ::boost::mpl::pop_front<Sequence>::type>::type::value>());
  71. // if at least one guards rejects, the event will not generate a call to no_transition
  72. if ((HANDLED_FALSE==sub_res) && (HANDLED_GUARD_REJECT==res) )
  73. return HANDLED_GUARD_REJECT;
  74. else
  75. return sub_res;
  76. }
  77. return res;
  78. }
  79. };
  80. // Take the transition action and return the next state.
  81. static HandledEnum execute(Fsm& fsm, int region_index, int state, Event const& evt)
  82. {
  83. // forward to helper
  84. return execute_helper::template execute<Seq>(fsm,region_index,state,evt,
  85. ::boost::mpl::bool_< ::boost::mpl::empty<Seq>::type::value>());
  86. }
  87. };
  88. // nullary metafunction whose only job is to prevent early evaluation of _1
  89. template< typename Entry >
  90. struct make_chain_row_from_map_entry
  91. {
  92. // if we have more than one frow with the same state as source, remove the ones extra
  93. // note: we know the frow's are located at the beginning so we remove at the beginning (number of frows - 1) elements
  94. enum {number_frows = ::boost::mpl::count_if< typename Entry::second,has_is_frow< ::boost::mpl::placeholders::_1> >::value};
  95. //erases the first NumberToDelete rows
  96. template<class Sequence, int NumberToDelete>
  97. struct erase_first_rows
  98. {
  99. typedef typename ::boost::mpl::erase<
  100. typename Entry::second,
  101. typename ::boost::mpl::begin<Sequence>::type,
  102. typename ::boost::mpl::advance<
  103. typename ::boost::mpl::begin<Sequence>::type,
  104. ::boost::mpl::int_<NumberToDelete> >::type
  105. >::type type;
  106. };
  107. // if we have more than 1 frow with this event (not allowed), delete the spare
  108. typedef typename ::boost::mpl::eval_if<
  109. typename ::boost::mpl::bool_< number_frows >= 2 >::type,
  110. erase_first_rows<typename Entry::second,number_frows-1>,
  111. ::boost::mpl::identity<typename Entry::second>
  112. >::type filtered_stt;
  113. typedef chain_row<filtered_stt,Event,
  114. typename Entry::first > type;
  115. };
  116. // helper for lazy evaluation in eval_if of change_frow_event
  117. template <class Transition,class NewEvent>
  118. struct replace_event
  119. {
  120. typedef typename Transition::template replace_event<NewEvent>::type type;
  121. };
  122. // changes the event type for a frow to the event we are dispatching
  123. // this helps ensure that an event does not get processed more than once because of frows and base events.
  124. template <class FrowTransition>
  125. struct change_frow_event
  126. {
  127. typedef typename ::boost::mpl::eval_if<
  128. typename has_is_frow<FrowTransition>::type,
  129. replace_event<FrowTransition,Event>,
  130. boost::mpl::identity<FrowTransition>
  131. >::type type;
  132. };
  133. // Compute the maximum state value in the sm so we know how big
  134. // to make the table
  135. typedef typename generate_state_set<Stt>::type state_list;
  136. BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size<state_list>::value));
  137. template <class Transition>
  138. struct convert_event_and_forward
  139. {
  140. static HandledEnum execute(Fsm& fsm, int region_index, int state, Event const& evt)
  141. {
  142. typename Transition::transition_event forwarded(evt);
  143. return Transition::execute(fsm,region_index,state,forwarded);
  144. }
  145. };
  146. // A function object for use with mpl::for_each that stuffs
  147. // transitions into cells.
  148. struct init_cell
  149. {
  150. init_cell(dispatch_table* self_)
  151. : self(self_)
  152. {}
  153. // version for transition event not base of our event
  154. // first for all transitions, then for internal ones of a fsm
  155. template <class Transition>
  156. typename ::boost::disable_if<
  157. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  158. ,void>::type
  159. init_event_base_case(Transition const&, ::boost::mpl::true_ const &, ::boost::mpl::false_ const &) const
  160. {
  161. typedef typename create_stt<Fsm>::type stt;
  162. BOOST_STATIC_CONSTANT(int, state_id =
  163. (get_state_id<stt,typename Transition::current_state_type>::value));
  164. self->entries[state_id+1] = reinterpret_cast<cell>(&Transition::execute);
  165. }
  166. template <class Transition>
  167. typename ::boost::enable_if<
  168. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  169. ,void>::type
  170. init_event_base_case(Transition const&, ::boost::mpl::true_ const &, ::boost::mpl::false_ const &) const
  171. {
  172. self->entries[0] = reinterpret_cast<cell>(&Transition::execute);
  173. }
  174. // version for transition event is boost::any
  175. // first for all transitions, then for internal ones of a fsm
  176. template <class Transition>
  177. typename ::boost::disable_if<
  178. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  179. ,void>::type
  180. init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::true_ const &) const
  181. {
  182. typedef typename create_stt<Fsm>::type stt;
  183. BOOST_STATIC_CONSTANT(int, state_id =
  184. (get_state_id<stt,typename Transition::current_state_type>::value));
  185. self->entries[state_id+1] = &convert_event_and_forward<Transition>::execute;
  186. }
  187. template <class Transition>
  188. typename ::boost::enable_if<
  189. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  190. ,void>::type
  191. init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::true_ const &) const
  192. {
  193. self->entries[0] = &convert_event_and_forward<Transition>::execute;
  194. }
  195. // version for transition event base of our event
  196. // first for all transitions, then for internal ones of a fsm
  197. template <class Transition>
  198. typename ::boost::disable_if<
  199. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  200. ,void>::type
  201. init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::false_ const &) const
  202. {
  203. typedef typename create_stt<Fsm>::type stt;
  204. BOOST_STATIC_CONSTANT(int, state_id =
  205. (get_state_id<stt,typename Transition::current_state_type>::value));
  206. self->entries[state_id+1] = &Transition::execute;
  207. }
  208. template <class Transition>
  209. typename ::boost::enable_if<
  210. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  211. ,void>::type
  212. init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::false_ const &) const
  213. {
  214. self->entries[0] = &Transition::execute;
  215. }
  216. // Cell initializer function object, used with mpl::for_each
  217. template <class Transition>
  218. typename ::boost::enable_if<typename has_not_real_row_tag<Transition>::type,void >::type
  219. operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
  220. {
  221. // version for not real rows. No problem because irrelevant for process_event
  222. }
  223. template <class Transition>
  224. typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
  225. operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
  226. {
  227. //only if the transition event is a base of our event is the reinterpret_case safe
  228. init_event_base_case(tr,
  229. ::boost::mpl::bool_<
  230. ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>(),
  231. ::boost::mpl::bool_<
  232. ::boost::msm::is_kleene_event<typename Transition::transition_event>::type::value>());
  233. }
  234. dispatch_table* self;
  235. };
  236. // Cell default-initializer function object, used with mpl::for_each
  237. // initializes with call_no_transition, defer_transition or default_eventless_transition
  238. // variant for non-anonymous transitions
  239. template <class EventType,class Enable=void>
  240. struct default_init_cell
  241. {
  242. default_init_cell(dispatch_table* self_,cell* tofill_entries_)
  243. : self(self_),tofill_entries(tofill_entries_)
  244. {}
  245. template <class State>
  246. typename ::boost::enable_if<typename has_state_delayed_event<State,Event>::type,void>::type
  247. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<0> = 0)
  248. {
  249. typedef typename create_stt<Fsm>::type stt;
  250. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  251. cell call_no_transition = &Fsm::defer_transition;
  252. tofill_entries[state_id+1] = call_no_transition;
  253. }
  254. template <class State>
  255. typename ::boost::disable_if<
  256. typename ::boost::mpl::or_<
  257. typename has_state_delayed_event<State,Event>::type,
  258. typename ::boost::is_same<State,Fsm>::type
  259. >::type
  260. ,void >::type
  261. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<1> = 0)
  262. {
  263. typedef typename create_stt<Fsm>::type stt;
  264. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  265. cell call_no_transition = &Fsm::call_no_transition;
  266. tofill_entries[state_id+1] = call_no_transition;
  267. }
  268. // case for internal transitions of this fsm
  269. template <class State>
  270. typename ::boost::enable_if<
  271. typename ::boost::mpl::and_<
  272. typename ::boost::mpl::not_<typename has_state_delayed_event<State,Event>::type>::type,
  273. typename ::boost::is_same<State,Fsm>::type
  274. >::type
  275. ,void>::type
  276. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<2> = 0)
  277. {
  278. cell call_no_transition = &Fsm::call_no_transition_internal;
  279. tofill_entries[0] = call_no_transition;
  280. }
  281. dispatch_table* self;
  282. cell* tofill_entries;
  283. };
  284. // variant for anonymous transitions
  285. template <class EventType>
  286. struct default_init_cell<EventType,
  287. typename ::boost::enable_if<
  288. typename is_completion_event<EventType>::type>::type>
  289. {
  290. default_init_cell(dispatch_table* self_,cell* tofill_entries_)
  291. : self(self_),tofill_entries(tofill_entries_)
  292. {}
  293. // this event is a compound one (not a real one, just one for use in event-less transitions)
  294. // Note this event cannot be used as deferred!
  295. // case for internal transitions of this fsm
  296. template <class State>
  297. typename ::boost::disable_if<
  298. typename ::boost::is_same<State,Fsm>::type
  299. ,void>::type
  300. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<0> = 0)
  301. {
  302. typedef typename create_stt<Fsm>::type stt;
  303. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  304. cell call_no_transition = &Fsm::default_eventless_transition;
  305. tofill_entries[state_id+1] = call_no_transition;
  306. }
  307. template <class State>
  308. typename ::boost::enable_if<
  309. typename ::boost::is_same<State,Fsm>::type
  310. ,void>::type
  311. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<1> = 0)
  312. {
  313. cell call_no_transition = &Fsm::default_eventless_transition;
  314. tofill_entries[0] = call_no_transition;
  315. }
  316. dispatch_table* self;
  317. cell* tofill_entries;
  318. };
  319. public:
  320. // initialize the dispatch table for a given Event and Fsm
  321. dispatch_table()
  322. {
  323. // Initialize cells for no transition
  324. ::boost::mpl::for_each<typename generate_state_set<Stt>::type,
  325. boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  326. (default_init_cell<Event>(this,entries));
  327. // build chaining rows for rows coming from the same state and the current event
  328. // first we build a map of sequence for every source
  329. // in reverse order so that the frow's are handled first (UML priority)
  330. typedef typename ::boost::mpl::reverse_fold<
  331. // filter on event
  332. ::boost::mpl::filter_view
  333. <Stt, boost::mpl::or_<
  334. ::boost::is_base_of<transition_event< ::boost::mpl::placeholders::_>, Event>,
  335. ::boost::msm::is_kleene_event<transition_event< ::boost::mpl::placeholders::_> >
  336. >
  337. >,
  338. // build a map
  339. ::boost::mpl::map<>,
  340. ::boost::mpl::if_<
  341. // if we already have a row on this source state
  342. ::boost::mpl::has_key< ::boost::mpl::placeholders::_1,
  343. transition_source_type< ::boost::mpl::placeholders::_2> >,
  344. // insert a new element in the value type
  345. ::boost::mpl::insert<
  346. ::boost::mpl::placeholders::_1,
  347. ::boost::mpl::pair<transition_source_type< ::boost::mpl::placeholders::_2>,
  348. ::boost::mpl::push_back<
  349. ::boost::mpl::at< ::boost::mpl::placeholders::_1,
  350. transition_source_type< ::boost::mpl::placeholders::_2> >,
  351. change_frow_event< ::boost::mpl::placeholders::_2 > >
  352. > >,
  353. // first row on this source state, make a vector with 1 element
  354. ::boost::mpl::insert<
  355. ::boost::mpl::placeholders::_1,
  356. ::boost::mpl::pair<transition_source_type< ::boost::mpl::placeholders::_2>,
  357. make_vector< change_frow_event< ::boost::mpl::placeholders::_2> > > >
  358. >
  359. >::type map_of_row_seq;
  360. // and then build chaining rows for all source states having more than 1 row
  361. typedef typename ::boost::mpl::fold<
  362. map_of_row_seq,::boost::mpl::vector0<>,
  363. ::boost::mpl::if_<
  364. ::boost::mpl::greater< ::boost::mpl::size<
  365. ::boost::mpl::second< ::boost::mpl::placeholders::_2> >,
  366. ::boost::mpl::int_<1> >,
  367. // we need row chaining
  368. ::boost::mpl::push_back< ::boost::mpl::placeholders::_1,
  369. make_chain_row_from_map_entry< ::boost::mpl::placeholders::_2> >,
  370. // just one row, no chaining, we rebuild the row like it was before
  371. ::boost::mpl::push_back< ::boost::mpl::placeholders::_1,
  372. get_first_element_pair_second< ::boost::mpl::placeholders::_2> >
  373. > >::type chained_rows;
  374. // Go back and fill in cells for matching transitions.
  375. ::boost::mpl::for_each<chained_rows>(init_cell(this));
  376. }
  377. // The singleton instance.
  378. static const dispatch_table instance;
  379. public: // data members
  380. // +1 => 0 is reserved for this fsm (internal transitions)
  381. cell entries[max_state+1];
  382. };
  383. }}} // boost::msm::back
  384. #endif //BOOST_MSM_BACK_DISPATCH_TABLE_H