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/publisher.hpp"
26#include "iox2/service_type.hpp"
27
28#include <cstdint>
29
30namespace iox2 {
33template <ServiceType S, typename Payload, typename UserHeader>
35 public:
37#ifdef DOXYGEN_MACRO_FIX
38 auto backpressure_strategy(const BackpressureStrategy value) -> decltype(auto);
39#else
41#endif
42
45#ifdef DOXYGEN_MACRO_FIX
46 auto max_loaned_samples(const uint64_t value) -> decltype(auto);
47#else
49#endif
50
51 public:
57
71
74 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
75 auto initial_max_slice_len(uint64_t value) && -> PortFactoryPublisher&&;
76
81 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
83
91
100
103
104 private:
105 template <ServiceType, typename, typename>
107
108 explicit PortFactoryPublisher(iox2_port_factory_publisher_builder_h handle);
109
110 iox2_port_factory_publisher_builder_h m_handle = nullptr;
111 bb::Optional<uint64_t> m_max_slice_len;
112 bb::Optional<AllocationStrategy> m_allocation_strategy;
113 bb::Optional<OverridePreallocationCallback> m_override_preallocation_callback;
114 bb::Optional<DegradationHandler* const> m_degradation_handler;
115 bb::Optional<BackpressureHandler* const> m_backpressure_handler;
116};
117
118template <ServiceType S, typename Payload, typename UserHeader>
119inline PortFactoryPublisher<S, Payload, UserHeader>::PortFactoryPublisher(iox2_port_factory_publisher_builder_h handle)
120 : m_handle { handle } {
121}
122
123template <ServiceType S, typename Payload, typename UserHeader>
124template <typename T, typename>
125inline auto
127 m_max_slice_len.emplace(value);
128 return std::move(*this);
129}
130
131template <ServiceType S, typename Payload, typename UserHeader>
133 const OverridePreallocationCallback& callback) && -> PortFactoryPublisher&& {
134 m_override_preallocation_callback.emplace(callback);
135 return std::move(*this);
136}
137
138template <ServiceType S, typename Payload, typename UserHeader>
139template <typename T, typename>
142 m_allocation_strategy.emplace(value);
143 return std::move(*this);
144}
145
146template <ServiceType S, typename Payload, typename UserHeader>
148 DegradationHandler* handler) && -> PortFactoryPublisher&& {
149 m_degradation_handler.emplace(handler);
150 return std::move(*this);
151}
152
153template <ServiceType S, typename Payload, typename UserHeader>
155 BackpressureHandler* handler) && -> PortFactoryPublisher&& {
156 m_backpressure_handler.emplace(handler);
157 return std::move(*this);
158}
159
160template <ServiceType S, typename Payload, typename UserHeader>
163 if (m_backpressure_strategy.has_value()) {
164 iox2_port_factory_publisher_builder_backpressure_strategy(
165 &m_handle, static_cast<iox2_backpressure_strategy_e>(bb::into<int>(m_backpressure_strategy.value())));
166 }
167 if (m_max_slice_len.has_value()) {
168 iox2_port_factory_publisher_builder_set_initial_max_slice_len(&m_handle, m_max_slice_len.value());
169 } else {
170 iox2_port_factory_publisher_builder_set_initial_max_slice_len(&m_handle, 1);
171 }
172 if (m_max_loaned_samples.has_value()) {
173 iox2_port_factory_publisher_builder_set_max_loaned_samples(&m_handle, m_max_loaned_samples.value());
174 }
175 if (m_allocation_strategy.has_value()) {
176 iox2_port_factory_publisher_builder_set_allocation_strategy(
177 &m_handle, bb::into<iox2_allocation_strategy_e>(m_allocation_strategy.value()));
178 }
179
180 if (m_degradation_handler.has_value()) {
181 iox2_port_factory_publisher_builder_set_degradation_handler(
182 &m_handle, detail::degradation_handler_delegate, static_cast<void*>(m_degradation_handler.value()));
183 }
184
185 if (m_backpressure_handler.has_value()) {
186 iox2_port_factory_publisher_builder_set_backpressure_handler(
187 &m_handle, detail::backpressure_handler_delegate, static_cast<void*>(m_backpressure_handler.value()));
188 }
189
190 if (m_override_preallocation_callback.has_value()) {
191 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be a raw pointer - crosses FFI boundary
192 auto* callback = new OverridePreallocationCallback(m_override_preallocation_callback.value());
193 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory) must be a raw pointer - crosses FFI boundary
195 iox2_port_factory_publisher_builder_override_samples_preallocation(
196 &m_handle, internal::override_callback, static_cast<void*>(ctx));
197 }
198
199
200 iox2_publisher_h pub_handle {};
201
202 auto result = iox2_port_factory_publisher_builder_create(m_handle, nullptr, &pub_handle);
203
204 if (result == IOX2_OK) {
205 return Publisher<S, Payload, UserHeader>(pub_handle);
206 }
207
208 return bb::err(bb::into<PublisherCreateError>(result));
209}
210} // namespace iox2
211
212#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 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
Sending endpoint of a publish-subscriber based communication.
Definition publisher.hpp:34
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