AnonymousTutorialEuml.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2010 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. #include <iostream>
  11. // back-end
  12. #include <boost/msm/back/state_machine.hpp>
  13. #include <boost/msm/front/euml/euml.hpp>
  14. namespace msm = boost::msm;
  15. using namespace boost::msm::front::euml;
  16. namespace
  17. {
  18. // events
  19. BOOST_MSM_EUML_EVENT(event1)
  20. BOOST_MSM_EUML_ACTION(State1_Entry)
  21. {
  22. template <class Event,class FSM,class STATE>
  23. void operator()(Event const&,FSM&,STATE& )
  24. {
  25. std::cout << "entering: State1" << std::endl;
  26. }
  27. };
  28. BOOST_MSM_EUML_ACTION(State1_Exit)
  29. {
  30. template <class Event,class FSM,class STATE>
  31. void operator()(Event const&,FSM&,STATE& )
  32. {
  33. std::cout << "leaving: State1" << std::endl;
  34. }
  35. };
  36. BOOST_MSM_EUML_ACTION(State2_Entry)
  37. {
  38. template <class Event,class FSM,class STATE>
  39. void operator()(Event const&,FSM&,STATE& )
  40. {
  41. std::cout << "entering: State2" << std::endl;
  42. }
  43. };
  44. BOOST_MSM_EUML_ACTION(State2_Exit)
  45. {
  46. template <class Event,class FSM,class STATE>
  47. void operator()(Event const&,FSM&,STATE& )
  48. {
  49. std::cout << "leaving: State2" << std::endl;
  50. }
  51. };
  52. BOOST_MSM_EUML_ACTION(State3_Entry)
  53. {
  54. template <class Event,class FSM,class STATE>
  55. void operator()(Event const&,FSM&,STATE& )
  56. {
  57. std::cout << "entering: State3" << std::endl;
  58. }
  59. };
  60. BOOST_MSM_EUML_ACTION(State3_Exit)
  61. {
  62. template <class Event,class FSM,class STATE>
  63. void operator()(Event const&,FSM&,STATE& )
  64. {
  65. std::cout << "leaving: State3" << std::endl;
  66. }
  67. };
  68. BOOST_MSM_EUML_ACTION(State4_Entry)
  69. {
  70. template <class Event,class FSM,class STATE>
  71. void operator()(Event const&,FSM&,STATE& )
  72. {
  73. std::cout << "entering: State4" << std::endl;
  74. }
  75. };
  76. BOOST_MSM_EUML_ACTION(State4_Exit)
  77. {
  78. template <class Event,class FSM,class STATE>
  79. void operator()(Event const&,FSM&,STATE& )
  80. {
  81. std::cout << "leaving: State4" << std::endl;
  82. }
  83. };
  84. // The list of FSM states
  85. BOOST_MSM_EUML_STATE(( State1_Entry,State1_Exit ),State1)
  86. BOOST_MSM_EUML_STATE(( State2_Entry,State2_Exit ),State2)
  87. BOOST_MSM_EUML_STATE(( State3_Entry,State3_Exit ),State3)
  88. BOOST_MSM_EUML_STATE(( State4_Entry,State4_Exit ),State4)
  89. // transition actions
  90. BOOST_MSM_EUML_ACTION(State2ToState3)
  91. {
  92. template <class FSM,class EVT,class SourceState,class TargetState>
  93. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  94. {
  95. std::cout << "my_machine::State2ToState3" << std::endl;
  96. }
  97. };
  98. BOOST_MSM_EUML_ACTION(State3ToState4)
  99. {
  100. template <class FSM,class EVT,class SourceState,class TargetState>
  101. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  102. {
  103. std::cout << "my_machine::State3ToState4" << std::endl;
  104. }
  105. };
  106. // guard conditions
  107. BOOST_MSM_EUML_ACTION(always_true)
  108. {
  109. template <class FSM,class EVT,class SourceState,class TargetState>
  110. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  111. {
  112. std::cout << "always_true" << std::endl;
  113. return true;
  114. }
  115. };
  116. BOOST_MSM_EUML_ACTION(always_false)
  117. {
  118. template <class FSM,class EVT,class SourceState,class TargetState>
  119. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  120. {
  121. std::cout << "always_false" << std::endl;
  122. return false;
  123. }
  124. };
  125. // replaces the old transition table
  126. BOOST_MSM_EUML_TRANSITION_TABLE((
  127. State2 == State1 ,
  128. State3 == State2 / State2ToState3,
  129. State4 == State3 [always_true] / State3ToState4,
  130. State4 == State3 [always_false],
  131. State1 == State4 + event1
  132. // +------------------------------------------------------------------------------+
  133. ),transition_table)
  134. // create a state machine "on the fly"
  135. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  136. init_ << State1 // Init State
  137. ),
  138. my_machine_) //fsm name
  139. // Pick a back-end
  140. typedef msm::back::state_machine<my_machine_> my_machine;
  141. //
  142. // Testing utilities.
  143. //
  144. static char const* const state_names[] = { "State1", "State2", "State3", "State4" };
  145. void pstate(my_machine const& p)
  146. {
  147. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  148. }
  149. void test()
  150. {
  151. my_machine p;
  152. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  153. // in this case it will also immediately trigger all anonymous transitions
  154. p.start();
  155. // this event will bring us back to the initial state and thus, a new "loop" will be started
  156. p.process_event(event1);
  157. }
  158. }
  159. int main()
  160. {
  161. test();
  162. return 0;
  163. }