AnonymousTutorialWithFunctors.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. //front-end
  14. #include <boost/msm/front/state_machine_def.hpp>
  15. // functors
  16. #include <boost/msm/front/functor_row.hpp>
  17. #include <boost/msm/front/euml/common.hpp>
  18. namespace msm = boost::msm;
  19. namespace mpl = boost::mpl;
  20. using namespace boost::msm::front;
  21. namespace
  22. {
  23. // events
  24. struct event1 {};
  25. // front-end: define the FSM structure
  26. struct my_machine_ : public msm::front::state_machine_def<my_machine_>
  27. {
  28. // The list of FSM states
  29. struct State1 : public msm::front::state<>
  30. {
  31. // every (optional) entry/exit methods get the event passed.
  32. template <class Event,class FSM>
  33. void on_entry(Event const&,FSM& ) {std::cout << "entering: State1" << std::endl;}
  34. template <class Event,class FSM>
  35. void on_exit(Event const&,FSM& ) {std::cout << "leaving: State1" << std::endl;}
  36. };
  37. struct State2 : public msm::front::state<>
  38. {
  39. template <class Event,class FSM>
  40. void on_entry(Event const& ,FSM&) {std::cout << "entering: State2" << std::endl;}
  41. template <class Event,class FSM>
  42. void on_exit(Event const&,FSM& ) {std::cout << "leaving: State2" << std::endl;}
  43. };
  44. struct State3 : public msm::front::state<>
  45. {
  46. // when stopped, the CD is loaded
  47. template <class Event,class FSM>
  48. void on_entry(Event const& ,FSM&) {std::cout << "entering: State3" << std::endl;}
  49. template <class Event,class FSM>
  50. void on_exit(Event const&,FSM& ) {std::cout << "leaving: State3" << std::endl;}
  51. };
  52. struct State4 : public msm::front::state<>
  53. {
  54. template <class Event,class FSM>
  55. void on_entry(Event const&,FSM& ) {std::cout << "entering: State4" << std::endl;}
  56. template <class Event,class FSM>
  57. void on_exit(Event const&,FSM& ) {std::cout << "leaving: State4" << std::endl;}
  58. };
  59. // the initial state of the player SM. Must be defined
  60. typedef State1 initial_state;
  61. // transition actions
  62. struct State2ToState3
  63. {
  64. template <class EVT,class FSM,class SourceState,class TargetState>
  65. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  66. {
  67. std::cout << "my_machine::State2ToState3" << std::endl;
  68. }
  69. };
  70. struct State3ToState4
  71. {
  72. template <class EVT,class FSM,class SourceState,class TargetState>
  73. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  74. {
  75. std::cout << "my_machine::State3ToState4" << std::endl;
  76. }
  77. };
  78. // guard conditions
  79. struct always_true
  80. {
  81. template <class EVT,class FSM,class SourceState,class TargetState>
  82. bool operator()(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt)
  83. {
  84. std::cout << "always_true" << std::endl;
  85. return true;
  86. }
  87. };
  88. struct always_false
  89. {
  90. template <class EVT,class FSM,class SourceState,class TargetState>
  91. bool operator()(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt)
  92. {
  93. std::cout << "always_false" << std::endl;
  94. return true;
  95. }
  96. };
  97. typedef my_machine_ p; // makes transition table cleaner
  98. // Transition table for player
  99. struct transition_table : mpl::vector<
  100. // Start Event Next Action Guard
  101. // +---------+-------------+---------+---------------------+----------------------+
  102. Row < State1 , none , State2 >,
  103. Row < State2 , none , State3 , State2ToState3 >,
  104. Row < State3 , none , State4 , none , always_false >,
  105. // +---------+-------------+---------+---------------------+----------------------+
  106. Row < State3 , none , State4 , State3ToState4 , always_true >,
  107. Row < State4 , event1 , State1 >
  108. // +---------+-------------+---------+---------------------+----------------------+
  109. > {};
  110. // Replaces the default no-transition response.
  111. template <class FSM,class Event>
  112. void no_transition(Event const& e, FSM&,int state)
  113. {
  114. std::cout << "no transition from state " << state
  115. << " on event " << typeid(e).name() << std::endl;
  116. }
  117. };
  118. // Pick a back-end
  119. typedef msm::back::state_machine<my_machine_> my_machine;
  120. //
  121. // Testing utilities.
  122. //
  123. static char const* const state_names[] = { "State1", "State2", "State3", "State4" };
  124. void pstate(my_machine const& p)
  125. {
  126. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  127. }
  128. void test()
  129. {
  130. my_machine p;
  131. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  132. // in this case it will also immediately trigger all anonymous transitions
  133. p.start();
  134. // this event will bring us back to the initial state and thus, a new "loop" will be started
  135. p.process_event(event1());
  136. }
  137. }
  138. int main()
  139. {
  140. test();
  141. return 0;
  142. }