dispatch_table.hpp 23 KB

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