MsmSimpleFunctors.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #include <boost/msm/front/functor_row.hpp>
  13. namespace msm = boost::msm;
  14. namespace mpl = boost::mpl;
  15. using namespace msm::front;
  16. #include <iostream>
  17. #ifdef WIN32
  18. #include "windows.h"
  19. #else
  20. #include <sys/time.h>
  21. #endif
  22. namespace test_fsm // Concrete FSM implementation
  23. {
  24. // events
  25. struct play {};
  26. struct end_pause {};
  27. struct stop {};
  28. struct pause {};
  29. struct open_close {};
  30. struct cd_detected{};
  31. // Concrete FSM implementation
  32. struct player_ : public msm::front::state_machine_def<player_>
  33. {
  34. // no need for exception handling or message queue
  35. typedef int no_exception_thrown;
  36. typedef int no_message_queue;
  37. // The list of FSM states
  38. struct Empty : public msm::front::state<>
  39. {
  40. // optional entry/exit methods
  41. template <class Event,class FSM>
  42. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Empty" << std::endl;*/}
  43. template <class Event,class FSM>
  44. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Empty" << std::endl;*/}
  45. };
  46. struct Open : public msm::front::state<>
  47. {
  48. template <class Event,class FSM>
  49. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Open" << std::endl;*/}
  50. template <class Event,class FSM>
  51. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Open" << std::endl;*/}
  52. };
  53. struct Stopped : public msm::front::state<>
  54. {
  55. // when stopped, the CD is loaded
  56. template <class Event,class FSM>
  57. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Stopped" << std::endl;*/}
  58. template <class Event,class FSM>
  59. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Stopped" << std::endl;*/}
  60. };
  61. struct Playing : public msm::front::state<>
  62. {
  63. template <class Event,class FSM>
  64. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Playing" << std::endl;*/}
  65. template <class Event,class FSM>
  66. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Playing" << std::endl;*/}
  67. };
  68. // state not defining any entry or exit
  69. struct Paused : public msm::front::state<>
  70. {
  71. template <class Event,class FSM>
  72. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Paused" << std::endl;*/}
  73. template <class Event,class FSM>
  74. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Paused" << std::endl;*/}
  75. };
  76. // the initial state of the player SM. Must be defined
  77. typedef Empty initial_state;
  78. // transition actions
  79. struct start_playback
  80. {
  81. template <class FSM,class EVT,class SourceState,class TargetState>
  82. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  83. {
  84. }
  85. };
  86. struct open_drawer
  87. {
  88. template <class FSM,class EVT,class SourceState,class TargetState>
  89. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  90. {
  91. }
  92. };
  93. struct close_drawer
  94. {
  95. template <class FSM,class EVT,class SourceState,class TargetState>
  96. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  97. {
  98. }
  99. };
  100. struct store_cd_info
  101. {
  102. template <class FSM,class EVT,class SourceState,class TargetState>
  103. void operator()(EVT const&, FSM& fsm ,SourceState& ,TargetState& )
  104. {
  105. }
  106. };
  107. struct stop_playback
  108. {
  109. template <class FSM,class EVT,class SourceState,class TargetState>
  110. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  111. {
  112. }
  113. };
  114. struct pause_playback
  115. {
  116. template <class FSM,class EVT,class SourceState,class TargetState>
  117. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  118. {
  119. }
  120. };
  121. struct resume_playback
  122. {
  123. template <class FSM,class EVT,class SourceState,class TargetState>
  124. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  125. {
  126. }
  127. };
  128. struct stop_and_open
  129. {
  130. template <class FSM,class EVT,class SourceState,class TargetState>
  131. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  132. {
  133. }
  134. };
  135. struct stopped_again
  136. {
  137. template <class FSM,class EVT,class SourceState,class TargetState>
  138. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  139. {
  140. }
  141. };
  142. // guard conditions
  143. // Transition table for player
  144. struct transition_table : mpl::vector<
  145. // Start Event Next Action Guard
  146. // +---------+-------------+---------+---------------------+----------------------+
  147. Row < Stopped , play , Playing , start_playback >,
  148. Row < Stopped , open_close , Open , open_drawer >,
  149. Row < Stopped , stop , Stopped , stopped_again >,
  150. // +---------+-------------+---------+---------------------+----------------------+
  151. Row < Open , open_close , Empty , close_drawer >,
  152. // +---------+-------------+---------+---------------------+----------------------+
  153. Row < Empty , open_close , Open , open_drawer >,
  154. Row < Empty , cd_detected , Stopped , store_cd_info >,
  155. // +---------+-------------+---------+---------------------+----------------------+
  156. Row < Playing , stop , Stopped , stop_playback >,
  157. Row < Playing , pause , Paused , pause_playback >,
  158. Row < Playing , open_close , Open , stop_and_open >,
  159. // +---------+-------------+---------+---------------------+----------------------+
  160. Row < Paused , end_pause , Playing , resume_playback >,
  161. Row < Paused , stop , Stopped , stop_playback >,
  162. Row < Paused , open_close , Open , stop_and_open >
  163. // +---------+-------------+---------+---------------------+----------------------+
  164. > {};
  165. // Replaces the default no-transition response.
  166. template <class FSM,class Event>
  167. void no_transition(Event const& e, FSM&,int state)
  168. {
  169. std::cout << "no transition from state " << state
  170. << " on event " << typeid(e).name() << std::endl;
  171. }
  172. };
  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. }
  183. #ifndef WIN32
  184. long mtime(struct timeval& tv1,struct timeval& tv2)
  185. {
  186. return (tv2.tv_sec-tv1.tv_sec) *1000000 + ((tv2.tv_usec-tv1.tv_usec));
  187. }
  188. #endif
  189. int main()
  190. {
  191. // for timing
  192. #ifdef WIN32
  193. LARGE_INTEGER res;
  194. ::QueryPerformanceFrequency(&res);
  195. LARGE_INTEGER li,li2;
  196. #else
  197. struct timeval tv1,tv2;
  198. gettimeofday(&tv1,NULL);
  199. #endif
  200. test_fsm::player p2;
  201. p2.start();
  202. // for timing
  203. #ifdef WIN32
  204. ::QueryPerformanceCounter(&li);
  205. #else
  206. gettimeofday(&tv1,NULL);
  207. #endif
  208. for (int i=0;i<100;++i)
  209. {
  210. p2.process_event(test_fsm::open_close());
  211. p2.process_event(test_fsm::open_close());
  212. p2.process_event(test_fsm::cd_detected());
  213. p2.process_event(test_fsm::play());
  214. p2.process_event(test_fsm::pause());
  215. // go back to Playing
  216. p2.process_event(test_fsm::end_pause());
  217. p2.process_event(test_fsm::pause());
  218. p2.process_event(test_fsm::stop());
  219. // event leading to the same state
  220. p2.process_event(test_fsm::stop());
  221. p2.process_event(test_fsm::open_close());
  222. p2.process_event(test_fsm::open_close());
  223. }
  224. #ifdef WIN32
  225. ::QueryPerformanceCounter(&li2);
  226. #else
  227. gettimeofday(&tv2,NULL);
  228. #endif
  229. #ifdef WIN32
  230. std::cout << "msm took in s:" << (double)(li2.QuadPart-li.QuadPart)/res.QuadPart <<"\n" <<std::endl;
  231. #else
  232. std::cout << "msm took in us:" << mtime(tv1,tv2) <<"\n" <<std::endl;
  233. #endif
  234. return 0;
  235. }