Flags.cpp 12 KB

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