History.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. struct NextSong {};
  26. struct PreviousSong {};
  27. // A "complicated" event type that carries some data.
  28. struct cd_detected
  29. {
  30. cd_detected(std::string name)
  31. : name(name)
  32. {}
  33. std::string name;
  34. };
  35. // front-end: define the FSM structure
  36. struct player_ : public msm::front::state_machine_def<player_>
  37. {
  38. // The list of FSM states
  39. struct Empty : public msm::front::state<>
  40. {
  41. // every (optional) entry/exit methods get the event passed
  42. template <class Event,class FSM>
  43. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  44. template <class Event,class FSM>
  45. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  46. };
  47. struct Open : public msm::front::state<>
  48. {
  49. template <class Event,class FSM>
  50. void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;}
  51. template <class Event,class FSM>
  52. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  53. };
  54. struct Stopped : public msm::front::state<>
  55. {
  56. // when stopped, the CD is loaded
  57. template <class Event,class FSM>
  58. void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;}
  59. template <class Event,class FSM>
  60. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  61. };
  62. struct Playing_ : public msm::front::state_machine_def<Playing_>
  63. {
  64. // when playing, the CD is loaded and we are in either pause or playing (duh)
  65. template <class Event,class FSM>
  66. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  67. template <class Event,class FSM>
  68. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  69. // The list of FSM states
  70. struct Song1 : public msm::front::state<>
  71. {
  72. template <class Event,class FSM>
  73. void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
  74. template <class Event,class FSM>
  75. void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
  76. };
  77. struct Song2 : public msm::front::state<>
  78. {
  79. template <class Event,class FSM>
  80. void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
  81. template <class Event,class FSM>
  82. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
  83. };
  84. struct Song3 : public msm::front::state<>
  85. {
  86. template <class Event,class FSM>
  87. void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
  88. template <class Event,class FSM>
  89. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
  90. };
  91. // the initial state. Must be defined
  92. typedef Song1 initial_state;
  93. // transition actions
  94. void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; }
  95. void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; }
  96. // guard conditions
  97. typedef Playing_ pl; // makes transition table cleaner
  98. // Transition table for Playing
  99. struct transition_table : mpl::vector4<
  100. // Start Event Next Action Guard
  101. // +---------+-------------+---------+---------------------+----------------------+
  102. a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
  103. a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >,
  104. a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
  105. a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >
  106. // +---------+-------------+---------+---------------------+----------------------+
  107. > {};
  108. // Replaces the default no-transition response.
  109. template <class FSM,class Event>
  110. void no_transition(Event const& e, FSM&,int state)
  111. {
  112. std::cout << "no transition from state " << state
  113. << " on event " << typeid(e).name() << std::endl;
  114. }
  115. };
  116. // back-end
  117. // demonstrates Shallow History: if the state gets activated with end_pause
  118. // then it will remember the last active state and reactivate it
  119. // also possible: AlwaysHistory, the last active state will always be reactivated
  120. // or NoHistory, always restart from the initial state
  121. typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing;
  122. // state not defining any entry or exit
  123. struct Paused : public msm::front::state<>
  124. {
  125. template <class Event,class FSM>
  126. void on_entry(Event const&,FSM& ) {std::cout << "entering: Paused" << std::endl;}
  127. template <class Event,class FSM>
  128. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Paused" << std::endl;}
  129. };
  130. // the initial state of the player SM. Must be defined
  131. typedef Empty initial_state;
  132. // transition actions
  133. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  134. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  135. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  136. void store_cd_info(cd_detected const& cd) {std::cout << "player::store_cd_info\n";}
  137. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  138. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  139. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  140. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  141. void stopped_again(stop const&){std::cout << "player::stopped_again\n";}
  142. // guard conditions
  143. typedef player_ p; // makes transition table cleaner
  144. // Transition table for player
  145. struct transition_table : mpl::vector<
  146. // Start Event Next Action Guard
  147. // +---------+-------------+---------+---------------------+----------------------+
  148. a_row < Stopped , play , Playing , &p::start_playback >,
  149. a_row < Stopped , open_close , Open , &p::open_drawer >,
  150. a_row < Stopped , stop , Stopped , &p::stopped_again >,
  151. // +---------+-------------+---------+---------------------+----------------------+
  152. a_row < Open , open_close , Empty , &p::close_drawer >,
  153. // +---------+-------------+---------+---------------------+----------------------+
  154. a_row < Empty , open_close , Open , &p::open_drawer >,
  155. a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
  156. // +---------+-------------+---------+---------------------+----------------------+
  157. a_row < Playing , stop , Stopped , &p::stop_playback >,
  158. a_row < Playing , pause , Paused , &p::pause_playback >,
  159. a_row < Playing , open_close , Open , &p::stop_and_open >,
  160. // +---------+-------------+---------+---------------------+----------------------+
  161. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  162. a_row < Paused , stop , Stopped , &p::stop_playback >,
  163. a_row < Paused , open_close , Open , &p::stop_and_open >
  164. // +---------+-------------+---------+---------------------+----------------------+
  165. > {};
  166. // Replaces the default no-transition response.
  167. template <class FSM,class Event>
  168. void no_transition(Event const& e, FSM&,int state)
  169. {
  170. std::cout << "no transition from state " << state
  171. << " on event " << typeid(e).name() << std::endl;
  172. }
  173. };
  174. // Pick a back-end
  175. typedef msm::back::state_machine<player_> player;
  176. //
  177. // Testing utilities.
  178. //
  179. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  180. void pstate(player const& p)
  181. {
  182. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  183. }
  184. void test()
  185. {
  186. player p;
  187. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  188. p.start();
  189. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  190. p.process_event(open_close()); pstate(p);
  191. p.process_event(open_close()); pstate(p);
  192. p.process_event(cd_detected("louie, louie"));
  193. p.process_event(play());
  194. // at this point, Play is active
  195. // make transition happen inside it. Player has no idea about this event but it's ok.
  196. p.process_event(NextSong());pstate(p); //2nd song active
  197. p.process_event(NextSong());pstate(p);//3rd song active
  198. p.process_event(PreviousSong());pstate(p);//2nd song active
  199. p.process_event(pause()); pstate(p);
  200. // go back to Playing
  201. // as you see, remembers the original state as end_pause is an history trigger
  202. p.process_event(end_pause()); pstate(p);
  203. p.process_event(pause()); pstate(p);
  204. p.process_event(stop()); pstate(p);
  205. // event leading to the same state
  206. p.process_event(stop()); pstate(p);
  207. // play does not trigger shallow history => start back from 1st song
  208. p.process_event(play()); pstate(p);
  209. }
  210. }
  211. int main()
  212. {
  213. test();
  214. return 0;
  215. }