Lightweight 0.20250904.0
Loading...
Searching...
No Matches
CollectDifferences.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include <reflection-cpp/reflection.hpp>
5
6#include <any>
7#include <iostream>
8
9namespace Lightweight
10{
11
12template <typename Record>
13struct DifferenceView
14{
15
16 DifferenceView(Record const* lhs, Record const* rhs) noexcept:
17 lhs(lhs),
18 rhs(rhs)
19 {
20 }
21
22 template <typename Callback>
23 void Iterate(Callback const& callback) noexcept
24 {
25 Reflection::template_for<0, Reflection::CountMembers<Record>>([&]<auto I>() {
26 if (std::find(indexes.begin(), indexes.end(), I) != indexes.end())
27 {
28 callback(Reflection::GetMemberAt<I>(*lhs), Reflection::GetMemberAt<I>(*rhs));
29 }
30 });
31 }
32
33 void push_back(size_t ind) noexcept
34 {
35 indexes.push_back(ind);
36 }
37
38 std::vector<size_t> indexes;
39 Record const* lhs;
40 Record const* rhs;
41};
42
43template <typename Record>
44DifferenceView<Record> CollectDifferences(Record const& left, Record const& right) noexcept
45{
46
47 DifferenceView<Record> view { &left, &right };
48
49 Reflection::CollectDifferences(
50 left, right, [&](size_t ind, [[maybe_unused]] auto const& left_elem, [[maybe_unused]] auto const& right_elem) {
51 view.push_back(ind);
52 });
53
54 return view;
55}
56
57} // namespace Lightweight