open62541pp 0.17.0
C++ wrapper of open62541
Loading...
Searching...
No Matches
callbackadapter.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4
6
7namespace opcua::services::detail {
8
9/**
10 * Helper class to map open62541 / C-style callbacks to `std::function` objects.
11 * The the concrete adapter should:
12 * - store the `std::function` objects
13 * - implement static methods of the native open62541 callbacks
14 * - pass the pointer to itself as the callback context
15 */
16struct CallbackAdapter {
17 // Optionally provide exception catcher, otherwise exceptions will be ignored
18 opcua::detail::ExceptionCatcher* catcher = nullptr;
19
20 template <typename F, typename... Args>
21 void invoke(F&& callback, Args&&... args) const noexcept {
22 if (callback != nullptr) {
23 if (catcher != nullptr) {
24 catcher->invoke(std::forward<F>(callback), std::forward<Args>(args)...);
25 } else {
26 try {
27 std::invoke(std::forward<F>(callback), std::forward<Args>(args)...);
28 } catch (...) { // NOLINT(bugprone-empty-catch)
29 // ignore exceptions
30 }
31 }
32 }
33 }
34};
35
36} // namespace opcua::services::detail