AnonymousTutorial.cpp 5.1 KB

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