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/port_name.hpp"
21#include "iox2/service_type.hpp"
23
24namespace iox2 {
26template <ServiceType S, typename KeyType>
27class Writer {
28 public:
29 Writer(Writer&& rhs) noexcept;
30 auto operator=(Writer&& rhs) noexcept -> Writer&;
31 ~Writer();
32
33 Writer(const Writer&) = delete;
34 auto operator=(const Writer&) -> Writer& = delete;
35
37 auto id() const -> UniqueWriterId;
38
40 auto name() const -> PortNameView;
41
44 template <typename ValueType>
45 auto entry(const KeyType& key) -> bb::Expected<EntryHandleMut<S, KeyType, ValueType>, EntryHandleMutError>;
46
47 private:
48 template <ServiceType, typename>
49 friend class PortFactoryWriter;
50
51 explicit Writer(iox2_writer_h handle);
52 void drop();
53
54 iox2_writer_h m_handle = nullptr;
55};
56
57template <ServiceType S, typename KeyType>
58inline Writer<S, KeyType>::Writer(iox2_writer_h handle)
59 : m_handle { handle } {
60}
61
62template <ServiceType S, typename KeyType>
63inline void Writer<S, KeyType>::drop() {
64 if (m_handle != nullptr) {
65 iox2_writer_drop(m_handle);
66 m_handle = nullptr;
67 }
68}
69
70template <ServiceType S, typename KeyType>
71inline Writer<S, KeyType>::Writer(Writer&& rhs) noexcept {
72 *this = std::move(rhs);
73}
74
75template <ServiceType S, typename KeyType>
76inline auto Writer<S, KeyType>::operator=(Writer&& rhs) noexcept -> Writer& {
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 KeyType>
88 drop();
89}
90
91template <ServiceType S, typename KeyType>
92inline auto Writer<S, KeyType>::id() const -> UniqueWriterId {
93 iox2_unique_writer_id_h id_handle = nullptr;
94
95 iox2_writer_id(&m_handle, nullptr, &id_handle);
96 return UniqueWriterId { id_handle };
97}
98
99template <ServiceType S, typename KeyType>
101 const auto* port_name_ptr = iox2_writer_name(&m_handle);
102 return PortNameView::from_native_ptr(port_name_ptr);
103}
104
105template <ServiceType S, typename KeyType>
106template <typename ValueType>
107inline auto Writer<S, KeyType>::entry(const KeyType& key)
109 iox2_entry_handle_mut_h entry_handle {};
110 const auto type_name = internal::get_type_name<ValueType>();
111
112 auto result = iox2_writer_entry(&m_handle,
113 nullptr,
114 &entry_handle,
115 &key,
116 type_name.unchecked_access().c_str(),
117 type_name.size(),
118 sizeof(ValueType),
119 alignof(ValueType));
120
121 if (result == IOX2_OK) {
122 return EntryHandleMut<S, KeyType, ValueType>(entry_handle);
123 }
124
125 return bb::err(bb::into<EntryHandleMutError>(result));
126}
127} // namespace iox2
128
129#endif
A handle for direct write 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].
The system-wide unique id of a [Writer].
Producing endpoint of a blackboard based communication.
Definition writer.hpp:27
Writer(Writer &&rhs) noexcept
Definition writer.hpp:71
auto id() const -> UniqueWriterId
Returns the [UniqueWriterId] of the [Writer].
Definition writer.hpp:92
auto entry(const KeyType &key) -> bb::Expected< EntryHandleMut< S, KeyType, ValueType >, EntryHandleMutError >
Definition writer.hpp:107
auto operator=(const Writer &) -> Writer &=delete
auto operator=(Writer &&rhs) noexcept -> Writer &
Definition writer.hpp:76
Writer(const Writer &)=delete
auto name() const -> PortNameView
Returns the [PortNameView] of the [Writer].
Definition writer.hpp:100
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()].