diff --git a/include/ylt/reflection/member_names.hpp b/include/ylt/reflection/member_names.hpp index adbd092be..b88e56750 100644 --- a/include/ylt/reflection/member_names.hpp +++ b/include/ylt/reflection/member_names.hpp @@ -109,7 +109,7 @@ get_member_names() { constexpr auto tp = struct_to_tuple(); std::array arr; -#if __cplusplus >= 202002L || defined(_MSC_VER) +#if __cplusplus >= 202002L [&](std::index_sequence) mutable { ((arr[Is] = internal::get_member_name(tp))>()), @@ -133,7 +133,7 @@ inline constexpr auto get_member_names_map_impl(T& name_arr, template inline constexpr auto get_member_names_map() { constexpr auto name_arr = get_member_names(); -#if __cplusplus >= 202002L || defined(_MSC_VER) +#if __cplusplus >= 202002L return [&](std::index_sequence) mutable { return frozen::unordered_map{ {name_arr[Is], Is}...}; @@ -210,26 +210,15 @@ 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 ylt_alias_struct::get_alias_field_names(); } else { return std::array{}; @@ -254,7 +243,7 @@ get_member_names() { if constexpr (internal::has_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/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index eddee0576..f1b85b1d0 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -187,8 +187,7 @@ inline constexpr std::string_view name_of(T& t, Field& value) { return arr[index]; } -template +template inline constexpr void visit_members_impl0(Visit&& func, std::index_sequence, Args&... args) { @@ -216,7 +215,7 @@ inline constexpr void for_each(T&& t, Visit&& func) { else { if constexpr (std::is_invocable_v) { visit_members(t, [&](auto&... args) { -#if __cplusplus >= 202002L || defined(_MSC_VER) +#if __cplusplus >= 202002L [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is]), ...); diff --git a/include/ylt/reflection/user_reflect_macro.hpp b/include/ylt/reflection/user_reflect_macro.hpp index 75808c956..bd90b3ca2 100644 --- a/include/ylt/reflection/user_reflect_macro.hpp +++ b/include/ylt/reflection/user_reflect_macro.hpp @@ -80,39 +80,41 @@ inline static decltype(auto) refl_object_to_tuple_impl(T &&t) { return std::apply(to_ref, tp); } -#define YLT_REFL_PRIVATE_(STRUCT, ...) \ - namespace ylt::reflection { \ - inline constexpr auto get_private_ptrs(const identity &t); \ - template struct private_visitor; \ - template \ - inline static constexpr decltype(auto) refl_visit_members( \ - STRUCT &t, Visitor &&visitor) { \ - return visit_private_fields(t, std::forward(visitor)); \ - } \ - template \ - inline static constexpr decltype(auto) refl_visit_members( \ - const STRUCT &t, Visitor &&visitor) { \ - return visit_private_fields(t, std::forward(visitor)); \ - } \ - inline static decltype(auto) refl_object_to_tuple(STRUCT &t) { \ - return refl_object_to_tuple_impl(t); \ - } \ - inline static decltype(auto) refl_object_to_tuple(const STRUCT &t) { \ - return refl_object_to_tuple_impl(t); \ - } \ - inline static constexpr std::size_t refl_member_count( \ - const ylt::reflection::identity &t) { \ - return (std::size_t)YLT_ARG_COUNT(__VA_ARGS__); \ - } \ +#define YLT_REFL_PRIVATE_(STRUCT, ...) \ + namespace ylt::reflection { \ + inline constexpr auto get_private_ptrs(const identity &t); \ + template struct private_visitor; \ + } \ + template \ + inline static constexpr decltype(auto) refl_visit_members( \ + STRUCT &t, Visitor &&visitor) { \ + return visit_private_fields(t, std::forward(visitor)); \ + } \ + template \ + inline static constexpr decltype(auto) refl_visit_members( \ + const STRUCT &t, Visitor &&visitor) { \ + return visit_private_fields(t, std::forward(visitor)); \ + } \ + [[maybe_unused]] inline static decltype(auto) refl_object_to_tuple( \ + STRUCT &t) { \ + return refl_object_to_tuple_impl(t); \ + } \ + [[maybe_unused]] inline static decltype(auto) refl_object_to_tuple( \ + const STRUCT &t) { \ + return refl_object_to_tuple_impl(t); \ + } \ + [[maybe_unused]] inline static constexpr std::size_t refl_member_count( \ + const ylt::reflection::identity &t) { \ + return (std::size_t)YLT_ARG_COUNT(__VA_ARGS__); \ } -#define YLT_REFL_PRIVATE(STRUCT, ...) \ - inline static constexpr decltype(auto) refl_member_names( \ - const ylt::reflection::identity &t) { \ - constexpr std::array arr{ \ - WRAP_ARGS(CONCAT_NAME, t, ##__VA_ARGS__)}; \ - return arr; \ - } \ +#define YLT_REFL_PRIVATE(STRUCT, ...) \ + [[maybe_unused]] inline static constexpr decltype(auto) refl_member_names( \ + const ylt::reflection::identity &t) { \ + constexpr std::array arr{ \ + WRAP_ARGS(CONCAT_NAME, t, ##__VA_ARGS__)}; \ + return arr; \ + } \ YLT_REFL_PRIVATE_(STRUCT, WRAP_ARGS(CONCAT_ADDR, STRUCT, ##__VA_ARGS__)) template diff --git a/src/reflection/tests/test_reflection.cpp b/src/reflection/tests/test_reflection.cpp index e5b9b16f9..598732801 100644 --- a/src/reflection/tests/test_reflection.cpp +++ b/src/reflection/tests/test_reflection.cpp @@ -433,13 +433,13 @@ YLT_REFL_PRIVATE(private_struct, a, b); TEST_CASE("test visit private") { const Bank_t bank(1, "ok"); constexpr auto tp = get_private_ptrs(identity{}); - refl_visit_members(bank, [](auto&... args) { + ylt::reflection::visit_members(bank, [](auto&... args) { ((std::cout << args << " "), ...); std::cout << "\n"; }); private_struct st(2, 4); - refl_visit_members(st, [](auto&... args) { + visit_members(st, [](auto&... args) { ((std::cout << args << " "), ...); std::cout << "\n"; }); diff --git a/src/struct_xml/examples/main.cpp b/src/struct_xml/examples/main.cpp index 18c1f57be..dba3a8f5a 100644 --- a/src/struct_xml/examples/main.cpp +++ b/src/struct_xml/examples/main.cpp @@ -296,7 +296,7 @@ struct ylt::reflection::ylt_alias_struct { static constexpr std::string_view get_alias_struct_name() { return "next"; } static constexpr auto get_alias_field_names() { - return std::make_tuple(field_alias_t<0>{"w"}, field_alias_t<1>{"h"}); + return std::array{field_alias_t{"w", 0}, field_alias_t{"h", 1}}; } }; @@ -312,7 +312,7 @@ struct ylt::reflection::ylt_alias_struct { static constexpr std::string_view get_alias_struct_name() { return "qi"; } static constexpr auto get_alias_field_names() { - return std::make_tuple(field_alias_t<0>{"i"}, field_alias_t<1>{"na"}); + return std::array{field_alias_t{"i", 0}, field_alias_t{"na", 1}}; } };