open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
attribute_handler.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <cstdint>
5#include <type_traits>
6#include <utility> // forward, move
7#include <vector>
8
10#include "open62541pp/common.hpp" // AttributeId, WriteMask, EventNotifier, AccessLevel
12#include "open62541pp/types.hpp"
13
15
16inline Result<Variant> getVariant(DataValue&& dv) noexcept {
17 if (dv.getStatus().isBad()) {
18 return BadResult(dv.getStatus());
19 }
20 if (!dv.hasValue()) {
22 }
23 return std::move(dv).getValue();
24}
25
26/**
27 * Attribute handler to convert DataValue objects to/from the attribute specific types.
28 * Template specializations must be provided for all AttributeIds.
29 */
30template <AttributeId Attribute>
32
34 using Type = Variant;
35
36 static Result<Variant> fromDataValue(DataValue&& dv) noexcept {
37 return getVariant(std::move(dv));
38 }
39
40 static DataValue toDataValue(const Variant& value) noexcept {
41 DataValue dv;
42 dv->value = value; // shallow copy
43 dv->value.storageType = UA_VARIANT_DATA_NODELETE; // prevent double delete
44 dv->hasValue = true;
45 return dv;
46 }
47};
48
49template <typename T, typename Enable = void>
51 using Type = T;
52
53 static Result<Type> fromDataValue(DataValue&& dv) noexcept {
54 return getVariant(std::move(dv)).transform([](Variant&& var) {
55 assert(var.isType<T>());
56 assert(var.isScalar());
57 return std::move(var).getScalar<T>();
58 });
59 }
60
61 template <typename U>
62 static DataValue toDataValue(U&& value) {
63 return DataValue::fromScalar(std::forward<U>(value));
64 }
65};
66
67template <typename T>
68struct AttributeHandlerScalar<T, std::enable_if_t<std::is_enum_v<T>>> {
69 using Type = T;
70 using UnderlyingType = std::underlying_type_t<Type>;
72
73 static Result<Type> fromDataValue(DataValue&& dv) noexcept {
74 return UnderlyingHandler::fromDataValue(std::move(dv)).transform([](auto value) {
75 return static_cast<Type>(value);
76 });
77 }
78
79 static DataValue toDataValue(Type value) {
80 return UnderlyingHandler::toDataValue(static_cast<UnderlyingType>(value));
81 }
82};
83
84template <typename T>
89
90 static Result<Type> fromDataValue(DataValue&& dv) noexcept {
91 return UnderlyingHandler::fromDataValue(std::move(dv)).transform([](auto value) {
92 return Bitmask<T>(value);
93 });
94 }
95
96 static DataValue toDataValue(Type value) {
97 return UnderlyingHandler::toDataValue(value.get());
98 }
99};
100
101template <>
103
104template <>
107
108 static Result<Type> fromDataValue(DataValue&& dv) noexcept {
109 return getVariant(std::move(dv)).transform([](const Variant& var) {
110 // workaround to read enum from variant...
111 return *static_cast<const NodeClass*>(var.data());
112 });
113 }
114};
115
116template <>
118
119template <>
121
122template <>
124
125template <>
127
128template <>
130
131template <>
133
134template <>
136
137template <>
139
140template <>
142
143template <>
145 : AttributeHandlerScalar<Bitmask<EventNotifier>> {};
146
147template <>
149
150template <>
152
153template <>
155
156template <>
158 using Type = std::vector<uint32_t>;
159
160 static Result<Type> fromDataValue(DataValue&& dv) noexcept {
161 return getVariant(std::move(dv)).transform([](Variant&& var) {
162 assert(var.isType<uint32_t>());
163 assert(var.isArray());
164 return std::move(var).getArrayCopy<uint32_t>();
165 });
166 }
167
169 return DataValue::fromArray(dimensions);
170 }
171};
172
173template <>
175
176template <>
179
180template <>
182
183template <>
185
186template <>
188
189template <>
191
192template <>
194
195} // namespace opcua::services::detail
Represents a bad result stored in Result.
Definition result.hpp:17
Bitmask using (scoped) enums.
Definition bitmask.hpp:125
std::underlying_type_t< T > Underlying
Definition bitmask.hpp:130
constexpr Underlying get() const noexcept
Get the bitmask as the underlying type (integer).
Definition bitmask.hpp:154
UA_DataType wrapper class.
Definition datatype.hpp:27
UA_DataValue wrapper class.
Definition types.hpp:1478
bool hasValue() const noexcept
Definition types.hpp:1525
static DataValue fromScalar(Args &&... args)
Create DataValue from scalar value.
Definition types.hpp:1514
static DataValue fromArray(Args &&... args)
Create DataValue from array.
Definition types.hpp:1521
UA_NodeId wrapper class.
Definition types.hpp:590
The template class Result encapsulates a StatusCode and optionally a value.
Definition result.hpp:53
View to a contiguous sequence of objects, similar to std::span in C++20.
Definition span.hpp:26
UA_Variant wrapper class.
Definition types.hpp:887
void * data() noexcept
Get pointer to the underlying data.
Definition types.hpp:1007
Result< Variant > getVariant(DataValue &&dv) noexcept
NodeClass
Node class.
Definition common.hpp:138
AccessLevel
Access level.
Definition common.hpp:161
EventNotifier
Event notifier.
Definition common.hpp:241
ValueRank
Value rank.
Definition common.hpp:224
WriteMask
Write mask.
Definition common.hpp:183
AttributeId
Attribute identifiers.
Definition common.hpp:28
@ UserExecutable
Indicates if the method is currently executable taking user access rights into account.
@ Historizing
Indicates whether the server is actively collecting data for the history of the variable.
@ Executable
Indicates if the method is currently executable.
@ DisplayName
The localized name of the node.
@ BrowseName
A non-localised human-readable name used to browse the address space.
@ Value
The most recent value of the variable that the server has.
@ MinimumSamplingInterval
Specifies (in milliseconds) how fast the server can reasonably sample the value for changes.
@ IsAbstract
If a reference is abstract, no reference of this type shall exist, only of its subtypes.
@ InverseName
The inverse name describes the reference type as seen from the target node.
@ Description
Explains the meaning of the node in a localized text.
@ UserWriteMask
Exposes the possibilities of a client to write the attributes of the node.
@ ArrayDimensions
Specifies the maximum supported length of each dimension of the Value attribute.
@ ContainsNoLoops
Indicates that by following the references in the context of the view there are no loops.
@ UserAccessLevel
Indicates how the value of a variable can be accessed (read/write) and if it contains current and/or ...
@ DataTypeDefinition
Provides the meta data and encoding information for custom data types.
@ Symmetric
If a reference is symmetric, it can seen from both the source and target node.
#define UA_STATUSCODE_BADUNEXPECTEDERROR
static Result< Type > fromDataValue(DataValue &&dv) noexcept
static Result< Type > fromDataValue(DataValue &&dv) noexcept
static DataValue toDataValue(const Variant &value) noexcept
static Result< Variant > fromDataValue(DataValue &&dv) noexcept
static DataValue toDataValue(Span< const uint32_t > dimensions)
Attribute handler to convert DataValue objects to/from the attribute specific types.
UA_VARIANT_DATA_NODELETE