13#ifndef IOX2_PUBLISHER_HPP
14#define IOX2_PUBLISHER_HPP
19#include "iox2/iceoryx2.h"
33template <ServiceType S,
typename Payload,
typename UserHeader>
53 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
59 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE,
void>>
62 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
69 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE,
void>>
77 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE,
void>>
86 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
93 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
108 explicit
Publisher(iox2_publisher_h handle);
111 iox2_publisher_h m_handle =
nullptr;
114template <
ServiceType S, typename Payload, typename UserHeader>
116 : m_handle { handle } {
119template <ServiceType S,
typename Payload,
typename UserHeader>
120inline void Publisher<S, Payload, UserHeader>::drop() {
121 if (m_handle !=
nullptr) {
122 iox2_publisher_drop(m_handle);
127template <ServiceType S,
typename Payload,
typename UserHeader>
129 *
this = std::move(rhs);
132template <ServiceType S,
typename Payload,
typename UserHeader>
136 m_handle = rhs.m_handle;
137 rhs.m_handle =
nullptr;
143template <ServiceType S,
typename Payload,
typename UserHeader>
148template <ServiceType S,
typename Payload,
typename UserHeader>
150 return iox2::bb::into<BackpressureStrategy>(
static_cast<int>(iox2_publisher_backpressure_strategy(&m_handle)));
154template <ServiceType S,
typename Payload,
typename UserHeader>
155template <
typename T,
typename>
157 return iox2_publisher_initial_max_slice_len(&m_handle);
160template <ServiceType S,
typename Payload,
typename UserHeader>
162 iox2_unique_publisher_id_h id_handle =
nullptr;
164 iox2_publisher_id(&m_handle,
nullptr, &id_handle);
168template <ServiceType S,
typename Payload,
typename UserHeader>
169template <
typename T,
typename>
172 static_assert(std::is_trivially_copyable<Payload>::value,
173 "The publisher supports only trivially copyable payload types.");
175 size_t number_of_recipients = 0;
177 iox2_publisher_send_copy(&m_handle,
static_cast<const void*
>(&payload),
sizeof(Payload), &number_of_recipients);
179 if (result == IOX2_OK) {
180 return number_of_recipients;
183 return bb::err(iox2::bb::into<SendError>(result));
186template <ServiceType S,
typename Payload,
typename UserHeader>
187template <
typename T,
typename>
190 size_t number_of_recipients = 0;
191 auto result = iox2_publisher_send_slice_copy(&m_handle,
193 sizeof(
typename Payload::ValueType),
194 payload.number_of_elements(),
195 &number_of_recipients);
197 if (result == IOX2_OK) {
198 return number_of_recipients;
201 return bb::err(iox2::bb::into<SendError>(result));
204template <ServiceType S,
typename Payload,
typename UserHeader>
205template <
typename T,
typename>
210 auto result = iox2_publisher_loan_slice_uninit(&m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, 1);
213 if (result == IOX2_OK) {
214 return std::move(sample);
217 return bb::err(iox2::bb::into<LoanError>(result));
220template <ServiceType S,
typename Payload,
typename UserHeader>
221template <
typename T,
typename>
225 if (!sample.has_value()) {
226 return bb::err(sample.error());
229 new (&sample->payload_mut()) Payload();
234template <ServiceType S,
typename Payload,
typename UserHeader>
235template <
typename T,
typename>
240 if (!sample_uninit.has_value()) {
241 return bb::err(sample_uninit.error());
243 auto sample_init = std::move(sample_uninit.value());
245 for (
auto& item : sample_init.payload_mut()) {
246 new (&item) ValueType();
252template <ServiceType S,
typename Payload,
typename UserHeader>
253template <
typename T,
typename>
258 auto result = iox2_publisher_loan_slice_uninit(
259 &m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, number_of_elements);
262 if (result == IOX2_OK) {
263 return std::move(sample);
266 return bb::err(iox2::bb::into<LoanError>(result));
269template <ServiceType S,
typename Payload,
typename UserHeader>
271 auto result = iox2_publisher_update_connections(&m_handle);
272 if (result != IOX2_OK) {
273 return bb::err(iox2::bb::into<ConnectionFailure>(result));
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 >
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 >
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 >
static void placement_default(S &payload)