#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if __has_include() #include #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 inline constexpr bool is_optional_v = false; template inline constexpr bool is_optional_v> = true; template inline constexpr bool is_vector_v = false; template inline constexpr bool is_vector_v> = true; template inline constexpr bool is_string_map_v = false; template inline constexpr bool is_string_map_v> = true; template inline constexpr bool is_duration_v = false; template inline constexpr bool is_duration_v> = true; template inline constexpr bool is_time_point_v = false; template inline constexpr bool is_time_point_v> = true; template inline constexpr bool is_variant_v = false; template inline constexpr bool is_variant_v> = true; template inline constexpr bool is_std_array_v = false; template inline constexpr bool is_std_array_v> = 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 inline constexpr bool is_map_v = false; template inline constexpr bool is_map_v> = !std::same_as; template inline constexpr bool is_map_v> = !std::same_as; template concept Leaf = std::same_as || std::is_arithmetic_v || std::is_enum_v || std::same_as; #ifdef VOID_HAS_META_REFLECTION namespace meta = std::meta; // ── member walk (the single reflection of a struct's data members) ──── template 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 [[nodiscard]] consteval std::size_t field_count() { return members_v.size(); } /// A class type with at least one reflectable data member. template concept Struct = (std::is_class_v || std::is_union_v) && (field_count() > 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 [[nodiscard]] std::vector field_names() { std::vector out; out.reserve(field_count()); template for (constexpr auto m : members_v) { 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 void for_each_field(T&& obj, Fn&& fn) { using U = std::remove_reference_t; template for (constexpr auto m : members_v) { 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 [[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 [[nodiscard]] consteval std::optional annotation_of(meta::info m) { for (meta::info a : meta::annotations_of_with_type(m, ^^Tag)) { return meta::extract(a); } return std::nullopt; } /// The (unqualified) name of a type, e.g. type_name() == "Foo". template [[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 requires std::is_enum_v inline constexpr std::size_t enum_count = meta::enumerators_of(^^E).size(); /// The enumerator values of E (declaration order). template requires std::is_enum_v [[nodiscard]] consteval std::array> enum_values() { std::array> 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 requires std::is_enum_v [[nodiscard]] std::array, enum_count> enum_entries() { std::array, enum_count> 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 concept Struct = false; #endif // VOID_HAS_META_REFLECTION // ── repr (diagnostic rendering) ─────────────────────────────────────── // 1. std::formatter → std::format. 2. a .format() method. 3. reflect // the data members as `Type{a: 1, b: 2}`. 4. "". template [[nodiscard]] std::string repr(const T& value); template concept Formattable = std::formattable; template concept HasFormatMethod = requires(const T& v) { { v.format() } -> std::convertible_to; }; #ifdef VOID_HAS_META_REFLECTION template [[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) { if (!first) { out += ", "; } out += meta::identifier_of(m); out += ": "; out += repr(obj.[:m:]); first = false; } out += '}'; return out; } #endif template [[nodiscard]] std::string repr(const T& value) { if constexpr (Formattable) { return std::format("{}", value); } else if constexpr (HasFormatMethod) { return std::string{value.format()}; } #ifdef VOID_HAS_META_REFLECTION else if constexpr (std::is_class_v || std::is_union_v) { return reflect_struct(value); } #endif else { return ""; } } // ── generic algorithms over data members ────────────────────────────── #ifdef VOID_HAS_META_REFLECTION /// Member-wise equality for reflectable structs (recurses on members). template [[nodiscard]] bool equal(const T& a, const T& b) { if constexpr (Struct && !Leaf) { bool eq = true; template for (constexpr auto m : members_v) { if (eq && !equal(a.[:m:], b.[:m:])) { eq = false; } } return eq; } else { return a == b; } } /// Lexicographic member-wise ordering for reflectable structs. template [[nodiscard]] std::strong_ordering compare(const T& a, const T& b) { if constexpr (Struct && !Leaf) { std::strong_ordering result = std::strong_ordering::equal; template for (constexpr auto m : members_v) { 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 [[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 && !Leaf) { std::uint64_t h = 0xcbf29ce484222325ULL; template for (constexpr auto m : members_v) { h = mix(h, hash(v.[:m:])); } return h; } else { return static_cast(std::hash{}(v)); } } #endif // VOID_HAS_META_REFLECTION } // namespace Void::Meta