]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/HTML/examples/AnonymousTutorialEuml.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>
14 #include <boost/msm/front/euml/euml.hpp>
16 namespace msm
= boost::msm
;
17 using namespace boost::msm::front::euml
;
22 BOOST_MSM_EUML_EVENT(event1
)
24 BOOST_MSM_EUML_ACTION(State1_Entry
)
26 template <class Event
,class FSM
,class STATE
>
27 void operator()(Event
const&,FSM
&,STATE
& )
29 std::cout
<< "entering: State1" << std::endl
;
32 BOOST_MSM_EUML_ACTION(State1_Exit
)
34 template <class Event
,class FSM
,class STATE
>
35 void operator()(Event
const&,FSM
&,STATE
& )
37 std::cout
<< "leaving: State1" << std::endl
;
40 BOOST_MSM_EUML_ACTION(State2_Entry
)
42 template <class Event
,class FSM
,class STATE
>
43 void operator()(Event
const&,FSM
&,STATE
& )
45 std::cout
<< "entering: State2" << std::endl
;
48 BOOST_MSM_EUML_ACTION(State2_Exit
)
50 template <class Event
,class FSM
,class STATE
>
51 void operator()(Event
const&,FSM
&,STATE
& )
53 std::cout
<< "leaving: State2" << std::endl
;
56 BOOST_MSM_EUML_ACTION(State3_Entry
)
58 template <class Event
,class FSM
,class STATE
>
59 void operator()(Event
const&,FSM
&,STATE
& )
61 std::cout
<< "entering: State3" << std::endl
;
64 BOOST_MSM_EUML_ACTION(State3_Exit
)
66 template <class Event
,class FSM
,class STATE
>
67 void operator()(Event
const&,FSM
&,STATE
& )
69 std::cout
<< "leaving: State3" << std::endl
;
72 BOOST_MSM_EUML_ACTION(State4_Entry
)
74 template <class Event
,class FSM
,class STATE
>
75 void operator()(Event
const&,FSM
&,STATE
& )
77 std::cout
<< "entering: State4" << std::endl
;
80 BOOST_MSM_EUML_ACTION(State4_Exit
)
82 template <class Event
,class FSM
,class STATE
>
83 void operator()(Event
const&,FSM
&,STATE
& )
85 std::cout
<< "leaving: State4" << std::endl
;
89 // The list of FSM states
90 BOOST_MSM_EUML_STATE(( State1_Entry
,State1_Exit
),State1
)
91 BOOST_MSM_EUML_STATE(( State2_Entry
,State2_Exit
),State2
)
92 BOOST_MSM_EUML_STATE(( State3_Entry
,State3_Exit
),State3
)
93 BOOST_MSM_EUML_STATE(( State4_Entry
,State4_Exit
),State4
)
96 BOOST_MSM_EUML_ACTION(State2ToState3
)
98 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
99 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
101 std::cout
<< "my_machine::State2ToState3" << std::endl
;
104 BOOST_MSM_EUML_ACTION(State3ToState4
)
106 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
107 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
109 std::cout
<< "my_machine::State3ToState4" << std::endl
;
113 BOOST_MSM_EUML_ACTION(always_true
)
115 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
116 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
118 std::cout
<< "always_true" << std::endl
;
122 BOOST_MSM_EUML_ACTION(always_false
)
124 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
125 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
127 std::cout
<< "always_false" << std::endl
;
131 // replaces the old transition table
132 BOOST_MSM_EUML_TRANSITION_TABLE((
134 State3
== State2
/ State2ToState3
,
135 State4
== State3
[always_true
] / State3ToState4
,
136 State4
== State3
[always_false
],
137 State1
== State4
+ event1
138 // +------------------------------------------------------------------------------+
141 // create a state machine "on the fly"
142 BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table
, //STT
143 init_
<< State1
// Init State
145 my_machine_
) //fsm name
148 typedef msm::back::state_machine
<my_machine_
> my_machine
;
151 // Testing utilities.
153 static char const* const state_names
[] = { "State1", "State2", "State3", "State4" };
154 void pstate(my_machine
const& p
)
156 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
163 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
164 // in this case it will also immediately trigger all anonymous transitions
166 // this event will bring us back to the initial state and thus, a new "loop" will be started
167 p
.process_event(event1
);