diff --git a/iguana/ylt/reflection/member_count.hpp b/iguana/ylt/reflection/member_count.hpp index e323c7ee..04d12a42 100644 --- a/iguana/ylt/reflection/member_count.hpp +++ b/iguana/ylt/reflection/member_count.hpp @@ -11,7 +11,10 @@ #endif #include "user_reflect_macro.hpp" - +namespace struct_pack { +template +struct compatible; +} namespace ylt::reflection { template using remove_cvref_t = std::remove_cv_t>; @@ -47,7 +50,7 @@ constexpr bool expected = expected_impl::value; #if __cpp_concepts >= 201907L template -concept optional = requires(Type optional) { +concept optional = !expected && requires(Type optional) { optional.value(); optional.has_value(); optional.operator*(); @@ -87,14 +90,12 @@ template constexpr bool tuple_size = tuple_size_impl::value; #endif -template -struct compatible; - template constexpr inline bool is_compatible_v = false; template -constexpr inline bool is_compatible_v> = true; +constexpr inline bool is_compatible_v> = + true; struct UniversalVectorType { template diff --git a/iguana/ylt/reflection/member_names.hpp b/iguana/ylt/reflection/member_names.hpp index 72c94168..e4a8c7bb 100644 --- a/iguana/ylt/reflection/member_names.hpp +++ b/iguana/ylt/reflection/member_names.hpp @@ -7,9 +7,6 @@ namespace ylt::reflection { -template -struct ylt_alias_struct; - template inline constexpr auto get_alias_field_names(); @@ -64,32 +61,54 @@ struct member_tratis { }; template -struct has_alias_field_names_t : std::false_type {}; +struct has_alias_struct_name_t : std::false_type {}; template -struct has_alias_field_names_t< - T, std::void_t::get_alias_field_names())>> +struct has_alias_struct_name_t< + T, std::void_t> + : std::true_type {}; + +template +struct has_inner_alias_struct_name_t : std::false_type {}; + +template +struct has_inner_alias_struct_name_t< + T, std::void_t> : std::true_type {}; template -inline constexpr bool has_alias_field_names_v = - has_alias_field_names_t::value; +constexpr bool has_alias_struct_name_v = has_alias_struct_name_t::value; + +template +constexpr bool has_inner_alias_struct_name_v = + has_inner_alias_struct_name_t::value; template -struct has_alias_struct_names_t : std::false_type {}; +struct has_alias_field_names_t : std::false_type {}; template -struct has_alias_struct_names_t< - T, std::void_t::get_alias_struct_name())>> +struct has_alias_field_names_t< + T, std::void_t> + : std::true_type {}; + +template +struct has_inner_alias_field_names_t : std::false_type {}; + +template +struct has_inner_alias_field_names_t< + T, std::void_t> : std::true_type {}; template -inline constexpr bool has_alias_struct_name_v = - has_alias_struct_names_t::value; +constexpr bool has_alias_field_names_v = has_alias_field_names_t::value; + +template +constexpr bool has_inner_alias_field_names_v = + has_inner_alias_field_names_t::value; template -inline constexpr void init_arr_with_tuple(const T& tp, U& arr, - std::index_sequence) { +inline constexpr void init_arr_with_tuple(U& arr, std::index_sequence) { + constexpr auto tp = struct_to_tuple(); ((arr[Is] = internal::get_member_name(tp))>()), ...); } @@ -106,10 +125,9 @@ get_member_names() { return type::refl_member_names(ylt::reflection::identity{}); } else { - constexpr auto tp = struct_to_tuple(); - std::array arr; #if __cplusplus >= 202002L + constexpr auto tp = struct_to_tuple(); [&](std::index_sequence) mutable { ((arr[Is] = internal::get_member_name(tp))>()), @@ -117,7 +135,7 @@ get_member_names() { } (std::make_index_sequence{}); #else - init_arr_with_tuple(tp, arr, std::make_index_sequence{}); + init_arr_with_tuple(arr, std::make_index_sequence{}); #endif return arr; } @@ -210,26 +228,18 @@ inline constexpr size_t index_of() { return names.size(); } -template struct field_alias_t { std::string_view alias_name; - inline static constexpr auto index = Idx; + size_t index; }; -template -inline constexpr auto get_alias_field_names_impl(Tuple& tp, - std::index_sequence) { - return std::array, sizeof...(Is)>{ - std::make_pair(std::tuple_element_t>::index, - std::get(tp).alias_name)...}; -} - template inline constexpr auto get_alias_field_names() { if constexpr (internal::has_alias_field_names_v) { - constexpr auto tp = ylt_alias_struct::get_alias_field_names(); - return get_alias_field_names_impl( - tp, std::make_index_sequence>{}); + return get_alias_field_names((T*)nullptr); + } + else if constexpr (internal::has_inner_alias_field_names_v) { + return T::get_alias_field_names((T*)nullptr); } else { return std::array{}; @@ -239,7 +249,10 @@ inline constexpr auto get_alias_field_names() { template constexpr std::string_view get_struct_name() { if constexpr (internal::has_alias_struct_name_v) { - return ylt_alias_struct::get_alias_struct_name(); + return get_alias_struct_name((T*)nullptr); + } + else if constexpr (internal::has_inner_alias_struct_name_v) { + return T::get_alias_struct_name((T*)nullptr); } else { return type_string(); @@ -251,10 +264,11 @@ inline constexpr std::array> get_member_names() { auto arr = internal::get_member_names(); using U = ylt::reflection::remove_cvref_t; - if constexpr (internal::has_alias_field_names_v) { + if constexpr (internal::has_alias_field_names_v || + internal::has_inner_alias_field_names_v) { constexpr auto alias_arr = get_alias_field_names(); for (size_t i = 0; i < alias_arr.size(); i++) { - arr[alias_arr[i].first] = alias_arr[i].second; + arr[alias_arr[i].index] = alias_arr[i].alias_name; } } return arr; diff --git a/iguana/ylt/reflection/member_ptr.hpp b/iguana/ylt/reflection/member_ptr.hpp index fec004ec..1fa090e9 100644 --- a/iguana/ylt/reflection/member_ptr.hpp +++ b/iguana/ylt/reflection/member_ptr.hpp @@ -3,7 +3,7 @@ // modified based on: // https://github.com/getml/reflect-cpp/blob/main/include/rfl/internal/bind_fake_object_to_tuple.hpp -// thanks for alxn4's greate idea! +// thanks for alxn4's great idea! namespace ylt::reflection { namespace internal { diff --git a/test/test_cpp20.cpp b/test/test_cpp20.cpp index 1d915371..0e8e2e58 100644 --- a/test/test_cpp20.cpp +++ b/test/test_cpp20.cpp @@ -15,17 +15,16 @@ YLT_REFL(point_t, x, y); struct point_t1 { int x; int y; -}; -YLT_REFL(point_t1, x, y); - -template <> -struct ylt::reflection::ylt_alias_struct { - static constexpr std::string_view get_alias_struct_name() { return "point"; } - static constexpr auto get_alias_field_names() { - return std::make_tuple(field_alias_t<0>{"X"}, field_alias_t<1>{"Y"}); + static constexpr auto get_alias_field_names(point_t1*) { + return std::array{field_alias_t{"X", 0}, field_alias_t{"Y", 1}}; + } + static constexpr std::string_view get_alias_struct_name(point_t1*) { + return "point"; } }; +YLT_REFL(point_t1, x, y); + struct test_variant { test_variant() = default; test_variant(int a, std::variant b, double c) @@ -253,8 +252,8 @@ TEST_CASE("test xml") { constexpr auto names = ylt::reflection::get_member_names(); constexpr auto st_name = ylt::reflection::get_struct_name(); CHECK(names == std::array{"X", "Y"}); - CHECK(alias_names[0].second == "X"); - CHECK(alias_names[1].second == "Y"); + CHECK(alias_names[0].alias_name == "X"); + CHECK(alias_names[1].alias_name == "Y"); CHECK(st_name == "point"); constexpr auto name1 = ylt::reflection::get_struct_name(); diff --git a/test/test_xml.cpp b/test/test_xml.cpp index cdc63833..25be2322 100644 --- a/test/test_xml.cpp +++ b/test/test_xml.cpp @@ -734,14 +734,14 @@ struct next_obj_t { }; YLT_REFL(next_obj_t, x, y); -template <> -struct ylt::reflection::ylt_alias_struct { - static constexpr std::string_view get_alias_struct_name() { return "next"; } +inline constexpr std::string_view get_alias_struct_name(next_obj_t *) { + return "next"; +} - static constexpr auto get_alias_field_names() { - return std::make_tuple(field_alias_t<0>{"w"}, field_alias_t<1>{"h"}); - } -}; +inline constexpr auto get_alias_field_names(next_obj_t *) { + return std::array{ylt::reflection::field_alias_t{"w", 0}, + ylt::reflection::field_alias_t{"h", 1}}; +} struct out_object { std::unique_ptr id; @@ -750,14 +750,14 @@ struct out_object { YLT_REFL(out_object, id, name, obj); }; -template <> -struct ylt::reflection::ylt_alias_struct { - static constexpr std::string_view get_alias_struct_name() { return "qi"; } +inline constexpr std::string_view get_alias_struct_name(out_object *) { + return "qi"; +} - static constexpr auto get_alias_field_names() { - return std::make_tuple(field_alias_t<0>{"w"}, field_alias_t<1>{"h"}); - } -}; +inline constexpr auto get_alias_field_names(out_object *) { + return std::array{ylt::reflection::field_alias_t{"w", 0}, + ylt::reflection::field_alias_t{"h", 1}}; +} TEST_CASE("test alias") { out_object m{std::make_unique(20), "tom", {21, 42}};