Skip to content

Add CallOnMembersWithoutName function #27

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
May 12, 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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(reflection-cpp VERSION 0.3.0 LANGUAGES CXX)
project(reflection-cpp VERSION 0.3.1 LANGUAGES CXX)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Expand Down
8 changes: 7 additions & 1 deletion include/reflection-cpp/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1264,12 +1264,18 @@ constexpr void EnumerateMembers(Callable&& callable)
}

template <typename Object, typename Callable>
requires std::same_as<void, std::invoke_result_t<Callable, std::string, MemberTypeOf<0, Object>>>
requires std::is_invocable_v<Callable, std::string, MemberTypeOf<0, Object>>
void CallOnMembers(Object& object, Callable&& callable)
{
EnumerateMembers(object, [&]<size_t I, typename T>(T&& value) { callable(MemberNameOf<I, Object>, value); });
}

template <typename Object, typename Callable>
void CallOnMembersWithoutName(Object& object, Callable&& callable)
{
EnumerateMembers(object, [&]<size_t I, typename T>(T&& value) { callable.template operator()<I, T>(value); });
}

/// Folds over the members of a type without an object of it.
///
/// @param initialValue The initial value to fold with
Expand Down
7 changes: 7 additions & 0 deletions test-reflection-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ TEST_CASE("single value record", "[reflection]")
CHECK(name == "value");
CHECK(value == 42);
});

}

TEST_CASE("core", "[reflection]")
Expand Down Expand Up @@ -181,6 +182,12 @@ TEST_CASE("CallOnMembers", "[reflection]")
result += " ";
});
CHECK(result == R"(name=John Doe email=john@doe.com age=42 )");

std::string resultAnother;
Reflection::CallOnMembersWithoutName(ps, [&resultAnother]<size_t I, typename FieldType>(FieldType const& value) {
resultAnother += std::format("{}", value);
});
CHECK(resultAnother == R"(John Doejohn@doe.com42)");
}

TEST_CASE("FoldMembers.type", "[reflection]")
Expand Down