forked from open62541pp/open62541pp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathua_types.cpp
150 lines (124 loc) · 4.94 KB
/
ua_types.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "open62541pp/ua/types.hpp"
#include <algorithm> // copy, transform
namespace opcua {
inline namespace ua {
#ifdef UA_ENABLE_SUBSCRIPTIONS
ContentFilterElement::ContentFilterElement(
FilterOperator filterOperator, Span<const FilterOperand> operands
) {
handle()->filterOperator = static_cast<UA_FilterOperator>(filterOperator);
handle()->filterOperandsSize = operands.size();
handle()->filterOperands = detail::allocateArray<UA_ExtensionObject>(
operands.size(), UA_TYPES[UA_TYPES_EXTENSIONOBJECT]
);
// transform array of operand variants to array of extension objects
std::transform(
operands.begin(),
operands.end(),
asWrapper<ExtensionObject>(handle()->filterOperands),
[](auto&& variant) {
return std::visit(
[](auto&& operand) { return ExtensionObject::fromDecodedCopy(operand); }, variant
);
}
);
}
ContentFilter::ContentFilter(std::initializer_list<ContentFilterElement> elements)
: ContentFilter({elements.begin(), elements.size()}) {}
ContentFilter::ContentFilter(Span<const ContentFilterElement> elements) {
handle()->elementsSize = elements.size();
handle()->elements = detail::toNativeArray(elements);
}
/* ----------------------------------- ContentFilter operators ---------------------------------- */
static ContentFilter concatFilterElements(
std::initializer_list<Span<const ContentFilterElement>> filterElementsList
) {
size_t totalSize = 0;
for (const auto& filterElements : filterElementsList) {
totalSize += filterElements.size();
}
ContentFilter result;
result->elementsSize = totalSize;
result->elements = detail::allocateArray<UA_ContentFilterElement>(
totalSize, UA_TYPES[UA_TYPES_CONTENTFILTERELEMENT]
);
const Span<ContentFilterElement> resultElements(
asWrapper<ContentFilterElement>(result->elements), totalSize
);
size_t offset = 0;
for (const auto& filterElements : filterElementsList) {
auto resultElementsView = resultElements.subview(offset, filterElements.size());
// copy to result
std::copy(filterElements.begin(), filterElements.end(), resultElementsView.begin());
// increment element operand indexes by offset
for (auto& element : resultElementsView) {
for (auto& operand : element.filterOperands()) {
auto* elementOperand = operand.decodedData<ElementOperand>();
if (elementOperand != nullptr) {
elementOperand->handle()->index += static_cast<uint32_t>(offset);
}
}
}
// increment offset
offset += filterElements.size();
}
return result;
}
static ContentFilter applyUnaryOperator(
FilterOperator unaryOperator, Span<const ContentFilterElement> elements
) {
return concatFilterElements({
{{unaryOperator, {ElementOperand(1)}}},
elements,
});
}
static ContentFilter applyBinaryOperator(
FilterOperator binaryOperator,
Span<const ContentFilterElement> lhs,
Span<const ContentFilterElement> rhs
) {
return concatFilterElements({
{{
binaryOperator,
{
ElementOperand(1),
ElementOperand(1 + static_cast<uint32_t>(lhs.size())),
},
}},
lhs,
rhs,
});
}
ContentFilter operator!(const ContentFilterElement& filterElement) {
return applyUnaryOperator(FilterOperator::Not, {filterElement});
}
ContentFilter operator!(const ContentFilter& filter) {
return applyUnaryOperator(FilterOperator::Not, filter.elements());
}
ContentFilter operator&&(const ContentFilterElement& lhs, const ContentFilterElement& rhs) {
return applyBinaryOperator(FilterOperator::And, {lhs}, {rhs});
}
ContentFilter operator&&(const ContentFilterElement& lhs, const ContentFilter& rhs) {
return applyBinaryOperator(FilterOperator::And, {lhs}, rhs.elements());
}
ContentFilter operator&&(const ContentFilter& lhs, const ContentFilterElement& rhs) {
return applyBinaryOperator(FilterOperator::And, lhs.elements(), {rhs});
}
ContentFilter operator&&(const ContentFilter& lhs, const ContentFilter& rhs) {
return applyBinaryOperator(FilterOperator::And, lhs.elements(), rhs.elements());
}
ContentFilter operator||(const ContentFilterElement& lhs, const ContentFilterElement& rhs) {
return applyBinaryOperator(FilterOperator::Or, {lhs}, {rhs});
}
ContentFilter operator||(const ContentFilterElement& lhs, const ContentFilter& rhs) {
return applyBinaryOperator(FilterOperator::Or, {lhs}, rhs.elements());
}
ContentFilter operator||(const ContentFilter& lhs, const ContentFilterElement& rhs) {
return applyBinaryOperator(FilterOperator::Or, lhs.elements(), {rhs});
}
ContentFilter operator||(const ContentFilter& lhs, const ContentFilter& rhs) {
return applyBinaryOperator(FilterOperator::Or, lhs.elements(), rhs.elements());
}
#endif
} // namespace ua
} // namespace opcua