1
2 // Copyright Aleksey Gurtovoy 2002-2004
3 //
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // See http://www.boost.org/libs/mpl for documentation.
9
10 // $Id$
11 // $Date$
12 // $Revision$
13
14 #include "state_machine.hpp"
15 #include <boost/mpl/list.hpp>
16
17 #include <iostream>
18
19 namespace mpl = boost::mpl;
20
21 class player
22 : public fsm::state_machine<player>
23 {
24 public:
player()25 player() {}
26
27 // events
28 struct play_event : event<play_event> {};
29 struct stop_event : event<stop_event> {};
30 struct pause_event : event<pause_event> {};
31
32 // MWCW 8.1 is too eager in inforcing access for non-type template parameters
33 // private:
34 typedef player self_t;
35
36 // state invariants
stopped_state_invariant() const37 void stopped_state_invariant() const {}
playing_state_invariant() const38 void playing_state_invariant() const {}
paused_state_invariant() const39 void paused_state_invariant() const {}
40
41 // states (invariants are passed as non-type template arguments)
42 typedef state<0, &self_t::stopped_state_invariant> stopped;
43 typedef state<1, &self_t::playing_state_invariant> playing;
44 typedef state<2, &self_t::paused_state_invariant> paused;
45
46 // private:
47
48 // transition functions
do_play(play_event const &)49 bool do_play(play_event const&) { std::cout << "player::do_play\n"; return true; }
do_stop(stop_event const &)50 bool do_stop(stop_event const&) { std::cout << "player::do_stop\n"; return true; }
do_pause(pause_event const &)51 bool do_pause(pause_event const&) { std::cout << "player::do_pause\n"; return true; }
do_resume(play_event const &)52 bool do_resume(play_event const&) { std::cout << "player::do_resume\n"; return true; }
53
54 // transitions, in the following format:
55 // | current state | event | next state | transition function |
56 friend class fsm::state_machine<player>;
57 typedef mpl::list<
58 transition<stopped, play_event, playing, &player::do_play>
59 , transition<playing, stop_event, stopped, &player::do_stop>
60 , transition<playing, pause_event, paused, &player::do_pause>
61 , transition<paused, play_event, playing, &player::do_resume>
62 , transition<paused, stop_event, stopped, &player::do_stop>
63 >::type transition_table;
64
65 typedef stopped initial_state;
66 };
67
main()68 int main()
69 {
70 player p;
71 p.process_event(player::play_event());
72 p.process_event(player::pause_event());
73 p.process_event(player::play_event());
74 p.process_event(player::stop_event());
75 return 0;
76 }
77