Skip to main content

Variant

darra::opcua::Variant This is a generic OPC UA value container SDK used to hold all values that can be read and written. It supports reading and writing the following values: Boolean / SByte / Byte / Int16 / UInt16 / Int32 / UInt32 / Int64 / Float / Double / String / DateTime. Move-only, Automatic release upon destructor execution.

Supporting

Public Properties (Accessors)​

CategoryPropertyTypeAccessDescription
StatusIsScalar()boolRIs it a scalar?
IsArray()boolRIs an array?
TypeDataType()DarraUa_BuiltinTypeREnumeration of built-in types
Handle()DarraUa_Variant*RC-layer handle (for internal use)

Construction (Default + Borrowing)​

Variant v;                            // empty Variant, owns=true
Variant v2(handle, /*owns=*/false); // Borrowing a resource handle; not owning it

Setter (Chain Setter)​

Variant v;
v.SetBool(true); // Boolean
v.SetSByte(-1); // SByte
v.SetByte(255); // Byte
v.SetInt16(42); // Int16
v.SetUInt16(42); // UInt16
v.SetInt32(42); // Int32
v.SetUInt32(42); // UInt32
v.SetInt64(42LL); // Int64
v.SetFloat(3.14f); // Float
v.SetDouble(3.14); // Double
v.SetString("hello"); // String
v.SetDateTime(file_time_utc); // DateTime (FileTime: 100ns since 1601)

Chained calls:

Variant v; v.SetDouble(3.14).SetString("override");  // The last one takes effect

Value Retrieval (Try* Safe Getter)​

bool b;        v.TryGetBool(b);
int8_t i8; v.TryGetSByte(i8);
uint8_t u8; v.TryGetByte(u8);
int16_t i16; v.TryGetInt16(i16);
uint16_t u16; v.TryGetUInt16(u16);
int32_t i32; v.TryGetInt32(i32);
uint32_t u32; v.TryGetUInt32(u32);
int64_t i64; v.TryGetInt64(i64);
double d; v.TryGetDouble(d);
float f; v.TryGetFloat(f);

std::string s = v.AsString(); // Returns "" on failure
int64_t ft = v.AsFileTime(); // FileTime, Returns 0 on failure

TryGet* Returns a value when there is a type mismatch false; does not throw an exception. C++ style encourages the use of try* instead of throwing exceptions.

Memory Ownership (RAII)​

Variant Holding a resource handle:

  • User-defined, default Variant v; constructor — owns=true, dtor automatically deletes
  • Borrowed s.Read(...).Value() from — owns=false, dtor does not release (DataValue holds)
  • From s.Call(...) the array containing — owns=true, the dtor automatically deletes

Best Practices — Always Use Stack Objects:

{
Variant v;
v.SetDouble(42.0);
s.Write("ns=2;s=Setpoint", v);
} // Out of scope: v automatically destroyed

// or temporary objects
Variant tmp;
tmp.SetInt32(123);
s.Write("ns=2;s=X", tmp);

Direct construction has only Variant two forms: the parameterless default constructor and the constructor that borrows a handle (explicit Variant(DarraUa_Variant* borrowed, bool owns = false) see “Construction (Default + Borrow)” above); the copy constructor has been deleted, and attempting a deep copy will result in a compilation error (see the next section for the move constructor).

Move Semantics​

Variant a; a.SetInt32(123);
Variant b = std::move(a); // OK, a Expires thereafter
// Variant c = a; // Compilation error: copy constructor deleted

You can put it in std::vector<Variant>, but you can only move it in:

std::vector<Variant> args;
{ Variant v; v.SetInt32(3); args.push_back(std::move(v)); }
{ Variant v; v.SetInt32(4); args.push_back(std::move(v)); }
auto outs = s.Call("ns=2;s=Calc", "ns=2;s=Calc.Add", args);

BuiltinType Enumeration​

UA_TYPE_NULL, UA_TYPE_BOOLEAN, UA_TYPE_SBYTE, UA_TYPE_BYTE,
UA_TYPE_INT16, UA_TYPE_UINT16, UA_TYPE_INT32, UA_TYPE_UINT32,
UA_TYPE_INT64, UA_TYPE_UINT64, UA_TYPE_FLOAT, UA_TYPE_DOUBLE,
UA_TYPE_STRING, UA_TYPE_DATETIME, UA_TYPE_GUID, UA_TYPE_BYTESTRING,
UA_TYPE_XML_ELEMENT, UA_TYPE_NODE_ID, UA_TYPE_EXPANDED_NODE_ID,
UA_TYPE_STATUS_CODE, UA_TYPE_QUALIFIED_NAME, UA_TYPE_LOCALIZED_TEXT,
UA_TYPE_EXTENSION_OBJECT, UA_TYPE_DATA_VALUE, UA_TYPE_VARIANT,
UA_TYPE_DIAGNOSTIC_INFO

Values range from 0 to 25; see opcua/opcua_types.h for details DarraUa_BuiltinType.

ExtensionObject(Complex Structure)​

OPC UA server-side custom Struct DataTypes are wrapped in ExtensionObjects. Variant currently exposes no ExtensionObject accessor; decoding of ExtensionObject binary data comes from the server-side DataTypeDefinition via StructDecoder — see StructDecoder.

Next Step​