Visitor.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #include <string>
  12. #include "boost/mpl/vector/vector30.hpp"
  13. #include <boost/msm/back/state_machine.hpp>
  14. #include <boost/msm/front/state_machine_def.hpp>
  15. #include <boost/msm/back/tools.hpp>
  16. using namespace std;
  17. namespace msm = boost::msm;
  18. namespace mpl = boost::mpl;
  19. namespace // Concrete FSM implementation
  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 ThreeSec {};
  30. struct TenSec {};
  31. struct error_found {};
  32. struct end_error {};
  33. struct cd_detected {};
  34. // a simple visitor
  35. struct SomeVisitor
  36. {
  37. template <class T>
  38. void visit_state(T* astate,int i)
  39. {
  40. std::cout << "visiting state:" << typeid(*astate).name()
  41. << " with data:" << i << std::endl;
  42. }
  43. };
  44. // base state for all states of ths fsm, to make them visitable
  45. struct my_visitable_state
  46. {
  47. // signature of the accept function
  48. typedef msm::back::args<void,SomeVisitor&,int> accept_sig;
  49. // we also want polymorphic states
  50. virtual ~my_visitable_state() {}
  51. // default implementation for states who do not need to be visited
  52. void accept(SomeVisitor&,int) const {}
  53. // or if you want all states to be visited, provide an implementation
  54. /*
  55. void accept(SomeVisitor& vis,int i) const
  56. {
  57. vis.visit_state(this,i);
  58. }
  59. */
  60. };
  61. // Concrete FSM implementation
  62. struct player_ : public msm::front::state_machine_def<player_,my_visitable_state>
  63. {
  64. template <class Event,class FSM>
  65. void on_entry(Event const&,FSM& ) {std::cout << "starting: player" << std::endl;}
  66. // The list of FSM states
  67. struct Empty : public msm::front::state<my_visitable_state>
  68. {
  69. typedef mpl::vector<play> deferred_events;
  70. template <class Event,class FSM>
  71. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  72. template <class Event,class FSM>
  73. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  74. // this state wants to be visited
  75. void accept(SomeVisitor& vis,int i) const
  76. {
  77. vis.visit_state(this,i);
  78. }
  79. };
  80. struct Open : public msm::front::state<my_visitable_state>
  81. {
  82. typedef mpl::vector<play> deferred_events;
  83. template <class Event,class FSM>
  84. void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;}
  85. template <class Event,class FSM>
  86. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  87. // this state wants to be visited
  88. void accept(SomeVisitor& vis,int i) const
  89. {
  90. vis.visit_state(this,i);
  91. }
  92. };
  93. struct Stopped : public msm::front::state<my_visitable_state>
  94. {
  95. template <class Event,class FSM>
  96. void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;}
  97. template <class Event,class FSM>
  98. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  99. // this state wants to be visited
  100. void accept(SomeVisitor& vis,int i) const
  101. {
  102. // note that visiting will recursively visit sub-states
  103. vis.visit_state(this,i);
  104. }
  105. };
  106. struct Playing_ : public msm::front::state_machine_def<Playing_,my_visitable_state >
  107. {
  108. template <class Event,class FSM>
  109. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  110. template <class Event,class FSM>
  111. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  112. void accept(SomeVisitor& vis,int i) const
  113. {
  114. // note that visiting will recursively visit sub-states
  115. vis.visit_state(this,i);
  116. }
  117. // The list of FSM states
  118. // the Playing state machine contains a state which is himself a state machine
  119. // so we have a SM containing a SM containing a SM
  120. struct Song1_ : public msm::front::state_machine_def<Song1_,my_visitable_state>
  121. {
  122. template <class Event,class FSM>
  123. void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
  124. template <class Event,class FSM>
  125. void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
  126. void accept(SomeVisitor& vis,int i) const
  127. {
  128. vis.visit_state(this,i);
  129. }
  130. struct LightOn : public msm::front::state<my_visitable_state>
  131. {
  132. template <class Event,class FSM>
  133. void on_entry(Event const&,FSM& ) {std::cout << "starting: LightOn" << std::endl;}
  134. template <class Event,class FSM>
  135. void on_exit(Event const&,FSM& ) {std::cout << "finishing: LightOn" << std::endl;}
  136. void accept(SomeVisitor& vis,int i) const
  137. {
  138. // note that visiting will recursively visit sub-states
  139. vis.visit_state(this,i);
  140. }
  141. };
  142. struct LightOff : public msm::front::state<my_visitable_state>
  143. {
  144. template <class Event,class FSM>
  145. void on_entry(Event const&,FSM& ) {std::cout << "starting: LightOff" << std::endl;}
  146. template <class Event,class FSM>
  147. void on_exit(Event const&,FSM& ) {std::cout << "finishing: LightOff" << std::endl;}
  148. };
  149. // the initial state. Must be defined
  150. typedef LightOn initial_state;
  151. // transition actions
  152. void turn_light_off(ThreeSec const&) { std::cout << "3s off::turn light off\n"; }
  153. // guard conditions
  154. typedef Song1_ s; // makes transition table cleaner
  155. // Transition table for Song1
  156. struct transition_table : mpl::vector1<
  157. // Start Event Next Action Guard
  158. // +---------+-------------+---------+---------------------+----------------------+
  159. a_row < LightOn , ThreeSec , LightOff, &s::turn_light_off >
  160. // +---------+-------------+---------+---------------------+----------------------+
  161. > {};
  162. // Replaces the default no-transition response.
  163. template <class FSM,class Event>
  164. void no_transition(Event const& e, FSM&,int state)
  165. {
  166. std::cout << "no transition from state " << state
  167. << " on event " << typeid(e).name() << std::endl;
  168. }
  169. };
  170. typedef msm::back::state_machine<Song1_> Song1;
  171. struct Song2 : public msm::front::state<my_visitable_state>
  172. {
  173. template <class Event,class FSM>
  174. void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
  175. template <class Event,class FSM>
  176. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
  177. };
  178. struct Song3 : public msm::front::state<my_visitable_state>
  179. {
  180. template <class Event,class FSM>
  181. void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
  182. template <class Event,class FSM>
  183. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
  184. };
  185. // the initial state. Must be defined
  186. typedef Song1 initial_state;
  187. // transition actions
  188. void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; }
  189. void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; }
  190. // guard conditions
  191. typedef Playing_ pl; // makes transition table cleaner
  192. // Transition table for Playing
  193. struct transition_table : mpl::vector4<
  194. // Start Event Next Action Guard
  195. // +---------+-------------+---------+---------------------+----------------------+
  196. a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
  197. a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >,
  198. a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
  199. a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >
  200. // +---------+-------------+---------+---------------------+----------------------+
  201. > {};
  202. // Replaces the default no-transition response.
  203. template <class FSM,class Event>
  204. void no_transition(Event const& e, FSM&,int state)
  205. {
  206. std::cout << "no transition from state " << state
  207. << " on event " << typeid(e).name() << std::endl;
  208. }
  209. };
  210. typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing;
  211. struct Paused : public msm::front::state<my_visitable_state>
  212. {
  213. template <class Event,class FSM>
  214. void on_entry(Event const&,FSM& ) {std::cout << "entering: Paused" << std::endl;}
  215. template <class Event,class FSM>
  216. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Paused" << std::endl;}
  217. };
  218. struct AllOk : public msm::front::state<my_visitable_state>
  219. {
  220. template <class Event,class FSM>
  221. void on_entry(Event const&,FSM& ) {std::cout << "starting: AllOk" << std::endl;}
  222. template <class Event,class FSM>
  223. void on_exit(Event const&,FSM& ) {std::cout << "finishing: AllOk" << std::endl;}
  224. };
  225. struct ErrorMode :
  226. public msm::front::interrupt_state<end_error,my_visitable_state>
  227. {
  228. template <class Event,class FSM>
  229. void on_entry(Event const&,FSM& ) {std::cout << "starting: ErrorMode" << std::endl;}
  230. template <class Event,class FSM>
  231. void on_exit(Event const&,FSM& ) {std::cout << "finishing: ErrorMode" << std::endl;}
  232. void accept(SomeVisitor& vis,int i) const
  233. {
  234. vis.visit_state(this,i);
  235. }
  236. };
  237. // the initial state of the player SM. Must be defined
  238. typedef mpl::vector<Empty,AllOk> initial_state;
  239. // transition actions
  240. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  241. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  242. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  243. void store_cd_info(cd_detected const&)
  244. {
  245. std::cout << "player::store_cd_info\n";
  246. }
  247. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  248. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  249. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  250. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  251. void stopped_again(stop const&) {std::cout << "player::stopped_again\n";}
  252. void report_error(error_found const&) {std::cout << "player::report_error\n";}
  253. void report_end_error(end_error const&) {std::cout << "player::report_end_error\n";}
  254. // guard conditions
  255. typedef player_ p; // makes transition table cleaner
  256. // Transition table for player
  257. struct transition_table : mpl::vector<
  258. // Start Event Next Action Guard
  259. // +---------+-------------+---------+---------------------+----------------------+
  260. a_row < Stopped , play , Playing , &p::start_playback >,
  261. a_row < Stopped , open_close , Open , &p::open_drawer >,
  262. a_row < Stopped , stop , Stopped , &p::stopped_again >,
  263. // +---------+-------------+---------+---------------------+----------------------+
  264. a_row < Open , open_close , Empty , &p::close_drawer >,
  265. // +---------+-------------+---------+---------------------+----------------------+
  266. a_row < Empty , open_close , Open , &p::open_drawer >,
  267. a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
  268. // +---------+-------------+---------+---------------------+----------------------+
  269. a_row < Playing , stop , Stopped , &p::stop_playback >,
  270. a_row < Playing , pause , Paused , &p::pause_playback >,
  271. a_row < Playing , open_close , Open , &p::stop_and_open >,
  272. // +---------+-------------+---------+---------------------+----------------------+
  273. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  274. a_row < Paused , stop , Stopped , &p::stop_playback >,
  275. a_row < Paused , open_close , Open , &p::stop_and_open >,
  276. // +---------+-------------+---------+---------------------+----------------------+
  277. a_row < AllOk , error_found ,ErrorMode, &p::report_error >,
  278. a_row <ErrorMode,end_error ,AllOk , &p::report_end_error >
  279. // +---------+-------------+---------+---------------------+----------------------+
  280. > {};
  281. // Replaces the default no-transition response.
  282. template <class FSM,class Event>
  283. void no_transition(Event const& e, FSM&,int state)
  284. {
  285. std::cout << "no transition from state " << state
  286. << " on event " << typeid(e).name() << std::endl;
  287. }
  288. };
  289. // back-end
  290. typedef msm::back::state_machine<player_> player;
  291. //
  292. // Testing utilities.
  293. //
  294. void pstate(player const& p)
  295. {
  296. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode","SleepMode" };
  297. for (unsigned int i=0;i<player::nr_regions::value;++i)
  298. {
  299. std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
  300. }
  301. }
  302. void test()
  303. {
  304. player p;
  305. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  306. p.start();
  307. // test deferred event
  308. // deferred in Empty and Open, will be handled only after event cd_detected
  309. p.process_event(play());
  310. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  311. p.process_event(open_close()); pstate(p);
  312. // visiting Paused and AllOk, but only Paused cares
  313. SomeVisitor vis;
  314. p.visit_current_states(boost::ref(vis),1);
  315. p.process_event(open_close()); pstate(p);
  316. // visiting Empty and AllOk, but only Empty cares
  317. p.visit_current_states(boost::ref(vis),2);
  318. p.process_event(cd_detected());
  319. // no need to call play() as the previous event does it in its action method
  320. //p.process_event(play());
  321. // at this point, Play is active, along FirstSong and LightOn
  322. pstate(p);
  323. // visiting Playing+Song1+LightOn and AllOk, but only Playing+Song1+LightOn care
  324. p.visit_current_states(boost::ref(vis),3);
  325. // Stop will be active
  326. p.process_event(stop()); pstate(p);
  327. // visiting when both regions have an active state who wants to be visited
  328. p.process_event(error_found());
  329. p.visit_current_states(boost::ref(vis),5);
  330. }
  331. }
  332. int main()
  333. {
  334. test();
  335. return 0;
  336. }