// Copyright 2025 Christian Granzin // Copyright 2010 Christophe Henry // henry UNDERSCORE christophe AT hotmail DOT com // This is an extended version of the state machine available in the boost::mpl library // Distributed under the same license as the original. // Copyright for the original version: // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed // under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // back-end #include #include //front-end #include #include #include #include #ifndef BOOST_MSM_NONSTANDALONE_TEST #define BOOST_TEST_MODULE backmp11_members_test #endif #include namespace msm = boost::msm; namespace mp11 = boost::mp11; using namespace msm::front; using namespace msm::backmp11; namespace { template struct Counter { size_t count{}; }; // Events. struct TriggerTransition{}; struct TriggerInternalTransition{}; struct TriggerSmInternalTransition{}; struct TriggerAnyTransition{}; // Actions struct MyAction { template void operator()(const Event&, Fsm& fsm, Source&, Target&) { std::get>(fsm.action_calls).count++; } }; // States. struct MyState : public state<>{}; struct MyOtherState : public state<>{}; class StateMachine; struct StateMachine_; using Counters = std::tuple< Counter, Counter, Counter, Counter>; struct SmConfig : state_machine_config { using root_sm = StateMachine; // using compile_policy = favor_compile_time; }; struct StateMachine_ : public state_machine_def { using activate_deferred_events = int; template void on_entry(const Event& /*event*/, Fsm& fsm) { fsm.entry_calls++; }; template void on_exit(const Event& /*event*/, Fsm& fsm) { fsm.exit_calls++; }; using initial_state = MyState; using transition_table = mp11::mp_list< Row, Row, Row >; using internal_transition_table = mp11::mp_list< Internal, Internal >; }; // Leave this class without inheriting constructors to check // that this only needs to be done in case we use a context // (for convenience). class StateMachine : public state_machine { public: size_t entry_calls{}; size_t exit_calls{}; Counters action_calls{}; }; #define ASSERT_ONE_AND_RESET(value) BOOST_REQUIRE(value == 1); value = 0 BOOST_AUTO_TEST_CASE( backmp11_members_test ) { StateMachine test_machine; [[maybe_unused]] auto& events_queue = test_machine.get_events_queue(); [[maybe_unused]] const auto& const_events_queue = static_cast(&test_machine)->get_events_queue(); [[maybe_unused]] auto& deferred_events_queue = test_machine.get_deferred_events_queue(); [[maybe_unused]] const auto& const_deferred_events_queue = static_cast(&test_machine)->get_deferred_events_queue(); test_machine.start(); BOOST_REQUIRE(test_machine.entry_calls == 1); if constexpr (std::is_same_v< typename StateMachine::config_t::compile_policy, favor_runtime_speed>) { test_machine.process_event(TriggerAnyTransition{}); ASSERT_ONE_AND_RESET(std::get>(test_machine.action_calls).count); } test_machine.process_event(TriggerInternalTransition{}); ASSERT_ONE_AND_RESET(std::get>(test_machine.action_calls).count); // Ensure MyState consumes the event instead of StateMachine. test_machine.process_event(TriggerSmInternalTransition{}); ASSERT_ONE_AND_RESET(std::get>(test_machine.action_calls).count); test_machine.process_event(TriggerTransition{}); ASSERT_ONE_AND_RESET(std::get>(test_machine.action_calls).count); BOOST_REQUIRE(test_machine.is_state_active()); if constexpr (std::is_same_v< typename StateMachine::config_t::compile_policy, favor_runtime_speed>) { test_machine.process_event(TriggerAnyTransition{}); ASSERT_ONE_AND_RESET(std::get>(test_machine.action_calls).count); } test_machine.process_event(TriggerSmInternalTransition{}); ASSERT_ONE_AND_RESET(std::get>(test_machine.action_calls).count); test_machine.stop(); BOOST_REQUIRE(test_machine.exit_calls == 1); } } // namespace