SimpleWithFunctors.cpp 12 KB

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