Skip to content

Commit 83f5e4c

Browse files
Probably good to also name both variants FoldMembers
Signed-off-by: Christian Parpart <[email protected]>
1 parent 484e25a commit 83f5e4c

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

include/reflection-cpp/reflection.hpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -639,29 +639,6 @@ constexpr void template_for(F&& f)
639639
}(std::make_integer_sequence<t, E - B> {});
640640
}
641641

642-
/// Folds over the members of a type
643-
///
644-
/// @param initialValue The initial value to fold with
645-
/// @param callable The callable to fold with. The parameters are the member name,
646-
/// the member's default value and the current result of the fold.
647-
///
648-
/// @return The result of the fold
649-
template <typename Object, typename Callable, typename ResultType>
650-
constexpr ResultType FoldType(ResultType initialValue, Callable const& callable)
651-
{
652-
// clang-format off
653-
ResultType result = initialValue;
654-
template_for<0, Reflection::CountMembers<Object>>(
655-
[&]<size_t I>() {
656-
result = callable(Reflection::MemberNameOf<I, Object>,
657-
std::get<I>(Reflection::ToTuple(Object {})),
658-
result);
659-
}
660-
);
661-
// clang-format on
662-
return result;
663-
}
664-
665642
template <auto P>
666643
requires(std::is_member_pointer_v<decltype(P)>)
667644
consteval std::string_view GetName()
@@ -719,6 +696,29 @@ void CallOnMembers(Object& object, Callable&& callable)
719696
[&]<auto I>() { callable(Reflection::MemberNameOf<I, Object>, std::get<I>(Reflection::ToTuple(object))); });
720697
}
721698

699+
/// Folds over the members of a type without an object of it.
700+
///
701+
/// @param initialValue The initial value to fold with
702+
/// @param callable The callable to fold with. The parameters are the member name,
703+
/// the member's default value and the current result of the fold.
704+
///
705+
/// @return The result of the fold
706+
template <typename Object, typename Callable, typename ResultType>
707+
constexpr ResultType FoldMembers(ResultType initialValue, Callable const& callable)
708+
{
709+
// clang-format off
710+
ResultType result = initialValue;
711+
template_for<0, Reflection::CountMembers<Object>>(
712+
[&]<size_t I>() {
713+
result = callable(Reflection::MemberNameOf<I, Object>,
714+
std::get<I>(Reflection::ToTuple(Object {})),
715+
result);
716+
}
717+
);
718+
// clang-format on
719+
return result;
720+
}
721+
722722
/// Folds over the members of an object
723723
///
724724
/// @param object The object to fold over

test-reflection-cpp.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ TEST_CASE("nested", "[reflection]")
6868
CHECK(result == R"(a=1 b=2 c=3 d="hello" e={name="John Doe" email="[email protected]" age=42})");
6969
}
7070

71-
TEST_CASE("FoldType", "[reflection]")
71+
TEST_CASE("FoldMembers.type", "[reflection]")
7272
{
7373
// clang-format off
74-
auto const result = Reflection::FoldType<TestStruct>(0, [](auto&& /*name*/, auto&& /*value*/, auto&& result) {
74+
auto const result = Reflection::FoldMembers<TestStruct>(0, [](auto&& /*name*/, auto&& /*value*/, auto&& result) {
7575
return result + 1;
7676
});
7777
// clang-format on
@@ -85,7 +85,7 @@ struct S
8585
int c {};
8686
};
8787

88-
TEST_CASE("FoldMembers", "[reflection]")
88+
TEST_CASE("FoldMembers.value", "[reflection]")
8989
{
9090
auto const s = S { 1, 2, 3 };
9191
auto const result = Reflection::FoldMembers(

0 commit comments

Comments
 (0)