SimpleMachine.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. #ifndef BOOST_MSM_NONSTANDALONE_TEST
  16. #define BOOST_TEST_MODULE MyTest
  17. #endif
  18. #include <boost/test/unit_test.hpp>
  19. namespace msm = boost::msm;
  20. namespace mpl = boost::mpl;
  21. namespace
  22. {
  23. // events
  24. struct play {};
  25. struct end_pause {};
  26. struct stop {};
  27. struct pause {};
  28. struct open_close {};
  29. // A "complicated" event type that carries some data.
  30. enum DiskTypeEnum
  31. {
  32. DISK_CD=0,
  33. DISK_DVD=1
  34. };
  35. struct cd_detected
  36. {
  37. cd_detected(std::string name, DiskTypeEnum diskType)
  38. : name(name),
  39. disc_type(diskType)
  40. {}
  41. std::string name;
  42. DiskTypeEnum disc_type;
  43. };
  44. // front-end: define the FSM structure
  45. struct player_ : public msm::front::state_machine_def<player_>
  46. {
  47. unsigned int start_playback_counter;
  48. unsigned int can_close_drawer_counter;
  49. player_():
  50. start_playback_counter(0),
  51. can_close_drawer_counter(0)
  52. {}
  53. // The list of FSM states
  54. struct Empty : public msm::front::state<>
  55. {
  56. template <class Event,class FSM>
  57. void on_entry(Event const&,FSM& ) {++entry_counter;}
  58. template <class Event,class FSM>
  59. void on_exit(Event const&,FSM& ) {++exit_counter;}
  60. int entry_counter;
  61. int exit_counter;
  62. };
  63. struct Open : public msm::front::state<>
  64. {
  65. template <class Event,class FSM>
  66. void on_entry(Event const&,FSM& ) {++entry_counter;}
  67. template <class Event,class FSM>
  68. void on_exit(Event const&,FSM& ) {++exit_counter;}
  69. int entry_counter;
  70. int exit_counter;
  71. };
  72. // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
  73. struct Stopped : public msm::front::state<>
  74. {
  75. template <class Event,class FSM>
  76. void on_entry(Event const&,FSM& ) {++entry_counter;}
  77. template <class Event,class FSM>
  78. void on_exit(Event const&,FSM& ) {++exit_counter;}
  79. int entry_counter;
  80. int exit_counter;
  81. };
  82. struct Playing : public msm::front::state<>
  83. {
  84. template <class Event,class FSM>
  85. void on_entry(Event const&,FSM& ) {++entry_counter;}
  86. template <class Event,class FSM>
  87. void on_exit(Event const&,FSM& ) {++exit_counter;}
  88. int entry_counter;
  89. int exit_counter;
  90. };
  91. // state not defining any entry or exit
  92. struct Paused : public msm::front::state<>
  93. {
  94. template <class Event,class FSM>
  95. void on_entry(Event const&,FSM& ) {++entry_counter;}
  96. template <class Event,class FSM>
  97. void on_exit(Event const&,FSM& ) {++exit_counter;}
  98. int entry_counter;
  99. int exit_counter;
  100. };
  101. // the initial state of the player SM. Must be defined
  102. typedef Empty initial_state;
  103. // transition actions
  104. void start_playback(play const&) {++start_playback_counter; }
  105. void open_drawer(open_close const&) { }
  106. void store_cd_info(cd_detected const&) { }
  107. void stop_playback(stop const&) { }
  108. void pause_playback(pause const&) { }
  109. void resume_playback(end_pause const&) { }
  110. void stop_and_open(open_close const&) { }
  111. void stopped_again(stop const&){}
  112. // guard conditions
  113. bool good_disk_format(cd_detected const& evt)
  114. {
  115. // to test a guard condition, let's say we understand only CDs, not DVD
  116. if (evt.disc_type != DISK_CD)
  117. {
  118. return false;
  119. }
  120. return true;
  121. }
  122. bool can_close_drawer(open_close const&)
  123. {
  124. ++can_close_drawer_counter;
  125. return true;
  126. }
  127. typedef player_ p; // makes transition table cleaner
  128. // Transition table for player
  129. struct transition_table : mpl::vector<
  130. // Start Event Next Action Guard
  131. // +---------+-------------+---------+---------------------+----------------------+
  132. a_row < Stopped , play , Playing , &p::start_playback >,
  133. a_row < Stopped , open_close , Open , &p::open_drawer >,
  134. _row < Stopped , stop , Stopped >,
  135. // +---------+-------------+---------+---------------------+----------------------+
  136. g_row < Open , open_close , Empty , &p::can_close_drawer >,
  137. // +---------+-------------+---------+---------------------+----------------------+
  138. a_row < Empty , open_close , Open , &p::open_drawer >,
  139. row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
  140. // +---------+-------------+---------+---------------------+----------------------+
  141. a_row < Playing , stop , Stopped , &p::stop_playback >,
  142. a_row < Playing , pause , Paused , &p::pause_playback >,
  143. a_row < Playing , open_close , Open , &p::stop_and_open >,
  144. // +---------+-------------+---------+---------------------+----------------------+
  145. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  146. a_row < Paused , stop , Stopped , &p::stop_playback >,
  147. a_row < Paused , open_close , Open , &p::stop_and_open >
  148. // +---------+-------------+---------+---------------------+----------------------+
  149. > {};
  150. // Replaces the default no-transition response.
  151. template <class FSM,class Event>
  152. void no_transition(Event const& , FSM&,int)
  153. {
  154. BOOST_FAIL("no_transition called!");
  155. }
  156. // init counters
  157. template <class Event,class FSM>
  158. void on_entry(Event const&,FSM& fsm)
  159. {
  160. fsm.template get_state<player_::Stopped&>().entry_counter=0;
  161. fsm.template get_state<player_::Stopped&>().exit_counter=0;
  162. fsm.template get_state<player_::Open&>().entry_counter=0;
  163. fsm.template get_state<player_::Open&>().exit_counter=0;
  164. fsm.template get_state<player_::Empty&>().entry_counter=0;
  165. fsm.template get_state<player_::Empty&>().exit_counter=0;
  166. fsm.template get_state<player_::Playing&>().entry_counter=0;
  167. fsm.template get_state<player_::Playing&>().exit_counter=0;
  168. fsm.template get_state<player_::Paused&>().entry_counter=0;
  169. fsm.template get_state<player_::Paused&>().exit_counter=0;
  170. }
  171. };
  172. // Pick a back-end
  173. typedef msm::back::state_machine<player_> player;
  174. // static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  175. BOOST_AUTO_TEST_CASE( my_test )
  176. {
  177. player p;
  178. p.start();
  179. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly");
  180. p.process_event(open_close());
  181. BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active"); //Open
  182. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly");
  183. BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly");
  184. p.process_event(open_close());
  185. BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active"); //Empty
  186. BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly");
  187. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly");
  188. BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly");
  189. p.process_event(
  190. cd_detected("louie, louie",DISK_DVD));
  191. BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active"); //Empty
  192. BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly");
  193. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly");
  194. p.process_event(
  195. cd_detected("louie, louie",DISK_CD));
  196. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
  197. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly");
  198. BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly");
  199. p.process_event(play());
  200. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
  201. BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly");
  202. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly");
  203. BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly");
  204. p.process_event(pause());
  205. BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
  206. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly");
  207. BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly");
  208. // go back to Playing
  209. p.process_event(end_pause());
  210. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
  211. BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 1,"Paused exit not called correctly");
  212. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 2,"Playing entry not called correctly");
  213. p.process_event(pause());
  214. BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
  215. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly");
  216. BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 2,"Paused entry not called correctly");
  217. p.process_event(stop());
  218. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
  219. BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 2,"Paused exit not called correctly");
  220. BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 2,"Stopped entry not called correctly");
  221. p.process_event(stop());
  222. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
  223. BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 2,"Stopped exit not called correctly");
  224. BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly");
  225. }
  226. }