iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
request_mut.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 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_REQUEST_MUT_HPP
14#define IOX2_REQUEST_MUT_HPP
15
16#include "iox2/bb/expected.hpp"
17#include "iox2/bb/slice.hpp"
18#include "iox2/deployment/iceoryx2_cxx_feature_flags.hpp"
21#include "iox2/marker.hpp"
22#include "iox2/payload_info.hpp"
24#include "iox2/port_error.hpp"
25#include "iox2/service_type.hpp"
26
27#if IOX2_FEATURE_FLATBUFFERS
28#include <flatbuffers/flatbuffers.h>
29#endif // IOX2_FEATURE_FLATBUFFERS
30
31#include <type_traits>
32
33namespace iox2 {
34
37template <ServiceType Service,
38 typename RequestPayload,
39 typename RequestUserHeader,
40 typename ResponsePayload,
41 typename ResponseUserHeader>
42// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) 'm_request' is not used directly but only via the initialized 'm_handle'; furthermore, it will be initialized on the call site
44 using ValueType = typename PayloadInfo<RequestPayload>::ValueType;
45
46 public:
47 RequestMut(RequestMut&& rhs) noexcept;
48 auto operator=(RequestMut&& rhs) noexcept -> RequestMut&;
49 ~RequestMut() noexcept;
50
51 RequestMut(const RequestMut&) = delete;
52 auto operator=(const RequestMut&) -> RequestMut& = delete;
53
55 auto header() const -> RequestHeader;
56
58 template <typename T = RequestUserHeader,
59 typename = std::enable_if_t<!std::is_same<void, RequestUserHeader>::value, T>>
60 auto user_header() const -> const T&;
61
63 template <typename T = RequestUserHeader,
64 typename = std::enable_if_t<!std::is_same<void, RequestUserHeader>::value, T>>
65 auto user_header_mut() -> T&;
66
68 template <typename T = RequestPayload,
69 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
70 auto payload() const -> const RequestPayload&;
71
73 template <typename T = RequestPayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
74 auto payload() const -> bb::ImmutableSlice<ValueType>;
75
77 template <typename T = RequestPayload,
78 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
79 auto payload_mut() -> RequestPayload&;
80
82 template <typename T = RequestPayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
83 auto payload_mut() -> bb::MutableSlice<ValueType>;
84
87 template <typename T = RequestPayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
88 void truncate(uint64_t number_of_elements);
89
90#if IOX2_FEATURE_FLATBUFFERS
92 template <typename T = RequestPayload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), void>>
93 auto payload_bytes() const -> bb::ImmutableSlice<uint8_t>;
94
96 template <typename T = RequestPayload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), void>>
97 auto payload_root() const -> const typename T::ValueType*;
98#endif // IOX2_FEATURE_FLATBUFFERS
99
100 private:
101 template <ServiceType, typename, typename, typename, typename>
102 friend class Client;
103 template <ServiceType, typename, typename, typename, typename>
104 friend class RequestMutUninit;
105
109 template <ServiceType S,
110 typename RequestPayloadT,
111 typename RequestUserHeaderT,
112 typename ResponsePayloadT,
113 typename ResponseUserHeaderT>
114 friend auto
118
119 explicit RequestMut() = default;
120 void drop();
121
122 iox2_request_mut_t m_request;
123 iox2_request_mut_h m_handle = nullptr;
124};
125
126// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) m_request will be initialized in the move assignment operator
127template <ServiceType Service,
128 typename RequestPayload,
129 typename RequestUserHeader,
130 typename ResponsePayload,
131 typename ResponseUserHeader>
136
137namespace internal {
138extern "C" {
139void iox2_request_mut_move(iox2_request_mut_t*, iox2_request_mut_t*, iox2_request_mut_h*);
140}
141} // namespace internal
142
143template <ServiceType Service,
144 typename RequestPayload,
145 typename RequestUserHeader,
146 typename ResponsePayload,
147 typename ResponseUserHeader>
149 RequestMut&& rhs) noexcept -> RequestMut& {
150 if (this != &rhs) {
151 drop();
152
153 internal::iox2_request_mut_move(&rhs.m_request, &m_request, &m_handle);
154 rhs.m_handle = nullptr;
155 }
156 return *this;
157}
158
159template <ServiceType Service,
160 typename RequestPayload,
161 typename RequestUserHeader,
162 typename ResponsePayload,
163 typename ResponseUserHeader>
168
169#if IOX2_FEATURE_FLATBUFFERS
170template <ServiceType Service,
171 typename RequestPayload,
172 typename RequestUserHeader,
173 typename ResponsePayload,
174 typename ResponseUserHeader>
175template <typename T, typename>
176inline auto
178 -> bb::ImmutableSlice<uint8_t> {
179 const void* ptr = nullptr;
180 size_t number_of_elements = 0;
181
182 iox2_request_mut_payload(&m_handle, &ptr, &number_of_elements);
183 auto payload_offset = header().payload_offset();
184 auto payload_len = header().number_of_elements();
185
186 return bb::ImmutableSlice<uint8_t>(static_cast<const uint8_t*>(ptr) + payload_offset, payload_len - payload_offset);
187}
188
189template <ServiceType Service,
190 typename RequestPayload,
191 typename RequestUserHeader,
192 typename ResponsePayload,
193 typename ResponseUserHeader>
194template <typename T, typename>
195inline auto
197 -> const typename T::ValueType* {
198 return flatbuffers::GetRoot<typename T::ValueType>(payload_bytes().data());
199}
200#endif // IOX2_FEATURE_FLATBUFFERS
201
202template <ServiceType Service,
203 typename RequestPayload,
204 typename RequestUserHeader,
205 typename ResponsePayload,
206 typename ResponseUserHeader>
208 -> RequestHeader {
209 iox2_request_header_h header_handle = nullptr;
210 iox2_request_mut_header(&m_handle, nullptr, &header_handle);
211 return RequestHeader { header_handle };
212}
213
214template <ServiceType Service,
215 typename RequestPayload,
216 typename RequestUserHeader,
217 typename ResponsePayload,
218 typename ResponseUserHeader>
219template <typename T, typename>
220inline auto
222 -> const T& {
223 const void* ptr = nullptr;
224 iox2_request_mut_user_header(&m_handle, &ptr);
225 return *static_cast<const T*>(ptr);
226}
227
228template <ServiceType Service,
229 typename RequestPayload,
230 typename RequestUserHeader,
231 typename ResponsePayload,
232 typename ResponseUserHeader>
233template <typename T, typename>
234inline auto
236 void* ptr = nullptr;
237 iox2_request_mut_user_header_mut(&m_handle, &ptr);
238 return *static_cast<T*>(ptr);
239}
240
241template <ServiceType Service,
242 typename RequestPayload,
243 typename RequestUserHeader,
244 typename ResponsePayload,
245 typename ResponseUserHeader>
246template <typename T, typename>
248 -> const RequestPayload& {
249 const void* ptr = nullptr;
250 iox2_request_mut_payload(&m_handle, &ptr, nullptr);
251 return *static_cast<const RequestPayload*>(ptr);
252}
253
254template <ServiceType Service,
255 typename RequestPayload,
256 typename RequestUserHeader,
257 typename ResponsePayload,
258 typename ResponseUserHeader>
259template <typename T, typename>
261 -> bb::ImmutableSlice<ValueType> {
262 const void* ptr = nullptr;
263 size_t number_of_elements = 0;
264 iox2_request_mut_payload(&m_handle, &ptr, &number_of_elements);
265
266 // for the custom payload marker, the slice length is the
267 // runtime payload byte size
268 auto length = number_of_elements;
269 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
270 length = iox2_request_mut_payload_number_of_bytes(&m_handle);
271 }
272
273 return bb::ImmutableSlice<ValueType>(static_cast<const ValueType*>(ptr), length);
274}
275
276template <ServiceType Service,
277 typename RequestPayload,
278 typename RequestUserHeader,
279 typename ResponsePayload,
280 typename ResponseUserHeader>
281template <typename T, typename>
283 -> RequestPayload& {
284 void* ptr = nullptr;
285 iox2_request_mut_payload_mut(&m_handle, &ptr, nullptr);
286 return *static_cast<RequestPayload*>(ptr);
287}
288
289template <ServiceType Service,
290 typename RequestPayload,
291 typename RequestUserHeader,
292 typename ResponsePayload,
293 typename ResponseUserHeader>
294template <typename T, typename>
297 void* ptr = nullptr;
298 size_t number_of_elements = 0;
299 iox2_request_mut_payload_mut(&m_handle, &ptr, &number_of_elements);
300
301 // for the custom payload marker, the slice length is the
302 // runtime payload byte size
303 auto length = number_of_elements;
304 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
305 length = iox2_request_mut_payload_number_of_bytes(&m_handle);
306 }
307
308 return bb::MutableSlice<ValueType>(static_cast<ValueType*>(ptr), length);
309}
310
311template <ServiceType Service,
312 typename RequestPayload,
313 typename RequestUserHeader,
314 typename ResponsePayload,
315 typename ResponseUserHeader>
316template <typename T, typename>
318 uint64_t number_of_elements) {
319 iox2_request_mut_truncate(&m_handle, number_of_elements);
320}
321
322template <ServiceType Service,
323 typename RequestPayload,
324 typename RequestUserHeader,
325 typename ResponsePayload,
326 typename ResponseUserHeader>
330 iox2_pending_response_h pending_response_handle {};
331 auto result = iox2_request_mut_send(request.m_handle, nullptr, &pending_response_handle);
332 request.m_handle = nullptr;
333
334 if (result == IOX2_OK) {
336 pending_response_handle);
337 }
338 return bb::err(bb::into<RequestSendError>(result));
339}
340
341template <ServiceType Service,
342 typename RequestPayload,
343 typename RequestUserHeader,
344 typename ResponsePayload,
345 typename ResponseUserHeader>
346inline void RequestMut<Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader>::drop() {
347 if (m_handle != nullptr) {
348 iox2_request_mut_drop(m_handle);
349 m_handle = nullptr;
350 }
351}
352
353} // namespace iox2
354
355#endif
Request header used by [MessagingPattern::RequestResponse].
auto payload() const -> const RequestPayload &
Returns a reference to the user defined request payload.
auto payload_mut() -> RequestPayload &
Returns a mutable reference to the user defined request payload.
auto payload_bytes() const -> bb::ImmutableSlice< uint8_t >
Returns the serialized flatbuffer data as bytes.
friend class RequestMutUninit
RequestMut(RequestMut &&rhs) noexcept
auto user_header() const -> const T &
Returns a reference to the user defined request header.
auto operator=(RequestMut &&rhs) noexcept -> RequestMut &
auto user_header_mut() -> T &
Returns a mutable reference to the user defined request header.
auto payload_root() const -> const typename T::ValueType *
Returns the root of the flatbuffer.
friend auto send(RequestMut< S, RequestPayloadT, RequestUserHeaderT, ResponsePayloadT, ResponseUserHeaderT > &&request) -> bb::Expected< PendingResponse< S, RequestPayloadT, RequestUserHeaderT, ResponsePayloadT, ResponseUserHeaderT >, RequestSendError >
RequestMut(const RequestMut &)=delete
friend class Client
void truncate(uint64_t number_of_elements)
~RequestMut() noexcept
auto operator=(const RequestMut &) -> RequestMut &=delete
auto header() const -> RequestHeader
Returns a reference to the iceoryx2 internal [RequestHeader].
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_request_mut_move(iox2_request_mut_t *, iox2_request_mut_t *, iox2_request_mut_h *)
constexpr auto has_flatbuffer_marker() -> bool
Definition marker.hpp:53
auto send(RequestMut< Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader > &&request) -> bb::Expected< PendingResponse< Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader >, RequestSendError >
RequestSendError
Failure that can be emitted when a [RequestMut] is sent.