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
| Members | Category | Reading and Writing | Note |
|---|---|---|---|
| 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_index | Field | Reading and Writing | uint16_t |
| nid.type | Field | Reading and Writing | NodeIdType (The field name is type, not id_type) |
| nid.identifier | Field | Reading and Writing | std::variant<uint32_t, std::string, std::array<uint8_t,16>, std::vector<uint8_t>> |
Related Structures — NodeIdType
| NodeIdType | Value | Coding Prefix | The type stored in the identifier |
|---|---|---|---|
| Numeric | 0 | i= | uint32_t |
| String | 1 | s= | std::string (UTF-8) |
| Guid | 2 | g= | std::array<uint8_t,16> |
| Opaque | 3 | b= | std::vector<uint8_t> (base64) |
String Encoding Rules (As Specified ToString/ Actual TryParse Behavior):
namespace_index == 0TimeToString()Omittedns=0;Prefix- Guid The output is
g=+36 hex characters, Without curly braces;ParseThe parentheses are optional Parse/TryParseThens=Nprefix 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
Writebefore the "Session::Read/"ToString() - Numeric Priority (identifier-based
uint32_t, most common) - User input always comes first
TryParse(returnsstd::optional, does not throw) - When creating a map or set key
ToString(), use a comma; don't expect theNodeIdbuilt-in comparison functionality.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| OpcUaNodeId | OpcUaNodeId | OpcUaNodeId | NodeId | OpcUaNodeId | DarraUa_OwnedNodeId |
| Parse | parse | parse | NodeId::Parse | parse | DarraUa_NodeId_Parse |
| FromNumeric | from_numeric | fromNumeric | NodeId::FromNumeric | from_numeric | DarraUa_NodeId_FromNumeric |