SimpleWithFunctors3.cpp 11 KB

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