13#ifndef IOX2_PUBLISHER_HPP
14#define IOX2_PUBLISHER_HPP
19#include "iox2/deployment/iceoryx2_cxx_feature_flags.hpp"
20#include "iox2/iceoryx2.h"
32#if IOX2_FEATURE_FLATBUFFERS
34#include <flatbuffers/flatbuffer_builder.h>
39template <ServiceType S,
typename Payload,
typename UserHeader>
62 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
70 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
76 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE,
void>>
82 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
85#if IOX2_FEATURE_FLATBUFFERS
87 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(),
void>>
95 template <
typename T = Payload,
96 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(),
void>>
104 template <
typename T = Payload,
105 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(),
void>>
114 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
121 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
133 template <ServiceType,
typename,
typename>
136 explicit Publisher(iox2_publisher_h handle);
139 iox2_publisher_h m_handle =
nullptr;
142template <ServiceType S,
typename Payload,
typename UserHeader>
144 : m_handle { handle } {
147template <ServiceType S,
typename Payload,
typename UserHeader>
148inline void Publisher<S, Payload, UserHeader>::drop() {
149 if (m_handle !=
nullptr) {
150 iox2_publisher_drop(m_handle);
155template <ServiceType S,
typename Payload,
typename UserHeader>
157 *
this = std::move(rhs);
160template <ServiceType S,
typename Payload,
typename UserHeader>
164 m_handle = rhs.m_handle;
165 rhs.m_handle =
nullptr;
171template <ServiceType S,
typename Payload,
typename UserHeader>
176template <ServiceType S,
typename Payload,
typename UserHeader>
178 return iox2::bb::into<BackpressureStrategy>(
static_cast<int>(iox2_publisher_backpressure_strategy(&m_handle)));
182template <ServiceType S,
typename Payload,
typename UserHeader>
183template <
typename T,
typename>
185 return iox2_publisher_initial_max_slice_len(&m_handle);
188template <ServiceType S,
typename Payload,
typename UserHeader>
190 return iox2_publisher_max_loaned_samples(&m_handle);
193template <ServiceType S,
typename Payload,
typename UserHeader>
194template <
typename T,
typename>
196 return iox2::bb::into<AllocationStrategy>(
static_cast<int>(iox2_publisher_allocation_strategy(&m_handle)));
199template <ServiceType S,
typename Payload,
typename UserHeader>
201 iox2_unique_publisher_id_h id_handle =
nullptr;
203 iox2_publisher_id(&m_handle,
nullptr, &id_handle);
207template <ServiceType S,
typename Payload,
typename UserHeader>
209 const auto* port_name_ptr = iox2_publisher_name(&m_handle);
213template <ServiceType S,
typename Payload,
typename UserHeader>
214template <
typename T,
typename>
217 static_assert(std::is_trivially_copyable<Payload>::value,
218 "The publisher supports only trivially copyable payload types.");
220 size_t number_of_recipients = 0;
222 iox2_publisher_send_copy(&m_handle,
static_cast<const void*
>(&payload),
sizeof(Payload), &number_of_recipients);
224 if (result == IOX2_OK) {
225 return number_of_recipients;
228 return bb::err(iox2::bb::into<SendError>(result));
231template <ServiceType S,
typename Payload,
typename UserHeader>
232template <
typename T,
typename>
235 size_t number_of_recipients = 0;
236 auto result = iox2_publisher_send_slice_copy(&m_handle,
238 sizeof(
typename Payload::ValueType),
239 payload.number_of_elements(),
240 &number_of_recipients);
242 if (result == IOX2_OK) {
243 return number_of_recipients;
246 return bb::err(iox2::bb::into<SendError>(result));
249#if IOX2_FEATURE_FLATBUFFERS
251template <ServiceType S,
typename Payload,
typename UserHeader>
252template <
typename T,
typename>
257 auto result = iox2_publisher_loan_slice_uninit(&m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, 1);
259 if (result != IOX2_OK) {
260 return bb::err(iox2::bb::into<LoanError>(result));
263 iox2_resizable_memory_publish_subscribe_h resizable_memory_handle {};
264 iox2_sample_mut_create_resizable_memory(&sample.m_sample.m_handle,
nullptr, &resizable_memory_handle);
269 auto initial_size = sample.m_memory->
len();
270 sample.m_flatbuffer_builder = flatbuffers::FlatBufferBuilder(initial_size, sample.m_memory,
true);
272 return std::move(sample);
277template <ServiceType S,
typename Payload,
typename UserHeader>
278template <
typename T,
typename>
283 auto result = iox2_publisher_loan_slice_uninit(&m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, 1);
286 if (result == IOX2_OK) {
287 return std::move(sample);
290 return bb::err(iox2::bb::into<LoanError>(result));
293template <ServiceType S,
typename Payload,
typename UserHeader>
294template <
typename T,
typename>
298 if (!sample.has_value()) {
299 return bb::err(sample.error());
302 new (&sample->payload_mut()) Payload();
307template <ServiceType S,
typename Payload,
typename UserHeader>
308template <
typename T,
typename>
311 auto sample_uninit = loan_slice_uninit(number_of_elements);
313 if (!sample_uninit.has_value()) {
314 return bb::err(sample_uninit.error());
316 auto sample_init = std::move(sample_uninit.value());
318 for (
auto& item : sample_init.payload_mut()) {
319 new (&item) ValueType();
325template <ServiceType S,
typename Payload,
typename UserHeader>
326template <
typename T,
typename>
331 auto result = iox2_publisher_loan_slice_uninit(
332 &m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, number_of_elements);
335 if (result == IOX2_OK) {
336 return std::move(sample);
339 return bb::err(iox2::bb::into<LoanError>(result));
342template <ServiceType S,
typename Payload,
typename UserHeader>
344 auto result = iox2_publisher_update_connections(&m_handle);
345 if (result != IOX2_OK) {
346 return bb::err(iox2::bb::into<ConnectionFailure>(result));
Non-owning view of a [PortName].
static auto from_native_ptr(iox2_port_name_ptr m_ptr) -> PortNameView
Creates a [PortNameView] from a native [iox2_port_name_ptr].
Sending endpoint of a publish-subscriber based communication.
auto loan() -> bb::Expected< SampleMut< S, Payload, UserHeader >, LoanError >
auto operator=(Publisher &&rhs) noexcept -> Publisher &
auto id() const -> UniquePublisherId
Returns the [UniquePublisherId] of the [Publisher].
auto loan_uninit() -> bb::Expected< SampleMutUninit< S, Payload, UserHeader >, LoanError >
auto loan_flatbuffer() -> bb::Expected< SampleMutUninit< S, Payload, UserHeader >, LoanError >
Acquires a [SampleMutUninit] with an integrated flatbuffer builder.
auto loan_slice_uninit(uint64_t number_of_elements) -> bb::Expected< SampleMutUninit< S, T, UserHeader >, LoanError >
auto initial_max_slice_len() const -> uint64_t
Returns the maximum number of elements that can be loaned in a slice.
auto send_copy(const Payload &payload) const -> bb::Expected< size_t, SendError >
auto allocation_strategy() const -> AllocationStrategy
auto name() const -> PortNameView
Returns the [PortNameView] of the [Publisher].
Publisher(Publisher &&rhs) noexcept
auto update_connections() -> bb::Expected< void, ConnectionFailure >
Publisher(const Publisher &)=delete
auto max_loaned_samples() const -> uint64_t
Returns the maximum number of [SampleMut] that can be loaned in parallel.
auto backpressure_strategy() const -> BackpressureStrategy
auto operator=(const Publisher &) -> Publisher &=delete
auto loan_slice(uint64_t number_of_elements) -> bb::Expected< SampleMut< S, T, UserHeader >, LoanError >
auto send_slice_copy(bb::ImmutableSlice< ValueType > &payload) const -> bb::Expected< size_t, SendError >
The system-wide unique id of a [Publisher].
A class representing a slice of contiguous elements of type T.
Internal helper class to handle dynamic memory allocations used in e.g. flatbuffers.
auto len() const -> size_t
constexpr auto err(const E &error) -> Unexpected< E >
iox2::bb::variation::Expected< T, E > Expected
SendError
Failure that can be emitted when data is sent.
auto assume_init(RequestMutUninit< Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader > &&self) -> RequestMut< Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader >
auto loan_uninit(EntryHandleMut< S, KeyType, ValueType > &&self) -> EntryValueUninit< S, KeyType, ValueType >
static void placement_default(S &payload)