| 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 | // back-end |
| 12 | #include <boost/msm/back/state_machine.hpp> |
| 13 | //front-end |
| 14 | #include <boost/msm/front/state_machine_def.hpp> |
| 15 | #ifndef BOOST_MSM_NONSTANDALONE_TEST |
| 16 | #define BOOST_TEST_MODULE MyTest |
| 17 | #endif |
| 18 | #include <boost/test/unit_test.hpp> |
| 19 | |
| 20 | namespace msm = boost::msm; |
| 21 | namespace mpl = boost::mpl; |
| 22 | |
| 23 | namespace |
| 24 | { |
| 25 | // events |
| 26 | struct play {}; |
| 27 | struct end_pause {}; |
| 28 | struct stop {}; |
| 29 | struct pause {}; |
| 30 | struct open_close {}; |
| 31 | struct internal_evt {}; |
| 32 | struct to_ignore {}; |
| 33 | |
| 34 | // A "complicated" event type that carries some data. |
| 35 | enum DiskTypeEnum |
| 36 | { |
| 37 | DISK_CD=0, |
| 38 | DISK_DVD=1 |
| 39 | }; |
| 40 | struct cd_detected |
| 41 | { |
| 42 | cd_detected(std::string name, DiskTypeEnum diskType) |
| 43 | : name(name), |
| 44 | disc_type(diskType) |
| 45 | {} |
| 46 | |
| 47 | std::string name; |
| 48 | DiskTypeEnum disc_type; |
| 49 | }; |
| 50 | |
| 51 | // front-end: define the FSM structure |
| 52 | struct player_ : public msm::front::state_machine_def<player_> |
| 53 | { |
| 54 | unsigned int start_playback_counter; |
| 55 | unsigned int can_close_drawer_counter; |
| 56 | unsigned int internal_action_counter; |
| 57 | unsigned int internal_guard_counter; |
| 58 | |
| 59 | player_(): |
| 60 | start_playback_counter(0), |
| 61 | can_close_drawer_counter(0), |
| 62 | internal_action_counter(0), |
| 63 | internal_guard_counter(0) |
| 64 | {} |
| 65 | |
| 66 | // The list of FSM states |
| 67 | struct Empty : public msm::front::state<> |
| 68 | { |
| 69 | template <class Event,class FSM> |
| 70 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 71 | template <class Event,class FSM> |
| 72 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 73 | int entry_counter; |
| 74 | int exit_counter; |
| 75 | }; |
| 76 | struct Open : public msm::front::state<> |
| 77 | { |
| 78 | template <class Event,class FSM> |
| 79 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 80 | template <class Event,class FSM> |
| 81 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 82 | int entry_counter; |
| 83 | int exit_counter; |
| 84 | }; |
| 85 | |
| 86 | // sm_ptr still supported but deprecated as functors are a much better way to do the same thing |
| 87 | struct Stopped : public msm::front::state<> |
| 88 | { |
| 89 | template <class Event,class FSM> |
| 90 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 91 | template <class Event,class FSM> |
| 92 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 93 | int entry_counter; |
| 94 | int exit_counter; |
| 95 | }; |
| 96 | |
| 97 | struct Playing : public msm::front::state<> |
| 98 | { |
| 99 | template <class Event,class FSM> |
| 100 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 101 | template <class Event,class FSM> |
| 102 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 103 | int entry_counter; |
| 104 | int exit_counter; |
| 105 | }; |
| 106 | |
| 107 | // state not defining any entry or exit |
| 108 | struct Paused : public msm::front::state<> |
| 109 | { |
| 110 | template <class Event,class FSM> |
| 111 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 112 | template <class Event,class FSM> |
| 113 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 114 | int entry_counter; |
| 115 | int exit_counter; |
| 116 | }; |
| 117 | |
| 118 | // the initial state of the player SM. Must be defined |
| 119 | typedef Empty initial_state; |
| 120 | |
| 121 | // transition actions |
| 122 | void start_playback(play const&) {++start_playback_counter; } |
| 123 | void open_drawer(open_close const&) { } |
| 124 | void store_cd_info(cd_detected const&) { } |
| 125 | void stop_playback(stop const&) { } |
| 126 | void pause_playback(pause const&) { } |
| 127 | void resume_playback(end_pause const&) { } |
| 128 | void stop_and_open(open_close const&) { } |
| 129 | void stopped_again(stop const&){} |
| 130 | void internal_action(internal_evt const&){++internal_action_counter; } |
| 131 | bool internal_guard(cd_detected const&){++internal_guard_counter;return false;} |
| 132 | bool internal_guard2(internal_evt const&){++internal_guard_counter;return true;} |
| 133 | // guard conditions |
| 134 | bool good_disk_format(cd_detected const& evt) |
| 135 | { |
| 136 | // to test a guard condition, let's say we understand only CDs, not DVD |
| 137 | if (evt.disc_type != DISK_CD) |
| 138 | { |
| 139 | return false; |
| 140 | } |
| 141 | return true; |
| 142 | } |
| 143 | bool can_close_drawer(open_close const&) |
| 144 | { |
| 145 | ++can_close_drawer_counter; |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | typedef player_ p; // makes transition table cleaner |
| 150 | |
| 151 | // Transition table for player |
| 152 | struct transition_table : mpl::vector< |
| 153 | // Start Event Next Action Guard |
| 154 | // +---------+-------------+---------+---------------------+----------------------+ |
| 155 | a_row < Stopped , play , Playing , &p::start_playback >, |
| 156 | a_row < Stopped , open_close , Open , &p::open_drawer >, |
| 157 | _row < Stopped , stop , Stopped >, |
| 158 | // +---------+-------------+---------+---------------------+----------------------+ |
| 159 | g_row < Open , open_close , Empty , &p::can_close_drawer >, |
| 160 | // +---------+-------------+---------+---------------------+----------------------+ |
| 161 | a_row < Empty , open_close , Open , &p::open_drawer >, |
| 162 | row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >, |
| 163 | irow < Empty , internal_evt, &p::internal_action ,&p::internal_guard2 >, |
| 164 | _irow < Empty , to_ignore >, |
| 165 | g_irow < Empty , cd_detected ,&p::internal_guard >, |
| 166 | // +---------+-------------+---------+---------------------+----------------------+ |
| 167 | a_row < Playing , stop , Stopped , &p::stop_playback >, |
| 168 | a_row < Playing , pause , Paused , &p::pause_playback >, |
| 169 | a_row < Playing , open_close , Open , &p::stop_and_open >, |
| 170 | // +---------+-------------+---------+---------------------+----------------------+ |
| 171 | a_row < Paused , end_pause , Playing , &p::resume_playback >, |
| 172 | a_row < Paused , stop , Stopped , &p::stop_playback >, |
| 173 | a_row < Paused , open_close , Open , &p::stop_and_open > |
| 174 | // +---------+-------------+---------+---------------------+----------------------+ |
| 175 | > {}; |
| 176 | // Replaces the default no-transition response. |
| 177 | template <class FSM,class Event> |
| 178 | void no_transition(Event const&, FSM&,int) |
| 179 | { |
| 180 | BOOST_FAIL("no_transition called!" ); |
| 181 | } |
| 182 | // init counters |
| 183 | template <class Event,class FSM> |
| 184 | void on_entry(Event const&,FSM& fsm) |
| 185 | { |
| 186 | fsm.template get_state<player_::Stopped&>().entry_counter=0; |
| 187 | fsm.template get_state<player_::Stopped&>().exit_counter=0; |
| 188 | fsm.template get_state<player_::Open&>().entry_counter=0; |
| 189 | fsm.template get_state<player_::Open&>().exit_counter=0; |
| 190 | fsm.template get_state<player_::Empty&>().entry_counter=0; |
| 191 | fsm.template get_state<player_::Empty&>().exit_counter=0; |
| 192 | fsm.template get_state<player_::Playing&>().entry_counter=0; |
| 193 | fsm.template get_state<player_::Playing&>().exit_counter=0; |
| 194 | fsm.template get_state<player_::Paused&>().entry_counter=0; |
| 195 | fsm.template get_state<player_::Paused&>().exit_counter=0; |
| 196 | } |
| 197 | |
| 198 | }; |
| 199 | // Pick a back-end |
| 200 | typedef msm::back::state_machine<player_> player; |
| 201 | |
| 202 | // static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" }; |
| 203 | |
| 204 | |
| 205 | BOOST_AUTO_TEST_CASE( my_test ) |
| 206 | { |
| 207 | player p; |
| 208 | |
| 209 | p.start(); |
| 210 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly" ); |
| 211 | // internal events |
| 212 | p.process_event(evt: to_ignore()); |
| 213 | p.process_event(evt: internal_evt()); |
| 214 | BOOST_CHECK_MESSAGE(p.internal_action_counter == 1,"Internal action not called correctly" ); |
| 215 | BOOST_CHECK_MESSAGE(p.internal_guard_counter == 1,"Internal guard not called correctly" ); |
| 216 | |
| 217 | p.process_event(evt: open_close()); |
| 218 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active" ); //Open |
| 219 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly" ); |
| 220 | BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly" ); |
| 221 | |
| 222 | p.process_event(evt: open_close()); |
| 223 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active" ); //Empty |
| 224 | BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly" ); |
| 225 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly" ); |
| 226 | BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly" ); |
| 227 | |
| 228 | p.process_event( |
| 229 | evt: cd_detected("louie, louie" ,DISK_DVD)); |
| 230 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active" ); //Empty |
| 231 | BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly" ); |
| 232 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly" ); |
| 233 | BOOST_CHECK_MESSAGE(p.internal_guard_counter == 2,"Internal guard not called correctly" ); |
| 234 | |
| 235 | p.process_event( |
| 236 | evt: cd_detected("louie, louie" ,DISK_CD)); |
| 237 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
| 238 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly" ); |
| 239 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly" ); |
| 240 | BOOST_CHECK_MESSAGE(p.internal_guard_counter == 3,"Internal guard not called correctly" ); |
| 241 | |
| 242 | p.process_event(evt: play()); |
| 243 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
| 244 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly" ); |
| 245 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly" ); |
| 246 | BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly" ); |
| 247 | |
| 248 | p.process_event(evt: pause()); |
| 249 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active" ); //Paused |
| 250 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly" ); |
| 251 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly" ); |
| 252 | |
| 253 | // go back to Playing |
| 254 | p.process_event(evt: end_pause()); |
| 255 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
| 256 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 1,"Paused exit not called correctly" ); |
| 257 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 2,"Playing entry not called correctly" ); |
| 258 | |
| 259 | p.process_event(evt: pause()); |
| 260 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active" ); //Paused |
| 261 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly" ); |
| 262 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 2,"Paused entry not called correctly" ); |
| 263 | |
| 264 | p.process_event(evt: stop()); |
| 265 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
| 266 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 2,"Paused exit not called correctly" ); |
| 267 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 2,"Stopped entry not called correctly" ); |
| 268 | |
| 269 | p.process_event(evt: stop()); |
| 270 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
| 271 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 2,"Stopped exit not called correctly" ); |
| 272 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly" ); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | |