iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
service_builder_blackboard.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 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_SERVICE_BLACKBOARD_BUILDER_HPP
14#define IOX2_SERVICE_BLACKBOARD_BUILDER_HPP
15
19#include "iox2/bb/expected.hpp"
23#include "iox2/service_type.hpp"
24
25#include <cstdint>
26#include <type_traits>
27
28namespace iox2 {
30template <typename KeyType, ServiceType S>
32 public:
33 static_assert(std::is_trivially_copyable<KeyType>::value,
34 "The blackboard supports only trivially copyable key types.");
35 static_assert(std::alignment_of<KeyType>() <= IOX2_MAX_BLACKBOARD_KEY_ALIGNMENT,
36 "The blackboard supports only key types with an alignment <= IOX2_MAX_BLACKBOARD_KEY_ALIGNMENT.");
37 static_assert(sizeof(KeyType) <= IOX2_MAX_BLACKBOARD_KEY_SIZE,
38 "The blackboard supports only key types with a size <= IOX2_MAX_BLACKBOARD_KEY_SIZE.");
39
40
42#ifdef DOXYGEN_MACRO_FIX
43 auto max_readers(const uint64_t value) -> decltype(auto);
44#else
46#endif
47
49#ifdef DOXYGEN_MACRO_FIX
50 auto max_nodes(const uint64_t value) -> decltype(auto);
51#else
53#endif
54
55 public:
59
62
64 template <typename ValueType>
65 auto add(KeyType key, ValueType value) -> ServiceBuilderBlackboardCreator&&;
66
68 template <typename ValueType>
70
73
75 auto
78
79 private:
80 template <ServiceType>
81 friend class ServiceBuilder;
82
83 explicit ServiceBuilderBlackboardCreator(iox2_service_builder_h handle);
84 void drop();
85
86 void set_parameters();
87
88 iox2_service_builder_blackboard_creator_h m_handle = nullptr;
89};
90
91template <typename KeyType, ServiceType S>
93 public:
95#ifdef DOXYGEN_MACRO_FIX
96 auto max_readers(const uint64_t value) -> decltype(auto);
97#else
99#endif
100
102#ifdef DOXYGEN_MACRO_FIX
103 auto max_nodes(const uint64_t value) -> decltype(auto);
104#else
106#endif
107
108 public:
112
115
118
122 const AttributeVerifier&
124
125 private:
126 template <ServiceType>
127 friend class ServiceBuilder;
128
129 explicit ServiceBuilderBlackboardOpener(iox2_service_builder_h handle);
130 void drop();
131
132 void set_parameters();
133
134 iox2_service_builder_blackboard_opener_h m_handle = nullptr;
135};
136
137namespace internal {
138template <typename T>
139auto default_key_eq_cmp_func(const void* lhs, const void* rhs) -> bool {
140 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast): C API requires to pass void* instead of T*
141 return (*reinterpret_cast<const T*>(lhs)) == (*reinterpret_cast<const T*>(rhs));
142}
143} // namespace internal
144
145template <typename KeyType, ServiceType S>
147 : m_handle { iox2_service_builder_blackboard_creator(handle) } {
148 // set key type details so that these are available in add()
149 const auto type_name = internal::get_type_name<KeyType>();
150 const auto key_type_result = iox2_service_builder_blackboard_creator_set_key_type_details(
151 &m_handle, type_name.unchecked_access().c_str(), type_name.size(), sizeof(KeyType), alignof(KeyType));
152 if (key_type_result != IOX2_OK) {
153 IOX2_PANIC("This should never happen! Implementation failure while setting the key type.");
154 }
155}
156
157template <typename KeyType, ServiceType S>
158inline void ServiceBuilderBlackboardCreator<KeyType, S>::drop() {
159 if (m_handle != nullptr) {
160 iox2_service_builder_blackboard_creator_drop(m_handle);
161 m_handle = nullptr;
162 }
163}
164
165template <typename KeyType, ServiceType S>
170
171template <typename KeyType, ServiceType S>
174 if (this != &rhs) {
175 drop();
176 m_handle = rhs.m_handle;
177 rhs.m_handle = nullptr;
178 m_max_readers = std::move(rhs.m_max_readers);
179 m_max_nodes = std::move(rhs.m_max_nodes);
180 }
181
182 return *this;
183}
184
185template <typename KeyType, ServiceType S>
189
190template <typename KeyType, ServiceType S>
191inline void ServiceBuilderBlackboardCreator<KeyType, S>::set_parameters() {
192 if (m_max_readers.has_value()) {
193 iox2_service_builder_blackboard_creator_set_max_readers(&m_handle, m_max_readers.value());
194 }
195 if (m_max_nodes.has_value()) {
196 iox2_service_builder_blackboard_creator_set_max_nodes(&m_handle, m_max_nodes.value());
197 }
198
199 // key eq comparison function
200 iox2_service_builder_blackboard_creator_set_key_eq_comparison_function(&m_handle,
202}
203
204template <typename KeyType, ServiceType S>
205template <typename ValueType>
206inline auto ServiceBuilderBlackboardCreator<KeyType, S>::add(KeyType key, ValueType value)
208 static_assert(std::is_trivially_copyable<ValueType>::value,
209 "The blackboard supports only trivially copyable value types.");
210
211 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory): required by C API
212 auto value_ptr = new ValueType(value);
213 const auto type_name = internal::get_type_name<ValueType>();
214
215 iox2_service_builder_blackboard_creator_add(
216 &m_handle,
217 &key,
218 value_ptr,
219 [](void* value) -> auto {
220 auto* value_ptr = static_cast<ValueType*>(value);
221 if (value_ptr != nullptr) {
222 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory): required by C API
223 delete value_ptr;
224 value_ptr = nullptr;
225 }
226 },
227 type_name.unchecked_access().c_str(),
228 type_name.size(),
229 sizeof(ValueType),
230 alignof(ValueType));
231
232 return std::move(*this);
233}
234
235template <typename KeyType, ServiceType S>
236template <typename ValueType>
239 return add(key, ValueType());
240}
241
242template <typename KeyType, ServiceType S>
245 set_parameters();
246
247 iox2_port_factory_blackboard_h port_factory_handle {};
248 auto result = iox2_service_builder_blackboard_create(m_handle, nullptr, &port_factory_handle);
249 m_handle = nullptr;
250
251 if (result == IOX2_OK) {
252 return PortFactoryBlackboard<S, KeyType>(port_factory_handle);
253 }
254
256}
257
258template <typename KeyType, ServiceType S>
261 set_parameters();
262
263 iox2_port_factory_blackboard_h port_factory_handle {};
264 auto result = iox2_service_builder_blackboard_create_with_attributes(
265 m_handle, &attributes.m_handle, nullptr, &port_factory_handle);
266 m_handle = nullptr;
267
268 if (result == IOX2_OK) {
269 return PortFactoryBlackboard<S, KeyType>(port_factory_handle);
270 }
271
273}
274
275template <typename KeyType, ServiceType S>
277 : m_handle { iox2_service_builder_blackboard_opener(handle) } {
278}
279
280template <typename KeyType, ServiceType S>
281inline void ServiceBuilderBlackboardOpener<KeyType, S>::drop() {
282 if (m_handle != nullptr) {
283 iox2_service_builder_blackboard_opener_drop(m_handle);
284 m_handle = nullptr;
285 }
286}
287
288template <typename KeyType, ServiceType S>
293
294template <typename KeyType, ServiceType S>
297 if (this != &rhs) {
298 drop();
299 m_handle = rhs.m_handle;
300 rhs.m_handle = nullptr;
301 m_max_readers = std::move(rhs.m_max_readers);
302 m_max_nodes = std::move(rhs.m_max_nodes);
303 }
304
305 return *this;
306}
307
308template <typename KeyType, ServiceType S>
312
313template <typename KeyType, ServiceType S>
314inline void ServiceBuilderBlackboardOpener<KeyType, S>::set_parameters() {
315 if (m_max_readers.has_value()) {
316 iox2_service_builder_blackboard_opener_set_max_readers(&m_handle, m_max_readers.value());
317 }
318 if (m_max_nodes.has_value()) {
319 iox2_service_builder_blackboard_opener_set_max_nodes(&m_handle, m_max_nodes.value());
320 }
321
322 // key type details
323 const auto type_name = internal::get_type_name<KeyType>();
324 const auto key_type_result = iox2_service_builder_blackboard_opener_set_key_type_details(
325 &m_handle, type_name.unchecked_access().c_str(), type_name.size(), sizeof(KeyType), alignof(KeyType));
326 if (key_type_result != IOX2_OK) {
327 IOX2_PANIC("This should never happen! Implementation failure while setting the Key-Type.");
328 }
329
330 // key eq comparison function
331 iox2_service_builder_blackboard_opener_set_key_eq_comparison_function(&m_handle,
333}
334
335template <typename KeyType, ServiceType S>
338 set_parameters();
339
340 iox2_port_factory_blackboard_h port_factory_handle {};
341 auto result = iox2_service_builder_blackboard_open(m_handle, nullptr, &port_factory_handle);
342 m_handle = nullptr;
343
344 if (result == IOX2_OK) {
345 return PortFactoryBlackboard<S, KeyType>(port_factory_handle);
346 }
347
349}
350
351template <typename KeyType, ServiceType S>
353 const AttributeVerifier&
355 set_parameters();
356
357 iox2_port_factory_blackboard_h port_factory_handle {};
358 auto result = iox2_service_builder_blackboard_open_with_attributes(
359 m_handle, &required_attributes.m_handle, nullptr, &port_factory_handle);
360 m_handle = nullptr;
361
362 if (result == IOX2_OK) {
363 return PortFactoryBlackboard<S, KeyType>(port_factory_handle);
364 }
365
367}
368} // namespace iox2
369
370#endif
#define IOX2_PANIC(message)
calls panic handler and does not return
#define IOX2_BUILDER_OPTIONAL(type, name)
Definition builder.hpp:47
Represents the port factory of a [Service] with [MessagingPattern::Blackboard].
auto create_with_attributes(const AttributeSpecifier &attributes) &&-> bb::Expected< PortFactoryBlackboard< S, KeyType >, BlackboardCreateError >
Creates a new [Service] with a set of attributes.
ServiceBuilderBlackboardCreator(const ServiceBuilderBlackboardCreator &)=delete
auto operator=(ServiceBuilderBlackboardCreator &&rhs) noexcept -> ServiceBuilderBlackboardCreator &
auto max_nodes(const uint64_t value) -> decltype(auto)
Defines how many [Node]s shall be able to open it in parallel.
auto add(KeyType key, ValueType value) -> ServiceBuilderBlackboardCreator &&
Adds key-value pairs to the blackboard.
auto create() &&-> bb::Expected< PortFactoryBlackboard< S, KeyType >, BlackboardCreateError >
Creates a new [Service].
ServiceBuilderBlackboardCreator(ServiceBuilderBlackboardCreator &&rhs) noexcept
auto max_readers(const uint64_t value) -> decltype(auto)
Defines how many [Reader]s shall be supported at most.
auto operator=(const ServiceBuilderBlackboardCreator &) -> ServiceBuilderBlackboardCreator &=delete
auto add_with_default(KeyType key) -> ServiceBuilderBlackboardCreator &&
Adds key-value pairs to the blackboard where value is a default value.
auto operator=(ServiceBuilderBlackboardOpener &&rhs) noexcept -> ServiceBuilderBlackboardOpener &
auto max_nodes(const uint64_t value) -> decltype(auto)
Defines how many [Node]s must be at least supported.
auto operator=(const ServiceBuilderBlackboardOpener &) -> ServiceBuilderBlackboardOpener &=delete
ServiceBuilderBlackboardOpener(const ServiceBuilderBlackboardOpener &)=delete
ServiceBuilderBlackboardOpener(ServiceBuilderBlackboardOpener &&rhs) noexcept
auto open() &&-> bb::Expected< PortFactoryBlackboard< S, KeyType >, BlackboardOpenError >
Opens an existing [Service].
auto max_readers(const uint64_t value) -> decltype(auto)
Defines how many [Reader]s must be at least supported.
auto open_with_attributes(const AttributeVerifier &required_attributes) &&-> bb::Expected< PortFactoryBlackboard< S, KeyType >, BlackboardOpenError >
constexpr auto into(const SourceType value) noexcept -> typename detail::ExtractIntoType< DestinationType >::TargetType
Converts a value of type SourceType to a corresponding value of type DestinationType....
Definition into.hpp:120
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
auto default_key_eq_cmp_func(const void *lhs, const void *rhs) -> bool
auto get_type_name() -> TypeName
BlackboardOpenError
Errors that can occur when an existing [MessagingPattern::Blackboard] [Service] shall be opened.
BlackboardCreateError
Errors that can occur when a new [MessagingPattern::Blackboard] [Service] shall be created.