]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 <iostream> | |
12 | ||
13 | // back-end | |
14 | #include <boost/msm/back/state_machine.hpp> | |
15 | #include <boost/msm/back/mpl_graph_fsm_check.hpp> | |
16 | //front-end | |
17 | #include <boost/msm/front/state_machine_def.hpp> | |
18 | ||
19 | namespace msm = boost::msm; | |
20 | namespace mpl = boost::mpl; | |
21 | using namespace msm::back; | |
22 | namespace | |
23 | { | |
24 | // events | |
25 | struct play {}; | |
26 | struct end_pause {}; | |
27 | struct stop {}; | |
28 | struct pause {}; | |
29 | struct open_close {}; | |
30 | struct cd_detected{}; | |
31 | struct error_found {}; | |
32 | ||
33 | // front-end: define the FSM structure | |
34 | struct player_ : public msm::front::state_machine_def<player_> | |
35 | { | |
36 | // The list of FSM states | |
37 | struct Empty : public msm::front::state<> | |
38 | { | |
39 | }; | |
40 | struct Open : public msm::front::state<> | |
41 | { | |
42 | }; | |
43 | ||
44 | // sm_ptr still supported but deprecated as functors are a much better way to do the same thing | |
45 | struct Stopped : public msm::front::state<> | |
46 | { | |
47 | }; | |
48 | ||
49 | struct Playing : public msm::front::state<> | |
50 | { | |
51 | }; | |
52 | ||
53 | // state not defining any entry or exit | |
54 | struct Paused : public msm::front::state<> | |
55 | { | |
56 | }; | |
57 | struct AllOk : public msm::front::state<> | |
58 | { | |
59 | }; | |
60 | struct ErrorMode : public msm::front::state<> | |
61 | { | |
62 | }; | |
63 | struct State1 : public msm::front::state<> | |
64 | { | |
65 | }; | |
66 | struct State2 : public msm::front::state<> | |
67 | { | |
68 | }; | |
69 | // the initial state of the player SM. Must be defined | |
70 | typedef mpl::vector<Empty,AllOk> initial_state; | |
71 | ||
72 | // Transition table for player | |
73 | struct transition_table : mpl::vector< | |
74 | // Start Event Next Action Guard | |
75 | // +---------+-------------+---------+---------------------+----------------------+ | |
76 | // adding this line makes non-reachable states and should cause a static assert | |
77 | //_row < State1 , open_close , State2 >, | |
78 | // adding this line makes non-orthogonal regions and should cause a static assert | |
79 | //_row < Paused , error_found , ErrorMode >, | |
80 | _row < Stopped , play , Playing >, | |
81 | _row < Stopped , open_close , Open >, | |
82 | _row < Stopped , stop , Stopped >, | |
83 | // +---------+-------------+---------+---------------------+----------------------+ | |
84 | _row < Open , open_close , Empty >, | |
85 | // +---------+-------------+---------+---------------------+----------------------+ | |
86 | _row < Empty , open_close , Open >, | |
87 | _row < Empty , cd_detected , Stopped >, | |
88 | _row < Empty , cd_detected , Playing >, | |
89 | // +---------+-------------+---------+---------------------+----------------------+ | |
90 | _row < Playing , stop , Stopped >, | |
91 | _row < Playing , pause , Paused >, | |
92 | _row < Playing , open_close , Open >, | |
93 | // +---------+-------------+---------+---------------------+----------------------+ | |
94 | _row < Paused , end_pause , Playing >, | |
95 | _row < Paused , stop , Stopped >, | |
96 | _row < Paused , open_close , Open >, | |
97 | _row < AllOk , error_found , ErrorMode > | |
98 | // +---------+-------------+---------+---------------------+----------------------+ | |
99 | > {}; | |
100 | // Replaces the default no-transition response. | |
101 | template <class FSM,class Event> | |
102 | void no_transition(Event const& e, FSM&,int state) | |
103 | { | |
104 | } | |
105 | }; | |
106 | // Pick a back-end | |
107 | typedef msm::back::state_machine<player_,msm::back::mpl_graph_fsm_check> player; | |
108 | ||
109 | void test() | |
110 | { | |
111 | player p; | |
112 | } | |
113 | } | |
114 | ||
115 | int main() | |
116 | { | |
117 | test(); | |
118 | return 0; | |
119 | } |