13#ifndef IOX2_SAMPLE_MUT_HPP
14#define IOX2_SAMPLE_MUT_HPP
19#include "iox2/iceoryx2.h"
20#include "iox2/iceoryx2_cxx_deployment.hpp"
28#if IOX2_FEATURE_FLATBUFFERS
29#include <flatbuffers/flatbuffers.h>
52template <ServiceType S,
typename Payload,
typename UserHeader>
69 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<
void, UserHeader>::value, T>>
73 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<
void, UserHeader>::value, T>>
77 template <typename T = Payload,
79 auto
payload() const -> const ValueType&;
82 template <typename T = Payload,
86#if IOX2_FEATURE_FLATBUFFERS
88 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(),
void>>
89 auto payload_bytes() const -> bb::ImmutableSlice<uint8_t>;
93 auto payload_root() const -> const typename T::ValueType*;
97 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
98 auto payload() const -> bb::ImmutableSlice<ValueType>;
101 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
110 template <
ServiceType ST, typename PayloadT, typename UserHeaderT>
119 iox2_sample_mut_t m_sample;
120 iox2_sample_mut_h m_handle =
nullptr;
123template <
ServiceType S, typename Payload, typename UserHeader>
124inline
void SampleMut<S, Payload, UserHeader>::drop() {
125 if (m_handle !=
nullptr) {
126 iox2_sample_mut_drop(m_handle);
132template <ServiceType S,
typename Payload,
typename UserHeader>
134 *
this = std::move(rhs);
139void iox2_sample_mut_move(iox2_sample_mut_t*, iox2_sample_mut_t*, iox2_sample_mut_h*);
143template <ServiceType S,
typename Payload,
typename UserHeader>
148 internal::iox2_sample_mut_move(&rhs.m_sample, &m_sample, &m_handle);
149 rhs.m_handle =
nullptr;
155template <ServiceType S,
typename Payload,
typename UserHeader>
160template <ServiceType S,
typename Payload,
typename UserHeader>
162 iox2_publish_subscribe_header_h header_handle =
nullptr;
163 iox2_sample_mut_header(&m_handle,
nullptr, &header_handle);
168template <ServiceType S,
typename Payload,
typename UserHeader>
169template <
typename T,
typename>
171 const void* ptr =
nullptr;
173 iox2_sample_mut_user_header(&m_handle, &ptr);
175 return *
static_cast<const T*
>(ptr);
178template <ServiceType S,
typename Payload,
typename UserHeader>
179template <
typename T,
typename>
183 iox2_sample_mut_user_header_mut(&m_handle, &ptr);
185 return *
static_cast<T*
>(ptr);
188template <ServiceType S,
typename Payload,
typename UserHeader>
189template <
typename T,
typename>
191 const void* ptr =
nullptr;
193 iox2_sample_mut_payload(&m_handle, &ptr,
nullptr);
195 return *
static_cast<const ValueType*
>(ptr);
198template <ServiceType S,
typename Payload,
typename UserHeader>
199template <
typename T,
typename>
203 iox2_sample_mut_payload_mut(&m_handle, &ptr,
nullptr);
205 return *
static_cast<ValueType*
>(ptr);
208template <ServiceType S,
typename Payload,
typename UserHeader>
209template <
typename T,
typename>
211 const void* ptr =
nullptr;
212 size_t number_of_elements = 0;
214 iox2_sample_mut_payload(&m_handle, &ptr, &number_of_elements);
218 auto length = number_of_elements;
219 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
220 length = iox2_sample_mut_payload_number_of_bytes(&m_handle);
223 return bb::ImmutableSlice<ValueType>(
static_cast<const ValueType*
>(ptr), length);
226template <ServiceType S,
typename Payload,
typename UserHeader>
227template <
typename T,
typename>
230 size_t number_of_elements = 0;
232 iox2_sample_mut_payload_mut(&m_handle, &ptr, &number_of_elements);
234 auto length = number_of_elements;
235 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
236 length = iox2_sample_mut_payload_number_of_bytes(&m_handle);
239 return bb::MutableSlice<ValueType>(
static_cast<ValueType*
>(ptr), length);
242#if IOX2_FEATURE_FLATBUFFERS
244template <ServiceType S,
typename Payload,
typename UserHeader>
245template <
typename T,
typename>
246inline auto SampleMut<S, Payload, UserHeader>::payload_bytes() const -> bb::ImmutableSlice<uint8_t> {
247 const void* ptr =
nullptr;
248 size_t number_of_elements = 0;
250 iox2_sample_mut_payload(&m_handle, &ptr, &number_of_elements);
251 auto payload_offset =
header().payload_offset();
252 auto payload_len =
header().number_of_elements();
254 return bb::ImmutableSlice<uint8_t>(
static_cast<const uint8_t*
>(ptr) + payload_offset, payload_len - payload_offset);
257template <ServiceType S,
typename Payload,
typename UserHeader>
258template <
typename T,
typename>
259auto SampleMut<S, Payload, UserHeader>::payload_root() const -> const typename T::ValueType* {
260 return flatbuffers::GetRoot<typename T::ValueType>(payload_bytes().data());
265template <ServiceType S,
typename Payload,
typename UserHeader>
267 size_t number_of_recipients = 0;
268 auto result = iox2_sample_mut_send(sample.m_handle, &number_of_recipients);
269 sample.m_handle =
nullptr;
271 if (result == IOX2_OK) {
272 return number_of_recipients;
275 return bb::err(bb::into<SendError>(result));
Sending endpoint of a publish-subscriber based communication.
auto user_header_mut() -> T &
Returns a mutable reference to the user_header of the [Sample].
auto header() const -> HeaderPublishSubscribe
Returns a reference to the [Header] of the [Sample].
SampleMut(const SampleMut &)=delete
auto payload_mut() -> ValueType &
Returns a reference to the payload of the sample.
friend auto send(SampleMut< ST, PayloadT, UserHeaderT > &&sample) -> bb::Expected< size_t, SendError >
auto payload() const -> const ValueType &
Returns a reference to the const payload of the sample.
auto payload() const -> bb::ImmutableSlice< ValueType >
Returns an immutable slice to the payload of the sample.
auto user_header() const -> const T &
Returns a reference to the user_header of the [Sample].
auto operator=(const SampleMut &) -> SampleMut &=delete
auto operator=(SampleMut &&rhs) noexcept -> SampleMut &
constexpr auto err(const E &error) -> Unexpected< E >
iox2::bb::variation::Expected< T, E > Expected
constexpr auto has_flatbuffer_marker() -> bool
SendError
Failure that can be emitted when data is sent.