open62541pp 0.15.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
result_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional> // invoke
4#include <type_traits> // invoke_result_t
5
7
8namespace opcua::detail {
9
10// Derive Result type from T
11template <typename T>
12struct ResultType {
14};
15
16template <>
20
21template <>
25
26/**
27 * Invoke a function and capture its Result (value or status code).
28 * This is especially useful for C-API callbacks, that are executed within the open62541 event loop.
29 */
30template <typename F, typename... Args>
31auto tryInvoke(F&& func, Args&&... args) noexcept ->
32 typename ResultType<std::invoke_result_t<F, Args...>>::Type {
33 using ReturnType = std::invoke_result_t<F, Args...>;
34 try {
35 if constexpr (std::is_void_v<ReturnType>) {
36 std::invoke(std::forward<F>(func), std::forward<Args>(args)...);
37 return {};
38 } else {
39 return std::invoke(std::forward<F>(func), std::forward<Args>(args)...);
40 }
41 } catch (...) {
42 return BadResult(getStatusCode(std::current_exception()));
43 }
44}
45
46} // namespace opcua::detail
Represents a bad result stored in Result.
Definition result.hpp:17
Template specialization of Result class for void types.
Definition result.hpp:346
The template class Result encapsulates a StatusCode and optionally a value.
Definition result.hpp:53
UA_StatusCode wrapper class.
Definition types.hpp:44
static UA_LogCategory const char va_list args
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).
UA_StatusCode getStatusCode(std::exception_ptr eptr) noexcept
Definition exception.hpp:67