]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/SimpleTutorialEuml.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / msm / doc / PDF / examples / SimpleTutorialEuml.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)
10
11 #include <vector>
12 #include <iostream>
13
14 #include <boost/msm/back/state_machine.hpp>
15 #include <boost/msm/front/euml/euml.hpp>
16
17 using namespace std;
18 using namespace boost::msm::front::euml;
19 namespace msm = boost::msm;
20
21 // entry/exit/action/guard logging functors
22 #include "logging_functors.h"
23
24 namespace // Concrete FSM implementation
25 {
26 // events
27 BOOST_MSM_EUML_EVENT(play)
28 BOOST_MSM_EUML_EVENT(end_pause)
29 BOOST_MSM_EUML_EVENT(stop)
30 BOOST_MSM_EUML_EVENT(pause)
31 BOOST_MSM_EUML_EVENT(open_close)
32
33 // A "complicated" event type that carries some data.
34 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
35 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
36 BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
37 BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
38
39 // Concrete FSM implementation
40 // The list of FSM states
41 // state not needing any entry or exit
42 BOOST_MSM_EUML_STATE((),Paused)
43
44 // it is also possible to define a state which you can implement normally
45 // just make it a state, as usual, and also a grammar terminal, euml_state
46 struct Empty_impl : public msm::front::state<> , public euml_state<Empty_impl>
47 {
48 // this allows us to add some functions
49 void activate_empty() {std::cout << "switching to Empty " << std::endl;}
50 // standard entry behavior
51 template <class Event,class FSM>
52 void on_entry(Event const& evt,FSM& fsm)
53 {
54 std::cout << "entering: Empty" << std::endl;
55 }
56 template <class Event,class FSM>
57 void on_exit(Event const& evt,FSM& fsm)
58 {
59 std::cout << "leaving: Empty" << std::endl;
60 }
61 };
62 //instance for use in the transition table
63 Empty_impl const Empty;
64
65 // create a functor and a eUML function for the activate_empty method from Entry
66 BOOST_MSM_EUML_METHOD(ActivateEmpty_ , activate_empty , activate_empty_ , void , void )
67
68 // define more states
69 BOOST_MSM_EUML_STATE(( Open_Entry,Open_Exit ),Open)
70 BOOST_MSM_EUML_STATE(( Stopped_Entry,Stopped_Exit ),Stopped)
71 BOOST_MSM_EUML_STATE(( Playing_Entry,Playing_Exit ),Playing)
72
73
74 // guard conditions
75 BOOST_MSM_EUML_ACTION(good_disk_format)
76 {
77 template <class FSM,class EVT,class SourceState,class TargetState>
78 bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
79 {
80 // to test a guard condition, let's say we understand only CDs, not DVD
81 if (evt.get_attribute(cd_type)!=DISK_CD)
82 {
83 std::cout << "wrong disk, sorry" << std::endl;
84 // just for logging, does not block any transition
85 return true;
86 }
87 std::cout << "good disk" << std::endl;
88 return true;
89 }
90 };
91 // it is also possible to use a plain functor, with default-constructor in the transition table
92 struct start_play
93 {
94 template <class FSM,class EVT,class SourceState,class TargetState>
95 void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
96 {
97 cout << "player::start_play" << endl;
98 }
99 };
100 // replaces the old transition table
101 BOOST_MSM_EUML_TRANSITION_TABLE((
102 Playing == Stopped + play / start_play() ,
103 Playing == Paused + end_pause / resume_playback,
104 // +------------------------------------------------------------------------------+
105 Empty == Open + open_close / (close_drawer,activate_empty_(target_)),
106 // +------------------------------------------------------------------------------+
107 Open == Empty + open_close / open_drawer,
108 Open == Paused + open_close / stop_and_open,
109 Open == Stopped + open_close / open_drawer,
110 Open == Playing + open_close / stop_and_open,
111 // +------------------------------------------------------------------------------+
112 Paused == Playing + pause / pause_playback,
113 // +------------------------------------------------------------------------------+
114 Stopped == Playing + stop / stop_playback,
115 Stopped == Paused + stop / stop_playback,
116 Stopped == Empty + cd_detected [good_disk_format &&
117 (event_(cd_type)==Int_<DISK_CD>())]
118 / (store_cd_info,process_(play)),
119 Stopped == Stopped + stop
120 // +------------------------------------------------------------------------------+
121 ),transition_table)
122
123 // create a state machine "on the fly"
124 BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
125 init_ << Empty, // Init State
126 no_action, // Entry
127 no_action, // Exit
128 attributes_ << no_attributes_, // Attributes
129 configure_ << no_configure_, // configuration
130 Log_No_Transition // no_transition handler
131 ),
132 player_) //fsm name
133
134 // or simply, if no no_transition handler needed:
135 //BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
136 // Empty // Init State
137 // ),player_)
138
139 // choice of back-end
140 typedef msm::back::state_machine<player_> player;
141
142 //
143 // Testing utilities.
144 //
145 static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
146 void pstate(player const& p)
147 {
148 std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
149 }
150
151 void test()
152 {
153 player p;
154 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
155 p.start();
156 // note that we write open_close and not open_close(), like usual. Both are possible with eUML, but
157 // you now have less to type.
158 // go to Open, call on_exit on Empty, then action, then on_entry on Open
159 p.process_event(open_close); pstate(p);
160 p.process_event(open_close); pstate(p);
161 // will be rejected, wrong disk type
162 p.process_event(
163 cd_detected("louie, louie",DISK_DVD)); pstate(p);
164 p.process_event(
165 cd_detected("louie, louie",DISK_CD)); pstate(p);
166 // no need to call play as the previous event does it in its action method
167 //p.process_event(play);
168
169 // at this point, Play is active
170 p.process_event(pause); pstate(p);
171 // go back to Playing
172 p.process_event(end_pause); pstate(p);
173 p.process_event(pause); pstate(p);
174 p.process_event(stop); pstate(p);
175 // event leading to the same state
176 // no action method called as none is defined in the transition table
177 p.process_event(stop); pstate(p);
178 // test call to no_transition
179 p.process_event(pause); pstate(p);
180 }
181 }
182
183 int main()
184 {
185 test();
186 return 0;
187 }