iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
response_mut_uninit.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_UNINIT_HPP
14#define IOX2_RESPONSE_MUT_UNINIT_HPP
15
16#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/response_mut.hpp"
23#include "iox2/service_type.hpp"
24
25#if IOX2_FEATURE_FLATBUFFERS
27
28#include <flatbuffers/buffer.h>
29#include <flatbuffers/flatbuffer_builder.h>
30#endif // IOX2_FEATURE_FLATBUFFERS
31
32namespace iox2 {
33
42template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
44 using ValueType = typename PayloadInfo<ResponsePayload>::ValueType;
45
46 public:
47 ResponseMutUninit(ResponseMutUninit&& rhs) noexcept = default;
48 auto operator=(ResponseMutUninit&& rhs) noexcept -> ResponseMutUninit& = default;
49 ~ResponseMutUninit() noexcept = default;
50
52 auto operator=(const ResponseMutUninit&) -> ResponseMutUninit& = delete;
53
55 template <typename T = ResponsePayload, typename = std::enable_if_t<!has_flatbuffer_marker<T>(), void>>
56 auto header() const -> ResponseHeader;
57
59 template <typename T = ResponseUserHeader,
60 typename U = ResponsePayload,
61 typename =
62 std::enable_if_t<!std::is_same<void, ResponseUserHeader>::value && !has_flatbuffer_marker<U>(), void>>
63 auto user_header() const -> const T&;
64
66 template <typename T = ResponseUserHeader,
67 typename U = ResponsePayload,
68 typename =
69 std::enable_if_t<!std::is_same<void, ResponseUserHeader>::value && !has_flatbuffer_marker<T>(), void>>
70 auto user_header_mut() -> T&;
71
73 template <typename T = ResponsePayload,
74 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
75 auto payload() const -> const T&;
76
78 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
79 auto payload() const -> bb::ImmutableSlice<ValueType>;
80
82 template <typename T = ResponsePayload,
83 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
84 auto payload_mut() -> T&;
85
87 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
88 auto payload_mut() -> bb::MutableSlice<ValueType>;
89
92 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
93 void truncate(uint64_t number_of_elements);
94
97 template <typename T = ResponsePayload,
98 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), T>>
99 auto write_payload(ResponsePayload&& payload) -> ResponseMut<Service, T, ResponseUserHeader>;
100
103 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
104 auto write_from_slice(bb::ImmutableSlice<ValueType>& value) -> ResponseMut<Service, T, ResponseUserHeader>;
105
108 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
109 auto write_from_fn(const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer)
110 -> ResponseMut<Service, T, ResponseUserHeader>;
111
112#if IOX2_FEATURE_FLATBUFFERS
115 template <typename T = ResponsePayload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), T>>
116 auto flatbuffer_builder() -> flatbuffers::FlatBufferBuilder&;
117#endif // IOX2_FEATURE_FLATBUFFERS
118
119 private:
120 template <ServiceType, typename, typename, typename, typename>
121 friend class ActiveRequest;
122
123 template <ServiceType S, typename ResponsePayloadT, typename ResponseUserHeaderT>
126
127#if IOX2_FEATURE_FLATBUFFERS
128 template <ServiceType S, typename ResponsePayloadT, typename ResponseUserHeaderT>
129 friend auto assume_init(ResponseMutUninit<S, Flatbuffer<ResponsePayloadT>, ResponseUserHeaderT>&& self,
130 flatbuffers::Offset<ResponsePayloadT>)
131 -> ResponseMut<S, Flatbuffer<ResponsePayloadT>, ResponseUserHeaderT>;
132#endif // IOX2_FEATURE_FLATBUFFERS
133
134 explicit ResponseMutUninit() = default;
135
136 auto get_handle() -> iox2_response_mut_h;
137
139
140#if IOX2_FEATURE_FLATBUFFERS
143#endif // IOX2_FEATURE_FLATBUFFERS
144};
145
146template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
147template <typename T, typename>
149 return m_response.header();
150}
151
152template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
153template <typename T, typename U, typename>
155 return m_response.template user_header<T>();
156}
157
158template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
159template <typename T, typename U, typename>
163
164template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
165template <typename T, typename>
167 return m_response.payload();
168}
169
170template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
171template <typename T, typename>
173 -> bb::ImmutableSlice<ValueType> {
174 return m_response.payload();
175}
176
177template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
178template <typename T, typename>
180 return m_response.payload_mut();
181}
182
183template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
184template <typename T, typename>
189
190template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
191template <typename T, typename>
193 m_response.truncate(number_of_elements);
194}
195
196template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
197template <typename T, typename>
200 new (&payload_mut()) ResponsePayload(std::forward<T>(payload));
201 return std::move(m_response);
202}
203
204template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
205template <typename T, typename>
206inline auto
209 auto dest = payload_mut();
210 IOX2_ASSERT(dest.number_of_bytes() >= value.number_of_bytes(),
211 "Destination payload size is smaller than source slice size");
212 std::memcpy(dest.begin(), value.begin(), value.number_of_bytes());
213 return std::move(m_response);
214}
215
216template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
217template <typename T, typename>
219 const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer)
221 auto slice = payload_mut();
222 for (uint64_t i = 0; i < slice.number_of_elements(); ++i) {
223 new (&slice[i]) typename T::ValueType(initializer(i));
224 }
225 return std::move(m_response);
226}
227
228template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
229inline auto ResponseMutUninit<Service, ResponsePayload, ResponseUserHeader>::get_handle() -> iox2_response_mut_h {
230 return m_response.m_handle;
231}
232
233#if IOX2_FEATURE_FLATBUFFERS
234template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
235template <typename T, typename>
237 -> flatbuffers::FlatBufferBuilder& {
238 return m_flatbuffer_builder.value();
239}
240
241template <ServiceType S, typename ResponsePayload, typename ResponseUserHeader>
242inline auto assume_init(ResponseMutUninit<S, Flatbuffer<ResponsePayload>, ResponseUserHeader>&& self,
243 flatbuffers::Offset<ResponsePayload> root)
244 -> ResponseMut<S, Flatbuffer<ResponsePayload>, ResponseUserHeader> {
245 self.flatbuffer_builder().Finish(root, nullptr);
246 const auto* payload_ptr = self.flatbuffer_builder().GetBufferPointer();
247 auto handle = self.get_handle();
248 iox2_response_mut_finish_serialized(&handle, payload_ptr);
249 // must be the last statement since `iox2_request_mut_finish_serialized` updates the
250 // header and user header ptrs when the flatbuffer builder has resized the memory
252
253 return std::move(self.m_response);
254}
255#endif // IOX2_FEATURE_FLATBUFFERS
256
257template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
262
263} // namespace iox2
264
265#endif
#define IOX2_ASSERT(condition, message)
only for debug builds: report fatal assert violation if expression evaluates to false
Response header used by [MessagingPattern::RequestResponse].
auto payload() const -> const T &
Returns a reference to the payload of the response.
auto payload_mut() -> T &
Returns a mutable reference to the payload of the response.
~ResponseMutUninit() noexcept=default
void truncate(uint64_t number_of_elements)
auto write_from_fn(const iox2::bb::StaticFunction< typename T::ValueType(uint64_t)> &initializer) -> ResponseMut< Service, T, ResponseUserHeader >
auto header() const -> ResponseHeader
Returns a reference to the [ResponseHeader].
auto write_from_slice(bb::ImmutableSlice< ValueType > &value) -> ResponseMut< Service, T, ResponseUserHeader >
auto user_header_mut() -> T &
Returns a mutable reference to the user header of the response.
ResponseMutUninit(ResponseMutUninit &&rhs) noexcept=default
auto flatbuffer_builder() -> flatbuffers::FlatBufferBuilder &
auto write_payload(ResponsePayload &&payload) -> ResponseMut< Service, T, ResponseUserHeader >
auto user_header() const -> const T &
Returns a reference to the user header of the response.
friend auto assume_init(ResponseMutUninit< S, Flatbuffer< ResponsePayloadT >, ResponseUserHeaderT > &&self, flatbuffers::Offset< ResponsePayloadT >) -> ResponseMut< S, Flatbuffer< ResponsePayloadT >, ResponseUserHeaderT >
friend auto assume_init(ResponseMutUninit< S, ResponsePayloadT, ResponseUserHeaderT > &&self) -> ResponseMut< S, ResponsePayloadT, ResponseUserHeaderT >
auto operator=(ResponseMutUninit &&rhs) noexcept -> ResponseMutUninit &=default
Internal helper class to handle dynamic memory allocations used in e.g. flatbuffers.
detail::StaticFunction< Capacity, Signature > StaticFunction
A static memory replacement for std::function.
Slice< const T > ImmutableSlice
Definition slice.hpp:95
Slice< T > MutableSlice
Definition slice.hpp:92
iox2::bb::variation::Optional< T > Optional
Definition optional.hpp:25
constexpr auto has_flatbuffer_marker() -> bool
Definition marker.hpp:53
auto assume_init(RequestMutUninit< Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader > &&self) -> RequestMut< Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader >
Identifies payloads that are serialized via flatbuffer.
Definition marker.hpp:24
static void placement_default(S &payload)
Definition helper.hpp:22