MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Reflection.hpp
Go to the documentation of this file.
1#pragma once
2
3namespace MayaFlux::IO {
4
5// ---------------------------------------------------------------------------
6// Property descriptors
7// ---------------------------------------------------------------------------
8
9/**
10 * @struct Property
11 * @brief Binds a string key to a required member pointer.
12 */
13template <typename Class, typename T>
14struct Property {
15 std::string_view key;
16 T Class::* member;
17 static constexpr bool is_optional = false;
18};
19
20/**
21 * @struct OptionalProperty
22 * @brief Binds a string key to a std::optional<T> member pointer.
23 * Omitted from output when nullopt; missing key on decode leaves nullopt.
24 */
25template <typename Class, typename T>
27 std::string_view key;
28 std::optional<T> Class::* member;
29 static constexpr bool is_optional = true;
30};
31
32// ---------------------------------------------------------------------------
33// Factory helpers
34// ---------------------------------------------------------------------------
35
36template <typename Class, typename T>
37constexpr auto member(std::string_view key, T Class::* ptr)
38{
39 return Property<Class, T> { key, ptr };
40}
41
42template <typename Class, typename T>
43constexpr auto opt_member(std::string_view key, std::optional<T> Class::* ptr)
44{
45 return OptionalProperty<Class, T> { key, ptr };
46}
47
48// ---------------------------------------------------------------------------
49// Concept: a type that exposes a constexpr describe() returning a tuple of
50// Property / OptionalProperty descriptors.
51// ---------------------------------------------------------------------------
52
53template <typename T>
54concept Reflectable = requires {
55 { T::describe() } -> std::same_as<decltype(T::describe())>;
56 requires std::tuple_size_v<decltype(T::describe())> >= 1;
57};
58
59// ---------------------------------------------------------------------------
60// Type-category detection helpers (used by the serializer engine)
61// ---------------------------------------------------------------------------
62
63template <typename T>
64struct is_optional : std::false_type { };
65template <typename T>
66struct is_optional<std::optional<T>> : std::true_type {
67 using inner = T;
68};
69template <typename T>
70inline constexpr bool is_optional_v = is_optional<T>::value;
71
72template <typename T>
73struct is_vector : std::false_type { };
74template <typename T>
75struct is_vector<std::vector<T>> : std::true_type {
76 using element = T;
77};
78template <typename T>
79inline constexpr bool is_vector_v = is_vector<T>::value;
80
81template <typename T>
82struct is_string_map : std::false_type { };
83template <typename V>
84struct is_string_map<std::unordered_map<std::string, V>> : std::true_type {
85 using element = V;
86};
87template <typename V>
88struct is_string_map<std::map<std::string, V>> : std::true_type {
89 using element = V;
90};
91template <typename T>
93
94template <typename T>
95concept GlmSerializable = GlmType<T>;
96
97} // namespace MayaFlux::IO
constexpr bool is_string_map_v
constexpr auto member(std::string_view key, T Class::*ptr)
constexpr auto opt_member(std::string_view key, std::optional< T > Class::*ptr)
constexpr bool is_optional_v
constexpr bool is_vector_v
std::optional< T > Class::* member
Binds a string key to a std::optional<T> member pointer.
std::string_view key
Binds a string key to a required member pointer.