Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add index_of #341

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading