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