ActivateStateBeforeTransitionEuml.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include <boost/msm/back/state_machine.hpp>
  12. #include <boost/msm/front/state_machine_def.hpp>
  13. #include <boost/msm/front/euml/euml.hpp>
  14. using namespace std;
  15. namespace msm = boost::msm;
  16. namespace mpl = boost::mpl;
  17. using namespace boost::msm::front::euml;
  18. namespace // Concrete FSM implementation
  19. {
  20. // events
  21. BOOST_MSM_EUML_EVENT(connect)
  22. BOOST_MSM_EUML_EVENT(disconnect)
  23. // flag
  24. BOOST_MSM_EUML_FLAG(is_connected)
  25. BOOST_MSM_EUML_ACTION(SignalConnect)
  26. {
  27. template <class FSM,class EVT,class SourceState,class TargetState>
  28. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  29. {
  30. // by default, this would be wrong (shows false)
  31. cout << "SignalConnect. Connected? "
  32. << std::boolalpha
  33. << fsm.template is_flag_active<BOOST_MSM_EUML_FLAG_NAME(is_connected)>() << endl;
  34. }
  35. };
  36. BOOST_MSM_EUML_ACTION(SignalDisconnect)
  37. {
  38. template <class FSM,class EVT,class SourceState,class TargetState>
  39. void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& )
  40. {
  41. // by default, this would be wrong (shows true)
  42. cout << "SignalDisconnect. Connected? "
  43. << std::boolalpha
  44. << fsm.template is_flag_active<BOOST_MSM_EUML_FLAG_NAME(is_connected)>()
  45. << endl;
  46. }
  47. };
  48. // The list of FSM states
  49. BOOST_MSM_EUML_ACTION(Connected_Entry)
  50. {
  51. template <class Event,class FSM,class STATE>
  52. void operator()(Event const&,FSM&,STATE& )
  53. {
  54. std::cout << "entering: Connected" << std::endl;
  55. }
  56. };
  57. BOOST_MSM_EUML_ACTION(Connected_Exit)
  58. {
  59. template <class Event,class FSM,class STATE>
  60. void operator()(Event const&,FSM&,STATE& )
  61. {
  62. std::cout << "leaving: Connected" << std::endl;
  63. }
  64. };
  65. BOOST_MSM_EUML_STATE(( Connected_Entry,Connected_Exit,
  66. attributes_ << no_attributes_,
  67. configure_<< is_connected ),Connected)
  68. BOOST_MSM_EUML_ACTION(Disconnected_Entry)
  69. {
  70. template <class Event,class FSM,class STATE>
  71. void operator()(Event const&,FSM&,STATE& )
  72. {
  73. std::cout << "entering: Disconnected" << std::endl;
  74. }
  75. };
  76. BOOST_MSM_EUML_ACTION(Disconnected_Exit)
  77. {
  78. template <class Event,class FSM,class STATE>
  79. void operator()(Event const&,FSM&,STATE& )
  80. {
  81. std::cout << "leaving: Disconnected" << std::endl;
  82. }
  83. };
  84. BOOST_MSM_EUML_STATE(( Disconnected_Entry,Disconnected_Exit ),Disconnected)
  85. // replaces the old transition table
  86. BOOST_MSM_EUML_TRANSITION_TABLE((
  87. Disconnected == Connected + disconnect / SignalDisconnect ,
  88. Connected == Disconnected + connect / SignalConnect
  89. // +------------------------------------------------------------------------------+
  90. ),transition_table)
  91. BOOST_MSM_EUML_ACTION(Log_No_Transition)
  92. {
  93. template <class FSM,class Event>
  94. void operator()(Event const& e,FSM&,int state)
  95. {
  96. std::cout << "no transition from state " << state
  97. << " on event " << typeid(e).name() << std::endl;
  98. }
  99. };
  100. // create a state machine "on the fly"
  101. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  102. init_ << Disconnected, // Init State
  103. no_action, // Entry
  104. no_action, // Exit
  105. attributes_ << no_attributes_, // Attributes
  106. configure_ << switch_active_before_transition, // configuration
  107. Log_No_Transition // no_transition handler
  108. ),
  109. Connection_) //fsm name
  110. typedef msm::back::state_machine<Connection_> Connection;
  111. void test()
  112. {
  113. Connection connection;
  114. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  115. connection.start();
  116. // signal a connection
  117. connection.process_event(connect);
  118. // signal a disconnection
  119. connection.process_event(disconnect);
  120. connection.stop();
  121. }
  122. }
  123. int main()
  124. {
  125. test();
  126. return 0;
  127. }