SimpleWithFunctors2.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 <vector>
  11. #include <iostream>
  12. // back-end
  13. #include <boost/msm/back/state_machine.hpp>
  14. //front-end
  15. #include <boost/msm/front/state_machine_def.hpp>
  16. // functors
  17. #include <boost/msm/front/functor_row.hpp>
  18. #include <boost/msm/front/euml/common.hpp>
  19. // for And_ operator
  20. #include <boost/msm/front/euml/operator.hpp>
  21. // for func_state and func_state_machine
  22. #include <boost/msm/front/euml/state_grammar.hpp>
  23. using namespace std;
  24. namespace msm = boost::msm;
  25. namespace mpl = boost::mpl;
  26. using namespace msm::front;
  27. // for And_ operator
  28. using namespace msm::front::euml;
  29. namespace // Concrete FSM implementation
  30. {
  31. // events
  32. struct play {};
  33. struct end_pause {};
  34. struct stop {};
  35. struct pause {};
  36. struct open_close {};
  37. // A "complicated" event type that carries some data.
  38. enum DiskTypeEnum
  39. {
  40. DISK_CD=0,
  41. DISK_DVD=1
  42. };
  43. struct cd_detected
  44. {
  45. cd_detected(std::string name, DiskTypeEnum diskType)
  46. : name(name),
  47. disc_type(diskType)
  48. {}
  49. std::string name;
  50. DiskTypeEnum disc_type;
  51. };
  52. // front-end: define the FSM structure
  53. struct player_ : public msm::front::state_machine_def<player_>
  54. {
  55. // The list of FSM states
  56. // entry and exit functors for Empty
  57. struct Empty_Entry
  58. {
  59. template <class Event,class FSM,class STATE>
  60. void operator()(Event const&,FSM&,STATE& )
  61. {
  62. std::cout << "entering: Empty" << std::endl;
  63. }
  64. };
  65. struct Empty_Exit
  66. {
  67. template <class Event,class FSM,class STATE>
  68. void operator()(Event const&,FSM&,STATE& )
  69. {
  70. std::cout << "leaving: Empty" << std::endl;
  71. }
  72. };
  73. // definition of Empty
  74. struct Empty_tag {};
  75. typedef msm::front::euml::func_state<Empty_tag,Empty_Entry,Empty_Exit> Empty;
  76. struct Open_Entry
  77. {
  78. template <class Event,class FSM,class STATE>
  79. void operator()(Event const&,FSM&,STATE& )
  80. {
  81. std::cout << "entering: Open" << std::endl;
  82. }
  83. };
  84. struct Open_Exit
  85. {
  86. template <class Event,class FSM,class STATE>
  87. void operator()(Event const&,FSM&,STATE& )
  88. {
  89. std::cout << "leaving: Open" << std::endl;
  90. }
  91. };
  92. struct Open_tag {};
  93. typedef msm::front::euml::func_state<Open_tag,Open_Entry,Open_Exit> Open;
  94. // states without entry/exit actions (can be declared as functor state, just without functors ;-) )
  95. struct Stopped_tag {};
  96. typedef msm::front::euml::func_state<Stopped_tag> Stopped;
  97. struct Playing_tag {};
  98. typedef msm::front::euml::func_state<Playing_tag> Playing;
  99. // state not defining any entry or exit (declared as simple state. Equivalent)
  100. struct Paused : public msm::front::state<>
  101. {
  102. };
  103. // the initial state of the player SM. Must be defined
  104. typedef Empty initial_state;
  105. // transition actions
  106. // as the functors are generic on events, fsm and source/target state,
  107. // you can reuse them in another machine if you wish
  108. struct TestFct
  109. {
  110. template <class EVT,class FSM,class SourceState,class TargetState>
  111. void operator()(EVT const&, FSM&,SourceState& ,TargetState& )
  112. {
  113. cout << "transition with event:" << typeid(EVT).name() << endl;
  114. }
  115. };
  116. struct start_playback
  117. {
  118. template <class EVT,class FSM,class SourceState,class TargetState>
  119. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  120. {
  121. cout << "player::start_playback" << endl;
  122. }
  123. };
  124. struct open_drawer
  125. {
  126. template <class EVT,class FSM,class SourceState,class TargetState>
  127. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  128. {
  129. cout << "player::open_drawer" << endl;
  130. }
  131. };
  132. struct close_drawer
  133. {
  134. template <class EVT,class FSM,class SourceState,class TargetState>
  135. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  136. {
  137. cout << "player::close_drawer" << endl;
  138. }
  139. };
  140. struct store_cd_info
  141. {
  142. template <class EVT,class FSM,class SourceState,class TargetState>
  143. void operator()(EVT const&,FSM& fsm ,SourceState& ,TargetState& )
  144. {
  145. cout << "player::store_cd_info" << endl;
  146. fsm.process_event(play());
  147. }
  148. };
  149. struct stop_playback
  150. {
  151. template <class EVT,class FSM,class SourceState,class TargetState>
  152. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  153. {
  154. cout << "player::stop_playback" << endl;
  155. }
  156. };
  157. struct pause_playback
  158. {
  159. template <class EVT,class FSM,class SourceState,class TargetState>
  160. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  161. {
  162. cout << "player::pause_playback" << endl;
  163. }
  164. };
  165. struct resume_playback
  166. {
  167. template <class EVT,class FSM,class SourceState,class TargetState>
  168. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  169. {
  170. cout << "player::resume_playback" << endl;
  171. }
  172. };
  173. struct stop_and_open
  174. {
  175. template <class EVT,class FSM,class SourceState,class TargetState>
  176. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  177. {
  178. cout << "player::stop_and_open" << endl;
  179. }
  180. };
  181. struct stopped_again
  182. {
  183. template <class EVT,class FSM,class SourceState,class TargetState>
  184. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  185. {
  186. cout << "player::stopped_again" << endl;
  187. }
  188. };
  189. // guard conditions
  190. struct DummyGuard
  191. {
  192. template <class EVT,class FSM,class SourceState,class TargetState>
  193. bool operator()(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt)
  194. {
  195. return true;
  196. }
  197. };
  198. struct good_disk_format
  199. {
  200. template <class EVT,class FSM,class SourceState,class TargetState>
  201. bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& )
  202. {
  203. // to test a guard condition, let's say we understand only CDs, not DVD
  204. if (evt.disc_type != DISK_CD)
  205. {
  206. std::cout << "wrong disk, sorry" << std::endl;
  207. return false;
  208. }
  209. return true;
  210. }
  211. };
  212. struct always_true
  213. {
  214. template <class EVT,class FSM,class SourceState,class TargetState>
  215. bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& )
  216. {
  217. return true;
  218. }
  219. };
  220. // we want to define one row with the classic look.
  221. bool auto_start(cd_detected const& evt)
  222. {
  223. return false;
  224. }
  225. typedef player_ p; // makes transition table cleaner
  226. // Transition table for player
  227. struct transition_table : mpl::vector<
  228. // Start Event Next Action Guard
  229. // +---------+-------------+---------+---------------------------+----------------------+
  230. Row < Stopped , play , Playing , ActionSequence_
  231. <mpl::vector<
  232. TestFct,start_playback> >
  233. , DummyGuard >,
  234. Row < Stopped , open_close , Open , open_drawer , none >,
  235. Row < Stopped , stop , Stopped , none , none >,
  236. // +---------+-------------+---------+---------------------------+----------------------+
  237. Row < Open , open_close , Empty , close_drawer , none >,
  238. // +---------+-------------+---------+---------------------------+----------------------+
  239. Row < Empty , open_close , Open , open_drawer , none >,
  240. Row < Empty , cd_detected , Stopped , store_cd_info , And_<good_disk_format,
  241. always_true> >,
  242. // we here also mix with some "classical row"
  243. g_row < Empty , cd_detected , Playing , &p::auto_start >,
  244. // +---------+-------------+---------+---------------------------+----------------------+
  245. Row < Playing , stop , Stopped , stop_playback , none >,
  246. Row < Playing , pause , Paused , pause_playback , none >,
  247. Row < Playing , open_close , Open , stop_and_open , none >,
  248. // +---------+-------------+---------+---------------------------+----------------------+
  249. Row < Paused , end_pause , Playing , resume_playback , none >,
  250. Row < Paused , stop , Stopped , stop_playback , none >,
  251. Row < Paused , open_close , Open , stop_and_open , none >
  252. // +---------+-------------+---------+---------------------------+----------------------+
  253. > {};
  254. // Replaces the default no-transition response.
  255. template <class FSM,class Event>
  256. void no_transition(Event const& e, FSM&,int state)
  257. {
  258. std::cout << "no transition from state " << state
  259. << " on event " << typeid(e).name() << std::endl;
  260. }
  261. };
  262. // Pick a back-end
  263. typedef msm::back::state_machine<player_> player;
  264. //
  265. // Testing utilities.
  266. //
  267. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  268. void pstate(player const& p)
  269. {
  270. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  271. }
  272. void test()
  273. {
  274. player p;
  275. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  276. p.start();
  277. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  278. p.process_event(open_close()); pstate(p);
  279. p.process_event(open_close()); pstate(p);
  280. // will be rejected, wrong disk type
  281. p.process_event(
  282. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  283. p.process_event(
  284. cd_detected("louie, louie",DISK_CD)); pstate(p);
  285. // no need to call play() as the previous event does it in its action method
  286. //p.process_event(play());
  287. // at this point, Play is active
  288. p.process_event(pause()); pstate(p);
  289. // go back to Playing
  290. p.process_event(end_pause()); pstate(p);
  291. p.process_event(pause()); pstate(p);
  292. p.process_event(stop()); pstate(p);
  293. // event leading to the same state
  294. // no action method called as it is not present in the transition table
  295. p.process_event(stop()); pstate(p);
  296. }
  297. }
  298. int main()
  299. {
  300. test();
  301. return 0;
  302. }