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/sample.hpp"
22#include "iox2/service_type.hpp"
25
26namespace iox2 {
28template <ServiceType S, typename Payload, typename UserHeader>
30 public:
31 Subscriber(Subscriber&& rhs) noexcept;
32 auto operator=(Subscriber&& rhs) noexcept -> Subscriber&;
34
35 Subscriber(const Subscriber&) = delete;
36 auto operator=(const Subscriber&) -> Subscriber& = delete;
37
39 auto id() const -> UniqueSubscriberId;
40
42 auto buffer_size() const -> uint64_t;
43
46 auto receive() const -> bb::Expected<bb::Optional<Sample<S, Payload, UserHeader>>, ReceiveError>;
47
50 auto has_samples() const -> bb::Expected<bool, ConnectionFailure>;
51
52 private:
53 template <ServiceType, typename, typename>
55
56 explicit Subscriber(iox2_subscriber_h handle);
57 void drop();
58
59 iox2_subscriber_h m_handle = nullptr;
60};
61template <ServiceType S, typename Payload, typename UserHeader>
62inline Subscriber<S, Payload, UserHeader>::Subscriber(iox2_subscriber_h handle)
63 : m_handle { handle } {
64}
65
66template <ServiceType S, typename Payload, typename UserHeader>
68 *this = std::move(rhs);
69}
70
71template <ServiceType S, typename Payload, typename UserHeader>
73 if (this != &rhs) {
74 drop();
75 m_handle = rhs.m_handle;
76 rhs.m_handle = nullptr;
77 }
78
79 return *this;
80}
81
82template <ServiceType S, typename Payload, typename UserHeader>
86
87template <ServiceType S, typename Payload, typename UserHeader>
89 if (m_handle != nullptr) {
90 iox2_subscriber_drop(m_handle);
91 m_handle = nullptr;
92 }
93}
94
95template <ServiceType S, typename Payload, typename UserHeader>
96inline auto Subscriber<S, Payload, UserHeader>::has_samples() const -> bb::Expected<bool, ConnectionFailure> {
97 bool has_samples_result = false;
98 auto result = iox2_subscriber_has_samples(&m_handle, &has_samples_result);
99
100 if (result == IOX2_OK) {
101 return has_samples_result;
102 }
103
104 return bb::err(bb::into<ConnectionFailure>(result));
105}
106
107template <ServiceType S, typename Payload, typename UserHeader>
109 iox2_unique_subscriber_id_h id_handle = nullptr;
110
111 iox2_subscriber_id(&m_handle, nullptr, &id_handle);
112 return UniqueSubscriberId { id_handle };
113}
114
115template <ServiceType S, typename Payload, typename UserHeader>
116inline auto Subscriber<S, Payload, UserHeader>::buffer_size() const -> uint64_t {
117 return iox2_subscriber_buffer_size(&m_handle);
118}
119
120template <ServiceType S, typename Payload, typename UserHeader>
122 -> bb::Expected<bb::Optional<Sample<S, Payload, UserHeader>>, ReceiveError> {
124 auto result = iox2_subscriber_receive(&m_handle, &sample.m_sample, &sample.m_handle);
125
126 if (result == IOX2_OK) {
127 if (sample.m_handle != nullptr) {
128 return bb::Optional<Sample<S, Payload, UserHeader>>(std::move(sample));
129 }
131 }
132
133 return bb::err(bb::into<ReceiveError>(result));
134}
135} // namespace iox2
136
137#endif
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 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