Orthogonal-deferred.cpp 16 KB

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