open62541pp 0.19.0
C++ wrapper of open62541
|
Wrapper types in open62541pp provide a modern C++ interface to native open62541 objects. They maintain full compatibility with native types while adding features like RAII and pointer interconvertibility.
All wrapper classes are based on opcua::Wrapper.
The native object can be accessed in two ways:
The template class opcua::Wrapper is pointer-interconvertible with the wrapped type.
Pointer interconvertibility enables lightweight casting between wrapper and native types without additional overhead. This is achieved by ensuring the wrapper class is a standard-layout type, as required by the C++ standard.
Following function allow seamless conversions between wrapper and native objects:
Implicit conversions to the native object are provided for convenience:
When wrapping composed types with nested members, getters and setters are essential for accessing these members as wrapper types. Let's walk through an example to demonstrate how to implement such functionality.
In this case, the struct contains heap-allocated members (nodeId
, indexRange
, and dataEncoding
). To manage such members safely, our wrapper should inherit from opcua::Wrapper and use the opcua::TypeHandlerNative policy, which provides appropriate handling for native open62541 types. The opcua::WrapperNative alias simplifies this by avoiding the need to repeat template parameters.
A minimal wrapper implementation looks like this:
We can now use the wrapper like this:
To handle nested members like nodeId
, we can use the opcua::asWrapper helper function to access these members as their corresponding wrapper types. Below is the enhanced wrapper class with getter and setter methods:
With these getters and setters, accessing and modifying nodeId
becomes straightforward: