]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/HTML/examples/AnonymousTutorialWithFunctors.cpp
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)
13 #include <boost/msm/back/state_machine.hpp>
15 #include <boost/msm/front/state_machine_def.hpp>
17 #include <boost/msm/front/functor_row.hpp>
18 #include <boost/msm/front/euml/common.hpp>
20 namespace msm
= boost::msm
;
21 namespace mpl
= boost::mpl
;
22 using namespace boost::msm::front
;
30 // front-end: define the FSM structure
31 struct my_machine_
: public msm::front::state_machine_def
<my_machine_
>
33 // The list of FSM states
34 struct State1
: public msm::front::state
<>
36 // every (optional) entry/exit methods get the event passed.
37 template <class Event
,class FSM
>
38 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: State1" << std::endl
;}
39 template <class Event
,class FSM
>
40 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: State1" << std::endl
;}
42 struct State2
: public msm::front::state
<>
44 template <class Event
,class FSM
>
45 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: State2" << std::endl
;}
46 template <class Event
,class FSM
>
47 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: State2" << std::endl
;}
50 struct State3
: public msm::front::state
<>
52 // when stopped, the CD is loaded
53 template <class Event
,class FSM
>
54 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: State3" << std::endl
;}
55 template <class Event
,class FSM
>
56 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: State3" << std::endl
;}
59 struct State4
: public msm::front::state
<>
61 template <class Event
,class FSM
>
62 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: State4" << std::endl
;}
63 template <class Event
,class FSM
>
64 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: State4" << std::endl
;}
67 // the initial state of the player SM. Must be defined
68 typedef State1 initial_state
;
73 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
74 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
76 std::cout
<< "my_machine::State2ToState3" << std::endl
;
81 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
82 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
84 std::cout
<< "my_machine::State3ToState4" << std::endl
;
90 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
91 bool operator()(EVT
const& evt
,FSM
& fsm
,SourceState
& src
,TargetState
& tgt
)
93 std::cout
<< "always_true" << std::endl
;
99 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
100 bool operator()(EVT
const& evt
,FSM
& fsm
,SourceState
& src
,TargetState
& tgt
)
102 std::cout
<< "always_false" << std::endl
;
107 typedef my_machine_ p
; // makes transition table cleaner
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 // +---------+-------------+---------+---------------------+----------------------+
121 // Replaces the default no-transition response.
122 template <class FSM
,class Event
>
123 void no_transition(Event
const& e
, FSM
&,int state
)
125 std::cout
<< "no transition from state " << state
126 << " on event " << typeid(e
).name() << std::endl
;
130 typedef msm::back::state_machine
<my_machine_
> my_machine
;
133 // Testing utilities.
135 static char const* const state_names
[] = { "State1", "State2", "State3", "State4" };
136 void pstate(my_machine
const& p
)
138 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
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
148 // this event will bring us back to the initial state and thus, a new "loop" will be started
149 p
.process_event(event1());