iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
service_builder_publish_subscribe.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_SERVICE_BUILDER_PUBLISH_SUBSCRIBE_HPP
14#define IOX2_SERVICE_BUILDER_PUBLISH_SUBSCRIBE_HPP
15
19#include "iox2/bb/expected.hpp"
20#include "iox2/bb/file_path.hpp"
21#include "iox2/bb/layout.hpp"
22#include "iox2/bb/slice.hpp"
25#include "iox2/marker.hpp"
27#include "iox2/payload_info.hpp"
30#include "iox2/service_type.hpp"
31#include "iox2/type_variant.hpp"
32
33#include <cstring>
34#include <type_traits>
35
36namespace iox2 {
37template <typename Payload, typename UserHeader, ServiceType S>
39
49template <typename Payload, typename UserHeader, ServiceType S>
51 -> std::enable_if_t<std::is_same<Payload, bb::Slice<CustomPayloadMarker>>::value>;
52
62template <typename Payload, typename UserHeader, ServiceType S>
64 const TypeDetail& value)
65 -> std::enable_if_t<std::is_same<UserHeader, CustomHeaderMarker>::value>;
66
68template <typename Payload, typename UserHeader, ServiceType S>
70 public:
75#ifdef DOXYGEN_MACRO_FIX
76 auto payload_alignment(const uint64_t value) -> decltype(auto);
77#else
79#endif
80
83#ifdef DOXYGEN_MACRO_FIX
84 auto enable_safe_overflow(const bool value) -> decltype(auto);
85#else
87#endif
88
92#ifdef DOXYGEN_MACRO_FIX
93 auto subscriber_max_borrowed_samples(const uint64_t value) -> decltype(auto);
94#else
96#endif
97
101#ifdef DOXYGEN_MACRO_FIX
102 auto history_size(const uint64_t value) -> decltype(auto);
103#else
105#endif
106
109#ifdef DOXYGEN_MACRO_FIX
110 auto subscriber_max_buffer_size(const uint64_t value) -> decltype(auto);
111#else
113#endif
114
118#ifdef DOXYGEN_MACRO_FIX
119 auto max_subscribers(const uint64_t value) -> decltype(auto);
120#else
122#endif
123
127#ifdef DOXYGEN_MACRO_FIX
128 auto max_publishers(const uint64_t value) -> decltype(auto);
129#else
131#endif
132
136#ifdef DOXYGEN_MACRO_FIX
137 auto max_nodes(const uint64_t value) -> decltype(auto);
138#else
140#endif
141
142 public:
146
149
153 template <typename U = Payload, typename = std::enable_if_t<has_flatbuffer_marker<U>(), void>>
155
157 template <typename NewHeader>
159
164
169
174 auto open_or_create_with_attributes(const AttributeVerifier& required_attributes) && -> bb::
175 Expected<PortFactoryPublishSubscribe<S, Payload, UserHeader>, PublishSubscribeOpenOrCreateError>;
176
179
182 auto open_with_attributes(const AttributeVerifier& required_attributes) && -> bb::
183 Expected<PortFactoryPublishSubscribe<S, Payload, UserHeader>, PublishSubscribeOpenError>;
184
187
192
193 private:
194 template <ServiceType>
195 friend class ServiceBuilder;
196
197 template <typename P, typename U, ServiceType St>
199 -> std::enable_if_t<std::is_same<P, bb::Slice<CustomPayloadMarker>>::value>;
200 template <typename P, typename U, ServiceType St>
202 -> std::enable_if_t<std::is_same<U, CustomHeaderMarker>::value>;
203
204 explicit ServiceBuilderPublishSubscribe(iox2_service_builder_h handle);
205 void drop();
206
207 template <typename U = Payload>
208 auto set_type_definition_name_hint() -> std::enable_if_t<has_flatbuffer_marker<U>(), void>;
209 template <typename U = Payload>
210 auto set_type_definition_name_hint() -> std::enable_if_t<!has_flatbuffer_marker<U>(), void>;
211
212 void set_parameters();
213 void derive_payload_type_details();
214 void override_payload_type_details(const TypeDetail& value);
215 void derive_user_header_type_details();
216 void override_user_header_type_details(const TypeDetail& value);
217
218 iox2_service_builder_pub_sub_h m_handle = nullptr;
219 bb::Optional<TypeDetail> m_user_header_type_details_override;
220 bb::Optional<TypeDetail> m_payload_type_details_override;
221};
222
223template <typename Payload, typename UserHeader, ServiceType S>
225 iox2_service_builder_h handle)
226 : m_handle { iox2_service_builder_pub_sub(handle) } {
227}
228
229template <typename Payload, typename UserHeader, ServiceType S>
230inline void ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::drop() {
231 if (m_handle != nullptr) {
232 iox2_service_builder_pub_sub_drop(m_handle);
233 m_handle = nullptr;
234 }
235}
236
237template <typename Payload, typename UserHeader, ServiceType S>
242
243template <typename Payload, typename UserHeader, ServiceType S>
244inline auto
247 if (this != &rhs) {
248 drop();
249 m_handle = rhs.m_handle;
250 rhs.m_handle = nullptr;
251 m_payload_alignment = std::move(rhs.m_payload_alignment);
252 m_enable_safe_overflow = std::move(rhs.m_enable_safe_overflow);
253 m_subscriber_max_borrowed_samples = std::move(rhs.m_subscriber_max_borrowed_samples);
254 m_history_size = std::move(rhs.m_history_size);
255 m_subscriber_max_buffer_size = std::move(rhs.m_subscriber_max_buffer_size);
256 m_max_subscribers = std::move(rhs.m_max_subscribers);
257 m_max_publishers = std::move(rhs.m_max_publishers);
258 m_max_nodes = std::move(rhs.m_max_nodes);
259 m_user_header_type_details_override = std::move(rhs.m_user_header_type_details_override);
260 m_payload_type_details_override = std::move(rhs.m_payload_type_details_override);
261 }
262
263 return *this;
264}
265
266template <typename Payload, typename UserHeader, ServiceType S>
270
271template <typename Payload, typename UserHeader, ServiceType S>
272template <typename U>
273inline auto ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::set_type_definition_name_hint()
274 -> std::enable_if_t<has_flatbuffer_marker<U>(), void> {
276 iox2_service_builder_pub_sub_type_definition_name_hint(
277 &m_handle, type_name.unchecked_access().c_str(), type_name.size(), "", 0);
278}
279
280template <typename Payload, typename UserHeader, ServiceType S>
281template <typename U>
282inline auto ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::set_type_definition_name_hint()
283 -> std::enable_if_t<!has_flatbuffer_marker<U>(), void> {
284}
285
286template <typename Payload, typename UserHeader, ServiceType S>
287inline void ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::set_parameters() {
288 if (m_enable_safe_overflow.has_value()) {
289 iox2_service_builder_pub_sub_set_enable_safe_overflow(&m_handle, m_enable_safe_overflow.value());
290 }
291 if (m_subscriber_max_borrowed_samples.has_value()) {
292 iox2_service_builder_pub_sub_set_subscriber_max_borrowed_samples(&m_handle,
293 m_subscriber_max_borrowed_samples.value());
294 }
295 if (m_history_size.has_value()) {
296 iox2_service_builder_pub_sub_set_history_size(&m_handle, m_history_size.value());
297 }
298 if (m_subscriber_max_buffer_size.has_value()) {
299 iox2_service_builder_pub_sub_set_subscriber_max_buffer_size(&m_handle, m_subscriber_max_buffer_size.value());
300 }
301 if (m_max_subscribers.has_value()) {
302 iox2_service_builder_pub_sub_set_max_subscribers(&m_handle, m_max_subscribers.value());
303 }
304 if (m_max_publishers.has_value()) {
305 iox2_service_builder_pub_sub_set_max_publishers(&m_handle, m_max_publishers.value());
306 }
307 if (m_payload_alignment.has_value()) {
308 iox2_service_builder_pub_sub_set_payload_alignment(&m_handle, m_payload_alignment.value());
309 }
310 if (m_max_nodes.has_value()) {
311 iox2_service_builder_pub_sub_set_max_nodes(&m_handle, m_max_nodes.value());
312 }
313
314 if (m_user_header_type_details_override.has_value()) {
315 override_user_header_type_details(m_user_header_type_details_override.value());
316 } else {
317 derive_user_header_type_details();
318 }
319
320 if (m_payload_type_details_override.has_value()) {
321 override_payload_type_details(m_payload_type_details_override.value());
322 } else {
323 derive_payload_type_details();
324 }
325
326 set_type_definition_name_hint();
327}
328
329template <typename Payload, typename UserHeader, ServiceType S>
330inline void ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::derive_user_header_type_details() {
331 // user header type details derived from the compile-time UserHeader
332 const auto header_layout = bb::Layout::from<UserHeader>();
333 const auto user_header_type_name = internal::get_type_name<UserHeader>();
334
335 const auto result =
336 iox2_service_builder_pub_sub_set_user_header_type_details(&m_handle,
337 iox2_type_variant_e_FIXED_SIZE,
338 user_header_type_name.unchecked_access().c_str(),
339 user_header_type_name.size(),
340 header_layout.size(),
341 header_layout.alignment());
342
343 if (result != IOX2_OK) {
344 IOX2_PANIC("This should never happen! Implementation failure while setting the User-Header-Type.");
345 }
346}
347
348template <typename Payload, typename UserHeader, ServiceType S>
349inline void
350ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::override_user_header_type_details(const TypeDetail& value) {
351 // user header type details provided at runtime
352 const auto type_variant =
353 value.variant() == TypeVariant::FixedSize ? iox2_type_variant_e_FIXED_SIZE : iox2_type_variant_e_DYNAMIC;
354
355 const auto result = iox2_service_builder_pub_sub_set_user_header_type_details(
356 &m_handle, type_variant, value.type_name(), std::strlen(value.type_name()), value.size(), value.alignment());
357
358 if (result != IOX2_OK) {
359 IOX2_PANIC("This should never happen! Implementation failure while setting the User-Header-Type.");
360 }
361}
362
363template <typename Payload, typename UserHeader, ServiceType S>
364inline void ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::derive_payload_type_details() {
365 using ValueType = typename PayloadInfo<Payload>::ValueType;
366
367 // payload type details derived from the compile-time Payload
368 const auto payload_type_name = internal::get_type_name<Payload>();
369 const auto type_variant =
370 bb::IsSlice<Payload>::VALUE ? iox2_type_variant_e_DYNAMIC : iox2_type_variant_e_FIXED_SIZE;
371
372 const auto result =
373 iox2_service_builder_pub_sub_set_payload_type_details(&m_handle,
374 type_variant,
375 payload_type_name.unchecked_access().c_str(),
376 payload_type_name.size(),
377 sizeof(ValueType),
378 alignof(ValueType));
379
380 if (result != IOX2_OK) {
381 IOX2_PANIC("This should never happen! Implementation failure while setting the Payload-Type.");
382 }
383}
384
385template <typename Payload, typename UserHeader, ServiceType S>
386inline void
387ServiceBuilderPublishSubscribe<Payload, UserHeader, S>::override_payload_type_details(const TypeDetail& value) {
388 // payload type details provided at runtime
389 const auto type_variant =
390 value.variant() == TypeVariant::FixedSize ? iox2_type_variant_e_FIXED_SIZE : iox2_type_variant_e_DYNAMIC;
391
392 const auto result = iox2_service_builder_pub_sub_set_payload_type_details(
393 &m_handle, type_variant, value.type_name(), std::strlen(value.type_name()), value.size(), value.alignment());
394
395 if (result != IOX2_OK) {
396 IOX2_PANIC("This should never happen! Implementation failure while setting the Payload-Type.");
397 }
398}
399
400
401template <typename Payload, typename UserHeader, ServiceType S>
402template <typename NewHeader>
405 // required here since we just change the template header type but the builder structure stays the same
406 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
407 return std::move(*reinterpret_cast<ServiceBuilderPublishSubscribe<Payload, NewHeader, S>*>(this));
408}
409
410
411template <typename Payload, typename UserHeader, ServiceType S>
412template <typename U, typename>
415 iox2_service_builder_pub_sub_set_flatbuffer_schema_path(&m_handle, value.as_string().unchecked_access().c_str());
416 return std::move(*this);
417}
418
419template <typename Payload, typename UserHeader, ServiceType S>
424
425template <typename Payload, typename UserHeader, ServiceType S>
427 const TypeDetail& value)
428 -> std::enable_if_t<std::is_same<UserHeader, CustomHeaderMarker>::value> {
429 builder.m_user_header_type_details_override = bb::Optional<TypeDetail>(value);
430}
431
432template <typename Payload, typename UserHeader, ServiceType S>
434 const TypeDetail& value)
435 -> std::enable_if_t<std::is_same<Payload, bb::Slice<CustomPayloadMarker>>::value> {
436 builder.m_payload_type_details_override = bb::Optional<TypeDetail>(value);
437}
438
439template <typename Payload, typename UserHeader, ServiceType S>
441 Expected<PortFactoryPublishSubscribe<S, Payload, UserHeader>, PublishSubscribeOpenOrCreateError> {
442 set_parameters();
443
444 iox2_port_factory_pub_sub_h port_factory_handle {};
445 auto result = iox2_service_builder_pub_sub_open_or_create(m_handle, nullptr, &port_factory_handle);
446 m_handle = nullptr;
447
448 if (result == IOX2_OK) {
449 return PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle);
450 }
451
453}
454
455template <typename Payload, typename UserHeader, ServiceType S>
457 Expected<PortFactoryPublishSubscribe<S, Payload, UserHeader>, PublishSubscribeOpenError> {
458 set_parameters();
459
460 iox2_port_factory_pub_sub_h port_factory_handle {};
461 auto result = iox2_service_builder_pub_sub_open(m_handle, nullptr, &port_factory_handle);
462 m_handle = nullptr;
463
464 if (result == IOX2_OK) {
465 return PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle);
466 }
467
469}
470
471template <typename Payload, typename UserHeader, ServiceType S>
473 Expected<PortFactoryPublishSubscribe<S, Payload, UserHeader>, PublishSubscribeCreateError> {
474 set_parameters();
475
476 iox2_port_factory_pub_sub_h port_factory_handle {};
477 auto result = iox2_service_builder_pub_sub_create(m_handle, nullptr, &port_factory_handle);
478 m_handle = nullptr;
479
480 if (result == IOX2_OK) {
481 return PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle);
482 }
483
485}
486
487template <typename Payload, typename UserHeader, ServiceType S>
489 const AttributeVerifier&
492 set_parameters();
493
494 iox2_port_factory_pub_sub_h port_factory_handle {};
495 auto result = iox2_service_builder_pub_sub_open_or_create_with_attributes(
496 m_handle, &required_attributes.m_handle, nullptr, &port_factory_handle);
497 m_handle = nullptr;
498
499 if (result == IOX2_OK) {
500 return PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle);
501 }
502
504}
505
506template <typename Payload, typename UserHeader, ServiceType S>
508 const AttributeVerifier&
511 set_parameters();
512
513 iox2_port_factory_pub_sub_h port_factory_handle {};
514 auto result = iox2_service_builder_pub_sub_open_with_attributes(
515 m_handle, &required_attributes.m_handle, nullptr, &port_factory_handle);
516 m_handle = nullptr;
517
518 if (result == IOX2_OK) {
519 return PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle);
520 }
521
523}
524
525template <typename Payload, typename UserHeader, ServiceType S>
529 set_parameters();
530
531 iox2_port_factory_pub_sub_h port_factory_handle {};
532 auto result = iox2_service_builder_pub_sub_create_with_attributes(
533 m_handle, &attributes.m_handle, nullptr, &port_factory_handle);
534 m_handle = nullptr;
535
536 if (result == IOX2_OK) {
537 return PortFactoryPublishSubscribe<S, Payload, UserHeader>(port_factory_handle);
538 }
539
541}
542} // namespace iox2
543
544#endif
#define IOX2_PANIC(message)
calls panic handler and does not return
#define IOX2_BUILDER_OPTIONAL(type, name)
Definition builder.hpp:47
Builder to create new [MessagingPattern::PublishSubscribe] based [Service]s.
auto user_header() &&-> ServiceBuilderPublishSubscribe< Payload, NewHeader, S > &&
Sets the user header type of the [Service].
auto open_or_create_with_attributes(const AttributeVerifier &required_attributes) &&-> bb::Expected< PortFactoryPublishSubscribe< S, Payload, UserHeader >, PublishSubscribeOpenOrCreateError >
auto max_publishers(const uint64_t value) -> decltype(auto)
auto operator=(ServiceBuilderPublishSubscribe &&rhs) noexcept -> ServiceBuilderPublishSubscribe &
auto open_with_attributes(const AttributeVerifier &required_attributes) &&-> bb::Expected< PortFactoryPublishSubscribe< S, Payload, UserHeader >, PublishSubscribeOpenError >
auto history_size(const uint64_t value) -> decltype(auto)
auto subscriber_max_borrowed_samples(const uint64_t value) -> decltype(auto)
auto create_with_attributes(const AttributeSpecifier &attributes) &&-> bb::Expected< PortFactoryPublishSubscribe< S, Payload, UserHeader >, PublishSubscribeCreateError >
Creates a new [Service] with a set of attributes.
auto operator=(const ServiceBuilderPublishSubscribe &) -> ServiceBuilderPublishSubscribe &=delete
ServiceBuilderPublishSubscribe(ServiceBuilderPublishSubscribe &&rhs) noexcept
auto open_or_create() &&-> bb::Expected< PortFactoryPublishSubscribe< S, Payload, UserHeader >, PublishSubscribeOpenOrCreateError >
auto open() &&-> bb::Expected< PortFactoryPublishSubscribe< S, Payload, UserHeader >, PublishSubscribeOpenError >
Opens an existing [Service].
auto flatbuffer_schema_path(const bb::FilePath &value) &&-> ServiceBuilderPublishSubscribe< U, UserHeader, S > &&
auto max_nodes(const uint64_t value) -> decltype(auto)
auto max_subscribers(const uint64_t value) -> decltype(auto)
auto payload_alignment(const uint64_t value) -> decltype(auto)
auto resume_build() &-> ServiceBuilderPublishSubscribe< Payload, UserHeader, S > &&
auto enable_safe_overflow(const bool value) -> decltype(auto)
friend auto set_user_header_type_details(ServiceBuilderPublishSubscribe< P, U, St > &builder, const TypeDetail &value) -> std::enable_if_t< std::is_same< U, CustomHeaderMarker >::value >
auto subscriber_max_buffer_size(const uint64_t value) -> decltype(auto)
ServiceBuilderPublishSubscribe(const ServiceBuilderPublishSubscribe &)=delete
auto create() &&-> bb::Expected< PortFactoryPublishSubscribe< S, Payload, UserHeader >, PublishSubscribeCreateError >
Creates a new [Service].
friend auto set_payload_type_details(ServiceBuilderPublishSubscribe< P, U, St > &builder, const TypeDetail &value) -> std::enable_if_t< std::is_same< P, bb::Slice< CustomPayloadMarker > >::value >
Represents a path to a file. It is not allowed to end with a path separator since this would then be ...
Definition file_path.hpp:44
static auto from() -> std::enable_if_t<!std::is_same< T, void >::value, Layout >
Definition layout.hpp:67
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
iox2::bb::variation::Optional< T > Optional
Definition optional.hpp:25
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
auto get_type_name() -> TypeName
auto set_payload_type_details(ServiceBuilderPublishSubscribe< Payload, UserHeader, S > &builder, const TypeDetail &value) -> std::enable_if_t< std::is_same< Payload, bb::Slice< CustomPayloadMarker > >::value >
PublishSubscribeCreateError
Errors that can occur when a new [MessagingPattern::PublishSubscribe] [Service] shall be created.
auto set_user_header_type_details(ServiceBuilderPublishSubscribe< Payload, UserHeader, S > &builder, const TypeDetail &value) -> std::enable_if_t< std::is_same< UserHeader, CustomHeaderMarker >::value >
@ FixedSize
A fixed size type like [u64].
PublishSubscribeOpenError
Errors that can occur when an existing [MessagingPattern::PublishSubscribe] [Service] shall be opened...
static constexpr bool VALUE
Definition slice.hpp:159