Skip to content

Commit

Permalink
fix and improve
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Sep 11, 2024
1 parent 81a1866 commit d145180
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 54 deletions.
21 changes: 5 additions & 16 deletions include/ylt/reflection/member_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ get_member_names() {
constexpr auto tp = struct_to_tuple<T>();

std::array<std::string_view, Count> arr;
#if __cplusplus >= 202002L || defined(_MSC_VER)
#if __cplusplus >= 202002L
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
((arr[Is] =
internal::get_member_name<internal::wrap(std::get<Is>(tp))>()),
Expand All @@ -133,7 +133,7 @@ inline constexpr auto get_member_names_map_impl(T& name_arr,
template <typename T>
inline constexpr auto get_member_names_map() {
constexpr auto name_arr = get_member_names<T>();
#if __cplusplus >= 202002L || defined(_MSC_VER)
#if __cplusplus >= 202002L
return [&]<size_t... Is>(std::index_sequence<Is...>) mutable {
return frozen::unordered_map<frozen::string, size_t, name_arr.size()>{
{name_arr[Is], Is}...};
Expand Down Expand Up @@ -210,26 +210,15 @@ inline constexpr size_t index_of() {
return names.size();
}

template <size_t Idx>
struct field_alias_t {
std::string_view alias_name;
inline static constexpr auto index = Idx;
size_t index;
};

template <typename Tuple, size_t... Is>
inline constexpr auto get_alias_field_names_impl(Tuple& tp,
std::index_sequence<Is...>) {
return std::array<std::pair<size_t, std::string_view>, sizeof...(Is)>{
std::make_pair(std::tuple_element_t<Is, std::decay_t<Tuple>>::index,
std::get<Is>(tp).alias_name)...};
}

template <typename T>
inline constexpr auto get_alias_field_names() {
if constexpr (internal::has_alias_field_names_v<T>) {
constexpr auto tp = ylt_alias_struct<T>::get_alias_field_names();
return get_alias_field_names_impl(
tp, std::make_index_sequence<std::tuple_size_v<decltype(tp)>>{});
return ylt_alias_struct<T>::get_alias_field_names();
}
else {
return std::array<std::string_view, 0>{};
Expand All @@ -254,7 +243,7 @@ get_member_names() {
if constexpr (internal::has_alias_field_names_v<U>) {
constexpr auto alias_arr = get_alias_field_names<U>();
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;
Expand Down
5 changes: 2 additions & 3 deletions include/ylt/reflection/member_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ inline constexpr std::string_view name_of(T& t, Field& value) {
return arr[index];
}

template <typename T, typename Visit, typename U, size_t... Is,
typename... Args>
template <typename T, typename Visit, size_t... Is, typename... Args>
inline constexpr void visit_members_impl0(Visit&& func,
std::index_sequence<Is...>,
Args&... args) {
Expand Down Expand Up @@ -216,7 +215,7 @@ inline constexpr void for_each(T&& t, Visit&& func) {
else {
if constexpr (std::is_invocable_v<Visit, first_t, std::string_view>) {
visit_members(t, [&](auto&... args) {
#if __cplusplus >= 202002L || defined(_MSC_VER)
#if __cplusplus >= 202002L
[&]<size_t... Is>(std::index_sequence<Is...>) mutable {
constexpr auto arr = get_member_names<T>();
(func(args, arr[Is]), ...);
Expand Down
64 changes: 33 additions & 31 deletions include/ylt/reflection/user_reflect_macro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<STRUCT> &t); \
template struct private_visitor<STRUCT, ##__VA_ARGS__>; \
template <typename Visitor> \
inline static constexpr decltype(auto) refl_visit_members( \
STRUCT &t, Visitor &&visitor) { \
return visit_private_fields(t, std::forward<Visitor>(visitor)); \
} \
template <typename Visitor> \
inline static constexpr decltype(auto) refl_visit_members( \
const STRUCT &t, Visitor &&visitor) { \
return visit_private_fields(t, std::forward<Visitor>(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<STRUCT> &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<STRUCT> &t); \
template struct private_visitor<STRUCT, ##__VA_ARGS__>; \
} \
template <typename Visitor> \
inline static constexpr decltype(auto) refl_visit_members( \
STRUCT &t, Visitor &&visitor) { \
return visit_private_fields(t, std::forward<Visitor>(visitor)); \
} \
template <typename Visitor> \
inline static constexpr decltype(auto) refl_visit_members( \
const STRUCT &t, Visitor &&visitor) { \
return visit_private_fields(t, std::forward<Visitor>(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<STRUCT> &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<STRUCT> &t) { \
constexpr std::array<std::string_view, YLT_ARG_COUNT(__VA_ARGS__)> 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<STRUCT> &t) { \
constexpr std::array<std::string_view, YLT_ARG_COUNT(__VA_ARGS__)> arr{ \
WRAP_ARGS(CONCAT_NAME, t, ##__VA_ARGS__)}; \
return arr; \
} \
YLT_REFL_PRIVATE_(STRUCT, WRAP_ARGS(CONCAT_ADDR, STRUCT, ##__VA_ARGS__))

template <typename T, typename = void>
Expand Down
4 changes: 2 additions & 2 deletions src/reflection/tests/test_reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bank_t>{});
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";
});
Expand Down
4 changes: 2 additions & 2 deletions src/struct_xml/examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ struct ylt::reflection::ylt_alias_struct<next_obj_t> {
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}};
}
};

Expand All @@ -312,7 +312,7 @@ struct ylt::reflection::ylt_alias_struct<out_object> {
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}};
}
};

Expand Down

0 comments on commit d145180

Please sign in to comment.