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