| 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/back11/state_machine.hpp> |
| 13 | //front-end |
| 14 | #include <boost/msm/front/state_machine_def.hpp> |
| 15 | #include <boost/msm/front/functor_row.hpp> |
| 16 | #include <boost/msm/front/euml/common.hpp> |
| 17 | |
| 18 | #include <boost/msm/back/queue_container_circular.hpp> |
| 19 | #include <boost/msm/back/history_policies.hpp> |
| 20 | |
| 21 | #ifndef BOOST_MSM_NONSTANDALONE_TEST |
| 22 | #define BOOST_TEST_MODULE back11_composite_machine_test |
| 23 | #endif |
| 24 | #include <boost/test/unit_test.hpp> |
| 25 | |
| 26 | namespace msm = boost::msm; |
| 27 | namespace mpl = boost::mpl; |
| 28 | using namespace msm::front; |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | // events |
| 33 | struct play {}; |
| 34 | struct end_pause {}; |
| 35 | struct stop {}; |
| 36 | struct pause {}; |
| 37 | struct open_close {}; |
| 38 | struct NextSong {}; |
| 39 | struct PreviousSong {}; |
| 40 | struct cd_detected |
| 41 | { |
| 42 | cd_detected(std::string name) |
| 43 | : name(name) |
| 44 | {} |
| 45 | std::string name; |
| 46 | }; |
| 47 | |
| 48 | // front-end: define the FSM structure |
| 49 | struct player_ : public msm::front::state_machine_def<player_> |
| 50 | { |
| 51 | unsigned int start_playback_counter=0; |
| 52 | unsigned int can_close_drawer_counter=0; |
| 53 | unsigned int test_upper=0; |
| 54 | |
| 55 | // The list of FSM states |
| 56 | struct Empty : public msm::front::state<> |
| 57 | { |
| 58 | template <class Event,class FSM> |
| 59 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 60 | template <class Event,class FSM> |
| 61 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 62 | int entry_counter; |
| 63 | int exit_counter; |
| 64 | }; |
| 65 | struct Open : public msm::front::state<> |
| 66 | { |
| 67 | template <class Event,class FSM> |
| 68 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 69 | template <class Event,class FSM> |
| 70 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 71 | int entry_counter; |
| 72 | int exit_counter; |
| 73 | }; |
| 74 | |
| 75 | // sm_ptr still supported but deprecated as functors are a much better way to do the same thing |
| 76 | struct Stopped : 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 | struct Playing_ : public msm::front::state_machine_def<Playing_> |
| 87 | { |
| 88 | template <class Event,class FSM> |
| 89 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 90 | template <class Event,class FSM> |
| 91 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 92 | int entry_counter; |
| 93 | int exit_counter; |
| 94 | unsigned int start_next_song_counter; |
| 95 | unsigned int start_prev_song_guard_counter; |
| 96 | |
| 97 | Playing_(): |
| 98 | start_next_song_counter(0), |
| 99 | start_prev_song_guard_counter(0) |
| 100 | {} |
| 101 | |
| 102 | // The list of FSM states |
| 103 | struct Song1 : public msm::front::state<> |
| 104 | { |
| 105 | template <class Event,class FSM> |
| 106 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 107 | template <class Event,class FSM> |
| 108 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 109 | int entry_counter; |
| 110 | int exit_counter; |
| 111 | }; |
| 112 | struct Song2 : public msm::front::state<> |
| 113 | { |
| 114 | template <class Event,class FSM> |
| 115 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 116 | template <class Event,class FSM> |
| 117 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 118 | int entry_counter; |
| 119 | int exit_counter; |
| 120 | }; |
| 121 | struct Song3 : public msm::front::state<> |
| 122 | { |
| 123 | template <class Event,class FSM> |
| 124 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 125 | template <class Event,class FSM> |
| 126 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 127 | int entry_counter; |
| 128 | int exit_counter; |
| 129 | }; |
| 130 | // the initial state. Must be defined |
| 131 | typedef Song1 initial_state; |
| 132 | // transition actions |
| 133 | void start_next_song(NextSong const&) {++start_next_song_counter; } |
| 134 | void start_prev_song(PreviousSong const&) { } |
| 135 | // guard conditions |
| 136 | bool start_prev_song_guard(PreviousSong const&) {++start_prev_song_guard_counter;return true; } |
| 137 | |
| 138 | struct ActionNextSong |
| 139 | { |
| 140 | template <class EVT,class FSM,class SourceState,class TargetState> |
| 141 | void operator()(EVT const& ,FSM& fsm,SourceState& ,TargetState& ) |
| 142 | { |
| 143 | ++fsm.get_upper()->test_upper; |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | typedef Playing_ pl; // makes transition table cleaner |
| 148 | // Transition table for Playing |
| 149 | struct transition_table : boost::fusion::vector< |
| 150 | // Start Event Next Action Guard |
| 151 | // +---------+-------------+---------+---------------------+----------------------+ |
| 152 | Row < Song1 , NextSong , Song2 , ActionNextSong >, |
| 153 | row < Song2 , PreviousSong, Song1 , &pl::start_prev_song,&pl::start_prev_song_guard>, |
| 154 | a_row < Song2 , NextSong , Song3 , &pl::start_next_song >, |
| 155 | g_row < Song3 , PreviousSong, Song2 ,&pl::start_prev_song_guard> |
| 156 | // +---------+-------------+---------+---------------------+----------------------+ |
| 157 | > {}; |
| 158 | // Replaces the default no-transition response. |
| 159 | template <class FSM,class Event> |
| 160 | void no_transition(Event const&, FSM&,int) |
| 161 | { |
| 162 | BOOST_FAIL("no_transition called!" ); |
| 163 | } |
| 164 | }; |
| 165 | // back-end |
| 166 | //class player; |
| 167 | typedef msm::back11::state_machine< |
| 168 | Playing_, |
| 169 | msm::back11::state_machine<player_>, |
| 170 | msm::back::queue_container_circular, |
| 171 | msm::back::AlwaysHistory/*, |
| 172 | msm::back11::state_machine<player_>*/ > Playing; |
| 173 | |
| 174 | // state not defining any entry or exit |
| 175 | struct Paused : public msm::front::state<> |
| 176 | { |
| 177 | template <class Event,class FSM> |
| 178 | void on_entry(Event const&,FSM& ) {++entry_counter;} |
| 179 | template <class Event,class FSM> |
| 180 | void on_exit(Event const&,FSM& ) {++exit_counter;} |
| 181 | int entry_counter; |
| 182 | int exit_counter; |
| 183 | }; |
| 184 | |
| 185 | // the initial state of the player SM. Must be defined |
| 186 | typedef Empty initial_state; |
| 187 | |
| 188 | // transition actions |
| 189 | void start_playback(play const&) {++start_playback_counter; } |
| 190 | void open_drawer(open_close const&) { } |
| 191 | void store_cd_info(cd_detected const&) { } |
| 192 | void stop_playback(stop const&) { } |
| 193 | void pause_playback(pause const&) { } |
| 194 | void resume_playback(end_pause const&) { } |
| 195 | void stop_and_open(open_close const&) { } |
| 196 | void stopped_again(stop const&){} |
| 197 | //guards |
| 198 | bool can_close_drawer(open_close const&) |
| 199 | { |
| 200 | ++can_close_drawer_counter; |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | typedef player_ p; // makes transition table cleaner |
| 206 | |
| 207 | // Transition table for player |
| 208 | struct transition_table : boost::fusion::vector< |
| 209 | // Start Event Next Action Guard |
| 210 | // +---------+-------------+---------+---------------------+----------------------+ |
| 211 | a_row < Stopped , play , Playing , &p::start_playback >, |
| 212 | a_row < Stopped , open_close , Open , &p::open_drawer >, |
| 213 | _row < Stopped , stop , Stopped >, |
| 214 | // +---------+-------------+---------+---------------------+----------------------+ |
| 215 | g_row < Open , open_close , Empty , &p::can_close_drawer >, |
| 216 | // +---------+-------------+---------+---------------------+----------------------+ |
| 217 | a_row < Empty , open_close , Open , &p::open_drawer >, |
| 218 | a_row < Empty , cd_detected , Stopped , &p::store_cd_info >, |
| 219 | // +---------+-------------+---------+---------------------+----------------------+ |
| 220 | a_row < Playing , stop , Stopped , &p::stop_playback >, |
| 221 | a_row < Playing , pause , Paused , &p::pause_playback >, |
| 222 | a_row < Playing , open_close , Open , &p::stop_and_open >, |
| 223 | // +---------+-------------+---------+---------------------+----------------------+ |
| 224 | a_row < Paused , end_pause , Playing , &p::resume_playback >, |
| 225 | a_row < Paused , stop , Stopped , &p::stop_playback >, |
| 226 | a_row < Paused , open_close , Open , &p::stop_and_open > |
| 227 | // +---------+-------------+---------+---------------------+----------------------+ |
| 228 | > {}; |
| 229 | // Replaces the default no-transition response. |
| 230 | template <class FSM,class Event> |
| 231 | void no_transition(Event const&, FSM&,int) |
| 232 | { |
| 233 | BOOST_FAIL("no_transition called!" ); |
| 234 | } |
| 235 | // init counters |
| 236 | template <class Event,class FSM> |
| 237 | void on_entry(Event const&,FSM& fsm) |
| 238 | { |
| 239 | fsm.template get_state<player_::Stopped&>().entry_counter=0; |
| 240 | fsm.template get_state<player_::Stopped&>().exit_counter=0; |
| 241 | fsm.template get_state<player_::Open&>().entry_counter=0; |
| 242 | fsm.template get_state<player_::Open&>().exit_counter=0; |
| 243 | fsm.template get_state<player_::Empty&>().entry_counter=0; |
| 244 | fsm.template get_state<player_::Empty&>().exit_counter=0; |
| 245 | fsm.template get_state<player_::Playing&>().entry_counter=0; |
| 246 | fsm.template get_state<player_::Playing&>().exit_counter=0; |
| 247 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().entry_counter=0; |
| 248 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().exit_counter=0; |
| 249 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().entry_counter=0; |
| 250 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().exit_counter=0; |
| 251 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().entry_counter=0; |
| 252 | fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().exit_counter=0; |
| 253 | fsm.template get_state<player_::Paused&>().entry_counter=0; |
| 254 | fsm.template get_state<player_::Paused&>().exit_counter=0; |
| 255 | } |
| 256 | |
| 257 | }; |
| 258 | // Pick a back-end |
| 259 | // class player : public msm::back11::state_machine<player_> |
| 260 | // {}; |
| 261 | using player = msm::back11::state_machine<player_>; |
| 262 | //typedef msm::back11::state_machine<player_> player; |
| 263 | |
| 264 | // static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" }; |
| 265 | |
| 266 | |
| 267 | BOOST_AUTO_TEST_CASE( back11_composite_machine_test ) |
| 268 | { |
| 269 | player p; |
| 270 | |
| 271 | p.start(); |
| 272 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly" ); |
| 273 | |
| 274 | p.process_event(evt: open_close()); |
| 275 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active" ); //Open |
| 276 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly" ); |
| 277 | BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly" ); |
| 278 | |
| 279 | p.process_event(evt: open_close()); |
| 280 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active" ); //Empty |
| 281 | BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly" ); |
| 282 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly" ); |
| 283 | BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly" ); |
| 284 | |
| 285 | p.process_event(evt: cd_detected("louie, louie" )); |
| 286 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
| 287 | BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly" ); |
| 288 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly" ); |
| 289 | |
| 290 | p.process_event(evt: play()); |
| 291 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
| 292 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly" ); |
| 293 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly" ); |
| 294 | BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly" ); |
| 295 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active" ); |
| 296 | BOOST_CHECK_MESSAGE( |
| 297 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().entry_counter == 1, |
| 298 | "Song1 entry not called correctly" ); |
| 299 | |
| 300 | p.process_event(evt: NextSong()); |
| 301 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
| 302 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active" ); |
| 303 | BOOST_CHECK_MESSAGE( |
| 304 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 1, |
| 305 | "Song2 entry not called correctly" ); |
| 306 | BOOST_CHECK_MESSAGE( |
| 307 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().exit_counter == 1, |
| 308 | "Song1 exit not called correctly" ); |
| 309 | BOOST_CHECK_MESSAGE( |
| 310 | p.get_state<player_::Playing&>().start_next_song_counter == 0, |
| 311 | "submachine action not called correctly" ); |
| 312 | BOOST_CHECK_MESSAGE(p.test_upper == 1,"upper not called correctly" ); |
| 313 | |
| 314 | p.process_event(evt: NextSong()); |
| 315 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
| 316 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 2,"Song3 should be active" ); |
| 317 | BOOST_CHECK_MESSAGE( |
| 318 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().entry_counter == 1, |
| 319 | "Song3 entry not called correctly" ); |
| 320 | BOOST_CHECK_MESSAGE( |
| 321 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().exit_counter == 1, |
| 322 | "Song2 exit not called correctly" ); |
| 323 | BOOST_CHECK_MESSAGE( |
| 324 | p.get_state<player_::Playing&>().start_next_song_counter == 1, |
| 325 | "submachine action not called correctly" ); |
| 326 | |
| 327 | p.process_event(evt: PreviousSong()); |
| 328 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
| 329 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active" ); |
| 330 | BOOST_CHECK_MESSAGE( |
| 331 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 2, |
| 332 | "Song2 entry not called correctly" ); |
| 333 | BOOST_CHECK_MESSAGE( |
| 334 | p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().exit_counter == 1, |
| 335 | "Song3 exit not called correctly" ); |
| 336 | BOOST_CHECK_MESSAGE( |
| 337 | p.get_state<player_::Playing&>().start_prev_song_guard_counter == 1, |
| 338 | "submachine guard not called correctly" ); |
| 339 | |
| 340 | p.process_event(evt: pause()); |
| 341 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active" ); //Paused |
| 342 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly" ); |
| 343 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly" ); |
| 344 | |
| 345 | // go back to Playing |
| 346 | p.process_event(evt: end_pause()); |
| 347 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active" ); //Playing |
| 348 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 1,"Paused exit not called correctly" ); |
| 349 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 2,"Playing entry not called correctly" ); |
| 350 | |
| 351 | p.process_event(evt: pause()); |
| 352 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active" ); //Paused |
| 353 | BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly" ); |
| 354 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 2,"Paused entry not called correctly" ); |
| 355 | |
| 356 | p.process_event(evt: stop()); |
| 357 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
| 358 | BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 2,"Paused exit not called correctly" ); |
| 359 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 2,"Stopped entry not called correctly" ); |
| 360 | |
| 361 | p.process_event(evt: stop()); |
| 362 | BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active" ); //Stopped |
| 363 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 2,"Stopped exit not called correctly" ); |
| 364 | BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly" ); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | |