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/port_name.hpp"
22#include "iox2/service_type.hpp"
24
25namespace iox2 {
27template <ServiceType S, typename KeyType>
28class Reader {
29 public:
30 Reader(Reader&& rhs) noexcept;
31 auto operator=(Reader&& rhs) noexcept -> Reader&;
32 ~Reader();
33
34 Reader(const Reader&) = delete;
35 auto operator=(const Reader&) -> Reader& = delete;
36
38 auto id() const -> UniqueReaderId;
39
41 auto name() const -> PortNameView;
42
44 template <typename ValueType>
45 auto entry(const KeyType& key) -> bb::Expected<EntryHandle<S, KeyType, ValueType>, EntryHandleError>;
46
47 private:
48 template <ServiceType, typename>
49 friend class PortFactoryReader;
50
51 explicit Reader(iox2_reader_h handle);
52 void drop();
53
54 iox2_reader_h m_handle = nullptr;
55};
56
57template <ServiceType S, typename KeyType>
58inline Reader<S, KeyType>::Reader(iox2_reader_h handle)
59 : m_handle { handle } {
60}
61
62template <ServiceType S, typename KeyType>
63inline void Reader<S, KeyType>::drop() {
64 if (m_handle != nullptr) {
65// NOTE: false positive; m_handle is initialized in the class itself
66#if (defined(__GNUC__) && __GNUC__ == 13 && !defined(__clang__))
67#pragma GCC diagnostic push
68#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
69#endif
70 iox2_reader_drop(m_handle);
71#if (defined(__GNUC__) && __GNUC__ == 13 && !defined(__clang__))
72#pragma GCC diagnostic pop
73#endif
74 m_handle = nullptr;
75 }
76}
77
78template <ServiceType S, typename KeyType>
79inline Reader<S, KeyType>::Reader(Reader&& rhs) noexcept {
80 *this = std::move(rhs);
81}
82
83template <ServiceType S, typename KeyType>
84inline auto Reader<S, KeyType>::operator=(Reader&& rhs) noexcept -> Reader& {
85 if (this != &rhs) {
86 drop();
87 m_handle = rhs.m_handle;
88 rhs.m_handle = nullptr;
89 }
90
91 return *this;
92}
93
94template <ServiceType S, typename KeyType>
96 drop();
97}
98
99template <ServiceType S, typename KeyType>
101 iox2_unique_reader_id_h id_handle = nullptr;
102
103 iox2_reader_id(&m_handle, nullptr, &id_handle);
104 return UniqueReaderId { id_handle };
105}
106
107template <ServiceType S, typename KeyType>
109 const auto* port_name_ptr = iox2_reader_name(&m_handle);
110 return PortNameView::from_native_ptr(port_name_ptr);
111}
112
113template <ServiceType S, typename KeyType>
114template <typename ValueType>
115inline auto Reader<S, KeyType>::entry(const KeyType& key)
117 iox2_entry_handle_h entry_handle {};
118 const auto type_name = internal::get_type_name<ValueType>();
119
120 auto result = iox2_reader_entry(&m_handle,
121 nullptr,
122 &entry_handle,
123 &key,
124 type_name.unchecked_access().c_str(),
125 type_name.size(),
126 sizeof(ValueType),
127 alignof(ValueType));
128
129 if (result == IOX2_OK) {
130 return EntryHandle<S, KeyType, ValueType>(entry_handle);
131 }
132
133 return bb::err(bb::into<EntryHandleError>(result));
134}
135} // namespace iox2
136
137#endif
A handle for direct read access to a specific blackboard value.
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].
Reading endpoint of a blackboard based communication.
Definition reader.hpp:28
auto id() const -> UniqueReaderId
Returns the [UniqueReaderId] of the [Reader].
Definition reader.hpp:100
auto name() const -> PortNameView
Returns the [PortNameView] of the [Reader].
Definition reader.hpp:108
auto operator=(const Reader &) -> Reader &=delete
auto operator=(Reader &&rhs) noexcept -> Reader &
Definition reader.hpp:84
Reader(const Reader &)=delete
Reader(Reader &&rhs) noexcept
Definition reader.hpp:79
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:115
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()].