Skip to content

Commit

Permalink
[struct_json] is compatible with msvc versions
Browse files Browse the repository at this point in the history
  • Loading branch information
RAVER0306 committed Oct 10, 2024
1 parent d5aba06 commit 736651c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
37 changes: 23 additions & 14 deletions include/ylt/reflection/member_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,25 @@ get_member_names() {
else {
std::array<std::string_view, Count> arr;
#if __cplusplus >= 202002L
#if defined(_MSC_VER)
#if _MSC_VER >= 1930
constexpr auto tp = struct_to_tuple<T>();
[&]<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>{});
}(std::make_index_sequence<Count>{});
#else
init_arr_with_tuple<T>(arr, std::make_index_sequence<Count>{});
#endif
#else
constexpr auto tp = struct_to_tuple<T>();
[&]<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>{});
#endif
#else
init_arr_with_tuple<T>(arr, std::make_index_sequence<Count>{});
#endif
Expand All @@ -155,8 +167,7 @@ inline constexpr auto get_member_names_map() {
return [&]<size_t... Is>(std::index_sequence<Is...>) mutable {
return frozen::unordered_map<frozen::string, size_t, name_arr.size()>{
{name_arr[Is], Is}...};
}
(std::make_index_sequence<name_arr.size()>{});
}(std::make_index_sequence<name_arr.size()>{});
#else
return get_member_names_map_impl(name_arr,
std::make_index_sequence<name_arr.size()>{});
Expand All @@ -177,15 +188,14 @@ inline const auto& get_member_offset_arr(T&& t) {
auto tp = ylt::reflection::object_to_tuple(std::forward<T>(t));

#if __cplusplus >= 202002L
[[maybe_unused]] static std::array<size_t, Count> arr = {[&]<size_t... Is>(
std::index_sequence<Is...>) mutable {std::array<size_t, Count> arr;
((arr[Is] = size_t((const char*)&std::get<Is>(tp) - (char*)(&t))), ...);
return arr;
}
(std::make_index_sequence<Count>{})
}; // namespace internal
[[maybe_unused]] static std::array<size_t, Count> arr = {
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
std::array<size_t, Count> arr;
((arr[Is] = size_t((const char*)&std::get<Is>(tp) - (char*)(&t))), ...);
return arr;
}(std::make_index_sequence<Count>{})}; // namespace internal

return arr;
return arr;
#else
[[maybe_unused]] static std::array<size_t, Count> arr =
get_member_offset_arr_impl(t, tp, std::make_index_sequence<Count>{});
Expand Down Expand Up @@ -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<arr.size()>{});
}(std::make_index_sequence<arr.size()>{});
#else
for_each_impl(std::forward<Visit>(func), arr,
std::make_index_sequence<arr.size()>{});
Expand Down
36 changes: 26 additions & 10 deletions include/ylt/reflection/member_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,31 +216,47 @@ inline constexpr void for_each(T&& t, Visit&& func) {
if constexpr (std::is_invocable_v<Visit, first_t, std::string_view>) {
visit_members(t, [&](auto&... args) {
#if __cplusplus >= 202002L
#if defined(_MSC_VER)
#if _MSC_VER >= 1930
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
constexpr auto arr = get_member_names<T>();
(func(args, arr[Is]), ...);
}
(std::make_index_sequence<sizeof...(args)>{});
}(std::make_index_sequence<sizeof...(args)>{});
#else
visit_members_impl0<T>(std::forward<Visit>(func),
std::make_index_sequence<sizeof...(args)>{},
args...);
visit_members_impl0<T>(std::forward<Visit>(func), std::make_index_sequence<sizeof...(args)>{}, args...);
#endif
#else
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
constexpr auto arr = get_member_names<T>();
(func(args, arr[Is]), ...);
}(std::make_index_sequence<sizeof...(args)>{});
#endif
#else
visit_members_impl0<T>(std::forward<Visit>(func), std::make_index_sequence<sizeof...(args)>{}, args...);
#endif
});
}
else if constexpr (std::is_invocable_v<Visit, first_t, std::string_view,
size_t>) {
visit_members(t, [&](auto&... args) {
#if __cplusplus >= 202002L
#if defined(_MSC_VER)
#if _MSC_VER >= 1930
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
constexpr auto arr = get_member_names<T>();
(func(args, arr[Is], Is), ...);
}
(std::make_index_sequence<sizeof...(args)>{});
}(std::make_index_sequence<sizeof...(args)>{});
#else
visit_members_impl<T>(std::forward<Visit>(func), std::make_index_sequence<sizeof...(args)>{}, args...);
#endif
#else
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
constexpr auto arr = get_member_names<T>();
(func(args, arr[Is], Is), ...);
}(std::make_index_sequence<sizeof...(args)>{});
#endif
#else
visit_members_impl<T>(std::forward<Visit>(func),
std::make_index_sequence<sizeof...(args)>{},
args...);
visit_members_impl<T>(std::forward<Visit>(func), std::make_index_sequence<sizeof...(args)>{}, args...);
#endif
});
}
Expand Down

0 comments on commit 736651c

Please sign in to comment.