open62541pp 0.15.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
response_handling.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm> // for_each_n
4#include <iterator> // make_move_iterator
5#include <type_traits>
6#include <utility> // exchange
7#include <vector>
8
12#include "open62541pp/span.hpp"
13#include "open62541pp/types.hpp" // StatusCode
14#include "open62541pp/wrapper.hpp" // asWrapper, isWrapper
15
17
18template <typename WrapperType>
19struct Wrap {
21 using NativeType = typename WrapperType::NativeType;
22
23 [[nodiscard]] constexpr WrapperType operator()(const NativeType& native) noexcept {
24 return {native};
25 }
26
27 [[nodiscard]] constexpr WrapperType operator()(NativeType& native) noexcept {
28 return {std::exchange(native, {})};
29 }
30
31 [[nodiscard]] constexpr WrapperType operator()(NativeType&& native) noexcept {
32 return {std::exchange(std::move(native), {})};
33 }
34};
35
36template <typename Response>
37inline const UA_ResponseHeader& getResponseHeader(const Response& response) noexcept {
39 return response->responseHeader;
40 } else {
41 return response.responseHeader;
42 }
43}
44
45template <typename Response>
46inline StatusCode getServiceResult(const Response& response) noexcept {
47 return getResponseHeader(response).serviceResult;
48}
49
50template <typename Response>
51auto getSingleResult(Response& response) noexcept -> Result<decltype(std::ref(*response.results))> {
52 if (const StatusCode serviceResult = getServiceResult(response); serviceResult.isBad()) {
53 return BadResult(serviceResult);
54 }
55 if (response.results == nullptr || response.resultsSize != 1) {
57 }
58 return std::ref(*response.results);
59}
60
61inline Result<void> toResult(UA_StatusCode code) noexcept {
62 if (opcua::detail::isBad(code)) {
63 return BadResult(code);
64 }
65 return {code};
66}
67
69 if (const StatusCode code = result.statusCode; code.isBad()) {
70 return BadResult(code);
71 }
72 return {std::exchange(result.addedNodeId, {})};
73}
74
76 if (const StatusCode code = result.statusCode; code.isBad()) {
77 return BadResult(result.statusCode);
78 }
79 for (const StatusCode code :
80 Span(result.inputArgumentResults, result.inputArgumentResultsSize)) {
81 if (code.isBad()) {
82 return BadResult(code);
83 }
84 }
85 return opcua::detail::tryInvoke([&] {
86 return std::vector<Variant>{
87 std::make_move_iterator(result.outputArguments),
88 std::make_move_iterator(result.outputArguments + result.outputArgumentsSize) // NOLINT
89 };
90 });
91}
92
93template <typename SubscriptionParameters, typename Response>
95 SubscriptionParameters& parameters, const Response& response
96) noexcept {
97 parameters.publishingInterval = response.revisedPublishingInterval;
98 parameters.lifetimeCount = response.revisedLifetimeCount;
99 parameters.maxKeepAliveCount = response.revisedMaxKeepAliveCount;
100}
101
102template <typename MonitoringParameters, typename Result>
104 MonitoringParameters& parameters, const Result& result
105) noexcept {
106 // result type may be UA_MonitoredItemCreateResult or UA_MonitoredItemModifyResult
107 parameters.samplingInterval = result.revisedSamplingInterval;
108 parameters.queueSize = result.revisedQueueSize;
109 parameters.filter = asWrapper<ExtensionObject>(result.filterResult);
110}
111
113 if (const StatusCode code = result.statusCode; code.isBad()) {
114 return BadResult(code);
115 }
116 return result.monitoredItemId;
117}
118
119} // namespace opcua::services::detail
Represents a bad result stored in Result.
Definition result.hpp:17
UA_MonitoringParameters wrapper class.
The template class Result encapsulates a StatusCode and optionally a value.
Definition result.hpp:53
UA_StatusCode wrapper class.
Definition types.hpp:44
constexpr bool isBad() const noexcept
Check if the status code is bad.
Definition types.hpp:76
constexpr WrapperType * asWrapper(NativeType *native) noexcept
Cast native object pointers to Wrapper object pointers.
Definition wrapper.hpp:164
auto tryInvoke(F &&func, Args &&... args) noexcept -> typename ResultType< std::invoke_result_t< F, Args... > >::Type
Invoke a function and capture its Result (value or status code).
constexpr bool isWrapper
Definition wrapper.hpp:103
constexpr bool isBad(UA_StatusCode code) noexcept
Definition exception.hpp:62
void reviseMonitoringParameters(MonitoringParameters &parameters, const Result &result) noexcept
void reviseSubscriptionParameters(SubscriptionParameters &parameters, const Response &response) noexcept
auto getSingleResult(Response &response) noexcept -> Result< decltype(std::ref(*response.results))>
Result< NodeId > getAddedNodeId(UA_AddNodesResult &result) noexcept
Result< uint32_t > getMonitoredItemId(const UA_MonitoredItemCreateResult &result) noexcept
const UA_ResponseHeader & getResponseHeader(const Response &response) noexcept
StatusCode getServiceResult(const Response &response) noexcept
Result< void > toResult(UA_StatusCode code) noexcept
Result< std::vector< Variant > > getOutputArguments(UA_CallMethodResult &result) noexcept
Span(Container &) -> Span< typename Container::value_type >
#define UA_STATUSCODE_BADUNEXPECTEDERROR
UA_StatusCode serviceResult
Subscription parameters with default values from open62541.
typename WrapperType::NativeType NativeType
constexpr WrapperType operator()(NativeType &native) noexcept
constexpr WrapperType operator()(const NativeType &native) noexcept
constexpr WrapperType operator()(NativeType &&native) noexcept
uint32_t UA_StatusCode