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 <vector> 12 #include <iostream> 13 // back-end 14 #include <boost/msm/back/state_machine.hpp> 15 //front-end 16 #include <boost/msm/front/state_machine_def.hpp> 17 // functors 18 #include <boost/msm/front/functor_row.hpp> 19 #include <boost/msm/front/euml/common.hpp> 20 21 #include "Empty.hpp" 22 #include "Open.hpp" 23 24 using namespace std; 25 namespace msm = boost::msm; 26 namespace mpl = boost::mpl; 27 using namespace msm::front; 28 29 30 // front-end: define the FSM structure 31 struct player_ : public msm::front::state_machine_def<player_> 32 { 33 // the initial state of the player SM. Must be defined 34 typedef Empty initial_state; 35 typedef mpl::vector<Empty,Open> explicit_creation; 36 37 typedef player_ p; // makes transition table cleaner 38 39 // Replaces the default no-transition response. 40 template <class FSM,class Event> no_transitionplayer_41 void no_transition(Event const& e, FSM&,int state) 42 { 43 std::cout << "no transition from state " << state 44 << " on event " << typeid(e).name() << std::endl; 45 } 46 }; 47 // Pick a back-end 48 typedef msm::back::state_machine<player_> player; 49 50 // 51 // Testing utilities. 52 // 53 static char const* const state_names[] = { "Empty", "Open" }; pstate(player const & p)54void pstate(player const& p) 55 { 56 std::cout << " -> " << state_names[p.current_state()[0]] << std::endl; 57 } 58 test()59void test() 60 { 61 player p; 62 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM 63 p.start(); 64 // go to Open, call on_exit on Empty, then action, then on_entry on Open 65 p.process_event(open_close()); pstate(p); 66 p.process_event(open_close()); pstate(p); 67 } 68 main()69int main() 70 { 71 test(); 72 return 0; 73 } 74 75