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>
This commit is contained in:
faumarayandClaude Opus 4.8 committed 2026-06-22 15:45:16 +03:00
1 parent 43b5a81504
commit 80293ee56f
93 files changed
+8500 -3312

No files matched your search

+200 -81
View File
@@ -1,114 +1,233 @@
#pragma once
#include <Void/Core/Meta.hpp>
#include <initializer_list>
#include <string>
#include <string_view>
#include <type_traits>
namespace Void::Flags
{
// ── Flags<E> ─────────────────────────────────────────────────────────────────
// Type-safe bitmask wrapper over a scoped enum class E.
// E's enumerators must be distinct single-bit values (1, 2, 4, 8, …).
// ── Flags<E>
// ─────────────────────────────────────────────────────────────────
// Type-safe bitmask wrapper over a scoped enum class E.
// E's enumerators must be distinct single-bit values (1, 2, 4, 8, …).
template <typename E>
requires std::is_enum_v<E>
class Flags
{
public:
using underlying = std::underlying_type_t<E>;
template <typename E>
requires std::is_enum_v<E>
class Flags
{
public:
constexpr Flags() noexcept = default;
constexpr Flags(E bit) noexcept : bits_{static_cast<underlying>(bit)} {}
constexpr explicit Flags(underlying raw) noexcept : bits_{raw} {}
constexpr Flags(std::initializer_list<E> list) noexcept
{
for (const E e : list) bits_ |= static_cast<underlying>(e);
}
using underlying = std::underlying_type_t<E>;
// ── query ─────────────────────────────────────────────────────────────────
constexpr Flags() noexcept = default;
constexpr Flags(E bit) noexcept
: bits_{static_cast<underlying>(bit)}
{
}
constexpr explicit Flags(underlying raw) noexcept : bits_{raw} {}
constexpr Flags(std::initializer_list<E> list) noexcept
{
for (const E e : list) {
bits_ |= static_cast<underlying>(e);
}
}
[[nodiscard]] constexpr bool test(E bit) const noexcept
{
return (bits_ & static_cast<underlying>(bit)) != 0;
}
// ── query
// ─────────────────────────────────────────────────────────────────
[[nodiscard]] constexpr bool any() const noexcept { return bits_ != 0; }
[[nodiscard]] constexpr bool none() const noexcept { return bits_ == 0; }
[[nodiscard]] constexpr bool test(E bit) const noexcept
{
return (bits_ & static_cast<underlying>(bit)) != 0;
}
[[nodiscard]] constexpr bool all(Flags mask) const noexcept
{
return (bits_ & mask.bits_) == mask.bits_;
}
[[nodiscard]] constexpr bool any() const noexcept
{
return bits_ != 0;
}
[[nodiscard]] constexpr bool none() const noexcept
{
return bits_ == 0;
}
[[nodiscard]] constexpr bool any_of(Flags mask) const noexcept
{
return (bits_ & mask.bits_) != 0;
}
[[nodiscard]] constexpr bool all(Flags mask) const noexcept
{
return (bits_ & mask.bits_) == mask.bits_;
}
[[nodiscard]] constexpr underlying raw() const noexcept { return bits_; }
[[nodiscard]] constexpr bool any_of(Flags mask) const noexcept
{
return (bits_ & mask.bits_) != 0;
}
// ── mutation ──────────────────────────────────────────────────────────────
[[nodiscard]] constexpr underlying raw() const noexcept
{
return bits_;
}
constexpr Flags& set(E bit) noexcept
{
bits_ |= static_cast<underlying>(bit);
return *this;
}
// ── mutation
// ──────────────────────────────────────────────────────────────
constexpr Flags& unset(E bit) noexcept
{
bits_ &= ~static_cast<underlying>(bit);
return *this;
}
constexpr Flags& set(E bit) noexcept
{
bits_ |= static_cast<underlying>(bit);
return *this;
}
constexpr Flags& toggle(E bit) noexcept
{
bits_ ^= static_cast<underlying>(bit);
return *this;
}
constexpr Flags& unset(E bit) noexcept
{
bits_ &= ~static_cast<underlying>(bit);
return *this;
}
constexpr void clear() noexcept { bits_ = 0; }
constexpr Flags& toggle(E bit) noexcept
{
bits_ ^= static_cast<underlying>(bit);
return *this;
}
// ── operators ─────────────────────────────────────────────────────────────
constexpr void clear() noexcept { bits_ = 0; }
[[nodiscard]] constexpr Flags operator|(Flags o) const noexcept { return Flags{static_cast<underlying>(bits_ | o.bits_)}; }
[[nodiscard]] constexpr Flags operator&(Flags o) const noexcept { return Flags{static_cast<underlying>(bits_ & o.bits_)}; }
[[nodiscard]] constexpr Flags operator^(Flags o) const noexcept { return Flags{static_cast<underlying>(bits_ ^ o.bits_)}; }
[[nodiscard]] constexpr Flags operator~() const noexcept { return Flags{static_cast<underlying>(~bits_)}; }
// ── operators
// ─────────────────────────────────────────────────────────────
[[nodiscard]] constexpr Flags operator|(E bit) const noexcept { return *this | Flags{bit}; }
[[nodiscard]] constexpr Flags operator&(E bit) const noexcept { return *this & Flags{bit}; }
[[nodiscard]] constexpr Flags operator^(E bit) const noexcept { return *this ^ Flags{bit}; }
[[nodiscard]] constexpr Flags operator|(Flags o) const noexcept
{
return Flags{static_cast<underlying>(bits_ | o.bits_)};
}
[[nodiscard]] constexpr Flags operator&(Flags o) const noexcept
{
return Flags{static_cast<underlying>(bits_ & o.bits_)};
}
[[nodiscard]] constexpr Flags operator^(Flags o) const noexcept
{
return Flags{static_cast<underlying>(bits_ ^ o.bits_)};
}
[[nodiscard]] constexpr Flags operator~() const noexcept
{
return Flags{static_cast<underlying>(~bits_)};
}
constexpr Flags& operator|=(Flags o) noexcept { bits_ |= o.bits_; return *this; }
constexpr Flags& operator&=(Flags o) noexcept { bits_ &= o.bits_; return *this; }
constexpr Flags& operator^=(Flags o) noexcept { bits_ ^= o.bits_; return *this; }
[[nodiscard]] constexpr Flags operator|(E bit) const noexcept
{
return *this | Flags{bit};
}
[[nodiscard]] constexpr Flags operator&(E bit) const noexcept
{
return *this & Flags{bit};
}
[[nodiscard]] constexpr Flags operator^(E bit) const noexcept
{
return *this ^ Flags { bit };
}
constexpr Flags& operator|=(E bit) noexcept { return set(bit); }
constexpr Flags& operator&=(E bit) noexcept { bits_ &= static_cast<underlying>(bit); return *this; }
constexpr Flags& operator^=(E bit) noexcept { return toggle(bit); }
constexpr Flags& operator|=(Flags o) noexcept
{
bits_ |= o.bits_;
return *this;
}
constexpr Flags& operator&=(Flags o) noexcept
{
bits_ &= o.bits_;
return *this;
}
constexpr Flags& operator^=(Flags o) noexcept
{
bits_ ^= o.bits_;
return *this;
}
[[nodiscard]] constexpr bool operator==(const Flags&) const noexcept = default;
[[nodiscard]] constexpr explicit operator bool() const noexcept { return any(); }
constexpr Flags& operator|=(E bit) noexcept { return set(bit); }
constexpr Flags& operator&=(E bit) noexcept
{
bits_ &= static_cast<underlying>(bit);
return *this;
}
constexpr Flags& operator^=(E bit) noexcept { return toggle(bit); }
private:
underlying bits_{0};
};
[[nodiscard]] constexpr bool
operator==(const Flags&) const noexcept = default;
[[nodiscard]] constexpr explicit operator bool() const noexcept
{
return any();
}
// ── make_flags — convenience factory ─────────────────────────────────────────
private:
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] constexpr Flags<E> make_flags(std::initializer_list<E> list) noexcept
{
return Flags<E>{list};
}
underlying bits_{0};
};
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] constexpr Flags<E> operator|(E a, E b) noexcept
{
return Flags<E>{a} | Flags<E>{b};
}
// ── make_flags — convenience factory
// ─────────────────────────────────────────
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] constexpr Flags<E>
make_flags(std::initializer_list<E> list) noexcept
{
return Flags<E>{list};
}
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] constexpr Flags<E> operator|(E a, E b) noexcept
{
return Flags<E>{a} | Flags<E>{b};
}
// ── reflective names (Void::Meta enum reflection)
// ────────────────────────────
#ifdef VOID_HAS_META_REFLECTION
/// Render the set bits as their enumerator names, joined by `sep`
/// (e.g. "Read|Write"). A zero-valued enumerator never appears.
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] std::string to_string(Flags<E> f, std::string_view sep = "|")
{
std::string out;
for (const auto& [value, name] : Void::Meta::enum_entries<E>()) {
if (f.test(value)) {
if (!out.empty()) {
out += sep;
}
out += name;
}
}
return out;
}
/// Parse a `sep`-separated list of enumerator names into a Flags<E>.
/// Unknown names are ignored.
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] Flags<E> parse(std::string_view text, char sep = '|')
{
Flags<E> f;
std::size_t pos = 0;
while (pos <= text.size()) {
const auto next = text.find(sep, pos);
const auto end =
(next == std::string_view::npos) ? text.size() : next;
const auto token = text.substr(pos, end - pos);
if (!token.empty()) {
if (const auto e = Void::Meta::enum_parse<E>(token);
e.has_value()) {
f.set(*e);
}
}
if (next == std::string_view::npos) {
break;
}
pos = next + 1;
}
return f;
}
#endif // VOID_HAS_META_REFLECTION
} // namespace Void::Flags
+351
View File
@@ -0,0 +1,351 @@
#pragma once
#include <Void/Detail/Enum.hpp>
#include <array>
#include <chrono>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <format>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
#if __has_include(<meta>)
#include <meta>
#define VOID_HAS_META_REFLECTION 1
#endif
// ── Void::Meta
// ──────────────────────────────────────────────────────────────── The shared
// C++26 P2996 reflection layer for VoidLibrary. One place for the member walk,
// leaf-shape traits, annotation helpers, generic algorithms over data members
// (equal / compare / hash / repr) and enum utilities. Every other module that
// reflects structs or enums builds on this (Data/Json, Configuration/Bind,
// Functional/Repr, Math, Text, Log, Time, ...).
//
// Annotations carry a value-based fixed-capacity string (`Meta::Str`) because
// std::string_view is not a structural type and a const char* payload cannot be
// promoted to a constant; domain vocabularies (json::Rename, config::Key,
// arg::Help, ...) are small structs over `Str`.
namespace Void::Meta
{
// ── leaf / container shape traits (single source) ─────────────────────
template <class> inline constexpr bool is_optional_v = false;
template <class U>
inline constexpr bool is_optional_v<std::optional<U>> = true;
template <class> inline constexpr bool is_vector_v = false;
template <class U> inline constexpr bool is_vector_v<std::vector<U>> = true;
template <class> inline constexpr bool is_string_map_v = false;
template <class U>
inline constexpr bool is_string_map_v<std::map<std::string, U>> = true;
template <class> inline constexpr bool is_duration_v = false;
template <class R, class P>
inline constexpr bool is_duration_v<std::chrono::duration<R, P>> = true;
template <class> inline constexpr bool is_time_point_v = false;
template <class C, class D>
inline constexpr bool is_time_point_v<std::chrono::time_point<C, D>> = true;
template <class> inline constexpr bool is_variant_v = false;
template <class... Ts>
inline constexpr bool is_variant_v<std::variant<Ts...>> = true;
template <class> inline constexpr bool is_std_array_v = false;
template <class U, std::size_t N>
inline constexpr bool is_std_array_v<std::array<U, N>> = true;
// A keyed map whose key is *not* std::string. String-keyed maps stay on the
// dedicated is_string_map_v object path; these serialize as an array of
// {key, value} pairs so an arbitrary reflectable key type works.
template <class> inline constexpr bool is_map_v = false;
template <class K, class V, class C, class A>
inline constexpr bool is_map_v<std::map<K, V, C, A>> =
!std::same_as<K, std::string>;
template <class K, class V, class H, class E, class A>
inline constexpr bool is_map_v<std::unordered_map<K, V, H, E, A>> =
!std::same_as<K, std::string>;
template <class T>
concept Leaf = std::same_as<T, bool> || std::is_arithmetic_v<T> ||
std::is_enum_v<T> || std::same_as<T, std::string>;
#ifdef VOID_HAS_META_REFLECTION
namespace meta = std::meta;
// ── member walk (the single reflection of a struct's data members) ────
template <typename T>
inline constexpr auto members_v =
std::define_static_array(meta::nonstatic_data_members_of(
^^T, meta::access_context::unchecked()));
/// Number of non-static data members of T.
template <typename T> [[nodiscard]] consteval std::size_t field_count()
{
return members_v<T>.size();
}
/// A class type with at least one reflectable data member.
template <class T>
concept Struct =
(std::is_class_v<T> || std::is_union_v<T>) && (field_count<T>() > 0);
/// Identifier of a data-member reflection.
[[nodiscard]] consteval std::string_view field_name(meta::info m)
{
return meta::identifier_of(m);
}
/// The declared field names of T (in declaration order).
template <typename T>
[[nodiscard]] std::vector<std::string_view> field_names()
{
std::vector<std::string_view> out;
out.reserve(field_count<T>());
template for (constexpr auto m : members_v<T>)
{
out.push_back(meta::identifier_of(m));
}
return out;
}
/// Invoke `fn(name, fieldref)` for each data member of `obj`
/// (mutable or const depending on `obj`).
template <typename T, typename Fn> void for_each_field(T&& obj, Fn&& fn)
{
using U = std::remove_reference_t<T>;
template for (constexpr auto m : members_v<U>)
{
fn(std::string_view{meta::identifier_of(m)}, obj.[:m:]);
}
}
// ── annotations ───────────────────────────────────────────────────────
/// Value-based fixed-capacity string usable as an annotation payload
/// (structural; copies up to 63 chars).
struct Str {
char data[64]{};
std::size_t len{};
consteval Str(const char* s)
{
while (s[len] != '\0' && len < 63) {
data[len] = s[len];
++len;
}
}
[[nodiscard]] constexpr std::string_view view() const
{
return {data, len};
}
[[nodiscard]] constexpr operator std::string_view() const
{
return view();
}
};
/// True if reflection `m` carries an annotation of type `Tag`.
template <typename Tag>
[[nodiscard]] consteval bool has_annotation(meta::info m)
{
return !meta::annotations_of_with_type(m, ^^Tag).empty();
}
/// The first `Tag` annotation on `m` (its value), or nullopt.
template <typename Tag>
[[nodiscard]] consteval std::optional<Tag> annotation_of(meta::info m)
{
for (meta::info a : meta::annotations_of_with_type(m, ^^Tag)) {
return meta::extract<Tag>(a);
}
return std::nullopt;
}
/// The (unqualified) name of a type, e.g. type_name<Foo>() == "Foo".
template <typename T> [[nodiscard]] std::string type_name()
{
if constexpr (meta::has_identifier(^^T)) {
return std::string{meta::identifier_of(^^T)};
} else {
return std::string{meta::display_string_of(^^T)};
}
}
// ── enum utilities (extend Void::Detail enum reflection) ──────────────
using Void::Detail::enum_cast;
using Void::Detail::enum_name;
using Void::Detail::enum_parse;
/// Number of enumerators of E.
template <typename E>
requires std::is_enum_v<E>
inline constexpr std::size_t enum_count = meta::enumerators_of(^^E).size();
/// The enumerator values of E (declaration order).
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] consteval std::array<E, enum_count<E>> enum_values()
{
std::array<E, enum_count<E>> out{};
std::size_t i = 0;
template for (constexpr auto e :
std::define_static_array(meta::enumerators_of(^^E)))
{
out[i++] = [:e:];
}
return out;
}
/// (value, identifier) pairs for every enumerator of E (declaration order).
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] std::array<std::pair<E, std::string_view>, enum_count<E>>
enum_entries()
{
std::array<std::pair<E, std::string_view>, enum_count<E>> out{};
std::size_t i = 0;
template for (constexpr auto e :
std::define_static_array(meta::enumerators_of(^^E)))
{
out[i++] = {[:e:], meta::identifier_of(e)};
}
return out;
}
#else // no reflection
template <class T>
concept Struct = false;
#endif // VOID_HAS_META_REFLECTION
// ── repr (diagnostic rendering) ───────────────────────────────────────
// 1. std::formatter<T> → std::format. 2. a .format() method. 3. reflect
// the data members as `Type{a: 1, b: 2}`. 4. "<unrepresentable>".
template <typename T> [[nodiscard]] std::string repr(const T& value);
template <typename T>
concept Formattable = std::formattable<T, char>;
template <typename T>
concept HasFormatMethod = requires(const T& v) {
{ v.format() } -> std::convertible_to<std::string>;
};
#ifdef VOID_HAS_META_REFLECTION
template <typename T> [[nodiscard]] std::string reflect_struct(const T& obj)
{
std::string out;
if constexpr (meta::has_identifier(^^T)) {
out += meta::identifier_of(^^T);
} else {
out += "?";
}
out += '{';
bool first = true;
template for (constexpr auto m : members_v<T>)
{
if (!first) {
out += ", ";
}
out += meta::identifier_of(m);
out += ": ";
out += repr(obj.[:m:]);
first = false;
}
out += '}';
return out;
}
#endif
template <typename T> [[nodiscard]] std::string repr(const T& value)
{
if constexpr (Formattable<T>) {
return std::format("{}", value);
} else if constexpr (HasFormatMethod<T>) {
return std::string{value.format()};
}
#ifdef VOID_HAS_META_REFLECTION
else if constexpr (std::is_class_v<T> || std::is_union_v<T>) {
return reflect_struct(value);
}
#endif
else {
return "<unrepresentable>";
}
}
// ── generic algorithms over data members ──────────────────────────────
#ifdef VOID_HAS_META_REFLECTION
/// Member-wise equality for reflectable structs (recurses on members).
template <typename T> [[nodiscard]] bool equal(const T& a, const T& b)
{
if constexpr (Struct<T> && !Leaf<T>) {
bool eq = true;
template for (constexpr auto m : members_v<T>)
{
if (eq && !equal(a.[:m:], b.[:m:])) {
eq = false;
}
}
return eq;
} else {
return a == b;
}
}
/// Lexicographic member-wise ordering for reflectable structs.
template <typename T>
[[nodiscard]] std::strong_ordering compare(const T& a, const T& b)
{
if constexpr (Struct<T> && !Leaf<T>) {
std::strong_ordering result = std::strong_ordering::equal;
template for (constexpr auto m : members_v<T>)
{
if (result == std::strong_ordering::equal) {
result = compare(a.[:m:], b.[:m:]);
}
}
return result;
} else {
return a <=> b;
}
}
/// 64-bit member-folded hash for reflectable structs (recurses).
template <typename T> [[nodiscard]] std::uint64_t hash(const T& v)
{
auto mix = [](std::uint64_t seed, std::uint64_t x) {
x ^= x >> 33;
x *= 0xff51afd7ed558ccdULL;
x ^= x >> 33;
return seed ^
(x + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2));
};
if constexpr (Struct<T> && !Leaf<T>) {
std::uint64_t h = 0xcbf29ce484222325ULL;
template for (constexpr auto m : members_v<T>)
{
h = mix(h, hash(v.[:m:]));
}
return h;
} else {
return static_cast<std::uint64_t>(std::hash<T>{}(v));
}
}
#endif // VOID_HAS_META_REFLECTION
} // namespace Void::Meta