iceoryx2
C++ Language 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"
19#include "iox2/iceoryx2_cxx_deployment.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
81 template <typename T = Payload,
82 typename = std::enable_if_t<!bb::IsSlice<T>::VALUE && !has_flatbuffer_marker<T>(), T>>
83 auto write_payload(T&& value) -> SampleMut<S, Payload, UserHeader>;
84
86 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
87 auto write_from_fn(const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer)
88 -> SampleMut<S, Payload, UserHeader>;
89
91 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
92 auto write_from_slice(bb::ImmutableSlice<ValueType>& value) -> SampleMut<S, Payload, UserHeader>;
93
94#if IOX2_FEATURE_FLATBUFFERS
97 template <typename T = Payload, typename = std::enable_if_t<has_flatbuffer_marker<T>(), T>>
98 auto flatbuffer_builder() -> flatbuffers::FlatBufferBuilder&;
99#endif // IOX2_FEATURE_FLATBUFFERS
100
101 private:
102 template <ServiceType, typename, typename>
103 friend class Publisher;
104
105 template <ServiceType ST, typename PayloadT, typename UserHeaderT>
107
108#if IOX2_FEATURE_FLATBUFFERS
109 template <ServiceType ST, typename PayloadT, typename UserHeaderT>
110 friend auto assume_init(SampleMutUninit<ST, Flatbuffer<PayloadT>, UserHeaderT>&& self,
111 flatbuffers::Offset<PayloadT>) -> SampleMut<ST, Flatbuffer<PayloadT>, UserHeaderT>;
112#endif // IOX2_FEATURE_FLATBUFFERS
113
114 // The sample is defaulted since both members are initialized in Publisher::loan_uninit() or
115 // Publisher::loan_slice_uninit()
116 explicit SampleMutUninit() = default;
117
118 auto get_handle() -> iox2_sample_mut_h;
119
121
122#if IOX2_FEATURE_FLATBUFFERS
123 internal::ResizableMemoryPublishSubscribe<S>* m_memory = nullptr;
125#endif // IOX2_FEATURE_FLATBUFFERS
126};
127
130template <ServiceType S, typename Payload, typename UserHeader>
132 return std::move(self.m_sample);
133}
134
135#if IOX2_FEATURE_FLATBUFFERS
136
139template <ServiceType S, typename Payload, typename UserHeader>
140inline auto assume_init(SampleMutUninit<S, Flatbuffer<Payload>, UserHeader>&& self, flatbuffers::Offset<Payload> root)
141 -> SampleMut<S, Flatbuffer<Payload>, UserHeader> {
142 self.flatbuffer_builder().Finish(root, nullptr);
143 const auto* payload_ptr = self.flatbuffer_builder().GetBufferPointer();
144 auto handle = self.get_handle();
145 iox2_sample_mut_finish_serialized(&handle, payload_ptr);
146 // must be the last statement since `iox2_sample_mut_finish_serialized` updates the
147 // header and user header ptrs when the flatbuffer builder has resized the memory
149
150 return std::move(self.m_sample);
151}
152
153template <ServiceType S, typename Payload, typename UserHeader>
154template <typename T, typename>
155inline auto SampleMutUninit<S, Payload, UserHeader>::flatbuffer_builder() -> flatbuffers::FlatBufferBuilder& {
156 return m_flatbuffer_builder.value();
157}
158
159#endif // IOX2_FEATURE_FLATBUFFERS
160
161template <ServiceType S, typename Payload, typename UserHeader>
162inline auto SampleMutUninit<S, Payload, UserHeader>::get_handle() -> iox2_sample_mut_h {
163 return m_sample.m_handle;
164}
165
166template <ServiceType S, typename Payload, typename UserHeader>
167template <typename T, typename>
169 return m_sample.header();
170}
171
172template <ServiceType S, typename Payload, typename UserHeader>
173template <typename T, typename U, typename>
175 return m_sample.template user_header<T>();
176}
177
178template <ServiceType S, typename Payload, typename UserHeader>
179template <typename T, typename U, typename>
181 return m_sample.template user_header_mut<T>();
182}
183
184template <ServiceType S, typename Payload, typename UserHeader>
185template <typename T, typename>
186inline auto SampleMutUninit<S, Payload, UserHeader>::payload() const -> const ValueType& {
187 return m_sample.payload();
188}
189
190template <ServiceType S, typename Payload, typename UserHeader>
191template <typename T, typename>
193 return m_sample.payload_mut();
194}
195
196template <ServiceType S, typename Payload, typename UserHeader>
197template <typename T, typename>
198inline auto SampleMutUninit<S, Payload, UserHeader>::payload() const -> bb::ImmutableSlice<ValueType> {
199 return m_sample.payload();
200}
201
202template <ServiceType S, typename Payload, typename UserHeader>
203template <typename T, typename>
204inline auto SampleMutUninit<S, Payload, UserHeader>::payload_mut() -> bb::MutableSlice<ValueType> {
205 return m_sample.payload_mut();
206}
207
208template <ServiceType S, typename Payload, typename UserHeader>
209template <typename T, typename>
211 new (&payload_mut()) Payload(std::forward<T>(value));
212 return std::move(m_sample);
213}
214
215template <ServiceType S, typename Payload, typename UserHeader>
216template <typename T, typename>
218 const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer) -> SampleMut<S, Payload, UserHeader> {
219 auto slice = payload_mut();
220 for (uint64_t i = 0; i < slice.number_of_elements(); ++i) {
221 new (&slice[i]) typename T::ValueType(initializer(i));
222 }
223 return std::move(m_sample);
224}
225
226template <ServiceType S, typename Payload, typename UserHeader>
227template <typename T, typename>
230 auto dest = payload_mut();
231 IOX2_ASSERT(dest.number_of_bytes() >= value.number_of_bytes(),
232 "Destination payload size is smaller than source slice size");
233 std::memcpy(dest.begin(), value.begin(), value.number_of_bytes());
234 return std::move(m_sample);
235}
236} // namespace iox2
237
238#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].
Sending endpoint of a publish-subscriber based communication.
Definition publisher.hpp:40
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, PayloadT, UserHeaderT > &&self) -> SampleMut< ST, PayloadT, UserHeaderT >
~SampleMutUninit() noexcept=default
auto header() const -> HeaderPublishSubscribe
Returns a reference to the [Header] of the [Sample].
auto payload() const -> const ValueType &
Returns a reference to the const payload of the sample.
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
A class representing a slice of contiguous elements of type T.
Definition slice.hpp:31
iox2::bb::variation::Optional< T > Optional
Definition optional.hpp:25
constexpr auto has_flatbuffer_marker() -> bool
Definition marker.hpp:49
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:21
static void placement_default(S &payload)
Definition helper.hpp:22