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