UA_Variant wrapper class.
Variants may contain scalar values or arrays of any type together with a type definition. The standard mandates that variants contain built-in data types only. open62541 transparently handles this wrapping in the encoding layer. If the type is unknown to the receiver, the variant stores the original ExtensionObject in binary or XML encoding.
open62541pp enhances variant handling with the following features:
Type category detection: Identifies whether T
is scalar or array:
- Scalar, if
T
is:
- Array, if
T
is a container type (e.g. std::vector
or std::list
) and does not satisfy the criterias of a scalar type.
Applied in constructors, assign, and to functions:
auto value = var.
to<
int>();
std::array<int>
array{1, 2, 3};
auto vec = var.to<std::vector<int>>();
UA_Variant wrapper class.
T to() const &
Converts the variant to the specified type T with automatic conversion if required.
Span< T > array()
Get array with given template type (only native or wrapper types).
Type definition retrieval: Automatically retrieves UA_DataType via TypeRegistry. For every registered type T
, the type definition parameter can be omitted:
const UA_DataType UA_TYPES[191]
Type conversion: Convert non-native types using TypeConverter. Native UA_*
types can be assigned and retrieved to/from variants without any conversion, because their binary layout is described by the type definition (UA_DataType). The same is true for wrapper types, that share the exact memory layout as their wrapped native type. Non-native types, like std::string
from the STL, may not be describeable by UA_DataType because their memory layout is an implementation detail. Instead, the conversion between non-native and native types can be defined with template specializations of TypeConverter. If a type is convertible (TypeConverter specialization), the Variant automatically manages the conversion, requiring a copy:
auto str = var.to<std::string>();
T & scalar() &
Get scalar value with given template type (only native or wrapper types).
constexpr const T & native() const noexcept
- Examples
- custom_datatypes/client_custom_datatypes.cpp, custom_datatypes/server_custom_datatypes.cpp, method/client_method_async.cpp, server.cpp, server_accesscontrol.cpp, server_datasource.cpp, server_instantiation.cpp, server_minimal.cpp, server_valuecallback.cpp, and typeconversion.cpp.
Definition at line 1048 of file types.hpp.
|
template<typename T , typename = std::enable_if_t<!std::is_const_v<T>>> |
| Variant (T *ptr) noexcept |
|
template<typename T , typename = std::enable_if_t<!std::is_const_v<T>>> |
| Variant (T *ptr, const UA_DataType &type) noexcept |
|
template<typename T , typename = std::enable_if_t<!isVariant<T>>> |
| Variant (T &&value) |
|
template<typename T > |
| Variant (T &&value, const UA_DataType &type) |
|
template<typename InputIt > |
| Variant (InputIt first, InputIt last) |
|
template<typename InputIt > |
| Variant (InputIt first, InputIt last, const UA_DataType &type) |
|
constexpr | Wrapper () noexcept=default |
|
constexpr | Wrapper (const Wrapper &other) |
|
constexpr | Wrapper (const T &native) |
|
constexpr | Wrapper (Wrapper &&other) noexcept |
|
constexpr | Wrapper (T &&native) noexcept |
|
|
Modify internal scalar/array value.
|
void | assign (std::nullptr_t ptr) noexcept=delete |
|
void | assign (std::nullptr_t ptr, const UA_DataType &type) noexcept=delete |
|
template<typename T , typename = std::enable_if_t<!std::is_const_v<T>>> |
void | assign (T *ptr) noexcept |
|
template<typename T , typename = std::enable_if_t<!std::is_const_v<T>>> |
void | assign (T *ptr, const UA_DataType &type) noexcept |
|
template<typename T > |
void | assign (T &&value) |
|
template<typename T > |
void | assign (T &&value, const UA_DataType &type) |
|
template<typename InputIt > |
void | assign (InputIt first, InputIt last) |
|
template<typename InputIt > |
void | assign (InputIt first, InputIt last, const UA_DataType &type) |
|
template<typename T , typename = std::enable_if_t<!isVariant<T>>> |
Variant & | operator= (T *value) noexcept |
|
template<typename T , typename = std::enable_if_t<!isVariant<T>>> |
Variant & | operator= (T &&value) |
|
|
Check the type category, type definition and array structure of the internal value.
|
bool | empty () const noexcept |
|
bool | isScalar () const noexcept |
|
bool | isArray () const noexcept |
|
bool | isType (const UA_DataType *type) const noexcept |
|
bool | isType (const UA_DataType &type) const noexcept |
|
bool | isType (const NodeId &id) const noexcept |
|
template<typename T > |
bool | isType () const noexcept |
|
const UA_DataType * | type () const noexcept |
|
size_t | arrayLength () const noexcept |
|
Span< const uint32_t > | arrayDimensions () const noexcept |
|
|
Access and convert internal scalar/array value.
|
void * | data () noexcept |
|
const void * | data () const noexcept |
|
template<typename T > |
T & | scalar () & |
|
template<typename T > |
const T & | scalar () const & |
|
template<typename T > |
T | scalar () && |
|
template<typename T > |
T | scalar () const && |
|
template<typename T > |
Span< T > | array () |
|
template<typename T > |
Span< const T > | array () const |
|
template<typename T > |
T | to () const & |
|
template<typename T > |
T | to () && |
|
constexpr | Wrapper () noexcept=default |
|
constexpr | Wrapper (const Wrapper &other) |
|
constexpr | Wrapper (const T &native) |
|
constexpr | Wrapper (Wrapper &&other) noexcept |
|
constexpr | Wrapper (T &&native) noexcept |
|
| ~Wrapper () noexcept=default |
|
constexpr Wrapper & | operator= (const Wrapper &other) |
|
constexpr Wrapper & | operator= (const T &native) |
|
constexpr Wrapper & | operator= (Wrapper &&other) noexcept |
|
constexpr Wrapper & | operator= (T &&native) noexcept |
|
constexpr | operator T& () noexcept |
|
constexpr | operator const T & () const noexcept |
|
constexpr T * | operator-> () noexcept |
|
constexpr const T * | operator-> () const noexcept |
|
constexpr T * | handle () noexcept |
|
constexpr const T * | handle () const noexcept |
|
constexpr void | swap (Wrapper &other) noexcept |
|
constexpr void | swap (T &native) noexcept |
|
|
using | NativeType = T |
|
using | HandlerType = Handler |
|
constexpr const T & | native () const noexcept |
|
constexpr T & | native () noexcept |
|
constexpr void | clear () noexcept |
|
template<typename T > |
constexpr T * | asWrapper (typename T::NativeType *native) noexcept |
|
template<typename T > |
constexpr const T * | asWrapper (const typename T::NativeType *native) noexcept |
|
template<typename T > |
constexpr T & | asWrapper (typename T::NativeType &native) noexcept |
|
template<typename T > |
constexpr const T & | asWrapper (const typename T::NativeType &native) noexcept |
|
template<typename T > |
constexpr T::NativeType * | asNative (T *wrapper) noexcept |
|
template<typename T > |
constexpr const T::NativeType * | asNative (const T *wrapper) noexcept |
|
template<typename T > |
constexpr T::NativeType & | asNative (T &wrapper) noexcept |
|
template<typename T > |
constexpr const T::NativeType & | asNative (const T &wrapper) noexcept |
|