dispatch_table.hpp 22 KB

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