Files
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

100 lines
2.9 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <variant>
using Void::Coro::Task;
using Void::Fsm::State;
using Void::Fsm::StateKey;
namespace Fsm = Void::Fsm;
// Demonstrates the header/source split the module is designed for: the machine
// declares its states as forward declarations inside `struct States { … };`
// (this block is what would live in a header), and the states are *realised*
// out-of-line at namespace scope (this is what would live in a .cpp). The base
// auto-registers them by reflecting `States`; no behaviour is written outside a
// state. The constructor is also defined out-of-line, after the realisations,
// so the states are complete when the base registers them.
namespace
{
struct Charge {
int amount;
};
struct Dispense {
};
// ── "header" part ──────────────────────────────────────────────────────
class Vending : public Fsm::Machine<Vending>
{
public:
using Success = int; // dispensed credit
int credit = 0;
struct States {
struct Idle; // first => initial
struct Collecting;
struct Done;
};
Vending(); // realised below, after the states
};
} // namespace
// ── "source" part: state realisations (namespace scope) ─────────────────────
struct Vending::States::Idle : Vending::State {
Task<StateKey> run()
{
co_await await_event<Charge>(); // first coin -> start collecting
co_return next<Collecting>();
}
};
struct Vending::States::Collecting : Vending::State {
Task<StateKey> run()
{
auto& m = self();
for (;;) {
auto ev = co_await await_any<Charge, Dispense>();
if (std::holds_alternative<Dispense>(ev)) {
co_return next<Done>();
}
m.credit += std::get<Charge>(ev).amount;
}
}
};
struct Vending::States::Done : Vending::State {
Task<StateKey> run() { co_return succeed(self().credit); }
};
Vending::Vending() = default; // base registers Idle/Collecting/Done from States
// @tag integration
Void::Testing::Test split_machine_runs_with_out_of_line_states()
{
Vending fsm;
fsm.start();
fsm.send(Charge{10}); // Idle -> Collecting (first coin counted there too?)
fsm.send(Charge{5}, Charge{10});
fsm.send(Dispense{});
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
// Idle consumes the first Charge to leave; Collecting sums 5 + 10 = 15.
Void::Testing::expect_eq(*outcome, 15);
}
Void::Testing::Test split_machine_auto_registers_states_in_order()
{
Vending fsm;
const auto& names = fsm.state_names();
Void::Testing::expect_eq(names.size(), std::size_t{3});
Void::Testing::expect_eq(names[0], std::string{"Idle"}); // initial
Void::Testing::expect_eq(names[1], std::string{"Collecting"});
Void::Testing::expect_eq(names[2], std::string{"Done"});
}