構造体のデコード
概要
OPC UA 1.04+ 各カスタム構造体のフィールド定義を、DataType ノードの DataTypeDefinition 属性(AttributeId=27、Part 5 §6.4.2)に配置する。StructDecoder はサーバーから提供された StructureDefinition に基づき、ExtensionObject の UA Binary バイトストリーム (Part 6 §5.2) をフィールド辞書として逆シリアル化します。解決可能な Builtin の範囲と制限については、以下の「実装上の制限」を参照してください。
API
StructDecoder は darra::opcua 名前空間のクラスで、ヘッダーファイルは darra/opcua/struct_decoder.hpp。デコード結果は std::unordered_map<std::string, std::any> のフィールド辞書です — DynamicExtensionObject のようなラッパー型はない。フィールドの取得は map の operator[] です。
| メンバー | カテゴリ | 読み書き | 説明 |
|---|---|---|---|
| StructDecoder(Session& ua) | 構造 | — | セッションへのバインド (explicit) |
| decoder.Decode(variant) | 方法 | 読み取り可 | std::unordered_map<std::string, std::any> を返す |
| decoder.DecodeBody(body, data_type_node_id) | 方法 | 読み取り可 | body バイトストリーム + DataType NodeId の場合、直接デコードすることがわかっている |
| decoder.ClearCache() | 方法 | 書く | DataTypeDefinition キャッシュをクリアする |
| decoder.CachedDefinitionCount() | 方法 | 読み取り可 | キャッシュされた定義の数 |
| map[key] | 索引 | 読み取り可 | フィールドを取得し、その値は std::any |
| StructDecoder::kStatusKey | 定数 | — | "_status" — デコードに失敗 / プレースホルダーの結果のキー |
| StructDecoder::kTypeIdKey | 定数 | — | "_typeId" — プレースホルダーの結果を、TypeId をキーとして保存する |
| StructDecoder::kAttrDataTypeDefinition | 定数 | — | 27 — DataTypeDefinition の AttributeId |
関連する構造 (StructureKind, Part 3 §8.49 StructureType):
- Structure (0) — 通常
- StructureWithSubtypedValues (1) — サブタイプを含む
- StructureWithOptionalFields (2) — オプションフィールドを含む(先頭に EncodingMask あり)
- Union (3) — Union (前置セレクタ)
- UnionWithSubtypedValues (4) — ユニオン + サブタイプ
コード例
#include <darra/opcua/struct_decoder.hpp>
#include <any>
#include <iostream>
using namespace darra::opcua;
Stack::Init();
Session s("opc.tcp://localhost:4840");
s.Connect();
StructDecoder dec(s);
// 1) カスタム構造の読み込み — Decode は DataValue 内の Variant を受け取る
auto dv = s.Read("ns=2;s=Robot.CurrentPose");
auto pose = dec.Decode(dv.Value()); // std::unordered_map<std::string, std::any>
std::cout << "x = " << std::any_cast<double>(pose["X"]) << "\n";
std::cout << "y = " << std::any_cast<double>(pose["Y"]) << "\n";
// 2) 全フィールドの走査 — 結果は map となり、直接反復処理が可能
for (auto& [name, value] : pose)
std::cout << name << "\n";
// 3) ネストされた構造体のフィールドの値は std::unordered_map<std::string, std::any> です。
auto quat = std::any_cast<std::unordered_map<std::string, std::any>>(pose["Quaternion"]);
std::cout << "qw = " << std::any_cast<double>(quat["W"]) << "\n";
// 4) 配列フィールドの値は std::vector<std::any> である
auto arr = std::any_cast<std::vector<std::any>>(pose["JointAngles"]);
for (auto& a : arr)
std::cout << std::any_cast<double>(a) << "\n";
// 5) 他の経路からすでに EO body を入手している場合は、直接解く
std::vector<uint8_t> body = /* ... */;
auto manual = dec.DecodeBody(body, "ns=2;i=3001");
実装上の制約
- ExtensionObject のバイトを取得できない場合、Decode(Variant) は
_statusフィールドを含む部分的な結果を返します。 - 元のバイトデータが存在する場合は、 DecodeBody(bytes, type_id) を使用して直接デコードする
- フィールド定義は
LoadDefinitionから取得されます。このキャッシュの充填は現時点で未接続(常にnullptrを返す)なので、現段階ではDecodeBodyは空の map を返します — キャッシュ接続後もシグネチャは変わりません。 - Builtin でオーバーライドされていない型(例:DiagnosticInfo、i=25)は例外をスローし
std::runtime_error("Builtin type not implemented in StructDecoder")、黙ってスキップされることはない
ベストプラクティス
- デコーダーを共有し、同じサーバー、同じタイプの定義は一回だけ読み込む
- サーバー側の DataType 定義を変更した後、
ClearCache()を呼び出して再度デコードする - ネストされた構造の値は map なので、
std::any_cast<std::unordered_map<std::string, std::any>>で取得してからアクセスする - Union の selector を確認:デコード後はフィールドが一つしか残らないため、フィールド名に基づいてどの項目が選択されたかを判断する
多言語対照
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| StructDecoder | StructDecoder | StructDecoder | StructDecoder | StructDecoder | DarraUa_StructDecoderHandle |
| Decode | decode | decode | Decode | decode | DarraUa_StructDecoder_Decode |
| DecodeBody | decode_body | decodeBody | DecodeBody | decode_body | DarraUa_StructDecoder_DecodeBody |
| ClearCache | clear_cache | — | ClearCache | clear_cache | DarraUa_StructDecoder_ClearCache |
| CachedDefinitionCount | cached_definition_count | — | CachedDefinitionCount | cached_definition_count | DarraUa_StructDecoder_CachedCount |