dispatch_table.hpp 20 KB

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