1#ifndef TATOOINE_REFLECTION_H
2#define TATOOINE_REFLECTION_H
20 static constexpr const bool value =
false;
43 return num_members<T>();
47template <reflectable T,
typename V>
53template <std::
size_t idx, reflectable T>
54constexpr auto get(T &&t) {
56 std::integral_constant<std::size_t, idx>{}, std::forward<T>(t));
60template <std::
size_t idx, reflectable T>
63 std::integral_constant<std::size_t, idx>{});
66template <std::
size_t idx,
typename T>
67constexpr auto name(T &&) ->
decltype(name<idx, T>()) {
68 return name<idx, T>();
72template <reflectable T>
77template <reflectable T>
82template <std::
size_t I,
typename T>
84 static_assert(is_reflectable<T>);
86 std::integral_constant<std::size_t, I>{}, std::declval<T>()));
89template <std::
size_t I,
typename T>
92#define TATOOINE_REFLECTION_INSERT_MEMBER(MEMBER) \
93 MEMBER TATOOINE_PP_COMMA() MEMBER
95#define TATOOINE_REFLECTION_INSERT_GETTER(GETTER) \
96 GETTER TATOOINE_PP_COMMA() GETTER()
98#define TATOOINE_REFLECTION_INSERT_METHOD(NAME, ACCESSOR) \
99 NAME TATOOINE_PP_COMMA() ACCESSOR
101#define TATOOINE_REFLECTION_NAME(NAME, ACCESSOR) NAME
103#define TATOOINE_REFLECTION_ACCESSOR(NAME, ACCESSOR) ACCESSOR
105#define TATOOINE_REFLECTION_MEMBER_HELPER(NAME, ACCESSOR) v(#NAME, t.ACCESSOR);
107#define TATOOINE_REFLECTION_MAKE_ACCESSOR(NAME, ACCESSOR) \
108 template <reflectable Reflectable> \
109 static constexpr auto get( \
110 index_t<static_cast<std::size_t>(field_indices::NAME)>, Reflectable &&t) \
115#define TATOOINE_REFLECTION_MAKE_NAME_GETTER(NAME, ACCESSOR) \
116 static constexpr auto name( \
117 index_t<static_cast<std::size_t>(field_indices::NAME)>) \
118 ->std::string_view { \
122#define TATOOINE_REFLECTION_MAKE_GETTERS(NAME, ACCESSOR) \
123 TATOOINE_REFLECTION_MAKE_ACCESSOR(NAME, ACCESSOR) \
124 TATOOINE_REFLECTION_MAKE_NAME_GETTER(NAME, ACCESSOR)
126#define TATOOINE_REFLECTION_FIELD_INDICES(...) \
127 enum class field_indices : std::size_t { __VA_ARGS__, count___ }; \
130#define TATOOINE_MAKE_SIMPLE_TYPE_REFLECTABLE(TYPE, ...) \
131 TATOOINE_MAKE_ADT_REFLECTABLE( \
132 TYPE, TATOOINE_PP_MAP(TATOOINE_REFLECTION_INSERT_MEMBER, __VA_ARGS__))
134#define TATOOINE_MAKE_ADT_REFLECTABLE(TYPE, ...) \
136 TATOOINE_MAKE_TEMPLATED_ADT_REFLECTABLE(TYPE, __VA_ARGS__)
138#define TATOOINE_MAKE_TEMPLATED_ADT_REFLECTABLE(TYPE, ...) \
139 struct reflector<TATOOINE_PP_PASS_ARGS(TYPE)> { \
140 template <std::size_t I> \
141 using index_t = std::integral_constant<std::size_t, I>; \
142 using reflected_type = TATOOINE_PP_PASS_ARGS(TYPE); \
143 static constexpr const bool value = true; \
145 TATOOINE_REFLECTION_FIELD_INDICES(TATOOINE_PP_ODDS(__VA_ARGS__)) \
146 static constexpr auto num_members() { \
147 return static_cast<std::size_t>(field_indices::count___); \
149 static constexpr auto name() -> std::string_view { \
150 return TATOOINE_PP_TO_STRING(TYPE); \
153 TATOOINE_PP_MAP2(TATOOINE_REFLECTION_MAKE_GETTERS, __VA_ARGS__) \
155 template <reflectable Reflectable, typename V> \
156 constexpr static auto for_each([[maybe_unused]] V && v, \
157 [[maybe_unused]] Reflectable &&t) -> void { \
158 for_each(std::forward<V>(v), std::forward<Reflectable>(t), \
159 std::make_index_sequence<num_members()>{}); \
161 template <reflectable Reflectable, typename V, std::size_t... Is> \
162 constexpr static auto for_each([[maybe_unused]] V && v, \
163 [[maybe_unused]] Reflectable &&t, \
164 std::index_sequence<Is...>) -> void { \
165 (v(name(index_t<Is>{}), get(index_t<Is>{}, t)), ...); \
Definition: reflection.h:31
Definition: linspace.h:172
constexpr auto for_each(T &&t, V &&v)
Iterate over each registered member.
Definition: reflection.h:48
constexpr auto num_members() -> std::size_t
Return number of fields in a reflector struct.
Definition: reflection.h:37
static constexpr auto is_reflectable
Definition: reflection.h:28
typename get_type_impl< I, T >::type get_type
Definition: reflection.h:90
constexpr auto name()
Definition: reflection.h:61
typename get_impl< Container, I >::type get
Definition: get.h:11
Definition: reflection.h:83
decltype(reflector< std::decay_t< T > >::get(std::integral_constant< std::size_t, I >{}, std::declval< T >())) type
Definition: reflection.h:86
Definition: reflection.h:24
Definition: reflection.h:19
static constexpr const bool value
Definition: reflection.h:20