iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
response_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_RESPONSE_MUT_HPP
14#define IOX2_RESPONSE_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/marker.hpp"
21#include "iox2/payload_info.hpp"
22#include "iox2/port_error.hpp"
23#include "iox2/service_type.hpp"
24
25#if IOX2_FEATURE_FLATBUFFERS
26#include <flatbuffers/flatbuffers.h>
27#endif // IOX2_FEATURE_FLATBUFFERS
28
29#include <type_traits>
30
31namespace iox2 {
32
41template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
42// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) 'm_response' 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<ResponsePayload>::ValueType;
45
46 public:
47 ResponseMut(ResponseMut&& rhs) noexcept;
48 auto operator=(ResponseMut&& rhs) noexcept -> ResponseMut&;
49 ~ResponseMut() noexcept;
50
51 ResponseMut(const ResponseMut&) = delete;
52 auto operator=(const ResponseMut&) -> ResponseMut& = delete;
53
55 auto header() const -> ResponseHeader;
56
58 template <typename T = ResponseUserHeader,
59 typename = std::enable_if_t<!std::is_same<void, ResponseUserHeader>::value, T>>
60 auto user_header() const -> const T&;
61
63 template <typename T = ResponseUserHeader,
64 typename = std::enable_if_t<!std::is_same<void, ResponseUserHeader>::value, T>>
65 auto user_header_mut() -> T&;
66
68 template <typename T = ResponsePayload,
69 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
70 auto payload() const -> const ResponsePayload&;
71
73 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
74 auto payload() const -> bb::ImmutableSlice<ValueType>;
75
77 template <typename T = ResponsePayload,
78 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
79 auto payload_mut() -> ResponsePayload&;
80
82 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
83 auto payload_mut() -> bb::MutableSlice<ValueType>;
84
87 template <typename T = ResponsePayload, 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 = ResponsePayload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), void>>
93 auto payload_bytes() const -> bb::ImmutableSlice<uint8_t>;
94
96 template <typename T = ResponsePayload, 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>
102 friend class ResponseMutUninit;
103 template <ServiceType, typename, typename, typename, typename>
104 friend class ActiveRequest;
105
108 template <ServiceType S, typename ResponsePayloadT, typename ResponseUserHeaderT>
110
111 explicit ResponseMut() = default;
112 void drop();
113
114 iox2_response_mut_t m_response;
115 iox2_response_mut_h m_handle = nullptr;
116};
117
118// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) m_response will be initialized in the move assignment operator
119template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
121 *this = std::move(rhs);
122}
123
124namespace internal {
125extern "C" {
126void iox2_response_mut_move(iox2_response_mut_t*, iox2_response_mut_t*, iox2_response_mut_h*);
127}
128} // namespace internal
129
130template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
132 -> ResponseMut& {
133 if (this != &rhs) {
134 drop();
135
136 internal::iox2_response_mut_move(&rhs.m_response, &m_response, &m_handle);
137 rhs.m_handle = nullptr;
138 }
139
140 return *this;
141}
142
143template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
147
148#if IOX2_FEATURE_FLATBUFFERS
149template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
150template <typename T, typename>
152 -> bb::ImmutableSlice<uint8_t> {
153 const void* ptr = nullptr;
154 size_t number_of_elements = 0;
155
156 iox2_response_mut_payload(&m_handle, &ptr, &number_of_elements);
157 auto payload_offset = header().payload_offset();
158 auto payload_len = header().number_of_elements();
159
160 return bb::ImmutableSlice<uint8_t>(static_cast<const uint8_t*>(ptr) + payload_offset, payload_len - payload_offset);
161}
162
163template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
164template <typename T, typename>
166 typename T::ValueType* {
167 return flatbuffers::GetRoot<typename T::ValueType>(payload_bytes().data());
168}
169#endif // IOX2_FEATURE_FLATBUFFERS
170
171template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
173 iox2_response_header_h header_handle = nullptr;
174 iox2_response_mut_header(&m_handle, nullptr, &header_handle);
175 return ResponseHeader { header_handle };
176}
177
178template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
179template <typename T, typename>
181 const void* ptr = nullptr;
182 iox2_response_mut_user_header(&m_handle, &ptr);
183 return *static_cast<const T*>(ptr);
184}
185
186template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
187template <typename T, typename>
189 void* ptr = nullptr;
190 iox2_response_mut_user_header_mut(&m_handle, &ptr);
191 return *static_cast<T*>(ptr);
192}
193
194template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
195template <typename T, typename>
196inline auto ResponseMut<Service, ResponsePayload, ResponseUserHeader>::payload() const -> const ResponsePayload& {
197 const void* ptr = nullptr;
198 iox2_response_mut_payload(&m_handle, &ptr, nullptr);
199 return *static_cast<const T*>(ptr);
200}
201
202template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
203template <typename T, typename>
205 -> bb::ImmutableSlice<ValueType> {
206 const void* ptr = nullptr;
207 size_t number_of_elements = 0;
208 iox2_response_mut_payload(&m_handle, &ptr, &number_of_elements);
209
210 // for the custom payload marker, the slice length is the
211 // runtime payload byte size
212 auto length = number_of_elements;
213 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
214 length = iox2_response_mut_payload_number_of_bytes(&m_handle);
215 }
216
217 return bb::ImmutableSlice<ValueType>(static_cast<const ValueType*>(ptr), length);
218}
219
220template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
221template <typename T, typename>
223 void* ptr = nullptr;
224 iox2_response_mut_payload_mut(&m_handle, &ptr, nullptr);
225 return *static_cast<ResponsePayload*>(ptr);
226}
227
228template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
229template <typename T, typename>
231 void* ptr = nullptr;
232 size_t number_of_elements = 0;
233 iox2_response_mut_payload_mut(&m_handle, &ptr, &number_of_elements);
234
235 // for the custom payload marker, the slice length is the
236 // runtime payload byte size
237 auto length = number_of_elements;
238 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
239 length = iox2_response_mut_payload_number_of_bytes(&m_handle);
240 }
241
242 return bb::MutableSlice<ValueType>(static_cast<ValueType*>(ptr), length);
243}
244
245template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
246template <typename T, typename>
248 iox2_response_mut_truncate(&m_handle, number_of_elements);
249}
250
251
252template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
255 auto result = iox2_response_mut_send(response.m_handle);
256 response.m_handle = nullptr;
257
258 if (result == IOX2_OK) {
259 return {};
260 }
261 return bb::err(bb::into<SendError>(result));
262}
263
264template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
265inline void ResponseMut<Service, ResponsePayload, ResponseUserHeader>::drop() {
266 if (m_handle != nullptr) {
267 iox2_response_mut_drop(m_handle);
268 m_handle = nullptr;
269 }
270}
271
272} // namespace iox2
273
274#endif
Response header used by [MessagingPattern::RequestResponse].
void truncate(uint64_t number_of_elements)
auto payload_root() const -> const typename T::ValueType *
Returns the root of the flatbuffer.
friend auto send(ResponseMut< S, ResponsePayloadT, ResponseUserHeaderT > &&response) -> bb::Expected< void, SendError >
auto header() const -> ResponseHeader
Returns a reference to the [ResponseHeader].
auto payload_bytes() const -> bb::ImmutableSlice< uint8_t >
Returns the serialized flatbuffer data as bytes.
friend class ResponseMutUninit
auto user_header() const -> const T &
Returns a reference to the user header of the response.
ResponseMut(ResponseMut &&rhs) noexcept
auto user_header_mut() -> T &
Returns a mutable reference to the user header of the response.
friend class ActiveRequest
auto operator=(ResponseMut &&rhs) noexcept -> ResponseMut &
~ResponseMut() noexcept
auto payload_mut() -> ResponsePayload &
Returns a mutable reference to the payload of the response.
ResponseMut(const ResponseMut &)=delete
auto operator=(const ResponseMut &) -> ResponseMut &=delete
auto payload() const -> const ResponsePayload &
Returns a reference to the payload of the response.
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_response_mut_move(iox2_response_mut_t *, iox2_response_mut_t *, iox2_response_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 >