Skip to content

Allow iteration over sequence in EnumerateMembers #21

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

Merged
merged 1 commit into from
Feb 3, 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
45 changes: 38 additions & 7 deletions include/reflection-cpp/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,11 @@ namespace detail
template <typename... Ts, typename F>
constexpr void enumerate_types(F&& f)
{
[&f]<auto... Is>(std::index_sequence<Is...>) {
[&f]<auto... Is>(std::index_sequence<Is...>)
{
(f.template operator()<Ts, Is>(), ...);
}(std::index_sequence_for<Ts...> {});
}
(std::index_sequence_for<Ts...> {});
}

template <auto... Xs, typename F>
Expand All @@ -625,9 +627,22 @@ constexpr void template_for(F&& f)
{
using t = std::common_type_t<decltype(B), decltype(E)>;

[&f]<auto... Xs>(std::integer_sequence<t, Xs...>) {
[&f]<auto... Xs>(std::integer_sequence<t, Xs...>)
{
detail::for_values<(B + Xs)...>(f);
}(std::make_integer_sequence<t, E - B> {});
}
(std::make_integer_sequence<t, E - B> {});
}

template <typename ElementMask, typename F>
constexpr void template_for(F&& f)
{
using t = typename ElementMask::value_type;
[&f]<auto... Xs>(std::integer_sequence<t, Xs...>)
{
Reflection::detail::for_values<(Xs)...>(f);
}
(ElementMask {});
}

template <auto P>
Expand Down Expand Up @@ -674,6 +689,22 @@ consteval auto GetName()
#endif
}

/// 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>
constexpr void EnumerateMembers(Object& object, Callable&& callable)
{
template_for<ElementMask>([&]<auto I>() { callable.template operator()<I>(GetMemberAt<I>(object)); });
}

/// Calls a callable on members of an object specified with ElementMask sequence with the index and member's type as
/// template arguments.
template <typename ElementMask, typename Object, typename Callable>
constexpr void EnumerateMembers(Callable&& callable)
{
template_for<ElementMask>([&]<auto I>() { callable.template operator()<I, MemberTypeOf<I, Object>>(); });
}

/// Calls a callable on each member of an object with the index of the member as the first argument.
/// and the member's default-constructed value as the second argument.
template <typename Object, typename Callable>
Expand All @@ -688,7 +719,7 @@ constexpr void EnumerateMembers(Callable&& callable)
{
// clang-format off
template_for<0, CountMembers<Object>>(
[&]<auto I>() {
[&]<auto I>() {
callable.template operator()<I, MemberTypeOf<I, Object>>();
}
);
Expand All @@ -699,7 +730,7 @@ template <typename Object, typename Callable>
requires std::same_as<void, std::invoke_result_t<Callable, std::string, MemberTypeOf<0, Object>>>
void CallOnMembers(Object& object, Callable&& callable)
{
EnumerateMembers<Object>(object,
EnumerateMembers(object,
[&]<size_t I, typename T>(T&& value) { callable(MemberNameOf<I, Object>, value); });
}

Expand Down Expand Up @@ -738,7 +769,7 @@ constexpr ResultType FoldMembers(Object& object, ResultType initialValue, Callab
{
// clang-format off
ResultType result = initialValue;
EnumerateMembers<Object>(
EnumerateMembers(
object,
[&]<size_t I, typename MemberType>(MemberType&& value) {
result = callable(MemberNameOf<I, Object>, value, result);
Expand Down
27 changes: 27 additions & 0 deletions test-reflection-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <string>
#include <string_view>
#include <utility>

struct Person
{
Expand Down Expand Up @@ -136,6 +137,25 @@ TEST_CASE("EnumerateMembers.index_and_type", "[reflection]")
});
}

TEST_CASE("EnumerateMembers.partial", "[reflection]")
{
Reflection::EnumerateMembers<std::integer_sequence<size_t, 0, 2>, Person>([]<auto I, typename T>() {
if constexpr (I == 0)
{
static_assert(std::same_as<T, std::string_view>);
}
if constexpr (I == 1)
{
static_assert(false);
}
if constexpr (I == 2)
{
static_assert(std::same_as<T, int>);
}
});
}


TEST_CASE("CallOnMembers", "[reflection]")
{
auto ps = Person { .name = "John Doe", .email = "john@doe.com", .age = 42 };
Expand Down Expand Up @@ -247,3 +267,10 @@ TEST_CASE("Compare.nested", "[reflection]")
Reflection::CollectDifferences(t1, t3, differenceCallback);
CHECK(diff == "id: 2 != 3\n");
}

TEST_CASE("TemplateFor over sequence", "[refleciton]")
{
std::string result {};
Reflection::template_for<std::integer_sequence<size_t, 3, 2, 1>>([&]<size_t I>(){result += std::to_string(I);});
CHECK(result == "321");
}