diff --git a/include/reflection-cpp/reflection.hpp b/include/reflection-cpp/reflection.hpp index 6229d85..91e5f3c 100644 --- a/include/reflection-cpp/reflection.hpp +++ b/include/reflection-cpp/reflection.hpp @@ -715,6 +715,7 @@ constexpr void EnumerateMembers(Callable&& callable) } template + requires std::same_as>> void CallOnMembers(Object& object, Callable&& callable) { EnumerateMembers(object, @@ -751,6 +752,7 @@ constexpr ResultType FoldMembers(ResultType initialValue, Callable const& callab /// /// @return The result of the fold template + requires std::same_as, ResultType>> constexpr ResultType FoldMembers(Object& object, ResultType initialValue, Callable const& callable) { // clang-format off diff --git a/test-reflection-cpp.cpp b/test-reflection-cpp.cpp index d4c98fc..1e2564d 100644 --- a/test-reflection-cpp.cpp +++ b/test-reflection-cpp.cpp @@ -68,6 +68,56 @@ TEST_CASE("nested", "[reflection]") CHECK(result == R"(a=1 b=2 c=3 d="hello" e={name="John Doe" email="john@doe.com" age=42})"); } +TEST_CASE("EnumerateMembers.index_and_value", "[reflection]") +{ + auto ps = Person { "John Doe", "john@doe.com", 42 }; + Reflection::EnumerateMembers(ps, [](auto&& value) { + if constexpr (I == 0) + { + CHECK(value == "John Doe"); + } + else if constexpr (I == 1) + { + CHECK(value == "john@doe.com"); + } + else if constexpr (I == 2) + { + CHECK(value == 42); + } + }); +} + +TEST_CASE("EnumerateMembers.index_and_type", "[reflection]") +{ + Reflection::EnumerateMembers([]() { + if constexpr (I == 0) + { + static_assert(std::same_as); + } + if constexpr (I == 1) + { + static_assert(std::same_as); + } + if constexpr (I == 2) + { + static_assert(std::same_as); + } + }); +} + +TEST_CASE("CallOnMembers", "[reflection]") +{ + auto ps = Person { "John Doe", "john@doe.com", 42 }; + std::string result; + Reflection::CallOnMembers(ps, [&result](auto&& name, auto&& value) { + result += name; + result += "="; + result += std::format("{}", value); + result += " "; + }); + CHECK(result == R"(name=John Doe email=john@doe.com age=42 )"); +} + TEST_CASE("FoldMembers.type", "[reflection]") { // clang-format off