SimpleInternal.cpp 13 KB

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