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/sample_mut.hpp"
20#include "iox2/service_type.hpp"
21
22namespace iox2 {
23template <ServiceType S, typename Payload, typename UserHeader>
24// 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
26 using ValueType = typename PayloadInfo<Payload>::ValueType;
27
28 public:
29 SampleMutUninit(SampleMutUninit&& rhs) noexcept = default;
30 auto operator=(SampleMutUninit&& rhs) noexcept -> SampleMutUninit& = default;
31 ~SampleMutUninit() noexcept = default;
32
34 auto operator=(const SampleMutUninit&) -> SampleMutUninit& = delete;
35
37 auto header() const -> HeaderPublishSubscribe;
38
40 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<void, UserHeader>::value, T>>
41 auto user_header() const -> const T&;
42
44 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<void, UserHeader>::value, T>>
45 auto user_header_mut() -> T&;
46
48 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
49 auto payload() const -> const ValueType&;
50
52 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
53 auto payload_mut() -> ValueType&;
54
55 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
56 auto payload() const -> bb::ImmutableSlice<ValueType>;
57
58 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
59 auto payload_mut() -> bb::MutableSlice<ValueType>;
60
62 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, T>>
63 auto write_payload(T&& value) -> SampleMut<S, Payload, UserHeader>;
64
66 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
67 auto write_from_fn(const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer)
68 -> SampleMut<S, Payload, UserHeader>;
69
71 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, T>>
72 auto write_from_slice(bb::ImmutableSlice<ValueType>& value) -> SampleMut<S, Payload, UserHeader>;
73
74 private:
75 template <ServiceType, typename, typename>
76 friend class Publisher;
77
78 template <ServiceType ST, typename PayloadT, typename UserHeaderT>
79 friend auto assume_init(SampleMutUninit<ST, PayloadT, UserHeaderT>&& self) -> SampleMut<ST, PayloadT, UserHeaderT>;
80
81 // The sample is defaulted since both members are initialized in Publisher::loan_uninit() or
82 // Publisher::loan_slice_uninit()
83 explicit SampleMutUninit() = default;
84
85 SampleMut<S, Payload, UserHeader> m_sample;
86};
87
90template <ServiceType S, typename Payload, typename UserHeader>
91inline auto assume_init(SampleMutUninit<S, Payload, UserHeader>&& self) -> SampleMut<S, Payload, UserHeader> {
92 return std::move(self.m_sample);
93}
94
95template <ServiceType S, typename Payload, typename UserHeader>
97 return m_sample.header();
98}
99
100template <ServiceType S, typename Payload, typename UserHeader>
101template <typename T, typename>
103 return m_sample.template user_header<T>();
104}
105
106template <ServiceType S, typename Payload, typename UserHeader>
107template <typename T, typename>
109 return m_sample.template user_header_mut<T>();
110}
111
112template <ServiceType S, typename Payload, typename UserHeader>
113template <typename T, typename>
114inline auto SampleMutUninit<S, Payload, UserHeader>::payload() const -> const ValueType& {
115 return m_sample.payload();
116}
117
118template <ServiceType S, typename Payload, typename UserHeader>
119template <typename T, typename>
121 return m_sample.payload_mut();
122}
123
124template <ServiceType S, typename Payload, typename UserHeader>
125template <typename T, typename>
126inline auto SampleMutUninit<S, Payload, UserHeader>::payload() const -> bb::ImmutableSlice<ValueType> {
127 return m_sample.payload();
128}
129
130template <ServiceType S, typename Payload, typename UserHeader>
131template <typename T, typename>
132inline auto SampleMutUninit<S, Payload, UserHeader>::payload_mut() -> bb::MutableSlice<ValueType> {
133 return m_sample.payload_mut();
134}
135
136template <ServiceType S, typename Payload, typename UserHeader>
137template <typename T, typename>
139 new (&payload_mut()) Payload(std::forward<T>(value));
140 return std::move(m_sample);
141}
142
143template <ServiceType S, typename Payload, typename UserHeader>
144template <typename T, typename>
146 const iox2::bb::StaticFunction<typename T::ValueType(uint64_t)>& initializer) -> SampleMut<S, Payload, UserHeader> {
147 auto slice = payload_mut();
148 for (uint64_t i = 0; i < slice.number_of_elements(); ++i) {
149 new (&slice[i]) typename T::ValueType(initializer(i));
150 }
151 return std::move(m_sample);
152}
153
154template <ServiceType S, typename Payload, typename UserHeader>
155template <typename T, typename>
158 auto dest = payload_mut();
159 IOX2_ASSERT(dest.number_of_bytes() >= value.number_of_bytes(),
160 "Destination payload size is smaller than source slice size");
161 std::memcpy(dest.begin(), value.begin(), value.number_of_bytes());
162 return std::move(m_sample);
163}
164} // namespace iox2
165
166#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:34
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 user_header_mut() -> T &
Returns a mutable reference to the user_header of the [Sample].
auto payload() const -> const ValueType &
Returns a reference to the const payload 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