MsmSimple.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 <boost/msm/back/state_machine.hpp>
  11. #include <boost/msm/front/state_machine_def.hpp>
  12. namespace msm = boost::msm;
  13. namespace mpl = boost::mpl;
  14. #include <iostream>
  15. #ifdef WIN32
  16. #include "windows.h"
  17. #else
  18. #include <sys/time.h>
  19. #endif
  20. namespace test_fsm // Concrete FSM implementation
  21. {
  22. // events
  23. struct play {};
  24. struct end_pause {};
  25. struct stop {};
  26. struct pause {};
  27. struct open_close {};
  28. struct cd_detected{};
  29. // Concrete FSM implementation
  30. struct player_ : public msm::front::state_machine_def<player_>
  31. {
  32. // no need for exception handling or message queue
  33. typedef int no_exception_thrown;
  34. typedef int no_message_queue;
  35. // The list of FSM states
  36. struct Empty : public msm::front::state<>
  37. {
  38. // optional entry/exit methods
  39. template <class Event,class FSM>
  40. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Empty" << std::endl;*/}
  41. template <class Event,class FSM>
  42. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Empty" << std::endl;*/}
  43. };
  44. struct Open : public msm::front::state<>
  45. {
  46. template <class Event,class FSM>
  47. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Open" << std::endl;*/}
  48. template <class Event,class FSM>
  49. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Open" << std::endl;*/}
  50. };
  51. struct Stopped : public msm::front::state<>
  52. {
  53. template <class Event,class FSM>
  54. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Stopped" << std::endl;*/}
  55. template <class Event,class FSM>
  56. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Stopped" << std::endl;*/}
  57. };
  58. struct Playing : public msm::front::state<>
  59. {
  60. template <class Event,class FSM>
  61. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Playing" << std::endl;*/}
  62. template <class Event,class FSM>
  63. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Playing" << std::endl;*/}
  64. };
  65. struct Paused : public msm::front::state<>
  66. {
  67. template <class Event,class FSM>
  68. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Paused" << std::endl;*/}
  69. template <class Event,class FSM>
  70. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Paused" << std::endl;*/}
  71. };
  72. // the initial state of the player SM. Must be defined
  73. typedef Empty initial_state;
  74. // transition actions
  75. void start_playback(play const&) { }
  76. void open_drawer(open_close const&) { }
  77. void close_drawer(open_close const&) { }
  78. void store_cd_info(cd_detected const& cd) { }
  79. void stop_playback(stop const&) { }
  80. void pause_playback(pause const&) { }
  81. void resume_playback(end_pause const&) { }
  82. void stop_and_open(open_close const&) { }
  83. void stopped_again(stop const&){}
  84. // guard conditions
  85. typedef player_ p; // makes transition table cleaner
  86. // Transition table for player
  87. struct transition_table : mpl::vector<
  88. // Start Event Next Action Guard
  89. // +---------+-------------+---------+---------------------+----------------------+
  90. _row < Stopped , play , Playing >,
  91. _row < Stopped , open_close , Open >,
  92. _row < Stopped , stop , Stopped >,
  93. // +---------+-------------+---------+---------------------+----------------------+
  94. _row < Open , open_close , Empty >,
  95. // +---------+-------------+---------+---------------------+----------------------+
  96. _row < Empty , open_close , Open >,
  97. _row < Empty , cd_detected , Stopped >,
  98. // +---------+-------------+---------+---------------------+----------------------+
  99. _row < Playing , stop , Stopped >,
  100. _row < Playing , pause , Paused >,
  101. _row < Playing , open_close , Open >,
  102. // +---------+-------------+---------+---------------------+----------------------+
  103. _row < Paused , end_pause , Playing >,
  104. _row < Paused , stop , Stopped >,
  105. _row < Paused , open_close , Open >
  106. // +---------+-------------+---------+---------------------+----------------------+
  107. > {};
  108. // Replaces the default no-transition response.
  109. template <class FSM,class Event>
  110. void no_transition(Event const& e, FSM&,int state)
  111. {
  112. std::cout << "no transition from state " << state
  113. << " on event " << typeid(e).name() << std::endl;
  114. }
  115. };
  116. typedef msm::back::state_machine<player_> player;
  117. //
  118. // Testing utilities.
  119. //
  120. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  121. void pstate(player const& p)
  122. {
  123. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  124. }
  125. }
  126. #ifndef WIN32
  127. long mtime(struct timeval& tv1,struct timeval& tv2)
  128. {
  129. return (tv2.tv_sec-tv1.tv_sec) *1000000 + ((tv2.tv_usec-tv1.tv_usec));
  130. }
  131. #endif
  132. int main()
  133. {
  134. // for timing
  135. #ifdef WIN32
  136. LARGE_INTEGER res;
  137. ::QueryPerformanceFrequency(&res);
  138. LARGE_INTEGER li,li2;
  139. #else
  140. struct timeval tv1,tv2;
  141. gettimeofday(&tv1,NULL);
  142. #endif
  143. test_fsm::player p2;
  144. p2.start();
  145. // for timing
  146. #ifdef WIN32
  147. ::QueryPerformanceCounter(&li);
  148. #else
  149. gettimeofday(&tv1,NULL);
  150. #endif
  151. for (int i=0;i<100;++i)
  152. {
  153. p2.process_event(test_fsm::open_close());
  154. p2.process_event(test_fsm::open_close());
  155. p2.process_event(test_fsm::cd_detected());
  156. p2.process_event(test_fsm::play());
  157. p2.process_event(test_fsm::pause());
  158. // go back to Playing
  159. p2.process_event(test_fsm::end_pause());
  160. p2.process_event(test_fsm::pause());
  161. p2.process_event(test_fsm::stop());
  162. // event leading to the same state
  163. p2.process_event(test_fsm::stop());
  164. p2.process_event(test_fsm::open_close());
  165. p2.process_event(test_fsm::open_close());
  166. }
  167. #ifdef WIN32
  168. ::QueryPerformanceCounter(&li2);
  169. #else
  170. gettimeofday(&tv2,NULL);
  171. #endif
  172. #ifdef WIN32
  173. std::cout << "msm took in s:" << (double)(li2.QuadPart-li.QuadPart)/res.QuadPart <<"\n" <<std::endl;
  174. #else
  175. std::cout << "msm took in us:" << mtime(tv1,tv2) <<"\n" <<std::endl;
  176. #endif
  177. return 0;
  178. }