13#ifndef IOX2_SAMPLE_HPP
14#define IOX2_SAMPLE_HPP
18#include "iox2/iceoryx2_cxx_deployment.hpp"
27#if IOX2_FEATURE_FLATBUFFERS
29#include <flatbuffers/flatbuffers.h>
45template <ServiceType,
typename Payload,
typename UserHeader>
59 template <
typename T = Payload,
60 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(),
void>>
61 auto payload() const -> const ValueType&;
64 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE,
void>>
65 auto
payload() const -> bb::ImmutableSlice<ValueType>;
67#if IOX2_FEATURE_FLATBUFFERS
69 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(),
void>>
70 auto payload_bytes() const -> bb::ImmutableSlice<uint8_t>;
74 auto payload_root() const -> const typename T::ValueType*;
78 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<void, UserHeader>::value, T>>
92 explicit
Sample() = default;
95 iox2_sample_t m_sample;
96 iox2_sample_h m_handle =
nullptr;
99template <
ServiceType S, typename Payload, typename UserHeader>
100inline
void Sample<S, Payload, UserHeader>::drop() {
101 if (m_handle !=
nullptr) {
102 iox2_sample_drop(m_handle);
108template <ServiceType S,
typename Payload,
typename UserHeader>
110 *
this = std::move(rhs);
115void iox2_sample_move(iox2_sample_t*, iox2_sample_t*, iox2_sample_h*);
119template <ServiceType S,
typename Payload,
typename UserHeader>
124 internal::iox2_sample_move(&rhs.m_sample, &m_sample, &m_handle);
125 rhs.m_handle =
nullptr;
131template <ServiceType S,
typename Payload,
typename UserHeader>
136template <ServiceType S,
typename Payload,
typename UserHeader>
137template <
typename T,
typename>
139 const void* ptr =
nullptr;
141 iox2_sample_payload(&m_handle, &ptr,
nullptr);
143 return *
static_cast<const ValueType*
>(ptr);
146template <ServiceType S,
typename Payload,
typename UserHeader>
147template <
typename T,
typename>
149 const void* ptr =
nullptr;
150 size_t number_of_elements = 0;
152 iox2_sample_payload(&m_handle, &ptr, &number_of_elements);
156 auto length = number_of_elements;
157 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
158 length = iox2_sample_payload_number_of_bytes(&m_handle);
161 return bb::ImmutableSlice<ValueType>(
static_cast<const ValueType*
>(ptr), length);
164#if IOX2_FEATURE_FLATBUFFERS
166template <ServiceType S,
typename Payload,
typename UserHeader>
167template <
typename T,
typename>
168inline auto Sample<S, Payload, UserHeader>::payload_bytes() const -> bb::ImmutableSlice<uint8_t> {
169 const void* ptr =
nullptr;
170 size_t number_of_elements = 0;
172 iox2_sample_payload(&m_handle, &ptr, &number_of_elements);
173 auto payload_offset =
header().payload_offset();
174 auto payload_len =
header().number_of_elements();
176 return bb::ImmutableSlice<uint8_t>(
static_cast<const uint8_t*
>(ptr) + payload_offset, payload_len - payload_offset);
179template <ServiceType S,
typename Payload,
typename UserHeader>
180template <
typename T,
typename>
181auto Sample<S, Payload, UserHeader>::payload_root() const -> const typename T::ValueType* {
182 return flatbuffers::GetRoot<typename T::ValueType>(payload_bytes().data());
187template <ServiceType S,
typename Payload,
typename UserHeader>
188template <
typename T,
typename>
190 const void* header_ptr =
nullptr;
192 iox2_sample_user_header(&m_handle, &header_ptr);
194 return *
static_cast<const T*
>(header_ptr);
197template <ServiceType S,
typename Payload,
typename UserHeader>
199 iox2_publish_subscribe_header_h header_handle =
nullptr;
200 iox2_sample_header(&m_handle,
nullptr, &header_handle);
205template <ServiceType S,
typename Payload,
typename UserHeader>
207 return header().publisher_id();
auto header() const -> HeaderPublishSubscribe
Returns a reference to the [Header] of the [Sample].
auto payload() const -> const ValueType &
Returns a reference to the payload of the [Sample].
auto origin() const -> UniquePublisherId
Returns the [UniquePublisherId] of the [Publisher](crate::port::publisher::Publisher)
auto operator=(Sample &&rhs) noexcept -> Sample &
auto user_header() const -> const T &
Returns a reference to the user_header of the [Sample].
Sample(const Sample &)=delete
auto operator=(const Sample &) -> Sample &=delete
The receiving endpoint of a publish-subscribe communication.
The system-wide unique id of a [Publisher].
constexpr auto has_flatbuffer_marker() -> bool