• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 namespace msm = boost::msm;
18 namespace mpl = boost::mpl;
19 
20 namespace  // Concrete FSM implementation
21 {
22     // events
23     struct play {};
24     struct end_pause {};
25     struct stop {};
26     struct pause {};
27     struct open_close {};
28     struct NextSong {};
29     struct PreviousSong {};
30 
31     // Flags. Allow information about a property of the current state
32     struct PlayingPaused{};
33     struct CDLoaded {};
34     struct FirstSongPlaying {};
35 
36     // A "complicated" event type that carries some data.
37     struct cd_detected
38     {
cd_detected__anon9a40eeec0111::cd_detected39         cd_detected(std::string name)
40             : name(name)
41         {}
42 
43         std::string name;
44     };
45 
46     // front-end: define the FSM structure
47     struct player_ : public msm::front::state_machine_def<player_>
48     {
49         // The list of FSM states
50         struct Empty : public msm::front::state<>
51         {
52             // every (optional) entry/exit methods get the event passed.
53             template <class Event,class FSM>
on_entry__anon9a40eeec0111::player_::Empty54             void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
55             template <class Event,class FSM>
on_exit__anon9a40eeec0111::player_::Empty56             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
57         };
58         struct Open : public msm::front::state<>
59         {
60             typedef mpl::vector1<CDLoaded>      flag_list;
61             template <class Event,class FSM>
on_entry__anon9a40eeec0111::player_::Open62             void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;}
63             template <class Event,class FSM>
on_exit__anon9a40eeec0111::player_::Open64             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
65         };
66 
67         struct Stopped : public msm::front::state<>
68         {
69             // when stopped, the CD is loaded
70             typedef mpl::vector1<CDLoaded>      flag_list;
71             template <class Event,class FSM>
on_entry__anon9a40eeec0111::player_::Stopped72             void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;}
73             template <class Event,class FSM>
on_exit__anon9a40eeec0111::player_::Stopped74             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
75         };
76 
77         // the player state machine contains a state which is himself a state machine
78         // as you see, no need to declare it anywhere so Playing can be developed separately
79         // by another team in another module. For simplicity I just declare it inside player
80         struct Playing_ : public msm::front::state_machine_def<Playing_>
81         {
82             // when playing, the CD is loaded and we are in either pause or playing (duh)
83             typedef mpl::vector2<PlayingPaused,CDLoaded>        flag_list;
84 
85             template <class Event,class FSM>
on_entry__anon9a40eeec0111::player_::Playing_86             void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
87             template <class Event,class FSM>
on_exit__anon9a40eeec0111::player_::Playing_88             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
89 
90             // The list of FSM states
91             struct Song1 : public msm::front::state<>
92             {
93                 typedef mpl::vector1<FirstSongPlaying>      flag_list;
94                 template <class Event,class FSM>
on_entry__anon9a40eeec0111::player_::Playing_::Song195                 void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
96                 template <class Event,class FSM>
on_exit__anon9a40eeec0111::player_::Playing_::Song197                 void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
98 
99             };
100             struct Song2 : public msm::front::state<>
101             {
102                 template <class Event,class FSM>
on_entry__anon9a40eeec0111::player_::Playing_::Song2103                 void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
104                 template <class Event,class FSM>
on_exit__anon9a40eeec0111::player_::Playing_::Song2105                 void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
106             };
107             struct Song3 : public msm::front::state<>
108             {
109                 template <class Event,class FSM>
on_entry__anon9a40eeec0111::player_::Playing_::Song3110                 void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
111                 template <class Event,class FSM>
on_exit__anon9a40eeec0111::player_::Playing_::Song3112                 void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
113             };
114             // the initial state. Must be defined
115             typedef Song1 initial_state;
116             // transition actions
start_next_song__anon9a40eeec0111::player_::Playing_117             void start_next_song(NextSong const&)       { std::cout << "Playing::start_next_song\n"; }
start_prev_song__anon9a40eeec0111::player_::Playing_118             void start_prev_song(PreviousSong const&)       { std::cout << "Playing::start_prev_song\n"; }
119             // guard conditions
120 
121             typedef Playing_ pl; // makes transition table cleaner
122             // Transition table for Playing
123             struct transition_table : mpl::vector4<
124                 //      Start     Event         Next      Action               Guard
125                 //    +---------+-------------+---------+---------------------+----------------------+
126                 a_row < Song1   , NextSong    , Song2   , &pl::start_next_song                       >,
127                 a_row < Song2   , PreviousSong, Song1   , &pl::start_prev_song                       >,
128                 a_row < Song2   , NextSong    , Song3   , &pl::start_next_song                       >,
129                 a_row < Song3   , PreviousSong, Song2   , &pl::start_prev_song                       >
130                 //    +---------+-------------+---------+---------------------+----------------------+
131             > {};
132             // Replaces the default no-transition response.
133             template <class FSM,class Event>
no_transition__anon9a40eeec0111::player_::Playing_134             void no_transition(Event const& e, FSM&,int state)
135             {
136                 std::cout << "no transition from state " << state
137                     << " on event " << typeid(e).name() << std::endl;
138             }
139         };
140         // back-end
141         typedef msm::back::state_machine<Playing_> Playing;
142 
143         // state not defining any entry or exit
144         struct Paused : public msm::front::state<>
145         {
146             typedef mpl::vector2<PlayingPaused,CDLoaded>        flag_list;
147         };
148 
149         // the initial state of the player SM. Must be defined
150         typedef Empty initial_state;
151 
152         // transition actions
start_playback__anon9a40eeec0111::player_153         void start_playback(play const&)       { std::cout << "player::start_playback\n"; }
open_drawer__anon9a40eeec0111::player_154         void open_drawer(open_close const&)    { std::cout << "player::open_drawer\n"; }
close_drawer__anon9a40eeec0111::player_155         void close_drawer(open_close const&)   { std::cout << "player::close_drawer\n"; }
store_cd_info__anon9a40eeec0111::player_156         void store_cd_info(cd_detected const& cd) {std::cout << "player::store_cd_info\n";}
stop_playback__anon9a40eeec0111::player_157         void stop_playback(stop const&)        { std::cout << "player::stop_playback\n"; }
pause_playback__anon9a40eeec0111::player_158         void pause_playback(pause const&)      { std::cout << "player::pause_playback\n"; }
resume_playback__anon9a40eeec0111::player_159         void resume_playback(end_pause const&)      { std::cout << "player::resume_playback\n"; }
stop_and_open__anon9a40eeec0111::player_160         void stop_and_open(open_close const&)  { std::cout << "player::stop_and_open\n"; }
stopped_again__anon9a40eeec0111::player_161         void stopped_again(stop const&){std::cout << "player::stopped_again\n";}
162         // guard conditions
163 
164         typedef player_ p; // makes transition table cleaner
165 
166         // Transition table for player
167         struct transition_table : mpl::vector<
168             //      Start     Event         Next      Action               Guard
169             //    +---------+-------------+---------+---------------------+----------------------+
170             a_row < Stopped , play        , Playing , &p::start_playback                         >,
171             a_row < Stopped , open_close  , Open    , &p::open_drawer                            >,
172             a_row < Stopped , stop        , Stopped , &p::stopped_again                          >,
173             //    +---------+-------------+---------+---------------------+----------------------+
174             a_row < Open    , open_close  , Empty   , &p::close_drawer                           >,
175             //    +---------+-------------+---------+---------------------+----------------------+
176             a_row < Empty   , open_close  , Open    , &p::open_drawer                            >,
177             a_row < Empty   , cd_detected , Stopped , &p::store_cd_info                          >,
178             //    +---------+-------------+---------+---------------------+----------------------+
179             a_row < Playing , stop        , Stopped , &p::stop_playback                          >,
180             a_row < Playing , pause       , Paused  , &p::pause_playback                         >,
181             a_row < Playing , open_close  , Open    , &p::stop_and_open                          >,
182             //    +---------+-------------+---------+---------------------+----------------------+
183             a_row < Paused  , end_pause   , Playing , &p::resume_playback                        >,
184             a_row < Paused  , stop        , Stopped , &p::stop_playback                          >,
185             a_row < Paused  , open_close  , Open    , &p::stop_and_open                          >
186             //    +---------+-------------+---------+---------------------+----------------------+
187         > {};
188 
189         // Replaces the default no-transition response.
190         template <class FSM,class Event>
no_transition__anon9a40eeec0111::player_191         void no_transition(Event const& e, FSM&,int state)
192         {
193             std::cout << "no transition from state " << state
194                 << " on event " << typeid(e).name() << std::endl;
195         }
196     };
197     // Pick a back-end
198     typedef msm::back::state_machine<player_> player;
199 
200     //
201     // Testing utilities.
202     //
203     static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
204 
pstate(player const & p)205     void pstate(player const& p)
206     {
207         std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
208     }
209 
test()210     void test()
211     {
212         player p;
213 
214         // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
215         p.start();
216         // tests some flags
217         std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl; //=> false (no CD yet)
218         // go to Open, call on_exit on Empty, then action, then on_entry on Open
219         p.process_event(open_close()); pstate(p);
220         p.process_event(open_close()); pstate(p);
221         p.process_event(cd_detected("louie, louie"));
222         p.process_event(play());
223 
224         // at this point, Play is active
225         std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
226         std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> true
227 
228         // make transition happen inside it. Player has no idea about this event but it's ok.
229         p.process_event(NextSong());pstate(p); //2nd song active
230         p.process_event(NextSong());pstate(p);//3rd song active
231         p.process_event(PreviousSong());pstate(p);//2nd song active
232         std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false
233 
234         std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
235         p.process_event(pause()); pstate(p);
236         std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
237         // go back to Playing
238         // as you see, it starts back from the original state
239         p.process_event(end_pause());  pstate(p);
240         p.process_event(pause()); pstate(p);
241         p.process_event(stop());  pstate(p);
242         std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> false
243         std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl;//=> true
244 
245         // event leading to the same state
246         p.process_event(stop());  pstate(p);
247     }
248 }
249 
main()250 int main()
251 {
252     test();
253     return 0;
254 }
255