There is now a single state base, `State`. The engine infers each state's
role purely from member signatures via P2996 — no `override`, no
framework-mandated names:
struct Work : State { // entry
Coro::Task<Step> run() { ... }
Coro::Task<Step> run(Cfg c) { ... } // startup-entry (launch data)
};
struct Idle : State { // reactive
void transitions(TransitionBuilder& on) { on.event<Go>().to<Run>(); }
};
struct After : State { // continuation
Coro::Task<Step> ok(ChildSuccess auto s) { use s.value; }
Coro::Task<Step> no(ChildFailure auto f) { use f.value; }
};
- Detail/Roles.hpp: ChildSuccess/ChildFailure concepts over boxes, plus the
reflection selectors (invoke_entry / invoke_startup / invoke_handler /
dispatch_result / has_transitions / make_driver). At add<S>() a per-state
driver thunk is stored (reactive table vs entry, by whether S has a
TransitionBuilder& member); drive() calls it. startMachine<Sub, Next>(data)
builds a Launch step carrying a launcher (injects data into Sub's initial
startup-entry) + a dispatcher (dispatch_result<Next, Sub>) that builds the
ChildSuccess/Failure box and calls Next's matching handler.
- Removed Reactive/Continue<R> bases, StateKind/kind(), StateBase::run/resume
(only on_enter/on_exit remain virtual). Reactive states keep the same DSL.
- Toolchain note: template-for + member splicing fails inside a coroutine
body in this GCC build, so the selectors are non-coroutines returning a
Task<Step> (plain Step wrapped via ready_task). Discovery is
invocability-based — no parameter-type reflection (P3096). Probe-verified.
All FSM tests migrated to the nameless style (SubMachine: Continue ->
ChildSuccess/Failure handlers + startup-entry seeding; reactive states drop
the Reactive base/override). 995 tests (992 green, 3 skipped), ASan clean,
-Werror clean; lone intermittent failure remains the pre-existing racy
plugin hot-reload-under-load test.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
126 lines
2.8 KiB
C++
126 lines
2.8 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::Step;
|
|
using Void::Fsm::Transition;
|
|
|
|
namespace
|
|
{
|
|
// Records enter/run/exit for each state into a shared log.
|
|
class Lifecycle : public Void::Fsm::Machine<Lifecycle>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
std::vector<std::string> log;
|
|
|
|
struct A : State {
|
|
Task<void> on_enter() override
|
|
{
|
|
self().log.push_back("enter A");
|
|
co_return;
|
|
}
|
|
Task<Step> run()
|
|
{
|
|
self().log.push_back("run A");
|
|
co_return next<B>();
|
|
}
|
|
Task<void> on_exit() override
|
|
{
|
|
self().log.push_back("exit A");
|
|
co_return;
|
|
}
|
|
};
|
|
|
|
struct B : State {
|
|
Task<void> on_enter() override
|
|
{
|
|
self().log.push_back("enter B");
|
|
co_return;
|
|
}
|
|
Task<Step> run()
|
|
{
|
|
self().log.push_back("run B");
|
|
co_return succeed(1);
|
|
}
|
|
Task<void> on_exit() override
|
|
{
|
|
self().log.push_back("exit B");
|
|
co_return;
|
|
}
|
|
};
|
|
|
|
using States = Void::Fsm::States<A, B>;
|
|
|
|
Lifecycle()
|
|
: Void::Fsm::Machine<Lifecycle>(
|
|
MachineOptions{.own_worker = false})
|
|
{
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test fsm_lifecycle_enter_run_exit_order()
|
|
{
|
|
Lifecycle fsm;
|
|
(void)fsm.run_blocking();
|
|
|
|
const std::vector<std::string> expected{"enter A", "run A", "exit A",
|
|
"enter B", "run B", "exit B"};
|
|
Void::Testing::expect(fsm.log == expected);
|
|
}
|
|
|
|
Void::Testing::Test fsm_observer_sees_every_transition()
|
|
{
|
|
Lifecycle fsm;
|
|
std::vector<std::string> edges;
|
|
fsm.observe([&](const Transition& t) {
|
|
edges.push_back((t.from.empty() ? "*" : t.from) + "->" + t.to);
|
|
});
|
|
(void)fsm.run_blocking();
|
|
|
|
// Initial entry (*->A), then A->B.
|
|
const std::vector<std::string> expected{"*->A", "A->B"};
|
|
Void::Testing::expect(edges == expected);
|
|
}
|
|
|
|
Void::Testing::Test fsm_state_names_and_path()
|
|
{
|
|
Lifecycle fsm;
|
|
const auto& names = fsm.state_names();
|
|
Void::Testing::expect_eq(names.size(), std::size_t{2});
|
|
Void::Testing::expect_eq(names[0], std::string{"A"});
|
|
Void::Testing::expect_eq(names[1], std::string{"B"});
|
|
|
|
fsm.start();
|
|
fsm.pump();
|
|
Void::Testing::expect(fsm.finished());
|
|
}
|
|
|
|
// A state defining only on_enter (no run) falls through to the default
|
|
// run() which fails — confirms the default body exists.
|
|
Void::Testing::Test fsm_state_without_run_fails()
|
|
{
|
|
struct NoRun : Void::Fsm::Machine<NoRun> {
|
|
struct Stuck : State {
|
|
Task<void> on_enter() override { co_return; }
|
|
// no run() override
|
|
};
|
|
using States = Void::Fsm::States<Stuck>;
|
|
NoRun()
|
|
: Void::Fsm::Machine<NoRun>(MachineOptions{.own_worker = false})
|
|
{
|
|
}
|
|
} fsm;
|
|
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(!outcome.has_value());
|
|
}
|