1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
// 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
// Generate the favor_compile_time SM manually.
#define BOOST_MSM_BACKMP11_MANUAL_GENERATION
#include <boost/msm/backmp11/favor_compile_time.hpp>
#include <boost/msm/backmp11/state_machine.hpp>
//front-end
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#ifndef BOOST_MSM_NONSTANDALONE_TEST
#define BOOST_TEST_MODULE backmp11_visitor_test
#endif
#include <boost/test/unit_test.hpp>
#include "Backmp11.hpp"
namespace msm = boost::msm;
namespace mp11 = boost::mp11;
using namespace msm::front;
using namespace msm::backmp11;
namespace
{
class CountVisitor
{
public:
CountVisitor(int& visits) : m_visits(visits) {}
template<typename State>
void operator()(State& state)
{
m_visits += 1;
state.visits += 1;
}
private:
int& m_visits;
};
class ResetCountVisitor
{
public:
ResetCountVisitor(int& visits) : m_visits(visits) {}
template<typename State>
void operator()(State& state)
{
m_visits = 0;
state.visits = 0;
}
private:
int& m_visits;
};
class NoOpVisitor
{
public:
template<typename State>
void operator()(State& /*state*/) {}
};
class NoOpConstVisitor
{
public:
template<typename State>
void operator()(State& /*state*/) const {}
};
// Events.
struct EnterSubFsm{};
struct ExitSubFsm{};
// States.
struct StateBase
{
size_t visits;
};
struct DefaultState : public state<StateBase> {};
template<typename T>
struct MachineBase_ : public state_machine_def<MachineBase_<T>>
{
using initial_state = DefaultState;
size_t visits;
};
template<typename Config = default_state_machine_config>
struct hierarchical_state_machine
{
struct LowerMachine_ : public MachineBase_<LowerMachine_> { };
using LowerMachine = state_machine<LowerMachine_, Config>;
struct MiddleMachine_ : public MachineBase_<MiddleMachine_>
{
using transition_table = mp11::mp_list<
Row< DefaultState , EnterSubFsm , LowerMachine >,
Row< LowerMachine , ExitSubFsm , DefaultState >
>;
};
using MiddleMachine = state_machine<MiddleMachine_, Config>;
struct UpperMachine_ : public MachineBase_<UpperMachine_>
{
using transition_table = mp11::mp_list<
Row< DefaultState , EnterSubFsm , MiddleMachine>,
Row< MiddleMachine , ExitSubFsm , DefaultState>
>;
};
using UpperMachine = state_machine<UpperMachine_, Config>;
};
using TestMachines = boost::mpl::vector<
hierarchical_state_machine<>,
hierarchical_state_machine<favor_compile_time_config>
>;
BOOST_AUTO_TEST_CASE_TEMPLATE( backmp11_visitor_test, TestMachine, TestMachines )
{
using UpperMachine = typename TestMachine::UpperMachine;
using MiddleMachine = typename TestMachine::MiddleMachine;
using LowerMachine = typename TestMachine::LowerMachine;
int visits = 0;
ResetCountVisitor reset_count_visitor{visits};
CountVisitor count_visitor{visits};
UpperMachine upper_machine{};
auto& upper_machine_state = upper_machine.template get_state<DefaultState&>();
auto& middle_machine = upper_machine.template get_state<MiddleMachine&>();
auto& middle_machine_state = middle_machine.template get_state<DefaultState&>();
auto& lower_machine = middle_machine.template get_state<LowerMachine&>();
auto& lower_machine_state = lower_machine.template get_state<DefaultState&>();
// Test visitor signature & compilation (non-const sm).
{
upper_machine.visit(NoOpConstVisitor{});
upper_machine.visit(NoOpVisitor{});
NoOpConstVisitor v0{};
NoOpVisitor v1{};
upper_machine.visit(v0);
upper_machine.visit(v1);
const NoOpConstVisitor v0_const{};
upper_machine.visit(v0_const);
upper_machine.visit([](auto&) mutable {});
}
// Test visitor signature & compilation (const sm).
// Requires more refactoring, visitor_dispatch_table
// with a Sm template param to preserve constness.
// {
// const UpperMachine& upper_machine_c{upper_machine};
// upper_machine_c.visit(NoOpConstVisitor{});
// upper_machine_c.visit(NoOpVisitor{});
// NoOpConstVisitor v0{};
// NoOpVisitor v1{};
// upper_machine_c.visit(v0);
// upper_machine_c.visit(v1);
// const NoOpConstVisitor v0_c{};
// upper_machine_c.visit(v0_c);
// upper_machine_c.visit([](auto&) mutable {});
// }
// Test active states
{
upper_machine.visit(count_visitor);
BOOST_REQUIRE(visits == 0);
upper_machine.start();
BOOST_REQUIRE(upper_machine.template is_state_active<DefaultState>() == true);
BOOST_REQUIRE(upper_machine.template is_state_active<MiddleMachine>() == false);
upper_machine.visit(count_visitor);
BOOST_REQUIRE(upper_machine_state.visits == 1);
BOOST_REQUIRE(visits == 1);
upper_machine.visit(reset_count_visitor);
upper_machine.process_event(EnterSubFsm());
BOOST_REQUIRE(upper_machine.template is_state_active<DefaultState>() == true);
BOOST_REQUIRE(upper_machine.template is_state_active<MiddleMachine>() == true);
BOOST_REQUIRE(middle_machine.template is_state_active<DefaultState>() == true);
BOOST_REQUIRE(middle_machine.template is_state_active<LowerMachine>() == false);
upper_machine.visit(count_visitor);
BOOST_REQUIRE(middle_machine.visits == 1);
BOOST_REQUIRE(middle_machine_state.visits == 1);
BOOST_REQUIRE(visits == 2);
upper_machine.visit(reset_count_visitor);
upper_machine.process_event(EnterSubFsm());
BOOST_REQUIRE(upper_machine.template is_state_active<MiddleMachine>() == true);
BOOST_REQUIRE(upper_machine.template is_state_active<LowerMachine>() == true);
BOOST_REQUIRE(middle_machine.template is_state_active<LowerMachine>() == true);
BOOST_REQUIRE(lower_machine.template is_state_active<DefaultState>() == true);
upper_machine.visit(count_visitor);
BOOST_REQUIRE(middle_machine.visits == 1);
BOOST_REQUIRE(lower_machine.visits == 1);
BOOST_REQUIRE(lower_machine_state.visits == 1);
BOOST_REQUIRE(visits == 3);
upper_machine.visit(reset_count_visitor);
upper_machine.stop();
upper_machine.visit(count_visitor);
BOOST_REQUIRE(visits == 0);
}
using vm = msm::backmp11::visit_mode;
// Test all states
{
upper_machine.template visit<vm::all_states>(count_visitor);
BOOST_CHECK(upper_machine_state.visits == 1);
BOOST_CHECK(middle_machine.visits == 1);
BOOST_CHECK(visits == 2);
upper_machine.template visit<vm::all_states>(reset_count_visitor);
upper_machine.template visit<vm::all_recursive>(count_visitor);
BOOST_CHECK(upper_machine_state.visits == 1);
BOOST_CHECK(middle_machine.visits == 1);
BOOST_CHECK(middle_machine_state.visits == 1);
BOOST_CHECK(lower_machine.visits == 1);
BOOST_CHECK(lower_machine_state.visits == 1);
BOOST_CHECK(visits == 5);
}
}
}
using TestMachine = hierarchical_state_machine<favor_compile_time_config>;
BOOST_MSM_BACKMP11_GENERATE_STATE_MACHINE(TestMachine::UpperMachine);
BOOST_MSM_BACKMP11_GENERATE_STATE_MACHINE(TestMachine::MiddleMachine);
BOOST_MSM_BACKMP11_GENERATE_STATE_MACHINE(TestMachine::LowerMachine);
|