• 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
21 {
22     // events
23     struct play {};
24     struct end_pause {};
25     struct stop {};
26     struct pause {};
27     struct open_close {};
28 
29     // A "complicated" event type that carries some data.
30 	enum DiskTypeEnum
31     {
32         DISK_CD=0,
33         DISK_DVD=1
34     };
35     struct cd_detected
36     {
cd_detected__anonc4f2d7930111::cd_detected37         cd_detected(std::string name, DiskTypeEnum diskType)
38             : name(name),
39             disc_type(diskType)
40         {}
41 
42         std::string name;
43         DiskTypeEnum disc_type;
44     };
45 
46     // front-end: define the FSM structure
47     struct player_ : public msm::front::state_machine_def<player_>
48     {
49         template <class Event,class FSM>
on_entry__anonc4f2d7930111::player_50         void on_entry(Event const& ,FSM&)
51         {
52             std::cout << "entering: Player" << std::endl;
53         }
54         template <class Event,class FSM>
on_exit__anonc4f2d7930111::player_55         void on_exit(Event const&,FSM& )
56         {
57             std::cout << "leaving: Player" << std::endl;
58         }
59 
60         // The list of FSM states
61         struct Empty : public msm::front::state<>
62         {
63             // every (optional) entry/exit methods get the event passed.
64             template <class Event,class FSM>
on_entry__anonc4f2d7930111::player_::Empty65             void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
66             template <class Event,class FSM>
on_exit__anonc4f2d7930111::player_::Empty67             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
68         };
69         struct Open : public msm::front::state<>
70         {
71             template <class Event,class FSM>
on_entry__anonc4f2d7930111::player_::Open72             void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
73             template <class Event,class FSM>
on_exit__anonc4f2d7930111::player_::Open74             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
75         };
76 
77         // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
78         struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr>
79         {
80             template <class Event,class FSM>
on_entry__anonc4f2d7930111::player_::Stopped81             void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
82             template <class Event,class FSM>
on_exit__anonc4f2d7930111::player_::Stopped83             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
set_sm_ptr__anonc4f2d7930111::player_::Stopped84             void set_sm_ptr(player_* pl)
85             {
86                 m_player=pl;
87             }
88             player_* m_player;
89         };
90 
91         struct Playing : public msm::front::state<>
92         {
93             template <class Event,class FSM>
on_entry__anonc4f2d7930111::player_::Playing94             void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
95             template <class Event,class FSM>
on_exit__anonc4f2d7930111::player_::Playing96             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
97         };
98 
99         // state not defining any entry or exit
100         struct Paused : public msm::front::state<>
101         {
102         };
103 
104         // the initial state of the player SM. Must be defined
105         typedef Empty initial_state;
106 
107         // transition actions
start_playback__anonc4f2d7930111::player_108         void start_playback(play const&)       { std::cout << "player::start_playback\n"; }
open_drawer__anonc4f2d7930111::player_109         void open_drawer(open_close const&)    { std::cout << "player::open_drawer\n"; }
close_drawer__anonc4f2d7930111::player_110         void close_drawer(open_close const&)   { std::cout << "player::close_drawer\n"; }
store_cd_info__anonc4f2d7930111::player_111         void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
stop_playback__anonc4f2d7930111::player_112         void stop_playback(stop const&)        { std::cout << "player::stop_playback\n"; }
pause_playback__anonc4f2d7930111::player_113         void pause_playback(pause const&)      { std::cout << "player::pause_playback\n"; }
resume_playback__anonc4f2d7930111::player_114         void resume_playback(end_pause const&)      { std::cout << "player::resume_playback\n"; }
stop_and_open__anonc4f2d7930111::player_115         void stop_and_open(open_close const&)  { std::cout << "player::stop_and_open\n"; }
stopped_again__anonc4f2d7930111::player_116         void stopped_again(stop const&)	{std::cout << "player::stopped_again\n";}
117         // guard conditions
good_disk_format__anonc4f2d7930111::player_118         bool good_disk_format(cd_detected const& evt)
119         {
120             // to test a guard condition, let's say we understand only CDs, not DVD
121             if (evt.disc_type != DISK_CD)
122             {
123                 std::cout << "wrong disk, sorry" << std::endl;
124                 return false;
125             }
126             return true;
127         }
128         // used to show a transition conflict. This guard will simply deactivate one transition and thus
129         // solve the conflict
auto_start__anonc4f2d7930111::player_130         bool auto_start(cd_detected const&)
131         {
132             return false;
133         }
134 
135         typedef player_ p; // makes transition table cleaner
136 
137         // Transition table for player
138         struct transition_table : mpl::vector<
139             //    Start     Event         Next      Action				 Guard
140             //  +---------+-------------+---------+---------------------+----------------------+
141           a_row < Stopped , play        , Playing , &p::start_playback                         >,
142           a_row < Stopped , open_close  , Open    , &p::open_drawer                            >,
143            _row < Stopped , stop        , Stopped                                              >,
144             //  +---------+-------------+---------+---------------------+----------------------+
145           a_row < Open    , open_close  , Empty   , &p::close_drawer                           >,
146             //  +---------+-------------+---------+---------------------+----------------------+
147           a_row < Empty   , open_close  , Open    , &p::open_drawer                            >,
148             row < Empty   , cd_detected , Stopped , &p::store_cd_info   ,&p::good_disk_format  >,
149             row < Empty   , cd_detected , Playing , &p::store_cd_info   ,&p::auto_start        >,
150             //  +---------+-------------+---------+---------------------+----------------------+
151           a_row < Playing , stop        , Stopped , &p::stop_playback                          >,
152           a_row < Playing , pause       , Paused  , &p::pause_playback                         >,
153           a_row < Playing , open_close  , Open    , &p::stop_and_open                          >,
154             //  +---------+-------------+---------+---------------------+----------------------+
155           a_row < Paused  , end_pause   , Playing , &p::resume_playback                        >,
156           a_row < Paused  , stop        , Stopped , &p::stop_playback                          >,
157           a_row < Paused  , open_close  , Open    , &p::stop_and_open                          >
158             //  +---------+-------------+---------+---------------------+----------------------+
159         > {};
160         // Replaces the default no-transition response.
161         template <class FSM,class Event>
no_transition__anonc4f2d7930111::player_162         void no_transition(Event const& e, FSM&,int state)
163         {
164             std::cout << "no transition from state " << state
165                 << " on event " << typeid(e).name() << std::endl;
166         }
167     };
168     // Pick a back-end
169     typedef msm::back::state_machine<player_> player;
170 
171     //
172     // Testing utilities.
173     //
174     static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
pstate(player const & p)175     void pstate(player const& p)
176     {
177         std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
178     }
179 
test()180     void test()
181     {
182 		player p;
183         // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
184         p.start();
185         // go to Open, call on_exit on Empty, then action, then on_entry on Open
186         p.process_event(open_close()); pstate(p);
187         p.process_event(open_close()); pstate(p);
188         // will be rejected, wrong disk type
189         p.process_event(
190             cd_detected("louie, louie",DISK_DVD)); pstate(p);
191         p.process_event(
192             cd_detected("louie, louie",DISK_CD)); pstate(p);
193 		p.process_event(play());
194 
195         // at this point, Play is active
196         p.process_event(pause()); pstate(p);
197         // go back to Playing
198         p.process_event(end_pause());  pstate(p);
199         p.process_event(pause()); pstate(p);
200         p.process_event(stop());  pstate(p);
201         // event leading to the same state
202         // no action method called as it is not present in the transition table
203         p.process_event(stop());  pstate(p);
204         std::cout << "stop fsm" << std::endl;
205         p.stop();
206     }
207 }
208 
main()209 int main()
210 {
211     test();
212     return 0;
213 }
214