From 9186051642e972a584c3bd9514d3ff8f10e7b8a0 Mon Sep 17 00:00:00 2001 From: Yaraslau Tamashevich Date: Thu, 12 Dec 2024 12:41:15 +0200 Subject: [PATCH] Fixes problem of working on structs with only a single element --- include/reflection-cpp/reflection.hpp | 4 ++-- test-reflection-cpp.cpp | 28 ++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/include/reflection-cpp/reflection.hpp b/include/reflection-cpp/reflection.hpp index 9ac6424..c2b6e61 100644 --- a/include/reflection-cpp/reflection.hpp +++ b/include/reflection-cpp/reflection.hpp @@ -715,7 +715,7 @@ constexpr void EnumerateMembers(Callable&& callable) } template - requires std::same_as>> + requires std::same_as>> void CallOnMembers(Object& object, Callable&& callable) { EnumerateMembers(object, @@ -752,7 +752,7 @@ constexpr ResultType FoldMembers(ResultType initialValue, Callable const& callab /// /// @return The result of the fold template - requires std::same_as, ResultType>> + 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 607cac2..4029687 100644 --- a/test-reflection-cpp.cpp +++ b/test-reflection-cpp.cpp @@ -30,6 +30,11 @@ enum Color Blue }; +struct SingleValueRecord +{ + int value; +}; + TEST_CASE("GetName", "[reflection]") { auto const enumValue = Reflection::GetName(); @@ -38,13 +43,34 @@ TEST_CASE("GetName", "[reflection]") auto const enumValue2 = Reflection::GetName(); CHECK(enumValue2 == "Green"); - auto const person = Person { "John Doe", "john@doe.com", 42 }; auto const memberName1 = Reflection::GetName<&Person::email>(); CHECK(memberName1 == "email"); + + auto const singleValueField = Reflection::GetName<&SingleValueRecord::value>(); + CHECK(singleValueField == "value"); +} + +TEST_CASE("single value record", "[reflection]") +{ + static_assert(Reflection::CountMembers == 1); + + auto const s = SingleValueRecord { 42 }; + auto const t = Reflection::ToTuple(s); + + CHECK(std::get<0>(t) == 42); + CHECK(Reflection::GetMemberAt<0>(s) == 42); + + Reflection::CallOnMembers(s, [](auto&& name, auto&& value) { + CHECK(name == "value"); + CHECK(value == 42); + }); } TEST_CASE("core", "[reflection]") { + auto s = SingleValueRecord { 42 }; + CHECK(Reflection::Inspect(s) == "value=42"); + auto p = Person { "John Doe", "john@doe.com", 42 }; auto const result = Reflection::Inspect(p); CHECK(result == R"(name="John Doe" email="john@doe.com" age=42)");