iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
port_factory_publisher.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_PORTFACTORY_PUBLISHER_HPP
14#define IOX2_PORTFACTORY_PUBLISHER_HPP
15
20#include "iox2/bb/expected.hpp"
21#include "iox2/bb/optional.hpp"
25#include "iox2/marker.hpp"
26#include "iox2/port_name.hpp"
27#include "iox2/publisher.hpp"
28#include "iox2/service_type.hpp"
29
30#include <cstdint>
31
32namespace iox2 {
35template <ServiceType S, typename Payload, typename UserHeader>
37 public:
39#ifdef DOXYGEN_MACRO_FIX
40 auto backpressure_strategy(const BackpressureStrategy value) -> decltype(auto);
41#else
43#endif
44
47#ifdef DOXYGEN_MACRO_FIX
48 auto max_loaned_samples(const uint64_t value) -> decltype(auto);
49#else
51#endif
52
56#ifdef DOXYGEN_MACRO_FIX
57 auto name(const PortName value) -> decltype(auto);
58#else
60#endif
61
62 public:
68
82
85 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
86 auto initial_max_slice_len(uint64_t value) && -> PortFactoryPublisher&&;
87
90 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), void>>
91 auto initial_reserved_memory(uint64_t value) && -> PortFactoryPublisher&&;
92
97 template <typename T = Payload,
98 typename = std::enable_if_t<bb::IsSlice<T>::VALUE || has_flatbuffer_marker<T>(), void>>
100
108
117
120
121 private:
122 template <ServiceType, typename, typename>
124
125 explicit PortFactoryPublisher(iox2_port_factory_publisher_builder_h handle);
126
127 iox2_port_factory_publisher_builder_h m_handle = nullptr;
128 bb::Optional<uint64_t> m_max_slice_len;
129 bb::Optional<AllocationStrategy> m_allocation_strategy;
130 bb::Optional<OverridePreallocationCallback> m_override_preallocation_callback;
131 bb::Optional<DegradationHandler* const> m_degradation_handler;
132 bb::Optional<BackpressureHandler* const> m_backpressure_handler;
133};
134
135template <ServiceType S, typename Payload, typename UserHeader>
136inline PortFactoryPublisher<S, Payload, UserHeader>::PortFactoryPublisher(iox2_port_factory_publisher_builder_h handle)
137 : m_handle { handle } {
138}
139
140template <ServiceType S, typename Payload, typename UserHeader>
141template <typename T, typename>
142inline auto
144 m_max_slice_len.emplace(value);
145 return std::move(*this);
146}
147
148template <ServiceType S, typename Payload, typename UserHeader>
150 const OverridePreallocationCallback& callback) && -> PortFactoryPublisher&& {
151 m_override_preallocation_callback.emplace(callback);
152 return std::move(*this);
153}
154
155template <ServiceType S, typename Payload, typename UserHeader>
156template <typename T, typename>
159 m_allocation_strategy.emplace(value);
160 return std::move(*this);
161}
162
163template <ServiceType S, typename Payload, typename UserHeader>
164template <typename T, typename>
165inline auto
167 m_max_slice_len.emplace(value);
168 return std::move(*this);
169}
170
171template <ServiceType S, typename Payload, typename UserHeader>
173 DegradationHandler* handler) && -> PortFactoryPublisher&& {
174 m_degradation_handler.emplace(handler);
175 return std::move(*this);
176}
177
178template <ServiceType S, typename Payload, typename UserHeader>
180 BackpressureHandler* handler) && -> PortFactoryPublisher&& {
181 m_backpressure_handler.emplace(handler);
182 return std::move(*this);
183}
184
185template <ServiceType S, typename Payload, typename UserHeader>
188 if (m_backpressure_strategy.has_value()) {
189 iox2_port_factory_publisher_builder_backpressure_strategy(
190 &m_handle, static_cast<iox2_backpressure_strategy_e>(bb::into<int>(m_backpressure_strategy.value())));
191 }
192 if (m_max_slice_len.has_value()) {
193 iox2_port_factory_publisher_builder_set_initial_max_slice_len(&m_handle, m_max_slice_len.value());
194 } else {
195 iox2_port_factory_publisher_builder_set_initial_max_slice_len(&m_handle, 1);
196 }
197 if (m_max_loaned_samples.has_value()) {
198 iox2_port_factory_publisher_builder_set_max_loaned_samples(&m_handle, m_max_loaned_samples.value());
199 }
200 if (m_allocation_strategy.has_value()) {
201 iox2_port_factory_publisher_builder_set_allocation_strategy(
202 &m_handle, bb::into<iox2_allocation_strategy_e>(m_allocation_strategy.value()));
203 }
204
205 if (m_degradation_handler.has_value()) {
206 iox2_port_factory_publisher_builder_set_degradation_handler(
207 &m_handle, detail::degradation_handler_delegate, static_cast<void*>(m_degradation_handler.value()));
208 }
209
210 if (m_backpressure_handler.has_value()) {
211 iox2_port_factory_publisher_builder_set_backpressure_handler(
212 &m_handle, detail::backpressure_handler_delegate, static_cast<void*>(m_backpressure_handler.value()));
213 }
214
215 if (m_override_preallocation_callback.has_value()) {
216 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be a raw pointer - crosses FFI boundary
217 auto* callback = new OverridePreallocationCallback(m_override_preallocation_callback.value());
218 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be a raw pointer - crosses FFI boundary
220 iox2_port_factory_publisher_builder_override_samples_preallocation(
221 &m_handle, internal::override_callback, static_cast<void*>(ctx));
222 }
223
224 if (m_name.has_value()) {
225 const auto* name_ptr = m_name.value().as_view().native_ptr();
226 iox2_port_factory_publisher_builder_set_name(&m_handle, name_ptr);
227 }
228
229 iox2_publisher_h pub_handle {};
230
231 auto result = iox2_port_factory_publisher_builder_create(m_handle, nullptr, &pub_handle);
232
233 if (result == IOX2_OK) {
234 return Publisher<S, Payload, UserHeader>(pub_handle);
235 }
236
237 return bb::err(bb::into<PublisherCreateError>(result));
238}
239} // namespace iox2
240
241#endif
#define IOX2_BUILDER_OPTIONAL(type, name)
Definition builder.hpp:47
auto override_sample_preallocation(const OverridePreallocationCallback &callback) &&-> PortFactoryPublisher &&
auto set_backpressure_handler(BackpressureHandler *handler) &&-> PortFactoryPublisher &&
auto max_loaned_samples(const uint64_t value) -> decltype(auto)
PortFactoryPublisher(PortFactoryPublisher &&)=default
PortFactoryPublisher(const PortFactoryPublisher &)=delete
auto set_degradation_handler(DegradationHandler *handler) &&-> PortFactoryPublisher &&
auto initial_max_slice_len(uint64_t value) &&-> PortFactoryPublisher &&
auto allocation_strategy(AllocationStrategy value) &&-> PortFactoryPublisher &&
auto backpressure_strategy(const BackpressureStrategy value) -> decltype(auto)
Sets the [BackpressureStrategy].
auto name(const PortName value) -> decltype(auto)
auto operator=(const PortFactoryPublisher &) -> PortFactoryPublisher &=delete
auto create() &&-> bb::Expected< Publisher< S, Payload, UserHeader >, PublisherCreateError >
Creates a new [Publisher] or returns a [PublisherCreateError] on failure.
auto operator=(PortFactoryPublisher &&) -> PortFactoryPublisher &=default
auto initial_reserved_memory(uint64_t value) &&-> PortFactoryPublisher &&
Represent the name for a [Port].
Definition port_name.hpp:52
Sending endpoint of a publish-subscriber based communication.
Definition publisher.hpp:40
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Optional< T > Optional
Definition optional.hpp:25
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
auto backpressure_handler_delegate(iox2_backpressure_info_h_ref info_handle, iox2_callback_context callback_cxt) -> iox2_backpressure_action_e
auto degradation_handler_delegate(iox2_degradation_cause_e degradation_cause, iox2_degradation_info_h_ref info_handle, iox2_callback_context callback_cxt) -> iox2_degradation_action_e
auto override_callback(size_t value, iox2_callback_context ctx) -> size_t
iox2::bb::StaticFunction< size_t(size_t)> OverridePreallocationCallback