iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
callback_context.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#ifndef IOX2_INTERNAL_CALLBACK_CONTEXT_HPP
14#define IOX2_INTERNAL_CALLBACK_CONTEXT_HPP
15
16#include "iox2/bb/optional.hpp"
19#include "iox2/node_details.hpp"
20#include "iox2/node_name.hpp"
21#include "iox2/node_state.hpp"
22#include "iox2/service_type.hpp"
24
25namespace iox2 {
27
28namespace internal {
46template <typename T>
48 public:
49 explicit CallbackContext(const T& ptr)
50 : m_ptr { &ptr } {
51 }
52
53 auto value() const -> const T& {
54 return *m_ptr;
55 }
56
57 private:
58 const T* m_ptr;
59};
60
61template <typename T>
62inline auto ctx(const T& ptr) -> CallbackContext<T> {
63 return CallbackContext<T>(ptr);
64}
65
66template <typename T>
67inline auto ctx_cast(void* ptr) -> CallbackContext<T>* {
68 return static_cast<CallbackContext<T>*>(ptr);
69}
70
71inline auto override_callback(size_t value, iox2_callback_context ctx) -> size_t {
72 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be raw pointer because of C interface
73 auto* context = ctx_cast<OverridePreallocationCallback>(ctx);
74 auto ret_val = context->value()(value);
75 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be raw pointer because of C interface
76 delete &context->value();
77 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be raw pointer because of C interface
78 delete context;
79 return ret_val;
80}
81
82template <typename T, typename ViewType>
83inline auto list_ports_callback(void* context, const T port_details_view) -> iox2_callback_progression_e {
84 auto* callback = internal::ctx_cast<iox2::bb::StaticFunction<CallbackProgression(ViewType)>>(context);
85 return iox2::bb::into<iox2_callback_progression_e>(callback->value()(ViewType(port_details_view)));
86}
87
88template <ServiceType T>
89// NOLINTBEGIN(readability-function-size)
90inline auto list_callback(iox2_node_state_e node_state,
91 iox2_unique_node_id_ptr node_id_ptr,
92 const char* executable,
93 iox2_node_name_ptr node_name,
94 iox2_config_ptr config,
95 iox2_callback_context context) -> iox2_callback_progression_e {
96 auto node_details = [&]() -> auto {
97 if (node_id_ptr == nullptr || config == nullptr) {
99 }
100
101 auto str =
102 iox2::bb::StaticString<iox2::bb::FileName::capacity()>::from_utf8_null_terminated_unchecked(executable);
103 if (!str.has_value()) {
105 }
106 auto file_name = bb::FileName::create(*str);
107 if (!file_name.has_value()) {
108 IOX2_PANIC("The executable file name should always be valid.");
109 }
111 NodeDetails { file_name.value(), NodeNameView { node_name }.to_owned(), Config {} });
112 }();
113
114 iox2_unique_node_id_h node_id_handle = nullptr;
115 iox2_unique_node_id_clone_from_ptr(nullptr, node_id_ptr, &node_id_handle);
116 UniqueNodeId node_id { node_id_handle };
117
118 auto node_state_object = [&]() -> auto {
119 switch (node_state) {
120 case iox2_node_state_e_ALIVE:
121 return NodeState<T> { AliveNodeView<T> { node_id, node_details } };
122 case iox2_node_state_e_DEAD:
123 return NodeState<T> { DeadNodeView<T> { AliveNodeView<T> { node_id, node_details } } };
124 case iox2_node_state_e_UNDEFINED:
125 return NodeState<T> { iox2_node_state_e_UNDEFINED, node_id };
126 case iox2_node_state_e_INACCESSIBLE:
127 return NodeState<T> { iox2_node_state_e_INACCESSIBLE, node_id };
128 }
129
131 }();
132
134 return iox2::bb::into<iox2_callback_progression_e>(callback->value()(node_state_object));
135}
136// NOLINTEND(readability-function-size)
137
138} // namespace internal
139} // namespace iox2
140
141#endif
#define IOX2_UNREACHABLE()
panic if control flow reaches this code at runtime
#define IOX2_PANIC(message)
calls panic handler and does not return
Contains all details of a [Node] that is alive.
Contains all details of a [Node] that is dead.
Contains details of a [Node].
Non-owning view of a [NodeName].
Definition node_name.hpp:33
auto to_owned() const -> NodeName
Creates a copy of the corresponding [NodeName] and returns it.
Describes the state of a [Node].
The system-wide unique id of a [Node].
static auto create(const char(&value)[N]) noexcept -> bb::Expected< FileName, SemanticStringError >
Creates a new SemanticString from the provided string literal. If the value contains invalid characte...
auto value() const -> const T &
iox2::bb::variation::Optional< T > Optional
Definition optional.hpp:25
auto override_callback(size_t value, iox2_callback_context ctx) -> size_t
auto list_callback(iox2_node_state_e node_state, iox2_unique_node_id_ptr node_id_ptr, const char *executable, iox2_node_name_ptr node_name, iox2_config_ptr config, iox2_callback_context context) -> iox2_callback_progression_e
auto list_ports_callback(void *context, const T port_details_view) -> iox2_callback_progression_e
auto ctx(const T &ptr) -> CallbackContext< T >
auto ctx_cast(void *ptr) -> CallbackContext< T > *