cpp-common/bt2: make setters return `*this`
[babeltrace.git] / src / cpp-common / bt2 / self-message-iterator.hpp
CommitLineData
73b3ea14
PP
1/*
2 * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com>
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7#ifndef BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP
8#define BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP
9
10#include <babeltrace2/babeltrace.h>
11
73b3ea14
PP
12#include "common/common.h"
13
14#include "borrowed-object.hpp"
15#include "message-iterator.hpp"
16#include "self-component-port.hpp"
17
18namespace bt2 {
19
20class SelfMessageIterator final : public BorrowedObject<bt_self_message_iterator>
21{
22public:
d246c457 23 explicit SelfMessageIterator(const LibObjPtr libObjPtr) noexcept :
73b3ea14
PP
24 _ThisBorrowedObject {libObjPtr}
25 {
26 }
27
28 MessageIterator::Shared createMessageIterator(const SelfComponentInputPort port) const
29 {
30 bt_message_iterator *libMsgIterPtr = nullptr;
31 const auto status = bt_message_iterator_create_from_message_iterator(
32 this->libObjPtr(), port.libObjPtr(), &libMsgIterPtr);
33
34 switch (status) {
35 case BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK:
73b3ea14
PP
36 return MessageIterator::Shared::createWithoutRef(libMsgIterPtr);
37 case BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_MEMORY_ERROR:
38 throw MemoryError {};
39 case BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_ERROR:
40 throw Error {};
41 default:
42 bt_common_abort();
43 }
44 }
45
46 SelfComponent component() const noexcept
47 {
48 return SelfComponent {bt_self_message_iterator_borrow_component(this->libObjPtr())};
49 }
50
51 SelfComponentOutputPort port() const noexcept
52 {
53 return SelfComponentOutputPort {bt_self_message_iterator_borrow_port(this->libObjPtr())};
54 }
55
56 bool isInterrupted() const noexcept
57 {
58 return static_cast<bool>(bt_self_message_iterator_is_interrupted(this->libObjPtr()));
59 }
60
61 template <typename T>
62 T& data() const noexcept
63 {
64 return *static_cast<T *>(bt_self_message_iterator_get_data(this->libObjPtr()));
65 }
66
67 template <typename T>
2a24eba8 68 SelfMessageIterator data(T& obj) const noexcept
73b3ea14 69 {
1e700b7d
SM
70 bt_self_message_iterator_set_data(this->libObjPtr(),
71 const_cast<void *>(static_cast<const void *>(&obj)));
2a24eba8 72 return *this;
73b3ea14 73 }
5003e61f
SM
74
75 bt2::StreamBeginningMessage::Shared
76 createStreamBeginningMessage(const bt2::ConstStream stream) const
77 {
78 const auto libObjPtr =
79 bt_message_stream_beginning_create(this->libObjPtr(), stream.libObjPtr());
80
81 if (!libObjPtr) {
82 throw MemoryError {};
83 }
84
85 return bt2::StreamBeginningMessage::Shared::createWithoutRef(libObjPtr);
86 }
87
88 bt2::StreamEndMessage::Shared createStreamEndMessage(const bt2::ConstStream stream) const
89 {
90 const auto libObjPtr = bt_message_stream_end_create(this->libObjPtr(), stream.libObjPtr());
91
92 if (!libObjPtr) {
93 throw MemoryError {};
94 }
95
96 return bt2::StreamEndMessage::Shared::createWithoutRef(libObjPtr);
97 }
98
99 bt2::EventMessage::Shared createEventMessage(const bt2::ConstEventClass eventCls,
100 const bt2::ConstStream stream) const
101 {
102 const auto libObjPtr =
103 bt_message_event_create(this->libObjPtr(), eventCls.libObjPtr(), stream.libObjPtr());
104
105 if (!libObjPtr) {
106 throw MemoryError {};
107 }
108
109 return bt2::EventMessage::Shared::createWithoutRef(libObjPtr);
110 }
111
112 bt2::EventMessage::Shared createEventMessage(const bt2::ConstEventClass eventCls,
113 const bt2::ConstStream stream,
114 const std::uint64_t clockSnapshotValue) const
115 {
116 const auto libObjPtr = bt_message_event_create_with_default_clock_snapshot(
117 this->libObjPtr(), eventCls.libObjPtr(), stream.libObjPtr(), clockSnapshotValue);
118
119 if (!libObjPtr) {
120 throw MemoryError {};
121 }
122
123 return bt2::EventMessage::Shared::createWithoutRef(libObjPtr);
124 }
125
126 bt2::EventMessage::Shared createEventMessage(const bt2::ConstEventClass eventCls,
127 const bt2::ConstPacket packet) const
128 {
129 const auto libObjPtr = bt_message_event_create_with_packet(
130 this->libObjPtr(), eventCls.libObjPtr(), packet.libObjPtr());
131
132 if (!libObjPtr) {
133 throw MemoryError {};
134 }
135
136 return bt2::EventMessage::Shared::createWithoutRef(libObjPtr);
137 }
138
139 bt2::EventMessage::Shared createEventMessage(const bt2::ConstEventClass eventCls,
140 const bt2::ConstPacket packet,
141 const std::uint64_t clockSnapshotValue) const
142 {
143 const auto libObjPtr = bt_message_event_create_with_packet_and_default_clock_snapshot(
144 this->libObjPtr(), eventCls.libObjPtr(), packet.libObjPtr(), clockSnapshotValue);
145
146 if (!libObjPtr) {
147 throw MemoryError {};
148 }
149
150 return bt2::EventMessage::Shared::createWithoutRef(libObjPtr);
151 }
152
153 bt2::PacketBeginningMessage::Shared
154 createPacketBeginningMessage(const bt2::ConstPacket packet) const
155 {
156 const auto libObjPtr =
157 bt_message_packet_beginning_create(this->libObjPtr(), packet.libObjPtr());
158
159 if (!libObjPtr) {
160 throw MemoryError {};
161 }
162
163 return bt2::PacketBeginningMessage::Shared::createWithoutRef(libObjPtr);
164 }
165
166 bt2::PacketBeginningMessage::Shared
167 createPacketBeginningMessage(const bt2::ConstPacket packet,
168 const std::uint64_t clockSnapshotValue) const
169 {
170 const auto libObjPtr = bt_message_packet_beginning_create_with_default_clock_snapshot(
171 this->libObjPtr(), packet.libObjPtr(), clockSnapshotValue);
172
173 if (!libObjPtr) {
174 throw MemoryError {};
175 }
176
177 return bt2::PacketBeginningMessage::Shared::createWithoutRef(libObjPtr);
178 }
179
180 bt2::PacketEndMessage::Shared createPacketEndMessage(const bt2::ConstPacket packet) const
181 {
182 const auto libObjPtr = bt_message_packet_end_create(this->libObjPtr(), packet.libObjPtr());
183
184 if (!libObjPtr) {
185 throw MemoryError {};
186 }
187
188 return bt2::PacketEndMessage::Shared::createWithoutRef(libObjPtr);
189 }
190
191 bt2::PacketEndMessage::Shared
192 createPacketEndMessage(const bt2::ConstPacket packet,
193 const std::uint64_t clockSnapshotValue) const
194 {
195 const auto libObjPtr = bt_message_packet_end_create_with_default_clock_snapshot(
196 this->libObjPtr(), packet.libObjPtr(), clockSnapshotValue);
197
198 if (!libObjPtr) {
199 throw MemoryError {};
200 }
201
202 return bt2::PacketEndMessage::Shared::createWithoutRef(libObjPtr);
203 }
204
205 bt2::DiscardedEventsMessage::Shared createDiscardedEventsMessage(const bt2::ConstStream stream)
206 {
207 const auto libObjPtr =
208 bt_message_discarded_events_create(this->libObjPtr(), stream.libObjPtr());
209
210 if (!libObjPtr) {
211 throw MemoryError {};
212 }
213
214 return bt2::DiscardedEventsMessage::Shared::createWithoutRef(libObjPtr);
215 }
216
217 bt2::DiscardedEventsMessage::Shared
218 createDiscardedEventsMessage(const bt2::ConstStream stream,
219 const std::uint64_t beginningClockSnapshotValue,
d2ba27dd 220 const std::uint64_t endClockSnapshotValue) const
5003e61f
SM
221 {
222 const auto libObjPtr = bt_message_discarded_events_create_with_default_clock_snapshots(
223 this->libObjPtr(), stream.libObjPtr(), beginningClockSnapshotValue,
224 endClockSnapshotValue);
225
226 if (!libObjPtr) {
227 throw MemoryError {};
228 }
229
230 return bt2::DiscardedEventsMessage::Shared::createWithoutRef(libObjPtr);
231 }
232
233 bt2::DiscardedPacketsMessage::Shared
d2ba27dd 234 createDiscardedPacketsMessage(const bt2::ConstStream stream) const
5003e61f
SM
235 {
236 const auto libObjPtr =
237 bt_message_discarded_packets_create(this->libObjPtr(), stream.libObjPtr());
238
239 if (!libObjPtr) {
240 throw MemoryError {};
241 }
242
243 return bt2::DiscardedPacketsMessage::Shared::createWithoutRef(libObjPtr);
244 }
245
246 bt2::DiscardedPacketsMessage::Shared
247 createDiscardedPacketsMessage(const bt2::ConstStream stream,
248 const std::uint64_t beginningClockSnapshotValue,
d2ba27dd 249 const std::uint64_t endClockSnapshotValue) const
5003e61f
SM
250 {
251 const auto libObjPtr = bt_message_discarded_packets_create_with_default_clock_snapshots(
252 this->libObjPtr(), stream.libObjPtr(), beginningClockSnapshotValue,
253 endClockSnapshotValue);
254
255 if (!libObjPtr) {
256 throw MemoryError {};
257 }
258
259 return bt2::DiscardedPacketsMessage::Shared::createWithoutRef(libObjPtr);
260 }
261
262 bt2::MessageIteratorInactivityMessage::Shared
263 createMessageIteratorInactivityMessage(const bt2::ConstClockClass clockClass,
d2ba27dd 264 const std::uint64_t clockSnapshotValue) const
5003e61f
SM
265 {
266 const auto libObjPtr = bt_message_message_iterator_inactivity_create(
267 this->libObjPtr(), clockClass.libObjPtr(), clockSnapshotValue);
268
269 if (!libObjPtr) {
270 throw MemoryError {};
271 }
272
273 return bt2::MessageIteratorInactivityMessage::Shared::createWithoutRef(libObjPtr);
274 }
73b3ea14
PP
275};
276
277} /* namespace bt2 */
278
279#endif /* BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP */
This page took 0.039258 seconds and 4 git commands to generate.