Skip to content
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 protected] 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 [email protected])");
}

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