13#ifndef IOX2_PUBLISHER_HPP
14#define IOX2_PUBLISHER_HPP
19#include "iox2/iceoryx2.h"
20#include "iox2/iceoryx2_cxx_deployment.hpp"
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>>
68 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE,
void>>
74 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
77#if IOX2_FEATURE_FLATBUFFERS
79 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(),
void>>
87 template <
typename T = Payload,
88 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(),
void>>
96 template <
typename T = Payload,
97 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(),
void>>
106 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
113 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
125 template <ServiceType,
typename,
typename>
128 explicit Publisher(iox2_publisher_h handle);
131 iox2_publisher_h m_handle =
nullptr;
134template <ServiceType S,
typename Payload,
typename UserHeader>
136 : m_handle { handle } {
139template <ServiceType S,
typename Payload,
typename UserHeader>
140inline void Publisher<S, Payload, UserHeader>::drop() {
141 if (m_handle !=
nullptr) {
142 iox2_publisher_drop(m_handle);
147template <ServiceType S,
typename Payload,
typename UserHeader>
149 *
this = std::move(rhs);
152template <ServiceType S,
typename Payload,
typename UserHeader>
156 m_handle = rhs.m_handle;
157 rhs.m_handle =
nullptr;
163template <ServiceType S,
typename Payload,
typename UserHeader>
168template <ServiceType S,
typename Payload,
typename UserHeader>
170 return iox2::bb::into<BackpressureStrategy>(
static_cast<int>(iox2_publisher_backpressure_strategy(&m_handle)));
174template <ServiceType S,
typename Payload,
typename UserHeader>
175template <
typename T,
typename>
177 return iox2_publisher_initial_max_slice_len(&m_handle);
180template <ServiceType S,
typename Payload,
typename UserHeader>
182 iox2_unique_publisher_id_h id_handle =
nullptr;
184 iox2_publisher_id(&m_handle,
nullptr, &id_handle);
188template <ServiceType S,
typename Payload,
typename UserHeader>
190 const auto* port_name_ptr = iox2_publisher_name(&m_handle);
194template <ServiceType S,
typename Payload,
typename UserHeader>
195template <
typename T,
typename>
198 static_assert(std::is_trivially_copyable<Payload>::value,
199 "The publisher supports only trivially copyable payload types.");
201 size_t number_of_recipients = 0;
203 iox2_publisher_send_copy(&m_handle,
static_cast<const void*
>(&payload),
sizeof(Payload), &number_of_recipients);
205 if (result == IOX2_OK) {
206 return number_of_recipients;
209 return bb::err(iox2::bb::into<SendError>(result));
212template <ServiceType S,
typename Payload,
typename UserHeader>
213template <
typename T,
typename>
216 size_t number_of_recipients = 0;
217 auto result = iox2_publisher_send_slice_copy(&m_handle,
219 sizeof(
typename Payload::ValueType),
220 payload.number_of_elements(),
221 &number_of_recipients);
223 if (result == IOX2_OK) {
224 return number_of_recipients;
227 return bb::err(iox2::bb::into<SendError>(result));
230#if IOX2_FEATURE_FLATBUFFERS
232template <ServiceType S,
typename Payload,
typename UserHeader>
233template <
typename T,
typename>
238 auto result = iox2_publisher_loan_slice_uninit(&m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, 1);
240 if (result != IOX2_OK) {
241 return bb::err(iox2::bb::into<LoanError>(result));
244 iox2_resizable_memory_publish_subscribe_h resizable_memory_handle {};
245 iox2_sample_mut_create_resizable_memory_builder(&sample.m_sample.m_handle,
nullptr, &resizable_memory_handle);
248 sample.m_memory =
new internal::ResizableMemoryPublishSubscribe<S>(resizable_memory_handle);
250 auto initial_size = sample.m_memory->len();
251 sample.m_flatbuffer_builder = flatbuffers::FlatBufferBuilder(initial_size, sample.m_memory,
true);
253 return std::move(sample);
258template <ServiceType S,
typename Payload,
typename UserHeader>
259template <
typename T,
typename>
264 auto result = iox2_publisher_loan_slice_uninit(&m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, 1);
267 if (result == IOX2_OK) {
268 return std::move(sample);
271 return bb::err(iox2::bb::into<LoanError>(result));
274template <ServiceType S,
typename Payload,
typename UserHeader>
275template <
typename T,
typename>
279 if (!sample.has_value()) {
280 return bb::err(sample.error());
288template <ServiceType S,
typename Payload,
typename UserHeader>
289template <
typename T,
typename>
292 auto sample_uninit = loan_slice_uninit(number_of_elements);
294 if (!sample_uninit.has_value()) {
295 return bb::err(sample_uninit.error());
297 auto sample_init = std::move(sample_uninit.value());
299 for (
auto& item : sample_init.payload_mut()) {
300 new (&item) ValueType();
306template <ServiceType S,
typename Payload,
typename UserHeader>
307template <
typename T,
typename>
312 auto result = iox2_publisher_loan_slice_uninit(
313 &m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, number_of_elements);
316 if (result == IOX2_OK) {
317 return std::move(sample);
320 return bb::err(iox2::bb::into<LoanError>(result));
323template <ServiceType S,
typename Payload,
typename UserHeader>
325 auto result = iox2_publisher_update_connections(&m_handle);
326 if (result != IOX2_OK) {
327 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_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 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 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 >
auto payload_mut() -> ValueType &
Returns a reference to the payload of the sample.
The system-wide unique id of a [Publisher].
A class representing a slice of contiguous elements of type 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)