1 | #include <iostream>
|
---|
2 | #include <boost/msm/back/state_machine.hpp>
|
---|
3 |
|
---|
4 | #include <boost/msm/front/state_machine_def.hpp>
|
---|
5 | #include <boost/msm/front/functor_row.hpp>
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace {
|
---|
9 | namespace msm = boost::msm;
|
---|
10 | namespace msmf = boost::msm::front;
|
---|
11 | namespace mpl = boost::mpl;
|
---|
12 |
|
---|
13 | // ----- Events
|
---|
14 | struct Event1 {};
|
---|
15 |
|
---|
16 | // ----- State machine
|
---|
17 | struct Sm1_:msmf::state_machine_def<Sm1_>
|
---|
18 | {
|
---|
19 | struct State1_:msmf::state<> {};
|
---|
20 | struct State2_:msmf::state_machine_def<State2_>
|
---|
21 | {
|
---|
22 | struct State2_1:msmf::state<> {
|
---|
23 | template <class Event,class Fsm>
|
---|
24 | void on_entry(Event const&, Fsm&) const {
|
---|
25 | std::cout << "State2_1::on_entry()" << std::endl;
|
---|
26 | }
|
---|
27 | };
|
---|
28 | struct Entry1:msmf::entry_pseudo_state<> {};
|
---|
29 | // Actions
|
---|
30 | struct Action1 {
|
---|
31 | template <class Event, class Fsm, class SourceState, class TargetState>
|
---|
32 | void operator()(Event const&, Fsm&, SourceState&, TargetState&)
|
---|
33 | {
|
---|
34 | std::cout << "Action1()" << std::endl;
|
---|
35 | }
|
---|
36 | };
|
---|
37 | // Set initial state
|
---|
38 | typedef mpl::vector<State2_1> initial_state;
|
---|
39 | // Transition table
|
---|
40 | struct transition_table:mpl::vector<
|
---|
41 | // Start Event Next Action Guard
|
---|
42 | msmf::Row < Entry1, msmf::none, State2_1, msmf::none, msmf::none >,
|
---|
43 | msmf::Row < State2_1, Event1, msmf::none, Action1, msmf::none >
|
---|
44 | > {};
|
---|
45 | };
|
---|
46 | typedef msm::back::state_machine<State2_> State2;
|
---|
47 |
|
---|
48 | // Set initial state
|
---|
49 | typedef State1_ initial_state;
|
---|
50 | // Transition table
|
---|
51 | struct transition_table:mpl::vector<
|
---|
52 | // Start Event Next Action Guard
|
---|
53 | msmf::Row < State1_, Event1, State2::entry_pt
|
---|
54 | <State2_::Entry1>, msmf::none, msmf::none >
|
---|
55 | > {};
|
---|
56 | };
|
---|
57 |
|
---|
58 | // Pick a back-end
|
---|
59 | typedef msm::back::state_machine<Sm1_> Sm1;
|
---|
60 |
|
---|
61 | void test()
|
---|
62 | {
|
---|
63 | Sm1 sm1;
|
---|
64 | sm1.start();
|
---|
65 | std::cout << "> Send Event1" << std::endl;
|
---|
66 | sm1.process_event(Event1());
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | int main()
|
---|
71 | {
|
---|
72 | test();
|
---|
73 | return 0;
|
---|
74 | }
|
---|
75 |
|
---|
76 | // Output:
|
---|
77 | //
|
---|
78 | // > Send Event1
|
---|
79 | // State2_1::on_entry()
|
---|
80 | // Action1()
|
---|