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