Files
Library/include/Void/Fsm/StateKey.hpp
T
faumarayandClaude Opus 4.8 80293ee56f Add async/resilience/reflection primitives + FSM v2 rewrite
A round of new universal capabilities (all generic — templated/reflection
based, no app coupling), verified by the test suite (1103 passing):

Data reflection:
- Reflect.hpp now (de)serializes chrono::duration, chrono::time_point,
  std::variant (tagged), std::array, and non-string-keyed std::map;
  Meta.hpp gains is_time_point_v et al.

Async:
- EventBus gains Mode::Async (worker-thread dispatch + flush()).
- ThreadPool gains a bounded queue, try_submit(), and rejection stats.

New modules / containers:
- Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker,
  ResourceMonitor.
- Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V>
  (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats).
- System: process/CPU/memory metrics (Metrics.hpp) and a streaming
  Process API.

Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions,
history states, deferred events), Configuration/Logging reflection
helpers, and the accompanying test suites.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-22 15:45:16 +03:00

144 lines
3.9 KiB
C++

#pragma once
#include <Void/Async/Task.hpp>
#include <any>
#include <functional>
#include <string>
#include <typeinfo>
#include <utility>
namespace Void::Fsm
{
namespace Coro = Void::Coro;
namespace Detail
{
class StateBase;
}
/// 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;
};
enum class Status {
Idle, // constructed, not started
Running, // worker active
Succeeded, // finished with a typed Success
Failed, // finished with a typed Failure
Faulted // stopped by a system fault (see system_error())
};
/// 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 StateKey
{
public:
enum class Kind { Transition, Success, Fail, Launch, Fault };
[[nodiscard]] static StateKey
transition(const std::type_info& target)
{
StateKey s;
s.m_kind = Kind::Transition;
s.m_target = &target;
return s;
}
/// Signal a system fault (programming error) — distinct from a
/// typed Failure. Routed to Machine::system_error().
[[nodiscard]] static StateKey fault(FsmError error)
{
StateKey s;
s.m_kind = Kind::Fault;
s.m_payload = std::any{std::move(error)};
return s;
}
/// A bound dispatcher: given the entered Next state and the sub's
/// LaunchResult, calls Next's signature-matched handler.
using Dispatch = std::function<Coro::Task<StateKey>(
Detail::StateBase&, LaunchResult)>;
/// Launch a sub-machine, then enter `target` (a continuation state)
/// with the sub's LaunchResult, dispatched via `dispatch`.
[[nodiscard]] static StateKey
launch(const std::type_info& target,
std::function<Coro::Task<LaunchResult>()> launcher,
Dispatch dispatch)
{
StateKey s;
s.m_kind = Kind::Launch;
s.m_target = &target;
s.m_launcher = std::move(launcher);
s.m_dispatch = std::move(dispatch);
return s;
}
/// Finish with a typed Success payload (empty std::any for void).
[[nodiscard]] static StateKey success(std::any value)
{
StateKey s;
s.m_kind = Kind::Success;
s.m_payload = std::move(value);
return s;
}
/// Finish with a typed Failure payload (empty std::any for void).
[[nodiscard]] static StateKey failure(std::any value)
{
StateKey s;
s.m_kind = Kind::Fail;
s.m_payload = std::move(value);
return s;
}
[[nodiscard]] Kind kind() const noexcept { return m_kind; }
[[nodiscard]] const std::type_info* target() const noexcept
{
return m_target;
}
/// 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()
{
return m_launcher();
}
/// Take the bound dispatcher for a Launch step.
[[nodiscard]] Dispatch take_dispatch()
{
return std::move(m_dispatch);
}
private:
Kind m_kind{Kind::Fail};
const std::type_info* m_target{nullptr};
std::any m_payload;
std::function<Coro::Task<LaunchResult>()> m_launcher;
Dispatch m_dispatch;
};
} // namespace Void::Fsm