Skip to content

Commit

Permalink
Merge pull request #271 from qicosmos/indexs_of
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored May 6, 2024
2 parents 738bf48 + bd8f648 commit aa8c376
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,11 @@ constexpr size_t index_of() {
return index;
}

template <auto... members>
constexpr std::array<size_t, sizeof...(members)> indexs_of() {
return std::array<size_t, sizeof...(members)>{index_of<members>()...};
}

template <auto member>
constexpr auto name_of() {
using T = typename member_tratis<decltype(member)>::owner_type;
Expand All @@ -966,6 +971,12 @@ constexpr auto name_of() {
return std::string_view(s.data(), s.size());
}

template <auto... members>
constexpr std::array<std::string_view, sizeof...(members)> names_of() {
return std::array<std::string_view, sizeof...(members)>{
name_of<members>()...};
}

template <auto member>
constexpr auto member_count_of() {
using T = typename member_tratis<decltype(member)>::owner_type;
Expand Down
6 changes: 6 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,12 @@ TEST_CASE("index_of name_of") {
CHECK(idx1 == 1);
CHECK(idx2 == 0);

constexpr auto index_arr = iguana::indexs_of<&point_t::x, &point_t::y>();
constexpr auto name_arr = iguana::names_of<&point_t::x, &point_t::y>();

CHECK(index_arr == std::array<size_t, 2>{0, 1});
CHECK(name_arr == std::array<std::string_view, 2>{"x", "y"});

constexpr auto s1 = iguana::name_of<&point_t::y>();
static_assert(s1 == "y");
constexpr auto s2 = iguana::name_of<&person::name>();
Expand Down

0 comments on commit aa8c376

Please sign in to comment.