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,
93 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), T>>
94 auto write_payload(ResponsePayload&& payload) -> ResponseMut<Service, T, ResponseUserHeader>;
95
98 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
99 auto write_from_slice(bb::ImmutableSlice<ValueType>& value) -> ResponseMut<Service, T, ResponseUserHeader>;
100
103 template <typename T = ResponsePayload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
104 auto write_from_fn(const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer)
105 -> ResponseMut<Service, T, ResponseUserHeader>;
106
107#if IOX2_FEATURE_FLATBUFFERS
110 template <typename T = ResponsePayload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), T>>
111 auto flatbuffer_builder() -> flatbuffers::FlatBufferBuilder&;
112#endif // IOX2_FEATURE_FLATBUFFERS
113
114 private:
115 template <ServiceType, typename, typename, typename, typename>
116 friend class ActiveRequest;
117
118 template <ServiceType S, typename ResponsePayloadT, typename ResponseUserHeaderT>
121
122#if IOX2_FEATURE_FLATBUFFERS
123 template <ServiceType S, typename ResponsePayloadT, typename ResponseUserHeaderT>
124 friend auto assume_init(ResponseMutUninit<S, Flatbuffer<ResponsePayloadT>, ResponseUserHeaderT>&& self,
125 flatbuffers::Offset<ResponsePayloadT>)
126 -> ResponseMut<S, Flatbuffer<ResponsePayloadT>, ResponseUserHeaderT>;
127#endif // IOX2_FEATURE_FLATBUFFERS
128
129 explicit ResponseMutUninit() = default;
130
131 auto get_handle() -> iox2_response_mut_h;
132
134
135#if IOX2_FEATURE_FLATBUFFERS
138#endif // IOX2_FEATURE_FLATBUFFERS
139};
140
141template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
142template <typename T, typename>
144 return m_response.header();
145}
146
147template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
148template <typename T, typename U, typename>
150 return m_response.template user_header<T>();
151}
152
153template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
154template <typename T, typename U, typename>
156 return m_response.template user_header_mut<T>();
157}
158
159template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
160template <typename T, typename>
162 return m_response.payload();
163}
164
165template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
166template <typename T, typename>
168 -> bb::ImmutableSlice<ValueType> {
169 return m_response.payload();
170}
171
172template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
173template <typename T, typename>
175 return m_response.payload_mut();
176}
177
178template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
179template <typename T, typename>
182 return m_response.payload_mut();
183}
184
185template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
186template <typename T, typename>
189 new (&payload_mut()) ResponsePayload(std::forward<T>(payload));
190 return std::move(m_response);
191}
192
193template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
194template <typename T, typename>
195inline auto
198 auto dest = payload_mut();
199 IOX2_ASSERT(dest.number_of_bytes() >= value.number_of_bytes(),
200 "Destination payload size is smaller than source slice size");
201 std::memcpy(dest.begin(), value.begin(), value.number_of_bytes());
202 return std::move(m_response);
203}
204
205template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
206template <typename T, typename>
208 const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer)
210 auto slice = payload_mut();
211 for (uint64_t i = 0; i < slice.number_of_elements(); ++i) {
212 new (&slice[i]) typename T::ValueType(initializer(i));
213 }
214 return std::move(m_response);
215}
216
217template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
219 return m_response.m_handle;
220}
221
222#if IOX2_FEATURE_FLATBUFFERS
223template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
224template <typename T, typename>
226 -> flatbuffers::FlatBufferBuilder& {
227 return m_flatbuffer_builder.value();
228}
229
230template <ServiceType S, typename ResponsePayload, typename ResponseUserHeader>
231inline auto assume_init(ResponseMutUninit<S, Flatbuffer<ResponsePayload>, ResponseUserHeader>&& self,
232 flatbuffers::Offset<ResponsePayload> root)
233 -> ResponseMut<S, Flatbuffer<ResponsePayload>, ResponseUserHeader> {
234 self.flatbuffer_builder().Finish(root, nullptr);
235 const auto* payload_ptr = self.flatbuffer_builder().GetBufferPointer();
236 auto handle = self.get_handle();
237 iox2_response_mut_finish_serialized(&handle, payload_ptr);
238 // must be the last statement since `iox2_request_mut_finish_serialized` updates the
239 // header and user header ptrs when the flatbuffer builder has resized the memory
241
242 return std::move(self.m_response);
243}
244#endif // IOX2_FEATURE_FLATBUFFERS
245
246template <ServiceType Service, typename ResponsePayload, typename ResponseUserHeader>
251
252} // namespace iox2
253
254#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
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
A class representing a slice of contiguous elements of type T.
Definition slice.hpp:31
Internal helper class to handle dynamic memory allocations used in e.g. flatbuffers.
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