]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/HTML/examples/MsmSimpleFunctors.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)
11 #include <boost/msm/back/state_machine.hpp>
12 #include <boost/msm/front/state_machine_def.hpp>
13 #include <boost/msm/front/functor_row.hpp>
15 namespace msm
= boost::msm
;
16 namespace mpl
= boost::mpl
;
17 using namespace msm::front
;
26 namespace test_fsm
// Concrete FSM implementation
36 // Concrete FSM implementation
37 struct player_
: public msm::front::state_machine_def
<player_
>
39 // no need for exception handling or message queue
40 typedef int no_exception_thrown
;
41 typedef int no_message_queue
;
43 // The list of FSM states
44 struct Empty
: public msm::front::state
<>
46 // optional entry/exit methods
47 template <class Event
,class FSM
>
48 void on_entry(Event
const&,FSM
& ) {/*std::cout << "entering: Empty" << std::endl;*/}
49 template <class Event
,class FSM
>
50 void on_exit(Event
const&,FSM
& ) {/*std::cout << "leaving: Empty" << std::endl;*/}
52 struct Open
: public msm::front::state
<>
54 template <class Event
,class FSM
>
55 void on_entry(Event
const&,FSM
& ) {/*std::cout << "entering: Open" << std::endl;*/}
56 template <class Event
,class FSM
>
57 void on_exit(Event
const&,FSM
& ) {/*std::cout << "leaving: Open" << std::endl;*/}
60 struct Stopped
: public msm::front::state
<>
62 // when stopped, the CD is loaded
63 template <class Event
,class FSM
>
64 void on_entry(Event
const&,FSM
& ) {/*std::cout << "entering: Stopped" << std::endl;*/}
65 template <class Event
,class FSM
>
66 void on_exit(Event
const&,FSM
& ) {/*std::cout << "leaving: Stopped" << std::endl;*/}
69 struct Playing
: public msm::front::state
<>
71 template <class Event
,class FSM
>
72 void on_entry(Event
const&,FSM
& ) {/*std::cout << "entering: Playing" << std::endl;*/}
73 template <class Event
,class FSM
>
74 void on_exit(Event
const&,FSM
& ) {/*std::cout << "leaving: Playing" << std::endl;*/}
77 // state not defining any entry or exit
78 struct Paused
: public msm::front::state
<>
80 template <class Event
,class FSM
>
81 void on_entry(Event
const&,FSM
& ) {/*std::cout << "entering: Paused" << std::endl;*/}
82 template <class Event
,class FSM
>
83 void on_exit(Event
const&,FSM
& ) {/*std::cout << "leaving: Paused" << std::endl;*/}
86 // the initial state of the player SM. Must be defined
87 typedef Empty initial_state
;
91 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
92 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
98 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
99 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
105 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
106 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
112 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
113 void operator()(EVT
const&, FSM
& fsm
,SourceState
& ,TargetState
& )
119 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
120 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
124 struct pause_playback
126 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
127 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
131 struct resume_playback
133 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
134 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
140 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
141 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
147 template <class FSM
,class EVT
,class SourceState
,class TargetState
>
148 void operator()(EVT
const& ,FSM
&,SourceState
& ,TargetState
& )
154 // Transition table for player
155 struct transition_table
: mpl::vector
<
156 // Start Event Next Action Guard
157 // +---------+-------------+---------+---------------------+----------------------+
158 Row
< Stopped
, play
, Playing
, start_playback
>,
159 Row
< Stopped
, open_close
, Open
, open_drawer
>,
160 Row
< Stopped
, stop
, Stopped
, stopped_again
>,
161 // +---------+-------------+---------+---------------------+----------------------+
162 Row
< Open
, open_close
, Empty
, close_drawer
>,
163 // +---------+-------------+---------+---------------------+----------------------+
164 Row
< Empty
, open_close
, Open
, open_drawer
>,
165 Row
< Empty
, cd_detected
, Stopped
, store_cd_info
>,
166 // +---------+-------------+---------+---------------------+----------------------+
167 Row
< Playing
, stop
, Stopped
, stop_playback
>,
168 Row
< Playing
, pause
, Paused
, pause_playback
>,
169 Row
< Playing
, open_close
, Open
, stop_and_open
>,
170 // +---------+-------------+---------+---------------------+----------------------+
171 Row
< Paused
, end_pause
, Playing
, resume_playback
>,
172 Row
< Paused
, stop
, Stopped
, stop_playback
>,
173 Row
< Paused
, open_close
, Open
, stop_and_open
>
174 // +---------+-------------+---------+---------------------+----------------------+
177 // Replaces the default no-transition response.
178 template <class FSM
,class Event
>
179 void no_transition(Event
const& e
, FSM
&,int state
)
181 std::cout
<< "no transition from state " << state
182 << " on event " << typeid(e
).name() << std::endl
;
185 typedef msm::back::state_machine
<player_
> player
;
188 // Testing utilities.
190 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
192 void pstate(player
const& p
)
194 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
200 long mtime(struct timeval
& tv1
,struct timeval
& tv2
)
202 return (tv2
.tv_sec
-tv1
.tv_sec
) *1000000 + ((tv2
.tv_usec
-tv1
.tv_usec
));
212 ::QueryPerformanceFrequency(&res
);
213 LARGE_INTEGER li
,li2
;
215 struct timeval tv1
,tv2
;
216 gettimeofday(&tv1
,NULL
);
222 ::QueryPerformanceCounter(&li
);
224 gettimeofday(&tv1
,NULL
);
226 for (int i
=0;i
<100;++i
)
228 p2
.process_event(test_fsm::open_close());
229 p2
.process_event(test_fsm::open_close());
230 p2
.process_event(test_fsm::cd_detected());
231 p2
.process_event(test_fsm::play());
232 p2
.process_event(test_fsm::pause());
233 // go back to Playing
234 p2
.process_event(test_fsm::end_pause());
235 p2
.process_event(test_fsm::pause());
236 p2
.process_event(test_fsm::stop());
237 // event leading to the same state
238 p2
.process_event(test_fsm::stop());
239 p2
.process_event(test_fsm::open_close());
240 p2
.process_event(test_fsm::open_close());
243 ::QueryPerformanceCounter(&li2
);
245 gettimeofday(&tv2
,NULL
);
248 std::cout
<< "msm took in s:" << (double)(li2
.QuadPart
-li
.QuadPart
)/res
.QuadPart
<<"\n" <<std::endl
;
250 std::cout
<< "msm took in us:" << mtime(tv1
,tv2
) <<"\n" <<std::endl
;