open62541pp 0.19.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
Public Member Functions | List of all members
opcua::Variant Class Reference

#include <types.hpp>

Inheritance diagram for opcua::Variant:
[legend]

Detailed Description

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:

  1. 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:

    opcua::Variant var{5}; // set scalar (via constructor)
    auto value = var.to<int>(); // convert scalar
    std::array<int> array{1, 2, 3};
    var.assign(array); // set array (via assign)
    auto vec = var.to<std::vector<int>>(); // convert array
    UA_Variant wrapper class.
    Definition types.hpp:1048
    T to() const &
    Converts the variant to the specified type T with automatic conversion if required.
    Definition types.hpp:1374
    Span< T > array()
    Get array with given template type (only native or wrapper types).
    Definition types.hpp:1338
  2. Type definition retrieval: Automatically retrieves UA_DataType via TypeRegistry. For every registered type T, the type definition parameter can be omitted:

    opcua::Variant var{5, UA_TYPES[UA_TYPES_INT]}; // explicit type definition
    opcua::Variant var{5}; // auto-detected type definition
    const UA_DataType UA_TYPES[191]
  3. 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:

    opcua::Variant var{std::string{"test"}}; // convert to native type (copy)
    auto& native = var.scalar<opcua::String>(); // reference to native type (no copy)
    auto str = var.to<std::string>(); // conversion (copy required)
    UA_String wrapper class.
    Definition types.hpp:256
    T & scalar() &
    Get scalar value with given template type (only native or wrapper types).
    Definition types.hpp:1311
    constexpr const T & native() const noexcept
    Definition wrapper.hpp:243
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.

Public Member Functions

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
 
Modifiers

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>>>
Variantoperator= (T *value) noexcept
 
template<typename T , typename = std::enable_if_t<!isVariant<T>>>
Variantoperator= (T &&value)
 
Observers

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_DataTypetype () const noexcept
 
size_t arrayLength () const noexcept
 
Span< const uint32_t > arrayDimensions () const noexcept
 
Accessors

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 >
scalar () &&
 
template<typename T >
scalar () const &&
 
template<typename T >
Span< T > array ()
 
template<typename T >
Span< const T > array () const
 
template<typename T >
to () const &
 
template<typename T >
to () &&
 
- Public Member Functions inherited from opcua::Wrapper< T, Handler >
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 Wrapperoperator= (const Wrapper &other)
 
constexpr Wrapperoperator= (const T &native)
 
constexpr Wrapperoperator= (Wrapper &&other) noexcept
 
constexpr Wrapperoperator= (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
 

Additional Inherited Members

- Public Types inherited from opcua::Wrapper< T, Handler >
using NativeType = T
 
using HandlerType = Handler
 
- Protected Member Functions inherited from opcua::Wrapper< T, Handler >
constexpr const T & native () const noexcept
 
constexpr T & native () noexcept
 
constexpr void clear () noexcept
 

Constructor & Destructor Documentation

◆ Variant() [1/6]

template<typename T , typename = std::enable_if_t<!std::is_const_v<T>>>
opcua::Variant::Variant ( T *  ptr)
inlineexplicitnoexcept

Create Variant from a pointer to a scalar/array (no copy).

See also
assign(T*)

Definition at line 1061 of file types.hpp.

◆ Variant() [2/6]

template<typename T , typename = std::enable_if_t<!std::is_const_v<T>>>
opcua::Variant::Variant ( T *  ptr,
const UA_DataType type 
)
inlinenoexcept

Create Variant from a pointer to a scalar/array with a custom data type (no copy).

See also
assign(T*, const UA_DataType&)

Definition at line 1068 of file types.hpp.

◆ Variant() [3/6]

template<typename T , typename = std::enable_if_t<!isVariant<T>>>
opcua::Variant::Variant ( T &&  value)
inlineexplicit

Create Variant from a scalar/array (copy/move).

See also
assign(T&&)

Definition at line 1075 of file types.hpp.

◆ Variant() [4/6]

template<typename T >
opcua::Variant::Variant ( T &&  value,
const UA_DataType type 
)
inline

Create Variant from a scalar/array with a custom data type (copy/move).

See also
assign(T&&, const UA_DataType&)

Definition at line 1082 of file types.hpp.

◆ Variant() [5/6]

template<typename InputIt >
opcua::Variant::Variant ( InputIt  first,
InputIt  last 
)
inline

Create Variant from a range of elements (copy).

See also
assign(InputIt, InputIt)

Definition at line 1089 of file types.hpp.

◆ Variant() [6/6]

template<typename InputIt >
opcua::Variant::Variant ( InputIt  first,
InputIt  last,
const UA_DataType type 
)
inline

Create Variant from a range of elements with a custom data type (copy).

See also
assign(InputIt, InputIt, const UA_DataType&)

Definition at line 1096 of file types.hpp.

Member Function Documentation

◆ array() [1/2]

template<typename T >
Span< T > opcua::Variant::array ( )
inline

Get array with given template type (only native or wrapper types).

Exceptions
BadVariantAccessIf the variant is not an array or not of type `T`.
Examples
custom_datatypes/client_custom_datatypes.cpp.

Definition at line 1338 of file types.hpp.

◆ array() [2/2]

template<typename T >
Span< const T > opcua::Variant::array ( ) const
inline

Get array with given template type (only native or wrapper types).

Exceptions
BadVariantAccessIf the variant is not an array or not of type `T`.

Definition at line 1345 of file types.hpp.

◆ arrayDimensions()

Span< const uint32_t > opcua::Variant::arrayDimensions ( ) const
inlinenoexcept

Get array dimensions.

Definition at line 1284 of file types.hpp.

◆ arrayLength()

size_t opcua::Variant::arrayLength ( ) const
inlinenoexcept

Get array length or 0 if variant is not an array.

Examples
typeconversion.cpp.

Definition at line 1279 of file types.hpp.

Referenced by array(), and array().

◆ assign() [1/8]

template<typename InputIt >
void opcua::Variant::assign ( InputIt  first,
InputIt  last 
)
inline

Assign range to variant (copy and convert if required).

Parameters
firstIterator to the beginning of the range.
lastIterator to the end of the range.
Template Parameters
InputItIterator of a container with native, wrapper or convertible elements.

Definition at line 1194 of file types.hpp.

◆ assign() [2/8]

template<typename InputIt >
void opcua::Variant::assign ( InputIt  first,
InputIt  last,
const UA_DataType type 
)
inline

Assign range to variant with custom data type (copy).

Parameters
firstIterator to the beginning of the range.
lastIterator to the end of the range.
typeCustom data type.
Template Parameters
InputItIterator of a container with native or wrapper elements.

Definition at line 1212 of file types.hpp.

◆ assign() [3/8]

void opcua::Variant::assign ( std::nullptr_t  ptr)
deletenoexcept

◆ assign() [4/8]

void opcua::Variant::assign ( std::nullptr_t  ptr,
const UA_DataType type 
)
deletenoexcept

◆ assign() [5/8]

template<typename T >
void opcua::Variant::assign ( T &&  value)
inline

Assign scalar/array to variant (copy/move and convert if required).

Parameters
valueValue to copy to the variant. It can be:
  • A scalar native, wrapper or convertible value.
  • A container with native, wrapper or convertible elements. The container must implement begin() and end().

Definition at line 1155 of file types.hpp.

◆ assign() [6/8]

template<typename T >
void opcua::Variant::assign ( T &&  value,
const UA_DataType type 
)
inline

Assign scalar/array to variant with custom data type (copy/move).

Parameters
valueValue to copy to the variant. It can be:
  • A scalar native or wrapper value.
  • A container with native or wrapper elements. The container must implement begin() and end().
typeCustom data type.

Definition at line 1178 of file types.hpp.

◆ assign() [7/8]

template<typename T , typename = std::enable_if_t<!std::is_const_v<T>>>
void opcua::Variant::assign ( T *  ptr)
inlinenoexcept

Assign pointer to scalar/array to variant (no copy).

The object will not be deleted when the Variant is destructed.

Parameters
ptrNon-const pointer to a value to assign to the variant. This can be:
  • A pointer to a scalar native or wrapper value.
  • A pointer to a contiguous container such as std::array or std::vector holding native or wrapper elements. The underlying array must be accessible with std::data and std::size.
  • A nullptr, in which case the variant will be cleared.

Definition at line 1120 of file types.hpp.

◆ assign() [8/8]

template<typename T , typename = std::enable_if_t<!std::is_const_v<T>>>
void opcua::Variant::assign ( T *  ptr,
const UA_DataType type 
)
inlinenoexcept

Assign pointer to scalar/array to variant with custom data type (no copy).

The object will not be deleted when the Variant is destructed.

Parameters
ptrNon-const pointer to a value to assign to the variant. This can be:
  • A pointer to a scalar native or wrapper value.
  • A pointer to a contiguous container such as std::array or std::vector holding native or wrapper elements. The underlying array must be accessible with std::data and std::size.
  • A nullptr, in which case the variant will be cleared.
typeCustom data type.

Definition at line 1137 of file types.hpp.

◆ data() [1/2]

const void * opcua::Variant::data ( ) const
inlinenoexcept

Get pointer to the underlying data.

Check the properties and data type before casting it to the actual type. Use the methods isScalar, isArray, isType / type.

Note
The UA_EMPTY_ARRAY_SENTINEL sentinel, used to indicate an empty array, is stripped from the returned pointer.

Definition at line 1304 of file types.hpp.

◆ data() [2/2]

void * opcua::Variant::data ( )
inlinenoexcept

Get pointer to the underlying data.

Check the properties and data type before casting it to the actual type. Use the methods isScalar, isArray, isType / type.

Note
The UA_EMPTY_ARRAY_SENTINEL sentinel, used to indicate an empty array, is stripped from the returned pointer.
Examples
custom_datatypes/client_custom_datatypes.cpp.

Definition at line 1299 of file types.hpp.

Referenced by array(), array(), data(), data(), scalar(), and scalar().

◆ empty()

bool opcua::Variant::empty ( ) const
inlinenoexcept

Check if the variant is empty.

Definition at line 1238 of file types.hpp.

Referenced by isArray().

◆ isArray()

bool opcua::Variant::isArray ( ) const
inlinenoexcept

Check if the variant is an array.

Examples
custom_datatypes/client_custom_datatypes.cpp.

Definition at line 1248 of file types.hpp.

◆ isScalar()

bool opcua::Variant::isScalar ( ) const
inlinenoexcept

Check if the variant is a scalar.

Examples
custom_datatypes/client_custom_datatypes.cpp.

Definition at line 1243 of file types.hpp.

Referenced by isArray().

◆ isType() [1/4]

template<typename T >
bool opcua::Variant::isType ( ) const
inlinenoexcept

Check if the variant type is equal to the provided template type.

Definition at line 1269 of file types.hpp.

Referenced by isType(), isType(), and isType().

◆ isType() [2/4]

bool opcua::Variant::isType ( const NodeId id) const
inlinenoexcept

Check if the variant type is equal to the provided data type id.

Definition at line 1263 of file types.hpp.

◆ isType() [3/4]

bool opcua::Variant::isType ( const UA_DataType type) const
inlinenoexcept

Check if the variant type is equal to the provided data type.

Definition at line 1258 of file types.hpp.

◆ isType() [4/4]

bool opcua::Variant::isType ( const UA_DataType type) const
inlinenoexcept

Check if the variant type is equal to the provided data type.

Examples
custom_datatypes/client_custom_datatypes.cpp.

Definition at line 1253 of file types.hpp.

◆ operator=() [1/2]

template<typename T , typename = std::enable_if_t<!isVariant<T>>>
Variant & opcua::Variant::operator= ( T &&  value)
inline

Assign scalar/array to variant (copy and convert if required).

See also
assign(T&&)

Definition at line 1227 of file types.hpp.

◆ operator=() [2/2]

template<typename T , typename = std::enable_if_t<!isVariant<T>>>
Variant & opcua::Variant::operator= ( T *  value)
inlinenoexcept

Assign pointer to scalar/array to variant (no copy).

See also
assign(T*)

Definition at line 1219 of file types.hpp.

◆ scalar() [1/4]

template<typename T >
T & opcua::Variant::scalar ( ) &
inline

Get scalar value with given template type (only native or wrapper types).

Exceptions
BadVariantAccessIf the variant is not a scalar or not of type `T`.
Examples
custom_datatypes/client_custom_datatypes.cpp, method/client_method_async.cpp, server_valuecallback.cpp, and typeconversion.cpp.

Definition at line 1311 of file types.hpp.

◆ scalar() [2/4]

template<typename T >
T opcua::Variant::scalar ( ) &&
inline

Get scalar value with given template type (only native or wrapper types).

Exceptions
BadVariantAccessIf the variant is not a scalar or not of type `T`.

Definition at line 1325 of file types.hpp.

◆ scalar() [3/4]

template<typename T >
const T & opcua::Variant::scalar ( ) const &
inline

Get scalar value with given template type (only native or wrapper types).

Exceptions
BadVariantAccessIf the variant is not a scalar or not of type `T`.

Definition at line 1318 of file types.hpp.

◆ scalar() [4/4]

template<typename T >
T opcua::Variant::scalar ( ) const &&
inline

Get scalar value with given template type (only native or wrapper types).

Exceptions
BadVariantAccessIf the variant is not a scalar or not of type `T`.

Definition at line 1331 of file types.hpp.

◆ to() [1/2]

template<typename T >
T opcua::Variant::to ( ) &&
inline

Converts the variant to the specified type T with automatic conversion if required.

Determines the type category (scalar or array) based on the characteristics of T using type category detection. If T is a container, it must be constructible from an iterator pair.

// Scalar
const auto value = var.to<int>();
// Array
std::array<std::string, 3> array{"One", "Two", "Three"};
const auto vec = var.to<std::vector<std::string>>();
const auto lst = var.to<std::list<opcua::String>>();
Exceptions
BadVariantAccessIf the variant is not convertible to `T`.

Definition at line 1380 of file types.hpp.

◆ to() [2/2]

template<typename T >
T opcua::Variant::to ( ) const &
inline

Converts the variant to the specified type T with automatic conversion if required.

Determines the type category (scalar or array) based on the characteristics of T using type category detection. If T is a container, it must be constructible from an iterator pair.

// Scalar
const auto value = var.to<int>();
// Array
std::array<std::string, 3> array{"One", "Two", "Three"};
const auto vec = var.to<std::vector<std::string>>();
const auto lst = var.to<std::list<opcua::String>>();
Exceptions
BadVariantAccessIf the variant is not convertible to `T`.
Examples
server.cpp, server_datasource.cpp, and typeconversion.cpp.

Definition at line 1374 of file types.hpp.

◆ type()

const UA_DataType * opcua::Variant::type ( ) const
inlinenoexcept

Get data type.

Definition at line 1274 of file types.hpp.

Referenced by assign(), assign(), assign(), isType(), isType(), isType(), Variant(), Variant(), and Variant().

◆ Wrapper() [1/5]

constexpr opcua::Wrapper< T, Handler >::Wrapper ( )
constexprdefaultnoexcept

◆ Wrapper() [2/5]

constexpr opcua::Wrapper< T, Handler >::Wrapper ( const T &  native)
inlineexplicitconstexpr

Copy constructor with native object.

Definition at line 151 of file wrapper.hpp.

◆ Wrapper() [3/5]

constexpr opcua::Wrapper< T, Handler >::Wrapper ( const Wrapper other)
inlineconstexpr

Copy constructor.

Definition at line 147 of file wrapper.hpp.

◆ Wrapper() [4/5]

constexpr opcua::Wrapper< T, Handler >::Wrapper ( T &&  native)
inlineconstexprnoexcept

Move constructor with native object.

Definition at line 159 of file wrapper.hpp.

◆ Wrapper() [5/5]

constexpr opcua::Wrapper< T, Handler >::Wrapper ( Wrapper &&  other)
inlineconstexprnoexcept

Move constructor.

Definition at line 155 of file wrapper.hpp.