iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
sample.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_HPP
14#define IOX2_SAMPLE_HPP
15
16#include "iox2/bb/slice.hpp"
20#include "iox2/payload_info.hpp"
21#include "iox2/service_type.hpp"
23
24#include <type_traits>
25
26namespace iox2 {
27
39template <ServiceType, typename Payload, typename UserHeader>
40// 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
41class Sample {
42 using ValueType = typename PayloadInfo<Payload>::ValueType;
43
44 public:
45 Sample(Sample&& rhs) noexcept;
46 auto operator=(Sample&& rhs) noexcept -> Sample&;
47 ~Sample();
48
49 Sample(const Sample&) = delete;
50 auto operator=(const Sample&) -> Sample& = delete;
51
53 template <typename T = Payload, typename = std::enable_if_t<!bb::IsSlice<T>::VALUE, void>>
54 auto payload() const -> const ValueType&;
55
57 template <typename T = Payload, typename = std::enable_if_t<bb::IsSlice<T>::VALUE, void>>
58 auto payload() const -> bb::ImmutableSlice<ValueType>;
59
61 template <typename T = UserHeader, typename = std::enable_if_t<!std::is_same<void, UserHeader>::value, T>>
62 auto user_header() const -> const T&;
63
65 auto header() const -> HeaderPublishSubscribe;
66
68 auto origin() const -> UniquePublisherId;
69
70 private:
71 template <ServiceType, typename, typename>
72 friend class Subscriber;
73
74 // The sample is defaulted since both members are initialized in Subscriber::receive
75 explicit Sample() = default;
76 void drop();
77
78 iox2_sample_t m_sample;
79 iox2_sample_h m_handle = nullptr;
80};
81
82template <ServiceType S, typename Payload, typename UserHeader>
83inline void Sample<S, Payload, UserHeader>::drop() {
84 if (m_handle != nullptr) {
85 iox2_sample_drop(m_handle);
86 m_handle = nullptr;
87 }
88}
89
90// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init,hicpp-member-init) m_sample will be initialized in the move assignment operator
91template <ServiceType S, typename Payload, typename UserHeader>
93 *this = std::move(rhs);
94}
95
96namespace internal {
97extern "C" {
98void iox2_sample_move(iox2_sample_t*, iox2_sample_t*, iox2_sample_h*);
99}
100} // namespace internal
101
102template <ServiceType S, typename Payload, typename UserHeader>
104 if (this != &rhs) {
105 drop();
106
107 internal::iox2_sample_move(&rhs.m_sample, &m_sample, &m_handle);
108 rhs.m_handle = nullptr;
109 }
110
111 return *this;
112}
113
114template <ServiceType S, typename Payload, typename UserHeader>
116 drop();
117}
118
119template <ServiceType S, typename Payload, typename UserHeader>
120template <typename T, typename>
121inline auto Sample<S, Payload, UserHeader>::payload() const -> const ValueType& {
122 const void* ptr = nullptr;
123
124 iox2_sample_payload(&m_handle, &ptr, nullptr);
125
126 return *static_cast<const ValueType*>(ptr);
127}
128
129template <ServiceType S, typename Payload, typename UserHeader>
130template <typename T, typename>
131inline auto Sample<S, Payload, UserHeader>::payload() const -> bb::ImmutableSlice<ValueType> {
132 const void* ptr = nullptr;
133 size_t number_of_elements = 0;
134
135 iox2_sample_payload(&m_handle, &ptr, &number_of_elements);
136
137 // for the custom payload marker, the slice length is the
138 // runtime payload byte size
139 auto length = number_of_elements;
140 if (std::is_same<ValueType, CustomPayloadMarker>::value) {
141 length = iox2_sample_payload_number_of_bytes(&m_handle);
142 }
143
144 return bb::ImmutableSlice<ValueType>(static_cast<const ValueType*>(ptr), length);
145}
146
147template <ServiceType S, typename Payload, typename UserHeader>
148template <typename T, typename>
149inline auto Sample<S, Payload, UserHeader>::user_header() const -> const T& {
150 const void* header_ptr = nullptr;
151
152 iox2_sample_user_header(&m_handle, &header_ptr);
153
154 return *static_cast<const T*>(header_ptr);
155}
156
157template <ServiceType S, typename Payload, typename UserHeader>
159 iox2_publish_subscribe_header_h header_handle = nullptr;
160 iox2_sample_header(&m_handle, nullptr, &header_handle);
161
162 return HeaderPublishSubscribe { header_handle };
163}
164
165template <ServiceType S, typename Payload, typename UserHeader>
167 return header().publisher_id();
168}
169
170} // namespace iox2
171
172#endif
Sample header used by [MessagingPattern::PublishSubscribe].
auto header() const -> HeaderPublishSubscribe
Returns a reference to the [Header] of the [Sample].
Definition sample.hpp:158
auto payload() const -> const ValueType &
Returns a reference to the payload of the [Sample].
Definition sample.hpp:121
auto origin() const -> UniquePublisherId
Returns the [UniquePublisherId] of the [Publisher](crate::port::publisher::Publisher)
Definition sample.hpp:166
auto operator=(Sample &&rhs) noexcept -> Sample &
Definition sample.hpp:103
auto user_header() const -> const T &
Returns a reference to the user_header of the [Sample].
Definition sample.hpp:149
Sample(const Sample &)=delete
auto operator=(const Sample &) -> Sample &=delete
The receiving endpoint of a publish-subscribe communication.
The system-wide unique id of a [Publisher].