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>
352 lines
11 KiB
C++
352 lines
11 KiB
C++
#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
|