forked from open62541pp/open62541pp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatatype.cpp
150 lines (128 loc) · 3.8 KB
/
datatype.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/datatype.hpp"
#include <algorithm> // copy_n
#include <cassert>
#include <utility> // exchange
#include "open62541pp/detail/types_handling.hpp"
namespace opcua {
static void clearMembers(UA_DataType& native) noexcept {
detail::deallocateArray(native.members);
native.members = nullptr;
native.membersSize = 0;
}
static void clear(UA_DataType& native) noexcept {
clearMembers(native);
native = {};
}
static void copyMembers(const DataTypeMember* members, size_t membersSize, UA_DataType& dst) {
dst.members = detail::allocateArray<DataTypeMember>(membersSize);
dst.membersSize = membersSize;
std::copy_n(members, membersSize, dst.members);
}
[[nodiscard]] static UA_DataType copy(const UA_DataType& other) {
UA_DataType result{other};
copyMembers(other.members, other.membersSize, result);
return result;
}
DataType::DataType(const UA_DataType& native)
: Wrapper(copy(native)) {}
DataType::DataType(UA_DataType&& native) // NOLINT
: Wrapper(std::exchange(native, {})) {}
DataType::DataType(TypeIndex typeIndex)
: DataType(UA_TYPES[typeIndex]) { // NOLINT
assert(typeIndex < UA_TYPES_COUNT);
}
DataType::~DataType() {
clear(native());
}
DataType::DataType(const DataType& other)
: Wrapper(copy(other.native())) {}
DataType::DataType(DataType&& other) noexcept
: Wrapper(std::exchange(other.native(), {})) {}
DataType& DataType::operator=(const DataType& other) {
if (this != &other) {
clear(native());
native() = copy(other.native());
}
return *this;
}
DataType& DataType::operator=(DataType&& other) noexcept {
if (this != &other) {
clear(native());
native() = std::exchange(other.native(), {});
}
return *this;
}
void DataType::setMembers(Span<const DataTypeMember> members) {
assert(members.size() < (1U << 8U));
clearMembers(native());
copyMembers(members.data(), members.size(), native());
}
namespace detail {
DataTypeMember createDataTypeMember(
[[maybe_unused]] const char* memberName,
const UA_DataType& memberType,
uint8_t padding,
bool isArray,
[[maybe_unused]] bool isOptional
) noexcept {
DataTypeMember result{};
#ifdef UA_ENABLE_TYPEDESCRIPTION
result.memberName = memberName;
#endif
#if UAPP_OPEN62541_VER_GE(1, 3)
result.memberType = &memberType;
#else
result.memberTypeIndex = memberType.typeIndex;
result.namespaceZero = memberType.typeId.namespaceIndex == 0;
#endif
result.padding = padding;
result.isArray = isArray; // NOLINT
#if UAPP_OPEN62541_VER_GE(1, 1)
result.isOptional = isOptional; // NOLINT
#endif
return result;
}
UA_DataType createDataType(
[[maybe_unused]] const char* typeName,
UA_NodeId typeId,
UA_NodeId binaryEncodingId,
uint16_t memSize,
uint8_t typeKind,
bool pointerFree,
bool overlayable,
uint32_t membersSize,
DataTypeMember* members
) noexcept {
UA_DataType result{};
#ifdef UA_ENABLE_TYPEDESCRIPTION
result.typeName = typeName;
#endif
result.typeId = typeId;
#if UAPP_OPEN62541_VER_GE(1, 2)
result.binaryEncodingId = binaryEncodingId;
#else
assert(binaryEncodingId.identifierType == UA_NODEIDTYPE_NUMERIC);
result.binaryEncodingId = binaryEncodingId.identifier.numeric; // NOLINT
#endif
result.memSize = memSize;
result.typeKind = typeKind;
result.pointerFree = pointerFree;
result.overlayable = overlayable;
result.membersSize = membersSize;
result.members = members;
return result;
}
UA_DataTypeArray createDataTypeArray(
Span<const DataType> types, const UA_DataTypeArray* next
) noexcept {
return UA_DataTypeArray{
next,
types.size(),
asNative(types.data()),
#if UAPP_OPEN62541_VER_GE(1, 4)
false, // cleanup
#endif
};
}
} // namespace detail
} // namespace opcua