1 // Copyright 2017 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 11 #include <iostream> 12 // back-end 13 #include <boost/msm/back/state_machine.hpp> 14 //front-end 15 #include <boost/msm/front/state_machine_def.hpp> 16 #include <boost/msm/front/functor_row.hpp> 17 18 #include <boost/test/unit_test.hpp> 19 20 namespace msm = boost::msm; 21 namespace mpl = boost::mpl; 22 using namespace boost::msm::front; 23 24 namespace 25 { 26 // events 27 struct event1 {}; 28 struct event2 {}; 29 struct event3 {}; 30 struct eventd {}; 31 32 33 // front-end: define the FSM structure 34 struct player_ : public msm::front::state_machine_def<player_> 35 { 36 // in this test, we inverse the order deferred queue / event queue 37 typedef int event_queue_before_deferred_queue; 38 player___anon3d6ecb050111::player_39 player_() 40 :expected_action_counter(0),expected_action2_counter(0) 41 {} 42 // The list of FSM states 43 struct State11 : public msm::front::state<> 44 { 45 typedef mpl::vector<eventd> deferred_events; 46 47 template <class Event,class FSM> on_entry__anon3d6ecb050111::player_::State1148 void on_entry(Event const&,FSM& ) {++entry_counter;} 49 template <class Event,class FSM> on_exit__anon3d6ecb050111::player_::State1150 void on_exit(Event const&,FSM& ) {++exit_counter;} 51 int entry_counter; 52 int exit_counter; 53 }; 54 struct State12 : public msm::front::state<> 55 { 56 template <class Event,class FSM> on_entry__anon3d6ecb050111::player_::State1257 void on_entry(Event const&,FSM& ) {++entry_counter;} 58 template <class Event,class FSM> on_exit__anon3d6ecb050111::player_::State1259 void on_exit(Event const&,FSM& ) {++exit_counter;} 60 int entry_counter; 61 int exit_counter; 62 }; 63 struct State13 : public msm::front::state<> 64 { 65 template <class Event,class FSM> on_entry__anon3d6ecb050111::player_::State1366 void on_entry(Event const&,FSM& ) {++entry_counter;} 67 template <class Event,class FSM> on_exit__anon3d6ecb050111::player_::State1368 void on_exit(Event const&,FSM& ) {++exit_counter;} 69 int entry_counter; 70 int exit_counter; 71 }; 72 struct enqueue_action 73 { 74 template <class EVT,class FSM,class SourceState,class TargetState> operator ()__anon3d6ecb050111::player_::enqueue_action75 void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& ) 76 { 77 fsm.template process_event(event2()); 78 } 79 }; 80 struct expected_action 81 { 82 template <class EVT,class FSM,class SourceState,class TargetState> operator ()__anon3d6ecb050111::player_::expected_action83 void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& ) 84 { 85 ++fsm.expected_action_counter; 86 } 87 }; 88 struct expected_action2 89 { 90 template <class EVT,class FSM,class SourceState,class TargetState> operator ()__anon3d6ecb050111::player_::expected_action291 void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& ) 92 { 93 ++fsm.expected_action2_counter; 94 } 95 }; 96 // the initial state of the player SM. Must be defined 97 typedef mpl::vector<State11> initial_state; 98 99 100 // Transition table for player 101 struct transition_table : mpl::vector< 102 // Start Event Next Action Guard 103 // +---------+-------------+---------+---------------------+----------------------+ 104 Row < State11 , event1 , State12 , enqueue_action >, 105 Row < State12 , event2 , State13 , expected_action2 >, 106 Row < State12 , eventd , State13 , expected_action >, 107 Row < State13 , event2 , State11 >, 108 Row < State13 , eventd , State11 > 109 // +---------+-------------+---------+---------------------+----------------------+ 110 > {}; 111 112 // Replaces the default no-transition response. 113 template <class FSM,class Event> no_transition__anon3d6ecb050111::player_114 void no_transition(Event const& , FSM&,int ) 115 { 116 BOOST_FAIL("no_transition called!"); 117 } 118 // init counters 119 template <class Event,class FSM> on_entry__anon3d6ecb050111::player_120 void on_entry(Event const&,FSM& fsm) 121 { 122 fsm.template get_state<player_::State11&>().entry_counter=0; 123 fsm.template get_state<player_::State11&>().exit_counter=0; 124 fsm.template get_state<player_::State12&>().entry_counter=0; 125 fsm.template get_state<player_::State12&>().exit_counter=0; 126 fsm.template get_state<player_::State13&>().entry_counter=0; 127 fsm.template get_state<player_::State13&>().exit_counter=0; 128 } 129 int expected_action_counter; 130 int expected_action2_counter; 131 }; 132 // Pick a back-end 133 typedef msm::back::state_machine<player_> player; 134 BOOST_AUTO_TEST_CASE(TestDeferAndMessageQueue2)135 BOOST_AUTO_TEST_CASE( TestDeferAndMessageQueue2 ) 136 { 137 player p; 138 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM 139 p.start(); 140 141 p.process_event(eventd()); 142 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"State11 should be active"); 143 BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().entry_counter == 1,"State11 entry not called correctly"); 144 145 p.process_event(event1()); 146 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"State11 should be active"); 147 BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().exit_counter == 1,"State11 exit not called correctly"); 148 BOOST_CHECK_MESSAGE(p.get_state<player_::State12&>().entry_counter == 1,"State12 entry not called correctly"); 149 BOOST_CHECK_MESSAGE(p.get_state<player_::State12&>().exit_counter == 1,"State12 exit not called correctly"); 150 BOOST_CHECK_MESSAGE(p.get_state<player_::State11&>().entry_counter == 2,"State11 entry not called correctly"); 151 BOOST_CHECK_MESSAGE(p.get_state<player_::State13&>().exit_counter == 1,"State13 exit not called correctly"); 152 BOOST_CHECK_MESSAGE(p.get_state<player_::State13&>().entry_counter == 1,"State13 entry not called correctly"); 153 BOOST_CHECK_MESSAGE(p.expected_action_counter == 0,"expected_action should have been called"); 154 BOOST_CHECK_MESSAGE(p.expected_action2_counter == 1,"expected_action2 should not have been called"); 155 } 156 } 157 158 159