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>
228 lines
6.1 KiB
C++
228 lines
6.1 KiB
C++
#pragma once
|
|
|
|
#include <Void/Async/Task.hpp>
|
|
#include <Void/Fsm/Detail/Result.hpp>
|
|
#include <Void/Fsm/Detail/TypeName.hpp>
|
|
#include <Void/Fsm/Step.hpp>
|
|
|
|
#include <any>
|
|
#include <chrono>
|
|
#include <functional>
|
|
#include <string>
|
|
#include <type_traits>
|
|
#include <typeinfo>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
// 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&`); 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
|
|
{
|
|
namespace Coro = Void::Coro;
|
|
|
|
/// One declared transition: event type, optional guard/action, the Step to
|
|
/// take, plus names for introspection.
|
|
template <typename Derived> struct EventEntry {
|
|
const std::type_info* type{nullptr};
|
|
std::function<bool(Derived&, const std::any&)> guard;
|
|
std::function<Coro::Task<void>(Derived&, const std::any&)> action;
|
|
Step step;
|
|
std::string event_name; // for introspection
|
|
std::string target_name; // for introspection
|
|
};
|
|
|
|
/// A reactive state's full transition table (events + an optional timeout).
|
|
template <typename Derived> struct TransitionTable {
|
|
std::vector<EventEntry<Derived>> events;
|
|
bool has_timeout{false};
|
|
std::chrono::steady_clock::duration timeout_after{};
|
|
Step timeout_step;
|
|
std::string timeout_target_name;
|
|
};
|
|
|
|
/// Fluent builder for one event row: `on.event<E>().guard(..).action(..)
|
|
/// .to<S>()` (or `.finish(v)` / `.fail(v)`).
|
|
template <typename Derived, typename E> class EventClause
|
|
{
|
|
public:
|
|
|
|
explicit EventClause(TransitionTable<Derived>& table)
|
|
: m_table(&table)
|
|
{
|
|
}
|
|
|
|
EventClause& guard(std::function<bool(Derived&, const E&)> g)
|
|
{
|
|
m_guard = std::move(g);
|
|
return *this;
|
|
}
|
|
|
|
EventClause&
|
|
action(std::function<Coro::Task<void>(Derived&, const E&)> a)
|
|
{
|
|
m_action = std::move(a);
|
|
return *this;
|
|
}
|
|
|
|
EventClause& action(std::function<void(Derived&, const E&)> a)
|
|
{
|
|
m_action = [a = std::move(a)](Derived& d,
|
|
const E& e) -> Coro::Task<void> {
|
|
a(d, e);
|
|
co_return;
|
|
};
|
|
return *this;
|
|
}
|
|
|
|
template <typename S> void to()
|
|
{
|
|
if constexpr (requires { typename Derived::States; }) {
|
|
static_assert(Derived::States::template contains<S>(),
|
|
"to<S>: S is not in this machine's "
|
|
"States<...> list");
|
|
}
|
|
commit(Step::transition(typeid(S)), type_name<S>());
|
|
}
|
|
|
|
void finish()
|
|
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<SuccessOf<Derived>>)
|
|
{
|
|
commit(Step::success(std::any{
|
|
SuccessOf<Derived>(std::forward<V>(value))}),
|
|
"(success)");
|
|
}
|
|
|
|
void fail()
|
|
requires std::is_void_v<FailureOf<Derived>>
|
|
{
|
|
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:
|
|
|
|
void commit(Step step, std::string target)
|
|
{
|
|
EventEntry<Derived> entry;
|
|
entry.type = &typeid(E);
|
|
if (m_guard) {
|
|
entry.guard = [g = std::move(m_guard)](Derived& d,
|
|
const std::any& a) {
|
|
return g(d, std::any_cast<const E&>(a));
|
|
};
|
|
}
|
|
if (m_action) {
|
|
entry.action =
|
|
[a = std::move(m_action)](
|
|
Derived& d,
|
|
const std::any& any) -> Coro::Task<void> {
|
|
co_await a(d, std::any_cast<const E&>(any));
|
|
};
|
|
}
|
|
entry.step = std::move(step);
|
|
entry.event_name = type_name<E>();
|
|
entry.target_name = std::move(target);
|
|
m_table->events.push_back(std::move(entry));
|
|
}
|
|
|
|
TransitionTable<Derived>* m_table;
|
|
std::function<bool(Derived&, const E&)> m_guard;
|
|
std::function<Coro::Task<void>(Derived&, const E&)> m_action;
|
|
};
|
|
|
|
/// Fluent builder for the timeout row: `on.after(d).to<S>()` / `.fail(v)`.
|
|
template <typename Derived> class AfterClause
|
|
{
|
|
public:
|
|
|
|
AfterClause(TransitionTable<Derived>& table,
|
|
std::chrono::steady_clock::duration after)
|
|
: m_table(&table), m_after(after)
|
|
{
|
|
}
|
|
|
|
template <typename S> void to()
|
|
{
|
|
if constexpr (requires { typename Derived::States; }) {
|
|
static_assert(Derived::States::template contains<S>(),
|
|
"after().to<S>: S not in States<...>");
|
|
}
|
|
m_table->has_timeout = true;
|
|
m_table->timeout_after = m_after;
|
|
m_table->timeout_step = Step::transition(typeid(S));
|
|
m_table->timeout_target_name = type_name<S>();
|
|
}
|
|
|
|
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(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)";
|
|
}
|
|
|
|
private:
|
|
|
|
TransitionTable<Derived>* m_table;
|
|
std::chrono::steady_clock::duration m_after;
|
|
};
|
|
|
|
/// Entry point handed to `transitions(TransitionBuilder& on)`.
|
|
template <typename Derived> class TransitionBuilder
|
|
{
|
|
public:
|
|
|
|
explicit TransitionBuilder(TransitionTable<Derived>& table)
|
|
: m_table(&table)
|
|
{
|
|
}
|
|
|
|
template <typename E> [[nodiscard]] EventClause<Derived, E> event()
|
|
{
|
|
return EventClause<Derived, E>{*m_table};
|
|
}
|
|
|
|
template <typename Rep, typename Period>
|
|
[[nodiscard]] AfterClause<Derived>
|
|
after(std::chrono::duration<Rep, Period> d)
|
|
{
|
|
return AfterClause<Derived>{
|
|
*m_table, std::chrono::duration_cast<
|
|
std::chrono::steady_clock::duration>(d)};
|
|
}
|
|
|
|
private:
|
|
|
|
TransitionTable<Derived>* m_table;
|
|
};
|
|
} // namespace Void::Fsm::Detail
|