SimpleTutorialInternal2.cpp 9.7 KB

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