Skip to content

Commit

Permalink
[reflection][feat]Reflect with macro (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jul 20, 2024
1 parent d026688 commit 152f879
Show file tree
Hide file tree
Showing 11 changed files with 628 additions and 44 deletions.
11 changes: 11 additions & 0 deletions include/ylt/reflection/internal/arg_list_macro.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#pragma once
#include "common_macro.hpp"

#define WRAP_ARGS0(w, o)
#define WRAP_ARGS1(w, o, _1) w(o, _1)
#include "generate/arg_list_macro_gen.hpp"

#define WRAP_ARGS_(w, object, ...) \
YLT_CONCAT(WRAP_ARGS, YLT_ARG_COUNT(__VA_ARGS__)) \
(w, object, __VA_ARGS__)
#define WRAP_ARGS(w, object, ...) WRAP_ARGS_(w, object, __VA_ARGS__)
26 changes: 26 additions & 0 deletions include/ylt/reflection/internal/args_count.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#define YLT_MACRO_EXPAND(...) __VA_ARGS__

#define YLT_ARG_COUNT(...) \
YLT_MACRO_EXPAND(YLT_ARG_COUNT_IMPL( \
0, ##__VA_ARGS__, 124, 123, 122, 121, 120, 119, 118, 117, 116, 115, 114, \
113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103, 102, 101, 100, \
99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83, 82, \
81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65, 64, \
63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, \
45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, \
27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, \
9, 8, 7, 6, 5, 4, 3, 2, 1, 0))

#define YLT_ARG_COUNT_IMPL( \
_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, \
_17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, _30, _31, \
_32, _33, _34, _35, _36, _37, _38, _39, _40, _41, _42, _43, _44, _45, _46, \
_47, _48, _49, _50, _51, _52, _53, _54, _55, _56, _57, _58, _59, _60, _61, \
_62, _63, _64, _65, _66, _67, _68, _69, _70, _71, _72, _73, _74, _75, _76, \
_77, _78, _79, _80, _81, _82, _83, _84, _85, _86, _87, _88, _89, _90, _91, \
_92, _93, _94, _95, _96, _97, _98, _99, _100, _101, _102, _103, _104, \
_105, _106, _107, _108, _109, _110, _111, _112, _113, _114, _115, _116, \
_117, _118, _119, _120, _121, _122, _123, _124, N, ...) \
N
16 changes: 16 additions & 0 deletions include/ylt/reflection/internal/common_macro.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "args_count.hpp"
#define YLT_CONCAT_(l, r) l##r

#define YLT_CONCAT(l, r) YLT_CONCAT_(l, r)

#define CONCAT_MEMBER(t, x) t.x

#define CONCAT_ADDR(T, x) &T::x

#define CONCAT_NAME(t, x) #x

namespace ylt::reflection {
template <typename T>
struct identity {};
} // namespace ylt::reflection
136 changes: 136 additions & 0 deletions include/ylt/reflection/internal/generate/arg_list_macro_gen.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*The following boilerplate code was generated using a Python script:
macro = "#define WRAP_ARGS"
with open("generated_args.txt", "w", encoding="utf-8") as codefile:
codefile.write(
"\n".join(
[
f"{macro}{i}(w,o,_1, ...) w(o, _1),
WRAP_ARGS{i-1}(w,o,__VA_ARGS__)" for i in range(2, 126)
]
)
)
*/
#define WRAP_ARGS2(w, o, _1, ...) w(o, _1), WRAP_ARGS1(w, o, __VA_ARGS__)
#define WRAP_ARGS3(w, o, _1, ...) w(o, _1), WRAP_ARGS2(w, o, __VA_ARGS__)
#define WRAP_ARGS4(w, o, _1, ...) w(o, _1), WRAP_ARGS3(w, o, __VA_ARGS__)
#define WRAP_ARGS5(w, o, _1, ...) w(o, _1), WRAP_ARGS4(w, o, __VA_ARGS__)
#define WRAP_ARGS6(w, o, _1, ...) w(o, _1), WRAP_ARGS5(w, o, __VA_ARGS__)
#define WRAP_ARGS7(w, o, _1, ...) w(o, _1), WRAP_ARGS6(w, o, __VA_ARGS__)
#define WRAP_ARGS8(w, o, _1, ...) w(o, _1), WRAP_ARGS7(w, o, __VA_ARGS__)
#define WRAP_ARGS9(w, o, _1, ...) w(o, _1), WRAP_ARGS8(w, o, __VA_ARGS__)
#define WRAP_ARGS10(w, o, _1, ...) w(o, _1), WRAP_ARGS9(w, o, __VA_ARGS__)
#define WRAP_ARGS11(w, o, _1, ...) w(o, _1), WRAP_ARGS10(w, o, __VA_ARGS__)
#define WRAP_ARGS12(w, o, _1, ...) w(o, _1), WRAP_ARGS11(w, o, __VA_ARGS__)
#define WRAP_ARGS13(w, o, _1, ...) w(o, _1), WRAP_ARGS12(w, o, __VA_ARGS__)
#define WRAP_ARGS14(w, o, _1, ...) w(o, _1), WRAP_ARGS13(w, o, __VA_ARGS__)
#define WRAP_ARGS15(w, o, _1, ...) w(o, _1), WRAP_ARGS14(w, o, __VA_ARGS__)
#define WRAP_ARGS16(w, o, _1, ...) w(o, _1), WRAP_ARGS15(w, o, __VA_ARGS__)
#define WRAP_ARGS17(w, o, _1, ...) w(o, _1), WRAP_ARGS16(w, o, __VA_ARGS__)
#define WRAP_ARGS18(w, o, _1, ...) w(o, _1), WRAP_ARGS17(w, o, __VA_ARGS__)
#define WRAP_ARGS19(w, o, _1, ...) w(o, _1), WRAP_ARGS18(w, o, __VA_ARGS__)
#define WRAP_ARGS20(w, o, _1, ...) w(o, _1), WRAP_ARGS19(w, o, __VA_ARGS__)
#define WRAP_ARGS21(w, o, _1, ...) w(o, _1), WRAP_ARGS20(w, o, __VA_ARGS__)
#define WRAP_ARGS22(w, o, _1, ...) w(o, _1), WRAP_ARGS21(w, o, __VA_ARGS__)
#define WRAP_ARGS23(w, o, _1, ...) w(o, _1), WRAP_ARGS22(w, o, __VA_ARGS__)
#define WRAP_ARGS24(w, o, _1, ...) w(o, _1), WRAP_ARGS23(w, o, __VA_ARGS__)
#define WRAP_ARGS25(w, o, _1, ...) w(o, _1), WRAP_ARGS24(w, o, __VA_ARGS__)
#define WRAP_ARGS26(w, o, _1, ...) w(o, _1), WRAP_ARGS25(w, o, __VA_ARGS__)
#define WRAP_ARGS27(w, o, _1, ...) w(o, _1), WRAP_ARGS26(w, o, __VA_ARGS__)
#define WRAP_ARGS28(w, o, _1, ...) w(o, _1), WRAP_ARGS27(w, o, __VA_ARGS__)
#define WRAP_ARGS29(w, o, _1, ...) w(o, _1), WRAP_ARGS28(w, o, __VA_ARGS__)
#define WRAP_ARGS30(w, o, _1, ...) w(o, _1), WRAP_ARGS29(w, o, __VA_ARGS__)
#define WRAP_ARGS31(w, o, _1, ...) w(o, _1), WRAP_ARGS30(w, o, __VA_ARGS__)
#define WRAP_ARGS32(w, o, _1, ...) w(o, _1), WRAP_ARGS31(w, o, __VA_ARGS__)
#define WRAP_ARGS33(w, o, _1, ...) w(o, _1), WRAP_ARGS32(w, o, __VA_ARGS__)
#define WRAP_ARGS34(w, o, _1, ...) w(o, _1), WRAP_ARGS33(w, o, __VA_ARGS__)
#define WRAP_ARGS35(w, o, _1, ...) w(o, _1), WRAP_ARGS34(w, o, __VA_ARGS__)
#define WRAP_ARGS36(w, o, _1, ...) w(o, _1), WRAP_ARGS35(w, o, __VA_ARGS__)
#define WRAP_ARGS37(w, o, _1, ...) w(o, _1), WRAP_ARGS36(w, o, __VA_ARGS__)
#define WRAP_ARGS38(w, o, _1, ...) w(o, _1), WRAP_ARGS37(w, o, __VA_ARGS__)
#define WRAP_ARGS39(w, o, _1, ...) w(o, _1), WRAP_ARGS38(w, o, __VA_ARGS__)
#define WRAP_ARGS40(w, o, _1, ...) w(o, _1), WRAP_ARGS39(w, o, __VA_ARGS__)
#define WRAP_ARGS41(w, o, _1, ...) w(o, _1), WRAP_ARGS40(w, o, __VA_ARGS__)
#define WRAP_ARGS42(w, o, _1, ...) w(o, _1), WRAP_ARGS41(w, o, __VA_ARGS__)
#define WRAP_ARGS43(w, o, _1, ...) w(o, _1), WRAP_ARGS42(w, o, __VA_ARGS__)
#define WRAP_ARGS44(w, o, _1, ...) w(o, _1), WRAP_ARGS43(w, o, __VA_ARGS__)
#define WRAP_ARGS45(w, o, _1, ...) w(o, _1), WRAP_ARGS44(w, o, __VA_ARGS__)
#define WRAP_ARGS46(w, o, _1, ...) w(o, _1), WRAP_ARGS45(w, o, __VA_ARGS__)
#define WRAP_ARGS47(w, o, _1, ...) w(o, _1), WRAP_ARGS46(w, o, __VA_ARGS__)
#define WRAP_ARGS48(w, o, _1, ...) w(o, _1), WRAP_ARGS47(w, o, __VA_ARGS__)
#define WRAP_ARGS49(w, o, _1, ...) w(o, _1), WRAP_ARGS48(w, o, __VA_ARGS__)
#define WRAP_ARGS50(w, o, _1, ...) w(o, _1), WRAP_ARGS49(w, o, __VA_ARGS__)
#define WRAP_ARGS51(w, o, _1, ...) w(o, _1), WRAP_ARGS50(w, o, __VA_ARGS__)
#define WRAP_ARGS52(w, o, _1, ...) w(o, _1), WRAP_ARGS51(w, o, __VA_ARGS__)
#define WRAP_ARGS53(w, o, _1, ...) w(o, _1), WRAP_ARGS52(w, o, __VA_ARGS__)
#define WRAP_ARGS54(w, o, _1, ...) w(o, _1), WRAP_ARGS53(w, o, __VA_ARGS__)
#define WRAP_ARGS55(w, o, _1, ...) w(o, _1), WRAP_ARGS54(w, o, __VA_ARGS__)
#define WRAP_ARGS56(w, o, _1, ...) w(o, _1), WRAP_ARGS55(w, o, __VA_ARGS__)
#define WRAP_ARGS57(w, o, _1, ...) w(o, _1), WRAP_ARGS56(w, o, __VA_ARGS__)
#define WRAP_ARGS58(w, o, _1, ...) w(o, _1), WRAP_ARGS57(w, o, __VA_ARGS__)
#define WRAP_ARGS59(w, o, _1, ...) w(o, _1), WRAP_ARGS58(w, o, __VA_ARGS__)
#define WRAP_ARGS60(w, o, _1, ...) w(o, _1), WRAP_ARGS59(w, o, __VA_ARGS__)
#define WRAP_ARGS61(w, o, _1, ...) w(o, _1), WRAP_ARGS60(w, o, __VA_ARGS__)
#define WRAP_ARGS62(w, o, _1, ...) w(o, _1), WRAP_ARGS61(w, o, __VA_ARGS__)
#define WRAP_ARGS63(w, o, _1, ...) w(o, _1), WRAP_ARGS62(w, o, __VA_ARGS__)
#define WRAP_ARGS64(w, o, _1, ...) w(o, _1), WRAP_ARGS63(w, o, __VA_ARGS__)
#define WRAP_ARGS65(w, o, _1, ...) w(o, _1), WRAP_ARGS64(w, o, __VA_ARGS__)
#define WRAP_ARGS66(w, o, _1, ...) w(o, _1), WRAP_ARGS65(w, o, __VA_ARGS__)
#define WRAP_ARGS67(w, o, _1, ...) w(o, _1), WRAP_ARGS66(w, o, __VA_ARGS__)
#define WRAP_ARGS68(w, o, _1, ...) w(o, _1), WRAP_ARGS67(w, o, __VA_ARGS__)
#define WRAP_ARGS69(w, o, _1, ...) w(o, _1), WRAP_ARGS68(w, o, __VA_ARGS__)
#define WRAP_ARGS70(w, o, _1, ...) w(o, _1), WRAP_ARGS69(w, o, __VA_ARGS__)
#define WRAP_ARGS71(w, o, _1, ...) w(o, _1), WRAP_ARGS70(w, o, __VA_ARGS__)
#define WRAP_ARGS72(w, o, _1, ...) w(o, _1), WRAP_ARGS71(w, o, __VA_ARGS__)
#define WRAP_ARGS73(w, o, _1, ...) w(o, _1), WRAP_ARGS72(w, o, __VA_ARGS__)
#define WRAP_ARGS74(w, o, _1, ...) w(o, _1), WRAP_ARGS73(w, o, __VA_ARGS__)
#define WRAP_ARGS75(w, o, _1, ...) w(o, _1), WRAP_ARGS74(w, o, __VA_ARGS__)
#define WRAP_ARGS76(w, o, _1, ...) w(o, _1), WRAP_ARGS75(w, o, __VA_ARGS__)
#define WRAP_ARGS77(w, o, _1, ...) w(o, _1), WRAP_ARGS76(w, o, __VA_ARGS__)
#define WRAP_ARGS78(w, o, _1, ...) w(o, _1), WRAP_ARGS77(w, o, __VA_ARGS__)
#define WRAP_ARGS79(w, o, _1, ...) w(o, _1), WRAP_ARGS78(w, o, __VA_ARGS__)
#define WRAP_ARGS80(w, o, _1, ...) w(o, _1), WRAP_ARGS79(w, o, __VA_ARGS__)
#define WRAP_ARGS81(w, o, _1, ...) w(o, _1), WRAP_ARGS80(w, o, __VA_ARGS__)
#define WRAP_ARGS82(w, o, _1, ...) w(o, _1), WRAP_ARGS81(w, o, __VA_ARGS__)
#define WRAP_ARGS83(w, o, _1, ...) w(o, _1), WRAP_ARGS82(w, o, __VA_ARGS__)
#define WRAP_ARGS84(w, o, _1, ...) w(o, _1), WRAP_ARGS83(w, o, __VA_ARGS__)
#define WRAP_ARGS85(w, o, _1, ...) w(o, _1), WRAP_ARGS84(w, o, __VA_ARGS__)
#define WRAP_ARGS86(w, o, _1, ...) w(o, _1), WRAP_ARGS85(w, o, __VA_ARGS__)
#define WRAP_ARGS87(w, o, _1, ...) w(o, _1), WRAP_ARGS86(w, o, __VA_ARGS__)
#define WRAP_ARGS88(w, o, _1, ...) w(o, _1), WRAP_ARGS87(w, o, __VA_ARGS__)
#define WRAP_ARGS89(w, o, _1, ...) w(o, _1), WRAP_ARGS88(w, o, __VA_ARGS__)
#define WRAP_ARGS90(w, o, _1, ...) w(o, _1), WRAP_ARGS89(w, o, __VA_ARGS__)
#define WRAP_ARGS91(w, o, _1, ...) w(o, _1), WRAP_ARGS90(w, o, __VA_ARGS__)
#define WRAP_ARGS92(w, o, _1, ...) w(o, _1), WRAP_ARGS91(w, o, __VA_ARGS__)
#define WRAP_ARGS93(w, o, _1, ...) w(o, _1), WRAP_ARGS92(w, o, __VA_ARGS__)
#define WRAP_ARGS94(w, o, _1, ...) w(o, _1), WRAP_ARGS93(w, o, __VA_ARGS__)
#define WRAP_ARGS95(w, o, _1, ...) w(o, _1), WRAP_ARGS94(w, o, __VA_ARGS__)
#define WRAP_ARGS96(w, o, _1, ...) w(o, _1), WRAP_ARGS95(w, o, __VA_ARGS__)
#define WRAP_ARGS97(w, o, _1, ...) w(o, _1), WRAP_ARGS96(w, o, __VA_ARGS__)
#define WRAP_ARGS98(w, o, _1, ...) w(o, _1), WRAP_ARGS97(w, o, __VA_ARGS__)
#define WRAP_ARGS99(w, o, _1, ...) w(o, _1), WRAP_ARGS98(w, o, __VA_ARGS__)
#define WRAP_ARGS100(w, o, _1, ...) w(o, _1), WRAP_ARGS99(w, o, __VA_ARGS__)
#define WRAP_ARGS101(w, o, _1, ...) w(o, _1), WRAP_ARGS100(w, o, __VA_ARGS__)
#define WRAP_ARGS102(w, o, _1, ...) w(o, _1), WRAP_ARGS101(w, o, __VA_ARGS__)
#define WRAP_ARGS103(w, o, _1, ...) w(o, _1), WRAP_ARGS102(w, o, __VA_ARGS__)
#define WRAP_ARGS104(w, o, _1, ...) w(o, _1), WRAP_ARGS103(w, o, __VA_ARGS__)
#define WRAP_ARGS105(w, o, _1, ...) w(o, _1), WRAP_ARGS104(w, o, __VA_ARGS__)
#define WRAP_ARGS106(w, o, _1, ...) w(o, _1), WRAP_ARGS105(w, o, __VA_ARGS__)
#define WRAP_ARGS107(w, o, _1, ...) w(o, _1), WRAP_ARGS106(w, o, __VA_ARGS__)
#define WRAP_ARGS108(w, o, _1, ...) w(o, _1), WRAP_ARGS107(w, o, __VA_ARGS__)
#define WRAP_ARGS109(w, o, _1, ...) w(o, _1), WRAP_ARGS108(w, o, __VA_ARGS__)
#define WRAP_ARGS110(w, o, _1, ...) w(o, _1), WRAP_ARGS109(w, o, __VA_ARGS__)
#define WRAP_ARGS111(w, o, _1, ...) w(o, _1), WRAP_ARGS110(w, o, __VA_ARGS__)
#define WRAP_ARGS112(w, o, _1, ...) w(o, _1), WRAP_ARGS111(w, o, __VA_ARGS__)
#define WRAP_ARGS113(w, o, _1, ...) w(o, _1), WRAP_ARGS112(w, o, __VA_ARGS__)
#define WRAP_ARGS114(w, o, _1, ...) w(o, _1), WRAP_ARGS113(w, o, __VA_ARGS__)
#define WRAP_ARGS115(w, o, _1, ...) w(o, _1), WRAP_ARGS114(w, o, __VA_ARGS__)
#define WRAP_ARGS116(w, o, _1, ...) w(o, _1), WRAP_ARGS115(w, o, __VA_ARGS__)
#define WRAP_ARGS117(w, o, _1, ...) w(o, _1), WRAP_ARGS116(w, o, __VA_ARGS__)
#define WRAP_ARGS118(w, o, _1, ...) w(o, _1), WRAP_ARGS117(w, o, __VA_ARGS__)
#define WRAP_ARGS119(w, o, _1, ...) w(o, _1), WRAP_ARGS118(w, o, __VA_ARGS__)
#define WRAP_ARGS120(w, o, _1, ...) w(o, _1), WRAP_ARGS119(w, o, __VA_ARGS__)
#define WRAP_ARGS121(w, o, _1, ...) w(o, _1), WRAP_ARGS120(w, o, __VA_ARGS__)
#define WRAP_ARGS122(w, o, _1, ...) w(o, _1), WRAP_ARGS121(w, o, __VA_ARGS__)
#define WRAP_ARGS123(w, o, _1, ...) w(o, _1), WRAP_ARGS122(w, o, __VA_ARGS__)
#define WRAP_ARGS124(w, o, _1, ...) w(o, _1), WRAP_ARGS123(w, o, __VA_ARGS__)
#define WRAP_ARGS125(w, o, _1, ...) w(o, _1), WRAP_ARGS124(w, o, __VA_ARGS__)
10 changes: 9 additions & 1 deletion include/ylt/reflection/member_count.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <vector>
#include <ylt/util/expected.hpp>

#include "user_reflect_macro.hpp"

namespace ylt::reflection {
template <typename T>
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
Expand Down Expand Up @@ -161,7 +163,13 @@ inline constexpr std::size_t members_count_impl() {
template <typename T>
inline constexpr std::size_t members_count() {
using type = remove_cvref_t<T>;
if constexpr (internal::tuple_size<type>) {
if constexpr (is_out_ylt_refl_v<type>) {
return refl_member_count(ylt::reflection::identity<type>{});
}
else if constexpr (is_inner_ylt_refl_v<type>) {
return type::refl_member_count(ylt::reflection::identity<type>{});
}
else if constexpr (internal::tuple_size<type>) {
return std::tuple_size<type>::value;
}
else {
Expand Down
37 changes: 23 additions & 14 deletions include/ylt/reflection/member_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
#include <string_view>
#include <variant>

#include "template_string.hpp"
#if (__has_include(<concepts>) || defined(__clang__) || defined(_MSC_VER)) || \
(defined(__GNUC__) && __GNUC__ > 10)
#include "member_ptr.hpp"
#include "template_string.hpp"

namespace ylt::reflection {

Expand Down Expand Up @@ -54,15 +52,25 @@ template <typename T>
inline constexpr std::array<std::string_view, members_count_v<T>>
get_member_names() {
constexpr size_t Count = members_count_v<T>;
constexpr auto tp = struct_to_tuple<T>();

std::array<std::string_view, Count> arr;
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
((arr[Is] = internal::get_member_name<internal::wrap(std::get<Is>(tp))>()),
...);
using type = remove_cvref_t<T>;
if constexpr (is_out_ylt_refl_v<type>) {
return refl_member_names(ylt::reflection::identity<type>{});
}
else if constexpr (is_inner_ylt_refl_v<type>) {
return type::refl_member_names(ylt::reflection::identity<type>{});
}
else {
constexpr auto tp = struct_to_tuple<T>();

std::array<std::string_view, Count> arr;
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
((arr[Is] =
internal::get_member_name<internal::wrap(std::get<Is>(tp))>()),
...);
}
(std::make_index_sequence<Count>{});
return arr;
}
(std::make_index_sequence<Count>{});
return arr;
}

template <typename T>
Expand Down Expand Up @@ -101,7 +109,7 @@ inline constexpr auto tuple_to_variant(std::tuple<Args...>) {

template <typename T>
using struct_variant_t = decltype(tuple_to_variant(
std::declval<decltype(struct_to_tuple<std::remove_cvref_t<T>>())>));
std::declval<decltype(struct_to_tuple<remove_cvref_t<T>>())>));

template <typename T>
constexpr auto member_names_map = internal::get_member_names_map<T>();
Expand Down Expand Up @@ -137,10 +145,12 @@ inline constexpr size_t index_of(std::string_view name) {
return arr.size();
}

#if __has_include(<concetps>)
template <typename T, FixedString name>
inline constexpr size_t index_of() {
return index_of<T>(name.str());
}
#endif

template <typename T, size_t index>
inline constexpr std::string_view name_of() {
Expand All @@ -160,7 +170,7 @@ inline constexpr std::string_view name_of(size_t index) {
}

template <typename T, typename Visit>
inline constexpr void for_each(Visit func) {
inline constexpr void for_each(Visit&& func) {
constexpr auto& arr = member_names<T>;
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
if constexpr (std::is_invocable_v<Visit, std::string_view, size_t>) {
Expand All @@ -180,4 +190,3 @@ inline constexpr void for_each(Visit func) {
}

} // namespace ylt::reflection
#endif
24 changes: 21 additions & 3 deletions include/ylt/reflection/member_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,30 @@ inline constexpr auto struct_to_tuple() {

template <class T>
inline constexpr auto object_to_tuple(T& t) {
return internal::tuple_view(t);
using type = remove_cvref_t<T>;
if constexpr (is_out_ylt_refl_v<type>) {
return refl_object_to_tuple(t);
}
else if constexpr (is_inner_ylt_refl_v<type>) {
return type::refl_object_to_tuple(t);
}
else {
return internal::tuple_view(t);
}
}

template <class T, typename Visitor, size_t Count = members_count_v<T>>
inline constexpr decltype(auto) visit_members(T&& t, Visitor&& visitor) {
return internal::tuple_view<Count>(std::forward<T>(t),
std::forward<Visitor>(visitor));
using type = remove_cvref_t<T>;
if constexpr (is_out_ylt_refl_v<type>) {
return refl_visit_members(t, visitor);
}
else if constexpr (is_inner_ylt_refl_v<type>) {
return type::refl_object_to_tuple(t, visitor);
}
else {
return internal::tuple_view<Count>(std::forward<T>(t),
std::forward<Visitor>(visitor));
}
}
} // namespace ylt::reflection
15 changes: 5 additions & 10 deletions include/ylt/reflection/member_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
#include <iterator>
#include <variant>

#include "member_names.hpp"
#include "template_string.hpp"
#include "template_switch.hpp"

#if (__has_include(<concepts>) || defined(__clang__) || defined(_MSC_VER)) || \
(defined(__GNUC__) && __GNUC__ > 10)
#include "member_names.hpp"

namespace ylt::reflection {

namespace internal {
Expand Down Expand Up @@ -118,11 +115,13 @@ inline constexpr auto& get(T& t) {
return std::get<index>(ref_tp);
}

#if __has_include(<concetps>)
template <FixedString name, typename T>
inline constexpr auto& get(T& t) {
constexpr size_t index = index_of<T, name>();
return get<index>(t);
}
#endif

template <typename T, typename Field>
inline size_t index_of(T& t, Field& value) {
Expand All @@ -146,7 +145,6 @@ inline constexpr std::string_view name_of(T& t, Field& value) {

return arr[index];
}
#endif

template <typename T, typename Visit>
inline constexpr void for_each(T&& t, Visit&& func) {
Expand All @@ -158,8 +156,6 @@ inline constexpr void for_each(T&& t, Visit&& func) {
});
}
else {
#if __has_include(<concepts>) || defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 10)
constexpr auto arr = member_names<T>;
if constexpr (std::is_invocable_v<Visit, first_t, std::string_view>) {
visit_members(t, [&](auto&... args) {
Expand All @@ -184,14 +180,13 @@ inline constexpr void for_each(T&& t, Visit&& func) {
"std::string_view, size_t], at least has field_value and "
"make sure keep the order of arguments");
}
#endif
}
}

} // namespace ylt::reflection

#if __has_include(<concepts>) || defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 10)
#if (defined(__GNUC__) && __GNUC__ > 10) || \
((defined(__clang__) || defined(_MSC_VER)) && __has_include(<concepts>))
template <ylt::reflection::FixedString s>
inline constexpr auto operator""_ylts() {
return s;
Expand Down
Loading

0 comments on commit 152f879

Please sign in to comment.