Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed Sep 11, 2024
1 parent 570d1b9 commit 79a090a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
26 changes: 7 additions & 19 deletions iguana/ylt/reflection/member_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ inline constexpr bool has_alias_struct_name_v =
has_alias_struct_names_t<T>::value;

template <typename T, typename U, size_t... Is>
inline constexpr void init_arr_with_tuple(const T& tp, U& arr,
std::index_sequence<Is...>) {
inline constexpr void init_arr_with_tuple(U& arr, std::index_sequence<Is...>) {
constexpr auto tp = struct_to_tuple<T>();
((arr[Is] = internal::get_member_name<internal::wrap(std::get<Is>(tp))>()),
...);
}
Expand All @@ -106,18 +106,17 @@ get_member_names() {
return type::refl_member_names(ylt::reflection::identity<type>{});
}
else {
constexpr auto tp = struct_to_tuple<T>();

std::array<std::string_view, Count> arr;
#if __cplusplus >= 202002L
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>{});
#else
init_arr_with_tuple(tp, arr, std::make_index_sequence<Count>{});
init_arr_with_tuple<T>(arr, std::make_index_sequence<Count>{});
#endif
return arr;
}
Expand Down Expand Up @@ -210,26 +209,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 +242,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
6 changes: 3 additions & 3 deletions test/test_cpp20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ylt::reflection::ylt_alias_struct<point_t1> {
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"});
return std::array{field_alias_t{"X", 0}, field_alias_t{"Y", 1}};
}
};
struct test_variant {
Expand Down Expand Up @@ -253,8 +253,8 @@ TEST_CASE("test xml") {
constexpr auto names = ylt::reflection::get_member_names<point_t1>();
constexpr auto st_name = ylt::reflection::get_struct_name<point_t1>();
CHECK(names == std::array<std::string_view, 2>{"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<int>();
Expand Down
4 changes: 2 additions & 2 deletions test/test_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,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 @@ -755,7 +755,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>{"w"}, field_alias_t<1>{"h"});
return std::array{field_alias_t{"w", 0}, field_alias_t{"h", 1}};
}
};

Expand Down

0 comments on commit 79a090a

Please sign in to comment.