NodeId — 強型 NodeId
概要
OPC UA ノードIDはデフォルトで文字列エンコード("ns=2;s=Foo")されます。NodeId これは通常の値型(struct、パブリックフィールドは直接読み書き可能)であり、明示的にnamespace_index / type / identifierの三要素を含み、文字列エンコードと相互変換が可能です。
該当する規格の節:第6部 §5.2.2。
ヘッダーファイル
#include <darra/opcua/node_id.hpp>
using namespace darra::opcua;
API
| メンバー | カテゴリ | 読み書き | 説明 |
|---|---|---|---|
| NodeId::Null() | 静的ファクトリ | — | 構造体空 NodeId (i=0), これは関数であり、定数ではありません。 |
| NodeId::FromNumeric(uint16_t ns, uint32_t id) | 静的ファクトリ | — | デジタル型 |
| NodeId::FromString(uint16_t ns, std::string id) | 静的ファクトリ | — | 文字列型 |
NodeId::FromGuid(uint16_t ns, std::array<uint8_t,16> guid_bytes) | 静的ファクトリ | — | GUID 型(16バイトのバイナリデータ、UUID文字列ではない) |
NodeId::FromOpaque(uint16_t ns, std::vector<uint8_t> bytes) | 静的ファクトリ | — | 不透明タイプ |
| NodeId::Parse(std::string const& s) | 静的 | — | 解析、失敗時の例外処理 std::invalid_argument |
| NodeId::TryParse(std::string const& s) | 静的 | — | セキュリティ解析、std::optional<NodeId> を返す、 bool + out パラメータではない |
| nid.ToString() | 方法 | — | 標準文字列としてシリアライズする |
| nid.IsNull() | 方法 | — | 空かどうか NodeId |
| nid.namespace_index | フィールド | 読み書き | uint16_t |
| nid.type | フィールド | 読み書き | NodeIdType (フィールド名は type で、id_type ではない) |
| nid.identifier | フィールド | 読み書き | std::variant<uint32_t, std::string, std::array<uint8_t,16>, std::vector<uint8_t>> |
関連構造体 — NodeIdType
| NodeIdType | 値 | エンコードプレフィックス | 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) |
文字列のエンコード規則(仕様 ToString / TryParse 実際の動作):
namespace_index == 0時ToString()省略ns=0;接頭辞- Guid 出力は
g=+ 36 文字の十六進数、 中括弧なし;Parse時は中括弧があってもなくても構いません Parse/TryParseのns=N接頭辞は省略可能(省略した場合は ns=0 となる)、番号の接頭辞は大文字・小文字を区別しない
既知の制限: node_id.hpp は NodeId に operator< / operator== を定義しておらず、std::hash 特化 (C++17 集約 struct) もありません。そのため std::map / std::unordered_map のキーには直接使えません —— ToString() をキーにしてください。
well-known パッケージが組み込まれています: NodeId::Well は NodeId const& を返す静的メソッド (Well::RootFolder() / Well::ObjectsFolder() / Well::Server() / Well::ServerStatus() / Well::CurrentTime() / Well::Organizes() など) については、以下を参照してください。 Well-Known Nodes。
コード例
#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) 工場(ns は uint16_t、ID番号は 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>{ // 16バイトのプレーンテキスト、UUIDではない文字列
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) 解析 — namespace_index / type / identifier はパブリックフィールドであるため、直接値を取得する
NodeId n5 = NodeId::Parse("ns=2;s=Demo.Tag");
std::cout << "ns=" << n5.namespace_index
<< ", type=" << static_cast<int>(n5.type) << "\n";
// TryParse は std::optional<NodeId> を返す, bool + out パラメータではない
if (auto n6 = NodeId::TryParse("ns=99;i=foo"); !n6)
std::cout << "invalid\n";
// 3) シリアライズ(ns=0 の場合は「ns=0;」というプレフィックスを省略)
std::string s = n2.ToString(); // "ns=2;s=Boiler1.Temp"
// 4) Session との相互運用性 — Session::Read/Write は文字列 NodeId のみを受け付ける
Stack::Init();
Session ua("opc.tcp://localhost:4840");
ua.Connect();
DataValue dv = ua.Read(n2.ToString());
// 5) map のキーとして — NodeId に operator< や std::hash がない場合は、ToString() をキーとして使用する
std::map<std::string, std::string> dict;
dict[n2.ToString()] = "锅炉一温度";
// 6) 空 NodeId — Null() は静的関数であり、定数ではない
std::cout << NodeId::Null().IsNull() << "\n"; // 1
ベストプラクティス
- プロファイルには文字列で保存し、読み込みには
Parseを使用する - コード内部では強型を使い、
Session::Read/Writeに引数を渡す前にToString()を呼ぶ - Numeric 優先(識別子は
uint32_t、最も一般的) - ユーザー入力は常に優先される
TryParse(戻り値std::optional、例外は発生しない) - map / set のキーには
ToString()を使い、組み込みNodeIdの比較機能に頼らないでください。
多言語対照
| 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 |