Skip to main content

NodeId — Strongly typed NodeId

Overview​

OPC UA node IDs are string-encoded by default ("ns=2;s=Foo"). NodeId is an ordinary value type (a struct whose public fields can be read and written directly), explicitly carrying namespace_index / type / identifier, and it converts freely to and from the string encoding.

Corresponding section of the standard: Part 6 §5.2.2.

Header Files​

#include <darra/opcua/node_id.hpp>
using namespace darra::opcua;

API​

MembersCategoryReading and WritingNote
NodeId::Null()Static Factory—Construct an empty NodeId (i=0), It is a function, not a constant.
NodeId::FromNumeric(uint16_t ns, uint32_t id)Static Factory—Digital
NodeId::FromString(uint16_t ns, std::string id)Static Factory—String type
NodeId::FromGuid(uint16_t ns, std::arraystd::array<uint8_t,16> guid_bytes)Static Factory—GUID Type (raw 16 bytes, not a UUID string)
NodeId::FromOpaque(uint16_t ns, std::vectorstd::vector<uint8_t> bytes)Static Factory—Opaque Type
NodeId::Parse(std::string const& s)Static—Parse, throw on failure std::invalid_argument
NodeId::TryParse(std::string const& s)Static—Security Analysis, Returns std::optional<NodeId>, Not a bool + out parameter
nid.ToString()Methods—Serialize to a standard string
nid.IsNull()Methods—Is it empty? NodeId
nid.namespace_indexFieldReading and Writinguint16_t
nid.typeFieldReading and WritingNodeIdType (The field name is type, not id_type)
nid.identifierFieldReading and Writingstd::variant<uint32_t, std::string, std::array<uint8_t,16>, std::vector<uint8_t>>
NodeIdTypeValueCoding PrefixThe type stored in the identifier
Numeric0i=uint32_t
String1s=std::string (UTF-8)
Guid2g=std::array<uint8_t,16>
Opaque3b=std::vector<uint8_t> (base64)

String Encoding Rules (As Specified ToString/ Actual TryParse Behavior):

  • namespace_index == 0 Time ToString() Omittedns=0; Prefix
  • Guid The output is g=+36 hex characters, Without curly braces; Parse The parentheses are optional
  • Parse / TryParse The ns=N prefix can be omitted (omitting it is equivalent to ns0); the prefix is case-insensitive.

Known Limitations: node_id.hpp There is no definition NodeId for operator</ operator==, nor is there a specialization std::hash (C++17 aggregate struct), so it cannot be used directly as a key std::unordered_map for std::map / — use ToString() as the key instead.

Built-in well-known packaging: NodeId::Well Provides static methods NodeId const& that return (Well::RootFolder() / Well::ObjectsFolder() / Well::Server() / Well::ServerStatus() / Well::CurrentTime() / Well::Organizes() etc.); see Well-Known Nodes。

Code Example​

#include <darra/opcua/node_id.hpp>
#include <darra/opcua/client.hpp>
#include <array>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace darra::opcua;

// 1) Factory (ns is uint16_t, the numeric ID is uint32_t)
NodeId n1 = NodeId::FromNumeric(0, 84); // i=84
NodeId n2 = NodeId::FromString(2, "Boiler1.Temp"); // ns=2;s=Boiler1.Temp
NodeId n3 = NodeId::FromGuid(3, std::array<uint8_t, 16>{ // Raw 16-byte, non-UUID string
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10});
NodeId n4 = NodeId::FromOpaque(4, std::vector<uint8_t>{1, 2, 3});

// 2) Explanation — namespace_index / type / identifier is a public field; its value is retrieved directly
NodeId n5 = NodeId::Parse("ns=2;s=Demo.Tag");
std::cout << "ns=" << n5.namespace_index
<< ", type=" << static_cast<int>(n5.type) << "\n";

// TryParse Returns std::optional<NodeId>, not a `bool` + `out` parameter
if (auto n6 = NodeId::TryParse("ns=99;i=foo"); !n6)
std::cout << "invalid\n";

// 3) Serialization (nsomits the "ns=0;" prefix when the value is 0)
std::string s = n2.ToString(); // "ns=2;s=Boiler1.Temp"

// 4) Interoperability with Session — Session::Read/Write Accepts only strings NodeId
Stack::Init();
Session ua("opc.tcp://localhost:4840");
ua.Connect();
DataValue dv = ua.Read(n2.ToString());

// 5) As a map key — no NodeId operator< or std::hash; use ToString() as the key
std::map<std::string, std::string> dict;
dict[n2.ToString()] = "锅炉一温度";

// 6) Null NodeId — Null() is a static function, not a constant
std::cout << NodeId::Null().IsNull() << "\n"; // 1

Best Practices​

  • Store strings in the configuration file; use to load them Parse
  • Use strong typing within the code; pass it to the call Write before the "Session::Read/" ToString()
  • Numeric Priority (identifier-based uint32_t, most common)
  • User input always comes first TryParse (returns std::optional, does not throw)
  • When creating a map or set key ToString(), use a comma; don't expect the NodeId built-in comparison functionality.

Cross-Language Comparison​

C#PythonJavaC++RustC
OpcUaNodeIdOpcUaNodeIdOpcUaNodeIdNodeIdOpcUaNodeIdDarraUa_OwnedNodeId
ParseparseparseNodeId::ParseparseDarraUa_NodeId_Parse
FromNumericfrom_numericfromNumericNodeId::FromNumericfrom_numericDarraUa_NodeId_FromNumeric

Next Step​