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>
113 lines
2.7 KiB
C++
113 lines
2.7 KiB
C++
#include <Void/Data/Reflect.hpp>
|
|
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::Step;
|
|
|
|
namespace
|
|
{
|
|
// A machine that parks at a named state and exposes its data for
|
|
// serialize/restore.
|
|
class Pipeline : public Void::Fsm::Machine<Pipeline>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
struct Data {
|
|
int processed = 0;
|
|
std::string stage = "init";
|
|
};
|
|
Data data;
|
|
|
|
struct Ingest : State {
|
|
Task<Step> run()
|
|
{
|
|
co_await await_event<int>(); // park here
|
|
co_return next<Process>();
|
|
}
|
|
};
|
|
struct Process : State {
|
|
Task<Step> run()
|
|
{
|
|
auto& m = self();
|
|
m.data.processed += 1;
|
|
co_return succeed(m.data.processed);
|
|
}
|
|
};
|
|
|
|
Pipeline()
|
|
: Void::Fsm::Machine<Pipeline>(
|
|
MachineOptions{.own_worker = false})
|
|
{
|
|
initial<Ingest>();
|
|
add<Process>();
|
|
}
|
|
|
|
protected:
|
|
|
|
void on_serialize(Void::Json::Value& out) const override
|
|
{
|
|
out = Void::Json::to_json(data);
|
|
}
|
|
void on_restore(const Void::Json::Value& in) override
|
|
{
|
|
if (auto r = Void::Json::from_json<Data>(in); r.has_value()) {
|
|
data = std::move(*r);
|
|
}
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test fsm_serialize_captures_state_and_data()
|
|
{
|
|
Pipeline fsm;
|
|
fsm.data.processed = 5;
|
|
fsm.data.stage = "running";
|
|
fsm.start();
|
|
fsm.pump(); // enters Ingest, parks on await_event<int>
|
|
|
|
const auto json = fsm.serialize();
|
|
Void::Testing::expect_eq(json["state"].as_string(), std::string{"Ingest"});
|
|
Void::Testing::expect_eq(json["data"]["processed"].as_int(),
|
|
std::int64_t{5});
|
|
Void::Testing::expect_eq(json["data"]["stage"].as_string(),
|
|
std::string{"running"});
|
|
fsm.stop();
|
|
}
|
|
|
|
Void::Testing::Test fsm_restore_resumes_at_named_state()
|
|
{
|
|
const auto saved = Void::Json::parse(
|
|
R"({"state":"Process","data":{"processed":9,"stage":"mid"}})");
|
|
Void::Testing::expect(saved.has_value());
|
|
|
|
Pipeline fsm;
|
|
const auto r = fsm.restore(*saved);
|
|
Void::Testing::expect(r.has_value());
|
|
|
|
// Starts directly at Process (skipping Ingest's event wait), with the
|
|
// restored data → succeeds with processed+1.
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 10);
|
|
}
|
|
|
|
Void::Testing::Test fsm_restore_rejects_unknown_state_and_running()
|
|
{
|
|
const auto bogus = Void::Json::parse(R"({"state":"NoSuch","data":{}})");
|
|
Void::Testing::expect(bogus.has_value());
|
|
|
|
Pipeline fsm;
|
|
Void::Testing::expect(!fsm.restore(*bogus).has_value());
|
|
|
|
fsm.start();
|
|
Void::Testing::expect(!fsm.restore(*bogus).has_value()); // running
|
|
fsm.stop();
|
|
}
|