iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
reader.hpp
Go to the documentation of this file.
1// Copyright (c) 2025 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_READER_HPP
14#define IOX2_READER_HPP
15
17#include "iox2/bb/expected.hpp"
18#include "iox2/entry_handle.hpp"
21#include "iox2/service_type.hpp"
23
24namespace iox2 {
26template <ServiceType S, typename KeyType>
27class Reader {
28 public:
29 Reader(Reader&& rhs) noexcept;
30 auto operator=(Reader&& rhs) noexcept -> Reader&;
31 ~Reader();
32
33 Reader(const Reader&) = delete;
34 auto operator=(const Reader&) -> Reader& = delete;
35
37 auto id() const -> UniqueReaderId;
38
40 template <typename ValueType>
41 auto entry(const KeyType& key) -> bb::Expected<EntryHandle<S, KeyType, ValueType>, EntryHandleError>;
42
43 private:
44 template <ServiceType, typename>
45 friend class PortFactoryReader;
46
47 explicit Reader(iox2_reader_h handle);
48 void drop();
49
50 iox2_reader_h m_handle = nullptr;
51};
52
53template <ServiceType S, typename KeyType>
54inline Reader<S, KeyType>::Reader(iox2_reader_h handle)
55 : m_handle { handle } {
56}
57
58template <ServiceType S, typename KeyType>
59inline void Reader<S, KeyType>::drop() {
60 if (m_handle != nullptr) {
61// NOTE: false positive; m_handle is initialized in the class itself
62#if (defined(__GNUC__) && __GNUC__ == 13 && !defined(__clang__))
63#pragma GCC diagnostic push
64#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
65#endif
66 iox2_reader_drop(m_handle);
67#if (defined(__GNUC__) && __GNUC__ == 13 && !defined(__clang__))
68#pragma GCC diagnostic pop
69#endif
70 m_handle = nullptr;
71 }
72}
73
74template <ServiceType S, typename KeyType>
75inline Reader<S, KeyType>::Reader(Reader&& rhs) noexcept {
76 *this = std::move(rhs);
77}
78
79template <ServiceType S, typename KeyType>
80inline auto Reader<S, KeyType>::operator=(Reader&& rhs) noexcept -> Reader& {
81 if (this != &rhs) {
82 drop();
83 m_handle = rhs.m_handle;
84 rhs.m_handle = nullptr;
85 }
86
87 return *this;
88}
89
90template <ServiceType S, typename KeyType>
92 drop();
93}
94
95template <ServiceType S, typename KeyType>
96inline auto Reader<S, KeyType>::id() const -> UniqueReaderId {
97 iox2_unique_reader_id_h id_handle = nullptr;
98
99 iox2_reader_id(&m_handle, nullptr, &id_handle);
100 return UniqueReaderId { id_handle };
101}
102
103template <ServiceType S, typename KeyType>
104template <typename ValueType>
105inline auto Reader<S, KeyType>::entry(const KeyType& key)
107 iox2_entry_handle_h entry_handle {};
108 const auto type_name = internal::get_type_name<ValueType>();
109
110 auto result = iox2_reader_entry(&m_handle,
111 nullptr,
112 &entry_handle,
113 &key,
114 type_name.unchecked_access().c_str(),
115 type_name.size(),
116 sizeof(ValueType),
117 alignof(ValueType));
118
119 if (result == IOX2_OK) {
120 return EntryHandle<S, KeyType, ValueType>(entry_handle);
121 }
122
123 return bb::err(bb::into<EntryHandleError>(result));
124}
125} // namespace iox2
126
127#endif
A handle for direct read access to a specific blackboard value.
Reading endpoint of a blackboard based communication.
Definition reader.hpp:27
auto id() const -> UniqueReaderId
Returns the [UniqueReaderId] of the [Reader].
Definition reader.hpp:96
auto operator=(const Reader &) -> Reader &=delete
auto operator=(Reader &&rhs) noexcept -> Reader &
Definition reader.hpp:80
Reader(const Reader &)=delete
Reader(Reader &&rhs) noexcept
Definition reader.hpp:75
auto entry(const KeyType &key) -> bb::Expected< EntryHandle< S, KeyType, ValueType >, EntryHandleError >
Creates an [EntryHandle] for direct read access to the value.
Definition reader.hpp:105
The system-wide unique id of a [Reader].
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
EntryHandleError
Defines a failure that can occur when a [EntryHandle] is created with [Reader::entry()].