]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/AnonymousTutorial.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 namespace msm
= boost::msm
;
18 namespace mpl
= boost::mpl
;
19 using namespace boost::msm::front
;
27 // front-end: define the FSM structure
28 struct my_machine_
: public msm::front::state_machine_def
<my_machine_
>
30 // The list of FSM states
31 struct State1
: public msm::front::state
<>
33 // every (optional) entry/exit methods get the event passed.
34 template <class Event
,class FSM
>
35 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: State1" << std::endl
;}
36 template <class Event
,class FSM
>
37 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: State1" << std::endl
;}
39 struct State2
: public msm::front::state
<>
41 template <class Event
,class FSM
>
42 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: State2" << std::endl
;}
43 template <class Event
,class FSM
>
44 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: State2" << std::endl
;}
47 struct State3
: public msm::front::state
<>
49 // when stopped, the CD is loaded
50 template <class Event
,class FSM
>
51 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: State3" << std::endl
;}
52 template <class Event
,class FSM
>
53 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: State3" << std::endl
;}
56 struct State4
: public msm::front::state
<>
58 template <class Event
,class FSM
>
59 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: State4" << std::endl
;}
60 template <class Event
,class FSM
>
61 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: State4" << std::endl
;}
64 // the initial state of the player SM. Must be defined
65 typedef State1 initial_state
;
68 void State2ToState3(none
const&) { std::cout
<< "my_machine::State2ToState3\n"; }
69 void State3ToState4(none
const&) { std::cout
<< "my_machine::State3ToState4\n"; }
71 bool always_true(none
const& evt
)
73 std::cout
<< "always_true" << std::endl
;
76 bool always_false(none
const& evt
)
78 std::cout
<< "always_false" << std::endl
;
82 typedef my_machine_ p
; // makes transition table cleaner
84 // Transition table for player
85 struct transition_table
: mpl::vector
<
86 // Start Event Next Action Guard
87 // +---------+-------------+---------+---------------------+----------------------+
88 _row
< State1
, none
, State2
>,
89 a_row
< State2
, none
, State3
, &p::State2ToState3
>,
90 // +---------+-------------+---------+---------------------+----------------------+
91 row
< State3
, none
, State4
, &p::State3ToState4
, &p::always_true
>,
92 g_row
< State3
, none
, State4
, &p::always_false
>,
93 _row
< State4
, event1
, State1
>
94 // +---------+-------------+---------+---------------------+----------------------+
96 // Replaces the default no-transition response.
97 template <class FSM
,class Event
>
98 void no_transition(Event
const& e
, FSM
&,int state
)
100 std::cout
<< "no transition from state " << state
101 << " on event " << typeid(e
).name() << std::endl
;
105 typedef msm::back::state_machine
<my_machine_
> my_machine
;
108 // Testing utilities.
110 static char const* const state_names
[] = { "State1", "State2", "State3", "State4" };
111 void pstate(my_machine
const& p
)
113 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
120 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
121 // in this case it will also immediately trigger all anonymous transitions
123 // this event will bring us back to the initial state and thus, a new "loop" will be started
124 p
.process_event(event1());