iceoryx2
C++ Language 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"
20#include "iox2/iceoryx2.h"
22#include "iox2/payload_info.hpp"
24#include "iox2/service_type.hpp"
25
26#include <type_traits>
27
28namespace iox2 {
29
48template <ServiceType S, typename Payload, typename UserHeader>
49// 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
50class SampleMut {
51 using ValueType = typename PayloadInfo<Payload>::ValueType;
52
53 public:
54 SampleMut(SampleMut&& rhs) noexcept;
55 auto operator=(SampleMut&& rhs) noexcept -> SampleMut&;
56 ~SampleMut() noexcept;
57
58 SampleMut(const SampleMut&) = delete;
59 auto operator=(const SampleMut&) -> SampleMut& = delete;
60
62 auto header() const -> HeaderPublishSubscribe;
63
65 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<void, UserHeader>::value, T>>
66 auto user_header() const -> const T&;
67
69 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<void, UserHeader>::value, T>>
70 auto user_header_mut() -> T&;
71
73 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
74 auto payload() const -> const ValueType&;
75
77 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
78 auto payload_mut() -> ValueType&;
79
80 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
81 auto payload() const -> bb::ImmutableSlice<ValueType>;
82
83 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
84 auto payload_mut() -> bb::MutableSlice<ValueType>;
85
86 private:
87 template <ServiceType, typename, typename>
88 friend class Publisher;
89 template <ServiceType, typename, typename>
90 friend class SampleMutUninit;
91
92 template <ServiceType ST, typename PayloadT, typename UserHeaderT>
93 friend auto send(SampleMut<ST, PayloadT, UserHeaderT>&& sample) -> bb::Expected<size_t, SendError>;
94
95 // The sample is defaulted since both members are initialized in Publisher::loan() or
96 // Publisher::loan_slice()
97 explicit SampleMut() = default;
98 void drop();
99
100 // 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
101 iox2_sample_mut_t m_sample;
102 iox2_sample_mut_h m_handle = nullptr;
103};
104
105template <ServiceType S, typename Payload, typename UserHeader>
106inline void SampleMut<S, Payload, UserHeader>::drop() {
107 if (m_handle != nullptr) {
108 iox2_sample_mut_drop(m_handle);
109 m_handle = nullptr;
110 }
111}
112
113// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) m_sample will be initialized in the move assignment operator
114template <ServiceType S, typename Payload, typename UserHeader>
116 *this = std::move(rhs);
117}
118
119namespace internal {
120extern "C" {
121void iox2_sample_mut_move(iox2_sample_mut_t*, iox2_sample_mut_t*, iox2_sample_mut_h*);
122}
123} // namespace internal
124
125template <ServiceType S, typename Payload, typename UserHeader>
127 if (this != &rhs) {
128 drop();
129
130 internal::iox2_sample_mut_move(&rhs.m_sample, &m_sample, &m_handle);
131 rhs.m_handle = nullptr;
132 }
133
134 return *this;
135}
136
137template <ServiceType S, typename Payload, typename UserHeader>
139 drop();
140}
141
142template <ServiceType S, typename Payload, typename UserHeader>
144 iox2_publish_subscribe_header_h header_handle = nullptr;
145 iox2_sample_mut_header(&m_handle, nullptr, &header_handle);
146
147 return HeaderPublishSubscribe { header_handle };
148}
149
150template <ServiceType S, typename Payload, typename UserHeader>
151template <typename T, typename>
152inline auto SampleMut<S, Payload, UserHeader>::user_header() const -> const T& {
153 const void* ptr = nullptr;
154
155 iox2_sample_mut_user_header(&m_handle, &ptr);
156
157 return *static_cast<const T*>(ptr);
158}
159
160template <ServiceType S, typename Payload, typename UserHeader>
161template <typename T, typename>
163 void* ptr = nullptr;
164
165 iox2_sample_mut_user_header_mut(&m_handle, &ptr);
166
167 return *static_cast<T*>(ptr);
168}
169
170template <ServiceType S, typename Payload, typename UserHeader>
171template <typename T, typename>
172inline auto SampleMut<S, Payload, UserHeader>::payload() const -> const ValueType& {
173 const void* ptr = nullptr;
174
175 iox2_sample_mut_payload(&m_handle, &ptr, nullptr);
176
177 return *static_cast<const ValueType*>(ptr);
178}
179
180template <ServiceType S, typename Payload, typename UserHeader>
181template <typename T, typename>
183 void* ptr = nullptr;
184
185 iox2_sample_mut_payload_mut(&m_handle, &ptr, nullptr);
186
187 return *static_cast<ValueType*>(ptr);
188}
189
190template <ServiceType S, typename Payload, typename UserHeader>
191template <typename T, typename>
192inline auto SampleMut<S, Payload, UserHeader>::payload() const -> bb::ImmutableSlice<ValueType> {
193 const void* ptr = nullptr;
194 size_t number_of_elements = 0;
195
196 iox2_sample_mut_payload(&m_handle, &ptr, &number_of_elements);
197
198 // for the custom payload marker, the slice length is the
199 // runtime payload byte size
200 auto length = number_of_elements;
201 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
202 length = iox2_sample_mut_payload_number_of_bytes(&m_handle);
203 }
204
205 return bb::ImmutableSlice<ValueType>(static_cast<const ValueType*>(ptr), length);
206}
207
208template <ServiceType S, typename Payload, typename UserHeader>
209template <typename T, typename>
210inline auto SampleMut<S, Payload, UserHeader>::payload_mut() -> bb::MutableSlice<ValueType> {
211 void* ptr = nullptr;
212 size_t number_of_elements = 0;
213
214 iox2_sample_mut_payload_mut(&m_handle, &ptr, &number_of_elements);
215
216 auto length = number_of_elements;
217 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
218 length = iox2_sample_mut_payload_number_of_bytes(&m_handle);
219 }
220
221 return bb::MutableSlice<ValueType>(static_cast<ValueType*>(ptr), length);
222}
223
224template <ServiceType S, typename Payload, typename UserHeader>
226 size_t number_of_recipients = 0;
227 auto result = iox2_sample_mut_send(sample.m_handle, &number_of_recipients);
228 sample.m_handle = nullptr;
229
230 if (result == IOX2_OK) {
231 return number_of_recipients;
232 }
233
234 return bb::err(bb::into<SendError>(result));
235}
236
237} // namespace iox2
238
239#endif
Sample header used by [MessagingPattern::PublishSubscribe].
Sending endpoint of a publish-subscriber based communication.
Definition publisher.hpp:34
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
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 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 >
Definition expected.hpp:33
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
SendError
Failure that can be emitted when data is sent.