FSM v2 (D): hierarchy ergonomics — composite regions, await_any, state_path
- run_region<Sub>() — run_child plus introspection: shares the parent's executor/event mailbox/services, the calling state's on_enter/on_exit wrap it, and the parent's state_path() reports the parent->child chain while it runs. The caller maps the child's Outcome to a sibling transition. - await_any<Es...>() — typed multi-event await yielding std::variant<Es...>, so one state handles several event types at a single level (parent-level watching alongside a child region's own events). - state_path() now appends an active region child's path (consumer- consistent; deterministic in pump() mode). current_state() stays the lock-free cross-thread read. Coroutines aren't preemptible mid-flight, so composite cancellation is cooperative: the region awaits a shared Cancel event (events route to the deepest awaiter) and returns a failing Outcome the parent maps — no frame is abandoned. Documented in CLAUDE.md. 3 new tests (Hierarchy): region result drives the parent transition, parent cancel routes to a sibling, state_path reports parent->child. 982 green, ASan+UBSan clean, stable across 10 runs, -Werror clean. Completes the FSM v2 plan (A-D). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d9842129c7
commit
667045564e
3 files changed
+226
-5
No files matched your search
@@ -16,6 +16,7 @@
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
@@ -26,6 +27,7 @@
|
||||
#include <typeinfo>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
#if __has_include(<meta>)
|
||||
@@ -214,6 +216,30 @@ namespace Void::Fsm
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/// Enter a child region (composite sub-state): a nested
|
||||
/// machine sharing this machine's executor, event mailbox
|
||||
/// and services. This state's on_enter/on_exit wrap it, and
|
||||
/// state_path() reports the parent→child chain while it
|
||||
/// runs. Map the child's Outcome to a sibling transition:
|
||||
/// auto r = co_await run_region<Work>();
|
||||
/// co_return r ? next<Done>() : next<Recover>();
|
||||
template <typename Sub, typename... Args>
|
||||
[[nodiscard]] auto run_region(Args&&... args) const
|
||||
{
|
||||
return m_machine->template run_region<Sub>(
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/// Await whichever of several event types arrives first,
|
||||
/// yielding a std::variant<Es...>. Handles multiple events
|
||||
/// at one level (e.g. a parent watching Cancel alongside a
|
||||
/// child region's own events).
|
||||
template <typename... Es>
|
||||
[[nodiscard]] auto await_any() const
|
||||
{
|
||||
return m_machine->template await_any<Es...>();
|
||||
}
|
||||
|
||||
template <typename S> [[nodiscard]] bool has_state() const
|
||||
{
|
||||
return m_machine->template has_state<S>();
|
||||
@@ -747,6 +773,41 @@ namespace Void::Fsm
|
||||
m_ctx.get(), std::move(types), {}, false, deadline};
|
||||
}
|
||||
|
||||
/// Typed multi-event await: suspend until one of `Es...` arrives,
|
||||
/// returning it as std::variant<Es...>. Lets one state react to
|
||||
/// several event types (parent-level handling for child regions).
|
||||
template <typename... Es> [[nodiscard]] auto await_any()
|
||||
{
|
||||
struct Awaiter {
|
||||
Detail::ExecutionContext* ctx;
|
||||
std::array<const std::type_info*, sizeof...(Es)> types;
|
||||
Detail::EventEnvelope env;
|
||||
|
||||
[[nodiscard]] bool await_ready()
|
||||
{
|
||||
return ctx->try_take_any(types, env);
|
||||
}
|
||||
void await_suspend(std::coroutine_handle<> h)
|
||||
{
|
||||
ctx->set_waiter(types, h, &env, nullptr,
|
||||
std::nullopt);
|
||||
}
|
||||
[[nodiscard]] std::variant<Es...> await_resume()
|
||||
{
|
||||
std::optional<std::variant<Es...>> out;
|
||||
((env.type != nullptr && *env.type == typeid(Es)
|
||||
? (out.emplace(std::in_place_type<Es>,
|
||||
std::any_cast<Es>(
|
||||
std::move(env.value))),
|
||||
true)
|
||||
: false) ||
|
||||
...);
|
||||
return std::move(*out);
|
||||
}
|
||||
};
|
||||
return Awaiter{m_ctx.get(), {(&typeid(Es))...}, {}};
|
||||
}
|
||||
|
||||
/// Suspend for `duration`, then resume (timer-driven).
|
||||
template <typename Rep, typename Period>
|
||||
[[nodiscard]] auto delay(std::chrono::duration<Rep, Period> d)
|
||||
@@ -781,6 +842,24 @@ namespace Void::Fsm
|
||||
co_return co_await child.drive();
|
||||
}
|
||||
|
||||
/// Composite sub-state: run `Sub` as a child region sharing this
|
||||
/// machine's executor, event mailbox and services, and expose its
|
||||
/// active state through this machine's state_path() while it runs
|
||||
/// (parent→child chain). Returns the child's Outcome, which the
|
||||
/// caller maps to a sibling transition. The calling state's
|
||||
/// on_enter/on_exit naturally wrap the region.
|
||||
template <typename Sub, typename... Args>
|
||||
[[nodiscard]] Coro::Task<typename Sub::Outcome>
|
||||
run_region(Args&&... args)
|
||||
{
|
||||
Sub child{std::forward<Args>(args)...};
|
||||
child.attach(m_ctx, m_services);
|
||||
m_region_probe = [&child] { return child.state_path(); };
|
||||
auto outcome = co_await child.drive();
|
||||
m_region_probe = nullptr;
|
||||
co_return std::move(outcome);
|
||||
}
|
||||
|
||||
// ── observation ───────────────────────────────────────────────
|
||||
[[nodiscard]] std::string current_state() const
|
||||
{
|
||||
@@ -791,14 +870,25 @@ namespace Void::Fsm
|
||||
return m_names[static_cast<std::size_t>(idx)];
|
||||
}
|
||||
|
||||
/// Active state chain (root → leaf). Currently a single entry;
|
||||
/// hierarchy (Phase D) extends it.
|
||||
/// Active state chain (root → leaf). When a child region is
|
||||
/// running (run_region), its path is appended, so a composite
|
||||
/// state reports {composite, ...child leaf}. Unlike the lock-free
|
||||
/// current_state(), the region part is consumer-consistent: read
|
||||
/// it from within the machine or once it has settled (it is
|
||||
/// deterministic in embedder/pump mode).
|
||||
[[nodiscard]] std::vector<std::string> state_path() const
|
||||
{
|
||||
std::vector<std::string> path;
|
||||
if (auto s = current_state(); !s.empty()) {
|
||||
return {std::move(s)};
|
||||
path.push_back(std::move(s));
|
||||
}
|
||||
return {};
|
||||
if (m_region_probe) {
|
||||
auto sub = m_region_probe();
|
||||
path.insert(path.end(),
|
||||
std::make_move_iterator(sub.begin()),
|
||||
std::make_move_iterator(sub.end()));
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/// All registered state names (registration order).
|
||||
@@ -1097,6 +1187,10 @@ namespace Void::Fsm
|
||||
std::function<void(const Transition&)> m_observer;
|
||||
std::vector<Interceptor> m_interceptors;
|
||||
std::uint64_t m_step{0};
|
||||
|
||||
// Active child region's path provider (run_region); consulted by
|
||||
// state_path(). Set/cleared on the consumer thread.
|
||||
std::function<std::vector<std::string>()> m_region_probe;
|
||||
};
|
||||
|
||||
} // namespace Void::Fsm
|
||||
Reference in new issue
Block a user