open62541pp 0.16.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 <functional> // ref
4#include <type_traits>
5#include <utility> // exchange
6
10#include "open62541pp/span.hpp"
11#include "open62541pp/types.hpp" // StatusCode
12#include "open62541pp/wrapper.hpp" // asNative, asWrapper, isWrapper
13
15
16template <typename WrapperType>
17struct Wrap {
19 using NativeType = typename WrapperType::NativeType;
20
21 [[nodiscard]] constexpr WrapperType operator()(const NativeType& native) noexcept {
22 return {native};
23 }
24
25 [[nodiscard]] constexpr WrapperType operator()(NativeType& native) noexcept {
26 return {std::exchange(native, {})};
27 }
28
29 [[nodiscard]] constexpr WrapperType operator()(NativeType&& native) noexcept {
30 return {std::exchange(std::move(native), {})};
31 }
32};
33
34template <typename Response>
35const UA_ResponseHeader& getResponseHeader(const Response& response) noexcept {
37 return asNative(response).responseHeader;
38 } else {
39 return response.responseHeader;
40 }
41}
42
43template <typename Response>
44StatusCode getServiceResult(const Response& response) noexcept {
45 return getResponseHeader(response).serviceResult;
46}
47
48template <typename Response>
49auto getSingleResultRef(Response& response) noexcept {
50 auto* native = [&] {
52 return asNative(&response);
53 } else {
54 return &response;
55 }
56 }();
57 using ResultType = Result<decltype(std::ref(*native->results))>;
58 if (const StatusCode serviceResult = getServiceResult(response); serviceResult.isBad()) {
59 return ResultType(BadResult(serviceResult));
60 }
61 if (native->results == nullptr || native->resultsSize != 1) {
63 }
64 return ResultType(std::ref(*native->results));
65}
66
67template <typename Response>
68StatusCode getSingleStatus(const Response& response) noexcept {
69 auto result = getSingleResultRef(response);
70 return result ? asWrapper<StatusCode>(result->get()) : result.code();
71}
72
73template <typename WrapperType, typename Response>
74Result<WrapperType> wrapSingleResult(Response& response) noexcept {
76}
77
78// Get single result from response and save response status code in result type's statusCode member.
79template <typename WrapperType, typename Response>
80WrapperType wrapSingleResultWithStatus(Response& response) noexcept {
81 return *wrapSingleResult<WrapperType>(response).orElse([](UA_StatusCode code) {
82 WrapperType result;
83 result->statusCode = code;
84 return result;
85 });
86}
87
89 if (const StatusCode code = result.statusCode; code.isBad()) {
90 return BadResult(code);
91 }
92 return Wrap<NodeId>{}(result.addedNodeId);
93}
94
95} // namespace opcua::services::detail
Represents a bad result stored in Result.
Definition result.hpp:17
The template class Result encapsulates a StatusCode and optionally a value.
Definition result.hpp:53
constexpr auto transform(F &&func) &
Transforms Result<T> to Result<U> using the given value transformation function.
Definition result.hpp:207
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 NativeType * asNative(WrapperType *wrapper) noexcept
Cast Wrapper object pointers to native object pointers.
Definition wrapper.hpp:191
constexpr bool isWrapper
Definition wrapper.hpp:103
Result< NodeId > getAddedNodeId(UA_AddNodesResult &result) noexcept
const UA_ResponseHeader & getResponseHeader(const Response &response) noexcept
WrapperType wrapSingleResultWithStatus(Response &response) noexcept
StatusCode getSingleStatus(const Response &response) noexcept
Result< WrapperType > wrapSingleResult(Response &response) noexcept
StatusCode getServiceResult(const Response &response) noexcept
auto getSingleResultRef(Response &response) noexcept
Client * asWrapper(UA_Client *client) noexcept
Convert native UA_Client pointer to its wrapper instance.
#define UA_STATUSCODE_BADUNEXPECTEDERROR
UA_StatusCode serviceResult
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