FSM: typed Success/Failure result model (reflected nested types)
A machine is now Machine<Derived> (dropping the ResultT template param) and
declares its result types as nested types reflected *by name* — Success and
Failure (a struct or a using-alias), each defaulting to void:
class Login : public Machine<Login> {
using Success = Token; // or a nested struct; default void
struct Failure { int code; };
};
- Outcome = std::expected<Success, Failure> (void failure -> std::monostate
error arm). succeed(Success{...}) / fail(Failure{...}) carry the typed
values (nullary when void). Resolved by plain dependent-name lookup
(Detail/Result.hpp), no P2996.
- Engine/system faults (no initial state, unregistered target, no entry)
travel a SEPARATE FsmError channel: status()==Faulted + system_error(),
never the typed Failure. New Step::Kind::Fault carries it.
- CRTP wrinkle: Derived is incomplete at base instantiation, so the engine
stores the result type-erased (std::any) and rebuilds the typed Outcome in
wait()/run_blocking()/take_outcome() — function templates defaulted on
Derived. run_child/run_region/start_machine reconstruct OutcomeOf<Sub> at
the boundary (Sub is complete).
- Step carries Success/Failure payloads as std::any; the reactive DSL
(finish/fail) and Continue<Sub> are reparameterised off the nested types.
Interim: Continue<Sub> keeps the named on_success/on_failure form (Phase 2
replaces it with signature-inferred ChildSuccess/ChildFailure). All FSM
tests migrated to the nested-type form. 995 tests (992 green, 3 skipped),
ASan clean, -Werror clean; the lone intermittent failure is the
pre-existing racy plugin hot-reload-under-load test (flaky on HEAD too).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7c7c590d79
commit
9e9eb838ea
20 files changed
+487
-245
No files matched your search
@@ -147,7 +147,9 @@ Other key points:
|
||||
|
||||
### Fsm module (inheritance + coroutine workflow)
|
||||
|
||||
`Void::Fsm::Machine<ResultT>` — derive it; declare states as nested structs deriving `State`; each implements `Coro::Task<Step> run()` returning `next<S>()` / `succeed(...)` / `fail(...)`. There is no statechart/event-graph; states drive themselves.
|
||||
`Void::Fsm::Machine<Derived>` (CRTP) — derive it; declare states as nested structs deriving `State`; each implements `Coro::Task<Step> run()` returning `next<S>()` / `succeed(...)` / `fail(...)`. There is no statechart/event-graph; states drive themselves.
|
||||
|
||||
**Typed results.** A machine declares its result types as nested types *reflected by name* — `struct Success {…}` (or `using Success = int;`) and `struct Failure {…}`, each defaulting to `void`. `Outcome = std::expected<Success, Failure>` (a void failure becomes `std::monostate`); `succeed(Success{…})` / `fail(Failure{…})` carry them (nullary when void). Engine/system faults (no initial state, jump to an unregistered state, a state with no entry) are a **separate `FsmError` channel** — `status()==Faulted`, `system_error()` returns the `FsmError`, never the typed `Failure`. Because `Derived` is incomplete at CRTP base instantiation, the engine stores outcomes type-erased and rebuilds the typed `Outcome` in `wait()`/`run_blocking()` (function templates defaulted on `Derived`); `Detail::SuccessOf`/`FailureOf<Derived>` resolve the nested types by plain name lookup (`Detail/Result.hpp`).
|
||||
|
||||
Header layout (include `<Void/Fsm/Fsm.hpp>` — a thin umbrella): `Machine.hpp` (the `Machine<Derived, ResultT>` template + `States<>`, nested `State`/`Reactive`/`Continue` bases, lifecycle, events, nesting, persistence); `Detail/Reactive.hpp` (the declarative transition DSL as standalone `Detail::TransitionBuilder`/`EventClause`/`AfterClause`/`TransitionTable<Derived,…>` templates, aliased by `Machine::Reactive`); `Detail/Awaiter.hpp` (one `Detail::EventAwaiter` behind `make_event_awaiter`, used by all `await_*`); `Detail/Continuation.hpp` (`Detail::Continuation<State,R>` = `Continue<R>`, with a `void` specialization); `Detail/Introspect.hpp` (`StateInfo`/`EdgeInfo` + `to_mermaid`); `Detail/Engine.{hpp,cpp}` (the non-template `ExecutionContext` + `StateBase`); `Detail/TypeName.hpp` (`type_name<T>()`).
|
||||
|
||||
|
||||
@@ -2,86 +2,75 @@
|
||||
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Fsm/Detail/Engine.hpp>
|
||||
#include <Void/Fsm/Detail/Result.hpp>
|
||||
#include <Void/Fsm/Step.hpp>
|
||||
|
||||
#include <any>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
|
||||
namespace Void::Fsm::Detail
|
||||
{
|
||||
namespace Coro = Void::Coro;
|
||||
|
||||
// Base for a state that resumes from a sub-machine launched via
|
||||
// startMachine<Sub, This>(). `StateT` is the owning machine's nested State
|
||||
// (so the continuation inherits self()/next/fail/...); `SubResult` is the
|
||||
// sub-machine's result_type. The engine enters this state carrying the
|
||||
// startMachine<Sub, This>(). `StateT` is the owning machine's nested State;
|
||||
// `Sub` is the launched machine. The engine enters this state carrying the
|
||||
// sub's LaunchResult and calls resume(), which dispatches to on_success /
|
||||
// on_failure — the FSM "finds on its own which method to call".
|
||||
// on_failure by the outcome. Machine exposes it as `Continue<Sub>`.
|
||||
//
|
||||
// Machine exposes it as `Continue<R>`; users derive it and implement
|
||||
// on_success(R) (and optionally on_failure).
|
||||
template <typename StateT, typename SubResult>
|
||||
class Continuation : public StateT
|
||||
// on_success takes the sub's typed Success, on_failure its typed Failure
|
||||
// (a void result becomes a std::monostate argument).
|
||||
//
|
||||
// NOTE: this is the interim Phase-1 form; Phase 2 replaces it with
|
||||
// signature-inferred ChildSuccess/ChildFailure handlers.
|
||||
template <typename StateT, typename Sub> class Continuation : public StateT
|
||||
{
|
||||
using Succ = SuccessOf<Sub>;
|
||||
using Fail = FailureOf<Sub>;
|
||||
using SuccArg =
|
||||
std::conditional_t<std::is_void_v<Succ>, std::monostate, Succ>;
|
||||
using FailArg =
|
||||
std::conditional_t<std::is_void_v<Fail>, std::monostate, Fail>;
|
||||
|
||||
public:
|
||||
|
||||
Coro::Task<Step> resume(LaunchResult lr) final
|
||||
{
|
||||
if (lr.ok) {
|
||||
if (lr.kind == LaunchResult::Kind::Failure) {
|
||||
if constexpr (std::is_void_v<Fail>) {
|
||||
co_return co_await on_failure(std::monostate{});
|
||||
} else {
|
||||
co_return co_await on_failure(
|
||||
std::any_cast<Fail>(std::move(lr.failure)));
|
||||
}
|
||||
}
|
||||
if constexpr (std::is_void_v<Succ>) {
|
||||
co_return co_await on_success(std::monostate{});
|
||||
} else {
|
||||
co_return co_await on_success(
|
||||
std::any_cast<SubResult>(std::move(lr.value)));
|
||||
std::any_cast<Succ>(std::move(lr.value)));
|
||||
}
|
||||
co_return co_await on_failure(std::move(lr.error));
|
||||
}
|
||||
|
||||
Coro::Task<Step> run() override
|
||||
{
|
||||
co_return this->fail(
|
||||
"Continue<R> state entered without a sub-machine result "
|
||||
"(reach it via startMachine<Sub, This>())");
|
||||
co_return Step::fault(FsmError{
|
||||
"continuation state entered without a sub-machine result "
|
||||
"(reach it via startMachine<Sub, This>())"});
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
/// The sub-machine succeeded with `result`.
|
||||
virtual Coro::Task<Step> on_success(SubResult result) = 0;
|
||||
virtual Coro::Task<Step> on_success(SuccArg result) = 0;
|
||||
|
||||
/// The sub-machine failed. Default: fail the parent with the same
|
||||
/// message; override to recover.
|
||||
virtual Coro::Task<Step> on_failure(FsmError error)
|
||||
/// Default: a sub-machine failure the parent didn't handle becomes
|
||||
/// a system fault. Override to recover.
|
||||
virtual Coro::Task<Step> on_failure(FailArg /*error*/)
|
||||
{
|
||||
co_return this->fail(std::move(error.message));
|
||||
}
|
||||
};
|
||||
|
||||
// Specialisation for a void-result sub-machine: on_success() takes no
|
||||
// argument.
|
||||
template <typename StateT> class Continuation<StateT, void> : public StateT
|
||||
{
|
||||
public:
|
||||
|
||||
Coro::Task<Step> resume(LaunchResult lr) final
|
||||
{
|
||||
if (lr.ok) {
|
||||
co_return co_await on_success();
|
||||
}
|
||||
co_return co_await on_failure(std::move(lr.error));
|
||||
}
|
||||
|
||||
Coro::Task<Step> run() override
|
||||
{
|
||||
co_return this->fail(
|
||||
"Continue<void> state entered without a sub-machine result "
|
||||
"(reach it via startMachine<Sub, This>())");
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
virtual Coro::Task<Step> on_success() = 0;
|
||||
|
||||
virtual Coro::Task<Step> on_failure(FsmError error)
|
||||
{
|
||||
co_return this->fail(std::move(error.message));
|
||||
co_return Step::fault(
|
||||
FsmError{"unhandled sub-machine failure"});
|
||||
}
|
||||
};
|
||||
} // namespace Void::Fsm::Detail
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Fsm/Detail/Result.hpp>
|
||||
#include <Void/Fsm/Detail/TypeName.hpp>
|
||||
#include <Void/Fsm/Step.hpp>
|
||||
|
||||
@@ -15,9 +16,10 @@
|
||||
|
||||
// The declarative transition DSL for reactive states, lifted out of the
|
||||
// Machine template so the core stays small. Every type is parameterised by the
|
||||
// owning machine's `Derived` (guards/actions take `Derived&`) and `ResultT`
|
||||
// (finish() carries the machine's result). Machine::Reactive aliases these and
|
||||
// the engine drives them; see include/Void/Fsm/Machine.hpp.
|
||||
// owning machine's `Derived` (guards/actions take `Derived&`); the machine's
|
||||
// typed Success/Failure (nested types, default void) are reflected from
|
||||
// `Derived` for finish()/fail(). Machine::Reactive aliases these and the engine
|
||||
// drives them; see include/Void/Fsm/Machine.hpp.
|
||||
|
||||
namespace Void::Fsm::Detail
|
||||
{
|
||||
@@ -44,8 +46,8 @@ namespace Void::Fsm::Detail
|
||||
};
|
||||
|
||||
/// Fluent builder for one event row: `on.event<E>().guard(..).action(..)
|
||||
/// .to<S>()` (or `.finish(v)` / `.fail(msg)`).
|
||||
template <typename Derived, typename ResultT, typename E> class EventClause
|
||||
/// .to<S>()` (or `.finish(v)` / `.fail(v)`).
|
||||
template <typename Derived, typename E> class EventClause
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -88,21 +90,31 @@ namespace Void::Fsm::Detail
|
||||
}
|
||||
|
||||
void finish()
|
||||
requires std::is_void_v<ResultT>
|
||||
requires std::is_void_v<SuccessOf<Derived>>
|
||||
{
|
||||
commit(Step::success(std::any{}), "(success)");
|
||||
}
|
||||
template <typename V>
|
||||
void finish(V&& value)
|
||||
requires(!std::is_void_v<ResultT>)
|
||||
requires(!std::is_void_v<SuccessOf<Derived>>)
|
||||
{
|
||||
commit(Step::success(std::any{ResultT(std::forward<V>(value))}),
|
||||
commit(Step::success(std::any{
|
||||
SuccessOf<Derived>(std::forward<V>(value))}),
|
||||
"(success)");
|
||||
}
|
||||
|
||||
void fail(std::string message)
|
||||
void fail()
|
||||
requires std::is_void_v<FailureOf<Derived>>
|
||||
{
|
||||
commit(Step::failure(FsmError{std::move(message)}), "(fail)");
|
||||
commit(Step::failure(std::any{}), "(fail)");
|
||||
}
|
||||
template <typename V>
|
||||
void fail(V&& value)
|
||||
requires(!std::is_void_v<FailureOf<Derived>>)
|
||||
{
|
||||
commit(Step::failure(std::any{
|
||||
FailureOf<Derived>{std::forward<V>(value)}}),
|
||||
"(fail)");
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -136,8 +148,8 @@ namespace Void::Fsm::Detail
|
||||
std::function<Coro::Task<void>(Derived&, const E&)> m_action;
|
||||
};
|
||||
|
||||
/// Fluent builder for the timeout row: `on.after(d).to<S>()` / `.fail(..)`.
|
||||
template <typename Derived, typename ResultT> class AfterClause
|
||||
/// Fluent builder for the timeout row: `on.after(d).to<S>()` / `.fail(v)`.
|
||||
template <typename Derived> class AfterClause
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -159,12 +171,22 @@ namespace Void::Fsm::Detail
|
||||
m_table->timeout_target_name = type_name<S>();
|
||||
}
|
||||
|
||||
void fail(std::string message)
|
||||
void fail()
|
||||
requires std::is_void_v<FailureOf<Derived>>
|
||||
{
|
||||
m_table->has_timeout = true;
|
||||
m_table->timeout_after = m_after;
|
||||
m_table->timeout_step =
|
||||
Step::failure(FsmError{std::move(message)});
|
||||
m_table->timeout_step = Step::failure(std::any{});
|
||||
m_table->timeout_target_name = "(fail)";
|
||||
}
|
||||
template <typename V>
|
||||
void fail(V&& value)
|
||||
requires(!std::is_void_v<FailureOf<Derived>>)
|
||||
{
|
||||
m_table->has_timeout = true;
|
||||
m_table->timeout_after = m_after;
|
||||
m_table->timeout_step = Step::failure(
|
||||
std::any{FailureOf<Derived>{std::forward<V>(value)}});
|
||||
m_table->timeout_target_name = "(fail)";
|
||||
}
|
||||
|
||||
@@ -175,7 +197,7 @@ namespace Void::Fsm::Detail
|
||||
};
|
||||
|
||||
/// Entry point handed to `transitions(TransitionBuilder& on)`.
|
||||
template <typename Derived, typename ResultT> class TransitionBuilder
|
||||
template <typename Derived> class TransitionBuilder
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -184,17 +206,16 @@ namespace Void::Fsm::Detail
|
||||
{
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
[[nodiscard]] EventClause<Derived, ResultT, E> event()
|
||||
template <typename E> [[nodiscard]] EventClause<Derived, E> event()
|
||||
{
|
||||
return EventClause<Derived, ResultT, E>{*m_table};
|
||||
return EventClause<Derived, E>{*m_table};
|
||||
}
|
||||
|
||||
template <typename Rep, typename Period>
|
||||
[[nodiscard]] AfterClause<Derived, ResultT>
|
||||
[[nodiscard]] AfterClause<Derived>
|
||||
after(std::chrono::duration<Rep, Period> d)
|
||||
{
|
||||
return AfterClause<Derived, ResultT>{
|
||||
return AfterClause<Derived>{
|
||||
*m_table, std::chrono::duration_cast<
|
||||
std::chrono::steady_clock::duration>(d)};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#include <expected>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
|
||||
// A machine declares its result types as nested types reflected *by name* —
|
||||
// `Success` / `Failure`, either a struct or a `using` alias — each defaulting
|
||||
// to void:
|
||||
// struct Login : Machine<Login> {
|
||||
// struct Success { Token token; }; // or: using Success = int;
|
||||
// struct Failure { int code; };
|
||||
// };
|
||||
// Resolved by plain dependent-name lookup (the names are fixed, so no P2996 is
|
||||
// needed), and shaped into the machine Outcome.
|
||||
|
||||
namespace Void::Fsm::Detail
|
||||
{
|
||||
template <typename D> consteval auto success_probe()
|
||||
{
|
||||
if constexpr (requires { typename D::Success; }) {
|
||||
return std::type_identity<typename D::Success>{};
|
||||
} else {
|
||||
return std::type_identity<void>{};
|
||||
}
|
||||
}
|
||||
template <typename D> consteval auto failure_probe()
|
||||
{
|
||||
if constexpr (requires { typename D::Failure; }) {
|
||||
return std::type_identity<typename D::Failure>{};
|
||||
} else {
|
||||
return std::type_identity<void>{};
|
||||
}
|
||||
}
|
||||
|
||||
/// The machine's success result type (nested `Success`, else void).
|
||||
template <typename D>
|
||||
using SuccessOf = typename decltype(success_probe<D>())::type;
|
||||
/// The machine's failure result type (nested `Failure`, else void).
|
||||
template <typename D>
|
||||
using FailureOf = typename decltype(failure_probe<D>())::type;
|
||||
|
||||
/// std::expected can't have a void error arm — map void failure to a unit.
|
||||
template <typename F>
|
||||
using ErrArm = std::conditional_t<std::is_void_v<F>, std::monostate, F>;
|
||||
|
||||
/// The user-facing outcome: typed success value or typed failure value.
|
||||
/// (Engine/system faults travel a separate FsmError channel, not this.)
|
||||
template <typename D>
|
||||
using OutcomeOf = std::expected<SuccessOf<D>, ErrArm<FailureOf<D>>>;
|
||||
} // namespace Void::Fsm::Detail
|
||||
+156
-68
@@ -8,6 +8,7 @@
|
||||
#include <Void/Fsm/Detail/Engine.hpp>
|
||||
#include <Void/Fsm/Detail/Introspect.hpp>
|
||||
#include <Void/Fsm/Detail/Reactive.hpp>
|
||||
#include <Void/Fsm/Detail/Result.hpp>
|
||||
#include <Void/Fsm/Detail/TypeName.hpp>
|
||||
#include <Void/Fsm/Step.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
@@ -89,13 +90,25 @@ namespace Void::Fsm
|
||||
// };
|
||||
// using States = Void::Fsm::States<Check>;
|
||||
// };
|
||||
template <typename Derived, typename ResultT = void> class Machine
|
||||
template <typename Derived> class Machine
|
||||
{
|
||||
public:
|
||||
|
||||
using result_type = ResultT;
|
||||
using Options = MachineOptions;
|
||||
using Outcome = std::expected<ResultT, FsmError>;
|
||||
|
||||
// Result types are reflected by name from Derived's nested
|
||||
// `Success`/`Failure` (default void). Because Derived is incomplete
|
||||
// at base instantiation (CRTP), they cannot appear in member
|
||||
// signatures or data members — only inside function templates /
|
||||
// bodies, where Derived is complete. So these are alias *templates*
|
||||
// (lazily resolved), the engine stores outcomes type-erased, and
|
||||
// the typed Outcome is rebuilt in wait()/take_outcome().
|
||||
template <typename D = Derived>
|
||||
using Success = Detail::SuccessOf<D>;
|
||||
template <typename D = Derived>
|
||||
using Failure = Detail::FailureOf<D>;
|
||||
template <typename D = Derived>
|
||||
using Outcome = Detail::OutcomeOf<D>;
|
||||
|
||||
// ── State base ────────────────────────────────────────────────
|
||||
class State : public Detail::StateBase
|
||||
@@ -188,22 +201,36 @@ namespace Void::Fsm
|
||||
co_return false;
|
||||
}
|
||||
|
||||
/// Finish with the machine's typed Success result (nested
|
||||
/// `Success`; nullary when it is void).
|
||||
[[nodiscard]] Step succeed() const
|
||||
requires std::is_void_v<ResultT>
|
||||
requires std::is_void_v<Detail::SuccessOf<Derived>>
|
||||
{
|
||||
return Step::success(std::any{});
|
||||
}
|
||||
template <typename V>
|
||||
[[nodiscard]] Step succeed(V&& value) const
|
||||
requires(!std::is_void_v<ResultT>)
|
||||
requires(!std::is_void_v<Detail::SuccessOf<Derived>>)
|
||||
{
|
||||
return Step::success(
|
||||
std::any{ResultT(std::forward<V>(value))});
|
||||
std::any{Detail::SuccessOf<Derived>(
|
||||
std::forward<V>(value))});
|
||||
}
|
||||
|
||||
[[nodiscard]] Step fail(std::string message) const
|
||||
/// Finish with the machine's typed Failure result (nested
|
||||
/// `Failure`; nullary when it is void).
|
||||
[[nodiscard]] Step fail() const
|
||||
requires std::is_void_v<Detail::FailureOf<Derived>>
|
||||
{
|
||||
return Step::failure(FsmError{std::move(message)});
|
||||
return Step::failure(std::any{});
|
||||
}
|
||||
template <typename V>
|
||||
[[nodiscard]] Step fail(V&& value) const
|
||||
requires(!std::is_void_v<Detail::FailureOf<Derived>>)
|
||||
{
|
||||
return Step::failure(
|
||||
std::any{Detail::FailureOf<Derived>{
|
||||
std::forward<V>(value)}});
|
||||
}
|
||||
|
||||
template <typename I> [[nodiscard]] I& service() const
|
||||
@@ -265,10 +292,9 @@ namespace Void::Fsm
|
||||
[[nodiscard]] Step startMachine(Args&&... args) const
|
||||
{
|
||||
static_assert(
|
||||
std::is_base_of_v<
|
||||
Continue<typename Sub::result_type>, Next>,
|
||||
std::is_base_of_v<Continue<Sub>, Next>,
|
||||
"startMachine<Sub, Next>: Next must derive "
|
||||
"Continue<Sub::result_type>");
|
||||
"Continue<Sub>");
|
||||
return m_machine->template start_machine<Sub, Next>(
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
@@ -298,8 +324,8 @@ namespace Void::Fsm
|
||||
// Coro::Task<Step> on_failure(FsmError e) override {
|
||||
// co_return fail(e.message); }
|
||||
// };
|
||||
template <typename SubResult>
|
||||
using Continue = Detail::Continuation<State, SubResult>;
|
||||
template <typename Sub>
|
||||
using Continue = Detail::Continuation<State, Sub>;
|
||||
|
||||
// ── reactive (declarative) states ─────────────────────────────
|
||||
//
|
||||
@@ -321,8 +347,7 @@ namespace Void::Fsm
|
||||
// The declarative transition DSL lives in Detail/Reactive.hpp;
|
||||
// these aliases give reactive states the ergonomic nested names.
|
||||
using TransitionTable = Detail::TransitionTable<Derived>;
|
||||
using TransitionBuilder =
|
||||
Detail::TransitionBuilder<Derived, ResultT>;
|
||||
using TransitionBuilder = Detail::TransitionBuilder<Derived>;
|
||||
|
||||
class Reactive : public State
|
||||
{
|
||||
@@ -549,7 +574,9 @@ namespace Void::Fsm
|
||||
}
|
||||
}
|
||||
|
||||
Outcome wait()
|
||||
// Function templates so the typed Outcome resolves where Derived is
|
||||
// complete (CRTP). Call sites stay `fsm.wait()` / `run_blocking()`.
|
||||
template <typename D = Derived> Outcome<D> wait()
|
||||
{
|
||||
if (m_ctx) {
|
||||
if (m_options.own_worker && m_owns_ctx) {
|
||||
@@ -562,13 +589,20 @@ namespace Void::Fsm
|
||||
}
|
||||
}
|
||||
}
|
||||
return take_outcome();
|
||||
return take_outcome<D>();
|
||||
}
|
||||
|
||||
Outcome run_blocking()
|
||||
template <typename D = Derived> Outcome<D> run_blocking()
|
||||
{
|
||||
start();
|
||||
return wait();
|
||||
return wait<D>();
|
||||
}
|
||||
|
||||
/// The system fault (if any) — a programming error caught by the
|
||||
/// engine, separate from a typed Failure result.
|
||||
[[nodiscard]] std::optional<FsmError> system_error() const
|
||||
{
|
||||
return m_system_error;
|
||||
}
|
||||
|
||||
std::size_t pump() { return m_ctx ? m_ctx->pump() : 0; }
|
||||
@@ -576,7 +610,8 @@ namespace Void::Fsm
|
||||
[[nodiscard]] bool finished() const noexcept
|
||||
{
|
||||
const auto s = m_status.load();
|
||||
return s == Status::Succeeded || s == Status::Failed;
|
||||
return s == Status::Succeeded || s == Status::Failed ||
|
||||
s == Status::Faulted;
|
||||
}
|
||||
|
||||
[[nodiscard]] Status status() const noexcept
|
||||
@@ -605,7 +640,8 @@ namespace Void::Fsm
|
||||
m_ctx.reset();
|
||||
m_owns_ctx = false;
|
||||
m_current.store(-1, std::memory_order_release);
|
||||
m_outcome.reset();
|
||||
m_result_value.reset();
|
||||
m_system_error.reset();
|
||||
m_pending_result.reset();
|
||||
m_jump.store(nullptr, std::memory_order_release);
|
||||
m_step = 0;
|
||||
@@ -764,12 +800,13 @@ namespace Void::Fsm
|
||||
|
||||
// ── nesting ───────────────────────────────────────────────────
|
||||
template <typename Sub, typename... Args>
|
||||
[[nodiscard]] Coro::Task<typename Sub::Outcome>
|
||||
[[nodiscard]] Coro::Task<Detail::OutcomeOf<Sub>>
|
||||
run_child(Args&&... args)
|
||||
{
|
||||
Sub child{std::forward<Args>(args)...};
|
||||
child.attach(m_ctx, m_services);
|
||||
co_return co_await child.drive();
|
||||
co_await child.drive();
|
||||
co_return child.template take_outcome<Sub>();
|
||||
}
|
||||
|
||||
/// Composite sub-state: run `Sub` as a child region sharing this
|
||||
@@ -779,15 +816,15 @@ namespace Void::Fsm
|
||||
/// caller maps to a sibling transition. The calling state's
|
||||
/// on_enter/on_exit naturally wrap the region.
|
||||
template <typename Sub, typename... Args>
|
||||
[[nodiscard]] Coro::Task<typename Sub::Outcome>
|
||||
[[nodiscard]] Coro::Task<Detail::OutcomeOf<Sub>>
|
||||
run_region(Args&&... args)
|
||||
{
|
||||
Sub child{std::forward<Args>(args)...};
|
||||
child.attach(m_ctx, m_services);
|
||||
m_region_probe = [&child] { return child.state_path(); };
|
||||
auto outcome = co_await child.drive();
|
||||
co_await child.drive();
|
||||
m_region_probe = nullptr;
|
||||
co_return std::move(outcome);
|
||||
co_return child.template take_outcome<Sub>();
|
||||
}
|
||||
|
||||
/// Build a Launch step: run `Sub` (constructed from `args`) on this
|
||||
@@ -805,22 +842,26 @@ namespace Void::Fsm
|
||||
[this,
|
||||
tup = std::make_tuple(std::forward<Args>(
|
||||
args)...)]() mutable -> Coro::Task<LaunchResult> {
|
||||
auto outcome = co_await std::apply(
|
||||
[this](auto&&... a) {
|
||||
return this->template run_child<Sub>(
|
||||
std::forward<decltype(a)>(a)...);
|
||||
},
|
||||
std::move(tup));
|
||||
// Run Sub on the shared executor and read its raw result so
|
||||
// Success / Failure / Fault are all distinguishable.
|
||||
Sub child = std::make_from_tuple<Sub>(std::move(tup));
|
||||
child.attach(m_ctx, m_services);
|
||||
co_await child.drive();
|
||||
LaunchResult lr;
|
||||
if (outcome.has_value()) {
|
||||
lr.ok = true;
|
||||
if constexpr (!std::is_void_v<
|
||||
typename Sub::result_type>) {
|
||||
lr.value = std::any{std::move(*outcome)};
|
||||
}
|
||||
} else {
|
||||
lr.ok = false;
|
||||
lr.error = std::move(outcome.error());
|
||||
switch (child.m_status.load(std::memory_order_acquire)) {
|
||||
case Status::Succeeded:
|
||||
lr.kind = LaunchResult::Kind::Success;
|
||||
lr.value = std::move(child.m_result_value);
|
||||
break;
|
||||
case Status::Failed:
|
||||
lr.kind = LaunchResult::Kind::Failure;
|
||||
lr.failure = std::move(child.m_result_value);
|
||||
break;
|
||||
default:
|
||||
lr.kind = LaunchResult::Kind::Fault;
|
||||
lr.fault = child.m_system_error.value_or(
|
||||
FsmError{"sub-machine fault"});
|
||||
break;
|
||||
}
|
||||
co_return lr;
|
||||
};
|
||||
@@ -962,7 +1003,7 @@ namespace Void::Fsm
|
||||
|
||||
private:
|
||||
|
||||
template <typename, typename>
|
||||
template <typename>
|
||||
friend class Machine; // run_child reaches child internals
|
||||
|
||||
void attach(std::shared_ptr<Detail::ExecutionContext> ctx,
|
||||
@@ -1040,13 +1081,26 @@ namespace Void::Fsm
|
||||
}
|
||||
}
|
||||
|
||||
// The state loop: build each state, run on_enter → run →
|
||||
// on_exit, follow the returned Step. Runs on the executor.
|
||||
Coro::Task<Outcome> drive()
|
||||
// Record a system fault (programming error) — separate from a
|
||||
// typed Failure result. Sets status Faulted + system_error.
|
||||
void fault(std::string message)
|
||||
{
|
||||
m_system_error = FsmError{std::move(message)};
|
||||
Void::Log::error("Fsm", "system fault: {}",
|
||||
m_system_error->message);
|
||||
m_status.store(Status::Faulted, std::memory_order_release);
|
||||
}
|
||||
|
||||
// The state loop. Type-erased: stores the Success/Failure payload
|
||||
// in m_result_value and sets m_status; the typed Outcome is rebuilt
|
||||
// in take_outcome() where Derived is complete. Runs on the
|
||||
// executor.
|
||||
Coro::Task<void> drive()
|
||||
{
|
||||
const std::type_info* cur = m_initial;
|
||||
if (cur == nullptr) {
|
||||
co_return std::unexpected{FsmError{"no initial state"}};
|
||||
fault("no initial state");
|
||||
co_return;
|
||||
}
|
||||
for (;;) {
|
||||
// Apply an external jump<S>() at this transition boundary.
|
||||
@@ -1058,18 +1112,22 @@ namespace Void::Fsm
|
||||
}
|
||||
const auto it = m_factories.find(std::type_index{*cur});
|
||||
if (it == m_factories.end()) {
|
||||
co_return std::unexpected{
|
||||
FsmError{"transition to unregistered state"}};
|
||||
fault("transition to unregistered state");
|
||||
co_return;
|
||||
}
|
||||
set_current(cur);
|
||||
auto state = it->second();
|
||||
co_await state->on_enter();
|
||||
// A state entered via startMachine resumes from the sub's
|
||||
// result (Continue<R> dispatches); otherwise it runs.
|
||||
// result; otherwise it runs.
|
||||
Step step;
|
||||
if (m_pending_result.has_value()) {
|
||||
LaunchResult pending = std::move(*m_pending_result);
|
||||
m_pending_result.reset();
|
||||
if (pending.kind == LaunchResult::Kind::Fault) {
|
||||
fault(std::move(pending.fault.message));
|
||||
co_return;
|
||||
}
|
||||
step = co_await state->resume(std::move(pending));
|
||||
} else {
|
||||
step = co_await state->run();
|
||||
@@ -1081,43 +1139,70 @@ namespace Void::Fsm
|
||||
cur = step.target();
|
||||
continue;
|
||||
case Step::Kind::Launch:
|
||||
// Run the sub-machine, stash its result, then enter
|
||||
// the Next (Continue<R>) state.
|
||||
m_pending_result.emplace(
|
||||
co_await step.run_launch());
|
||||
cur = step.target();
|
||||
continue;
|
||||
case Step::Kind::Success:
|
||||
if constexpr (std::is_void_v<ResultT>) {
|
||||
co_return Outcome{};
|
||||
} else {
|
||||
co_return Outcome{std::any_cast<ResultT>(
|
||||
std::move(step.result()))};
|
||||
}
|
||||
m_result_value = std::move(step.payload());
|
||||
m_status.store(Status::Succeeded,
|
||||
std::memory_order_release);
|
||||
co_return;
|
||||
case Step::Kind::Fail:
|
||||
co_return std::unexpected{step.error()};
|
||||
m_result_value = std::move(step.payload());
|
||||
m_status.store(Status::Failed,
|
||||
std::memory_order_release);
|
||||
co_return;
|
||||
case Step::Kind::Fault:
|
||||
fault(std::any_cast<FsmError>(step.payload())
|
||||
.message);
|
||||
co_return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Coro::Task<void> driver()
|
||||
{
|
||||
Outcome outcome = co_await drive();
|
||||
const bool ok = outcome.has_value();
|
||||
m_outcome.emplace(std::move(outcome));
|
||||
m_status.store(ok ? Status::Succeeded : Status::Failed,
|
||||
std::memory_order_release);
|
||||
co_await drive();
|
||||
if (m_owns_ctx) {
|
||||
m_ctx->finish();
|
||||
}
|
||||
}
|
||||
|
||||
Outcome take_outcome()
|
||||
// Rebuild the typed Outcome from the type-erased result. Function
|
||||
// template so SuccessOf/FailureOf<Derived> resolve where Derived is
|
||||
// complete. A system fault yields a default-constructed error arm
|
||||
// (consult system_error()).
|
||||
template <typename D = Derived> Outcome<D> take_outcome()
|
||||
{
|
||||
if (m_outcome.has_value()) {
|
||||
return std::move(*m_outcome);
|
||||
using Succ = Detail::SuccessOf<D>;
|
||||
using Fail = Detail::FailureOf<D>;
|
||||
using Err = Detail::ErrArm<Fail>;
|
||||
switch (m_status.load(std::memory_order_acquire)) {
|
||||
case Status::Succeeded:
|
||||
if constexpr (std::is_void_v<Succ>) {
|
||||
return Outcome<D>{};
|
||||
} else {
|
||||
return Outcome<D>{
|
||||
std::any_cast<Succ>(m_result_value)};
|
||||
}
|
||||
case Status::Failed:
|
||||
if constexpr (std::is_void_v<Fail>) {
|
||||
return Outcome<D>{std::unexpect, std::monostate{}};
|
||||
} else {
|
||||
return Outcome<D>{
|
||||
std::unexpect,
|
||||
std::any_cast<Fail>(m_result_value)};
|
||||
}
|
||||
default: // Faulted / not finished
|
||||
static_assert(
|
||||
std::is_default_constructible_v<Err>,
|
||||
"a machine with a non-default-constructible "
|
||||
"Failure "
|
||||
"cannot represent a system fault in its Outcome; "
|
||||
"check system_error()");
|
||||
return Outcome<D>{std::unexpect, Err{}};
|
||||
}
|
||||
return std::unexpected{FsmError{"machine did not finish"}};
|
||||
}
|
||||
|
||||
Options m_options;
|
||||
@@ -1137,7 +1222,10 @@ namespace Void::Fsm
|
||||
|
||||
std::atomic<int> m_current{-1};
|
||||
std::atomic<Status> m_status{Status::Idle};
|
||||
std::optional<Outcome> m_outcome;
|
||||
// Type-erased finished result (Success or Failure payload), rebuilt
|
||||
// typed in take_outcome(); system faults in m_system_error.
|
||||
std::any m_result_value;
|
||||
std::optional<FsmError> m_system_error;
|
||||
std::function<void(const Transition&)> m_observer;
|
||||
std::vector<Interceptor> m_interceptors;
|
||||
std::uint64_t m_step{0};
|
||||
|
||||
+42
-31
@@ -12,35 +12,40 @@ namespace Void::Fsm
|
||||
{
|
||||
namespace Coro = Void::Coro;
|
||||
|
||||
/// An engine/system fault — a programming error in the machine itself (no
|
||||
/// initial state, transition to an unregistered state, …). Distinct from a
|
||||
/// machine's typed `Failure` result: faults travel a separate channel
|
||||
/// (Machine::system_error()), never the Outcome.
|
||||
struct FsmError {
|
||||
std::string message;
|
||||
};
|
||||
|
||||
/// The outcome of a sub-machine launched via startMachine<Sub, Next>():
|
||||
/// `ok` with the result in `value` (a `Sub::result_type`, empty for void),
|
||||
/// or `!ok` with `error`. Delivered to the Next state's Continue<R>
|
||||
/// handlers (on_success / on_failure).
|
||||
struct LaunchResult {
|
||||
bool ok{false};
|
||||
std::any value;
|
||||
FsmError error;
|
||||
};
|
||||
|
||||
enum class Status {
|
||||
Idle, // constructed, not started
|
||||
Running, // worker active
|
||||
Succeeded, // finished with a result
|
||||
Failed // finished with an error
|
||||
Succeeded, // finished with a typed Success
|
||||
Failed, // finished with a typed Failure
|
||||
Faulted // stopped by a system fault (see system_error())
|
||||
};
|
||||
|
||||
/// The outcome a state's run() returns: move to another state, finish
|
||||
/// the machine with a result, or fail. Built only through the State
|
||||
/// helpers (next<S>() / succeed() / fail()).
|
||||
/// The outcome of a sub-machine launched via startMachine<Sub, Next>():
|
||||
/// a typed Success value, a typed Failure value, or a system fault.
|
||||
struct LaunchResult {
|
||||
enum class Kind { Success, Failure, Fault };
|
||||
Kind kind{Kind::Fault};
|
||||
std::any value; // Success payload (kind == Success)
|
||||
std::any failure; // Failure payload (kind == Failure)
|
||||
FsmError fault; // system fault (kind == Fault)
|
||||
};
|
||||
|
||||
/// What a state's entry returns: move to another state, finish with a typed
|
||||
/// Success / Failure, or launch a sub-machine. Built only through the State
|
||||
/// helpers (next<S>() / succeed(...) / fail(...) / startMachine).
|
||||
class Step
|
||||
{
|
||||
public:
|
||||
|
||||
enum class Kind { Transition, Success, Fail, Launch };
|
||||
enum class Kind { Transition, Success, Fail, Launch, Fault };
|
||||
|
||||
[[nodiscard]] static Step transition(const std::type_info& target)
|
||||
{
|
||||
@@ -50,9 +55,18 @@ namespace Void::Fsm
|
||||
return s;
|
||||
}
|
||||
|
||||
/// Launch a sub-machine, then enter `target` (a Continue<R> state)
|
||||
/// with the sub's LaunchResult. `launcher` runs the sub and yields
|
||||
/// its result; the engine awaits it in drive().
|
||||
/// Signal a system fault (programming error) — distinct from a
|
||||
/// typed Failure. Routed to Machine::system_error().
|
||||
[[nodiscard]] static Step fault(FsmError error)
|
||||
{
|
||||
Step s;
|
||||
s.m_kind = Kind::Fault;
|
||||
s.m_payload = std::any{std::move(error)};
|
||||
return s;
|
||||
}
|
||||
|
||||
/// Launch a sub-machine, then enter `target` (a continuation state)
|
||||
/// with the sub's LaunchResult.
|
||||
[[nodiscard]] static Step
|
||||
launch(const std::type_info& target,
|
||||
std::function<Coro::Task<LaunchResult>()> launcher)
|
||||
@@ -64,19 +78,21 @@ namespace Void::Fsm
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Step success(std::any result)
|
||||
/// Finish with a typed Success payload (empty std::any for void).
|
||||
[[nodiscard]] static Step success(std::any value)
|
||||
{
|
||||
Step s;
|
||||
s.m_kind = Kind::Success;
|
||||
s.m_result = std::move(result);
|
||||
s.m_payload = std::move(value);
|
||||
return s;
|
||||
}
|
||||
|
||||
[[nodiscard]] static Step failure(FsmError error)
|
||||
/// Finish with a typed Failure payload (empty std::any for void).
|
||||
[[nodiscard]] static Step failure(std::any value)
|
||||
{
|
||||
Step s;
|
||||
s.m_kind = Kind::Fail;
|
||||
s.m_error = std::move(error);
|
||||
s.m_payload = std::move(value);
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -87,12 +103,8 @@ namespace Void::Fsm
|
||||
return m_target;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::any& result() noexcept { return m_result; }
|
||||
|
||||
[[nodiscard]] const FsmError& error() const noexcept
|
||||
{
|
||||
return m_error;
|
||||
}
|
||||
/// The Success or Failure payload (by kind).
|
||||
[[nodiscard]] std::any& payload() noexcept { return m_payload; }
|
||||
|
||||
/// Run the sub-machine for a Launch step (yields its LaunchResult).
|
||||
[[nodiscard]] Coro::Task<LaunchResult> run_launch()
|
||||
@@ -104,8 +116,7 @@ namespace Void::Fsm
|
||||
|
||||
Kind m_kind{Kind::Fail};
|
||||
const std::type_info* m_target{nullptr};
|
||||
std::any m_result;
|
||||
FsmError m_error;
|
||||
std::any m_payload;
|
||||
std::function<Coro::Task<LaunchResult>()> m_launcher;
|
||||
};
|
||||
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ namespace Void::Fsm::Detail
|
||||
Coro::Task<void> StateBase::on_exit() { co_return; }
|
||||
Coro::Task<Step> StateBase::run()
|
||||
{
|
||||
co_return Step::failure(FsmError{"state defines no run()"});
|
||||
co_return Step::fault(FsmError{"state defines no entry"});
|
||||
}
|
||||
|
||||
Coro::Task<Step> StateBase::resume(LaunchResult /*result*/)
|
||||
|
||||
@@ -37,10 +37,12 @@ namespace
|
||||
// An FSM that drives the plugin-provided ICalc service: each Work step
|
||||
// accumulates and loops until a target total is reached. It resolves
|
||||
// the service every step, so it picks up a reloaded implementation.
|
||||
class Accumulate : public Void::Fsm::Machine<Accumulate, int>
|
||||
class Accumulate : public Void::Fsm::Machine<Accumulate>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Work : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
|
||||
@@ -12,10 +12,12 @@ using Void::Fsm::Step;
|
||||
namespace
|
||||
{
|
||||
// A linear workflow: Start -> Work (loops 3x) -> success(count).
|
||||
class Counter : public Void::Fsm::Machine<Counter, int>
|
||||
class Counter : public Void::Fsm::Machine<Counter>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
int count = 0;
|
||||
std::vector<std::string>* log = nullptr;
|
||||
|
||||
@@ -47,16 +49,19 @@ namespace
|
||||
using States = Void::Fsm::States<Start, Work>;
|
||||
|
||||
Counter()
|
||||
: Void::Fsm::Machine<Counter, int>(
|
||||
: Void::Fsm::Machine<Counter>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class Failing : public Void::Fsm::Machine<Failing, int>
|
||||
class Failing : public Void::Fsm::Machine<Failing>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
using Failure = Void::Fsm::FsmError;
|
||||
|
||||
struct Boom : State {
|
||||
Task<Step> run() override { co_return fail("kaboom"); }
|
||||
};
|
||||
@@ -64,7 +69,7 @@ namespace
|
||||
using States = Void::Fsm::States<Boom>;
|
||||
|
||||
Failing()
|
||||
: Void::Fsm::Machine<Failing, int>(
|
||||
: Void::Fsm::Machine<Failing>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
}
|
||||
@@ -107,13 +112,13 @@ Void::Testing::Test fsm_pump_drives_to_completion()
|
||||
|
||||
Void::Testing::Test fsm_void_result_succeeds()
|
||||
{
|
||||
struct Trivial : Void::Fsm::Machine<Trivial, void> {
|
||||
struct Trivial : Void::Fsm::Machine<Trivial> {
|
||||
struct Done : State {
|
||||
Task<Step> run() override { co_return succeed(); }
|
||||
};
|
||||
using States = Void::Fsm::States<Done>;
|
||||
Trivial()
|
||||
: Void::Fsm::Machine<Trivial, void>(
|
||||
: Void::Fsm::Machine<Trivial>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
}
|
||||
@@ -127,7 +132,9 @@ Void::Testing::Test fsm_dynamic_state_registration()
|
||||
{
|
||||
// Dynamic machine: states registered conditionally, addressed with
|
||||
// next_dynamic<S>() (skips the compile-time States<> check).
|
||||
struct Flow : Void::Fsm::Machine<Flow, int> {
|
||||
struct Flow : Void::Fsm::Machine<Flow> {
|
||||
using Success = int;
|
||||
|
||||
bool express;
|
||||
struct Express : State {
|
||||
Task<Step> run() override { co_return succeed(99); }
|
||||
@@ -145,8 +152,7 @@ Void::Testing::Test fsm_dynamic_state_registration()
|
||||
}
|
||||
};
|
||||
explicit Flow(bool e)
|
||||
: Void::Fsm::Machine<Flow, int>(
|
||||
MachineOptions{.own_worker = false}),
|
||||
: Void::Fsm::Machine<Flow>(MachineOptions{.own_worker = false}),
|
||||
express(e)
|
||||
{
|
||||
initial<Start>();
|
||||
|
||||
@@ -22,10 +22,12 @@ 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<Summer, long long>
|
||||
class Summer : public Void::Fsm::Machine<Summer>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = long long;
|
||||
|
||||
long long total = 0;
|
||||
long long target;
|
||||
|
||||
@@ -42,7 +44,7 @@ namespace
|
||||
};
|
||||
|
||||
explicit Summer(long long tgt)
|
||||
: Void::Fsm::Machine<Summer, long long>(), target(tgt)
|
||||
: Void::Fsm::Machine<Summer>(), target(tgt)
|
||||
{
|
||||
initial<Run>();
|
||||
}
|
||||
@@ -132,7 +134,9 @@ Void::Testing::Test fsm_current_state_reads_never_tear_under_load()
|
||||
|
||||
Void::Testing::Test fsm_resolves_service_through_context()
|
||||
{
|
||||
struct Calc : Void::Fsm::Machine<Calc, int> {
|
||||
struct Calc : Void::Fsm::Machine<Calc> {
|
||||
using Success = int;
|
||||
|
||||
struct Go : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -140,8 +144,7 @@ Void::Testing::Test fsm_resolves_service_through_context()
|
||||
}
|
||||
};
|
||||
Calc()
|
||||
: Void::Fsm::Machine<Calc, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Calc>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Go>();
|
||||
}
|
||||
|
||||
@@ -20,10 +20,12 @@ namespace
|
||||
};
|
||||
|
||||
// Sums two Add events, posted in one fluent send() call.
|
||||
class Sum : public Fsm::Machine<Sum, int>
|
||||
class Sum : public Fsm::Machine<Sum>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
int total = 0;
|
||||
|
||||
struct Take : State {
|
||||
@@ -55,10 +57,12 @@ namespace
|
||||
{
|
||||
// A and C are not connected by any declared edge; goto_state<C>() jumps
|
||||
// from A to C freely.
|
||||
class Hop : public Fsm::Machine<Hop, int>
|
||||
class Hop : public Fsm::Machine<Hop>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct A : State {
|
||||
Task<Step> run() override { co_return goto_state<C>(); }
|
||||
};
|
||||
@@ -88,10 +92,12 @@ namespace
|
||||
|
||||
// Loops in Loop on each Tick; an external jump<Done>() redirects at the
|
||||
// next boundary.
|
||||
class Jumper : public Fsm::Machine<Jumper, int>
|
||||
class Jumper : public Fsm::Machine<Jumper>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Loop : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -104,10 +110,7 @@ namespace
|
||||
};
|
||||
using States = Fsm::States<Loop, Done>;
|
||||
|
||||
explicit Jumper(MachineOptions o = {})
|
||||
: Fsm::Machine<Jumper, int>(o)
|
||||
{
|
||||
}
|
||||
explicit Jumper(MachineOptions o = {}) : Fsm::Machine<Jumper>(o) {}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
@@ -136,10 +139,12 @@ Void::Testing::Test jump_redirects_at_next_boundary()
|
||||
namespace
|
||||
{
|
||||
// Increments a machine-owned counter each run (data survives reset()).
|
||||
class Counter : public Fsm::Machine<Counter, int>
|
||||
class Counter : public Fsm::Machine<Counter>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
int runs = 0;
|
||||
|
||||
struct Once : State {
|
||||
@@ -165,10 +170,12 @@ Void::Testing::Test restart_reruns_the_machine()
|
||||
|
||||
namespace
|
||||
{
|
||||
class Pick : public Fsm::Machine<Pick, int>
|
||||
class Pick : public Fsm::Machine<Pick>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct A : State {
|
||||
Task<Step> run() override { co_return succeed(1); }
|
||||
};
|
||||
@@ -194,10 +201,12 @@ Void::Testing::Test restart_at_changes_initial_state()
|
||||
|
||||
namespace
|
||||
{
|
||||
class Mut : public Fsm::Machine<Mut, int>
|
||||
class Mut : public Fsm::Machine<Mut>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct A : State {
|
||||
Task<Step> run() override { co_return succeed(1); }
|
||||
};
|
||||
@@ -245,10 +254,12 @@ namespace
|
||||
struct Go {
|
||||
};
|
||||
|
||||
class Sleeper : public Fsm::Machine<Sleeper, int>
|
||||
class Sleeper : public Fsm::Machine<Sleeper>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct W : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
|
||||
@@ -20,10 +20,12 @@ namespace
|
||||
};
|
||||
|
||||
// Waits for Input events, accumulating until a Stop event arrives.
|
||||
class Accumulator : public Void::Fsm::Machine<Accumulator, int>
|
||||
class Accumulator : public Void::Fsm::Machine<Accumulator>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
int total = 0;
|
||||
std::vector<int>* order = nullptr;
|
||||
|
||||
@@ -45,7 +47,7 @@ namespace
|
||||
};
|
||||
|
||||
explicit Accumulator(bool own_worker)
|
||||
: Void::Fsm::Machine<Accumulator, int>(
|
||||
: Void::Fsm::Machine<Accumulator>(
|
||||
MachineOptions{.own_worker = own_worker})
|
||||
{
|
||||
initial<Waiting>();
|
||||
|
||||
@@ -24,10 +24,13 @@ namespace
|
||||
|
||||
// Child region: waits for either Tick (succeed) or Cancel (fail),
|
||||
// handling both at one level via await_any.
|
||||
class Work : public Void::Fsm::Machine<Work, std::string>
|
||||
class Work : public Void::Fsm::Machine<Work>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = std::string;
|
||||
using Failure = Void::Fsm::FsmError;
|
||||
|
||||
struct Waiting : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -41,17 +44,19 @@ namespace
|
||||
using States = Void::Fsm::States<Waiting>;
|
||||
|
||||
explicit Work(MachineOptions opts = {})
|
||||
: Void::Fsm::Machine<Work, std::string>(opts)
|
||||
: Void::Fsm::Machine<Work>(opts)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
// Parent: a composite Running state enters the Work region and maps its
|
||||
// result — success passes the value through, failure routes to a sibling.
|
||||
class Job : public Void::Fsm::Machine<Job, std::string>
|
||||
class Job : public Void::Fsm::Machine<Job>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = std::string;
|
||||
|
||||
struct Running : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -71,7 +76,7 @@ namespace
|
||||
using States = Void::Fsm::States<Running, Aborted>;
|
||||
|
||||
explicit Job(MachineOptions opts = {})
|
||||
: Void::Fsm::Machine<Job, std::string>(opts)
|
||||
: Void::Fsm::Machine<Job>(opts)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
@@ -13,10 +13,12 @@ using Void::Fsm::Transition;
|
||||
namespace
|
||||
{
|
||||
// Records enter/run/exit for each state into a shared log.
|
||||
class Lifecycle : public Void::Fsm::Machine<Lifecycle, int>
|
||||
class Lifecycle : public Void::Fsm::Machine<Lifecycle>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
std::vector<std::string> log;
|
||||
|
||||
struct A : State {
|
||||
@@ -58,7 +60,7 @@ namespace
|
||||
using States = Void::Fsm::States<A, B>;
|
||||
|
||||
Lifecycle()
|
||||
: Void::Fsm::Machine<Lifecycle, int>(
|
||||
: Void::Fsm::Machine<Lifecycle>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
}
|
||||
@@ -106,15 +108,14 @@ Void::Testing::Test fsm_state_names_and_path()
|
||||
// run() which fails — confirms the default body exists.
|
||||
Void::Testing::Test fsm_state_without_run_fails()
|
||||
{
|
||||
struct NoRun : Void::Fsm::Machine<NoRun, void> {
|
||||
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, void>(
|
||||
MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<NoRun>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
}
|
||||
} fsm;
|
||||
|
||||
@@ -11,10 +11,12 @@ using Void::Fsm::Step;
|
||||
namespace
|
||||
{
|
||||
// Child machine: computes a value and succeeds.
|
||||
class Subtotal : public Void::Fsm::Machine<Subtotal, int>
|
||||
class Subtotal : public Void::Fsm::Machine<Subtotal>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
int seed;
|
||||
|
||||
struct Compute : State {
|
||||
@@ -26,7 +28,7 @@ namespace
|
||||
};
|
||||
|
||||
explicit Subtotal(int s)
|
||||
: Void::Fsm::Machine<Subtotal, int>(
|
||||
: Void::Fsm::Machine<Subtotal>(
|
||||
MachineOptions{.own_worker = false}),
|
||||
seed(s)
|
||||
{
|
||||
@@ -35,15 +37,18 @@ namespace
|
||||
};
|
||||
|
||||
// Child machine that fails.
|
||||
class FailingChild : public Void::Fsm::Machine<FailingChild, int>
|
||||
class FailingChild : public Void::Fsm::Machine<FailingChild>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
using Failure = Void::Fsm::FsmError;
|
||||
|
||||
struct Boom : State {
|
||||
Task<Step> run() override { co_return fail("child boom"); }
|
||||
};
|
||||
FailingChild()
|
||||
: Void::Fsm::Machine<FailingChild, int>(
|
||||
: Void::Fsm::Machine<FailingChild>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Boom>();
|
||||
@@ -51,10 +56,13 @@ namespace
|
||||
};
|
||||
|
||||
// Parent: enters a child, uses its result to decide the outcome.
|
||||
class Parent : public Void::Fsm::Machine<Parent, int>
|
||||
class Parent : public Void::Fsm::Machine<Parent>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
using Failure = Void::Fsm::FsmError;
|
||||
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -68,17 +76,20 @@ namespace
|
||||
};
|
||||
|
||||
Parent()
|
||||
: Void::Fsm::Machine<Parent, int>(
|
||||
: Void::Fsm::Machine<Parent>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>();
|
||||
}
|
||||
};
|
||||
|
||||
class ParentOfFailure : public Void::Fsm::Machine<ParentOfFailure, int>
|
||||
class ParentOfFailure : public Void::Fsm::Machine<ParentOfFailure>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
using Failure = Void::Fsm::FsmError;
|
||||
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -91,7 +102,7 @@ namespace
|
||||
};
|
||||
|
||||
ParentOfFailure()
|
||||
: Void::Fsm::Machine<ParentOfFailure, int>(
|
||||
: Void::Fsm::Machine<ParentOfFailure>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>();
|
||||
@@ -121,7 +132,9 @@ Void::Testing::Test fsm_nesting_with_worker_thread()
|
||||
{
|
||||
Parent fsm{}; // embedder by default here
|
||||
// Re-run as worker-driven to exercise the shared executor + worker.
|
||||
struct WorkerParent : Void::Fsm::Machine<WorkerParent, int> {
|
||||
struct WorkerParent : Void::Fsm::Machine<WorkerParent> {
|
||||
using Success = int;
|
||||
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
|
||||
@@ -19,10 +19,13 @@ namespace
|
||||
{
|
||||
// Attempt fails the first `fail_n` times, then succeeds. with_retry
|
||||
// keeps trying; next_or routes exhaustion to a fallback.
|
||||
class Flaky : public Void::Fsm::Machine<Flaky, std::string>
|
||||
class Flaky : public Void::Fsm::Machine<Flaky>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = std::string;
|
||||
using Failure = Void::Fsm::FsmError;
|
||||
|
||||
int fail_n = 0;
|
||||
int attempts = 0;
|
||||
int retries; // how many tries with_retry gets
|
||||
@@ -82,10 +85,12 @@ Void::Testing::Test retry_exhausts_routes_to_fallback()
|
||||
|
||||
namespace
|
||||
{
|
||||
class Pipe : public Void::Fsm::Machine<Pipe, int>
|
||||
class Pipe : public Void::Fsm::Machine<Pipe>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct A : State {
|
||||
Task<Step> run() override { co_return next<B>(); }
|
||||
};
|
||||
@@ -94,8 +99,7 @@ namespace
|
||||
};
|
||||
using States = Void::Fsm::States<A, B>;
|
||||
Pipe()
|
||||
: Void::Fsm::Machine<Pipe, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Pipe>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
}
|
||||
};
|
||||
@@ -145,10 +149,12 @@ namespace
|
||||
struct Cancel {
|
||||
};
|
||||
|
||||
class Flow : public Void::Fsm::Machine<Flow, int>
|
||||
class Flow : public Void::Fsm::Machine<Flow>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Idle : Reactive {
|
||||
void transitions(TransitionBuilder& on) override
|
||||
{
|
||||
|
||||
@@ -22,10 +22,13 @@ namespace
|
||||
|
||||
// A declarative machine: Idle reacts to events with guards/actions; on
|
||||
// timeout it bails out.
|
||||
class Gate : public Void::Fsm::Machine<Gate, int>
|
||||
class Gate : public Void::Fsm::Machine<Gate>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
using Failure = Void::Fsm::FsmError;
|
||||
|
||||
int total = 0;
|
||||
std::vector<std::string> trace;
|
||||
|
||||
@@ -87,10 +90,12 @@ namespace
|
||||
};
|
||||
|
||||
// Imperative state using await_event_for: times out → expired.
|
||||
class Sleeper : public Void::Fsm::Machine<Sleeper, std::string>
|
||||
class Sleeper : public Void::Fsm::Machine<Sleeper>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = std::string;
|
||||
|
||||
struct Waiting : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -129,10 +134,12 @@ Void::Testing::Test await_event_for_receives_event()
|
||||
namespace
|
||||
{
|
||||
// Reactive timeout transition (on.after().to<>).
|
||||
class Deadline : public Void::Fsm::Machine<Deadline, int>
|
||||
class Deadline : public Void::Fsm::Machine<Deadline>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Pending : Reactive {
|
||||
void transitions(TransitionBuilder& on) override
|
||||
{
|
||||
|
||||
@@ -13,10 +13,12 @@ namespace
|
||||
{
|
||||
// A machine that parks at a named state and exposes its data for
|
||||
// serialize/restore.
|
||||
class Pipeline : public Void::Fsm::Machine<Pipeline, int>
|
||||
class Pipeline : public Void::Fsm::Machine<Pipeline>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Data {
|
||||
int processed = 0;
|
||||
std::string stage = "init";
|
||||
@@ -40,7 +42,7 @@ namespace
|
||||
};
|
||||
|
||||
Pipeline()
|
||||
: Void::Fsm::Machine<Pipeline, int>(
|
||||
: Void::Fsm::Machine<Pipeline>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Ingest>();
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::FsmError;
|
||||
@@ -16,10 +17,13 @@ namespace Fsm = Void::Fsm;
|
||||
namespace
|
||||
{
|
||||
// Sub-machine seeded by constructor arg: doubles `seed`, fails if negative.
|
||||
class Doubler : public Fsm::Machine<Doubler, int>
|
||||
class Doubler : public Fsm::Machine<Doubler>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
using Failure = Void::Fsm::FsmError;
|
||||
|
||||
int seed;
|
||||
|
||||
struct Run : State {
|
||||
@@ -36,10 +40,12 @@ namespace
|
||||
explicit Doubler(int s = 0) : seed(s) {}
|
||||
};
|
||||
|
||||
class Calc : public Fsm::Machine<Calc, std::string>
|
||||
class Calc : public Fsm::Machine<Calc>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = std::string;
|
||||
|
||||
int input = 0;
|
||||
|
||||
struct Start : State {
|
||||
@@ -49,7 +55,7 @@ namespace
|
||||
self().input);
|
||||
}
|
||||
};
|
||||
struct AfterDouble : Continue<int> {
|
||||
struct AfterDouble : Continue<Doubler> {
|
||||
Task<Step> on_success(int v) override
|
||||
{
|
||||
co_return succeed("doubled:" + std::to_string(v));
|
||||
@@ -87,7 +93,7 @@ Void::Testing::Test submachine_failure_dispatches_on_failure()
|
||||
|
||||
namespace
|
||||
{
|
||||
class Pinger : public Fsm::Machine<Pinger, void>
|
||||
class Pinger : public Fsm::Machine<Pinger>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -97,18 +103,23 @@ namespace
|
||||
using States = Fsm::States<Ping>;
|
||||
};
|
||||
|
||||
class Host : public Fsm::Machine<Host, int>
|
||||
class Host : public Fsm::Machine<Host>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Go : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
co_return startMachine<Pinger, Done>();
|
||||
}
|
||||
};
|
||||
struct Done : Continue<void> {
|
||||
Task<Step> on_success() override { co_return succeed(42); }
|
||||
struct Done : Continue<Pinger> {
|
||||
Task<Step> on_success(std::monostate) override
|
||||
{
|
||||
co_return succeed(42);
|
||||
}
|
||||
};
|
||||
using States = Fsm::States<Go, Done>;
|
||||
};
|
||||
@@ -127,23 +138,25 @@ Void::Testing::Test submachine_void_result_calls_nullary_on_success()
|
||||
|
||||
namespace
|
||||
{
|
||||
class Chain : public Fsm::Machine<Chain, int>
|
||||
class Chain : public Fsm::Machine<Chain>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct A : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
co_return startMachine<Doubler, B>(3); // 3 -> 6
|
||||
}
|
||||
};
|
||||
struct B : Continue<int> {
|
||||
struct B : Continue<Doubler> {
|
||||
Task<Step> on_success(int v) override
|
||||
{
|
||||
co_return startMachine<Doubler, C>(v); // 6 -> 12
|
||||
}
|
||||
};
|
||||
struct C : Continue<int> {
|
||||
struct C : Continue<Doubler> {
|
||||
Task<Step> on_success(int v) override
|
||||
{
|
||||
co_return succeed(v);
|
||||
@@ -169,10 +182,12 @@ namespace
|
||||
struct Tick {
|
||||
};
|
||||
|
||||
class WaitSub : public Fsm::Machine<WaitSub, int>
|
||||
class WaitSub : public Fsm::Machine<WaitSub>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct W : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -183,17 +198,19 @@ namespace
|
||||
using States = Fsm::States<W>;
|
||||
};
|
||||
|
||||
class Outer : public Fsm::Machine<Outer, int>
|
||||
class Outer : public Fsm::Machine<Outer>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Begin : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
co_return startMachine<WaitSub, Fin>();
|
||||
}
|
||||
};
|
||||
struct Fin : Continue<int> {
|
||||
struct Fin : Continue<WaitSub> {
|
||||
Task<Step> on_success(int v) override
|
||||
{
|
||||
co_return succeed(v);
|
||||
|
||||
@@ -66,10 +66,12 @@ namespace
|
||||
};
|
||||
|
||||
// An FSM that bumps the counter service N times then succeeds.
|
||||
class Worker : public Void::Fsm::Machine<Worker, int>
|
||||
class Worker : public Void::Fsm::Machine<Worker>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
int times;
|
||||
|
||||
struct Run : State {
|
||||
@@ -85,7 +87,7 @@ namespace
|
||||
};
|
||||
|
||||
explicit Worker(int t)
|
||||
: Void::Fsm::Machine<Worker, int>(
|
||||
: Void::Fsm::Machine<Worker>(
|
||||
MachineOptions{.own_worker = false}),
|
||||
times(t)
|
||||
{
|
||||
@@ -110,7 +112,9 @@ Void::Testing::Test orchestrator_launches_and_supervises()
|
||||
{
|
||||
// Worker-backed FSM so the orchestrator's start() spins a worker and
|
||||
// the FSM runs to completion on its own.
|
||||
struct AsyncWorker : Void::Fsm::Machine<AsyncWorker, int> {
|
||||
struct AsyncWorker : Void::Fsm::Machine<AsyncWorker> {
|
||||
using Success = int;
|
||||
|
||||
struct Run : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user