diff --git a/include/reflection-cpp/reflection.hpp b/include/reflection-cpp/reflection.hpp index 4e9b415..c33551c 100644 --- a/include/reflection-cpp/reflection.hpp +++ b/include/reflection-cpp/reflection.hpp @@ -812,6 +812,25 @@ void CollectDifferences(const Object& lhs, const Object& rhs, Callback&& callbac }); } +template +requires std::same_as, MemberTypeOf<0, Object>>> +void CollectDifferences(const Object& lhs, const Object& rhs, Callback&& callback) +{ + template_for<0, CountMembers>([&]() { + if constexpr (std::equality_comparable>) + { + if (GetMemberAt(lhs) != GetMemberAt(rhs)) + { + callback(I, GetMemberAt(lhs), GetMemberAt(rhs)); + } + } + else + { + CollectDifferences(GetMemberAt(lhs), GetMemberAt(rhs), callback); + } + }); +} + } // namespace Reflection template diff --git a/test-reflection-cpp.cpp b/test-reflection-cpp.cpp index 4029687..99437ef 100644 --- a/test-reflection-cpp.cpp +++ b/test-reflection-cpp.cpp @@ -203,6 +203,23 @@ TEST_CASE("Compare.simple", "[reflection]") CHECK(diff == "id: 1 != 2\nname: John Doe != Jane Doe\nage: 42 != 43\n"); } + + +TEST_CASE("Compare.simple_with_indexing", "[reflection]") +{ + auto const r1 = Record { .id = 1, .name = "John Doe", .age = 42 }; + auto const r2 = Record { .id = 2, .name = "John Doe", .age = 42 }; + + size_t check = -1; + auto differenceCallback = [&](size_t ind, auto const& lhs, auto const& rhs) { + check = ind; + }; + + Reflection::CollectDifferences(r1, r2, differenceCallback); + CHECK(check == 0); +} + + struct Table { Record first;