state_machine_def.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_STATEMACHINE_DEF_H
  11. #define BOOST_MSM_FRONT_STATEMACHINE_DEF_H
  12. #include <exception>
  13. #include <boost/assert.hpp>
  14. #include <boost/fusion/include/vector.hpp>
  15. #include <boost/mpl/vector.hpp>
  16. #include <boost/msm/row_tags.hpp>
  17. #include <boost/msm/back/common_types.hpp>
  18. #include <boost/msm/front/states.hpp>
  19. #include <boost/msm/front/completion_event.hpp>
  20. #include <boost/msm/front/common_states.hpp>
  21. namespace boost { namespace msm { namespace front
  22. {
  23. template<class Derived,class BaseState = default_base_state>
  24. struct state_machine_def : public boost::msm::front::detail::state_base<BaseState>
  25. {
  26. // tags
  27. // default: no flag
  28. typedef ::boost::fusion::vector0<> flag_list;
  29. typedef ::boost::fusion::vector0<> internal_flag_list;
  30. //default: no deferred events
  31. typedef ::boost::fusion::vector0<> deferred_events;
  32. // customization (message queue, exceptions)
  33. typedef ::boost::fusion::vector0<> configuration;
  34. typedef BaseState BaseAllStates;
  35. template<
  36. typename T1
  37. , class Event
  38. , typename T2
  39. , void (Derived::*action)(Event const&)
  40. >
  41. struct a_row
  42. {
  43. typedef a_row_tag row_type_tag;
  44. typedef T1 Source;
  45. typedef T2 Target;
  46. typedef Event Evt;
  47. template <class FSM,class SourceState,class TargetState,class AllStates>
  48. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,Event const& evt,SourceState&,TargetState&, AllStates&)
  49. {
  50. // in this front-end, we don't need to know source and target states
  51. (fsm.*action)(evt);
  52. return ::boost::msm::back::HANDLED_TRUE;
  53. }
  54. };
  55. template<
  56. typename T1
  57. , class Event
  58. , typename T2
  59. >
  60. struct _row
  61. {
  62. typedef _row_tag row_type_tag;
  63. typedef T1 Source;
  64. typedef T2 Target;
  65. typedef Event Evt;
  66. };
  67. template<
  68. typename T1
  69. , class Event
  70. , typename T2
  71. , void (Derived::*action)(Event const&)
  72. , bool (Derived::*guard)(Event const&)
  73. >
  74. struct row
  75. {
  76. typedef row_tag row_type_tag;
  77. typedef T1 Source;
  78. typedef T2 Target;
  79. typedef Event Evt;
  80. template <class FSM,class SourceState,class TargetState, class AllStates>
  81. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
  82. {
  83. // in this front-end, we don't need to know source and target states
  84. (fsm.*action)(evt);
  85. return ::boost::msm::back::HANDLED_TRUE;
  86. }
  87. template <class FSM,class SourceState,class TargetState,class AllStates>
  88. static bool guard_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
  89. {
  90. // in this front-end, we don't need to know source and target states
  91. return (fsm.*guard)(evt);
  92. }
  93. };
  94. template<
  95. typename T1
  96. , class Event
  97. , typename T2
  98. , bool (Derived::*guard)(Event const&)
  99. >
  100. struct g_row
  101. {
  102. typedef g_row_tag row_type_tag;
  103. typedef T1 Source;
  104. typedef T2 Target;
  105. typedef Event Evt;
  106. template <class FSM,class SourceState,class TargetState,class AllStates>
  107. static bool guard_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
  108. {
  109. // in this front-end, we don't need to know source and target states
  110. return (fsm.*guard)(evt);
  111. }
  112. };
  113. // internal transitions
  114. template<
  115. typename T1
  116. , class Event
  117. , void (Derived::*action)(Event const&)
  118. >
  119. struct a_irow
  120. {
  121. typedef a_irow_tag row_type_tag;
  122. typedef T1 Source;
  123. typedef T1 Target;
  124. typedef Event Evt;
  125. template <class FSM,class SourceState,class TargetState,class AllStates>
  126. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
  127. {
  128. // in this front-end, we don't need to know source and target states
  129. (fsm.*action)(evt);
  130. return ::boost::msm::back::HANDLED_TRUE;
  131. }
  132. };
  133. template<
  134. typename T1
  135. , class Event
  136. , void (Derived::*action)(Event const&)
  137. , bool (Derived::*guard)(Event const&)
  138. >
  139. struct irow
  140. {
  141. typedef irow_tag row_type_tag;
  142. typedef T1 Source;
  143. typedef T1 Target;
  144. typedef Event Evt;
  145. template <class FSM,class SourceState,class TargetState,class AllStates>
  146. static ::boost::msm::back::HandledEnum action_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
  147. {
  148. // in this front-end, we don't need to know source and target states
  149. (fsm.*action)(evt);
  150. return ::boost::msm::back::HANDLED_TRUE;
  151. }
  152. template <class FSM,class SourceState,class TargetState,class AllStates>
  153. static bool guard_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
  154. {
  155. // in this front-end, we don't need to know source and target states
  156. return (fsm.*guard)(evt);
  157. }
  158. };
  159. template<
  160. typename T1
  161. , class Event
  162. , bool (Derived::*guard)(Event const&)
  163. >
  164. struct g_irow
  165. {
  166. typedef g_irow_tag row_type_tag;
  167. typedef T1 Source;
  168. typedef T1 Target;
  169. typedef Event Evt;
  170. template <class FSM,class SourceState,class TargetState,class AllStates>
  171. static bool guard_call(FSM& fsm,Event const& evt,SourceState&,TargetState&,AllStates&)
  172. {
  173. // in this front-end, we don't need to know source and target states
  174. return (fsm.*guard)(evt);
  175. }
  176. };
  177. // internal row withou action or guard. Does nothing except forcing the event to be ignored.
  178. template<
  179. typename T1
  180. , class Event
  181. >
  182. struct _irow
  183. {
  184. typedef _irow_tag row_type_tag;
  185. typedef T1 Source;
  186. typedef T1 Target;
  187. typedef Event Evt;
  188. };
  189. protected:
  190. // Default no-transition handler. Can be replaced in the Derived SM class.
  191. template <class FSM,class Event>
  192. void no_transition(Event const& ,FSM&, int )
  193. {
  194. BOOST_ASSERT(false);
  195. }
  196. // default exception handler. Can be replaced in the Derived SM class.
  197. template <class FSM,class Event>
  198. void exception_caught (Event const&,FSM&,std::exception& )
  199. {
  200. BOOST_ASSERT(false);
  201. }
  202. };
  203. } } }// boost::msm::front
  204. #endif //BOOST_MSM_FRONT_STATEMACHINE_DEF_H