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 using namespace msm::front;
20 
21 namespace
22 {
23     // events
24     struct play {};
25     struct end_pause {};
26     struct stop {};
27     struct pause {};
28     struct open_close {};
29 
30     // A "complicated" event type that carries some data.
31     enum DiskTypeEnum
32     {
33         DISK_CD=0,
34         DISK_DVD=1
35     };
36     struct cd_detected
37     {
cd_detected__anonca23d1bc0111::cd_detected38         cd_detected(std::string name, DiskTypeEnum diskType)
39             : name(name),
40             disc_type(diskType)
41         {}
42 
43         std::string name;
44         DiskTypeEnum disc_type;
45     };
46 
47     // front-end: define the FSM structure
48     struct player_ : public msm::front::state_machine_def<player_>
49     {
50         // actions for Empty's internal transitions
internal_action__anonca23d1bc0111::player_51         void internal_action(cd_detected const&){ std::cout << "Empty::internal action\n"; }
internal_guard__anonca23d1bc0111::player_52         bool internal_guard(cd_detected const&)
53         {
54             std::cout << "Empty::internal guard\n";
55             return false;
56         }
57         struct internal_guard_fct
58         {
59             template <class EVT,class FSM,class SourceState,class TargetState>
operator ()__anonca23d1bc0111::player_::internal_guard_fct60             bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& )
61             {
62                 std::cout << "Empty::internal guard functor\n";
63                 return false;
64             }
65         };
66         struct internal_action_fct
67         {
68             template <class EVT,class FSM,class SourceState,class TargetState>
operator ()__anonca23d1bc0111::player_::internal_action_fct69             void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
70             {
71                 std::cout << "Empty::internal action functor" << std::endl;
72             }
73         };
74 
75         // The list of FSM states
76         struct Empty : public msm::front::state<>
77         {
78             // every (optional) entry/exit methods get the event passed.
79             template <class Event,class FSM>
on_entry__anonca23d1bc0111::player_::Empty80             void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
81             template <class Event,class FSM>
on_exit__anonca23d1bc0111::player_::Empty82             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
83 
84             // Transition table for Empty
85             struct internal_transition_table : mpl::vector<
86                 //    Start     Event         Next      Action               Guard
87            Internal <           cd_detected           , internal_action_fct ,internal_guard_fct    >
88                 //  +---------+-------------+---------+---------------------+----------------------+
89             > {};
90         };
91         struct Open : public msm::front::state<>
92         {
93             template <class Event,class FSM>
on_entry__anonca23d1bc0111::player_::Open94             void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
95             template <class Event,class FSM>
on_exit__anonca23d1bc0111::player_::Open96             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
97         };
98 
99         // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
100         struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr>
101         {
102             template <class Event,class FSM>
on_entry__anonca23d1bc0111::player_::Stopped103             void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
104             template <class Event,class FSM>
on_exit__anonca23d1bc0111::player_::Stopped105             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
set_sm_ptr__anonca23d1bc0111::player_::Stopped106             void set_sm_ptr(player_* pl)
107             {
108                 m_player=pl;
109             }
110             player_* m_player;
111         };
112 
113         struct Playing : public msm::front::state<>
114         {
115             template <class Event,class FSM>
on_entry__anonca23d1bc0111::player_::Playing116             void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
117             template <class Event,class FSM>
on_exit__anonca23d1bc0111::player_::Playing118             void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
119         };
120 
121         // state not defining any entry or exit
122         struct Paused : public msm::front::state<>
123         {
124         };
125 
126         // the initial state of the player SM. Must be defined
127         typedef Empty initial_state;
128 
129         // transition actions
start_playback__anonca23d1bc0111::player_130         void start_playback(play const&)       { std::cout << "player::start_playback\n"; }
open_drawer__anonca23d1bc0111::player_131         void open_drawer(open_close const&)    { std::cout << "player::open_drawer\n"; }
close_drawer__anonca23d1bc0111::player_132         void close_drawer(open_close const&)   { std::cout << "player::close_drawer\n"; }
store_cd_info__anonca23d1bc0111::player_133         void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
stop_playback__anonca23d1bc0111::player_134         void stop_playback(stop const&)        { std::cout << "player::stop_playback\n"; }
pause_playback__anonca23d1bc0111::player_135         void pause_playback(pause const&)      { std::cout << "player::pause_playback\n"; }
resume_playback__anonca23d1bc0111::player_136         void resume_playback(end_pause const&)      { std::cout << "player::resume_playback\n"; }
stop_and_open__anonca23d1bc0111::player_137         void stop_and_open(open_close const&)  { std::cout << "player::stop_and_open\n"; }
stopped_again__anonca23d1bc0111::player_138         void stopped_again(stop const&)        {std::cout << "player::stopped_again\n";}
139         // guard conditions
good_disk_format__anonca23d1bc0111::player_140         bool good_disk_format(cd_detected const& evt)
141         {
142             // to test a guard condition, let's say we understand only CDs, not DVD
143             if (evt.disc_type != DISK_CD)
144             {
145                 std::cout << "wrong disk, sorry" << std::endl;
146                 return false;
147             }
148             return true;
149         }
150 
151         typedef player_ p; // makes transition table cleaner
152 
153         // Transition table for player
154         struct transition_table : mpl::vector<
155             //    Start     Event         Next      Action               Guard
156             //  +---------+-------------+---------+---------------------+----------------------+
157           a_row < Stopped , play        , Playing , &p::start_playback                         >,
158           a_row < Stopped , open_close  , Open    , &p::open_drawer                            >,
159            _row < Stopped , stop        , Stopped                                              >,
160             //  +---------+-------------+---------+---------------------+----------------------+
161           a_row < Open    , open_close  , Empty   , &p::close_drawer                           >,
162             //  +---------+-------------+---------+---------------------+----------------------+
163           a_row < Empty   , open_close  , Open    , &p::open_drawer                            >,
164           // conflict between a normal and 2 internal transitions (irow/g_irow)
165             row < Empty   , cd_detected , Stopped , &p::store_cd_info   ,&p::good_disk_format  >,
166            irow < Empty   , cd_detected ,           &p::internal_action ,&p::internal_guard    >,
167          g_irow < Empty   , cd_detected                                 ,&p::internal_guard    >,
168             //  +---------+-------------+---------+---------------------+----------------------+
169           a_row < Playing , stop        , Stopped , &p::stop_playback                          >,
170           a_row < Playing , pause       , Paused  , &p::pause_playback                         >,
171           a_row < Playing , open_close  , Open    , &p::stop_and_open                          >,
172             //  +---------+-------------+---------+---------------------+----------------------+
173           a_row < Paused  , end_pause   , Playing , &p::resume_playback                        >,
174           a_row < Paused  , stop        , Stopped , &p::stop_playback                          >,
175           a_row < Paused  , open_close  , Open    , &p::stop_and_open                          >
176             //  +---------+-------------+---------+---------------------+----------------------+
177         > {};
178         // Replaces the default no-transition response.
179         template <class FSM,class Event>
no_transition__anonca23d1bc0111::player_180         void no_transition(Event const& e, FSM&,int state)
181         {
182             std::cout << "no transition from state " << state
183                 << " on event " << typeid(e).name() << std::endl;
184         }
185         typedef int no_message_queue;
186 
187     };
188     // Pick a back-end
189     typedef msm::back::state_machine<player_> player;
190 
191     //
192     // Testing utilities.
193     //
194     static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
pstate(player const & p)195     void pstate(player const& p)
196     {
197         std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
198     }
199 
test()200     void test()
201     {
202         player p;
203         // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
204         p.start();
205         // go to Open, call on_exit on Empty, then action, then on_entry on Open
206         p.process_event(open_close()); pstate(p);
207         p.process_event(open_close()); pstate(p);
208         // will be rejected, wrong disk type
209         p.process_event(
210             cd_detected("louie, louie",DISK_DVD)); pstate(p);
211         p.process_event(
212             cd_detected("louie, louie",DISK_CD)); pstate(p);
213         p.process_event(play());
214 
215         // at this point, Play is active
216         p.process_event(pause()); pstate(p);
217         // go back to Playing
218         p.process_event(end_pause());  pstate(p);
219         p.process_event(pause()); pstate(p);
220         p.process_event(stop());  pstate(p);
221         // event leading to the same state
222         // no action method called as it is not present in the transition table
223         p.process_event(stop());  pstate(p);
224     }
225 }
226 
main()227 int main()
228 {
229     test();
230     return 0;
231 }
232