Skip to content

Commit 1ef843f

Browse files
authored
Merge pull request #19 from contour-terminal/improvement/collectdifference_with_ind
collectDifferences with first argument beeing index of the data member
2 parents 94414ed + def0134 commit 1ef843f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

include/reflection-cpp/reflection.hpp

+19
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,25 @@ void CollectDifferences(const Object& lhs, const Object& rhs, Callback&& callbac
812812
});
813813
}
814814

815+
template <typename Object, typename Callback>
816+
requires std::same_as<void, std::invoke_result_t<Callback, size_t, MemberTypeOf<0, Object>, MemberTypeOf<0, Object>>>
817+
void CollectDifferences(const Object& lhs, const Object& rhs, Callback&& callback)
818+
{
819+
template_for<0, CountMembers<Object>>([&]<auto I>() {
820+
if constexpr (std::equality_comparable<MemberTypeOf<I, Object>>)
821+
{
822+
if (GetMemberAt<I>(lhs) != GetMemberAt<I>(rhs))
823+
{
824+
callback(I, GetMemberAt<I>(lhs), GetMemberAt<I>(rhs));
825+
}
826+
}
827+
else
828+
{
829+
CollectDifferences(GetMemberAt<I>(lhs), GetMemberAt<I>(rhs), callback);
830+
}
831+
});
832+
}
833+
815834
} // namespace Reflection
816835

817836
template <std::size_t N>

test-reflection-cpp.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ TEST_CASE("Compare.simple", "[reflection]")
203203
CHECK(diff == "id: 1 != 2\nname: John Doe != Jane Doe\nage: 42 != 43\n");
204204
}
205205

206+
207+
208+
TEST_CASE("Compare.simple_with_indexing", "[reflection]")
209+
{
210+
auto const r1 = Record { .id = 1, .name = "John Doe", .age = 42 };
211+
auto const r2 = Record { .id = 2, .name = "John Doe", .age = 42 };
212+
213+
size_t check = -1;
214+
auto differenceCallback = [&](size_t ind, auto const& lhs, auto const& rhs) {
215+
check = ind;
216+
};
217+
218+
Reflection::CollectDifferences(r1, r2, differenceCallback);
219+
CHECK(check == 0);
220+
}
221+
222+
206223
struct Table
207224
{
208225
Record first;

0 commit comments

Comments
 (0)