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 MemberClassType<P> and MemberIndexOf<P> to get the class type and member-index of P. #23

Merged
merged 1 commit into from
Feb 8, 2025
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
36 changes: 35 additions & 1 deletion include/reflection-cpp/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,24 @@ constexpr auto TypeNameOf = [] {
#endif
}();

namespace detail
{
template <typename T>
struct MemberClassTypeHelper;

template <typename M, typename T>
struct MemberClassTypeHelper<M T::*>
{
using type = std::remove_cvref_t<T>;
};
} // namespace detail

/// Represents the class type of a member pointer
///
/// @tparam P The member pointer, e.g. &MyClass::member
template <auto P>
using MemberClassType = typename detail::MemberClassTypeHelper<decltype(P)>::type;

namespace detail
{
template <class T, size_t... I>
Expand Down Expand Up @@ -641,7 +659,6 @@ constexpr void template_for(F&& f)

namespace detail
{

template <auto P>
requires(std::is_member_pointer_v<decltype(P)>)
consteval std::string_view GetName()
Expand Down Expand Up @@ -691,6 +708,23 @@ namespace detail
template <auto V>
constexpr std::string_view NameOf = detail::GetName<V>();

namespace detail
{
// private helper for implementing MemberIndexOf<P>
template <auto P, size_t... I>
consteval size_t MemberIndexHelperImpl(std::index_sequence<I...>)
{
return (((NameOf<P> == MemberNames<MemberClassType<P>>[I]) ? I : 0) + ...);
}
} // namespace detail

/// Gets the index of a member pointer in the member list of its class
///
/// @tparam P The member pointer, e.g. &MyClass::member
template <auto P>
constexpr size_t MemberIndexOf =
detail::MemberIndexHelperImpl<P>(std::make_index_sequence<CountMembers<MemberClassType<P>>> {});

/// Calls a callable on members of an object specified with ElementMask sequence with the index of the member as the
/// first argument. and the member's default-constructed value as the second argument.
template <typename ElementMask, typename Object, typename Callable>
Expand Down
7 changes: 7 additions & 0 deletions test-reflection-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ struct SingleValueRecord
int value;
};

TEST_CASE("MemberIndex", "[reflection]")
{
static_assert(Reflection::MemberIndexOf<&Person::name> == 0);
static_assert(Reflection::MemberIndexOf<&Person::email> == 1);
static_assert(Reflection::MemberIndexOf<&Person::age> == 2);
}

TEST_CASE("TypeNameOf", "[reflection]")
{
CHECK(Reflection::TypeNameOf<int> == "int");
Expand Down