forked from objectcomputing/mFAST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield_comparator.h
More file actions
71 lines (62 loc) · 1.95 KB
/
field_comparator.h
File metadata and controls
71 lines (62 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2016, Huang-Ming Huang, Object Computing, Inc.
// All rights reserved.
//
// This file is part of mFAST.
// See the file license.txt for licensing information.
#pragma once
#include "field_visitor.h"
namespace mfast {
namespace detail {
class field_comparator {
private:
field_cref rhs_;
public:
field_comparator(const field_cref &rhs) : rhs_(rhs) {}
template <typename SimpleType> bool visit(const SimpleType &lhs) const {
SimpleType rhs = static_cast<SimpleType>(rhs_);
return lhs == rhs;
}
static bool compare(const aggregate_cref lhs, const aggregate_cref rhs) {
if (lhs.num_fields() == rhs.num_fields()) {
for (std::size_t i = 0; i < lhs.num_fields(); ++i) {
if (lhs.instruction()->field_type() !=
rhs.instruction()->field_type() ||
lhs.present() != rhs.present())
return false;
if (lhs.present()) {
field_comparator comparator(rhs[i]);
if (!apply_accessor(comparator, lhs[i]))
return false;
}
}
return true;
}
return false;
}
bool visit(const aggregate_cref &lhs, int) const {
return compare(lhs, static_cast<aggregate_cref>(rhs_));
}
bool visit(const sequence_cref &lhs, int) const {
sequence_cref rhs = static_cast<sequence_cref>(rhs_);
if (lhs.num_fields() == rhs.num_fields() && lhs.size() == rhs.size()) {
for (std::size_t i = 0; i < lhs.size(); ++i) {
if (!compare(lhs[i], rhs[i]))
return false;
}
return true;
}
return false;
}
};
}
inline bool operator==(const aggregate_cref &lhs, const aggregate_cref &rhs) {
return detail::field_comparator::compare(lhs, rhs);
}
inline bool operator==(const field_cref &lhs, const field_cref &rhs) {
if (lhs.instruction()->field_type() != rhs.instruction()->field_type())
return false;
detail::field_comparator comparator(rhs);
return apply_accessor(comparator, lhs);
}
using namespace std::rel_ops;
}