dispatch_table.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. template <class Transition>
  196. typename ::boost::disable_if<
  197. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  198. ,void>::type
  199. init_event_base_case(Transition const&, ::boost::mpl::true_ const &, ::boost::mpl::true_ const &) const
  200. {
  201. typedef typename create_stt<Fsm>::type stt;
  202. BOOST_STATIC_CONSTANT(int, state_id =
  203. (get_state_id<stt,typename Transition::current_state_type>::value));
  204. self->entries[state_id+1] = &convert_event_and_forward<Transition>::execute;
  205. }
  206. template <class Transition>
  207. typename ::boost::enable_if<
  208. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  209. ,void>::type
  210. init_event_base_case(Transition const&, ::boost::mpl::true_ const &, ::boost::mpl::true_ const &) const
  211. {
  212. self->entries[0] = &convert_event_and_forward<Transition>::execute;
  213. }
  214. // end version for kleene
  215. // version for transition event base of our event
  216. // first for all transitions, then for internal ones of a fsm
  217. template <class Transition>
  218. typename ::boost::disable_if<
  219. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  220. ,void>::type
  221. init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::false_ const &) const
  222. {
  223. typedef typename create_stt<Fsm>::type stt;
  224. BOOST_STATIC_CONSTANT(int, state_id =
  225. (get_state_id<stt,typename Transition::current_state_type>::value));
  226. self->entries[state_id+1] = &Transition::execute;
  227. }
  228. template <class Transition>
  229. typename ::boost::enable_if<
  230. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  231. ,void>::type
  232. init_event_base_case(Transition const&, ::boost::mpl::false_ const &, ::boost::mpl::false_ const &) const
  233. {
  234. self->entries[0] = &Transition::execute;
  235. }
  236. // Cell initializer function object, used with mpl::for_each
  237. template <class Transition>
  238. typename ::boost::enable_if<typename has_not_real_row_tag<Transition>::type,void >::type
  239. operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
  240. {
  241. // version for not real rows. No problem because irrelevant for process_event
  242. }
  243. template <class Transition>
  244. typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
  245. operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
  246. {
  247. //only if the transition event is a base of our event is the reinterpret_case safe
  248. init_event_base_case(tr,
  249. ::boost::mpl::bool_<
  250. ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>(),
  251. ::boost::mpl::bool_<
  252. ::boost::msm::is_kleene_event<typename Transition::transition_event>::type::value>());
  253. }
  254. dispatch_table* self;
  255. };
  256. // Cell default-initializer function object, used with mpl::for_each
  257. // initializes with call_no_transition, defer_transition or default_eventless_transition
  258. // variant for non-anonymous transitions
  259. template <class EventType,class Enable=void>
  260. struct default_init_cell
  261. {
  262. default_init_cell(dispatch_table* self_,cell* tofill_entries_)
  263. : self(self_),tofill_entries(tofill_entries_)
  264. {}
  265. template <class State>
  266. typename ::boost::enable_if<typename has_state_delayed_event<State,Event>::type,void>::type
  267. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<0> = 0)
  268. {
  269. typedef typename create_stt<Fsm>::type stt;
  270. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  271. cell call_no_transition = &Fsm::defer_transition;
  272. tofill_entries[state_id+1] = call_no_transition;
  273. }
  274. template <class State>
  275. typename ::boost::disable_if<
  276. typename ::boost::mpl::or_<
  277. typename has_state_delayed_event<State,Event>::type,
  278. typename ::boost::is_same<State,Fsm>::type
  279. >::type
  280. ,void >::type
  281. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<1> = 0)
  282. {
  283. typedef typename create_stt<Fsm>::type stt;
  284. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  285. cell call_no_transition = &Fsm::call_no_transition;
  286. tofill_entries[state_id+1] = call_no_transition;
  287. }
  288. // case for internal transitions of this fsm
  289. template <class State>
  290. typename ::boost::enable_if<
  291. typename ::boost::mpl::and_<
  292. typename ::boost::mpl::not_<typename has_state_delayed_event<State,Event>::type>::type,
  293. typename ::boost::is_same<State,Fsm>::type
  294. >::type
  295. ,void>::type
  296. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<2> = 0)
  297. {
  298. cell call_no_transition = &Fsm::call_no_transition_internal;
  299. tofill_entries[0] = call_no_transition;
  300. }
  301. dispatch_table* self;
  302. cell* tofill_entries;
  303. };
  304. // variant for anonymous transitions
  305. template <class EventType>
  306. struct default_init_cell<EventType,
  307. typename ::boost::enable_if<
  308. typename is_completion_event<EventType>::type>::type>
  309. {
  310. default_init_cell(dispatch_table* self_,cell* tofill_entries_)
  311. : self(self_),tofill_entries(tofill_entries_)
  312. {}
  313. // this event is a compound one (not a real one, just one for use in event-less transitions)
  314. // Note this event cannot be used as deferred!
  315. // case for internal transitions of this fsm
  316. template <class State>
  317. typename ::boost::disable_if<
  318. typename ::boost::is_same<State,Fsm>::type
  319. ,void>::type
  320. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<0> = 0)
  321. {
  322. typedef typename create_stt<Fsm>::type stt;
  323. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  324. cell call_no_transition = &Fsm::default_eventless_transition;
  325. tofill_entries[state_id+1] = call_no_transition;
  326. }
  327. template <class State>
  328. typename ::boost::enable_if<
  329. typename ::boost::is_same<State,Fsm>::type
  330. ,void>::type
  331. operator()(boost::msm::wrap<State> const&,boost::msm::back::dummy<1> = 0)
  332. {
  333. cell call_no_transition = &Fsm::default_eventless_transition;
  334. tofill_entries[0] = call_no_transition;
  335. }
  336. dispatch_table* self;
  337. cell* tofill_entries;
  338. };
  339. public:
  340. // initialize the dispatch table for a given Event and Fsm
  341. dispatch_table()
  342. {
  343. // Initialize cells for no transition
  344. ::boost::mpl::for_each<typename generate_state_set<Stt>::type,
  345. boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  346. (default_init_cell<Event>(this,entries));
  347. // build chaining rows for rows coming from the same state and the current event
  348. // first we build a map of sequence for every source
  349. // in reverse order so that the frow's are handled first (UML priority)
  350. typedef typename ::boost::mpl::reverse_fold<
  351. // filter on event
  352. ::boost::mpl::filter_view
  353. <Stt, boost::mpl::or_<
  354. ::boost::is_base_of<transition_event< ::boost::mpl::placeholders::_>, Event>,
  355. ::boost::msm::is_kleene_event<transition_event< ::boost::mpl::placeholders::_> >
  356. >
  357. >,
  358. // build a map
  359. ::boost::mpl::map<>,
  360. ::boost::mpl::if_<
  361. // if we already have a row on this source state
  362. ::boost::mpl::has_key< ::boost::mpl::placeholders::_1,
  363. transition_source_type< ::boost::mpl::placeholders::_2> >,
  364. // insert a new element in the value type
  365. ::boost::mpl::insert<
  366. ::boost::mpl::placeholders::_1,
  367. ::boost::mpl::pair<transition_source_type< ::boost::mpl::placeholders::_2>,
  368. ::boost::mpl::push_back<
  369. ::boost::mpl::at< ::boost::mpl::placeholders::_1,
  370. transition_source_type< ::boost::mpl::placeholders::_2> >,
  371. change_frow_event< ::boost::mpl::placeholders::_2 > >
  372. > >,
  373. // first row on this source state, make a vector with 1 element
  374. ::boost::mpl::insert<
  375. ::boost::mpl::placeholders::_1,
  376. ::boost::mpl::pair<transition_source_type< ::boost::mpl::placeholders::_2>,
  377. make_vector< change_frow_event< ::boost::mpl::placeholders::_2> > > >
  378. >
  379. >::type map_of_row_seq;
  380. // and then build chaining rows for all source states having more than 1 row
  381. typedef typename ::boost::mpl::fold<
  382. map_of_row_seq,::boost::mpl::vector0<>,
  383. ::boost::mpl::if_<
  384. ::boost::mpl::greater< ::boost::mpl::size<
  385. ::boost::mpl::second< ::boost::mpl::placeholders::_2> >,
  386. ::boost::mpl::int_<1> >,
  387. // we need row chaining
  388. ::boost::mpl::push_back< ::boost::mpl::placeholders::_1,
  389. make_chain_row_from_map_entry< ::boost::mpl::placeholders::_2> >,
  390. // just one row, no chaining, we rebuild the row like it was before
  391. ::boost::mpl::push_back< ::boost::mpl::placeholders::_1,
  392. get_first_element_pair_second< ::boost::mpl::placeholders::_2> >
  393. > >::type chained_rows;
  394. // Go back and fill in cells for matching transitions.
  395. ::boost::mpl::for_each<chained_rows>(init_cell(this));
  396. }
  397. // The singleton instance.
  398. static const dispatch_table instance;
  399. public: // data members
  400. // +1 => 0 is reserved for this fsm (internal transitions)
  401. cell entries[max_state+1];
  402. };
  403. }}} // boost::msm::back
  404. #endif //BOOST_MSM_BACK_DISPATCH_TABLE_H