• 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 // functors
17 #include <boost/msm/front/functor_row.hpp>
18 #include <boost/msm/front/euml/common.hpp>
19 
20 namespace msm = boost::msm;
21 namespace mpl = boost::mpl;
22 using namespace boost::msm::front;
23 
24 namespace
25 {
26     // events
27     struct event1 {};
28 
29 
30     // front-end: define the FSM structure
31     struct my_machine_ : public msm::front::state_machine_def<my_machine_>
32     {
33         // The list of FSM states
34         struct State1 : public msm::front::state<>
35         {
36             // every (optional) entry/exit methods get the event passed.
37             template <class Event,class FSM>
on_entry__anon49ec38690111::my_machine_::State138             void on_entry(Event const&,FSM& ) {std::cout << "entering: State1" << std::endl;}
39             template <class Event,class FSM>
on_exit__anon49ec38690111::my_machine_::State140             void on_exit(Event const&,FSM& ) {std::cout << "leaving: State1" << std::endl;}
41         };
42         struct State2 : public msm::front::state<>
43         {
44             template <class Event,class FSM>
on_entry__anon49ec38690111::my_machine_::State245             void on_entry(Event const& ,FSM&) {std::cout << "entering: State2" << std::endl;}
46             template <class Event,class FSM>
on_exit__anon49ec38690111::my_machine_::State247             void on_exit(Event const&,FSM& ) {std::cout << "leaving: State2" << std::endl;}
48         };
49 
50         struct State3 : public msm::front::state<>
51         {
52             // when stopped, the CD is loaded
53             template <class Event,class FSM>
on_entry__anon49ec38690111::my_machine_::State354             void on_entry(Event const& ,FSM&) {std::cout << "entering: State3" << std::endl;}
55             template <class Event,class FSM>
on_exit__anon49ec38690111::my_machine_::State356             void on_exit(Event const&,FSM& ) {std::cout << "leaving: State3" << std::endl;}
57         };
58 
59         struct State4 : public msm::front::state<>
60         {
61             template <class Event,class FSM>
on_entry__anon49ec38690111::my_machine_::State462             void on_entry(Event const&,FSM& ) {std::cout << "entering: State4" << std::endl;}
63             template <class Event,class FSM>
on_exit__anon49ec38690111::my_machine_::State464             void on_exit(Event const&,FSM& ) {std::cout << "leaving: State4" << std::endl;}
65         };
66 
67         // the initial state of the player SM. Must be defined
68         typedef State1 initial_state;
69 
70         // transition actions
71         struct State2ToState3
72         {
73             template <class EVT,class FSM,class SourceState,class TargetState>
operator ()__anon49ec38690111::my_machine_::State2ToState374             void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
75             {
76                 std::cout << "my_machine::State2ToState3" << std::endl;
77             }
78         };
79         struct State3ToState4
80         {
81             template <class EVT,class FSM,class SourceState,class TargetState>
operator ()__anon49ec38690111::my_machine_::State3ToState482             void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
83             {
84                 std::cout << "my_machine::State3ToState4" << std::endl;
85             }
86         };
87         // guard conditions
88         struct always_true
89         {
90             template <class EVT,class FSM,class SourceState,class TargetState>
operator ()__anon49ec38690111::my_machine_::always_true91             bool operator()(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt)
92             {
93                 std::cout << "always_true" << std::endl;
94                 return true;
95             }
96         };
97         struct always_false
98         {
99             template <class EVT,class FSM,class SourceState,class TargetState>
operator ()__anon49ec38690111::my_machine_::always_false100             bool operator()(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt)
101             {
102                 std::cout << "always_false" << std::endl;
103                 return true;
104             }
105         };
106 
107         typedef my_machine_ p; // makes transition table cleaner
108 
109         // Transition table for player
110         struct transition_table : mpl::vector<
111             //    Start     Event         Next      Action               Guard
112             //  +---------+-------------+---------+---------------------+----------------------+
113             Row < State1  , none        , State2                                               >,
114             Row < State2  , none        , State3  , State2ToState3                             >,
115             Row < State3  , none        , State4  , none                , always_false         >,
116             //  +---------+-------------+---------+---------------------+----------------------+
117             Row < State3  , none        , State4  , State3ToState4      , always_true          >,
118             Row < State4  , event1      , State1                                               >
119             //  +---------+-------------+---------+---------------------+----------------------+
120         > {};
121         // Replaces the default no-transition response.
122         template <class FSM,class Event>
no_transition__anon49ec38690111::my_machine_123         void no_transition(Event const& e, FSM&,int state)
124         {
125             std::cout << "no transition from state " << state
126                 << " on event " << typeid(e).name() << std::endl;
127         }
128     };
129     // Pick a back-end
130     typedef msm::back::state_machine<my_machine_> my_machine;
131 
132     //
133     // Testing utilities.
134     //
135     static char const* const state_names[] = { "State1", "State2", "State3", "State4" };
pstate(my_machine const & p)136     void pstate(my_machine const& p)
137     {
138         std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
139     }
140 
test()141     void test()
142     {
143         my_machine p;
144 
145         // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
146         // in this case it will also immediately trigger all anonymous transitions
147         p.start();
148         // this event will bring us back to the initial state and thus, a new "loop" will be started
149         p.process_event(event1());
150 
151     }
152 }
153 
main()154 int main()
155 {
156     test();
157     return 0;
158 }
159