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)
13 #include <boost/msm/back/state_machine.hpp>
15 #include <boost/msm/front/state_machine_def.hpp>
16 #include <boost/msm/front/functor_row.hpp>
18 #include <boost/test/unit_test.hpp>
20 namespace msm
= boost::msm
;
21 namespace mpl
= boost::mpl
;
22 using namespace boost::msm::front
;
33 // front-end: define the FSM structure
34 struct player_
: public msm::front::state_machine_def
<player_
>
36 // in this test, we inverse the order deferred queue / event queue
37 typedef int event_queue_before_deferred_queue
;
40 :expected_action_counter(0),expected_action2_counter(0)
42 // The list of FSM states
43 struct State11
: public msm::front::state
<>
45 typedef mpl::vector
<eventd
> deferred_events
;
47 template <class Event
,class FSM
>
48 void on_entry(Event
const&,FSM
& ) {++entry_counter
;}
49 template <class Event
,class FSM
>
50 void on_exit(Event
const&,FSM
& ) {++exit_counter
;}
54 struct State12
: public msm::front::state
<>
56 template <class Event
,class FSM
>
57 void on_entry(Event
const&,FSM
& ) {++entry_counter
;}
58 template <class Event
,class FSM
>
59 void on_exit(Event
const&,FSM
& ) {++exit_counter
;}
63 struct State13
: public msm::front::state
<>
65 template <class Event
,class FSM
>
66 void on_entry(Event
const&,FSM
& ) {++entry_counter
;}
67 template <class Event
,class FSM
>
68 void on_exit(Event
const&,FSM
& ) {++exit_counter
;}
74 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
75 void operator()(EVT
const& ,FSM
& fsm
,SourceState
& ,TargetState
& )
77 fsm
.template process_event(event2());
80 struct expected_action
82 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
83 void operator()(EVT
const& ,FSM
& fsm
,SourceState
& ,TargetState
& )
85 ++fsm
.expected_action_counter
;
88 struct expected_action2
90 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
91 void operator()(EVT
const& ,FSM
& fsm
,SourceState
& ,TargetState
& )
93 ++fsm
.expected_action2_counter
;
96 // the initial state of the player SM. Must be defined
97 typedef mpl::vector
<State11
> initial_state
;
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 // +---------+-------------+---------+---------------------+----------------------+
112 // Replaces the default no-transition response.
113 template <class FSM
,class Event
>
114 void no_transition(Event
const& , FSM
&,int )
116 BOOST_FAIL("no_transition called!");
119 template <class Event
,class FSM
>
120 void on_entry(Event
const&,FSM
& fsm
)
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;
129 int expected_action_counter
;
130 int expected_action2_counter
;
133 typedef msm::back::state_machine
<player_
> player
;
135 BOOST_AUTO_TEST_CASE( TestDeferAndMessageQueue2
)
138 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
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");
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");