iceoryx2
C++ 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:
66
69
83
86 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
87 auto initial_max_slice_len(uint64_t value) && -> PortFactoryPublisher&&;
88
91 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), void>>
92 auto initial_reserved_memory(uint64_t value) && -> PortFactoryPublisher&&;
93
98 template <typename T = Payload,
99 typename = std::enable_if_t<bb::IsSlice<T>::VALUE || has_flatbuffer_marker<T>(), void>>
101
109
118
121
122 private:
123 template <ServiceType, typename, typename>
125
126 explicit PortFactoryPublisher(iox2_port_factory_publisher_builder_h handle);
127 void drop();
128
129 iox2_port_factory_publisher_builder_h m_handle = nullptr;
130 bb::Optional<uint64_t> m_max_slice_len;
131 bb::Optional<AllocationStrategy> m_allocation_strategy;
132 bb::Optional<OverridePreallocationCallback> m_override_preallocation_callback;
133 bb::Optional<DegradationHandler*> m_degradation_handler;
134 bb::Optional<BackpressureHandler*> m_backpressure_handler;
135};
136
137template <ServiceType S, typename Payload, typename UserHeader>
138inline PortFactoryPublisher<S, Payload, UserHeader>::PortFactoryPublisher(iox2_port_factory_publisher_builder_h handle)
139 : m_handle { handle } {
140}
141
142template <ServiceType S, typename Payload, typename UserHeader>
143inline void PortFactoryPublisher<S, Payload, UserHeader>::drop() {
144 if (m_handle != nullptr) {
145 iox2_port_factory_publisher_builder_drop(m_handle);
146 m_handle = nullptr;
147 }
148}
149
150template <ServiceType S, typename Payload, typename UserHeader>
152 *this = std::move(rhs);
153}
154
155template <ServiceType S, typename Payload, typename UserHeader>
158 if (this != &rhs) {
159 drop();
160 m_handle = rhs.m_handle;
161 rhs.m_handle = nullptr;
162 m_backpressure_strategy = std::move(rhs.m_backpressure_strategy);
163 m_max_loaned_samples = std::move(rhs.m_max_loaned_samples);
164 m_name = std::move(rhs.m_name);
165 m_max_slice_len = std::move(rhs.m_max_slice_len);
166 m_allocation_strategy = std::move(rhs.m_allocation_strategy);
167 m_override_preallocation_callback = std::move(rhs.m_override_preallocation_callback);
168 m_degradation_handler = std::move(rhs.m_degradation_handler);
169 m_backpressure_handler = std::move(rhs.m_backpressure_handler);
170 }
171
172 return *this;
173}
174
175template <ServiceType S, typename Payload, typename UserHeader>
179
180template <ServiceType S, typename Payload, typename UserHeader>
181template <typename T, typename>
182inline auto
184 m_max_slice_len.emplace(value);
185 return std::move(*this);
186}
187
188template <ServiceType S, typename Payload, typename UserHeader>
190 const OverridePreallocationCallback& callback) && -> PortFactoryPublisher&& {
191 m_override_preallocation_callback.emplace(callback);
192 return std::move(*this);
193}
194
195template <ServiceType S, typename Payload, typename UserHeader>
196template <typename T, typename>
199 m_allocation_strategy.emplace(value);
200 return std::move(*this);
201}
202
203template <ServiceType S, typename Payload, typename UserHeader>
204template <typename T, typename>
205inline auto
207 m_max_slice_len.emplace(value);
208 return std::move(*this);
209}
210
211template <ServiceType S, typename Payload, typename UserHeader>
213 DegradationHandler* handler) && -> PortFactoryPublisher&& {
214 m_degradation_handler.emplace(handler);
215 return std::move(*this);
216}
217
218template <ServiceType S, typename Payload, typename UserHeader>
220 BackpressureHandler* handler) && -> PortFactoryPublisher&& {
221 m_backpressure_handler.emplace(handler);
222 return std::move(*this);
223}
224
225template <ServiceType S, typename Payload, typename UserHeader>
228 if (m_backpressure_strategy.has_value()) {
229 iox2_port_factory_publisher_builder_backpressure_strategy(
230 &m_handle, static_cast<iox2_backpressure_strategy_e>(bb::into<int>(m_backpressure_strategy.value())));
231 }
232 if (m_max_slice_len.has_value()) {
233 iox2_port_factory_publisher_builder_set_initial_max_slice_len(&m_handle, m_max_slice_len.value());
234 } else {
235 iox2_port_factory_publisher_builder_set_initial_max_slice_len(&m_handle, 1);
236 }
237 if (m_max_loaned_samples.has_value()) {
238 iox2_port_factory_publisher_builder_set_max_loaned_samples(&m_handle, m_max_loaned_samples.value());
239 }
240 if (m_allocation_strategy.has_value()) {
241 iox2_port_factory_publisher_builder_set_allocation_strategy(
242 &m_handle, bb::into<iox2_allocation_strategy_e>(m_allocation_strategy.value()));
243 }
244
245 if (m_degradation_handler.has_value()) {
246 iox2_port_factory_publisher_builder_set_degradation_handler(
247 &m_handle, detail::degradation_handler_delegate, static_cast<void*>(m_degradation_handler.value()));
248 }
249
250 if (m_backpressure_handler.has_value()) {
251 iox2_port_factory_publisher_builder_set_backpressure_handler(
252 &m_handle, detail::backpressure_handler_delegate, static_cast<void*>(m_backpressure_handler.value()));
253 }
254
255 if (m_override_preallocation_callback.has_value()) {
256 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be a raw pointer - crosses FFI boundary
257 auto* callback = new OverridePreallocationCallback(m_override_preallocation_callback.value());
258 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be a raw pointer - crosses FFI boundary
260 iox2_port_factory_publisher_builder_override_samples_preallocation(
261 &m_handle, internal::override_callback, static_cast<void*>(ctx));
262 }
263
264 if (m_name.has_value()) {
265 const auto* name_ptr = m_name.value().as_view().native_ptr();
266 iox2_port_factory_publisher_builder_set_name(&m_handle, name_ptr);
267 }
268
269 iox2_publisher_h pub_handle {};
270
271 auto result = iox2_port_factory_publisher_builder_create(m_handle, nullptr, &pub_handle);
272 m_handle = nullptr;
273
274 if (result == IOX2_OK) {
275 return Publisher<S, Payload, UserHeader>(pub_handle);
276 }
277
279}
280} // namespace iox2
281
282#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(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].
PortFactoryPublisher(PortFactoryPublisher &&rhs) noexcept
auto name(const PortName value) -> decltype(auto)
auto operator=(PortFactoryPublisher &&rhs) noexcept -> PortFactoryPublisher &
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 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 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 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
constexpr auto has_flatbuffer_marker() -> bool
Definition marker.hpp:53
iox2::bb::StaticFunction< size_t(size_t)> OverridePreallocationCallback
iox2::bb::StaticFunction< BackpressureAction(const BackpressureInfo &)> BackpressureHandler
iox2::bb::StaticFunction< DegradationAction(DegradationCause, DegradationInfo &)> DegradationHandler