SimplePhoenix.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <vector>
  11. #include <iostream>
  12. #include <boost/phoenix/phoenix.hpp>
  13. // add phoenix support in eUML
  14. #define BOOST_MSM_EUML_PHOENIX_SUPPORT
  15. #include <boost/msm/back/state_machine.hpp>
  16. #include <boost/msm/front/euml/euml.hpp>
  17. using namespace std;
  18. using namespace boost::msm::front::euml;
  19. namespace msm = boost::msm;
  20. using namespace boost::phoenix;
  21. // entry/exit/action/guard logging functors
  22. #include "logging_functors.h"
  23. namespace // Concrete FSM implementation
  24. {
  25. // events
  26. BOOST_MSM_EUML_EVENT(end_pause)
  27. BOOST_MSM_EUML_EVENT(stop)
  28. BOOST_MSM_EUML_EVENT(pause)
  29. BOOST_MSM_EUML_EVENT(open_close)
  30. struct play_event : boost::msm::front::euml::euml_event<play_event>
  31. {
  32. };
  33. play_event play;
  34. enum DiskTypeEnum
  35. {
  36. DISK_CD=0,
  37. DISK_DVD=1
  38. };
  39. // A "complicated" event type that carries some data.
  40. struct cd_detected_event : boost::msm::front::euml::euml_event<cd_detected_event>
  41. {
  42. cd_detected_event(){}
  43. cd_detected_event(std::string const& name,DiskTypeEnum disk):cd_name(name),cd_type(disk){}
  44. std::string cd_name;
  45. DiskTypeEnum cd_type;
  46. };
  47. // define an instance for a nicer transition table
  48. cd_detected_event cd_detected;
  49. // Concrete FSM implementation
  50. // The list of FSM states
  51. // state not needing any entry or exit
  52. BOOST_MSM_EUML_STATE((),Paused)
  53. // states with standard eUML actions
  54. BOOST_MSM_EUML_STATE(( Stopped_Entry,Stopped_Exit ),Stopped)
  55. BOOST_MSM_EUML_STATE(( Playing_Entry,Playing_Exit ),Playing)
  56. // a "standard" msm state
  57. struct Empty_impl : public msm::front::state<> , public euml_state<Empty_impl>
  58. {
  59. // this allows us to add some functions
  60. void foo() {std::cout << "Empty::foo " << std::endl;}
  61. // standard entry behavior
  62. template <class Event,class FSM>
  63. void on_entry(Event const& evt,FSM& fsm)
  64. {
  65. std::cout << "entering: Empty" << std::endl;
  66. }
  67. template <class Event,class FSM>
  68. void on_exit(Event const& evt,FSM& fsm)
  69. {
  70. std::cout << "leaving: Empty" << std::endl;
  71. }
  72. };
  73. //instance for use in the transition table
  74. Empty_impl const Empty;
  75. // entry and exit actions as phoenix functions
  76. struct open_entry_impl
  77. {
  78. typedef void result_type;
  79. void operator()()
  80. {
  81. cout << "entering: Open" << endl;
  82. }
  83. };
  84. boost::phoenix::function<open_entry_impl> open_entry;
  85. struct open_exit_impl
  86. {
  87. typedef void result_type;
  88. void operator()()
  89. {
  90. cout << "leaving: Open" << endl;
  91. }
  92. };
  93. boost::phoenix::function<open_exit_impl> open_exit;
  94. // a state using phoenix for entry/exit actions
  95. BOOST_MSM_EUML_STATE(( open_entry(),open_exit() ),Open)
  96. // actions and guards using boost::phoenix
  97. struct start_playback_impl
  98. {
  99. typedef void result_type;
  100. void operator()()
  101. {
  102. cout << "calling: start_playback" << endl;
  103. }
  104. };
  105. boost::phoenix::function<start_playback_impl> start_playback;
  106. // a guard taking the event as argument
  107. struct good_disk_format_impl
  108. {
  109. typedef bool result_type;
  110. template <class Event>
  111. bool operator()(Event const& evt)
  112. {
  113. // to test a guard condition, let's say we understand only CDs, not DVD
  114. if (evt.cd_type!=DISK_CD)
  115. {
  116. std::cout << "wrong disk, sorry" << std::endl;
  117. return false;
  118. }
  119. std::cout << "good disk" << std::endl;
  120. return true;
  121. }
  122. };
  123. boost::phoenix::function<good_disk_format_impl> good_disk_format;
  124. // a simple action
  125. struct store_cd_info_impl
  126. {
  127. typedef void result_type;
  128. void operator()()
  129. {
  130. cout << "calling: store_cd_info" << endl;
  131. }
  132. };
  133. boost::phoenix::function<store_cd_info_impl> store_cd_info;
  134. // an action taking the fsm as argument and sending it a new event
  135. struct process_play_impl
  136. {
  137. typedef void result_type;
  138. template <class Fsm>
  139. void operator()(Fsm& fsm)
  140. {
  141. cout << "queuing a play event" << endl;
  142. fsm.process_event(play);
  143. }
  144. };
  145. // it is also possible to use BOOST_PHOENIX_ADAPT_CALLABLE to avoid defining a global variable
  146. BOOST_PHOENIX_ADAPT_CALLABLE(process_play, process_play_impl, 1)
  147. // transition table. Actions and guards are written as phoenix functions
  148. BOOST_MSM_EUML_TRANSITION_TABLE((
  149. //an action without arguments
  150. Playing == Stopped + play / start_playback() ,
  151. Playing == Paused + end_pause ,
  152. // +------------------------------------------------------------------------------+
  153. Empty == Open + open_close ,
  154. // +------------------------------------------------------------------------------+
  155. Open == Empty + open_close ,
  156. Open == Paused + open_close ,
  157. Open == Stopped + open_close ,
  158. Open == Playing + open_close ,
  159. // +------------------------------------------------------------------------------+
  160. Paused == Playing + pause ,
  161. // +------------------------------------------------------------------------------+
  162. Stopped == Playing + stop ,
  163. Stopped == Paused + stop ,
  164. // a guard taking the event as argument
  165. // and an action made of a phoenix expression of 2 actions
  166. // _event is a placeholder for the current event
  167. // _fsm is a placeholder for the current state machine
  168. Stopped == Empty + cd_detected [good_disk_format(_event)]
  169. / (store_cd_info(),process_play(_fsm)),
  170. Stopped == Stopped + stop
  171. // +------------------------------------------------------------------------------+
  172. ),transition_table)
  173. // create a state machine "on the fly"
  174. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  175. init_ << Empty, // Init State
  176. no_action, // Entry
  177. no_action, // Exit
  178. attributes_ << no_attributes_, // Attributes
  179. configure_ << no_configure_, // configuration
  180. Log_No_Transition // no_transition handler
  181. ),
  182. player_) //fsm name
  183. // or simply, if no no_transition handler needed:
  184. //BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  185. // Empty // Init State
  186. // ),player_)
  187. // choice of back-end
  188. typedef msm::back::state_machine<player_> player;
  189. //
  190. // Testing utilities.
  191. //
  192. static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
  193. void pstate(player const& p)
  194. {
  195. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  196. }
  197. void test()
  198. {
  199. player p;
  200. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  201. p.start();
  202. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  203. p.process_event(open_close); pstate(p);
  204. p.process_event(open_close); pstate(p);
  205. // will be rejected, wrong disk type
  206. p.process_event(
  207. cd_detected_event("louie, louie",DISK_DVD)); pstate(p);
  208. p.process_event(
  209. cd_detected_event("louie, louie",DISK_CD)); pstate(p);
  210. // no need to call play as the previous event does it in its action method
  211. //p.process_event(play);
  212. // at this point, Play is active
  213. p.process_event(pause); pstate(p);
  214. // go back to Playing
  215. p.process_event(end_pause); pstate(p);
  216. p.process_event(pause); pstate(p);
  217. p.process_event(stop); pstate(p);
  218. // event leading to the same state
  219. // no action method called as none is defined in the transition table
  220. p.process_event(stop); pstate(p);
  221. // test call to no_transition
  222. p.process_event(pause); pstate(p);
  223. }
  224. }
  225. int main()
  226. {
  227. test();
  228. return 0;
  229. }