Skip to content

Commit

Permalink
Merge pull request #341 from qicosmos/add_index_of
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Dec 11, 2024
2 parents e69a7e7 + 7bd7da6 commit e53b762
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions iguana/ylt/reflection/member_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ inline size_t index_of(T& t, Field& value) {
return std::distance(offset_arr.begin(), it);
}

template <typename Member>
inline size_t index_of(Member member) {
using T = typename member_traits<Member>::owner_type;
static auto& t = internal::get_fake_object<T>();
return index_of(t, t.*member);
}

template <typename T, typename Field>
inline constexpr std::string_view name_of(T& t, Field& value) {
size_t index = index_of(t, value);
Expand Down
5 changes: 5 additions & 0 deletions test/test_reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,11 @@ TEST_CASE("test macros") {
constexpr auto idx2 = index_of<&simple2::id>();
static_assert(idx2 == 1);

auto i = index_of(&simple::id);
CHECK(i == 1);
i = index_of(&simple::age);
CHECK(i == 3);

constexpr auto arr = get_member_names<simple2>();
static_assert(arr.size() == 4);
constexpr auto map = member_names_map<simple2>;
Expand Down

0 comments on commit e53b762

Please sign in to comment.