]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | #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 | namespace msm = boost::msm; | |
19 | namespace mpl = boost::mpl; | |
20 | using namespace msm::front; | |
21 | ||
22 | namespace | |
23 | { | |
24 | // events | |
25 | struct play {}; | |
26 | struct end_pause {}; | |
27 | struct stop {}; | |
28 | struct pause {}; | |
29 | struct open_close {}; | |
30 | struct to_ignore {}; | |
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 | ||
49 | // front-end: define the FSM structure | |
50 | struct player_ : public msm::front::state_machine_def<player_> | |
51 | { | |
52 | // The list of FSM states | |
53 | struct Empty : public msm::front::state<> | |
54 | { | |
55 | // every (optional) entry/exit methods get the event passed. | |
56 | template <class Event,class FSM> | |
57 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;} | |
58 | template <class Event,class FSM> | |
59 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;} | |
60 | }; | |
61 | struct Open : public msm::front::state<> | |
62 | { | |
63 | template <class Event,class FSM> | |
64 | void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;} | |
65 | template <class Event,class FSM> | |
66 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;} | |
67 | }; | |
68 | ||
69 | // sm_ptr still supported but deprecated as functors are a much better way to do the same thing | |
70 | struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr> | |
71 | { | |
72 | template <class Event,class FSM> | |
73 | void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;} | |
74 | template <class Event,class FSM> | |
75 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;} | |
76 | void set_sm_ptr(player_* pl) | |
77 | { | |
78 | m_player=pl; | |
79 | } | |
80 | player_* m_player; | |
81 | }; | |
82 | ||
83 | struct Playing : public msm::front::state<> | |
84 | { | |
85 | template <class Event,class FSM> | |
86 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;} | |
87 | template <class Event,class FSM> | |
88 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;} | |
89 | }; | |
90 | ||
91 | // state not defining any entry or exit | |
92 | struct Paused : public msm::front::state<> | |
93 | { | |
94 | }; | |
95 | ||
96 | // the initial state of the player SM. Must be defined | |
97 | typedef Empty initial_state; | |
98 | ||
99 | // transition actions | |
100 | void start_playback(play const&) { std::cout << "player::start_playback\n"; } | |
101 | void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; } | |
102 | void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; } | |
103 | void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; } | |
104 | void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; } | |
105 | void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; } | |
106 | void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; } | |
107 | void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; } | |
108 | void stopped_again(stop const&) {std::cout << "player::stopped_again\n";} | |
109 | // guard conditions | |
110 | bool good_disk_format(cd_detected const& evt) | |
111 | { | |
112 | // to test a guard condition, let's say we understand only CDs, not DVD | |
113 | if (evt.disc_type != DISK_CD) | |
114 | { | |
115 | std::cout << "wrong disk, sorry" << std::endl; | |
116 | return false; | |
117 | } | |
118 | return true; | |
119 | } | |
120 | // transitions internal to Empty | |
121 | void internal_action(cd_detected const&){ std::cout << "Empty::internal action\n"; } | |
122 | bool internal_guard(cd_detected const&) | |
123 | { | |
124 | std::cout << "Empty::internal guard\n"; | |
125 | return false; | |
126 | } | |
127 | struct internal_guard_fct | |
128 | { | |
129 | template <class EVT,class FSM,class SourceState,class TargetState> | |
130 | bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& ) | |
131 | { | |
132 | std::cout << "Empty::internal guard functor\n"; | |
133 | return false; | |
134 | } | |
135 | }; | |
136 | struct internal_action_fct | |
137 | { | |
138 | template <class EVT,class FSM,class SourceState,class TargetState> | |
139 | void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& ) | |
140 | { | |
141 | std::cout << "Empty::internal action functor" << std::endl; | |
142 | } | |
143 | }; | |
144 | ||
145 | typedef player_ p; // makes transition table cleaner | |
146 | ||
147 | // Transition table for player | |
148 | struct transition_table : mpl::vector< | |
149 | // Start Event Next Action Guard | |
150 | // +---------+-------------+---------+---------------------+----------------------+ | |
151 | a_row < Stopped , play , Playing , &p::start_playback >, | |
152 | a_row < Stopped , open_close , Open , &p::open_drawer >, | |
153 | _row < Stopped , stop , Stopped >, | |
154 | // +---------+-------------+---------+---------------------+----------------------+ | |
155 | a_row < Open , open_close , Empty , &p::close_drawer >, | |
156 | // +---------+-------------+---------+---------------------+----------------------+ | |
157 | a_row < Empty , open_close , Open , &p::open_drawer >, | |
158 | row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >, | |
159 | irow < Empty , cd_detected , &p::internal_action ,&p::internal_guard >, | |
160 | _irow < Empty , to_ignore >, | |
161 | g_irow < Empty , cd_detected ,&p::internal_guard >, | |
162 | Row < Empty , cd_detected , none , internal_action_fct ,internal_guard_fct >, | |
163 | // +---------+-------------+---------+---------------------+----------------------+ | |
164 | a_row < Playing , stop , Stopped , &p::stop_playback >, | |
165 | a_row < Playing , pause , Paused , &p::pause_playback >, | |
166 | a_row < Playing , open_close , Open , &p::stop_and_open >, | |
167 | // +---------+-------------+---------+---------------------+----------------------+ | |
168 | a_row < Paused , end_pause , Playing , &p::resume_playback >, | |
169 | a_row < Paused , stop , Stopped , &p::stop_playback >, | |
170 | a_row < Paused , open_close , Open , &p::stop_and_open > | |
171 | // +---------+-------------+---------+---------------------+----------------------+ | |
172 | > {}; | |
173 | // Replaces the default no-transition response. | |
174 | template <class FSM,class Event> | |
175 | void no_transition(Event const& e, FSM&,int state) | |
176 | { | |
177 | std::cout << "no transition from state " << state | |
178 | << " on event " << typeid(e).name() << std::endl; | |
179 | } | |
180 | }; | |
181 | // Pick a back-end | |
182 | typedef msm::back::state_machine<player_> player; | |
183 | ||
184 | // | |
185 | // Testing utilities. | |
186 | // | |
187 | static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" }; | |
188 | void pstate(player const& p) | |
189 | { | |
190 | std::cout << " -> " << state_names[p.current_state()[0]] << std::endl; | |
191 | } | |
192 | ||
193 | void test() | |
194 | { | |
195 | player p; | |
196 | // needed to start the highest-level SM. This will call on_entry and mark the start of the SM | |
197 | p.start(); | |
198 | // this event will be ignored and not call no_transition | |
199 | p.process_event(to_ignore()); | |
200 | // go to Open, call on_exit on Empty, then action, then on_entry on Open | |
201 | p.process_event(open_close()); pstate(p); | |
202 | p.process_event(open_close()); pstate(p); | |
203 | // will be rejected, wrong disk type | |
204 | p.process_event( | |
205 | cd_detected("louie, louie",DISK_DVD)); pstate(p); | |
206 | p.process_event( | |
207 | cd_detected("louie, louie",DISK_CD)); pstate(p); | |
208 | p.process_event(play()); | |
209 | ||
210 | // at this point, Play is active | |
211 | p.process_event(pause()); pstate(p); | |
212 | // go back to Playing | |
213 | p.process_event(end_pause()); pstate(p); | |
214 | p.process_event(pause()); pstate(p); | |
215 | p.process_event(stop()); pstate(p); | |
216 | // event leading to the same state | |
217 | // no action method called as it is not present in the transition table | |
218 | p.process_event(stop()); pstate(p); | |
219 | } | |
220 | } | |
221 | ||
222 | int main() | |
223 | { | |
224 | test(); | |
225 | return 0; | |
226 | } |