favor_compile_time.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_FAVOR_COMPILE_TIME_H
  11. #define BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
  12. #include <utility>
  13. #include <deque>
  14. #include <boost/mpl/filter_view.hpp>
  15. #include <boost/mpl/for_each.hpp>
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/any.hpp>
  18. #include <boost/msm/common.hpp>
  19. #include <boost/msm/back/metafunctions.hpp>
  20. #include <boost/msm/back/common_types.hpp>
  21. #include <boost/msm/back/dispatch_table.hpp>
  22. namespace boost { namespace msm { namespace back
  23. {
  24. template <class Fsm>
  25. struct process_any_event_helper
  26. {
  27. process_any_event_helper(msm::back::HandledEnum& res_,Fsm* self_,::boost::any any_event_):
  28. res(res_),self(self_),any_event(any_event_),finished(false){}
  29. template <class Event>
  30. void operator()(boost::msm::wrap<Event> const&)
  31. {
  32. if ( ! finished && ::boost::any_cast<Event>(&any_event)!=0)
  33. {
  34. finished = true;
  35. res = self->process_event_internal(::boost::any_cast<Event>(any_event));
  36. }
  37. }
  38. private:
  39. msm::back::HandledEnum& res;
  40. Fsm* self;
  41. ::boost::any any_event;
  42. bool finished;
  43. };
  44. #define BOOST_MSM_BACK_GENERATE_PROCESS_EVENT(fsmname) \
  45. namespace boost { namespace msm { namespace back{ \
  46. template<> \
  47. ::boost::msm::back::HandledEnum fsmname::process_any_event( ::boost::any const& any_event) \
  48. { \
  49. typedef ::boost::msm::back::recursive_get_transition_table<fsmname>::type stt; \
  50. typedef ::boost::msm::back::generate_event_set<stt>::type stt_events; \
  51. typedef ::boost::msm::back::recursive_get_internal_transition_table<fsmname, ::boost::mpl::true_ >::type istt; \
  52. typedef ::boost::msm::back::generate_event_set<create_real_stt<fsmname,istt>::type >::type istt_events; \
  53. typedef ::boost::msm::back::set_insert_range<stt_events,istt_events>::type all_events; \
  54. ::boost::msm::back::HandledEnum res= ::boost::msm::back::HANDLED_FALSE; \
  55. ::boost::mpl::for_each<all_events, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> > \
  56. (::boost::msm::back::process_any_event_helper<fsmname>(res,this,any_event)); \
  57. return res; \
  58. } \
  59. }}}
  60. struct favor_compile_time
  61. {
  62. typedef int compile_policy;
  63. typedef ::boost::mpl::false_ add_forwarding_rows;
  64. };
  65. // Generates a singleton runtime lookup table that maps current state
  66. // to a function that makes the SM take its transition on the given
  67. // Event type.
  68. template <class Fsm,class Stt, class Event>
  69. struct dispatch_table < Fsm, Stt, Event, ::boost::msm::back::favor_compile_time>
  70. {
  71. private:
  72. // This is a table of these function pointers.
  73. typedef HandledEnum (*cell)(Fsm&, int,int,Event const&);
  74. typedef bool (*guard)(Fsm&, Event const&);
  75. // Compute the maximum state value in the sm so we know how big
  76. // to make the table
  77. typedef typename generate_state_set<Stt>::type state_list;
  78. BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size<state_list>::value));
  79. struct chain_row
  80. {
  81. HandledEnum operator()(Fsm& fsm, int region,int state,Event const& evt) const
  82. {
  83. HandledEnum res = HANDLED_FALSE;
  84. typename std::deque<cell>::const_iterator it = one_state.begin();
  85. while (it != one_state.end() && (res != HANDLED_TRUE && res != HANDLED_DEFERRED ))
  86. {
  87. HandledEnum handled = (*it)(fsm,region,state,evt);
  88. // reject is considered as erasing an error (HANDLED_FALSE)
  89. if ((HANDLED_FALSE==handled) && (HANDLED_GUARD_REJECT==res) )
  90. res = HANDLED_GUARD_REJECT;
  91. else
  92. res = handled;
  93. ++it;
  94. }
  95. return res;
  96. }
  97. std::deque<cell> one_state;
  98. };
  99. template <class TransitionState>
  100. static HandledEnum call_submachine(Fsm& fsm, int , int , Event const& evt)
  101. {
  102. return (fsm.template get_state<TransitionState&>()).process_any_event( ::boost::any(evt));
  103. }
  104. // A function object for use with mpl::for_each that stuffs
  105. // transitions into cells.
  106. struct init_cell
  107. {
  108. init_cell(dispatch_table* self_)
  109. : self(self_)
  110. {}
  111. // version for transition event not base of our event
  112. template <class Transition>
  113. typename ::boost::disable_if<
  114. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  115. ,void>::type
  116. init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
  117. {
  118. typedef typename create_stt<Fsm>::type stt;
  119. BOOST_STATIC_CONSTANT(int, state_id =
  120. (get_state_id<stt,typename Transition::current_state_type>::value));
  121. self->entries[state_id+1].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
  122. }
  123. template <class Transition>
  124. typename ::boost::enable_if<
  125. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  126. ,void>::type
  127. init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
  128. {
  129. self->entries[0].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
  130. }
  131. // version for transition event base of our event
  132. template <class Transition>
  133. typename ::boost::disable_if<
  134. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  135. ,void>::type
  136. init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
  137. {
  138. typedef typename create_stt<Fsm>::type stt;
  139. BOOST_STATIC_CONSTANT(int, state_id =
  140. (get_state_id<stt,typename Transition::current_state_type>::value));
  141. self->entries[state_id+1].one_state.push_front(&Transition::execute);
  142. }
  143. template <class Transition>
  144. typename ::boost::enable_if<
  145. typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
  146. ,void>::type
  147. init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
  148. {
  149. self->entries[0].one_state.push_front(&Transition::execute);
  150. }
  151. // Cell initializer function object, used with mpl::for_each
  152. template <class Transition>
  153. typename ::boost::enable_if<typename has_not_real_row_tag<Transition>::type,void >::type
  154. operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
  155. {
  156. // version for not real rows. No problem because irrelevant for process_event
  157. }
  158. template <class Transition>
  159. typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
  160. operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
  161. {
  162. //only if the transition event is a base of our event is the reinterpret_case safe
  163. init_event_base_case(tr,
  164. ::boost::mpl::bool_<
  165. ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>() );
  166. }
  167. dispatch_table* self;
  168. };
  169. // Cell default-initializer function object, used with mpl::for_each
  170. // initializes with call_no_transition, defer_transition or default_eventless_transition
  171. // variant for non-anonymous transitions
  172. template <class EventType,class Enable=void>
  173. struct default_init_cell
  174. {
  175. default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
  176. : self(self_),tofill_entries(tofill_entries_)
  177. {}
  178. template <bool deferred,bool composite, int some_dummy=0>
  179. struct helper
  180. {};
  181. template <int some_dummy> struct helper<true,false,some_dummy>
  182. {
  183. template <class State>
  184. static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
  185. {
  186. typedef typename create_stt<Fsm>::type stt;
  187. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  188. cell call_no_transition = &Fsm::defer_transition;
  189. tofill[state_id+1].one_state.push_back(call_no_transition);
  190. }
  191. };
  192. template <int some_dummy> struct helper<true,true,some_dummy>
  193. {
  194. template <class State>
  195. static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
  196. {
  197. typedef typename create_stt<Fsm>::type stt;
  198. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  199. cell call_no_transition = &Fsm::defer_transition;
  200. tofill[state_id+1].one_state.push_back(call_no_transition);
  201. }
  202. };
  203. template <int some_dummy> struct helper<false,true,some_dummy>
  204. {
  205. template <class State>
  206. static
  207. typename ::boost::enable_if<
  208. typename ::boost::is_same<State,Fsm>::type
  209. ,void>::type
  210. execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<0> = 0)
  211. {
  212. // for internal tables
  213. cell call_no_transition_internal = &Fsm::call_no_transition;
  214. tofill[0].one_state.push_front(call_no_transition_internal);
  215. }
  216. template <class State>
  217. static
  218. typename ::boost::disable_if<
  219. typename ::boost::is_same<State,Fsm>::type
  220. ,void>::type
  221. execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<1> = 0)
  222. {
  223. typedef typename create_stt<Fsm>::type stt;
  224. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  225. cell call_no_transition = &call_submachine< State >;
  226. tofill[state_id+1].one_state.push_front(call_no_transition);
  227. }
  228. };
  229. template <int some_dummy> struct helper<false,false,some_dummy>
  230. {
  231. template <class State>
  232. static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
  233. {
  234. typedef typename create_stt<Fsm>::type stt;
  235. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  236. cell call_no_transition = &Fsm::call_no_transition;
  237. tofill[state_id+1].one_state.push_back(call_no_transition);
  238. }
  239. };
  240. template <class State>
  241. void operator()(boost::msm::wrap<State> const& s)
  242. {
  243. helper<has_state_delayed_event<State,Event>::type::value,
  244. is_composite_state<State>::type::value>::execute(s,tofill_entries);
  245. }
  246. dispatch_table* self;
  247. chain_row* tofill_entries;
  248. };
  249. // variant for anonymous transitions
  250. template <class EventType>
  251. struct default_init_cell<EventType,
  252. typename ::boost::enable_if<
  253. typename is_completion_event<EventType>::type>::type>
  254. {
  255. default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
  256. : self(self_),tofill_entries(tofill_entries_)
  257. {}
  258. // this event is a compound one (not a real one, just one for use in event-less transitions)
  259. // Note this event cannot be used as deferred!
  260. template <class State>
  261. void operator()(boost::msm::wrap<State> const&)
  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::default_eventless_transition;
  266. tofill_entries[state_id+1].one_state.push_back(call_no_transition);
  267. }
  268. dispatch_table* self;
  269. chain_row* tofill_entries;
  270. };
  271. public:
  272. // initialize the dispatch table for a given Event and Fsm
  273. dispatch_table()
  274. {
  275. // Initialize cells for no transition
  276. ::boost::mpl::for_each<
  277. ::boost::mpl::filter_view<
  278. Stt, ::boost::is_base_of<transition_event< ::boost::mpl::placeholders::_>, Event> > >
  279. (init_cell(this));
  280. ::boost::mpl::for_each<
  281. typename generate_state_set<Stt>::type,
  282. boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  283. (default_init_cell<Event>(this,entries));
  284. }
  285. // The singleton instance.
  286. static const dispatch_table instance;
  287. public: // data members
  288. chain_row entries[max_state+1];
  289. };
  290. template <class Fsm,class Stt, class Event>
  291. const boost::msm::back::dispatch_table<Fsm,Stt, Event,favor_compile_time>
  292. dispatch_table<Fsm,Stt, Event,favor_compile_time>::instance;
  293. }}} // boost::msm::back
  294. #endif //BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H