iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
subscriber.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_SUBSCRIBER_HPP
14#define IOX2_SUBSCRIBER_HPP
15
16#include "iox2/bb/expected.hpp"
17#include "iox2/bb/optional.hpp"
19#include "iox2/iceoryx2.h"
21#include "iox2/port_name.hpp"
22#include "iox2/sample.hpp"
23#include "iox2/service_type.hpp"
26
27namespace iox2 {
29template <ServiceType S, typename Payload, typename UserHeader>
31 public:
32 Subscriber(Subscriber&& rhs) noexcept;
33 auto operator=(Subscriber&& rhs) noexcept -> Subscriber&;
35
36 Subscriber(const Subscriber&) = delete;
37 auto operator=(const Subscriber&) -> Subscriber& = delete;
38
40 auto id() const -> UniqueSubscriberId;
41
43 auto name() const -> PortNameView;
44
46 auto buffer_size() const -> uint64_t;
47
50 auto receive() const -> bb::Expected<bb::Optional<Sample<S, Payload, UserHeader>>, ReceiveError>;
51
54 auto has_samples() const -> bb::Expected<bool, ConnectionFailure>;
55
56 private:
57 template <ServiceType, typename, typename>
59
60 explicit Subscriber(iox2_subscriber_h handle);
61 void drop();
62
63 iox2_subscriber_h m_handle = nullptr;
64};
65template <ServiceType S, typename Payload, typename UserHeader>
66inline Subscriber<S, Payload, UserHeader>::Subscriber(iox2_subscriber_h handle)
67 : m_handle { handle } {
68}
69
70template <ServiceType S, typename Payload, typename UserHeader>
72 *this = std::move(rhs);
73}
74
75template <ServiceType S, typename Payload, typename UserHeader>
77 if (this != &rhs) {
78 drop();
79 m_handle = rhs.m_handle;
80 rhs.m_handle = nullptr;
81 }
82
83 return *this;
84}
85
86template <ServiceType S, typename Payload, typename UserHeader>
90
91template <ServiceType S, typename Payload, typename UserHeader>
93 if (m_handle != nullptr) {
94 iox2_subscriber_drop(m_handle);
95 m_handle = nullptr;
96 }
97}
98
99template <ServiceType S, typename Payload, typename UserHeader>
100inline auto Subscriber<S, Payload, UserHeader>::has_samples() const -> bb::Expected<bool, ConnectionFailure> {
101 bool has_samples_result = false;
102 auto result = iox2_subscriber_has_samples(&m_handle, &has_samples_result);
103
104 if (result == IOX2_OK) {
105 return has_samples_result;
106 }
107
108 return bb::err(bb::into<ConnectionFailure>(result));
109}
110
111template <ServiceType S, typename Payload, typename UserHeader>
113 iox2_unique_subscriber_id_h id_handle = nullptr;
114
115 iox2_subscriber_id(&m_handle, nullptr, &id_handle);
116 return UniqueSubscriberId { id_handle };
117}
118
119template <ServiceType S, typename Payload, typename UserHeader>
121 const auto* port_name_ptr = iox2_subscriber_name(&m_handle);
122 return PortNameView::from_native_ptr(port_name_ptr);
123}
124
125template <ServiceType S, typename Payload, typename UserHeader>
126inline auto Subscriber<S, Payload, UserHeader>::buffer_size() const -> uint64_t {
127 return iox2_subscriber_buffer_size(&m_handle);
128}
129
130template <ServiceType S, typename Payload, typename UserHeader>
132 -> bb::Expected<bb::Optional<Sample<S, Payload, UserHeader>>, ReceiveError> {
134 auto result = iox2_subscriber_receive(&m_handle, &sample.m_sample, &sample.m_handle);
135
136 if (result == IOX2_OK) {
137 if (sample.m_handle != nullptr) {
138 return bb::Optional<Sample<S, Payload, UserHeader>>(std::move(sample));
139 }
141 }
142
143 return bb::err(bb::into<ReceiveError>(result));
144}
145} // namespace iox2
146
147#endif
Non-owning view of a [PortName].
Definition port_name.hpp:25
static auto from_native_ptr(iox2_port_name_ptr m_ptr) -> PortNameView
Creates a [PortNameView] from a native [iox2_port_name_ptr].
The receiving endpoint of a publish-subscribe communication.
Subscriber(Subscriber &&rhs) noexcept
auto has_samples() const -> bb::Expected< bool, ConnectionFailure >
auto receive() const -> bb::Expected< bb::Optional< Sample< S, Payload, UserHeader > >, ReceiveError >
auto operator=(Subscriber &&rhs) noexcept -> Subscriber &
auto id() const -> UniqueSubscriberId
Returns the [UniqueSubscriberId] of the [Subscriber].
auto operator=(const Subscriber &) -> Subscriber &=delete
auto name() const -> PortNameView
Returns the [PortNameView] of the [Subscriber].
auto buffer_size() const -> uint64_t
Returns the internal buffer size of the [Subscriber].
Subscriber(const Subscriber &)=delete
The system-wide unique id of a [Subscriber].
constexpr NulloptT NULLOPT
Definition optional.hpp:28
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Optional< T > Optional
Definition optional.hpp:25