DistributedTable.cpp 2.1 KB

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