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_
>
37 :expected_action_counter(0),expected_action2_counter(0)
39 // The list of FSM states
40 struct State11
: public msm::front::state
<>
42 typedef mpl::vector
<eventd
> deferred_events
;
44 template <class Event
,class FSM
>
45 void on_entry(Event
const&,FSM
& ) {++entry_counter
;}
46 template <class Event
,class FSM
>
47 void on_exit(Event
const&,FSM
& ) {++exit_counter
;}
51 struct State12
: public msm::front::state
<>
53 template <class Event
,class FSM
>
54 void on_entry(Event
const&,FSM
& ) {++entry_counter
;}
55 template <class Event
,class FSM
>
56 void on_exit(Event
const&,FSM
& ) {++exit_counter
;}
60 struct State13
: public msm::front::state
<>
62 template <class Event
,class FSM
>
63 void on_entry(Event
const&,FSM
& ) {++entry_counter
;}
64 template <class Event
,class FSM
>
65 void on_exit(Event
const&,FSM
& ) {++exit_counter
;}
71 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
72 void operator()(EVT
const& ,FSM
& fsm
,SourceState
& ,TargetState
& )
74 fsm
.template process_event(event2());
77 struct expected_action
79 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
80 void operator()(EVT
const& ,FSM
& fsm
,SourceState
& ,TargetState
& )
82 ++fsm
.expected_action_counter
;
85 struct expected_action2
87 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
88 void operator()(EVT
const& ,FSM
& fsm
,SourceState
& ,TargetState
& )
90 ++fsm
.expected_action2_counter
;
93 // the initial state of the player SM. Must be defined
94 typedef mpl::vector
<State11
> initial_state
;
97 // Transition table for player
98 struct transition_table
: mpl::vector
<
99 // Start Event Next Action Guard
100 // +---------+-------------+---------+---------------------+----------------------+
101 Row
< State11
, event1
, State12
, enqueue_action
>,
102 Row
< State12
, event2
, State13
, expected_action2
>,
103 Row
< State12
, eventd
, State13
, expected_action
>,
104 Row
< State13
, event2
, State11
>,
105 Row
< State13
, eventd
, State11
>
106 // +---------+-------------+---------+---------------------+----------------------+
109 // Replaces the default no-transition response.
110 template <class FSM
,class Event
>
111 void no_transition(Event
const& , FSM
&,int )
113 BOOST_FAIL("no_transition called!");
116 template <class Event
,class FSM
>
117 void on_entry(Event
const&,FSM
& fsm
)
119 fsm
.template get_state
<player_::State11
&>().entry_counter
=0;
120 fsm
.template get_state
<player_::State11
&>().exit_counter
=0;
121 fsm
.template get_state
<player_::State12
&>().entry_counter
=0;
122 fsm
.template get_state
<player_::State12
&>().exit_counter
=0;
123 fsm
.template get_state
<player_::State13
&>().entry_counter
=0;
124 fsm
.template get_state
<player_::State13
&>().exit_counter
=0;
126 int expected_action_counter
;
127 int expected_action2_counter
;
130 typedef msm::back::state_machine
<player_
> player
;
132 BOOST_AUTO_TEST_CASE( TestDeferAndMessageQueue2
)
135 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
138 p
.process_event(eventd());
139 BOOST_CHECK_MESSAGE(p
.current_state()[0] == 0,"State11 should be active");
140 BOOST_CHECK_MESSAGE(p
.get_state
<player_::State11
&>().entry_counter
== 1,"State11 entry not called correctly");
142 p
.process_event(event1());
143 BOOST_CHECK_MESSAGE(p
.current_state()[0] == 0,"State11 should be active");
144 BOOST_CHECK_MESSAGE(p
.get_state
<player_::State11
&>().exit_counter
== 1,"State11 exit not called correctly");
145 BOOST_CHECK_MESSAGE(p
.get_state
<player_::State12
&>().entry_counter
== 1,"State12 entry not called correctly");
146 BOOST_CHECK_MESSAGE(p
.get_state
<player_::State12
&>().exit_counter
== 1,"State12 exit not called correctly");
147 BOOST_CHECK_MESSAGE(p
.get_state
<player_::State11
&>().entry_counter
== 2,"State11 entry not called correctly");
148 BOOST_CHECK_MESSAGE(p
.get_state
<player_::State13
&>().exit_counter
== 1,"State13 exit not called correctly");
149 BOOST_CHECK_MESSAGE(p
.get_state
<player_::State13
&>().entry_counter
== 1,"State13 entry not called correctly");
150 BOOST_CHECK_MESSAGE(p
.expected_action_counter
== 1,"expected_action should have been called");
151 BOOST_CHECK_MESSAGE(p
.expected_action2_counter
== 0,"expected_action2 should not have been called");