| 1 | #include <boost/statechart/state_machine.hpp>
|
|---|
| 2 | #include <boost/statechart/event.hpp>
|
|---|
| 3 | #include <boost/statechart/simple_state.hpp>
|
|---|
| 4 | #include <boost/statechart/custom_reaction.hpp>
|
|---|
| 5 |
|
|---|
| 6 | #include <iostream>
|
|---|
| 7 |
|
|---|
| 8 | namespace bs = boost::statechart;
|
|---|
| 9 |
|
|---|
| 10 | using namespace std;
|
|---|
| 11 |
|
|---|
| 12 | // The main event, should be handles by a sub-state and the main state
|
|---|
| 13 | struct update_event;
|
|---|
| 14 | // An event triggering the sub-state to switch
|
|---|
| 15 | struct toggle_event;
|
|---|
| 16 | // A dummy event
|
|---|
| 17 | struct other_event;
|
|---|
| 18 |
|
|---|
| 19 | // Sub-states in 2 orthogonal regions
|
|---|
| 20 | struct state_1A;
|
|---|
| 21 | struct state_1B;
|
|---|
| 22 | struct state_2A;
|
|---|
| 23 | struct state_2B;
|
|---|
| 24 |
|
|---|
| 25 | // Main state
|
|---|
| 26 | struct parent_state;
|
|---|
| 27 |
|
|---|
| 28 | // State machine consisting of the main state
|
|---|
| 29 | struct machine : bs::state_machine<machine, parent_state>
|
|---|
| 30 | {
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | typedef
|
|---|
| 34 | boost::mpl::list
|
|---|
| 35 | < state_1A
|
|---|
| 36 | , state_2A
|
|---|
| 37 | > inner_states_t;
|
|---|
| 38 |
|
|---|
| 39 | // The main state has 2 orthogonal sub-states
|
|---|
| 40 | struct parent_state : bs::simple_state<parent_state, machine, inner_states_t>
|
|---|
| 41 | {
|
|---|
| 42 | typedef boost::mpl::list
|
|---|
| 43 | < bs::custom_reaction <update_event>
|
|---|
| 44 | >
|
|---|
| 45 | reactions;
|
|---|
| 46 |
|
|---|
| 47 | bs::result react(const update_event&)
|
|---|
| 48 | {
|
|---|
| 49 | cout << __FUNCTION__ << endl;
|
|---|
| 50 |
|
|---|
| 51 | return discard_event();
|
|---|
| 52 | }
|
|---|
| 53 | };
|
|---|
| 54 |
|
|---|
| 55 | struct update_event : bs::event<update_event>
|
|---|
| 56 | {
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | struct other_event : bs::event<other_event>
|
|---|
| 60 | {
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | struct toggle_event : bs::event<toggle_event>
|
|---|
| 64 | {
|
|---|
| 65 | };
|
|---|
| 66 |
|
|---|
| 67 | struct state_1A : bs::simple_state<state_1A, parent_state::orthogonal<0>>
|
|---|
| 68 | {
|
|---|
| 69 | typedef boost::mpl::list
|
|---|
| 70 | < bs::custom_reaction <toggle_event>
|
|---|
| 71 | , bs::custom_reaction <update_event>
|
|---|
| 72 | >
|
|---|
| 73 | reactions;
|
|---|
| 74 |
|
|---|
| 75 | state_1A()
|
|---|
| 76 | {
|
|---|
| 77 | cout << __FUNCTION__ << endl;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | bs::result react(const toggle_event&)
|
|---|
| 81 | {
|
|---|
| 82 | cout << __FUNCTION__ << endl;
|
|---|
| 83 |
|
|---|
| 84 | return transit<state_1B>();
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | bs::result react(const update_event&)
|
|---|
| 88 | {
|
|---|
| 89 | cout << __FUNCTION__ << endl;
|
|---|
| 90 |
|
|---|
| 91 | return forward_event();
|
|---|
| 92 | }
|
|---|
| 93 | };
|
|---|
| 94 |
|
|---|
| 95 | struct state_1B : bs::simple_state<state_1B, parent_state::orthogonal<0>>
|
|---|
| 96 | {
|
|---|
| 97 | typedef boost::mpl::list
|
|---|
| 98 | < bs::custom_reaction <toggle_event>
|
|---|
| 99 | >
|
|---|
| 100 | reactions;
|
|---|
| 101 |
|
|---|
| 102 | state_1B()
|
|---|
| 103 | {
|
|---|
| 104 | cout << __FUNCTION__ << endl;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | bs::result react(const toggle_event&)
|
|---|
| 108 | {
|
|---|
| 109 | cout << __FUNCTION__ << endl;
|
|---|
| 110 |
|
|---|
| 111 | return transit<state_1A>();
|
|---|
| 112 | }
|
|---|
| 113 | };
|
|---|
| 114 |
|
|---|
| 115 | struct state_2A : bs::simple_state<state_2A, parent_state::orthogonal<1>>
|
|---|
| 116 | {
|
|---|
| 117 | typedef boost::mpl::list
|
|---|
| 118 | < bs::custom_reaction <other_event>
|
|---|
| 119 | >
|
|---|
| 120 | reactions;
|
|---|
| 121 |
|
|---|
| 122 | state_2A()
|
|---|
| 123 | {
|
|---|
| 124 | cout << __FUNCTION__ << endl;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | bs::result react(const other_event&)
|
|---|
| 128 | {
|
|---|
| 129 | cout << __FUNCTION__ << endl;
|
|---|
| 130 |
|
|---|
| 131 | return discard_event();
|
|---|
| 132 | }
|
|---|
| 133 | };
|
|---|
| 134 |
|
|---|
| 135 | struct state_2B : bs::simple_state<state_2B, parent_state::orthogonal<1>>
|
|---|
| 136 | {
|
|---|
| 137 | typedef boost::mpl::list
|
|---|
| 138 | < bs::custom_reaction <other_event>
|
|---|
| 139 | >
|
|---|
| 140 | reactions;
|
|---|
| 141 |
|
|---|
| 142 | state_2B()
|
|---|
| 143 | {
|
|---|
| 144 | cout << __FUNCTION__ << endl;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | bs::result react(const other_event&)
|
|---|
| 148 | {
|
|---|
| 149 | cout << __FUNCTION__ << endl;
|
|---|
| 150 |
|
|---|
| 151 | return discard_event();
|
|---|
| 152 | }
|
|---|
| 153 | };
|
|---|
| 154 |
|
|---|
| 155 | int main(int argc, char* argv[])
|
|---|
| 156 | {
|
|---|
| 157 | machine m;
|
|---|
| 158 |
|
|---|
| 159 | cout << "0. Initiating state machine:" << endl;
|
|---|
| 160 | m.initiate();
|
|---|
| 161 |
|
|---|
| 162 | cout << endl << "1. Processing 'other_event':" << endl;
|
|---|
| 163 | m.process_event(other_event ());
|
|---|
| 164 |
|
|---|
| 165 | cout << endl << "2. Processing 'update_event':" << endl;
|
|---|
| 166 | m.process_event(update_event());
|
|---|
| 167 |
|
|---|
| 168 | cout << endl << "3. Processing 'toggle_event':" << endl;
|
|---|
| 169 | m.process_event(toggle_event());
|
|---|
| 170 |
|
|---|
| 171 | cout << endl << "4. Processing 'update_event':" << endl;
|
|---|
| 172 | m.process_event(update_event());
|
|---|
| 173 |
|
|---|
| 174 | cout << endl << "5. Processing 'toggle_event':" << endl;
|
|---|
| 175 | m.process_event(toggle_event());
|
|---|
| 176 |
|
|---|
| 177 | cout << endl << "6. Processing 'update_event':" << endl;
|
|---|
| 178 | m.process_event(update_event());
|
|---|
| 179 |
|
|---|
| 180 | return 0;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 184 | // Program output after building with VC 9 and Boost 1.37.0
|
|---|
| 185 | //
|
|---|
| 186 | // Note the different event handling in step 6. as compared to step 2.
|
|---|
| 187 | // although the machine should be back in the same state.
|
|---|
| 188 | //////////////////////////////////////////////////////////////////////////
|
|---|
| 189 |
|
|---|
| 190 | /*
|
|---|
| 191 |
|
|---|
| 192 | 0. Initiating state machine:
|
|---|
| 193 | state_1A::state_1A
|
|---|
| 194 | state_2A::state_2A
|
|---|
| 195 |
|
|---|
| 196 | 1. Processing 'other_event':
|
|---|
| 197 | state_2A::react
|
|---|
| 198 |
|
|---|
| 199 | 2. Processing 'update_event':
|
|---|
| 200 | state_1A::react
|
|---|
| 201 | parent_state::react
|
|---|
| 202 |
|
|---|
| 203 | 3. Processing 'toggle_event':
|
|---|
| 204 | state_1A::react
|
|---|
| 205 | state_1B::state_1B
|
|---|
| 206 |
|
|---|
| 207 | 4. Processing 'update_event':
|
|---|
| 208 | parent_state::react
|
|---|
| 209 |
|
|---|
| 210 | 5. Processing 'toggle_event':
|
|---|
| 211 | state_1B::react
|
|---|
| 212 | state_1A::state_1A
|
|---|
| 213 |
|
|---|
| 214 | 6. Processing 'update_event':
|
|---|
| 215 | parent_state::react
|
|---|
| 216 |
|
|---|
| 217 | */
|
|---|