FsmAsPtr.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "FsmAsPtr.h"
  11. #include <boost/msm/back/state_machine.hpp>
  12. #include <boost/msm/front/euml/euml.hpp>
  13. // cpp: using directives are okay
  14. using namespace std;
  15. using namespace boost::msm::front::euml;
  16. namespace msm = boost::msm;
  17. // entry/exit/action/guard logging functors
  18. #include "logging_functors.h"
  19. namespace
  20. {
  21. // events
  22. BOOST_MSM_EUML_EVENT(play)
  23. BOOST_MSM_EUML_EVENT(end_pause)
  24. BOOST_MSM_EUML_EVENT(stop)
  25. BOOST_MSM_EUML_EVENT(pause)
  26. BOOST_MSM_EUML_EVENT(open_close)
  27. BOOST_MSM_EUML_EVENT(cd_detected)
  28. // Concrete FSM implementation
  29. // The list of FSM states
  30. // state not needing any entry or exit
  31. BOOST_MSM_EUML_STATE((),Paused)
  32. // it is also possible to define a state which you can implement normally
  33. // just make it a state, as usual, and also a grammar terminal, euml_state
  34. struct Empty_impl : public msm::front::state<> , public euml_state<Empty_impl>
  35. {
  36. // this allows us to add some functions
  37. void activate_empty() {std::cout << "switching to Empty " << std::endl;}
  38. // standard entry behavior
  39. template <class Event,class FSM>
  40. void on_entry(Event const& evt,FSM& fsm)
  41. {
  42. std::cout << "entering: Empty" << std::endl;
  43. }
  44. template <class Event,class FSM>
  45. void on_exit(Event const& evt,FSM& fsm)
  46. {
  47. std::cout << "leaving: Empty" << std::endl;
  48. }
  49. };
  50. //instance for use in the transition table
  51. Empty_impl const Empty;
  52. // create a functor and a eUML function for the activate_empty method from Entry
  53. BOOST_MSM_EUML_METHOD(ActivateEmpty_ , activate_empty , activate_empty_ , void , void )
  54. // define more states
  55. BOOST_MSM_EUML_STATE(( Open_Entry,Open_Exit ),Open)
  56. BOOST_MSM_EUML_STATE(( Stopped_Entry,Stopped_Exit ),Stopped)
  57. BOOST_MSM_EUML_STATE(( Playing_Entry,Playing_Exit ),Playing)
  58. // it is also possible to use a plain functor, with default-constructor in the transition table
  59. struct start_play
  60. {
  61. template <class FSM,class EVT,class SourceState,class TargetState>
  62. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  63. {
  64. cout << "player::start_play" << endl;
  65. }
  66. };
  67. // replaces the old transition table
  68. BOOST_MSM_EUML_TRANSITION_TABLE((
  69. Playing == Stopped + play / start_play() ,
  70. Playing == Paused + end_pause / resume_playback,
  71. // +------------------------------------------------------------------------------+
  72. Empty == Open + open_close / (close_drawer,activate_empty_(target_)),
  73. // +------------------------------------------------------------------------------+
  74. Open == Empty + open_close / open_drawer,
  75. Open == Paused + open_close / stop_and_open,
  76. Open == Stopped + open_close / open_drawer,
  77. Open == Playing + open_close / stop_and_open,
  78. // +------------------------------------------------------------------------------+
  79. Paused == Playing + pause / pause_playback,
  80. // +------------------------------------------------------------------------------+
  81. Stopped == Playing + stop / stop_playback,
  82. Stopped == Paused + stop / stop_playback,
  83. Stopped == Empty + cd_detected / (store_cd_info,process_(play)),
  84. Stopped == Stopped + stop
  85. // +------------------------------------------------------------------------------+
  86. ),transition_table)
  87. // create a state machine "on the fly"
  88. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  89. init_ << Empty, // Init State
  90. no_action, // Entry
  91. no_action, // Exit
  92. attributes_ << no_attributes_, // Attributes
  93. configure_ << no_configure_, // configuration
  94. Log_No_Transition // no_transition handler
  95. ),
  96. my_machine_impl_) //fsm name
  97. // choice of back-end
  98. typedef msm::back::state_machine<my_machine_impl_> my_machine_impl;
  99. }
  100. player::player()
  101. : fsm_(new my_machine_impl)
  102. {
  103. boost::static_pointer_cast<my_machine_impl>(fsm_)->start();
  104. }
  105. void player::do_play()
  106. {
  107. boost::static_pointer_cast<my_machine_impl>(fsm_)->process_event(play);
  108. }
  109. void player::do_pause()
  110. {
  111. boost::static_pointer_cast<my_machine_impl>(fsm_)->process_event(pause);
  112. }
  113. void player::do_open_close()
  114. {
  115. boost::static_pointer_cast<my_machine_impl>(fsm_)->process_event(open_close);
  116. }
  117. void player::do_end_pause()
  118. {
  119. boost::static_pointer_cast<my_machine_impl>(fsm_)->process_event(end_pause);
  120. }
  121. void player::do_stop()
  122. {
  123. boost::static_pointer_cast<my_machine_impl>(fsm_)->process_event(stop);
  124. }
  125. void player::do_cd_detected()
  126. {
  127. boost::static_pointer_cast<my_machine_impl>(fsm_)->process_event(cd_detected);
  128. }
  129. int main()
  130. {
  131. player p;
  132. // note that we write open_close and not open_close(), like usual. Both are possible with eUML, but
  133. // you now have less to type.
  134. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  135. p.do_open_close();
  136. p.do_open_close();
  137. p.do_cd_detected();
  138. // no need to call play as the previous event does it in its action method
  139. // at this point, Play is active
  140. p.do_pause();
  141. // go back to Playing
  142. p.do_end_pause();
  143. p.do_pause();
  144. p.do_stop();
  145. // event leading to the same state
  146. // no action method called as none is defined in the transition table
  147. p.do_stop();
  148. // test call to no_transition
  149. p.do_pause();
  150. return 0;
  151. }