MethodBuilder
Overview
OPC UA's Call service requires input arguments to be packed into std::vector<Variant>, ordered to match the server's InputArguments. MethodBuilder provides a chained API.
Corresponding sections of the standard: Part 4 §5.11.2 / Part 5 §6.4.
API
MethodBuilder It is a class (method_builder.hpp) in the namespace darra::opcua; is passed during construction Session&.
| Methods | Category | Reading and Writing | Note |
|---|---|---|---|
| MethodBuilder(Session& s, std::string object_id, std::string method_id) | Structure | — | Bound to a method |
| .LoadInputArgs() | Chain-type | Read | Attempt to read InputArguments, then return MethodBuilder& |
| .Arg(std::string const& name, ArgValue value) | Chain-type | — | Set a value by name, return MethodBuilder& |
| .Arg(int index, ArgValue value) | Chain-type | — | Set values by index (starting at 0) |
| .Arg(int index, Variant&& v) | Chain-type | — | Direct transfer Variant; transfer of ownership |
| .Invoke() | Terminal | Write | Returns MethodCallResult { status, outputs } |
| .InputArgsLoaded() | Methods | Read | Returns bool — whether InputArguments was ever read |
namespace darra::opcua {
struct MethodCallResult {
Status status = Status::Bad;
std::vector<Variant> outputs;
bool IsGood() const; // status == Status::Good
};
using ArgValue = std::variant<
std::monostate,
bool,
int8_t, uint8_t, int16_t, uint16_t,
int32_t, uint32_t, int64_t, uint64_t,
float, double,
std::string>;
class MethodBuilder {
public:
MethodBuilder(Session& s, std::string object_id, std::string method_id);
MethodBuilder& LoadInputArgs();
MethodBuilder& Arg(std::string const& name, ArgValue value);
MethodBuilder& Arg(int index, ArgValue value);
MethodBuilder& Arg(int index, Variant&& v);
MethodCallResult Invoke();
bool InputArgsLoaded() const;
};
}
ArgValue Accepts only scalars:bool / 8/16/32/64 signed and unsigned integers / float / double / std::string, plus std::monostate(placeholder, encoded as null Variant).None system_clock::time_point, andNonestd::vector<uint8_t> — To pass arrays or complex structures, use the third overloaded method: construct it .Arg(index, Variant&&) yourself Variant(though C++ currently Variant only supports scalar setters; see the implementation limitations below).
Invoke() Go CallSafe(safe_api.hpp),Never Throw Exceptions, on failure, put the status code in MethodCallResult::status.
| Parameters | Default | Note |
|---|---|---|
| s | — | Connected Session |
| object_id | — | The string NodeId of the Object on which the method resides |
| method_id | — | String NodeId of the method node |
| name / index | — | Parameter Name / Parameter Index (starting at 0) |
| value | — | ArgValue or Variant&& |
How is the input-argument count determined?: take max(LoadInputArgs 得到的个数提示, 最大索引 + 1, 按名设值的个数). For the same subscript the preconstructed Variant wins first, then the value set by index, and finally the name-based values are filled in in insertion order; empty slots are encoded as an empty Variant.
Code Example
#include <darra/opcua/client.hpp>
#include <darra/opcua/method_builder.hpp>
#include <chrono>
#include <iostream>
#include <thread>
using namespace darra::opcua;
Session s("opc.tcp://localhost:4840");
s.Connect();
// 1) Stream invocation—by name
MethodCallResult r = MethodBuilder(s, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.LoadInputArgs()
.Arg("x", 100.0)
.Arg("y", 200.0)
.Arg("speed", 50)
.Invoke();
if (!r.IsGood())
std::cerr << "MoveTo 失败: " << Stack::StatusName(static_cast<uint32_t>(r.status)) << "\n";
// 2) Access by index + retrieve output value (Try* series, none AsDouble())
MethodCallResult r2 = MethodBuilder(s, "ns=2;s=Calc", "ns=2;s=Calc.Add")
.Arg(0, 3.14).Arg(1, 2.71).Invoke();
double out = 0;
if (r2.IsGood() && !r2.outputs.empty() && r2.outputs[0].TryGetDouble(out))
std::cout << "Add → " << out << "\n";
// 3) Reusable builder (Arg returns self; values can be set repeatedly before calling Invoke)
MethodBuilder b(s, "ns=2;s=Pump", "ns=2;s=Pump.SetSpeed");
b.LoadInputArgs();
for (int rpm : {100, 500, 1000, 1500}) {
MethodCallResult r3 = b.Arg(0, rpm).Invoke();
if (!r3.IsGood()) break;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
Comparison with Call
// Direct Call: Implement your own std::vector<Variant>, Keep track of the parameter order yourself
std::vector<Variant> inputs;
inputs.emplace_back(); inputs[0].SetDouble(100.0);
inputs.emplace_back(); inputs[1].SetDouble(200.0);
inputs.emplace_back(); inputs[2].SetInt32(50);
std::vector<Variant> outs = s.Call("ns=2;s=Robot", "ns=2;s=Robot.MoveTo", inputs);
// MethodBuilder: Chained initialization, a single Invoke
MethodCallResult r = MethodBuilder(s, "ns=2;s=Robot", "ns=2;s=Robot.MoveTo")
.Arg(0, 100.0).Arg(1, 200.0).Arg(2, 50)
.Invoke();
Variant is move-only(Copy construction has been removed), so the above uses emplace_back() + setter; you cannot write std::vector<Variant> inputs = { Variant(100.0), ... } —, as there is no Variant such Variant(double) construction.
Implementation Constraints
LoadInputArgsCan only confirm the existence of the parameter table + estimate the number:OPC UAInputArgumentsisArgument[](ExtensionObject, TypeIdi=297), since the current C++ implementation does notVariantexpose the decoding ExtensionObject functionality API, theseValueRankfields (/Name/DataType) cannot be retrieved and should be left blank.Setting values by index is not affected...it will still function normally.- Does not support complex structures or arrays as input parameters:
ArgValueOnly scalars,Variantand only scalar setters; there are no array setters, nor are there any encoding ExtensionObject entry points. - Failures do not throw:
Invoke()goes throughCallSafe, folding the status intoMethodCallResult::status; an emptyoutputsmeans there was no return value.
Cross-Language Comparison
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | MethodBuilder | DarraUa_MethodBuilder_New/_Free |
The C family is DarraUa_MethodBuilder_*, with handle type DarraUa_MethodBuilderHandle. Name-based assignment on the C side is one function per type: DarraUa_MethodBuilder_ArgBoolean / _ArgInt32 / _ArgUInt32 / _ArgInt64 / _ArgFloat / _ArgDouble / _ArgString / _ArgDateTime (each taking const char* name after the first parameter); index-based assignment is DarraUa_MethodBuilder_ArgByIndex(h, index, const DarraUa_Variant*). There is no DarraUa_MethodBuilder_ArgByName function on the C side.