open62541pp 0.16.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
async_hook.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional> // invoke
4#include <type_traits>
5#include <utility>
6
8
10
11/**
12 * Special token to execute a hook function with the const result within the completion handler.
13 * The HookToken wraps a hook function (object) and the original completion token.
14 */
15template <typename HookFunction, typename CompletionToken>
16struct HookToken {
17 HookToken(HookFunction&& hookFunction, CompletionToken&& completionToken)
18 : hook(std::move(hookFunction)),
19 token(std::move(completionToken)) {}
20
21 std::decay_t<HookFunction> hook;
22 std::decay_t<CompletionToken> token;
23};
24
25template <typename HookFunction, typename CompletionToken>
26HookToken(HookFunction&&, CompletionToken&&) -> HookToken<HookFunction, CompletionToken>;
27
28} // namespace opcua::services::detail
29
30namespace opcua {
31
32template <typename HookFunction, typename CompletionToken, typename T>
33struct AsyncResult<services::detail::HookToken<HookFunction, CompletionToken>, T> {
35
36 template <typename Initiation, typename... Args>
37 static auto initiate(
38 Initiation&& initiation,
39 Token&& token, // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
40 Args&&... args
41 ) {
42 return asyncInitiate<T>(
43 [innerInitiation = std::forward<Initiation>(initiation),
44 innerHook = std::forward<Token>(token).hook](
45 auto&& handler, auto&&... innerArgs
46 ) mutable {
47 std::invoke(
48 innerInitiation,
49 [innerHook = std::move(innerHook),
50 innerHandler = std::forward<decltype(handler)>(handler)](auto&& result
51 ) mutable {
52 std::invoke(innerHook, std::as_const(result));
53 std::invoke(innerHandler, result);
54 },
55 std::forward<decltype(innerArgs)>(innerArgs)...
56 );
57 },
58 std::forward<Token>(token).token,
59 std::forward<Args>(args)...
60 );
61 }
62};
63
64} // namespace opcua
auto asyncInitiate(Initiation &&initiation, CompletionToken &&token, Args &&... args)
Definition async.hpp:40
static UA_LogCategory const char va_list args
HookToken(HookFunction &&, CompletionToken &&) -> HookToken< HookFunction, CompletionToken >
static auto initiate(Initiation &&initiation, Token &&token, Args &&... args)
Special token to execute a hook function with the const result within the completion handler.
HookToken(HookFunction &&hookFunction, CompletionToken &&completionToken)
std::decay_t< HookFunction > hook
std::decay_t< CompletionToken > token