iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
publisher.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_PUBLISHER_HPP
14#define IOX2_PUBLISHER_HPP
15
16#include "iox2/bb/expected.hpp"
17#include "iox2/bb/slice.hpp"
19#include "iox2/iceoryx2.h"
23#include "iox2/sample_mut.hpp"
25#include "iox2/service_type.hpp"
27
28#include <cstdint>
29#include <type_traits>
30
31namespace iox2 {
33template <ServiceType S, typename Payload, typename UserHeader>
34class Publisher {
35 using ValueType = typename PayloadInfo<Payload>::ValueType;
36
37 public:
38 Publisher(Publisher&& rhs) noexcept;
39 auto operator=(Publisher&& rhs) noexcept -> Publisher&;
40 ~Publisher();
41
42 Publisher(const Publisher&) = delete;
43 auto operator=(const Publisher&) -> Publisher& = delete;
44
46 auto id() const -> UniquePublisherId;
47
51
53 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
54 auto initial_max_slice_len() const -> uint64_t;
55
59 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
60 auto send_copy(const Payload& payload) const -> bb::Expected<size_t, SendError>;
61
62 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
63 auto send_slice_copy(bb::ImmutableSlice<ValueType>& payload) const -> bb::Expected<size_t, SendError>;
64
69 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
70 auto loan_uninit() -> bb::Expected<SampleMutUninit<S, Payload, UserHeader>, LoanError>;
71
77 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
78 auto loan() -> bb::Expected<SampleMut<S, Payload, UserHeader>, LoanError>;
79
86 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
87 auto loan_slice(uint64_t number_of_elements) -> bb::Expected<SampleMut<S, T, UserHeader>, LoanError>;
88
93 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
94 auto loan_slice_uninit(uint64_t number_of_elements) -> bb::Expected<SampleMutUninit<S, T, UserHeader>, LoanError>;
95
102 auto update_connections() -> bb::Expected<void, ConnectionFailure>;
103
104 private:
105 template <ServiceType, typename, typename>
107
108 explicit Publisher(iox2_publisher_h handle);
109 void drop();
110
111 iox2_publisher_h m_handle = nullptr;
112};
113
114template <ServiceType S, typename Payload, typename UserHeader>
115inline Publisher<S, Payload, UserHeader>::Publisher(iox2_publisher_h handle)
116 : m_handle { handle } {
117}
118
119template <ServiceType S, typename Payload, typename UserHeader>
120inline void Publisher<S, Payload, UserHeader>::drop() {
121 if (m_handle != nullptr) {
122 iox2_publisher_drop(m_handle);
123 m_handle = nullptr;
124 }
125}
126
127template <ServiceType S, typename Payload, typename UserHeader>
129 *this = std::move(rhs);
130}
131
132template <ServiceType S, typename Payload, typename UserHeader>
134 if (this != &rhs) {
135 drop();
136 m_handle = rhs.m_handle;
137 rhs.m_handle = nullptr;
138 }
139
140 return *this;
141}
142
143template <ServiceType S, typename Payload, typename UserHeader>
147
148template <ServiceType S, typename Payload, typename UserHeader>
150 return iox2::bb::into<BackpressureStrategy>(static_cast<int>(iox2_publisher_backpressure_strategy(&m_handle)));
151}
152
153
154template <ServiceType S, typename Payload, typename UserHeader>
155template <typename T, typename>
157 return iox2_publisher_initial_max_slice_len(&m_handle);
158}
159
160template <ServiceType S, typename Payload, typename UserHeader>
162 iox2_unique_publisher_id_h id_handle = nullptr;
163
164 iox2_publisher_id(&m_handle, nullptr, &id_handle);
165 return UniquePublisherId { id_handle };
166}
167
168template <ServiceType S, typename Payload, typename UserHeader>
169template <typename T, typename>
170inline auto Publisher<S, Payload, UserHeader>::send_copy(const Payload& payload) const
172 static_assert(std::is_trivially_copyable<Payload>::value,
173 "The publisher supports only trivially copyable payload types.");
174
175 size_t number_of_recipients = 0;
176 auto result =
177 iox2_publisher_send_copy(&m_handle, static_cast<const void*>(&payload), sizeof(Payload), &number_of_recipients);
178
179 if (result == IOX2_OK) {
180 return number_of_recipients;
181 }
182
183 return bb::err(iox2::bb::into<SendError>(result));
184}
185
186template <ServiceType S, typename Payload, typename UserHeader>
187template <typename T, typename>
190 size_t number_of_recipients = 0;
191 auto result = iox2_publisher_send_slice_copy(&m_handle,
192 payload.data(),
193 sizeof(typename Payload::ValueType),
194 payload.number_of_elements(),
195 &number_of_recipients);
196
197 if (result == IOX2_OK) {
198 return number_of_recipients;
199 }
200
201 return bb::err(iox2::bb::into<SendError>(result));
202}
203
204template <ServiceType S, typename Payload, typename UserHeader>
205template <typename T, typename>
209
210 auto result = iox2_publisher_loan_slice_uninit(&m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, 1);
212
213 if (result == IOX2_OK) {
214 return std::move(sample);
215 }
216
217 return bb::err(iox2::bb::into<LoanError>(result));
218}
219
220template <ServiceType S, typename Payload, typename UserHeader>
221template <typename T, typename>
223 auto sample = loan_uninit();
224
225 if (!sample.has_value()) {
226 return bb::err(sample.error());
227 }
228
229 new (&sample->payload_mut()) Payload();
230
231 return assume_init(std::move(*sample));
232}
233
234template <ServiceType S, typename Payload, typename UserHeader>
235template <typename T, typename>
236inline auto Publisher<S, Payload, UserHeader>::loan_slice(const uint64_t number_of_elements)
238 auto sample_uninit = loan_slice_uninit(number_of_elements);
239
240 if (!sample_uninit.has_value()) {
241 return bb::err(sample_uninit.error());
242 }
243 auto sample_init = std::move(sample_uninit.value());
244
245 for (auto& item : sample_init.payload_mut()) {
246 new (&item) ValueType();
247 }
248
249 return assume_init(std::move(sample_init));
250}
251
252template <ServiceType S, typename Payload, typename UserHeader>
253template <typename T, typename>
254inline auto Publisher<S, Payload, UserHeader>::loan_slice_uninit(const uint64_t number_of_elements)
257
258 auto result = iox2_publisher_loan_slice_uninit(
259 &m_handle, &sample.m_sample.m_sample, &sample.m_sample.m_handle, number_of_elements);
261
262 if (result == IOX2_OK) {
263 return std::move(sample);
264 }
265
266 return bb::err(iox2::bb::into<LoanError>(result));
267}
268
269template <ServiceType S, typename Payload, typename UserHeader>
271 auto result = iox2_publisher_update_connections(&m_handle);
272 if (result != IOX2_OK) {
273 return bb::err(iox2::bb::into<ConnectionFailure>(result));
274 }
275
276 return {};
277}
278} // namespace iox2
279
280#endif
Sending endpoint of a publish-subscriber based communication.
Definition publisher.hpp:34
auto loan() -> bb::Expected< SampleMut< S, Payload, UserHeader >, LoanError >
auto operator=(Publisher &&rhs) noexcept -> Publisher &
auto id() const -> UniquePublisherId
Returns the [UniquePublisherId] of the [Publisher].
auto loan_uninit() -> bb::Expected< SampleMutUninit< S, Payload, UserHeader >, LoanError >
auto loan_slice_uninit(uint64_t number_of_elements) -> bb::Expected< SampleMutUninit< S, T, UserHeader >, LoanError >
auto initial_max_slice_len() const -> uint64_t
Returns the maximum number of elements that can be loaned in a slice.
auto send_copy(const Payload &payload) const -> bb::Expected< size_t, SendError >
Publisher(Publisher &&rhs) noexcept
auto update_connections() -> bb::Expected< void, ConnectionFailure >
Publisher(const Publisher &)=delete
auto backpressure_strategy() const -> BackpressureStrategy
auto operator=(const Publisher &) -> Publisher &=delete
auto loan_slice(uint64_t number_of_elements) -> bb::Expected< SampleMut< S, T, UserHeader >, LoanError >
auto send_slice_copy(bb::ImmutableSlice< ValueType > &payload) const -> bb::Expected< size_t, SendError >
The system-wide unique id of a [Publisher].
A class representing a slice of contiguous elements of type T.
Definition slice.hpp:31
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
SendError
Failure that can be emitted when data is sent.
auto assume_init(RequestMutUninit< Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader > &&self) -> RequestMut< Service, RequestPayload, RequestUserHeader, ResponsePayload, ResponseUserHeader >
static void placement_default(S &payload)
Definition helper.hpp:22