FSM v2 (A): CRTP typed access, States<> auto-registration, lifecycle, observers
Maintainability core, breaking the state API (all FSM tests migrated):
- Machine<Derived, ResultT> (CRTP). State::self() returns Derived& — drops
the static_cast<Derived&>(machine()) boilerplate from every state.
- `using States = Fsm::States<...>` auto-registers every state in the base
ctor (first entry = initial unless `using Initial = S`); no more manual
add<S>()/initial<S>() for static machines. next<S>() static_asserts S is
in the list (unregistered targets are now a COMPILE error, not a runtime
fail); next_dynamic<S>() + add<S>()/add_if<S>() remain for dynamic
machines.
- Per-state lifecycle: StateBase gains virtual on_enter()/on_exit()
(Coro::Task<void>, default no-op) run around run(); the engine visit is
enter -> run -> exit, so entry/exit actions split out of run(). run()
now has a default (fails) so reactive states (Phase B) needn't define it.
- Observability: virtual on_transition(from,to) (default logs via Void::Log)
+ observe(fn) called on every transition (and the initial entry) with a
Transition{from,to,step}. state_path()/state_names() added.
Migrated Basic/Events/Nesting/Serialize/Concurrency + the Orchestrator and
service-hot-reload tests to CRTP + self() + States<>; new Lifecycle suite
(enter/run/exit ordering, observer edges, default-run failure). 968 green,
ASan+UBSan clean, -Werror clean.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7b6cd7341a
commit
e941a48007
11 files changed
+355
-117
No files matched your search
@@ -22,7 +22,7 @@ namespace
|
||||
// Sums many Tick events delivered from several threads, finishing when
|
||||
// the running total reaches a target. Single consumer (the worker) so
|
||||
// the total is race-free without the FSM holding any lock.
|
||||
class Summer : public Void::Fsm::Machine<long long>
|
||||
class Summer : public Void::Fsm::Machine<Summer, long long>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace
|
||||
struct Run : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Summer&>(machine());
|
||||
auto& m = self();
|
||||
while (m.total < m.target) {
|
||||
auto t = co_await await_event<Tick>();
|
||||
m.total += t.n;
|
||||
@@ -42,7 +42,7 @@ namespace
|
||||
};
|
||||
|
||||
explicit Summer(long long tgt)
|
||||
: Void::Fsm::Machine<long long>(), target(tgt)
|
||||
: Void::Fsm::Machine<Summer, long long>(), target(tgt)
|
||||
{
|
||||
initial<Run>();
|
||||
}
|
||||
@@ -132,7 +132,7 @@ Void::Testing::Test fsm_current_state_reads_never_tear_under_load()
|
||||
|
||||
Void::Testing::Test fsm_resolves_service_through_context()
|
||||
{
|
||||
struct Calc : Void::Fsm::Machine<int> {
|
||||
struct Calc : Void::Fsm::Machine<Calc, int> {
|
||||
struct Go : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -140,7 +140,8 @@ Void::Testing::Test fsm_resolves_service_through_context()
|
||||
}
|
||||
};
|
||||
Calc()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Calc, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Go>();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user