CompositeTutorial.cpp 11 KB

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