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)
|
### 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>()`).
|
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/Async/Task.hpp>
|
||||||
#include <Void/Fsm/Detail/Engine.hpp>
|
#include <Void/Fsm/Detail/Engine.hpp>
|
||||||
|
#include <Void/Fsm/Detail/Result.hpp>
|
||||||
#include <Void/Fsm/Step.hpp>
|
#include <Void/Fsm/Step.hpp>
|
||||||
|
|
||||||
#include <any>
|
#include <any>
|
||||||
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <variant>
|
||||||
|
|
||||||
namespace Void::Fsm::Detail
|
namespace Void::Fsm::Detail
|
||||||
{
|
{
|
||||||
namespace Coro = Void::Coro;
|
namespace Coro = Void::Coro;
|
||||||
|
|
||||||
// Base for a state that resumes from a sub-machine launched via
|
// Base for a state that resumes from a sub-machine launched via
|
||||||
// startMachine<Sub, This>(). `StateT` is the owning machine's nested State
|
// startMachine<Sub, This>(). `StateT` is the owning machine's nested State;
|
||||||
// (so the continuation inherits self()/next/fail/...); `SubResult` is the
|
// `Sub` is the launched machine. The engine enters this state carrying the
|
||||||
// sub-machine's result_type. The engine enters this state carrying the
|
|
||||||
// sub's LaunchResult and calls resume(), which dispatches to on_success /
|
// 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 takes the sub's typed Success, on_failure its typed Failure
|
||||||
// on_success(R) (and optionally on_failure).
|
// (a void result becomes a std::monostate argument).
|
||||||
template <typename StateT, typename SubResult>
|
//
|
||||||
class Continuation : public StateT
|
// 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:
|
public:
|
||||||
|
|
||||||
Coro::Task<Step> resume(LaunchResult lr) final
|
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(
|
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
|
Coro::Task<Step> run() override
|
||||||
{
|
{
|
||||||
co_return this->fail(
|
co_return Step::fault(FsmError{
|
||||||
"Continue<R> state entered without a sub-machine result "
|
"continuation state entered without a sub-machine result "
|
||||||
"(reach it via startMachine<Sub, This>())");
|
"(reach it via startMachine<Sub, This>())"});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
/// The sub-machine succeeded with `result`.
|
virtual Coro::Task<Step> on_success(SuccArg result) = 0;
|
||||||
virtual Coro::Task<Step> on_success(SubResult result) = 0;
|
|
||||||
|
|
||||||
/// The sub-machine failed. Default: fail the parent with the same
|
/// Default: a sub-machine failure the parent didn't handle becomes
|
||||||
/// message; override to recover.
|
/// a system fault. Override to recover.
|
||||||
virtual Coro::Task<Step> on_failure(FsmError error)
|
virtual Coro::Task<Step> on_failure(FailArg /*error*/)
|
||||||
{
|
{
|
||||||
co_return this->fail(std::move(error.message));
|
co_return Step::fault(
|
||||||
}
|
FsmError{"unhandled sub-machine failure"});
|
||||||
};
|
|
||||||
|
|
||||||
// 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));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace Void::Fsm::Detail
|
} // namespace Void::Fsm::Detail
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Void/Async/Task.hpp>
|
#include <Void/Async/Task.hpp>
|
||||||
|
#include <Void/Fsm/Detail/Result.hpp>
|
||||||
#include <Void/Fsm/Detail/TypeName.hpp>
|
#include <Void/Fsm/Detail/TypeName.hpp>
|
||||||
#include <Void/Fsm/Step.hpp>
|
#include <Void/Fsm/Step.hpp>
|
||||||
|
|
||||||
@@ -15,9 +16,10 @@
|
|||||||
|
|
||||||
// The declarative transition DSL for reactive states, lifted out of the
|
// The declarative transition DSL for reactive states, lifted out of the
|
||||||
// Machine template so the core stays small. Every type is parameterised by the
|
// Machine template so the core stays small. Every type is parameterised by the
|
||||||
// owning machine's `Derived` (guards/actions take `Derived&`) and `ResultT`
|
// owning machine's `Derived` (guards/actions take `Derived&`); the machine's
|
||||||
// (finish() carries the machine's result). Machine::Reactive aliases these and
|
// typed Success/Failure (nested types, default void) are reflected from
|
||||||
// the engine drives them; see include/Void/Fsm/Machine.hpp.
|
// `Derived` for finish()/fail(). Machine::Reactive aliases these and the engine
|
||||||
|
// drives them; see include/Void/Fsm/Machine.hpp.
|
||||||
|
|
||||||
namespace Void::Fsm::Detail
|
namespace Void::Fsm::Detail
|
||||||
{
|
{
|
||||||
@@ -44,8 +46,8 @@ namespace Void::Fsm::Detail
|
|||||||
};
|
};
|
||||||
|
|
||||||
/// Fluent builder for one event row: `on.event<E>().guard(..).action(..)
|
/// Fluent builder for one event row: `on.event<E>().guard(..).action(..)
|
||||||
/// .to<S>()` (or `.finish(v)` / `.fail(msg)`).
|
/// .to<S>()` (or `.finish(v)` / `.fail(v)`).
|
||||||
template <typename Derived, typename ResultT, typename E> class EventClause
|
template <typename Derived, typename E> class EventClause
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -88,21 +90,31 @@ namespace Void::Fsm::Detail
|
|||||||
}
|
}
|
||||||
|
|
||||||
void finish()
|
void finish()
|
||||||
requires std::is_void_v<ResultT>
|
requires std::is_void_v<SuccessOf<Derived>>
|
||||||
{
|
{
|
||||||
commit(Step::success(std::any{}), "(success)");
|
commit(Step::success(std::any{}), "(success)");
|
||||||
}
|
}
|
||||||
template <typename V>
|
template <typename V>
|
||||||
void finish(V&& value)
|
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)");
|
"(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:
|
private:
|
||||||
@@ -136,8 +148,8 @@ namespace Void::Fsm::Detail
|
|||||||
std::function<Coro::Task<void>(Derived&, const E&)> m_action;
|
std::function<Coro::Task<void>(Derived&, const E&)> m_action;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Fluent builder for the timeout row: `on.after(d).to<S>()` / `.fail(..)`.
|
/// Fluent builder for the timeout row: `on.after(d).to<S>()` / `.fail(v)`.
|
||||||
template <typename Derived, typename ResultT> class AfterClause
|
template <typename Derived> class AfterClause
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -159,12 +171,22 @@ namespace Void::Fsm::Detail
|
|||||||
m_table->timeout_target_name = type_name<S>();
|
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->has_timeout = true;
|
||||||
m_table->timeout_after = m_after;
|
m_table->timeout_after = m_after;
|
||||||
m_table->timeout_step =
|
m_table->timeout_step = Step::failure(std::any{});
|
||||||
Step::failure(FsmError{std::move(message)});
|
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)";
|
m_table->timeout_target_name = "(fail)";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +197,7 @@ namespace Void::Fsm::Detail
|
|||||||
};
|
};
|
||||||
|
|
||||||
/// Entry point handed to `transitions(TransitionBuilder& on)`.
|
/// Entry point handed to `transitions(TransitionBuilder& on)`.
|
||||||
template <typename Derived, typename ResultT> class TransitionBuilder
|
template <typename Derived> class TransitionBuilder
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -184,17 +206,16 @@ namespace Void::Fsm::Detail
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename E>
|
template <typename E> [[nodiscard]] EventClause<Derived, E> event()
|
||||||
[[nodiscard]] EventClause<Derived, ResultT, E> event()
|
|
||||||
{
|
{
|
||||||
return EventClause<Derived, ResultT, E>{*m_table};
|
return EventClause<Derived, E>{*m_table};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Rep, typename Period>
|
template <typename Rep, typename Period>
|
||||||
[[nodiscard]] AfterClause<Derived, ResultT>
|
[[nodiscard]] AfterClause<Derived>
|
||||||
after(std::chrono::duration<Rep, Period> d)
|
after(std::chrono::duration<Rep, Period> d)
|
||||||
{
|
{
|
||||||
return AfterClause<Derived, ResultT>{
|
return AfterClause<Derived>{
|
||||||
*m_table, std::chrono::duration_cast<
|
*m_table, std::chrono::duration_cast<
|
||||||
std::chrono::steady_clock::duration>(d)};
|
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/Engine.hpp>
|
||||||
#include <Void/Fsm/Detail/Introspect.hpp>
|
#include <Void/Fsm/Detail/Introspect.hpp>
|
||||||
#include <Void/Fsm/Detail/Reactive.hpp>
|
#include <Void/Fsm/Detail/Reactive.hpp>
|
||||||
|
#include <Void/Fsm/Detail/Result.hpp>
|
||||||
#include <Void/Fsm/Detail/TypeName.hpp>
|
#include <Void/Fsm/Detail/TypeName.hpp>
|
||||||
#include <Void/Fsm/Step.hpp>
|
#include <Void/Fsm/Step.hpp>
|
||||||
#include <Void/Log/Log.hpp>
|
#include <Void/Log/Log.hpp>
|
||||||
@@ -89,13 +90,25 @@ namespace Void::Fsm
|
|||||||
// };
|
// };
|
||||||
// using States = Void::Fsm::States<Check>;
|
// using States = Void::Fsm::States<Check>;
|
||||||
// };
|
// };
|
||||||
template <typename Derived, typename ResultT = void> class Machine
|
template <typename Derived> class Machine
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using result_type = ResultT;
|
|
||||||
using Options = MachineOptions;
|
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 ────────────────────────────────────────────────
|
// ── State base ────────────────────────────────────────────────
|
||||||
class State : public Detail::StateBase
|
class State : public Detail::StateBase
|
||||||
@@ -188,22 +201,36 @@ namespace Void::Fsm
|
|||||||
co_return false;
|
co_return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finish with the machine's typed Success result (nested
|
||||||
|
/// `Success`; nullary when it is void).
|
||||||
[[nodiscard]] Step succeed() const
|
[[nodiscard]] Step succeed() const
|
||||||
requires std::is_void_v<ResultT>
|
requires std::is_void_v<Detail::SuccessOf<Derived>>
|
||||||
{
|
{
|
||||||
return Step::success(std::any{});
|
return Step::success(std::any{});
|
||||||
}
|
}
|
||||||
template <typename V>
|
template <typename V>
|
||||||
[[nodiscard]] Step succeed(V&& value) const
|
[[nodiscard]] Step succeed(V&& value) const
|
||||||
requires(!std::is_void_v<ResultT>)
|
requires(!std::is_void_v<Detail::SuccessOf<Derived>>)
|
||||||
{
|
{
|
||||||
return Step::success(
|
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
|
template <typename I> [[nodiscard]] I& service() const
|
||||||
@@ -265,10 +292,9 @@ namespace Void::Fsm
|
|||||||
[[nodiscard]] Step startMachine(Args&&... args) const
|
[[nodiscard]] Step startMachine(Args&&... args) const
|
||||||
{
|
{
|
||||||
static_assert(
|
static_assert(
|
||||||
std::is_base_of_v<
|
std::is_base_of_v<Continue<Sub>, Next>,
|
||||||
Continue<typename Sub::result_type>, Next>,
|
|
||||||
"startMachine<Sub, Next>: Next must derive "
|
"startMachine<Sub, Next>: Next must derive "
|
||||||
"Continue<Sub::result_type>");
|
"Continue<Sub>");
|
||||||
return m_machine->template start_machine<Sub, Next>(
|
return m_machine->template start_machine<Sub, Next>(
|
||||||
std::forward<Args>(args)...);
|
std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
@@ -298,8 +324,8 @@ namespace Void::Fsm
|
|||||||
// Coro::Task<Step> on_failure(FsmError e) override {
|
// Coro::Task<Step> on_failure(FsmError e) override {
|
||||||
// co_return fail(e.message); }
|
// co_return fail(e.message); }
|
||||||
// };
|
// };
|
||||||
template <typename SubResult>
|
template <typename Sub>
|
||||||
using Continue = Detail::Continuation<State, SubResult>;
|
using Continue = Detail::Continuation<State, Sub>;
|
||||||
|
|
||||||
// ── reactive (declarative) states ─────────────────────────────
|
// ── reactive (declarative) states ─────────────────────────────
|
||||||
//
|
//
|
||||||
@@ -321,8 +347,7 @@ namespace Void::Fsm
|
|||||||
// The declarative transition DSL lives in Detail/Reactive.hpp;
|
// The declarative transition DSL lives in Detail/Reactive.hpp;
|
||||||
// these aliases give reactive states the ergonomic nested names.
|
// these aliases give reactive states the ergonomic nested names.
|
||||||
using TransitionTable = Detail::TransitionTable<Derived>;
|
using TransitionTable = Detail::TransitionTable<Derived>;
|
||||||
using TransitionBuilder =
|
using TransitionBuilder = Detail::TransitionBuilder<Derived>;
|
||||||
Detail::TransitionBuilder<Derived, ResultT>;
|
|
||||||
|
|
||||||
class Reactive : public State
|
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_ctx) {
|
||||||
if (m_options.own_worker && m_owns_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();
|
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; }
|
std::size_t pump() { return m_ctx ? m_ctx->pump() : 0; }
|
||||||
@@ -576,7 +610,8 @@ namespace Void::Fsm
|
|||||||
[[nodiscard]] bool finished() const noexcept
|
[[nodiscard]] bool finished() const noexcept
|
||||||
{
|
{
|
||||||
const auto s = m_status.load();
|
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
|
[[nodiscard]] Status status() const noexcept
|
||||||
@@ -605,7 +640,8 @@ namespace Void::Fsm
|
|||||||
m_ctx.reset();
|
m_ctx.reset();
|
||||||
m_owns_ctx = false;
|
m_owns_ctx = false;
|
||||||
m_current.store(-1, std::memory_order_release);
|
m_current.store(-1, std::memory_order_release);
|
||||||
m_outcome.reset();
|
m_result_value.reset();
|
||||||
|
m_system_error.reset();
|
||||||
m_pending_result.reset();
|
m_pending_result.reset();
|
||||||
m_jump.store(nullptr, std::memory_order_release);
|
m_jump.store(nullptr, std::memory_order_release);
|
||||||
m_step = 0;
|
m_step = 0;
|
||||||
@@ -764,12 +800,13 @@ namespace Void::Fsm
|
|||||||
|
|
||||||
// ── nesting ───────────────────────────────────────────────────
|
// ── nesting ───────────────────────────────────────────────────
|
||||||
template <typename Sub, typename... Args>
|
template <typename Sub, typename... Args>
|
||||||
[[nodiscard]] Coro::Task<typename Sub::Outcome>
|
[[nodiscard]] Coro::Task<Detail::OutcomeOf<Sub>>
|
||||||
run_child(Args&&... args)
|
run_child(Args&&... args)
|
||||||
{
|
{
|
||||||
Sub child{std::forward<Args>(args)...};
|
Sub child{std::forward<Args>(args)...};
|
||||||
child.attach(m_ctx, m_services);
|
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
|
/// 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
|
/// caller maps to a sibling transition. The calling state's
|
||||||
/// on_enter/on_exit naturally wrap the region.
|
/// on_enter/on_exit naturally wrap the region.
|
||||||
template <typename Sub, typename... Args>
|
template <typename Sub, typename... Args>
|
||||||
[[nodiscard]] Coro::Task<typename Sub::Outcome>
|
[[nodiscard]] Coro::Task<Detail::OutcomeOf<Sub>>
|
||||||
run_region(Args&&... args)
|
run_region(Args&&... args)
|
||||||
{
|
{
|
||||||
Sub child{std::forward<Args>(args)...};
|
Sub child{std::forward<Args>(args)...};
|
||||||
child.attach(m_ctx, m_services);
|
child.attach(m_ctx, m_services);
|
||||||
m_region_probe = [&child] { return child.state_path(); };
|
m_region_probe = [&child] { return child.state_path(); };
|
||||||
auto outcome = co_await child.drive();
|
co_await child.drive();
|
||||||
m_region_probe = nullptr;
|
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
|
/// Build a Launch step: run `Sub` (constructed from `args`) on this
|
||||||
@@ -805,22 +842,26 @@ namespace Void::Fsm
|
|||||||
[this,
|
[this,
|
||||||
tup = std::make_tuple(std::forward<Args>(
|
tup = std::make_tuple(std::forward<Args>(
|
||||||
args)...)]() mutable -> Coro::Task<LaunchResult> {
|
args)...)]() mutable -> Coro::Task<LaunchResult> {
|
||||||
auto outcome = co_await std::apply(
|
// Run Sub on the shared executor and read its raw result so
|
||||||
[this](auto&&... a) {
|
// Success / Failure / Fault are all distinguishable.
|
||||||
return this->template run_child<Sub>(
|
Sub child = std::make_from_tuple<Sub>(std::move(tup));
|
||||||
std::forward<decltype(a)>(a)...);
|
child.attach(m_ctx, m_services);
|
||||||
},
|
co_await child.drive();
|
||||||
std::move(tup));
|
|
||||||
LaunchResult lr;
|
LaunchResult lr;
|
||||||
if (outcome.has_value()) {
|
switch (child.m_status.load(std::memory_order_acquire)) {
|
||||||
lr.ok = true;
|
case Status::Succeeded:
|
||||||
if constexpr (!std::is_void_v<
|
lr.kind = LaunchResult::Kind::Success;
|
||||||
typename Sub::result_type>) {
|
lr.value = std::move(child.m_result_value);
|
||||||
lr.value = std::any{std::move(*outcome)};
|
break;
|
||||||
}
|
case Status::Failed:
|
||||||
} else {
|
lr.kind = LaunchResult::Kind::Failure;
|
||||||
lr.ok = false;
|
lr.failure = std::move(child.m_result_value);
|
||||||
lr.error = std::move(outcome.error());
|
break;
|
||||||
|
default:
|
||||||
|
lr.kind = LaunchResult::Kind::Fault;
|
||||||
|
lr.fault = child.m_system_error.value_or(
|
||||||
|
FsmError{"sub-machine fault"});
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
co_return lr;
|
co_return lr;
|
||||||
};
|
};
|
||||||
@@ -962,7 +1003,7 @@ namespace Void::Fsm
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
template <typename, typename>
|
template <typename>
|
||||||
friend class Machine; // run_child reaches child internals
|
friend class Machine; // run_child reaches child internals
|
||||||
|
|
||||||
void attach(std::shared_ptr<Detail::ExecutionContext> ctx,
|
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 →
|
// Record a system fault (programming error) — separate from a
|
||||||
// on_exit, follow the returned Step. Runs on the executor.
|
// typed Failure result. Sets status Faulted + system_error.
|
||||||
Coro::Task<Outcome> drive()
|
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;
|
const std::type_info* cur = m_initial;
|
||||||
if (cur == nullptr) {
|
if (cur == nullptr) {
|
||||||
co_return std::unexpected{FsmError{"no initial state"}};
|
fault("no initial state");
|
||||||
|
co_return;
|
||||||
}
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
// Apply an external jump<S>() at this transition boundary.
|
// 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});
|
const auto it = m_factories.find(std::type_index{*cur});
|
||||||
if (it == m_factories.end()) {
|
if (it == m_factories.end()) {
|
||||||
co_return std::unexpected{
|
fault("transition to unregistered state");
|
||||||
FsmError{"transition to unregistered state"}};
|
co_return;
|
||||||
}
|
}
|
||||||
set_current(cur);
|
set_current(cur);
|
||||||
auto state = it->second();
|
auto state = it->second();
|
||||||
co_await state->on_enter();
|
co_await state->on_enter();
|
||||||
// A state entered via startMachine resumes from the sub's
|
// A state entered via startMachine resumes from the sub's
|
||||||
// result (Continue<R> dispatches); otherwise it runs.
|
// result; otherwise it runs.
|
||||||
Step step;
|
Step step;
|
||||||
if (m_pending_result.has_value()) {
|
if (m_pending_result.has_value()) {
|
||||||
LaunchResult pending = std::move(*m_pending_result);
|
LaunchResult pending = std::move(*m_pending_result);
|
||||||
m_pending_result.reset();
|
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));
|
step = co_await state->resume(std::move(pending));
|
||||||
} else {
|
} else {
|
||||||
step = co_await state->run();
|
step = co_await state->run();
|
||||||
@@ -1081,43 +1139,70 @@ namespace Void::Fsm
|
|||||||
cur = step.target();
|
cur = step.target();
|
||||||
continue;
|
continue;
|
||||||
case Step::Kind::Launch:
|
case Step::Kind::Launch:
|
||||||
// Run the sub-machine, stash its result, then enter
|
|
||||||
// the Next (Continue<R>) state.
|
|
||||||
m_pending_result.emplace(
|
m_pending_result.emplace(
|
||||||
co_await step.run_launch());
|
co_await step.run_launch());
|
||||||
cur = step.target();
|
cur = step.target();
|
||||||
continue;
|
continue;
|
||||||
case Step::Kind::Success:
|
case Step::Kind::Success:
|
||||||
if constexpr (std::is_void_v<ResultT>) {
|
m_result_value = std::move(step.payload());
|
||||||
co_return Outcome{};
|
m_status.store(Status::Succeeded,
|
||||||
} else {
|
std::memory_order_release);
|
||||||
co_return Outcome{std::any_cast<ResultT>(
|
co_return;
|
||||||
std::move(step.result()))};
|
|
||||||
}
|
|
||||||
case Step::Kind::Fail:
|
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()
|
Coro::Task<void> driver()
|
||||||
{
|
{
|
||||||
Outcome outcome = co_await drive();
|
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);
|
|
||||||
if (m_owns_ctx) {
|
if (m_owns_ctx) {
|
||||||
m_ctx->finish();
|
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()) {
|
using Succ = Detail::SuccessOf<D>;
|
||||||
return std::move(*m_outcome);
|
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;
|
Options m_options;
|
||||||
@@ -1137,7 +1222,10 @@ namespace Void::Fsm
|
|||||||
|
|
||||||
std::atomic<int> m_current{-1};
|
std::atomic<int> m_current{-1};
|
||||||
std::atomic<Status> m_status{Status::Idle};
|
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::function<void(const Transition&)> m_observer;
|
||||||
std::vector<Interceptor> m_interceptors;
|
std::vector<Interceptor> m_interceptors;
|
||||||
std::uint64_t m_step{0};
|
std::uint64_t m_step{0};
|
||||||
|
|||||||
+42
-31
@@ -12,35 +12,40 @@ namespace Void::Fsm
|
|||||||
{
|
{
|
||||||
namespace Coro = Void::Coro;
|
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 {
|
struct FsmError {
|
||||||
std::string message;
|
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 {
|
enum class Status {
|
||||||
Idle, // constructed, not started
|
Idle, // constructed, not started
|
||||||
Running, // worker active
|
Running, // worker active
|
||||||
Succeeded, // finished with a result
|
Succeeded, // finished with a typed Success
|
||||||
Failed // finished with an error
|
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 outcome of a sub-machine launched via startMachine<Sub, Next>():
|
||||||
/// the machine with a result, or fail. Built only through the State
|
/// a typed Success value, a typed Failure value, or a system fault.
|
||||||
/// helpers (next<S>() / succeed() / fail()).
|
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
|
class Step
|
||||||
{
|
{
|
||||||
public:
|
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)
|
[[nodiscard]] static Step transition(const std::type_info& target)
|
||||||
{
|
{
|
||||||
@@ -50,9 +55,18 @@ namespace Void::Fsm
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Launch a sub-machine, then enter `target` (a Continue<R> state)
|
/// Signal a system fault (programming error) — distinct from a
|
||||||
/// with the sub's LaunchResult. `launcher` runs the sub and yields
|
/// typed Failure. Routed to Machine::system_error().
|
||||||
/// its result; the engine awaits it in drive().
|
[[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
|
[[nodiscard]] static Step
|
||||||
launch(const std::type_info& target,
|
launch(const std::type_info& target,
|
||||||
std::function<Coro::Task<LaunchResult>()> launcher)
|
std::function<Coro::Task<LaunchResult>()> launcher)
|
||||||
@@ -64,19 +78,21 @@ namespace Void::Fsm
|
|||||||
return s;
|
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;
|
Step s;
|
||||||
s.m_kind = Kind::Success;
|
s.m_kind = Kind::Success;
|
||||||
s.m_result = std::move(result);
|
s.m_payload = std::move(value);
|
||||||
return s;
|
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;
|
Step s;
|
||||||
s.m_kind = Kind::Fail;
|
s.m_kind = Kind::Fail;
|
||||||
s.m_error = std::move(error);
|
s.m_payload = std::move(value);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,12 +103,8 @@ namespace Void::Fsm
|
|||||||
return m_target;
|
return m_target;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] std::any& result() noexcept { return m_result; }
|
/// The Success or Failure payload (by kind).
|
||||||
|
[[nodiscard]] std::any& payload() noexcept { return m_payload; }
|
||||||
[[nodiscard]] const FsmError& error() const noexcept
|
|
||||||
{
|
|
||||||
return m_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Run the sub-machine for a Launch step (yields its LaunchResult).
|
/// Run the sub-machine for a Launch step (yields its LaunchResult).
|
||||||
[[nodiscard]] Coro::Task<LaunchResult> run_launch()
|
[[nodiscard]] Coro::Task<LaunchResult> run_launch()
|
||||||
@@ -104,8 +116,7 @@ namespace Void::Fsm
|
|||||||
|
|
||||||
Kind m_kind{Kind::Fail};
|
Kind m_kind{Kind::Fail};
|
||||||
const std::type_info* m_target{nullptr};
|
const std::type_info* m_target{nullptr};
|
||||||
std::any m_result;
|
std::any m_payload;
|
||||||
FsmError m_error;
|
|
||||||
std::function<Coro::Task<LaunchResult>()> m_launcher;
|
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<void> StateBase::on_exit() { co_return; }
|
||||||
Coro::Task<Step> StateBase::run()
|
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*/)
|
Coro::Task<Step> StateBase::resume(LaunchResult /*result*/)
|
||||||
|
|||||||
@@ -37,10 +37,12 @@ namespace
|
|||||||
// An FSM that drives the plugin-provided ICalc service: each Work step
|
// An FSM that drives the plugin-provided ICalc service: each Work step
|
||||||
// accumulates and loops until a target total is reached. It resolves
|
// accumulates and loops until a target total is reached. It resolves
|
||||||
// the service every step, so it picks up a reloaded implementation.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct Work : State {
|
struct Work : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -12,10 +12,12 @@ using Void::Fsm::Step;
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// A linear workflow: Start -> Work (loops 3x) -> success(count).
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
std::vector<std::string>* log = nullptr;
|
std::vector<std::string>* log = nullptr;
|
||||||
|
|
||||||
@@ -47,16 +49,19 @@ namespace
|
|||||||
using States = Void::Fsm::States<Start, Work>;
|
using States = Void::Fsm::States<Start, Work>;
|
||||||
|
|
||||||
Counter()
|
Counter()
|
||||||
: Void::Fsm::Machine<Counter, int>(
|
: Void::Fsm::Machine<Counter>(
|
||||||
MachineOptions{.own_worker = false})
|
MachineOptions{.own_worker = false})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Failing : public Void::Fsm::Machine<Failing, int>
|
class Failing : public Void::Fsm::Machine<Failing>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
using Failure = Void::Fsm::FsmError;
|
||||||
|
|
||||||
struct Boom : State {
|
struct Boom : State {
|
||||||
Task<Step> run() override { co_return fail("kaboom"); }
|
Task<Step> run() override { co_return fail("kaboom"); }
|
||||||
};
|
};
|
||||||
@@ -64,7 +69,7 @@ namespace
|
|||||||
using States = Void::Fsm::States<Boom>;
|
using States = Void::Fsm::States<Boom>;
|
||||||
|
|
||||||
Failing()
|
Failing()
|
||||||
: Void::Fsm::Machine<Failing, int>(
|
: Void::Fsm::Machine<Failing>(
|
||||||
MachineOptions{.own_worker = false})
|
MachineOptions{.own_worker = false})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -107,13 +112,13 @@ Void::Testing::Test fsm_pump_drives_to_completion()
|
|||||||
|
|
||||||
Void::Testing::Test fsm_void_result_succeeds()
|
Void::Testing::Test fsm_void_result_succeeds()
|
||||||
{
|
{
|
||||||
struct Trivial : Void::Fsm::Machine<Trivial, void> {
|
struct Trivial : Void::Fsm::Machine<Trivial> {
|
||||||
struct Done : State {
|
struct Done : State {
|
||||||
Task<Step> run() override { co_return succeed(); }
|
Task<Step> run() override { co_return succeed(); }
|
||||||
};
|
};
|
||||||
using States = Void::Fsm::States<Done>;
|
using States = Void::Fsm::States<Done>;
|
||||||
Trivial()
|
Trivial()
|
||||||
: Void::Fsm::Machine<Trivial, void>(
|
: Void::Fsm::Machine<Trivial>(
|
||||||
MachineOptions{.own_worker = false})
|
MachineOptions{.own_worker = false})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -127,7 +132,9 @@ Void::Testing::Test fsm_dynamic_state_registration()
|
|||||||
{
|
{
|
||||||
// Dynamic machine: states registered conditionally, addressed with
|
// Dynamic machine: states registered conditionally, addressed with
|
||||||
// next_dynamic<S>() (skips the compile-time States<> check).
|
// 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;
|
bool express;
|
||||||
struct Express : State {
|
struct Express : State {
|
||||||
Task<Step> run() override { co_return succeed(99); }
|
Task<Step> run() override { co_return succeed(99); }
|
||||||
@@ -145,8 +152,7 @@ Void::Testing::Test fsm_dynamic_state_registration()
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
explicit Flow(bool e)
|
explicit Flow(bool e)
|
||||||
: Void::Fsm::Machine<Flow, int>(
|
: Void::Fsm::Machine<Flow>(MachineOptions{.own_worker = false}),
|
||||||
MachineOptions{.own_worker = false}),
|
|
||||||
express(e)
|
express(e)
|
||||||
{
|
{
|
||||||
initial<Start>();
|
initial<Start>();
|
||||||
|
|||||||
@@ -22,10 +22,12 @@ namespace
|
|||||||
// Sums many Tick events delivered from several threads, finishing when
|
// Sums many Tick events delivered from several threads, finishing when
|
||||||
// the running total reaches a target. Single consumer (the worker) so
|
// the running total reaches a target. Single consumer (the worker) so
|
||||||
// the total is race-free without the FSM holding any lock.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = long long;
|
||||||
|
|
||||||
long long total = 0;
|
long long total = 0;
|
||||||
long long target;
|
long long target;
|
||||||
|
|
||||||
@@ -42,7 +44,7 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
explicit Summer(long long tgt)
|
explicit Summer(long long tgt)
|
||||||
: Void::Fsm::Machine<Summer, long long>(), target(tgt)
|
: Void::Fsm::Machine<Summer>(), target(tgt)
|
||||||
{
|
{
|
||||||
initial<Run>();
|
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()
|
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 {
|
struct Go : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
@@ -140,8 +144,7 @@ Void::Testing::Test fsm_resolves_service_through_context()
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
Calc()
|
Calc()
|
||||||
: Void::Fsm::Machine<Calc, int>(
|
: Void::Fsm::Machine<Calc>(MachineOptions{.own_worker = false})
|
||||||
MachineOptions{.own_worker = false})
|
|
||||||
{
|
{
|
||||||
initial<Go>();
|
initial<Go>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,10 +20,12 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Sums two Add events, posted in one fluent send() call.
|
// Sums two Add events, posted in one fluent send() call.
|
||||||
class Sum : public Fsm::Machine<Sum, int>
|
class Sum : public Fsm::Machine<Sum>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
int total = 0;
|
int total = 0;
|
||||||
|
|
||||||
struct Take : State {
|
struct Take : State {
|
||||||
@@ -55,10 +57,12 @@ namespace
|
|||||||
{
|
{
|
||||||
// A and C are not connected by any declared edge; goto_state<C>() jumps
|
// A and C are not connected by any declared edge; goto_state<C>() jumps
|
||||||
// from A to C freely.
|
// from A to C freely.
|
||||||
class Hop : public Fsm::Machine<Hop, int>
|
class Hop : public Fsm::Machine<Hop>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct A : State {
|
struct A : State {
|
||||||
Task<Step> run() override { co_return goto_state<C>(); }
|
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
|
// Loops in Loop on each Tick; an external jump<Done>() redirects at the
|
||||||
// next boundary.
|
// next boundary.
|
||||||
class Jumper : public Fsm::Machine<Jumper, int>
|
class Jumper : public Fsm::Machine<Jumper>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct Loop : State {
|
struct Loop : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
@@ -104,10 +110,7 @@ namespace
|
|||||||
};
|
};
|
||||||
using States = Fsm::States<Loop, Done>;
|
using States = Fsm::States<Loop, Done>;
|
||||||
|
|
||||||
explicit Jumper(MachineOptions o = {})
|
explicit Jumper(MachineOptions o = {}) : Fsm::Machine<Jumper>(o) {}
|
||||||
: Fsm::Machine<Jumper, int>(o)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
@@ -136,10 +139,12 @@ Void::Testing::Test jump_redirects_at_next_boundary()
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// Increments a machine-owned counter each run (data survives reset()).
|
// Increments a machine-owned counter each run (data survives reset()).
|
||||||
class Counter : public Fsm::Machine<Counter, int>
|
class Counter : public Fsm::Machine<Counter>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
int runs = 0;
|
int runs = 0;
|
||||||
|
|
||||||
struct Once : State {
|
struct Once : State {
|
||||||
@@ -165,10 +170,12 @@ Void::Testing::Test restart_reruns_the_machine()
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
class Pick : public Fsm::Machine<Pick, int>
|
class Pick : public Fsm::Machine<Pick>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct A : State {
|
struct A : State {
|
||||||
Task<Step> run() override { co_return succeed(1); }
|
Task<Step> run() override { co_return succeed(1); }
|
||||||
};
|
};
|
||||||
@@ -194,10 +201,12 @@ Void::Testing::Test restart_at_changes_initial_state()
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
class Mut : public Fsm::Machine<Mut, int>
|
class Mut : public Fsm::Machine<Mut>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct A : State {
|
struct A : State {
|
||||||
Task<Step> run() override { co_return succeed(1); }
|
Task<Step> run() override { co_return succeed(1); }
|
||||||
};
|
};
|
||||||
@@ -245,10 +254,12 @@ namespace
|
|||||||
struct Go {
|
struct Go {
|
||||||
};
|
};
|
||||||
|
|
||||||
class Sleeper : public Fsm::Machine<Sleeper, int>
|
class Sleeper : public Fsm::Machine<Sleeper>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct W : State {
|
struct W : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -20,10 +20,12 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Waits for Input events, accumulating until a Stop event arrives.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
int total = 0;
|
int total = 0;
|
||||||
std::vector<int>* order = nullptr;
|
std::vector<int>* order = nullptr;
|
||||||
|
|
||||||
@@ -45,7 +47,7 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
explicit Accumulator(bool own_worker)
|
explicit Accumulator(bool own_worker)
|
||||||
: Void::Fsm::Machine<Accumulator, int>(
|
: Void::Fsm::Machine<Accumulator>(
|
||||||
MachineOptions{.own_worker = own_worker})
|
MachineOptions{.own_worker = own_worker})
|
||||||
{
|
{
|
||||||
initial<Waiting>();
|
initial<Waiting>();
|
||||||
|
|||||||
@@ -24,10 +24,13 @@ namespace
|
|||||||
|
|
||||||
// Child region: waits for either Tick (succeed) or Cancel (fail),
|
// Child region: waits for either Tick (succeed) or Cancel (fail),
|
||||||
// handling both at one level via await_any.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = std::string;
|
||||||
|
using Failure = Void::Fsm::FsmError;
|
||||||
|
|
||||||
struct Waiting : State {
|
struct Waiting : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
@@ -41,17 +44,19 @@ namespace
|
|||||||
using States = Void::Fsm::States<Waiting>;
|
using States = Void::Fsm::States<Waiting>;
|
||||||
|
|
||||||
explicit Work(MachineOptions opts = {})
|
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
|
// Parent: a composite Running state enters the Work region and maps its
|
||||||
// result — success passes the value through, failure routes to a sibling.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = std::string;
|
||||||
|
|
||||||
struct Running : State {
|
struct Running : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
@@ -71,7 +76,7 @@ namespace
|
|||||||
using States = Void::Fsm::States<Running, Aborted>;
|
using States = Void::Fsm::States<Running, Aborted>;
|
||||||
|
|
||||||
explicit Job(MachineOptions opts = {})
|
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
|
namespace
|
||||||
{
|
{
|
||||||
// Records enter/run/exit for each state into a shared log.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
std::vector<std::string> log;
|
std::vector<std::string> log;
|
||||||
|
|
||||||
struct A : State {
|
struct A : State {
|
||||||
@@ -58,7 +60,7 @@ namespace
|
|||||||
using States = Void::Fsm::States<A, B>;
|
using States = Void::Fsm::States<A, B>;
|
||||||
|
|
||||||
Lifecycle()
|
Lifecycle()
|
||||||
: Void::Fsm::Machine<Lifecycle, int>(
|
: Void::Fsm::Machine<Lifecycle>(
|
||||||
MachineOptions{.own_worker = false})
|
MachineOptions{.own_worker = false})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@@ -106,15 +108,14 @@ Void::Testing::Test fsm_state_names_and_path()
|
|||||||
// run() which fails — confirms the default body exists.
|
// run() which fails — confirms the default body exists.
|
||||||
Void::Testing::Test fsm_state_without_run_fails()
|
Void::Testing::Test fsm_state_without_run_fails()
|
||||||
{
|
{
|
||||||
struct NoRun : Void::Fsm::Machine<NoRun, void> {
|
struct NoRun : Void::Fsm::Machine<NoRun> {
|
||||||
struct Stuck : State {
|
struct Stuck : State {
|
||||||
Task<void> on_enter() override { co_return; }
|
Task<void> on_enter() override { co_return; }
|
||||||
// no run() override
|
// no run() override
|
||||||
};
|
};
|
||||||
using States = Void::Fsm::States<Stuck>;
|
using States = Void::Fsm::States<Stuck>;
|
||||||
NoRun()
|
NoRun()
|
||||||
: Void::Fsm::Machine<NoRun, void>(
|
: Void::Fsm::Machine<NoRun>(MachineOptions{.own_worker = false})
|
||||||
MachineOptions{.own_worker = false})
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
} fsm;
|
} fsm;
|
||||||
|
|||||||
@@ -11,10 +11,12 @@ using Void::Fsm::Step;
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// Child machine: computes a value and succeeds.
|
// Child machine: computes a value and succeeds.
|
||||||
class Subtotal : public Void::Fsm::Machine<Subtotal, int>
|
class Subtotal : public Void::Fsm::Machine<Subtotal>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
int seed;
|
int seed;
|
||||||
|
|
||||||
struct Compute : State {
|
struct Compute : State {
|
||||||
@@ -26,7 +28,7 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
explicit Subtotal(int s)
|
explicit Subtotal(int s)
|
||||||
: Void::Fsm::Machine<Subtotal, int>(
|
: Void::Fsm::Machine<Subtotal>(
|
||||||
MachineOptions{.own_worker = false}),
|
MachineOptions{.own_worker = false}),
|
||||||
seed(s)
|
seed(s)
|
||||||
{
|
{
|
||||||
@@ -35,15 +37,18 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Child machine that fails.
|
// Child machine that fails.
|
||||||
class FailingChild : public Void::Fsm::Machine<FailingChild, int>
|
class FailingChild : public Void::Fsm::Machine<FailingChild>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
using Failure = Void::Fsm::FsmError;
|
||||||
|
|
||||||
struct Boom : State {
|
struct Boom : State {
|
||||||
Task<Step> run() override { co_return fail("child boom"); }
|
Task<Step> run() override { co_return fail("child boom"); }
|
||||||
};
|
};
|
||||||
FailingChild()
|
FailingChild()
|
||||||
: Void::Fsm::Machine<FailingChild, int>(
|
: Void::Fsm::Machine<FailingChild>(
|
||||||
MachineOptions{.own_worker = false})
|
MachineOptions{.own_worker = false})
|
||||||
{
|
{
|
||||||
initial<Boom>();
|
initial<Boom>();
|
||||||
@@ -51,10 +56,13 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Parent: enters a child, uses its result to decide the outcome.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
using Failure = Void::Fsm::FsmError;
|
||||||
|
|
||||||
struct Start : State {
|
struct Start : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
@@ -68,17 +76,20 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
Parent()
|
Parent()
|
||||||
: Void::Fsm::Machine<Parent, int>(
|
: Void::Fsm::Machine<Parent>(
|
||||||
MachineOptions{.own_worker = false})
|
MachineOptions{.own_worker = false})
|
||||||
{
|
{
|
||||||
initial<Start>();
|
initial<Start>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class ParentOfFailure : public Void::Fsm::Machine<ParentOfFailure, int>
|
class ParentOfFailure : public Void::Fsm::Machine<ParentOfFailure>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
using Failure = Void::Fsm::FsmError;
|
||||||
|
|
||||||
struct Start : State {
|
struct Start : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
@@ -91,7 +102,7 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
ParentOfFailure()
|
ParentOfFailure()
|
||||||
: Void::Fsm::Machine<ParentOfFailure, int>(
|
: Void::Fsm::Machine<ParentOfFailure>(
|
||||||
MachineOptions{.own_worker = false})
|
MachineOptions{.own_worker = false})
|
||||||
{
|
{
|
||||||
initial<Start>();
|
initial<Start>();
|
||||||
@@ -121,7 +132,9 @@ Void::Testing::Test fsm_nesting_with_worker_thread()
|
|||||||
{
|
{
|
||||||
Parent fsm{}; // embedder by default here
|
Parent fsm{}; // embedder by default here
|
||||||
// Re-run as worker-driven to exercise the shared executor + worker.
|
// 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 {
|
struct Start : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,10 +19,13 @@ namespace
|
|||||||
{
|
{
|
||||||
// Attempt fails the first `fail_n` times, then succeeds. with_retry
|
// Attempt fails the first `fail_n` times, then succeeds. with_retry
|
||||||
// keeps trying; next_or routes exhaustion to a fallback.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = std::string;
|
||||||
|
using Failure = Void::Fsm::FsmError;
|
||||||
|
|
||||||
int fail_n = 0;
|
int fail_n = 0;
|
||||||
int attempts = 0;
|
int attempts = 0;
|
||||||
int retries; // how many tries with_retry gets
|
int retries; // how many tries with_retry gets
|
||||||
@@ -82,10 +85,12 @@ Void::Testing::Test retry_exhausts_routes_to_fallback()
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
class Pipe : public Void::Fsm::Machine<Pipe, int>
|
class Pipe : public Void::Fsm::Machine<Pipe>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct A : State {
|
struct A : State {
|
||||||
Task<Step> run() override { co_return next<B>(); }
|
Task<Step> run() override { co_return next<B>(); }
|
||||||
};
|
};
|
||||||
@@ -94,8 +99,7 @@ namespace
|
|||||||
};
|
};
|
||||||
using States = Void::Fsm::States<A, B>;
|
using States = Void::Fsm::States<A, B>;
|
||||||
Pipe()
|
Pipe()
|
||||||
: Void::Fsm::Machine<Pipe, int>(
|
: Void::Fsm::Machine<Pipe>(MachineOptions{.own_worker = false})
|
||||||
MachineOptions{.own_worker = false})
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -145,10 +149,12 @@ namespace
|
|||||||
struct Cancel {
|
struct Cancel {
|
||||||
};
|
};
|
||||||
|
|
||||||
class Flow : public Void::Fsm::Machine<Flow, int>
|
class Flow : public Void::Fsm::Machine<Flow>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct Idle : Reactive {
|
struct Idle : Reactive {
|
||||||
void transitions(TransitionBuilder& on) override
|
void transitions(TransitionBuilder& on) override
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -22,10 +22,13 @@ namespace
|
|||||||
|
|
||||||
// A declarative machine: Idle reacts to events with guards/actions; on
|
// A declarative machine: Idle reacts to events with guards/actions; on
|
||||||
// timeout it bails out.
|
// timeout it bails out.
|
||||||
class Gate : public Void::Fsm::Machine<Gate, int>
|
class Gate : public Void::Fsm::Machine<Gate>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
using Failure = Void::Fsm::FsmError;
|
||||||
|
|
||||||
int total = 0;
|
int total = 0;
|
||||||
std::vector<std::string> trace;
|
std::vector<std::string> trace;
|
||||||
|
|
||||||
@@ -87,10 +90,12 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Imperative state using await_event_for: times out → expired.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = std::string;
|
||||||
|
|
||||||
struct Waiting : State {
|
struct Waiting : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
@@ -129,10 +134,12 @@ Void::Testing::Test await_event_for_receives_event()
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// Reactive timeout transition (on.after().to<>).
|
// Reactive timeout transition (on.after().to<>).
|
||||||
class Deadline : public Void::Fsm::Machine<Deadline, int>
|
class Deadline : public Void::Fsm::Machine<Deadline>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct Pending : Reactive {
|
struct Pending : Reactive {
|
||||||
void transitions(TransitionBuilder& on) override
|
void transitions(TransitionBuilder& on) override
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -13,10 +13,12 @@ namespace
|
|||||||
{
|
{
|
||||||
// A machine that parks at a named state and exposes its data for
|
// A machine that parks at a named state and exposes its data for
|
||||||
// serialize/restore.
|
// serialize/restore.
|
||||||
class Pipeline : public Void::Fsm::Machine<Pipeline, int>
|
class Pipeline : public Void::Fsm::Machine<Pipeline>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct Data {
|
struct Data {
|
||||||
int processed = 0;
|
int processed = 0;
|
||||||
std::string stage = "init";
|
std::string stage = "init";
|
||||||
@@ -40,7 +42,7 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
Pipeline()
|
Pipeline()
|
||||||
: Void::Fsm::Machine<Pipeline, int>(
|
: Void::Fsm::Machine<Pipeline>(
|
||||||
MachineOptions{.own_worker = false})
|
MachineOptions{.own_worker = false})
|
||||||
{
|
{
|
||||||
initial<Ingest>();
|
initial<Ingest>();
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <Void/Testing/Test.hpp>
|
#include <Void/Testing/Test.hpp>
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <variant>
|
||||||
|
|
||||||
using Void::Coro::Task;
|
using Void::Coro::Task;
|
||||||
using Void::Fsm::FsmError;
|
using Void::Fsm::FsmError;
|
||||||
@@ -16,10 +17,13 @@ namespace Fsm = Void::Fsm;
|
|||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// Sub-machine seeded by constructor arg: doubles `seed`, fails if negative.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
using Failure = Void::Fsm::FsmError;
|
||||||
|
|
||||||
int seed;
|
int seed;
|
||||||
|
|
||||||
struct Run : State {
|
struct Run : State {
|
||||||
@@ -36,10 +40,12 @@ namespace
|
|||||||
explicit Doubler(int s = 0) : seed(s) {}
|
explicit Doubler(int s = 0) : seed(s) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Calc : public Fsm::Machine<Calc, std::string>
|
class Calc : public Fsm::Machine<Calc>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = std::string;
|
||||||
|
|
||||||
int input = 0;
|
int input = 0;
|
||||||
|
|
||||||
struct Start : State {
|
struct Start : State {
|
||||||
@@ -49,7 +55,7 @@ namespace
|
|||||||
self().input);
|
self().input);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
struct AfterDouble : Continue<int> {
|
struct AfterDouble : Continue<Doubler> {
|
||||||
Task<Step> on_success(int v) override
|
Task<Step> on_success(int v) override
|
||||||
{
|
{
|
||||||
co_return succeed("doubled:" + std::to_string(v));
|
co_return succeed("doubled:" + std::to_string(v));
|
||||||
@@ -87,7 +93,7 @@ Void::Testing::Test submachine_failure_dispatches_on_failure()
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
class Pinger : public Fsm::Machine<Pinger, void>
|
class Pinger : public Fsm::Machine<Pinger>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@@ -97,18 +103,23 @@ namespace
|
|||||||
using States = Fsm::States<Ping>;
|
using States = Fsm::States<Ping>;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Host : public Fsm::Machine<Host, int>
|
class Host : public Fsm::Machine<Host>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct Go : State {
|
struct Go : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
co_return startMachine<Pinger, Done>();
|
co_return startMachine<Pinger, Done>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
struct Done : Continue<void> {
|
struct Done : Continue<Pinger> {
|
||||||
Task<Step> on_success() override { co_return succeed(42); }
|
Task<Step> on_success(std::monostate) override
|
||||||
|
{
|
||||||
|
co_return succeed(42);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
using States = Fsm::States<Go, Done>;
|
using States = Fsm::States<Go, Done>;
|
||||||
};
|
};
|
||||||
@@ -127,23 +138,25 @@ Void::Testing::Test submachine_void_result_calls_nullary_on_success()
|
|||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
class Chain : public Fsm::Machine<Chain, int>
|
class Chain : public Fsm::Machine<Chain>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct A : State {
|
struct A : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
co_return startMachine<Doubler, B>(3); // 3 -> 6
|
co_return startMachine<Doubler, B>(3); // 3 -> 6
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
struct B : Continue<int> {
|
struct B : Continue<Doubler> {
|
||||||
Task<Step> on_success(int v) override
|
Task<Step> on_success(int v) override
|
||||||
{
|
{
|
||||||
co_return startMachine<Doubler, C>(v); // 6 -> 12
|
co_return startMachine<Doubler, C>(v); // 6 -> 12
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
struct C : Continue<int> {
|
struct C : Continue<Doubler> {
|
||||||
Task<Step> on_success(int v) override
|
Task<Step> on_success(int v) override
|
||||||
{
|
{
|
||||||
co_return succeed(v);
|
co_return succeed(v);
|
||||||
@@ -169,10 +182,12 @@ namespace
|
|||||||
struct Tick {
|
struct Tick {
|
||||||
};
|
};
|
||||||
|
|
||||||
class WaitSub : public Fsm::Machine<WaitSub, int>
|
class WaitSub : public Fsm::Machine<WaitSub>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct W : State {
|
struct W : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
@@ -183,17 +198,19 @@ namespace
|
|||||||
using States = Fsm::States<W>;
|
using States = Fsm::States<W>;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Outer : public Fsm::Machine<Outer, int>
|
class Outer : public Fsm::Machine<Outer>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
struct Begin : State {
|
struct Begin : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
co_return startMachine<WaitSub, Fin>();
|
co_return startMachine<WaitSub, Fin>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
struct Fin : Continue<int> {
|
struct Fin : Continue<WaitSub> {
|
||||||
Task<Step> on_success(int v) override
|
Task<Step> on_success(int v) override
|
||||||
{
|
{
|
||||||
co_return succeed(v);
|
co_return succeed(v);
|
||||||
|
|||||||
@@ -66,10 +66,12 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
// An FSM that bumps the counter service N times then succeeds.
|
// 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:
|
public:
|
||||||
|
|
||||||
|
using Success = int;
|
||||||
|
|
||||||
int times;
|
int times;
|
||||||
|
|
||||||
struct Run : State {
|
struct Run : State {
|
||||||
@@ -85,7 +87,7 @@ namespace
|
|||||||
};
|
};
|
||||||
|
|
||||||
explicit Worker(int t)
|
explicit Worker(int t)
|
||||||
: Void::Fsm::Machine<Worker, int>(
|
: Void::Fsm::Machine<Worker>(
|
||||||
MachineOptions{.own_worker = false}),
|
MachineOptions{.own_worker = false}),
|
||||||
times(t)
|
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
|
// Worker-backed FSM so the orchestrator's start() spins a worker and
|
||||||
// the FSM runs to completion on its own.
|
// 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 {
|
struct Run : State {
|
||||||
Task<Step> run() override
|
Task<Step> run() override
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user