]>
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 | // back-end | |
13 | #include <boost/msm/back/state_machine.hpp> | |
14 | //front-end | |
15 | #include <boost/msm/front/state_machine_def.hpp> | |
16 | ||
17 | namespace msm = boost::msm; | |
18 | namespace mpl = boost::mpl; | |
19 | using namespace boost::msm::front; | |
20 | ||
21 | namespace | |
22 | { | |
23 | // events | |
24 | struct event1 {}; | |
25 | ||
26 | ||
27 | // front-end: define the FSM structure | |
28 | struct my_machine_ : public msm::front::state_machine_def<my_machine_> | |
29 | { | |
30 | // The list of FSM states | |
31 | struct State1 : public msm::front::state<> | |
32 | { | |
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;} | |
38 | }; | |
39 | struct State2 : public msm::front::state<> | |
40 | { | |
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;} | |
45 | }; | |
46 | ||
47 | struct State3 : public msm::front::state<> | |
48 | { | |
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;} | |
54 | }; | |
55 | ||
56 | struct State4 : public msm::front::state<> | |
57 | { | |
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;} | |
62 | }; | |
63 | ||
64 | // the initial state of the player SM. Must be defined | |
65 | typedef State1 initial_state; | |
66 | ||
67 | // transition actions | |
68 | void State2ToState3(none const&) { std::cout << "my_machine::State2ToState3\n"; } | |
69 | void State3ToState4(none const&) { std::cout << "my_machine::State3ToState4\n"; } | |
70 | // guard conditions | |
71 | bool always_true(none const& evt) | |
72 | { | |
73 | std::cout << "always_true" << std::endl; | |
74 | return true; | |
75 | } | |
76 | bool always_false(none const& evt) | |
77 | { | |
78 | std::cout << "always_false" << std::endl; | |
79 | return false; | |
80 | } | |
81 | ||
82 | typedef my_machine_ p; // makes transition table cleaner | |
83 | ||
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 | // +---------+-------------+---------+---------------------+----------------------+ | |
95 | > {}; | |
96 | // Replaces the default no-transition response. | |
97 | template <class FSM,class Event> | |
98 | void no_transition(Event const& e, FSM&,int state) | |
99 | { | |
100 | std::cout << "no transition from state " << state | |
101 | << " on event " << typeid(e).name() << std::endl; | |
102 | } | |
103 | }; | |
104 | // Pick a back-end | |
105 | typedef msm::back::state_machine<my_machine_> my_machine; | |
106 | ||
107 | // | |
108 | // Testing utilities. | |
109 | // | |
110 | static char const* const state_names[] = { "State1", "State2", "State3", "State4" }; | |
111 | void pstate(my_machine const& p) | |
112 | { | |
113 | std::cout << " -> " << state_names[p.current_state()[0]] << std::endl; | |
114 | } | |
115 | ||
116 | void test() | |
117 | { | |
118 | my_machine p; | |
119 | ||
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 | |
122 | p.start(); | |
123 | // this event will bring us back to the initial state and thus, a new "loop" will be started | |
124 | p.process_event(event1()); | |
125 | ||
126 | } | |
127 | } | |
128 | ||
129 | int main() | |
130 | { | |
131 | test(); | |
132 | return 0; | |
133 | } |