SimpleTutorial2.cpp 9.5 KB

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