2 // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
3 // under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 #include "boost/mpl/int.hpp"
8 #include "boost/mpl/fold.hpp"
9 #include "boost/mpl/prior.hpp"
10 #include "boost/mpl/count.hpp"
11 #include "boost/mpl/insert.hpp"
12 #include <boost/mpl/greater.hpp>
13 #include <boost/mpl/for_each.hpp>
14 #include <boost/mpl/filter_view.hpp>
15 #include "boost/mpl/vector/vector20.hpp"
16 #include "boost/assert.hpp"
17 #include <boost/type_traits/is_same.hpp>
23 #if defined(BOOST_DINKUMWARE_STDLIB) && BOOST_DINKUMWARE_STDLIB < 310
24 namespace std { using ::clock_t; }
27 namespace mpl = boost::mpl;
28 using namespace mpl::placeholders;
30 // A metafunction that returns the Event associated with a transition.
31 template <class Transition>
32 struct transition_event
34 typedef typename Transition::event type;
37 // A metafunction computing the maximum of a transition's source and
39 template <class Transition>
40 struct transition_max_state
42 typedef typename mpl::int_<
43 (Transition::current_state > Transition::next_state)
44 ? Transition::current_state
45 : Transition::next_state
49 template<class Derived>
52 // Generates a singleton runtime lookup table that maps current state
53 // to a function that makes the FSM take its transition on the given
55 template <class Fsm, int initial_state, class Stt, class Event>
59 // This is a table of these function pointers.
60 typedef int (*cell)(Fsm&, int, Event const&);
62 // Compute the maximum state value in the Fsm so we know how big
64 BOOST_STATIC_CONSTANT(
67 , mpl::int_<initial_state>
69 mpl::greater<transition_max_state<_2>,_1>
70 , transition_max_state<_2>
77 // A function object for use with mpl::for_each that stuffs
78 // transitions into cells.
81 init_cell(dispatch_table* self_)
85 // Cell initializer function object, used with mpl::for_each
86 template <class Transition>
87 void operator()(Transition const&) const
89 self->entries[Transition::current_state] = &Transition::execute;
96 // initialize the dispatch table for a given Event and Fsm
99 // Initialize cells for no transition
100 for (int i = 0; i <= max_state; ++i)
102 // VC7.1 seems to need the two-phase assignment.
103 cell call_no_transition = &state_machine<Fsm>::call_no_transition;
104 entries[i] = call_no_transition;
107 // Go back and fill in cells for matching transitions.
111 , boost::is_same<transition_event<_>, Event>
116 // The singleton instance.
117 static const dispatch_table instance;
119 public: // data members
120 cell entries[max_state + 1];
123 // This declares the statically-initialized dispatch_table instance.
124 template <class Fsm, int initial_state, class Stt, class Event>
125 const dispatch_table<Fsm, initial_state, Stt, Event>
126 dispatch_table<Fsm, initial_state, Stt, Event>::instance;
128 // CRTP base class for state machines. Pass the actual FSM class as
129 // the Derived parameter.
130 template<class Derived>
133 public: // Member functions
135 // Main function used by clients of the derived FSM to make
137 template<class Event>
138 int process_event(Event const& evt)
140 typedef typename Derived::transition_table stt;
141 typedef dispatch_table<Derived, Derived::initial_state,stt,Event> table;
145 = table::instance.entries[this->m_state](
146 *static_cast<Derived*>(this), this->m_state, evt);
149 // Getter that returns the current state of the FSM
150 int current_state() const
152 return this->m_state;
156 template <class Fsm, int initial_state, class Stt, class Event>
157 friend class dispatch_table;
159 template <class Event>
160 static int call_no_transition(Derived& fsm, int state, Event const& e)
162 return fsm.no_transition(state, e);
165 // Default no-transition handler. Can be replaced in the Derived
167 template <class Event>
168 int no_transition(int state, Event const& e)
174 protected: // interface for the derived class
176 template<class State>
177 state_machine(State state) // Construct with an initial state
183 : m_state(Derived::initial_state) // Construct with the default initial_state
187 // Template used to form rows in the transition table
192 , void (Derived::*action)(Event const&)
196 BOOST_STATIC_CONSTANT(int, current_state = CurrentState);
197 BOOST_STATIC_CONSTANT(int, next_state = NextState);
200 // Take the transition action and return the next state.
201 static int execute(Derived& fsm, int state, Event const& evt)
203 BOOST_ASSERT(state == current_state);
209 private: // data members
213 namespace // Concrete FSM implementation
219 struct open_close {};
221 // A "complicated" event type that carries some data.
224 cd_detected(std::string name, std::vector<std::clock_t> durations)
226 , track_durations(durations)
230 std::vector<std::clock_t> track_durations;
233 // Concrete FSM implementation
234 class player : public state_machine<player>
236 // The list of FSM states
238 Empty, Open, Stopped, Playing, Paused
239 , initial_state = Empty
243 public: // Codewarrior bug workaround. Tested at 0x3202
245 // transition actions
246 void start_playback(play const&) { std::cout << "player::start_playback\n"; }
247 void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
248 void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
249 void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
250 void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
251 void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
252 void resume_playback(play const&) { std::cout << "player::resume_playback\n"; }
253 void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
257 friend class state_machine<player>;
258 typedef player p; // makes transition table cleaner
261 struct transition_table : mpl::vector11<
262 // Start Event Next Action
263 // +---------+-------------+---------+---------------------+
264 row < Stopped , play , Playing , &p::start_playback >,
265 row < Stopped , open_close , Open , &p::open_drawer >,
266 // +---------+-------------+---------+---------------------+
267 row < Open , open_close , Empty , &p::close_drawer >,
268 // +---------+-------------+---------+---------------------+
269 row < Empty , open_close , Open , &p::open_drawer >,
270 row < Empty , cd_detected , Stopped , &p::store_cd_info >,
271 // +---------+-------------+---------+---------------------+
272 row < Playing , stop , Stopped , &p::stop_playback >,
273 row < Playing , pause , Paused , &p::pause_playback >,
274 row < Playing , open_close , Open , &p::stop_and_open >,
275 // +---------+-------------+---------+---------------------+
276 row < Paused , play , Playing , &p::resume_playback >,
277 row < Paused , stop , Stopped , &p::stop_playback >,
278 row < Paused , open_close , Open , &p::stop_and_open >
279 // +---------+-------------+---------+---------------------+
282 // Replaces the default no-transition response.
283 template <class Event>
284 int no_transition(int state, Event const& e)
286 std::cout << "no transition from state " << state
287 << " on event " << typeid(e).name() << std::endl;
293 // Testing utilities.
295 static char const* const state_names[] = { "Empty", "Open", "Stopped", "Playing", "Paused" };
297 void pstate(player const& p)
299 std::cout << " -> " << state_names[p.current_state()] << std::endl;
305 p.process_event(open_close()); pstate(p);
306 p.process_event(open_close()); pstate(p);
310 , std::vector<std::clock_t>( /* track lengths */ )
315 p.process_event(play()); pstate(p);
316 p.process_event(pause()); pstate(p);
317 p.process_event(play()); pstate(p);
318 p.process_event(stop()); pstate(p);