19#if defined( DAW_JSON_HAS_REFLECTION )
22#include <daw/daw_bind_args_at.h>
23#include <daw/daw_concepts.h>
24#include <daw/daw_move.h>
25#include <daw/daw_pipelines.h>
36 struct reflect_all_t {};
38 template<EnumType E, json_options_t Options = json_custom_opts_def>
41 struct refl_ignored_base {
42 consteval refl_ignored_base( ) =
default;
45 struct refl_annotation_base {
46 consteval refl_annotation_base( ) =
default;
49 struct refl_map_as : refl_annotation_base {
52 explicit consteval refl_map_as( std::meta::info i )
56 struct refl_enum_string : refl_annotation_base {
59 explicit consteval refl_enum_string( json_options_t opts )
63 struct refl_rename : refl_annotation_base {
66 explicit consteval refl_rename(
char const *Name )
70 template<
typename T, T... Vals>
71 inline constexpr std::array<T,
sizeof...( Vals )> constant_fixed_array = {
74 template<std::ranges::input_range R>
75 requires( std::is_constructible_v<std::ranges::range_value_t<R>,
76 std::ranges::range_reference_t<R>> )
77 consteval auto as_stdarray( R &&elems ) {
79 auto args = std::vector{ ^^std::ranges::range_value_t<R> };
80 for(
const auto &V : elems ) {
81 args.push_back( reflect_constant( V ) );
83 return substitute( ^^constant_fixed_array, args );
86 consteval std::vector<std::meta::info>
87 annotations_of_with_base_type( std::meta::info item, std::meta::info type ) {
88 auto result = std::vector<std::meta::info>{ };
89 for(
auto annotation : annotations_of( item ) ) {
90 if( is_base_of_type( type, type_of( annotation ) ) ) {
91 result.push_back( annotation );
96#if defined( __clang__ )
97 consteval std::vector<std::meta::info>
98 annotations_of_with_type( std::meta::info item, std::meta::info type ) {
99 auto result = std::vector<std::meta::info>{ };
100 for(
auto annot : annotations_of( item ) ) {
101 if( type_of( annot ) == type ) {
102 result.push_back( annot );
109 template<
typename AnnotationType,
typename T>
110 consteval bool has_annotation( ) {
111 return not annotations_of_with_base_type( ^^T, ^^AnnotationType ).empty( );
115 consteval std::vector<std::meta::info> get_reflectible_members( ) {
116 using namespace daw::pipelines;
117 auto result = std::vector<std::meta::info>{ };
118 if( not annotations_of_with_base_type( ^^T, ^^reflect_all_t ).empty( ) ) {
119 return all_nsdm_of( ^^T );
121 return pub_nsdm_of( ^^T );
125 consteval std::vector<std::meta::info>
126 get_non_ignored_reflectible_members( ) {
127 using namespace daw::pipelines;
128 auto result = std::vector<std::meta::info>{ };
129 auto const members = get_reflectible_members<T>( );
130 for(
auto const member : members ) {
131 if( annotations_of_with_base_type( member, ^^refl_ignored_base )
133 result.push_back( member );
140 constexpr auto to_tuple( T
const &value ) {
141 static constexpr auto
142 members = [:as_stdarray( get_non_ignored_reflectible_members<T>( ) ):];
148 return [&]<std::size_t... Is>( std::index_sequence<Is...> ) {
149 return daw::forward_nonrvalue_as_tuple( value.[:members[Is]:]... );
150 }( std::make_index_sequence<members.size( )>{ } );
154 using to_tuple_t = DAW_TYPEOF( to_tuple( std::declval<T>( ) ) );
156 template<std::meta::info members_i>
157 consteval auto make_arg_indexes( ) {
158 static constexpr auto members = [:members_i:];
159 auto r = std::array<std::size_t, members.size( )>{ };
160 for(
auto const [index, member] :
daw::pipelines::Enumerate( members ) ) {
161 if( not annotations_of_with_base_type( member, ^^refl_ignored_base )
163 r[index] = daw::max_value<std::size_t>;
165 r[index] =
static_cast<std::size_t
>(
166 std::count_if( r.data( ),
167 r.data( ) +
static_cast<std::ptrdiff_t
>( index ),
168 []( std::size_t s ) {
169 return s != daw::max_value<std::size_t>;
176 template<
typename T,
typename... Fns>
178 static constexpr T operator( )(
auto &&fns,
auto &&...args ) {
179 auto const &[... member_fns] = fns;
180 return T{ member_fns( DAW_FWD( args )... )... };
185 template<
typename result_t, std::
size_t index>
187 static constexpr result_t operator( )(
auto &&...arguments ) {
188 return static_cast<result_t
>( DAW_FWD( arguments...[index] ) );
192 template<
typename result_t, std::meta::info annotation>
193 struct DefaultReturn {
194 static constexpr result_t operator( )(
auto &&... ) {
195 static constexpr auto const ignored_default = [:constant_of(
197 return static_cast<result_t
>( ignored_default );
201 template<std::meta::info members_i, std::meta::info arg_indexes_i,
202 typename... Args,
typename C>
203 consteval auto fn_maker( C ) {
204 static constexpr auto members = [:members_i:];
205 static constexpr auto index = C::value;
206 static constexpr auto member = members[index];
207 static constexpr auto annotations = [:as_stdarray(
208 annotations_of_with_base_type(
209 member, ^^refl_ignored_base ) ):];
210 using result_t = [:type_of( member ):];
211 if constexpr( annotations.empty( ) ) {
212 static constexpr auto arg_indexes = [:arg_indexes_i:];
213 static constexpr auto i = arg_indexes[index];
214 static_assert( i != daw::max_value<std::size_t> );
215 static constexpr auto fn = ArgReturn<result_t, i>{ };
218 static_assert( annotations.size( ) == 1 );
219 static constexpr std::meta::info annotation = annotations.front( );
220 static constexpr auto fn = DefaultReturn<result_t, annotation>{ };
225 template<std::meta::info members_i, std::meta::info arg_indexes_i,
226 typename... Args, std::size_t... Is>
227 consteval auto make_member_fns( std::index_sequence<Is...> ) {
228 return std::tuple{ fn_maker<members_i, arg_indexes_i, Args...>(
229 std::integral_constant<std::size_t, Is>{ } )... };
233 struct reflected_constructor {
234 static constexpr auto members_b = [:as_stdarray(
235 get_reflectible_members<T>( ) ):];
236 static constexpr auto arg_indexes = make_arg_indexes<^^members_b>( );
237 using result_t = std::remove_cvref_t<T>;
239 template<
typename... Args>
240 static constexpr result_t operator( )( Args &&...args ) {
241 static constexpr auto const member_fns =
242 make_member_fns<^^members_b, ^^arg_indexes, Args...>(
243 std::make_index_sequence<members_b.size( )>{ } );
244 return construct_t<result_t>{ }( member_fns, DAW_FWD( args )... );
248 template<
typename,
typename...>
249 inline constexpr bool construction_test_v =
false;
251 template<
typename T,
typename... Ts>
252 inline constexpr bool construction_test_v<T, std::tuple<Ts...>> =
253 requires( Ts... ts ) {
254 reflected_constructor<T>{ }( ts... );
258 concept Reflectable =
259 not std::is_empty_v<T> and std::is_class_v<T> and
requires( T v ) {
262 and construction_test_v<T, to_tuple_t<T>>;
265 template<
typename AnnotationType, std::meta::info r>
266 consteval std::optional<AnnotationType> get_annotation( ) {
267 auto annotations = annotations_of_with_type( r, ^^AnnotationType );
268 if( annotations.empty( ) ) {
271 return extract<AnnotationType>( annotations.front( ) );
274 template<auto member_info, json_name name>
275 consteval std::optional<refl_map_as> get_map_as_annotation( ) {
276 static constexpr auto refl_map_as_annot =
277 get_annotation<refl_map_as, member_info>( );
279 static constexpr auto refl_enum_string_annot =
280 get_annotation<refl_enum_string, member_info>( );
282 if constexpr( refl_map_as_annot ) {
283 static_assert( not refl_enum_string_annot,
284 "Do not use reflect.enum_string and reflect.map_as "
285 "at the same time" );
286 return refl_map_as_annot;
287 }
else if constexpr( refl_enum_string_annot ) {
288 using json_member_no_name = enum_string<
289 typename[:type_of( member_info ):], refl_enum_string_annot->Options>;
290 static constexpr auto info =
291 ^^
typename json_member_no_name::template with_name<name>;
292 return refl_map_as{ info };
298 template<JSONNAMETYPE Name,
typename T>
299 using deduce_t =
typename json_details::ensure_mapped_t<
300 json_details::json_deduced_type<T>>::template with_name<Name>;
302 template<auto member_info>
303 consteval std::meta::info get_member_link_func( ) {
304 static constexpr auto annot_rename =
305 get_annotation<refl_rename, member_info>( );
307 static constexpr std::string_view svname =
308 annot_rename ? std::string_view( annot_rename->name )
309 : identifier_of( member_info );
310 static constexpr auto name = json_name<svname.size( ) + 1>(
311 svname.data( ), std::make_index_sequence<svname.size( ) + 1>{ } );
312 static_assert( not name.empty( ),
"Unexpected empty name" );
314 static constexpr auto annot_map_as =
315 get_map_as_annotation<member_info, name>( );
317 if constexpr( annot_map_as ) {
320 "Do not use reflect.rename and reflect.map_as at the same time" );
321 return annot_map_as->type;
323 return ^^deduce_t<name,
typename[:type_of( member_info ):]>;
327 template<
typename T, std::
size_t Idx>
328 using get_member_link_t =
typename
329 [:get_member_link_func<get_non_ignored_reflectible_members<T>( )[Idx]>( ):];
std::uint32_t json_options_t
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.