src/ParameterSchema/ParameterSchema.hpp
src/ParameterSchema/ParameterSchema.hpp
Compile-time parameter schema extraction for reflect-cpp structs. More…
Namespaces
| Name |
|---|
| detail |
Classes
| Name | |
|---|---|
| struct | VariantAlternative Describes one alternative in a tagged-union variant field. |
| struct | ParameterFieldDescriptor |
| struct | ParameterSchema Runtime schema describing all fields in a parameter struct. |
| struct | ParameterUIHints Trait struct for providing custom UI hints for a parameter type. |
| struct | ConstraintInfo Constraint information extracted from a type string. |
| struct | detail::unwrap_optional Unwrap std::optional |
| struct | detail::unwrap_optional< std::optional< T > > |
| struct | detail::is_std_vector Detect std::vector |
| struct | detail::is_std_vector< std::vector< T, A > > |
| struct | detail::vector_element Extract the element type from std::vector |
| struct | detail::vector_element< std::vector< T, A > > |
| struct | detail::is_tagged_union Detect rfl::TaggedUnion<_discriminator, Ts…> via SFINAE. |
| struct | detail::is_tagged_union< T, std::void_t< typename T::VariantType, decltype(T::discrimininator_)> > |
| struct | detail::first_tagged_union_alt |
| struct | detail::first_tagged_union_alt< rfl::TaggedUnion< Disc, First, Rest… > > |
Functions
| Name | |
|---|---|
| std::string | snakeCaseToDisplay(std::string const & snake) Convert snake_case field name to “Title Case” for display. |
| std::string | parseUnderlyingType(std::string const & type_str) |
| bool | isOptionalType(std::string const & type_str) Check if a type string represents an optional field. |
| bool | isVectorType(std::string const & type_str) Check if a type string represents a std::vector field. |
| bool | hasValidator(std::string const & type_str) Check if a type string contains an rfl::Validator. |
| ConstraintInfo | extractConstraints(std::string const & type_str) Extract validator constraint information from a type string. |
| template <typename Params > ParameterSchema |
extractParameterSchema() Extract a ParameterSchema from a reflect-cpp parameter struct. |
Detailed Description
Compile-time parameter schema extraction for reflect-cpp structs.
Provides ParameterFieldDescriptor, ParameterSchema, extractParameterSchema
Functions Documentation
function snakeCaseToDisplay
std::string snakeCaseToDisplay(
std::string const & snake
)Convert snake_case field name to “Title Case” for display.
function parseUnderlyingType
std::string parseUnderlyingType(
std::string const & type_str
)Parse a reflect-cpp type string to determine the underlying type name e.g., “std::optional<rfl::Validator<float, …>>” → “float”
function isOptionalType
bool isOptionalType(
std::string const & type_str
)Check if a type string represents an optional field.
function isVectorType
bool isVectorType(
std::string const & type_str
)Check if a type string represents a std::vector field.
function hasValidator
bool hasValidator(
std::string const & type_str
)Check if a type string contains an rfl::Validator.
function extractConstraints
ConstraintInfo extractConstraints(
std::string const & type_str
)Extract validator constraint information from a type string.
function extractParameterSchema
template <typename Params >
ParameterSchema extractParameterSchema()Extract a ParameterSchema from a reflect-cpp parameter struct.
Template Parameters:
- Params The parameter struct type (must be reflectable by reflect-cpp)
Return: ParameterSchema describing all fields in the struct
Uses rfl::fields
This is typically called once per parameter type at registration time.
Source code
#ifndef NEURALYZER_V2_PARAMETER_SCHEMA_HPP
#define NEURALYZER_V2_PARAMETER_SCHEMA_HPP
#include <rfl.hpp>
#include <rfl/json.hpp>
#include "ParameterSchema/PathFieldKind.hpp"
#include "TimeFrame/TimeFrameIndexReflector.hpp"
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <vector>
// ============================================================================
// ParameterFieldDescriptor — Runtime schema for a single parameter field
// ============================================================================
// Forward declarations to break circular dependency:
// VariantAlternative → ParameterSchema → ParameterFieldDescriptor → VariantAlternative
struct ParameterSchema;
struct VariantAlternative {
std::string tag;
std::unique_ptr<ParameterSchema> schema;
VariantAlternative();
~VariantAlternative();
VariantAlternative(VariantAlternative const & other);
VariantAlternative & operator=(VariantAlternative const & other);
VariantAlternative(VariantAlternative && other) noexcept;
VariantAlternative & operator=(VariantAlternative && other) noexcept;
};
struct ParameterFieldDescriptor {
std::string name;
std::string type_name;
std::string raw_type_name;
std::string display_name;
// Value range (from rfl::Validator constraints or manual UI hints)
std::optional<double> min_value;
std::optional<double> max_value;
bool is_exclusive_min = false;
bool is_exclusive_max = false;
// For enum-like string fields (e.g., {"positive", "negative", "absolute"})
std::vector<std::string> allowed_values;
// Default value as JSON string for type-erased storage
std::optional<std::string> default_value_json;
// UI hints
std::string tooltip;
std::string group;
bool is_advanced = false;
bool is_hidden = false;
int display_order = 0;
bool is_optional = false;
bool is_bound = false;
// Vector support (for std::vector<T> fields)
bool is_vector = false;
std::string vector_element_type;
// Dynamic combo support (for fields populated at runtime, e.g. DataManager keys)
bool dynamic_combo = false;
bool include_none_sentinel = false;
// Path field support (for file/directory browse in AutoParamWidget)
PathFieldKind path_field_kind = PathFieldKind::None;
std::string file_dialog_id;
// Variant support (populated only when type_name == "variant")
bool is_variant = false;
std::string variant_discriminator;
std::vector<VariantAlternative> variant_alternatives;
};
// ============================================================================
// ParameterSchema — Complete schema for a parameter struct
// ============================================================================
struct ParameterSchema {
std::string params_type_name;
std::vector<ParameterFieldDescriptor> fields;
ParameterFieldDescriptor * field(std::string const & name) {
for (auto & f: fields) {
if (f.name == name) return &f;
}
return nullptr;
}
[[nodiscard]] ParameterFieldDescriptor const * field(std::string const & name) const {
for (auto const & f: fields) {
if (f.name == name) return &f;
}
return nullptr;
}
};
// ============================================================================
// ParameterUIHints — Optional override trait for richer UI
// ============================================================================
template<typename Params>
struct ParameterUIHints {
static void annotate(ParameterSchema & /*schema*/) {
// Default: no annotations
}
};
// ============================================================================
// Helper Functions (declared here, defined in ParameterSchema.cpp)
// ============================================================================
std::string snakeCaseToDisplay(std::string const & snake);
std::string parseUnderlyingType(std::string const & type_str);
bool isOptionalType(std::string const & type_str);
bool isVectorType(std::string const & type_str);
bool hasValidator(std::string const & type_str);
struct ConstraintInfo {
std::optional<double> min_value;
std::optional<double> max_value;
bool is_exclusive_min = false;
bool is_exclusive_max = false;
};
ConstraintInfo extractConstraints(std::string const & type_str);
// ============================================================================
// Compile-Time Helpers
// ============================================================================
namespace detail {
template<typename T>
struct unwrap_optional {
using type = T;
};
template<typename T>
struct unwrap_optional<std::optional<T>> {
using type = T;
};
template<typename T>
using unwrap_optional_t = typename unwrap_optional<T>::type;
template<typename T>
struct is_std_vector : std::false_type {};
template<typename T, typename A>
struct is_std_vector<std::vector<T, A>> : std::true_type {};
template<typename T>
inline constexpr bool is_std_vector_v = is_std_vector<T>::value;
template<typename T>
struct vector_element {};
template<typename T, typename A>
struct vector_element<std::vector<T, A>> {
using type = T;
};
template<typename T>
using vector_element_t = typename vector_element<T>::type;
template<typename T, typename = void>
struct is_tagged_union : std::false_type {};
template<typename T>
struct is_tagged_union<T, std::void_t<
typename T::VariantType,
decltype(T::discrimininator_)>> : std::true_type {};
template<typename T>
inline constexpr bool is_tagged_union_v = is_tagged_union<T>::value;
template<typename T>
struct first_tagged_union_alt {};
template<auto Disc, typename First, typename... Rest>
struct first_tagged_union_alt<rfl::TaggedUnion<Disc, First, Rest...>> {
using type = First;
};
template<typename Params>
Params getDefaultInstance() {
if constexpr (is_tagged_union_v<Params>) {
using FirstAlt = typename first_tagged_union_alt<Params>::type;
return Params{FirstAlt{}};
} else {
return Params{};
}
}
}// namespace detail
// Forward declaration for recursive use in detail::makeVariantAlternative
template<typename Params>
ParameterSchema extractParameterSchema();
namespace detail {
template<typename TaggedUnionType, typename AltType>
VariantAlternative makeVariantAlternative(std::string_view disc_str) {
VariantAlternative alt;
// Extract tag name: serialize a TaggedUnion holding a default-constructed AltType
TaggedUnionType tu{AltType{}};
auto json_str = rfl::json::write(tu);
auto parsed = rfl::json::read<rfl::Generic>(json_str);
if (parsed) {
auto const * obj = std::get_if<rfl::Generic::Object>(&parsed.value().get());
if (obj) {
auto disc_val = obj->get(std::string(disc_str));
if (disc_val) {
if (auto const * s = std::get_if<std::string>(&disc_val.value().get())) {
alt.tag = *s;
}
}
}
}
// Recursively extract sub-schema for the alternative struct
*alt.schema = extractParameterSchema<AltType>();
return alt;
}
template<rfl::internal::StringLiteral _disc, typename... Ts>
void processTaggedUnion(
rfl::TaggedUnion<_disc, Ts...> const * /*type_tag*/,
ParameterFieldDescriptor & desc) {
using TU = rfl::TaggedUnion<_disc, Ts...>;
constexpr auto disc_sv = _disc.string_view();
desc.is_variant = true;
desc.type_name = "variant";
desc.variant_discriminator = std::string(disc_sv);
desc.variant_alternatives.clear();
desc.allowed_values.clear();
// Fold expression: process each alternative type
(([&] {
auto alt = makeVariantAlternative<TU, Ts>(disc_sv);
desc.allowed_values.push_back(alt.tag);
desc.variant_alternatives.push_back(std::move(alt));
}()),
...);
}
}// namespace detail
// ============================================================================
// Compile-Time Schema Extraction
// ============================================================================
template<typename Params>
ParameterSchema extractParameterSchema() {
ParameterSchema schema;
schema.params_type_name = typeid(Params).name();
auto meta_fields = rfl::fields<Params>();
// Default-construct a Params instance and serialize to JSON to extract defaults.
// TaggedUnion has no default constructor; use first alternative.
auto const defaults_instance = detail::getDefaultInstance<Params>();
auto const defaults_json = rfl::json::write(defaults_instance);
auto const defaults_parsed = rfl::json::read<rfl::Generic>(defaults_json);
rfl::Generic::Object const * defaults_obj = nullptr;
if (defaults_parsed) {
defaults_obj = std::get_if<rfl::Generic::Object>(&defaults_parsed.value().get());
}
int order = 0;
for (auto const & mf: meta_fields) {
ParameterFieldDescriptor desc;
desc.name = mf.name();
desc.raw_type_name = mf.type();
desc.type_name = parseUnderlyingType(mf.type());
desc.display_name = snakeCaseToDisplay(mf.name());
desc.is_optional = isOptionalType(mf.type());
desc.display_order = order++;
// Extract validator constraints if present
if (hasValidator(mf.type())) {
auto constraints = extractConstraints(mf.type());
desc.min_value = constraints.min_value;
desc.max_value = constraints.max_value;
desc.is_exclusive_min = constraints.is_exclusive_min;
desc.is_exclusive_max = constraints.is_exclusive_max;
}
// Extract default value from the default-constructed instance
if (defaults_obj) {
auto field_val = defaults_obj->get(desc.name);
if (field_val) {
desc.default_value_json = rfl::json::write(field_val.value());
}
}
schema.fields.push_back(std::move(desc));
}
// Enum detection pass: rfl::fields() only provides type strings, but we
// need actual C++ types to use std::is_enum_v and rfl::get_enumerator_array.
// rfl::to_view() gives typed references to each field.
auto view = rfl::to_view(defaults_instance);
size_t field_idx = 0;
view.apply([&](auto const & field) {
// rfl::to_view returns pointer fields, so field.value() is T*
// defaults_instance is const, so pointers are const T* — strip cv.
using PtrType = std::remove_cvref_t<decltype(field.value())>;
using RawFieldType = std::remove_cv_t<std::remove_pointer_t<PtrType>>;
using InnerType = detail::unwrap_optional_t<RawFieldType>;
if constexpr (detail::is_tagged_union_v<InnerType>) {
if (field_idx < schema.fields.size()) {
detail::processTaggedUnion(
static_cast<InnerType const *>(nullptr),
schema.fields[field_idx]);
}
} else if constexpr (detail::is_std_vector_v<InnerType>) {
if (field_idx < schema.fields.size()) {
auto & fd = schema.fields[field_idx];
fd.is_vector = true;
// vector_element_type is the element's base type name.
// parseUnderlyingType already returned "vector<...>" for type_name.
// Extract just the element part.
using ElemType = detail::vector_element_t<InnerType>;
if constexpr (std::is_same_v<ElemType, float>) {
fd.vector_element_type = "float";
} else if constexpr (std::is_same_v<ElemType, double>) {
fd.vector_element_type = "double";
} else if constexpr (std::is_same_v<ElemType, int>) {
fd.vector_element_type = "int";
} else if constexpr (std::is_same_v<ElemType, bool>) {
fd.vector_element_type = "bool";
} else if constexpr (std::is_same_v<ElemType, std::string>) {
fd.vector_element_type = "std::string";
} else if constexpr (std::is_integral_v<ElemType>) {
fd.vector_element_type = "int";
} else if constexpr (std::is_floating_point_v<ElemType>) {
fd.vector_element_type = "float";
} else {
fd.vector_element_type = "unknown";
}
}
} else if constexpr (std::is_enum_v<InnerType>) {
if (field_idx < schema.fields.size()) {
auto & fd = schema.fields[field_idx];
fd.type_name = "enum";
fd.allowed_values.clear();
constexpr auto enumerators = rfl::get_enumerator_array<InnerType>();
for (auto const & [name, val]: enumerators) {
fd.allowed_values.emplace_back(std::string(name));
}
}
}
++field_idx;
});
// Apply optional UI hints from specializations
ParameterUIHints<Params>::annotate(schema);
return schema;
}
#endif// NEURALYZER_V2_PARAMETER_SCHEMA_HPPUpdated on 2026-11-13 at 20:25:44 +0000