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