open62541pp 0.16.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 derive from opcua::Wrapper. The template type opcua::TypeWrapper extends opcua::Wrapper class to safely copy, move and delete native types with heap-allocated memory.
The native object can be accessed in two ways:
Both opcua::Wrapper and opcua::TypeWrapper are pointer-interconvertible with the wrapped native 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.
Two objects
a
andb
are pointer-interconvertible if: One is a standard-layout class object [wrapper] and the other is the first non-static data member of that object [wrapped native type]. Derived classes must fulfill the requirements of standard-layout types to be convertible.
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 these members correctly, our wrapper should inherit from opcua::TypeWrapper.
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: