player2.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  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)
  6. //
  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>
  18. #include <vector>
  19. #include <ctime>
  20. #include <iostream>
  21. #if defined(BOOST_DINKUMWARE_STDLIB) && BOOST_DINKUMWARE_STDLIB < 310
  22. namespace std { using ::clock_t; }
  23. #endif
  24. namespace mpl = boost::mpl;
  25. using namespace mpl::placeholders;
  26. // A metafunction that returns the Event associated with a transition.
  27. template <class Transition>
  28. struct transition_event
  29. {
  30. typedef typename Transition::event type;
  31. };
  32. // A metafunction computing the maximum of a transition's source and
  33. // end states.
  34. template <class Transition>
  35. struct transition_max_state
  36. {
  37. typedef typename mpl::int_<
  38. (Transition::current_state > Transition::next_state)
  39. ? Transition::current_state
  40. : Transition::next_state
  41. > type;
  42. };
  43. template<class Derived>
  44. class state_machine;
  45. // Generates a singleton runtime lookup table that maps current state
  46. // to a function that makes the FSM take its transition on the given
  47. // Event type.
  48. template <class Fsm, int initial_state, class Stt, class Event>
  49. struct dispatch_table
  50. {
  51. private:
  52. // This is a table of these function pointers.
  53. typedef int (*cell)(Fsm&, int, Event const&);
  54. // Compute the maximum state value in the Fsm so we know how big
  55. // to make the table
  56. BOOST_STATIC_CONSTANT(
  57. int, max_state = (
  58. mpl::fold<Stt
  59. , mpl::int_<initial_state>
  60. , mpl::if_<
  61. mpl::greater<transition_max_state<_2>,_1>
  62. , transition_max_state<_2>
  63. , _1
  64. >
  65. >::type::value
  66. )
  67. );
  68. // A function object for use with mpl::for_each that stuffs
  69. // transitions into cells.
  70. struct init_cell
  71. {
  72. init_cell(dispatch_table* self_)
  73. : self(self_)
  74. {}
  75. // Cell initializer function object, used with mpl::for_each
  76. template <class Transition>
  77. void operator()(Transition const&) const
  78. {
  79. self->entries[Transition::current_state] = &Transition::execute;
  80. }
  81. dispatch_table* self;
  82. };
  83. public:
  84. // initialize the dispatch table for a given Event and Fsm
  85. dispatch_table()
  86. {
  87. // Initialize cells for no transition
  88. for (int i = 0; i <= max_state; ++i)
  89. {
  90. // VC7.1 seems to need the two-phase assignment.
  91. cell call_no_transition = &state_machine<Fsm>::call_no_transition;
  92. entries[i] = call_no_transition;
  93. }
  94. // Go back and fill in cells for matching transitions.
  95. mpl::for_each<
  96. mpl::filter_view<
  97. Stt
  98. , boost::is_same<transition_event<_>, Event>
  99. >
  100. >(init_cell(this));
  101. }
  102. // The singleton instance.
  103. static const dispatch_table instance;
  104. public: // data members
  105. cell entries[max_state + 1];
  106. };
  107. // This declares the statically-initialized dispatch_table instance.
  108. template <class Fsm, int initial_state, class Stt, class Event>
  109. const dispatch_table<Fsm, initial_state, Stt, Event>
  110. dispatch_table<Fsm, initial_state, Stt, Event>::instance;
  111. // CRTP base class for state machines. Pass the actual FSM class as
  112. // the Derived parameter.
  113. template<class Derived>
  114. class state_machine
  115. {
  116. public: // Member functions
  117. // Main function used by clients of the derived FSM to make
  118. // transitions.
  119. template<class Event>
  120. int process_event(Event const& evt)
  121. {
  122. typedef typename Derived::transition_table stt;
  123. typedef dispatch_table<Derived, Derived::initial_state,stt,Event> table;
  124. // Call the action
  125. return this->m_state
  126. = table::instance.entries[this->m_state](
  127. *static_cast<Derived*>(this), this->m_state, evt);
  128. }
  129. // Getter that returns the current state of the FSM
  130. int current_state() const
  131. {
  132. return this->m_state;
  133. }
  134. private:
  135. template <class Fsm, int initial_state, class Stt, class Event>
  136. friend class dispatch_table;
  137. template <class Event>
  138. static int call_no_transition(Derived& fsm, int state, Event const& e)
  139. {
  140. return fsm.no_transition(state, e);
  141. }
  142. // Default no-transition handler. Can be replaced in the Derived
  143. // FSM class.
  144. template <class Event>
  145. int no_transition(int state, Event const& e)
  146. {
  147. BOOST_ASSERT(false);
  148. return state;
  149. }
  150. protected: // interface for the derived class
  151. template<class State>
  152. state_machine(State state) // Construct with an initial state
  153. : m_state(state)
  154. {
  155. }
  156. state_machine()
  157. : m_state(Derived::initial_state) // Construct with the default initial_state
  158. {
  159. }
  160. // Template used to form rows in the transition table
  161. template<
  162. int CurrentState
  163. , class Event
  164. , int NextState
  165. , void (Derived::*action)(Event const&)
  166. >
  167. struct row
  168. {
  169. BOOST_STATIC_CONSTANT(int, current_state = CurrentState);
  170. BOOST_STATIC_CONSTANT(int, next_state = NextState);
  171. typedef Event event;
  172. // Take the transition action and return the next state.
  173. static int execute(Derived& fsm, int state, Event const& evt)
  174. {
  175. BOOST_ASSERT(state == current_state);
  176. (fsm.*action)(evt);
  177. return next_state;
  178. }
  179. };
  180. private: // data members
  181. int m_state;
  182. };
  183. namespace // Concrete FSM implementation
  184. {
  185. // events
  186. struct play {};
  187. struct stop {};
  188. struct pause {};
  189. struct open_close {};
  190. // A "complicated" event type that carries some data.
  191. struct cd_detected
  192. {
  193. cd_detected(std::string name, std::vector<std::clock_t> durations)
  194. : name(name)
  195. , track_durations(durations)
  196. {}
  197. std::string name;
  198. std::vector<std::clock_t> track_durations;
  199. };
  200. // Concrete FSM implementation
  201. class player : public state_machine<player>
  202. {
  203. // The list of FSM states
  204. enum states {
  205. Empty, Open, Stopped, Playing, Paused
  206. , initial_state = Empty
  207. };
  208. #ifdef __MWERKS__
  209. public: // Codewarrior bug workaround. Tested at 0x3202
  210. #endif
  211. // transition actions
  212. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  213. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  214. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  215. void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
  216. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  217. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  218. void resume_playback(play const&) { std::cout << "player::resume_playback\n"; }
  219. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  220. #ifdef __MWERKS__
  221. private:
  222. #endif
  223. friend class state_machine<player>;
  224. typedef player p; // makes transition table cleaner
  225. // Transition table
  226. struct transition_table : mpl::vector11<
  227. // Start Event Next Action
  228. // +---------+-------------+---------+---------------------+
  229. row < Stopped , play , Playing , &p::start_playback >,
  230. row < Stopped , open_close , Open , &p::open_drawer >,
  231. // +---------+-------------+---------+---------------------+
  232. row < Open , open_close , Empty , &p::close_drawer >,
  233. // +---------+-------------+---------+---------------------+
  234. row < Empty , open_close , Open , &p::open_drawer >,
  235. row < Empty , cd_detected , Stopped , &p::store_cd_info >,
  236. // +---------+-------------+---------+---------------------+
  237. row < Playing , stop , Stopped , &p::stop_playback >,
  238. row < Playing , pause , Paused , &p::pause_playback >,
  239. row < Playing , open_close , Open , &p::stop_and_open >,
  240. // +---------+-------------+---------+---------------------+
  241. row < Paused , play , Playing , &p::resume_playback >,
  242. row < Paused , stop , Stopped , &p::stop_playback >,
  243. row < Paused , open_close , Open , &p::stop_and_open >
  244. // +---------+-------------+---------+---------------------+
  245. > {};
  246. // Replaces the default no-transition response.
  247. template <class Event>
  248. int no_transition(int state, Event const& e)
  249. {
  250. std::cout << "no transition from state " << state
  251. << " on event " << typeid(e).name() << std::endl;
  252. return state;
  253. }
  254. };
  255. //
  256. // Testing utilities.
  257. //
  258. static char const* const state_names[] = { "Empty", "Open", "Stopped", "Playing", "Paused" };
  259. void pstate(player const& p)
  260. {
  261. std::cout << " -> " << state_names[p.current_state()] << std::endl;
  262. }
  263. void test()
  264. {
  265. player p;
  266. p.process_event(open_close()); pstate(p);
  267. p.process_event(open_close()); pstate(p);
  268. p.process_event(
  269. cd_detected(
  270. "louie, louie"
  271. , std::vector<std::clock_t>( /* track lengths */ )
  272. )
  273. );
  274. pstate(p);
  275. p.process_event(play()); pstate(p);
  276. p.process_event(pause()); pstate(p);
  277. p.process_event(play()); pstate(p);
  278. p.process_event(stop()); pstate(p);
  279. }
  280. }
  281. int main()
  282. {
  283. test();
  284. return 0;
  285. }