cpp-common/bt2: add message creation methods to `SelfMessageIterator`
[babeltrace.git] / src / cpp-common / bt2 / self-message-iterator.hpp
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:
24 explicit SelfMessageIterator(const LibObjPtr libObjPtr) noexcept :
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 {
72 bt_self_message_iterator_set_data(this->libObjPtr(), static_cast<void *>(&obj));
73 }
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,
220 const std::uint64_t endClockSnapshotValue)
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
234 createDiscardedPacketsMessage(const bt2::ConstStream stream)
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,
249 const std::uint64_t endClockSnapshotValue)
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,
264 const std::uint64_t clockSnapshotValue)
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 }
275 };
276
277 } /* namespace bt2 */
278
279 #endif /* BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP */
This page took 0.034611 seconds and 4 git commands to generate.