SimpleTutorial.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. namespace
  18. {
  19. // events
  20. struct play {};
  21. struct end_pause {};
  22. struct stop {};
  23. struct pause {};
  24. struct open_close {};
  25. // A "complicated" event type that carries some data.
  26. enum DiskTypeEnum
  27. {
  28. DISK_CD=0,
  29. DISK_DVD=1
  30. };
  31. struct cd_detected
  32. {
  33. cd_detected(std::string name, DiskTypeEnum diskType)
  34. : name(name),
  35. disc_type(diskType)
  36. {}
  37. std::string name;
  38. DiskTypeEnum disc_type;
  39. };
  40. // front-end: define the FSM structure
  41. struct player_ : public msm::front::state_machine_def<player_>
  42. {
  43. template <class Event,class FSM>
  44. void on_entry(Event const& ,FSM&)
  45. {
  46. std::cout << "entering: Player" << std::endl;
  47. }
  48. template <class Event,class FSM>
  49. void on_exit(Event const&,FSM& )
  50. {
  51. std::cout << "leaving: Player" << std::endl;
  52. }
  53. // The list of FSM states
  54. struct Empty : public msm::front::state<>
  55. {
  56. // every (optional) entry/exit methods get the event passed.
  57. template <class Event,class FSM>
  58. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  59. template <class Event,class FSM>
  60. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  61. };
  62. struct Open : public msm::front::state<>
  63. {
  64. template <class Event,class FSM>
  65. void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
  66. template <class Event,class FSM>
  67. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  68. };
  69. // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
  70. struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr>
  71. {
  72. template <class Event,class FSM>
  73. void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
  74. template <class Event,class FSM>
  75. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  76. void set_sm_ptr(player_* pl)
  77. {
  78. m_player=pl;
  79. }
  80. player_* m_player;
  81. };
  82. struct Playing : public msm::front::state<>
  83. {
  84. template <class Event,class FSM>
  85. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  86. template <class Event,class FSM>
  87. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  88. };
  89. // state not defining any entry or exit
  90. struct Paused : public msm::front::state<>
  91. {
  92. };
  93. // the initial state of the player SM. Must be defined
  94. typedef Empty initial_state;
  95. // transition actions
  96. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  97. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  98. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  99. void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
  100. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  101. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  102. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  103. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  104. void stopped_again(stop const&) {std::cout << "player::stopped_again\n";}
  105. // guard conditions
  106. bool good_disk_format(cd_detected const& evt)
  107. {
  108. // to test a guard condition, let's say we understand only CDs, not DVD
  109. if (evt.disc_type != DISK_CD)
  110. {
  111. std::cout << "wrong disk, sorry" << std::endl;
  112. return false;
  113. }
  114. return true;
  115. }
  116. // used to show a transition conflict. This guard will simply deactivate one transition and thus
  117. // solve the conflict
  118. bool auto_start(cd_detected const&)
  119. {
  120. return false;
  121. }
  122. typedef player_ p; // makes transition table cleaner
  123. // Transition table for player
  124. struct transition_table : mpl::vector<
  125. // Start Event Next Action Guard
  126. // +---------+-------------+---------+---------------------+----------------------+
  127. a_row < Stopped , play , Playing , &p::start_playback >,
  128. a_row < Stopped , open_close , Open , &p::open_drawer >,
  129. _row < Stopped , stop , Stopped >,
  130. // +---------+-------------+---------+---------------------+----------------------+
  131. a_row < Open , open_close , Empty , &p::close_drawer >,
  132. // +---------+-------------+---------+---------------------+----------------------+
  133. a_row < Empty , open_close , Open , &p::open_drawer >,
  134. row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
  135. row < Empty , cd_detected , Playing , &p::store_cd_info ,&p::auto_start >,
  136. // +---------+-------------+---------+---------------------+----------------------+
  137. a_row < Playing , stop , Stopped , &p::stop_playback >,
  138. a_row < Playing , pause , Paused , &p::pause_playback >,
  139. a_row < Playing , open_close , Open , &p::stop_and_open >,
  140. // +---------+-------------+---------+---------------------+----------------------+
  141. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  142. a_row < Paused , stop , Stopped , &p::stop_playback >,
  143. a_row < Paused , open_close , Open , &p::stop_and_open >
  144. // +---------+-------------+---------+---------------------+----------------------+
  145. > {};
  146. // Replaces the default no-transition response.
  147. template <class FSM,class Event>
  148. void no_transition(Event const& e, FSM&,int state)
  149. {
  150. std::cout << "no transition from state " << state
  151. << " on event " << typeid(e).name() << std::endl;
  152. }
  153. };
  154. // Pick a back-end
  155. typedef msm::back::state_machine<player_> player;
  156. //
  157. // Testing utilities.
  158. //
  159. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  160. void pstate(player const& p)
  161. {
  162. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  163. }
  164. void test()
  165. {
  166. player p;
  167. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  168. p.start();
  169. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  170. p.process_event(open_close()); pstate(p);
  171. p.process_event(open_close()); pstate(p);
  172. // will be rejected, wrong disk type
  173. p.process_event(
  174. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  175. p.process_event(
  176. cd_detected("louie, louie",DISK_CD)); pstate(p);
  177. p.process_event(play());
  178. // at this point, Play is active
  179. p.process_event(pause()); pstate(p);
  180. // go back to Playing
  181. p.process_event(end_pause()); pstate(p);
  182. p.process_event(pause()); pstate(p);
  183. p.process_event(stop()); pstate(p);
  184. // event leading to the same state
  185. // no action method called as it is not present in the transition table
  186. p.process_event(stop()); pstate(p);
  187. std::cout << "stop fsm" << std::endl;
  188. p.stop();
  189. }
  190. }
  191. int main()
  192. {
  193. test();
  194. return 0;
  195. }