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 | |
11 | // back-end |
12 | #include <boost/msm/back/state_machine.hpp> |
13 | //front-end |
14 | #include <boost/msm/front/state_machine_def.hpp> |
15 | #ifndef BOOST_MSM_NONSTANDALONE_TEST |
16 | #define BOOST_TEST_MODULE MyTest |
17 | #endif |
18 | #include <boost/test/unit_test.hpp> |
19 | |
20 | namespace msm = boost::msm; |
21 | namespace mpl = boost::mpl; |
22 | |
23 | namespace |
24 | { |
25 | // events |
26 | struct play {}; |
27 | struct end_pause {}; |
28 | struct stop {}; |
29 | struct pause {}; |
30 | struct open_close {}; |
31 | |
32 | // A "complicated" event type that carries some data. |
33 | enum DiskTypeEnum |
34 | { |
35 | DISK_CD=0, |
36 | DISK_DVD=1 |
37 | }; |
38 | struct cd_detected |
39 | { |
40 | cd_detected(std::string name, DiskTypeEnum diskType) |
41 | : name(name), |
42 | disc_type(diskType) |
43 | {} |
44 | |
45 | std::string name; |
46 | DiskTypeEnum disc_type; |
47 | }; |
48 | struct SomeExternalContext |
49 | { |
50 | SomeExternalContext(int b):bla(b){} |
51 | int bla; |
52 | }; |
53 | // front-end: define the FSM structure |
54 | struct player_ : public msm::front::state_machine_def<player_> |
55 | { |
56 | player_(SomeExternalContext& context,int someint): |
57 | context_(context), |
58 | someint_(someint) |
59 | {} |
60 | |
61 | SomeExternalContext& context_; |
62 | int someint_; |
63 | |
64 | // The list of FSM states |
65 | struct Empty : public msm::front::state<> |
66 | { |
67 | template <class Event,class FSM> |
68 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
69 | template <class Event,class FSM> |
70 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
71 | int entry_counter; |
72 | int exit_counter; |
73 | }; |
74 | struct Open : public msm::front::state<> |
75 | { |
76 | template <class Event,class FSM> |
77 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
78 | template <class Event,class FSM> |
79 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
80 | int entry_counter; |
81 | int exit_counter; |
82 | }; |
83 | |
84 | // sm_ptr still supported but deprecated as functors are a much better way to do the same thing |
85 | struct Stopped : public msm::front::state<> |
86 | { |
87 | template <class Event,class FSM> |
88 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
89 | template <class Event,class FSM> |
90 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
91 | int entry_counter; |
92 | int exit_counter; |
93 | }; |
94 | |
95 | struct Playing : public msm::front::state<> |
96 | { |
97 | template <class Event,class FSM> |
98 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
99 | template <class Event,class FSM> |
100 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
101 | int entry_counter; |
102 | int exit_counter; |
103 | }; |
104 | |
105 | // state not defining any entry or exit |
106 | struct Paused : public msm::front::state<> |
107 | { |
108 | template <class Event,class FSM> |
109 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
110 | template <class Event,class FSM> |
111 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
112 | int entry_counter; |
113 | int exit_counter; |
114 | }; |
115 | |
116 | // the initial state of the player SM. Must be defined |
117 | typedef Empty initial_state; |
118 | |
119 | // transition actions |
120 | void start_playback(play const&) {} |
121 | void open_drawer(open_close const&) { } |
122 | void store_cd_info(cd_detected const&) { } |
123 | void stop_playback(stop const&) { } |
124 | void pause_playback(pause const&) { } |
125 | void resume_playback(end_pause const&) { } |
126 | void stop_and_open(open_close const&) { } |
127 | void stopped_again(stop const&){} |
128 | // guard conditions |
129 | bool good_disk_format(cd_detected const& evt) |
130 | { |
131 | // to test a guard condition, let's say we understand only CDs, not DVD |
132 | if (evt.disc_type != DISK_CD) |
133 | { |
134 | return false; |
135 | } |
136 | return true; |
137 | } |
138 | bool can_close_drawer(open_close const&) |
139 | { |
140 | return true; |
141 | } |
142 | |
143 | typedef player_ p; // makes transition table cleaner |
144 | |
145 | // Transition table for player |
146 | struct transition_table : mpl::vector< |
147 | // Start Event Next Action Guard |
148 | // +---------+-------------+---------+---------------------+----------------------+ |
149 | a_row < Stopped , play , Playing , &p::start_playback >, |
150 | a_row < Stopped , open_close , Open , &p::open_drawer >, |
151 | _row < Stopped , stop , Stopped >, |
152 | // +---------+-------------+---------+---------------------+----------------------+ |
153 | g_row < Open , open_close , Empty , &p::can_close_drawer >, |
154 | // +---------+-------------+---------+---------------------+----------------------+ |
155 | a_row < Empty , open_close , Open , &p::open_drawer >, |
156 | row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >, |
157 | // +---------+-------------+---------+---------------------+----------------------+ |
158 | a_row < Playing , stop , Stopped , &p::stop_playback >, |
159 | a_row < Playing , pause , Paused , &p::pause_playback >, |
160 | a_row < Playing , open_close , Open , &p::stop_and_open >, |
161 | // +---------+-------------+---------+---------------------+----------------------+ |
162 | a_row < Paused , end_pause , Playing , &p::resume_playback >, |
163 | a_row < Paused , stop , Stopped , &p::stop_playback >, |
164 | a_row < Paused , open_close , Open , &p::stop_and_open > |
165 | // +---------+-------------+---------+---------------------+----------------------+ |
166 | > {}; |
167 | // Replaces the default no-transition response. |
168 | template <class FSM,class Event> |
169 | void no_transition(Event const&, FSM&,int) |
170 | { |
171 | BOOST_FAIL("no_transition called!" ); |
172 | } |
173 | // init counters |
174 | template <class Event,class FSM> |
175 | void on_entry(Event const&,FSM& fsm) |
176 | { |
177 | fsm.template get_state<player_::Stopped&>().entry_counter=0; |
178 | fsm.template get_state<player_::Stopped&>().exit_counter=0; |
179 | fsm.template get_state<player_::Open&>().entry_counter=0; |
180 | fsm.template get_state<player_::Open&>().exit_counter=0; |
181 | fsm.template get_state<player_::Empty&>().entry_counter=0; |
182 | fsm.template get_state<player_::Empty&>().exit_counter=0; |
183 | fsm.template get_state<player_::Playing&>().entry_counter=0; |
184 | fsm.template get_state<player_::Playing&>().exit_counter=0; |
185 | fsm.template get_state<player_::Paused&>().entry_counter=0; |
186 | fsm.template get_state<player_::Paused&>().exit_counter=0; |
187 | fsm.context_ = 20; |
188 | } |
189 | |
190 | }; |
191 | // Pick a back-end |
192 | typedef msm::back::state_machine<player_> player; |
193 | |
194 | // static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" }; |
195 | |
196 | |
197 | BOOST_AUTO_TEST_CASE( my_test ) |
198 | { |
199 | SomeExternalContext ctx(3); |
200 | player p(boost::ref(t&: ctx),5); |
201 | BOOST_CHECK_MESSAGE(p.context_.bla == 3,"wrong value passed" ); |
202 | BOOST_CHECK_MESSAGE(p.someint_ == 5,"wrong value passed" ); |
203 | ctx.bla = 10; |
204 | BOOST_CHECK_MESSAGE(p.context_.bla == 10,"error with reference" ); |
205 | p.start(); |
206 | BOOST_CHECK_MESSAGE(p.context_.bla == 20,"error with fsm entry behavior" ); |
207 | } |
208 | } |
209 | |
210 | |