SimpleTutorialInternal.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include <boost/msm/front/functor_row.hpp>
  16. namespace msm = boost::msm;
  17. namespace mpl = boost::mpl;
  18. using namespace msm::front;
  19. namespace
  20. {
  21. // events
  22. struct play {};
  23. struct end_pause {};
  24. struct stop {};
  25. struct pause {};
  26. struct open_close {};
  27. struct to_ignore {};
  28. // A "complicated" event type that carries some data.
  29. enum DiskTypeEnum
  30. {
  31. DISK_CD=0,
  32. DISK_DVD=1
  33. };
  34. struct cd_detected
  35. {
  36. cd_detected(std::string name, DiskTypeEnum diskType)
  37. : name(name),
  38. disc_type(diskType)
  39. {}
  40. std::string name;
  41. DiskTypeEnum disc_type;
  42. };
  43. // front-end: define the FSM structure
  44. struct player_ : public msm::front::state_machine_def<player_>
  45. {
  46. // The list of FSM states
  47. struct Empty : public msm::front::state<>
  48. {
  49. // every (optional) entry/exit methods get the event passed.
  50. template <class Event,class FSM>
  51. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  52. template <class Event,class FSM>
  53. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  54. };
  55. struct Open : public msm::front::state<>
  56. {
  57. template <class Event,class FSM>
  58. void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
  59. template <class Event,class FSM>
  60. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  61. };
  62. // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
  63. struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr>
  64. {
  65. template <class Event,class FSM>
  66. void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
  67. template <class Event,class FSM>
  68. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  69. void set_sm_ptr(player_* pl)
  70. {
  71. m_player=pl;
  72. }
  73. player_* m_player;
  74. };
  75. struct Playing : public msm::front::state<>
  76. {
  77. template <class Event,class FSM>
  78. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  79. template <class Event,class FSM>
  80. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  81. };
  82. // state not defining any entry or exit
  83. struct Paused : public msm::front::state<>
  84. {
  85. };
  86. // the initial state of the player SM. Must be defined
  87. typedef Empty initial_state;
  88. // transition actions
  89. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  90. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  91. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  92. void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
  93. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  94. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  95. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  96. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  97. void stopped_again(stop const&) {std::cout << "player::stopped_again\n";}
  98. // guard conditions
  99. bool good_disk_format(cd_detected const& evt)
  100. {
  101. // to test a guard condition, let's say we understand only CDs, not DVD
  102. if (evt.disc_type != DISK_CD)
  103. {
  104. std::cout << "wrong disk, sorry" << std::endl;
  105. return false;
  106. }
  107. return true;
  108. }
  109. // transitions internal to Empty
  110. void internal_action(cd_detected const&){ std::cout << "Empty::internal action\n"; }
  111. bool internal_guard(cd_detected const&)
  112. {
  113. std::cout << "Empty::internal guard\n";
  114. return false;
  115. }
  116. struct internal_guard_fct
  117. {
  118. template <class EVT,class FSM,class SourceState,class TargetState>
  119. bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& )
  120. {
  121. std::cout << "Empty::internal guard functor\n";
  122. return false;
  123. }
  124. };
  125. struct internal_action_fct
  126. {
  127. template <class EVT,class FSM,class SourceState,class TargetState>
  128. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  129. {
  130. std::cout << "Empty::internal action functor" << std::endl;
  131. }
  132. };
  133. typedef player_ p; // makes transition table cleaner
  134. // Transition table for player
  135. struct transition_table : mpl::vector<
  136. // Start Event Next Action Guard
  137. // +---------+-------------+---------+---------------------+----------------------+
  138. a_row < Stopped , play , Playing , &p::start_playback >,
  139. a_row < Stopped , open_close , Open , &p::open_drawer >,
  140. _row < Stopped , stop , Stopped >,
  141. // +---------+-------------+---------+---------------------+----------------------+
  142. a_row < Open , open_close , Empty , &p::close_drawer >,
  143. // +---------+-------------+---------+---------------------+----------------------+
  144. a_row < Empty , open_close , Open , &p::open_drawer >,
  145. row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
  146. irow < Empty , cd_detected , &p::internal_action ,&p::internal_guard >,
  147. _irow < Empty , to_ignore >,
  148. g_irow < Empty , cd_detected ,&p::internal_guard >,
  149. Row < Empty , cd_detected , none , internal_action_fct ,internal_guard_fct >,
  150. // +---------+-------------+---------+---------------------+----------------------+
  151. a_row < Playing , stop , Stopped , &p::stop_playback >,
  152. a_row < Playing , pause , Paused , &p::pause_playback >,
  153. a_row < Playing , open_close , Open , &p::stop_and_open >,
  154. // +---------+-------------+---------+---------------------+----------------------+
  155. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  156. a_row < Paused , stop , Stopped , &p::stop_playback >,
  157. a_row < Paused , open_close , Open , &p::stop_and_open >
  158. // +---------+-------------+---------+---------------------+----------------------+
  159. > {};
  160. // Replaces the default no-transition response.
  161. template <class FSM,class Event>
  162. void no_transition(Event const& e, FSM&,int state)
  163. {
  164. std::cout << "no transition from state " << state
  165. << " on event " << typeid(e).name() << std::endl;
  166. }
  167. };
  168. // Pick a back-end
  169. typedef msm::back::state_machine<player_> player;
  170. //
  171. // Testing utilities.
  172. //
  173. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  174. void pstate(player const& p)
  175. {
  176. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  177. }
  178. void test()
  179. {
  180. player p;
  181. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  182. p.start();
  183. // this event will be ignored and not call no_transition
  184. p.process_event(to_ignore());
  185. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  186. p.process_event(open_close()); pstate(p);
  187. p.process_event(open_close()); pstate(p);
  188. // will be rejected, wrong disk type
  189. p.process_event(
  190. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  191. p.process_event(
  192. cd_detected("louie, louie",DISK_CD)); pstate(p);
  193. p.process_event(play());
  194. // at this point, Play is active
  195. p.process_event(pause()); pstate(p);
  196. // go back to Playing
  197. p.process_event(end_pause()); pstate(p);
  198. p.process_event(pause()); pstate(p);
  199. p.process_event(stop()); pstate(p);
  200. // event leading to the same state
  201. // no action method called as it is not present in the transition table
  202. p.process_event(stop()); pstate(p);
  203. }
  204. }
  205. int main()
  206. {
  207. test();
  208. return 0;
  209. }