iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
sample_mut.hpp
Go to the documentation of this file.
1// Copyright (c) 2024 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#ifndef IOX2_SAMPLE_MUT_HPP
14#define IOX2_SAMPLE_MUT_HPP
15
16#include "iox2/bb/expected.hpp"
17#include "iox2/bb/slice.hpp"
18#include "iox2/deployment/iceoryx2_cxx_feature_flags.hpp"
20#include "iox2/iceoryx2.h"
22#include "iox2/marker.hpp"
23#include "iox2/payload_info.hpp"
24#include "iox2/service_type.hpp"
25
26#include <type_traits>
27
28#if IOX2_FEATURE_FLATBUFFERS
29#include <flatbuffers/flatbuffers.h>
30#endif
31
32namespace iox2 {
33
52template <ServiceType S, typename Payload, typename UserHeader>
53// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) 'm_sample' is not used directly but only via the initialized 'm_handle'; furthermore, it will be initialized on the call site
54class SampleMut {
55 using ValueType = typename PayloadInfo<Payload>::ValueType;
56
57 public:
58 SampleMut(SampleMut&& rhs) noexcept;
59 auto operator=(SampleMut&& rhs) noexcept -> SampleMut&;
60 ~SampleMut() noexcept;
61
62 SampleMut(const SampleMut&) = delete;
63 auto operator=(const SampleMut&) -> SampleMut& = delete;
64
66 auto header() const -> HeaderPublishSubscribe;
67
69 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<void, UserHeader>::value, T>>
70 auto user_header() const -> const T&;
71
73 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<void, UserHeader>::value, T>>
74 auto user_header_mut() -> T&;
75
77 template <typename T = Payload,
78 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
79 auto payload() const -> const ValueType&;
80
82 template <typename T = Payload,
83 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
84 auto payload_mut() -> ValueType&;
85
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>;
90
92 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), void>>
93 auto payload_root() const -> const typename T::ValueType*;
94#endif // IOX2_FEATURE_FLATBUFFERS
95
97 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
98 auto payload() const -> bb::ImmutableSlice<ValueType>;
99
101 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
102 auto payload_mut() -> bb::MutableSlice<ValueType>;
103
106 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
107 void truncate(uint64_t number_of_elements);
108
109 private:
110 template <ServiceType, typename, typename>
111 friend class Publisher;
112 template <ServiceType, typename, typename>
113 friend class SampleMutUninit;
114
115 template <ServiceType ST, typename PayloadT, typename UserHeaderT>
116 friend auto send(SampleMut<ST, PayloadT, UserHeaderT>&& sample) -> bb::Expected<size_t, SendError>;
117
118 // The sample is defaulted since both members are initialized in Publisher::loan() or
119 // Publisher::loan_slice()
120 explicit SampleMut() = default;
121 void drop();
122
123 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) will not be accessed directly but only via m_handle and will be set together with m_handle
124 iox2_sample_mut_t m_sample;
125 iox2_sample_mut_h m_handle = nullptr;
126};
127
128template <ServiceType S, typename Payload, typename UserHeader>
129inline void SampleMut<S, Payload, UserHeader>::drop() {
130 if (m_handle != nullptr) {
131 iox2_sample_mut_drop(m_handle);
132 m_handle = nullptr;
133 }
134}
135
136// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) m_sample will be initialized in the move assignment operator
137template <ServiceType S, typename Payload, typename UserHeader>
139 *this = std::move(rhs);
140}
141
142namespace internal {
143extern "C" {
144void iox2_sample_mut_move(iox2_sample_mut_t*, iox2_sample_mut_t*, iox2_sample_mut_h*);
145}
146} // namespace internal
147
148template <ServiceType S, typename Payload, typename UserHeader>
150 if (this != &rhs) {
151 drop();
152
153 internal::iox2_sample_mut_move(&rhs.m_sample, &m_sample, &m_handle);
154 rhs.m_handle = nullptr;
155 }
156
157 return *this;
158}
159
160template <ServiceType S, typename Payload, typename UserHeader>
162 drop();
163}
164
165template <ServiceType S, typename Payload, typename UserHeader>
167 iox2_publish_subscribe_header_h header_handle = nullptr;
168 iox2_sample_mut_header(&m_handle, nullptr, &header_handle);
169
170 return HeaderPublishSubscribe { header_handle };
171}
172
173template <ServiceType S, typename Payload, typename UserHeader>
174template <typename T, typename>
175inline auto SampleMut<S, Payload, UserHeader>::user_header() const -> const T& {
176 const void* ptr = nullptr;
177
178 iox2_sample_mut_user_header(&m_handle, &ptr);
179
180 return *static_cast<const T*>(ptr);
181}
182
183template <ServiceType S, typename Payload, typename UserHeader>
184template <typename T, typename>
186 void* ptr = nullptr;
187
188 iox2_sample_mut_user_header_mut(&m_handle, &ptr);
189
190 return *static_cast<T*>(ptr);
191}
192
193template <ServiceType S, typename Payload, typename UserHeader>
194template <typename T, typename>
195inline auto SampleMut<S, Payload, UserHeader>::payload() const -> const ValueType& {
196 const void* ptr = nullptr;
197
198 iox2_sample_mut_payload(&m_handle, &ptr, nullptr);
199
200 return *static_cast<const ValueType*>(ptr);
201}
202
203template <ServiceType S, typename Payload, typename UserHeader>
204template <typename T, typename>
206 void* ptr = nullptr;
207
208 iox2_sample_mut_payload_mut(&m_handle, &ptr, nullptr);
209
210 return *static_cast<ValueType*>(ptr);
211}
212
213template <ServiceType S, typename Payload, typename UserHeader>
214template <typename T, typename>
215inline auto SampleMut<S, Payload, UserHeader>::payload() const -> bb::ImmutableSlice<ValueType> {
216 const void* ptr = nullptr;
217 size_t number_of_elements = 0;
218
219 iox2_sample_mut_payload(&m_handle, &ptr, &number_of_elements);
220
221 // for the custom payload marker, the slice length is the
222 // runtime payload byte size
223 auto length = number_of_elements;
224 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
225 length = iox2_sample_mut_payload_number_of_bytes(&m_handle);
226 }
227
228 return bb::ImmutableSlice<ValueType>(static_cast<const ValueType*>(ptr), length);
229}
230
231template <ServiceType S, typename Payload, typename UserHeader>
232template <typename T, typename>
234 void* ptr = nullptr;
235 size_t number_of_elements = 0;
236
237 iox2_sample_mut_payload_mut(&m_handle, &ptr, &number_of_elements);
238
239 auto length = number_of_elements;
240 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
241 length = iox2_sample_mut_payload_number_of_bytes(&m_handle);
242 }
243
244 return bb::MutableSlice<ValueType>(static_cast<ValueType*>(ptr), length);
245}
246
247template <ServiceType S, typename Payload, typename UserHeader>
248template <typename T, typename>
249inline void SampleMut<S, Payload, UserHeader>::truncate(uint64_t number_of_elements) {
250 iox2_sample_mut_truncate(&m_handle, number_of_elements);
251}
252
253#if IOX2_FEATURE_FLATBUFFERS
254
255template <ServiceType S, typename Payload, typename UserHeader>
256template <typename T, typename>
257inline auto SampleMut<S, Payload, UserHeader>::payload_bytes() const -> bb::ImmutableSlice<uint8_t> {
258 const void* ptr = nullptr;
259 size_t number_of_elements = 0;
260
261 iox2_sample_mut_payload(&m_handle, &ptr, &number_of_elements);
262 auto payload_offset = header().payload_offset();
263 auto payload_len = header().number_of_elements();
264
265 return bb::ImmutableSlice<uint8_t>(static_cast<const uint8_t*>(ptr) + payload_offset, payload_len - payload_offset);
266}
267
268template <ServiceType S, typename Payload, typename UserHeader>
269template <typename T, typename>
270auto SampleMut<S, Payload, UserHeader>::payload_root() const -> const typename T::ValueType* {
271 return flatbuffers::GetRoot<typename T::ValueType>(payload_bytes().data());
272}
273
274#endif // IOX2_FEATURE_FLATBUFFERS
275
276template <ServiceType S, typename Payload, typename UserHeader>
278 size_t number_of_recipients = 0;
279 auto result = iox2_sample_mut_send(sample.m_handle, &number_of_recipients);
280 sample.m_handle = nullptr;
281
282 if (result == IOX2_OK) {
283 return number_of_recipients;
284 }
285
286 return bb::err(bb::into<SendError>(result));
287}
288
289} // namespace iox2
290
291#endif
Sample header used by [MessagingPattern::PublishSubscribe].
friend class SampleMutUninit
friend class Publisher
auto user_header_mut() -> T &
Returns a mutable reference to the user_header of the [Sample].
~SampleMut() noexcept
auto header() const -> HeaderPublishSubscribe
Returns a reference to the [Header] of the [Sample].
SampleMut(const SampleMut &)=delete
SampleMut(SampleMut &&rhs) noexcept
auto payload_mut() -> ValueType &
Returns a reference to the payload of the sample.
auto payload_root() const -> const typename T::ValueType *
Returns the root of the flatbuffer.
friend auto send(SampleMut< ST, PayloadT, UserHeaderT > &&sample) -> bb::Expected< size_t, SendError >
auto payload_bytes() const -> bb::ImmutableSlice< uint8_t >
Returns the serialized flatbuffer data as bytes.
auto payload() const -> const ValueType &
Returns a reference to the const payload of the sample.
void truncate(uint64_t number_of_elements)
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 into(const SourceType value) noexcept -> typename detail::ExtractIntoType< DestinationType >::TargetType
Converts a value of type SourceType to a corresponding value of type DestinationType....
Definition into.hpp:120
Slice< const T > ImmutableSlice
Definition slice.hpp:95
Slice< T > MutableSlice
Definition slice.hpp:92
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
void iox2_sample_mut_move(iox2_sample_mut_t *, iox2_sample_mut_t *, iox2_sample_mut_h *)
constexpr auto has_flatbuffer_marker() -> bool
Definition marker.hpp:53
SendError
Failure that can be emitted when data is sent.