/Src/Dependencies/Boost/boost/msm/front/functor_row.hpp

http://hadesmem.googlecode.com/ · C++ Header · 338 lines · 287 code · 15 blank · 36 comment · 0 complexity · 935ac834e6a5f8e499a489d86f33f3a0 MD5 · raw file

  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_FRONT_FUNCTOR_ROW_H
  11. #define BOOST_MSM_FRONT_FUNCTOR_ROW_H
  12. #include <boost/mpl/set.hpp>
  13. #include <boost/mpl/for_each.hpp>
  14. #include <boost/mpl/has_xxx.hpp>
  15. #include <boost/typeof/typeof.hpp>
  16. #include <boost/msm/back/common_types.hpp>
  17. #include <boost/msm/row_tags.hpp>
  18. #include <boost/msm/common.hpp>
  19. #include <boost/msm/front/completion_event.hpp>
  20. #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
  21. BOOST_MPL_HAS_XXX_TRAIT_DEF(deferring_action)
  22. namespace boost { namespace msm { namespace front
  23. {
  24. template <class Func,class Enable=void>
  25. struct get_functor_return_value
  26. {
  27. static const ::boost::msm::back::HandledEnum value = ::boost::msm::back::HANDLED_TRUE;
  28. };
  29. template <class Func>
  30. struct get_functor_return_value<Func,
  31. typename ::boost::enable_if<
  32. typename has_deferring_action<Func>::type
  33. >::type
  34. >
  35. {
  36. static const ::boost::msm::back::HandledEnum value = ::boost::msm::back::HANDLED_DEFERRED;
  37. };
  38. template <class SOURCE,class EVENT,class TARGET,class ACTION=none,class GUARD=none>
  39. struct Row
  40. {
  41. typedef SOURCE Source;
  42. typedef EVENT Evt;
  43. typedef TARGET Target;
  44. typedef ACTION Action;
  45. typedef GUARD Guard;
  46. // action plus guard
  47. typedef row_tag row_type_tag;
  48. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  49. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  50. {
  51. // create functor, call it
  52. Action()(evt,fsm,src,tgt);
  53. return get_functor_return_value<Action>::value;
  54. }
  55. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  56. static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt,AllStates&)
  57. {
  58. // create functor, call it
  59. return Guard()(evt,fsm,src,tgt);
  60. }
  61. };
  62. template<class SOURCE,class EVENT,class TARGET>
  63. struct Row<SOURCE,EVENT,TARGET,none,none>
  64. {
  65. typedef SOURCE Source;
  66. typedef EVENT Evt;
  67. typedef TARGET Target;
  68. typedef none Action;
  69. typedef none Guard;
  70. // no action, no guard
  71. typedef _row_tag row_type_tag;
  72. };
  73. template<class SOURCE,class EVENT,class TARGET,class ACTION>
  74. struct Row<SOURCE,EVENT,TARGET,ACTION,none>
  75. {
  76. typedef SOURCE Source;
  77. typedef EVENT Evt;
  78. typedef TARGET Target;
  79. typedef ACTION Action;
  80. typedef none Guard;
  81. // no guard
  82. typedef a_row_tag row_type_tag;
  83. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  84. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  85. {
  86. // create functor, call it
  87. Action()(evt,fsm,src,tgt);
  88. return get_functor_return_value<Action>::value;
  89. }
  90. };
  91. template<class SOURCE,class EVENT,class TARGET,class GUARD>
  92. struct Row<SOURCE,EVENT,TARGET,none,GUARD>
  93. {
  94. typedef SOURCE Source;
  95. typedef EVENT Evt;
  96. typedef TARGET Target;
  97. typedef none Action;
  98. typedef GUARD Guard;
  99. // no action
  100. typedef g_row_tag row_type_tag;
  101. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  102. static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  103. {
  104. // create functor, call it
  105. return Guard()(evt,fsm,src,tgt);
  106. }
  107. };
  108. // internal transitions
  109. template<class SOURCE,class EVENT,class ACTION>
  110. struct Row<SOURCE,EVENT,none,ACTION,none>
  111. {
  112. typedef SOURCE Source;
  113. typedef EVENT Evt;
  114. typedef Source Target;
  115. typedef ACTION Action;
  116. typedef none Guard;
  117. // no guard
  118. typedef a_irow_tag row_type_tag;
  119. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  120. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  121. {
  122. // create functor, call it
  123. Action()(evt,fsm,src,tgt);
  124. return get_functor_return_value<Action>::value;
  125. }
  126. };
  127. template<class SOURCE,class EVENT,class GUARD>
  128. struct Row<SOURCE,EVENT,none,none,GUARD>
  129. {
  130. typedef SOURCE Source;
  131. typedef EVENT Evt;
  132. typedef Source Target;
  133. typedef none Action;
  134. typedef GUARD Guard;
  135. // no action
  136. typedef g_irow_tag row_type_tag;
  137. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  138. static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  139. {
  140. // create functor, call it
  141. return Guard()(evt,fsm,src,tgt);
  142. }
  143. };
  144. template<class SOURCE,class EVENT,class ACTION,class GUARD>
  145. struct Row<SOURCE,EVENT,none,ACTION,GUARD>
  146. {
  147. typedef SOURCE Source;
  148. typedef EVENT Evt;
  149. typedef Source Target;
  150. typedef ACTION Action;
  151. typedef GUARD Guard;
  152. // action + guard
  153. typedef irow_tag row_type_tag;
  154. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  155. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  156. {
  157. // create functor, call it
  158. Action()(evt,fsm,src,tgt);
  159. return get_functor_return_value<Action>::value;
  160. }
  161. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  162. static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  163. {
  164. // create functor, call it
  165. return Guard()(evt,fsm,src,tgt);
  166. }
  167. };
  168. template<class SOURCE,class EVENT>
  169. struct Row<SOURCE,EVENT,none,none,none>
  170. {
  171. typedef SOURCE Source;
  172. typedef EVENT Evt;
  173. typedef Source Target;
  174. typedef none Action;
  175. typedef none Guard;
  176. // no action, no guard
  177. typedef _irow_tag row_type_tag;
  178. };
  179. template<class TGT>
  180. struct get_row_target
  181. {
  182. typedef typename TGT::Target type;
  183. };
  184. template <class EVENT,class ACTION=none,class GUARD=none>
  185. struct Internal
  186. {
  187. typedef EVENT Evt;
  188. typedef ACTION Action;
  189. typedef GUARD Guard;
  190. // action plus guard
  191. typedef sm_i_row_tag row_type_tag;
  192. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  193. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  194. {
  195. // create functor, call it
  196. Action()(evt,fsm,src,tgt);
  197. return get_functor_return_value<Action>::value;
  198. }
  199. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  200. static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  201. {
  202. // create functor, call it
  203. return Guard()(evt,fsm,src,tgt);
  204. }
  205. };
  206. template<class EVENT,class ACTION>
  207. struct Internal<EVENT,ACTION,none>
  208. {
  209. typedef EVENT Evt;
  210. typedef ACTION Action;
  211. typedef none Guard;
  212. // no guard
  213. typedef sm_a_i_row_tag row_type_tag;
  214. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  215. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  216. {
  217. // create functor, call it
  218. Action()(evt,fsm,src,tgt);
  219. return get_functor_return_value<Action>::value;
  220. }
  221. };
  222. template<class EVENT,class GUARD>
  223. struct Internal<EVENT,none,GUARD>
  224. {
  225. typedef EVENT Evt;
  226. typedef none Action;
  227. typedef GUARD Guard;
  228. // no action
  229. typedef sm_g_i_row_tag row_type_tag;
  230. template <class EVT,class FSM,class SourceState,class TargetState,class AllStates>
  231. static bool guard_call(FSM& fsm,EVT const& evt,SourceState& src,TargetState& tgt, AllStates&)
  232. {
  233. // create functor, call it
  234. return Guard()(evt,fsm,src,tgt);
  235. }
  236. };
  237. template<class EVENT>
  238. struct Internal<EVENT,none,none>
  239. {
  240. typedef EVENT Evt;
  241. typedef none Action;
  242. typedef none Guard;
  243. // no action, no guard
  244. typedef sm__i_row_tag row_type_tag;
  245. };
  246. struct event_tag{};
  247. struct action_tag{};
  248. struct state_action_tag{};
  249. struct flag_tag{};
  250. struct config_tag{};
  251. struct not_euml_tag{};
  252. template <class Sequence>
  253. struct ActionSequence_
  254. {
  255. typedef Sequence sequence;
  256. template <class Event,class FSM,class STATE >
  257. struct state_action_result
  258. {
  259. typedef void type;
  260. };
  261. template <class EVT,class FSM,class STATE>
  262. struct Call
  263. {
  264. Call(EVT const& evt,FSM& fsm,STATE& state):
  265. evt_(evt),fsm_(fsm),state_(state){}
  266. template <class FCT>
  267. void operator()(::boost::msm::wrap<FCT> const& )
  268. {
  269. FCT()(evt_,fsm_,state_);
  270. }
  271. private:
  272. EVT const& evt_;
  273. FSM& fsm_;
  274. STATE& state_;
  275. };
  276. template <class EVT,class FSM,class SourceState,class TargetState>
  277. struct transition_action_result
  278. {
  279. typedef void type;
  280. };
  281. template <class EVT,class FSM,class SourceState,class TargetState>
  282. struct Call2
  283. {
  284. Call2(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt):
  285. evt_(evt),fsm_(fsm),src_(src),tgt_(tgt){}
  286. template <class FCT>
  287. void operator()(::boost::msm::wrap<FCT> const& )
  288. {
  289. FCT()(evt_,fsm_,src_,tgt_);
  290. }
  291. private:
  292. EVT const & evt_;
  293. FSM& fsm_;
  294. SourceState& src_;
  295. TargetState& tgt_;
  296. };
  297. typedef ::boost::mpl::set<state_action_tag,action_tag> tag_type;
  298. template <class EVT,class FSM,class STATE>
  299. void operator()(EVT const& evt,FSM& fsm,STATE& state)
  300. {
  301. mpl::for_each<Sequence,boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  302. (Call<EVT,FSM,STATE>(evt,fsm,state));
  303. }
  304. template <class EVT,class FSM,class SourceState,class TargetState>
  305. void operator()(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt)
  306. {
  307. mpl::for_each<Sequence,boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  308. (Call2<EVT,FSM,SourceState,TargetState>(evt,fsm,src,tgt));
  309. }
  310. };
  311. // functor pre-defined for basic functionality
  312. struct Defer
  313. {
  314. // mark as deferring to avoid stack overflows in certain conditions
  315. typedef int deferring_action;
  316. template <class EVT,class FSM,class SourceState,class TargetState>
  317. void operator()(EVT const& evt,FSM& fsm,SourceState& ,TargetState& ) const
  318. {
  319. fsm.defer_event(evt);
  320. }
  321. };
  322. }}}
  323. #endif //BOOST_MSM_FRONT_FUNCTOR_ROW_H