From 736651c70c1438c55fbefa1c527badd4b9f652f1 Mon Sep 17 00:00:00 2001 From: raver <1434271517@qq.com> Date: Thu, 10 Oct 2024 16:37:15 +0800 Subject: [PATCH 1/8] [struct_json] is compatible with msvc versions --- include/ylt/reflection/member_names.hpp | 37 +++++++++++++++---------- include/ylt/reflection/member_value.hpp | 36 +++++++++++++++++------- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/include/ylt/reflection/member_names.hpp b/include/ylt/reflection/member_names.hpp index cf53e5b3a..ad63aa896 100644 --- a/include/ylt/reflection/member_names.hpp +++ b/include/ylt/reflection/member_names.hpp @@ -127,13 +127,25 @@ get_member_names() { else { std::array arr; #if __cplusplus >= 202002L +#if defined(_MSC_VER) +#if _MSC_VER >= 1930 constexpr auto tp = struct_to_tuple(); [&](std::index_sequence) mutable { ((arr[Is] = internal::get_member_name(tp))>()), ...); - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); +#else + init_arr_with_tuple(arr, std::make_index_sequence{}); +#endif +#else + constexpr auto tp = struct_to_tuple(); + [&](std::index_sequence) mutable { + ((arr[Is] = + internal::get_member_name(tp))>()), + ...); + }(std::make_index_sequence{}); +#endif #else init_arr_with_tuple(arr, std::make_index_sequence{}); #endif @@ -155,8 +167,7 @@ inline constexpr auto get_member_names_map() { return [&](std::index_sequence) mutable { return frozen::unordered_map{ {name_arr[Is], Is}...}; - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); #else return get_member_names_map_impl(name_arr, std::make_index_sequence{}); @@ -177,15 +188,14 @@ inline const auto& get_member_offset_arr(T&& t) { auto tp = ylt::reflection::object_to_tuple(std::forward(t)); #if __cplusplus >= 202002L - [[maybe_unused]] static std::array arr = {[&]( - std::index_sequence) mutable {std::array arr; - ((arr[Is] = size_t((const char*)&std::get(tp) - (char*)(&t))), ...); - return arr; -} -(std::make_index_sequence{}) -}; // namespace internal + [[maybe_unused]] static std::array arr = { + [&](std::index_sequence) mutable { + std::array arr; + ((arr[Is] = size_t((const char*)&std::get(tp) - (char*)(&t))), ...); + return arr; + }(std::make_index_sequence{})}; // namespace internal -return arr; + return arr; #else [[maybe_unused]] static std::array arr = get_member_offset_arr_impl(t, tp, std::make_index_sequence{}); @@ -357,8 +367,7 @@ inline constexpr void for_each(Visit&& func) { "size_t], at least has std::string_view and make sure keep " "the order of arguments"); } - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); #else for_each_impl(std::forward(func), arr, std::make_index_sequence{}); diff --git a/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index dbdafbabe..7ceb82de5 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -216,15 +216,23 @@ inline constexpr void for_each(T&& t, Visit&& func) { if constexpr (std::is_invocable_v) { visit_members(t, [&](auto&... args) { #if __cplusplus >= 202002L +#if defined(_MSC_VER) +#if _MSC_VER >= 1930 [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is]), ...); - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); #else - visit_members_impl0(std::forward(func), - std::make_index_sequence{}, - args...); + visit_members_impl0(std::forward(func), std::make_index_sequence{}, args...); +#endif +#else + [&](std::index_sequence) mutable { + constexpr auto arr = get_member_names(); + (func(args, arr[Is]), ...); + }(std::make_index_sequence{}); +#endif +#else + visit_members_impl0(std::forward(func), std::make_index_sequence{}, args...); #endif }); } @@ -232,15 +240,23 @@ inline constexpr void for_each(T&& t, Visit&& func) { size_t>) { visit_members(t, [&](auto&... args) { #if __cplusplus >= 202002L +#if defined(_MSC_VER) +#if _MSC_VER >= 1930 [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is], Is), ...); - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); +#else + visit_members_impl(std::forward(func), std::make_index_sequence{}, args...); +#endif +#else + [&](std::index_sequence) mutable { + constexpr auto arr = get_member_names(); + (func(args, arr[Is], Is), ...); + }(std::make_index_sequence{}); +#endif #else - visit_members_impl(std::forward(func), - std::make_index_sequence{}, - args...); + visit_members_impl(std::forward(func), std::make_index_sequence{}, args...); #endif }); } From 02cb00a9f0992e3b426593fb910af4ccb6c813f4 Mon Sep 17 00:00:00 2001 From: raver <1434271517@qq.com> Date: Thu, 10 Oct 2024 17:00:57 +0800 Subject: [PATCH 2/8] Simplified conditional logic for C++20 and MSVC compatibility - Merged conditional checks for C++20 support and MSVC version. - Reduced redundancy by combining similar code paths. - Used a unified approach to handle both C++20 compliant compilers and fallback for older MSVC versions. - Ensured that the implementation uses fold expressions and generic lambdas when available, and falls back to visit_members_impl for older environments. --- include/ylt/reflection/member_names.hpp | 15 +------------- include/ylt/reflection/member_value.hpp | 26 ++----------------------- 2 files changed, 3 insertions(+), 38 deletions(-) diff --git a/include/ylt/reflection/member_names.hpp b/include/ylt/reflection/member_names.hpp index ad63aa896..1cbcb6718 100644 --- a/include/ylt/reflection/member_names.hpp +++ b/include/ylt/reflection/member_names.hpp @@ -126,26 +126,13 @@ get_member_names() { } else { std::array arr; -#if __cplusplus >= 202002L -#if defined(_MSC_VER) -#if _MSC_VER >= 1930 +#if __cplusplus >= 202002L && (!defined(_MSC_VER) || _MSC_VER >= 1930) constexpr auto tp = struct_to_tuple(); [&](std::index_sequence) mutable { ((arr[Is] = internal::get_member_name(tp))>()), ...); }(std::make_index_sequence{}); -#else - init_arr_with_tuple(arr, std::make_index_sequence{}); -#endif -#else - constexpr auto tp = struct_to_tuple(); - [&](std::index_sequence) mutable { - ((arr[Is] = - internal::get_member_name(tp))>()), - ...); - }(std::make_index_sequence{}); -#endif #else init_arr_with_tuple(arr, std::make_index_sequence{}); #endif diff --git a/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index 7ceb82de5..4d9577f19 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -215,22 +215,11 @@ inline constexpr void for_each(T&& t, Visit&& func) { else { if constexpr (std::is_invocable_v) { visit_members(t, [&](auto&... args) { -#if __cplusplus >= 202002L -#if defined(_MSC_VER) -#if _MSC_VER >= 1930 +#if __cplusplus >= 202002L && (!defined(_MSC_VER) || _MSC_VER >= 1930) [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is]), ...); }(std::make_index_sequence{}); -#else - visit_members_impl0(std::forward(func), std::make_index_sequence{}, args...); -#endif -#else - [&](std::index_sequence) mutable { - constexpr auto arr = get_member_names(); - (func(args, arr[Is]), ...); - }(std::make_index_sequence{}); -#endif #else visit_members_impl0(std::forward(func), std::make_index_sequence{}, args...); #endif @@ -239,24 +228,13 @@ inline constexpr void for_each(T&& t, Visit&& func) { else if constexpr (std::is_invocable_v) { visit_members(t, [&](auto&... args) { -#if __cplusplus >= 202002L -#if defined(_MSC_VER) -#if _MSC_VER >= 1930 +#if __cplusplus >= 202002L && (!defined(_MSC_VER) || _MSC_VER >= 1930) [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is], Is), ...); }(std::make_index_sequence{}); #else visit_members_impl(std::forward(func), std::make_index_sequence{}, args...); -#endif -#else - [&](std::index_sequence) mutable { - constexpr auto arr = get_member_names(); - (func(args, arr[Is], Is), ...); - }(std::make_index_sequence{}); -#endif -#else - visit_members_impl(std::forward(func), std::make_index_sequence{}, args...); #endif }); } From 253834875994f1c8060f2dd5cdbe34c1f2b653cd Mon Sep 17 00:00:00 2001 From: lilingyun <1434271517@qq.com> Date: Thu, 10 Oct 2024 17:24:21 +0800 Subject: [PATCH 3/8] Code format adjustment --- include/ylt/reflection/member_names.hpp | 24 ++++++++++++++---------- include/ylt/reflection/member_value.hpp | 6 ++++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/ylt/reflection/member_names.hpp b/include/ylt/reflection/member_names.hpp index 1cbcb6718..285705922 100644 --- a/include/ylt/reflection/member_names.hpp +++ b/include/ylt/reflection/member_names.hpp @@ -132,7 +132,8 @@ get_member_names() { ((arr[Is] = internal::get_member_name(tp))>()), ...); - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else init_arr_with_tuple(arr, std::make_index_sequence{}); #endif @@ -154,7 +155,8 @@ inline constexpr auto get_member_names_map() { return [&](std::index_sequence) mutable { return frozen::unordered_map{ {name_arr[Is], Is}...}; - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else return get_member_names_map_impl(name_arr, std::make_index_sequence{}); @@ -175,14 +177,15 @@ inline const auto& get_member_offset_arr(T&& t) { auto tp = ylt::reflection::object_to_tuple(std::forward(t)); #if __cplusplus >= 202002L - [[maybe_unused]] static std::array arr = { - [&](std::index_sequence) mutable { - std::array arr; - ((arr[Is] = size_t((const char*)&std::get(tp) - (char*)(&t))), ...); - return arr; - }(std::make_index_sequence{})}; // namespace internal - + [[maybe_unused]] static std::array arr = {[&]( + std::index_sequence) mutable {std::array arr; + ((arr[Is] = size_t((const char*)&std::get(tp) - (char*)(&t))), ...); return arr; +} +(std::make_index_sequence{}) +}; // namespace internal + +return arr; #else [[maybe_unused]] static std::array arr = get_member_offset_arr_impl(t, tp, std::make_index_sequence{}); @@ -354,7 +357,8 @@ inline constexpr void for_each(Visit&& func) { "size_t], at least has std::string_view and make sure keep " "the order of arguments"); } - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else for_each_impl(std::forward(func), arr, std::make_index_sequence{}); diff --git a/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index 4d9577f19..28b7f1d8b 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -219,7 +219,8 @@ inline constexpr void for_each(T&& t, Visit&& func) { [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is]), ...); - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else visit_members_impl0(std::forward(func), std::make_index_sequence{}, args...); #endif @@ -232,7 +233,8 @@ inline constexpr void for_each(T&& t, Visit&& func) { [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is], Is), ...); - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else visit_members_impl(std::forward(func), std::make_index_sequence{}, args...); #endif From fd4208d7f622d410d247416feaf30aa442dbcfb2 Mon Sep 17 00:00:00 2001 From: lilingyun <1434271517@qq.com> Date: Thu, 10 Oct 2024 17:26:55 +0800 Subject: [PATCH 4/8] Revert "Code format adjustment" This reverts commit 253834875994f1c8060f2dd5cdbe34c1f2b653cd. --- include/ylt/reflection/member_names.hpp | 24 ++++++++++-------------- include/ylt/reflection/member_value.hpp | 6 ++---- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/include/ylt/reflection/member_names.hpp b/include/ylt/reflection/member_names.hpp index 285705922..1cbcb6718 100644 --- a/include/ylt/reflection/member_names.hpp +++ b/include/ylt/reflection/member_names.hpp @@ -132,8 +132,7 @@ get_member_names() { ((arr[Is] = internal::get_member_name(tp))>()), ...); - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); #else init_arr_with_tuple(arr, std::make_index_sequence{}); #endif @@ -155,8 +154,7 @@ inline constexpr auto get_member_names_map() { return [&](std::index_sequence) mutable { return frozen::unordered_map{ {name_arr[Is], Is}...}; - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); #else return get_member_names_map_impl(name_arr, std::make_index_sequence{}); @@ -177,15 +175,14 @@ inline const auto& get_member_offset_arr(T&& t) { auto tp = ylt::reflection::object_to_tuple(std::forward(t)); #if __cplusplus >= 202002L - [[maybe_unused]] static std::array arr = {[&]( - std::index_sequence) mutable {std::array arr; - ((arr[Is] = size_t((const char*)&std::get(tp) - (char*)(&t))), ...); - return arr; -} -(std::make_index_sequence{}) -}; // namespace internal + [[maybe_unused]] static std::array arr = { + [&](std::index_sequence) mutable { + std::array arr; + ((arr[Is] = size_t((const char*)&std::get(tp) - (char*)(&t))), ...); + return arr; + }(std::make_index_sequence{})}; // namespace internal -return arr; + return arr; #else [[maybe_unused]] static std::array arr = get_member_offset_arr_impl(t, tp, std::make_index_sequence{}); @@ -357,8 +354,7 @@ inline constexpr void for_each(Visit&& func) { "size_t], at least has std::string_view and make sure keep " "the order of arguments"); } - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); #else for_each_impl(std::forward(func), arr, std::make_index_sequence{}); diff --git a/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index 28b7f1d8b..4d9577f19 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -219,8 +219,7 @@ inline constexpr void for_each(T&& t, Visit&& func) { [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is]), ...); - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); #else visit_members_impl0(std::forward(func), std::make_index_sequence{}, args...); #endif @@ -233,8 +232,7 @@ inline constexpr void for_each(T&& t, Visit&& func) { [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is], Is), ...); - } - (std::make_index_sequence{}); + }(std::make_index_sequence{}); #else visit_members_impl(std::forward(func), std::make_index_sequence{}, args...); #endif From 6918255e54ba2f7caae8f802b8b9568227cef5ed Mon Sep 17 00:00:00 2001 From: raver <1434271517@qq.com> Date: Thu, 10 Oct 2024 17:28:29 +0800 Subject: [PATCH 5/8] Code format adjustment --- include/ylt/reflection/member_names.hpp | 24 ++++++++++++++---------- include/ylt/reflection/member_value.hpp | 6 ++++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/include/ylt/reflection/member_names.hpp b/include/ylt/reflection/member_names.hpp index 1cbcb6718..285705922 100644 --- a/include/ylt/reflection/member_names.hpp +++ b/include/ylt/reflection/member_names.hpp @@ -132,7 +132,8 @@ get_member_names() { ((arr[Is] = internal::get_member_name(tp))>()), ...); - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else init_arr_with_tuple(arr, std::make_index_sequence{}); #endif @@ -154,7 +155,8 @@ inline constexpr auto get_member_names_map() { return [&](std::index_sequence) mutable { return frozen::unordered_map{ {name_arr[Is], Is}...}; - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else return get_member_names_map_impl(name_arr, std::make_index_sequence{}); @@ -175,14 +177,15 @@ inline const auto& get_member_offset_arr(T&& t) { auto tp = ylt::reflection::object_to_tuple(std::forward(t)); #if __cplusplus >= 202002L - [[maybe_unused]] static std::array arr = { - [&](std::index_sequence) mutable { - std::array arr; - ((arr[Is] = size_t((const char*)&std::get(tp) - (char*)(&t))), ...); - return arr; - }(std::make_index_sequence{})}; // namespace internal - + [[maybe_unused]] static std::array arr = {[&]( + std::index_sequence) mutable {std::array arr; + ((arr[Is] = size_t((const char*)&std::get(tp) - (char*)(&t))), ...); return arr; +} +(std::make_index_sequence{}) +}; // namespace internal + +return arr; #else [[maybe_unused]] static std::array arr = get_member_offset_arr_impl(t, tp, std::make_index_sequence{}); @@ -354,7 +357,8 @@ inline constexpr void for_each(Visit&& func) { "size_t], at least has std::string_view and make sure keep " "the order of arguments"); } - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else for_each_impl(std::forward(func), arr, std::make_index_sequence{}); diff --git a/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index 4d9577f19..28b7f1d8b 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -219,7 +219,8 @@ inline constexpr void for_each(T&& t, Visit&& func) { [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is]), ...); - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else visit_members_impl0(std::forward(func), std::make_index_sequence{}, args...); #endif @@ -232,7 +233,8 @@ inline constexpr void for_each(T&& t, Visit&& func) { [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is], Is), ...); - }(std::make_index_sequence{}); + } + (std::make_index_sequence{}); #else visit_members_impl(std::forward(func), std::make_index_sequence{}, args...); #endif From 027457309f20167af3d37be7c87555b6305d8495 Mon Sep 17 00:00:00 2001 From: raver <1434271517@qq.com> Date: Thu, 10 Oct 2024 18:13:05 +0800 Subject: [PATCH 6/8] Apply clang-format suggestions --- include/ylt/reflection/member_value.hpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index 28b7f1d8b..3c3d4fc55 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -215,28 +215,35 @@ 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) || _MSC_VER >= 1930) +#if __cplusplus >= 202002L && (!defined(_MSC_VER) || _MSC_VER >= + 1930) [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is]), ...); } (std::make_index_sequence{}); #else - visit_members_impl0(std::forward(func), std::make_index_sequence{}, args...); + visit_members_impl0(std::forward(func), + std::make_index_sequence{}, + args...); + #endif }); } else if constexpr (std::is_invocable_v) { visit_members(t, [&](auto&... args) { -#if __cplusplus >= 202002L && (!defined(_MSC_VER) || _MSC_VER >= 1930) +#if __cplusplus >= 202002L && (!defined(_MSC_VER) || _MSC_VER >= + 1930) [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is], Is), ...); } (std::make_index_sequence{}); #else - visit_members_impl(std::forward(func), std::make_index_sequence{}, args...); + visit_members_impl(std::forward(func), + std::make_index_sequence{}, + args...); #endif }); } From 822c49af3325348c6a2c0de2e8fbc1c4e062306f Mon Sep 17 00:00:00 2001 From: raver <1434271517@qq.com> Date: Thu, 10 Oct 2024 18:26:39 +0800 Subject: [PATCH 7/8] Apply clang-format suggestions --- include/ylt/reflection/member_value.hpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index 3c3d4fc55..c1fe88a87 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -215,8 +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) || _MSC_VER >= - 1930) +#if __cplusplus >= 202002L && (!defined(_MSC_VER) || _MSC_VER >= 1930) [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is]), ...); @@ -233,8 +232,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) || _MSC_VER >= - 1930) +#if __cplusplus >= 202002L && (!defined(_MSC_VER) || _MSC_VER >= 1930) [&](std::index_sequence) mutable { constexpr auto arr = get_member_names(); (func(args, arr[Is], Is), ...); From 01ddc95b8f8304ed3acf975508d0534351593699 Mon Sep 17 00:00:00 2001 From: raver <1434271517@qq.com> Date: Thu, 10 Oct 2024 20:57:26 +0800 Subject: [PATCH 8/8] Apply clang-format suggestions --- include/ylt/reflection/member_value.hpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/include/ylt/reflection/member_value.hpp b/include/ylt/reflection/member_value.hpp index c1fe88a87..afa307a1a 100644 --- a/include/ylt/reflection/member_value.hpp +++ b/include/ylt/reflection/member_value.hpp @@ -222,10 +222,9 @@ inline constexpr void for_each(T&& t, Visit&& func) { } (std::make_index_sequence{}); #else - visit_members_impl0(std::forward(func), - std::make_index_sequence{}, - args...); - + visit_members_impl0(std::forward(func), + std::make_index_sequence{}, + args...); #endif }); } @@ -239,9 +238,9 @@ inline constexpr void for_each(T&& t, Visit&& func) { } (std::make_index_sequence{}); #else - visit_members_impl(std::forward(func), - std::make_index_sequence{}, - args...); + visit_members_impl(std::forward(func), + std::make_index_sequence{}, + args...); #endif }); }