iceoryx2
C++ Bindings
Loading...
Searching...
No Matches
sample_mut_uninit.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_UNINIT_HPP
14#define IOX2_SAMPLE_MUT_UNINIT_HPP
15
16#include "iox2/bb/slice.hpp"
18#include "iox2/deployment/iceoryx2_cxx_feature_flags.hpp"
21#include "iox2/marker.hpp"
22#include "iox2/sample_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
31
32namespace iox2 {
33template <ServiceType S, typename Payload, typename UserHeader>
34// 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
36 using ValueType = typename PayloadInfo<Payload>::ValueType;
37
38 public:
39 SampleMutUninit(SampleMutUninit&& rhs) noexcept = default;
40 auto operator=(SampleMutUninit&& rhs) noexcept -> SampleMutUninit& = default;
41 ~SampleMutUninit() noexcept = default;
42
44 auto operator=(const SampleMutUninit&) -> SampleMutUninit& = delete;
45
47 template <typename T = Payload, typename = std::enable_if_t<!has_flatbuffer_marker<T>(), void>>
48 auto header() const -> HeaderPublishSubscribe;
49
51 template <typename T = UserHeader,
52 typename U = Payload,
53 typename = std::enable_if_t<!std::is_same<void, UserHeader>::value && !has_flatbuffer_marker<U>(), T>>
54 auto user_header() const -> const T&;
55
57 template <typename T = UserHeader,
58 typename U = Payload,
59 typename = std::enable_if_t<!std::is_same<void, UserHeader>::value && !has_flatbuffer_marker<U>(), T>>
60 auto user_header_mut() -> T&;
61
63 template <typename T = Payload,
64 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
65 auto payload() const -> const ValueType&;
66
68 template <typename T = Payload,
69 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), void>>
70 auto payload_mut() -> ValueType&;
71
73 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
74 auto payload() const -> bb::ImmutableSlice<ValueType>;
75
77 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
78 auto payload_mut() -> bb::MutableSlice<ValueType>;
79
82 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
83 void truncate(uint64_t number_of_elements);
84
86 template <typename T = Payload,
87 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), T>>
88 auto write_payload(T&& value) -> SampleMut<S, Payload, UserHeader>;
89
91 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
92 auto write_from_fn(const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer)
93 -> SampleMut<S, Payload, UserHeader>;
94
96 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
97 auto write_from_slice(bb::ImmutableSlice<ValueType>& value) -> SampleMut<S, Payload, UserHeader>;
98
99#if IOX2_FEATURE_FLATBUFFERS
102 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), T>>
103 auto flatbuffer_builder() -> flatbuffers::FlatBufferBuilder&;
104#endif // IOX2_FEATURE_FLATBUFFERS
105
106 private:
107 template <ServiceType, typename, typename>
108 friend class Publisher;
109
110 template <ServiceType ST, typename PayloadT, typename UserHeaderT>
112
113#if IOX2_FEATURE_FLATBUFFERS
114 template <ServiceType ST, typename PayloadT, typename UserHeaderT>
115 friend auto assume_init(SampleMutUninit<ST, Flatbuffer<PayloadT>, UserHeaderT>&& self,
116 flatbuffers::Offset<PayloadT>) -> SampleMut<ST, Flatbuffer<PayloadT>, UserHeaderT>;
117#endif // IOX2_FEATURE_FLATBUFFERS
118
119 // The sample is defaulted since both members are initialized in Publisher::loan_uninit() or
120 // Publisher::loan_slice_uninit()
121 explicit SampleMutUninit() = default;
122
123 auto get_handle() -> iox2_sample_mut_h;
124
126
127#if IOX2_FEATURE_FLATBUFFERS
130#endif // IOX2_FEATURE_FLATBUFFERS
131};
132
135template <ServiceType S, typename Payload, typename UserHeader>
137 return std::move(self.m_sample);
138}
139
140#if IOX2_FEATURE_FLATBUFFERS
141
144template <ServiceType S, typename Payload, typename UserHeader>
145inline auto assume_init(SampleMutUninit<S, Flatbuffer<Payload>, UserHeader>&& self, flatbuffers::Offset<Payload> root)
146 -> SampleMut<S, Flatbuffer<Payload>, UserHeader> {
147 self.flatbuffer_builder().Finish(root, nullptr);
148 const auto* payload_ptr = self.flatbuffer_builder().GetBufferPointer();
149 auto handle = self.get_handle();
150 iox2_sample_mut_finish_serialized(&handle, payload_ptr);
151 // must be the last statement since `iox2_sample_mut_finish_serialized` updates the
152 // header and user header ptrs when the flatbuffer builder has resized the memory
154
155 return std::move(self.m_sample);
156}
157
158template <ServiceType S, typename Payload, typename UserHeader>
159template <typename T, typename>
160inline auto SampleMutUninit<S, Payload, UserHeader>::flatbuffer_builder() -> flatbuffers::FlatBufferBuilder& {
161 return m_flatbuffer_builder.value();
162}
163
164#endif // IOX2_FEATURE_FLATBUFFERS
165
166template <ServiceType S, typename Payload, typename UserHeader>
167inline auto SampleMutUninit<S, Payload, UserHeader>::get_handle() -> iox2_sample_mut_h {
168 return m_sample.m_handle;
169}
170
171template <ServiceType S, typename Payload, typename UserHeader>
172template <typename T, typename>
174 return m_sample.header();
175}
176
177template <ServiceType S, typename Payload, typename UserHeader>
178template <typename T, typename U, typename>
180 return m_sample.template user_header<T>();
181}
182
183template <ServiceType S, typename Payload, typename UserHeader>
184template <typename T, typename U, typename>
186 return m_sample.template user_header_mut<T>();
187}
188
189template <ServiceType S, typename Payload, typename UserHeader>
190template <typename T, typename>
191inline auto SampleMutUninit<S, Payload, UserHeader>::payload() const -> const ValueType& {
192 return m_sample.payload();
193}
194
195template <ServiceType S, typename Payload, typename UserHeader>
196template <typename T, typename>
198 return m_sample.payload_mut();
199}
200
201template <ServiceType S, typename Payload, typename UserHeader>
202template <typename T, typename>
203inline auto SampleMutUninit<S, Payload, UserHeader>::payload() const -> bb::ImmutableSlice<ValueType> {
204 return m_sample.payload();
205}
206
207template <ServiceType S, typename Payload, typename UserHeader>
208template <typename T, typename>
210 return m_sample.payload_mut();
211}
212
213template <ServiceType S, typename Payload, typename UserHeader>
214template <typename T, typename>
215inline void SampleMutUninit<S, Payload, UserHeader>::truncate(uint64_t number_of_elements) {
216 m_sample.truncate(number_of_elements);
217}
218
219template <ServiceType S, typename Payload, typename UserHeader>
220template <typename T, typename>
222 new (&payload_mut()) Payload(std::forward<T>(value));
223 return std::move(m_sample);
224}
225
226template <ServiceType S, typename Payload, typename UserHeader>
227template <typename T, typename>
229 const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer) -> SampleMut<S, Payload, UserHeader> {
230 auto slice = payload_mut();
231 for (uint64_t i = 0; i < slice.number_of_elements(); ++i) {
232 new (&slice[i]) typename T::ValueType(initializer(i));
233 }
234 return std::move(m_sample);
235}
236
237template <ServiceType S, typename Payload, typename UserHeader>
238template <typename T, typename>
241 auto dest = payload_mut();
242 IOX2_ASSERT(dest.number_of_bytes() >= value.number_of_bytes(),
243 "Destination payload size is smaller than source slice size");
244 std::memcpy(dest.begin(), value.begin(), value.number_of_bytes());
245 return std::move(m_sample);
246}
247} // namespace iox2
248
249#endif
#define IOX2_ASSERT(condition, message)
only for debug builds: report fatal assert violation if expression evaluates to false
Sample header used by [MessagingPattern::PublishSubscribe].
auto payload_mut() -> ValueType &
Returns a reference to the payload of the sample.
auto user_header() const -> const T &
Returns a reference to the user_header of the [Sample].
auto write_payload(T &&value) -> SampleMut< S, Payload, UserHeader >
Writes the payload to the sample.
auto operator=(SampleMutUninit &&rhs) noexcept -> SampleMutUninit &=default
friend auto assume_init(SampleMutUninit< ST, Flatbuffer< PayloadT >, UserHeaderT > &&self, flatbuffers::Offset< PayloadT >) -> SampleMut< ST, Flatbuffer< PayloadT >, UserHeaderT >
friend auto assume_init(SampleMutUninit< ST, PayloadT, UserHeaderT > &&self) -> SampleMut< ST, PayloadT, UserHeaderT >
~SampleMutUninit() noexcept=default
auto header() const -> HeaderPublishSubscribe
Returns a reference to the [Header] of the [Sample].
void truncate(uint64_t number_of_elements)
auto payload() const -> const ValueType &
Returns a reference to the const payload of the sample.
auto flatbuffer_builder() -> flatbuffers::FlatBufferBuilder &
auto user_header_mut() -> T &
Returns a mutable reference to the user_header of the [Sample].
auto write_from_fn(const iox2::bb::StaticFunction< typename T::ValueType(uint64_t)> &initializer) -> SampleMut< S, Payload, UserHeader >
Writes the payload to the sample.
auto write_from_slice(bb::ImmutableSlice< ValueType > &value) -> SampleMut< S, Payload, UserHeader >
mem copies the value to the sample
SampleMutUninit(SampleMutUninit &&rhs) noexcept=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