iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
writer.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_WRITER_HPP
14#define IOX2_WRITER_HPP
15
16#include "iox2/bb/expected.hpp"
20#include "iox2/service_type.hpp"
22
23namespace iox2 {
25template <ServiceType S, typename KeyType>
26class Writer {
27 public:
28 Writer(Writer&& rhs) noexcept;
29 auto operator=(Writer&& rhs) noexcept -> Writer&;
30 ~Writer();
31
32 Writer(const Writer&) = delete;
33 auto operator=(const Writer&) -> Writer& = delete;
34
36 auto id() const -> UniqueWriterId;
37
40 template <typename ValueType>
41 auto entry(const KeyType& key) -> bb::Expected<EntryHandleMut<S, KeyType, ValueType>, EntryHandleMutError>;
42
43 private:
44 template <ServiceType, typename>
45 friend class PortFactoryWriter;
46
47 explicit Writer(iox2_writer_h handle);
48 void drop();
49
50 iox2_writer_h m_handle = nullptr;
51};
52
53template <ServiceType S, typename KeyType>
54inline Writer<S, KeyType>::Writer(iox2_writer_h handle)
55 : m_handle { handle } {
56}
57
58template <ServiceType S, typename KeyType>
59inline void Writer<S, KeyType>::drop() {
60 if (m_handle != nullptr) {
61 iox2_writer_drop(m_handle);
62 m_handle = nullptr;
63 }
64}
65
66template <ServiceType S, typename KeyType>
67inline Writer<S, KeyType>::Writer(Writer&& rhs) noexcept {
68 *this = std::move(rhs);
69}
70
71template <ServiceType S, typename KeyType>
72inline auto Writer<S, KeyType>::operator=(Writer&& rhs) noexcept -> Writer& {
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 KeyType>
84 drop();
85}
86
87template <ServiceType S, typename KeyType>
88inline auto Writer<S, KeyType>::id() const -> UniqueWriterId {
89 iox2_unique_writer_id_h id_handle = nullptr;
90
91 iox2_writer_id(&m_handle, nullptr, &id_handle);
92 return UniqueWriterId { id_handle };
93}
94
95template <ServiceType S, typename KeyType>
96template <typename ValueType>
97inline auto Writer<S, KeyType>::entry(const KeyType& key)
99 iox2_entry_handle_mut_h entry_handle {};
100 const auto type_name = internal::get_type_name<ValueType>();
101
102 auto result = iox2_writer_entry(&m_handle,
103 nullptr,
104 &entry_handle,
105 &key,
106 type_name.unchecked_access().c_str(),
107 type_name.size(),
108 sizeof(ValueType),
109 alignof(ValueType));
110
111 if (result == IOX2_OK) {
112 return EntryHandleMut<S, KeyType, ValueType>(entry_handle);
113 }
114
115 return bb::err(bb::into<EntryHandleMutError>(result));
116}
117} // namespace iox2
118
119#endif
A handle for direct write access to a specific blackboard value.
The system-wide unique id of a [Writer].
Producing endpoint of a blackboard based communication.
Definition writer.hpp:26
Writer(Writer &&rhs) noexcept
Definition writer.hpp:67
auto id() const -> UniqueWriterId
Returns the [UniqueWriterId] of the [Writer].
Definition writer.hpp:88
auto entry(const KeyType &key) -> bb::Expected< EntryHandleMut< S, KeyType, ValueType >, EntryHandleMutError >
Definition writer.hpp:97
auto operator=(const Writer &) -> Writer &=delete
auto operator=(Writer &&rhs) noexcept -> Writer &
Definition writer.hpp:72
Writer(const Writer &)=delete
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
iox2::bb::variation::Expected< T, E > Expected
Definition expected.hpp:22
EntryHandleMutError
Defines a failure that can occur when a [EntryHandleMut] is created with [Writer::entry()].