#pragma once #include #include #include #include #include #include #include #include #include #include #include #if __has_include() #include #define VOID_HAS_CONFIG_REFLECTION 1 #endif // ── Reflection config binding // ───────────────────────────────────────────────── Fill an aggregate struct // from a Config using P2996 reflection: each data member binds to the dotted // key matching its name (under an optional prefix); nested aggregates extend // the prefix. The default-initialized member values double as the schema's // defaults — a missing key keeps the default, a present key that fails // conversion is an error. // // struct LogSettings { // std::string level = "info"; // bool async = true; // std::chrono::milliseconds flush_every{250}; // }; // auto s = Void::Configuration::bind(cfg, "log"); // // Supported member types: everything Config::get_as accepts (string, // bool, arithmetic, enums, chrono durations), std::optional, // std::vector (keys "name.0", "name.1", ...), nested aggregates. namespace Void::Configuration { // ── field annotations ───────────────────────────────────────────────── // Annotate aggregate members to control binding: // struct Settings { // [[= Configuration::Key{"log_level"}]] std::string level = "info"; // [[= Configuration::Required{}]] std::string host; // }; struct Key { Void::Meta::Str name; }; struct Required { }; struct Secret { }; struct RestartRequired { }; struct Deprecated { Void::Meta::Str message; }; struct IntRange { std::int64_t min{0}; std::int64_t max{0}; }; struct RealRange { double min{0.0}; double max{0.0}; }; #ifdef VOID_HAS_CONFIG_REFLECTION namespace BindDetail { // Leaf / container shape detection comes from Void::Meta (single // source); Config additionally treats chrono durations as leaves. template concept Leaf = std::is_same_v || std::is_same_v || std::is_arithmetic_v || std::is_enum_v || Void::Meta::is_duration_v; template concept BindableAggregate = std::is_aggregate_v && !std::is_array_v && std::is_default_constructible_v; [[nodiscard]] inline std::string join(std::string_view prefix, std::string_view name) { if (prefix.empty()) { return std::string{name}; } return std::string{prefix} + "." + std::string{name}; } template Void::Result::Result bind_into(const Config& cfg, std::string_view prefix) { T out{}; std::optional failure; template for (constexpr auto m : Void::Meta::members_v) { if (!failure) { using MT = [:std::meta::type_of(m):]; // Key segment: a [[= Configuration::Key("…")]] override, // else the field identifier. constexpr auto ko = Void::Meta::annotation_of(m); const std::string seg = ko.has_value() ? std::string{ko->name.view()} : std::string{std::meta::identifier_of(m)}; const auto key = join(prefix, seg); [[maybe_unused]] constexpr bool required = Void::Meta::has_annotation(m); if constexpr (BindableAggregate && !Leaf) { auto nested = bind_into(cfg, key); if (!nested.has_value()) { failure = std::move(nested).error(); } else { out.[:m:] = std::move(*nested); } } else if constexpr (Void::Meta::is_optional_v) { using U = typename MT::value_type; if (cfg.contains(key)) { auto v = cfg.get_as(key); if (!v.has_value()) { failure = std::move(v).error(); } else { out.[:m:] = std::move(*v); } } } else if constexpr (Void::Meta::is_vector_v) { using U = typename MT::value_type; MT items; for (std::size_t i = 0;; ++i) { const auto item_key = key + "." + std::to_string(i); if (!cfg.contains(item_key)) { break; } auto v = cfg.get_as(item_key); if (!v.has_value()) { failure = std::move(v).error(); break; } items.push_back(std::move(*v)); } if (!failure && !items.empty()) { out.[:m:] = std::move(items); } } else { static_assert(Leaf, "member type not bindable from Config"); if (cfg.contains(key)) { auto v = cfg.get_as(key); if (!v.has_value()) { failure = std::move(v).error(); } else { out.[:m:] = std::move(*v); } } else if constexpr (required) { failure = Void::Result::Error{ "missing required config key '" + key + "'"}; } // absent + not required: keep the default member value } } } if (failure) { return Void::Result::fail(std::move(*failure)); } return Void::Result::ok(std::move(out)); } } // namespace BindDetail /// Bind cfg's keys under `prefix` into a default-constructed T. template [[nodiscard]] Void::Result::Result bind(const Config& cfg, std::string_view prefix = {}) { return BindDetail::bind_into(cfg, prefix); } #endif // VOID_HAS_CONFIG_REFLECTION } // namespace Void::Configuration