| 1 | // Copyright 2024 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 | #include <iostream> |
| 12 | // back-end |
| 13 | #include <boost/msm/back11/state_machine.hpp> |
| 14 | //front-end |
| 15 | #include <boost/msm/front/functor_row.hpp> |
| 16 | #include <boost/msm/front/state_machine_def.hpp> |
| 17 | #ifndef BOOST_MSM_NONSTANDALONE_TEST |
| 18 | #define BOOST_TEST_MODULE back11_many_deferred_transitions |
| 19 | #endif |
| 20 | #include <boost/test/unit_test.hpp> |
| 21 | |
| 22 | namespace msm = boost::msm; |
| 23 | namespace msmf = boost::msm::front; |
| 24 | namespace mpl = boost::mpl; |
| 25 | |
| 26 | namespace |
| 27 | { |
| 28 | // ----- Events |
| 29 | struct Event1 {}; |
| 30 | struct Event2 {}; |
| 31 | struct Event3 {}; |
| 32 | struct Event4 {}; |
| 33 | |
| 34 | // ----- State machine |
| 35 | struct Sm1_ :msmf::state_machine_def<Sm1_> { |
| 36 | |
| 37 | // States |
| 38 | struct State1 :msmf::state<> { |
| 39 | template <class Event, class Fsm> |
| 40 | void on_entry(Event const&, Fsm&) const { |
| 41 | } |
| 42 | }; |
| 43 | struct State2 :msmf::state<> { |
| 44 | template <class Event, class Fsm> |
| 45 | void on_entry(Event const&, Fsm&) const { |
| 46 | } |
| 47 | }; |
| 48 | struct State3 :msmf::state<> { |
| 49 | template <class Event, class Fsm> |
| 50 | void on_entry(Event const&, Fsm&) const { |
| 51 | } |
| 52 | }; |
| 53 | struct State4 :msmf::state<> { |
| 54 | template <class Event, class Fsm> |
| 55 | void on_entry(Event const&, Fsm&) const { |
| 56 | } |
| 57 | }; |
| 58 | struct State5 :msmf::state<> { |
| 59 | template <class Event, class Fsm> |
| 60 | void on_entry(Event const&, Fsm&) const { |
| 61 | } |
| 62 | }; |
| 63 | // Set initial state |
| 64 | typedef State1 initial_state; |
| 65 | // Enable deferred capability |
| 66 | typedef int activate_deferred_events; |
| 67 | // Transition table |
| 68 | struct transition_table :mpl::vector< |
| 69 | // Start Event Next Action Guard |
| 70 | msmf::Row < State1, Event1, msmf::none, msmf::Defer>, |
| 71 | msmf::Row < State1, Event2, msmf::none, msmf::Defer>, |
| 72 | msmf::Row < State1, Event3, msmf::none, msmf::Defer>, |
| 73 | msmf::Row < State1, Event4, State2 , msmf::none>, |
| 74 | |
| 75 | msmf::Row < State2, Event3, msmf::none, msmf::Defer>, |
| 76 | msmf::Row < State2, Event1, msmf::none, msmf::Defer>, |
| 77 | msmf::Row < State2, Event2, State3 , msmf::none>, |
| 78 | |
| 79 | msmf::Row < State3, Event1, State4 , msmf::none>, |
| 80 | msmf::Row < State3, Event3, State5 , msmf::none>, |
| 81 | msmf::Row < State4, Event3, State5 , msmf::none>, |
| 82 | msmf::Row < State5, Event1, State4 , msmf::none> |
| 83 | > {}; |
| 84 | |
| 85 | template <class Fsm, class Event> |
| 86 | void no_transition(Event const&, Fsm&, int /*state*/) { |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | // Pick a back-end |
| 91 | typedef msm::back11::state_machine<Sm1_> Sm1; |
| 92 | |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(back11_many_deferred_transitions) |
| 95 | { |
| 96 | Sm1 sm1; |
| 97 | sm1.start(); |
| 98 | sm1.process_event(evt: Event1()); |
| 99 | sm1.process_event(evt: Event2()); |
| 100 | sm1.process_event(evt: Event3()); |
| 101 | sm1.process_event(evt: Event4()); |
| 102 | |
| 103 | BOOST_CHECK_MESSAGE(sm1.current_state()[0] == 4, "State5 should be active" ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |