Orthogonal-deferred2.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/functor_row.hpp>
  16. namespace msm = boost::msm;
  17. namespace mpl = boost::mpl;
  18. using namespace boost::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. struct NextSong {};
  28. struct PreviousSong {};
  29. struct error_found {};
  30. struct end_error {};
  31. // Flags. Allow information about a property of the current state
  32. struct PlayingPaused{};
  33. struct CDLoaded {};
  34. struct FirstSongPlaying {};
  35. // A "complicated" event type that carries some data.
  36. struct cd_detected
  37. {
  38. cd_detected(std::string name)
  39. : name(name)
  40. {}
  41. std::string name;
  42. };
  43. // front-end: define the FSM structure
  44. struct player_ : public msm::front::state_machine_def<player_>
  45. {
  46. // we want deferred events and no state requires deferred events (only the fsm in the
  47. // transition table), so the fsm does.
  48. typedef int activate_deferred_events;
  49. // The list of FSM states
  50. struct Empty : public msm::front::state<>
  51. {
  52. // every (optional) entry/exit methods get the event passed.
  53. template <class Event,class FSM>
  54. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  55. template <class Event,class FSM>
  56. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  57. };
  58. struct Open : public msm::front::state<>
  59. {
  60. typedef mpl::vector1<CDLoaded> flag_list;
  61. template <class Event,class FSM>
  62. void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;}
  63. template <class Event,class FSM>
  64. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  65. };
  66. struct Stopped : public msm::front::state<>
  67. {
  68. // when stopped, the CD is loaded
  69. typedef mpl::vector1<CDLoaded> flag_list;
  70. template <class Event,class FSM>
  71. void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;}
  72. template <class Event,class FSM>
  73. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  74. };
  75. // the player state machine contains a state which is himself a state machine
  76. // as you see, no need to declare it anywhere so Playing can be developed separately
  77. // by another team in another module. For simplicity I just declare it inside player
  78. struct Playing_ : public msm::front::state_machine_def<Playing_>
  79. {
  80. // when playing, the CD is loaded and we are in either pause or playing (duh)
  81. typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
  82. template <class Event,class FSM>
  83. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  84. template <class Event,class FSM>
  85. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  86. // The list of FSM states
  87. struct Song1 : public msm::front::state<>
  88. {
  89. typedef mpl::vector1<FirstSongPlaying> flag_list;
  90. template <class Event,class FSM>
  91. void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
  92. template <class Event,class FSM>
  93. void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
  94. };
  95. struct Song2 : public msm::front::state<>
  96. {
  97. template <class Event,class FSM>
  98. void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
  99. template <class Event,class FSM>
  100. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
  101. };
  102. struct Song3 : public msm::front::state<>
  103. {
  104. template <class Event,class FSM>
  105. void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
  106. template <class Event,class FSM>
  107. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
  108. };
  109. // the initial state. Must be defined
  110. typedef Song1 initial_state;
  111. // transition actions
  112. void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; }
  113. void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; }
  114. // guard conditions
  115. typedef Playing_ pl; // makes transition table cleaner
  116. // Transition table for Playing
  117. struct transition_table : mpl::vector4<
  118. // Start Event Next Action Guard
  119. // +---------+-------------+---------+---------------------+----------------------+
  120. a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
  121. a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >,
  122. a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
  123. a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >
  124. // +---------+-------------+---------+---------------------+----------------------+
  125. > {};
  126. // Replaces the default no-transition response.
  127. template <class FSM,class Event>
  128. void no_transition(Event const& e, FSM&,int state)
  129. {
  130. std::cout << "no transition from state " << state
  131. << " on event " << typeid(e).name() << std::endl;
  132. }
  133. };
  134. // back-end
  135. typedef msm::back::state_machine<Playing_> Playing;
  136. // state not defining any entry or exit
  137. struct Paused : public msm::front::state<>
  138. {
  139. typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
  140. };
  141. struct AllOk : public msm::front::state<>
  142. {
  143. template <class Event,class FSM>
  144. void on_entry(Event const&,FSM& ) {std::cout << "starting: AllOk" << std::endl;}
  145. template <class Event,class FSM>
  146. void on_exit(Event const&,FSM& ) {std::cout << "finishing: AllOk" << std::endl;}
  147. };
  148. // this state is also made terminal so that all the events are blocked
  149. struct ErrorMode : //public msm::front::terminate_state<> // ErrorMode terminates the state machine
  150. public msm::front::interrupt_state<end_error> // ErroMode just interrupts. Will resume if
  151. // the event end_error is generated
  152. {
  153. template <class Event,class FSM>
  154. void on_entry(Event const&,FSM& ) {std::cout << "starting: ErrorMode" << std::endl;}
  155. template <class Event,class FSM>
  156. void on_exit(Event const&,FSM& ) {std::cout << "finishing: ErrorMode" << std::endl;}
  157. };
  158. // the initial state of the player SM. Must be defined
  159. typedef mpl::vector<Empty,AllOk> initial_state;
  160. // transition actions
  161. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  162. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  163. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  164. void store_cd_info(cd_detected const& cd) {std::cout << "player::store_cd_info\n";}
  165. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  166. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  167. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  168. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  169. void stopped_again(stop const&){std::cout << "player::stopped_again\n";}
  170. void report_error(error_found const&) {std::cout << "player::report_error\n";}
  171. void report_end_error(end_error const&) {std::cout << "player::report_end_error\n";}
  172. // guard conditions
  173. typedef player_ p; // makes transition table cleaner
  174. // Transition table for player
  175. struct transition_table : mpl::vector<
  176. // Start Event Next Action Guard
  177. // +---------+-------------+---------+---------------------+----------------------+
  178. a_row < Stopped , play , Playing , &p::start_playback >,
  179. a_row < Stopped , open_close , Open , &p::open_drawer >,
  180. a_row < Stopped , stop , Stopped , &p::stopped_again >,
  181. // +---------+-------------+---------+---------------------+----------------------+
  182. a_row < Open , open_close , Empty , &p::close_drawer >,
  183. Row < Open , play , none , Defer , none >,
  184. // +---------+-------------+---------+---------------------+----------------------+
  185. a_row < Empty , open_close , Open , &p::open_drawer >,
  186. a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
  187. Row < Empty , play , none , Defer , none >,
  188. // +---------+-------------+---------+---------------------+----------------------+
  189. a_row < Playing , stop , Stopped , &p::stop_playback >,
  190. a_row < Playing , pause , Paused , &p::pause_playback >,
  191. a_row < Playing , open_close , Open , &p::stop_and_open >,
  192. // +---------+-------------+---------+---------------------+----------------------+
  193. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  194. a_row < Paused , stop , Stopped , &p::stop_playback >,
  195. a_row < Paused , open_close , Open , &p::stop_and_open >,
  196. // +---------+-------------+---------+---------------------+----------------------+
  197. a_row < AllOk , error_found ,ErrorMode, &p::report_error >,
  198. a_row <ErrorMode,end_error ,AllOk , &p::report_end_error >
  199. // +---------+-------------+---------+---------------------+----------------------+
  200. > {};
  201. // Replaces the default no-transition response.
  202. template <class FSM,class Event>
  203. void no_transition(Event const& e, FSM&,int state)
  204. {
  205. std::cout << "no transition from state " << state
  206. << " on event " << typeid(e).name() << std::endl;
  207. }
  208. };
  209. // Pick a back-end
  210. typedef msm::back::state_machine<player_> player;
  211. //
  212. // Testing utilities.
  213. //
  214. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode" };
  215. void pstate(player const& p)
  216. {
  217. // we have now several active states, which we show
  218. for (unsigned int i=0;i<player::nr_regions::value;++i)
  219. {
  220. std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
  221. }
  222. }
  223. void test()
  224. {
  225. player p;
  226. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  227. p.start();
  228. // test deferred event
  229. // deferred in Empty and Open, will be handled only after event cd_detected
  230. p.process_event(play());
  231. // tests some flags
  232. std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl; //=> false (no CD yet)
  233. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  234. p.process_event(open_close()); pstate(p);
  235. p.process_event(open_close()); pstate(p);
  236. p.process_event(cd_detected("louie, louie"));
  237. // at this point, Play is active (was deferred)
  238. std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
  239. std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> true
  240. // make transition happen inside it. Player has no idea about this event but it's ok.
  241. p.process_event(NextSong());pstate(p); //2nd song active
  242. p.process_event(NextSong());pstate(p);//3rd song active
  243. p.process_event(PreviousSong());pstate(p);//2nd song active
  244. std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false
  245. std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
  246. p.process_event(pause()); pstate(p);
  247. std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
  248. // go back to Playing
  249. // as you see, it starts back from the original state
  250. p.process_event(end_pause()); pstate(p);
  251. p.process_event(pause()); pstate(p);
  252. p.process_event(stop()); pstate(p);
  253. std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> false
  254. std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl;//=> true
  255. // by default, the flags are OR'ed but you can also use AND. Then the flag must be present in
  256. // all of the active states
  257. std::cout << "CDLoaded active with AND:" << std::boolalpha << p.is_flag_active<CDLoaded,player::Flag_AND>() << std::endl;//=> false
  258. // event leading to the same state
  259. p.process_event(stop()); pstate(p);
  260. // event leading to a terminal/interrupt state
  261. p.process_event(error_found()); pstate(p);
  262. // try generating more events
  263. std::cout << "Trying to generate another event" << std::endl; // will not work, fsm is terminated or interrupted
  264. p.process_event(play());pstate(p);
  265. std::cout << "Trying to end the error" << std::endl; // will work only if ErrorMode is interrupt state
  266. p.process_event(end_error());pstate(p);
  267. std::cout << "Trying to generate another event" << std::endl; // will work only if ErrorMode is interrupt state
  268. p.process_event(play());pstate(p);
  269. }
  270. }
  271. int main()
  272. {
  273. test();
  274. return 0;
  275. }