cpp-common/bt2: add message creation methods to `UserMessageIterator`
[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>
68 void data(T& obj) const noexcept
69 {
1e700b7d
SM
70 bt_self_message_iterator_set_data(this->libObjPtr(),
71 const_cast<void *>(static_cast<const void *>(&obj)));
73b3ea14 72 }
5003e61f
SM
73
74 bt2::StreamBeginningMessage::Shared
75 createStreamBeginningMessage(const bt2::ConstStream stream) const
76 {
77 const auto libObjPtr =
78 bt_message_stream_beginning_create(this->libObjPtr(), stream.libObjPtr());
79
80 if (!libObjPtr) {
81 throw MemoryError {};
82 }
83
84 return bt2::StreamBeginningMessage::Shared::createWithoutRef(libObjPtr);
85 }
86
87 bt2::StreamEndMessage::Shared createStreamEndMessage(const bt2::ConstStream stream) const
88 {
89 const auto libObjPtr = bt_message_stream_end_create(this->libObjPtr(), stream.libObjPtr());
90
91 if (!libObjPtr) {
92 throw MemoryError {};
93 }
94
95 return bt2::StreamEndMessage::Shared::createWithoutRef(libObjPtr);
96 }
97
98 bt2::EventMessage::Shared createEventMessage(const bt2::ConstEventClass eventCls,
99 const bt2::ConstStream stream) const
100 {
101 const auto libObjPtr =
102 bt_message_event_create(this->libObjPtr(), eventCls.libObjPtr(), stream.libObjPtr());
103
104 if (!libObjPtr) {
105 throw MemoryError {};
106 }
107
108 return bt2::EventMessage::Shared::createWithoutRef(libObjPtr);
109 }
110
111 bt2::EventMessage::Shared createEventMessage(const bt2::ConstEventClass eventCls,
112 const bt2::ConstStream stream,
113 const std::uint64_t clockSnapshotValue) const
114 {
115 const auto libObjPtr = bt_message_event_create_with_default_clock_snapshot(
116 this->libObjPtr(), eventCls.libObjPtr(), stream.libObjPtr(), clockSnapshotValue);
117
118 if (!libObjPtr) {
119 throw MemoryError {};
120 }
121
122 return bt2::EventMessage::Shared::createWithoutRef(libObjPtr);
123 }
124
125 bt2::EventMessage::Shared createEventMessage(const bt2::ConstEventClass eventCls,
126 const bt2::ConstPacket packet) const
127 {
128 const auto libObjPtr = bt_message_event_create_with_packet(
129 this->libObjPtr(), eventCls.libObjPtr(), packet.libObjPtr());
130
131 if (!libObjPtr) {
132 throw MemoryError {};
133 }
134
135 return bt2::EventMessage::Shared::createWithoutRef(libObjPtr);
136 }
137
138 bt2::EventMessage::Shared createEventMessage(const bt2::ConstEventClass eventCls,
139 const bt2::ConstPacket packet,
140 const std::uint64_t clockSnapshotValue) const
141 {
142 const auto libObjPtr = bt_message_event_create_with_packet_and_default_clock_snapshot(
143 this->libObjPtr(), eventCls.libObjPtr(), packet.libObjPtr(), clockSnapshotValue);
144
145 if (!libObjPtr) {
146 throw MemoryError {};
147 }
148
149 return bt2::EventMessage::Shared::createWithoutRef(libObjPtr);
150 }
151
152 bt2::PacketBeginningMessage::Shared
153 createPacketBeginningMessage(const bt2::ConstPacket packet) const
154 {
155 const auto libObjPtr =
156 bt_message_packet_beginning_create(this->libObjPtr(), packet.libObjPtr());
157
158 if (!libObjPtr) {
159 throw MemoryError {};
160 }
161
162 return bt2::PacketBeginningMessage::Shared::createWithoutRef(libObjPtr);
163 }
164
165 bt2::PacketBeginningMessage::Shared
166 createPacketBeginningMessage(const bt2::ConstPacket packet,
167 const std::uint64_t clockSnapshotValue) const
168 {
169 const auto libObjPtr = bt_message_packet_beginning_create_with_default_clock_snapshot(
170 this->libObjPtr(), packet.libObjPtr(), clockSnapshotValue);
171
172 if (!libObjPtr) {
173 throw MemoryError {};
174 }
175
176 return bt2::PacketBeginningMessage::Shared::createWithoutRef(libObjPtr);
177 }
178
179 bt2::PacketEndMessage::Shared createPacketEndMessage(const bt2::ConstPacket packet) const
180 {
181 const auto libObjPtr = bt_message_packet_end_create(this->libObjPtr(), packet.libObjPtr());
182
183 if (!libObjPtr) {
184 throw MemoryError {};
185 }
186
187 return bt2::PacketEndMessage::Shared::createWithoutRef(libObjPtr);
188 }
189
190 bt2::PacketEndMessage::Shared
191 createPacketEndMessage(const bt2::ConstPacket packet,
192 const std::uint64_t clockSnapshotValue) const
193 {
194 const auto libObjPtr = bt_message_packet_end_create_with_default_clock_snapshot(
195 this->libObjPtr(), packet.libObjPtr(), clockSnapshotValue);
196
197 if (!libObjPtr) {
198 throw MemoryError {};
199 }
200
201 return bt2::PacketEndMessage::Shared::createWithoutRef(libObjPtr);
202 }
203
204 bt2::DiscardedEventsMessage::Shared createDiscardedEventsMessage(const bt2::ConstStream stream)
205 {
206 const auto libObjPtr =
207 bt_message_discarded_events_create(this->libObjPtr(), stream.libObjPtr());
208
209 if (!libObjPtr) {
210 throw MemoryError {};
211 }
212
213 return bt2::DiscardedEventsMessage::Shared::createWithoutRef(libObjPtr);
214 }
215
216 bt2::DiscardedEventsMessage::Shared
217 createDiscardedEventsMessage(const bt2::ConstStream stream,
218 const std::uint64_t beginningClockSnapshotValue,
d2ba27dd 219 const std::uint64_t endClockSnapshotValue) const
5003e61f
SM
220 {
221 const auto libObjPtr = bt_message_discarded_events_create_with_default_clock_snapshots(
222 this->libObjPtr(), stream.libObjPtr(), beginningClockSnapshotValue,
223 endClockSnapshotValue);
224
225 if (!libObjPtr) {
226 throw MemoryError {};
227 }
228
229 return bt2::DiscardedEventsMessage::Shared::createWithoutRef(libObjPtr);
230 }
231
232 bt2::DiscardedPacketsMessage::Shared
d2ba27dd 233 createDiscardedPacketsMessage(const bt2::ConstStream stream) const
5003e61f
SM
234 {
235 const auto libObjPtr =
236 bt_message_discarded_packets_create(this->libObjPtr(), stream.libObjPtr());
237
238 if (!libObjPtr) {
239 throw MemoryError {};
240 }
241
242 return bt2::DiscardedPacketsMessage::Shared::createWithoutRef(libObjPtr);
243 }
244
245 bt2::DiscardedPacketsMessage::Shared
246 createDiscardedPacketsMessage(const bt2::ConstStream stream,
247 const std::uint64_t beginningClockSnapshotValue,
d2ba27dd 248 const std::uint64_t endClockSnapshotValue) const
5003e61f
SM
249 {
250 const auto libObjPtr = bt_message_discarded_packets_create_with_default_clock_snapshots(
251 this->libObjPtr(), stream.libObjPtr(), beginningClockSnapshotValue,
252 endClockSnapshotValue);
253
254 if (!libObjPtr) {
255 throw MemoryError {};
256 }
257
258 return bt2::DiscardedPacketsMessage::Shared::createWithoutRef(libObjPtr);
259 }
260
261 bt2::MessageIteratorInactivityMessage::Shared
262 createMessageIteratorInactivityMessage(const bt2::ConstClockClass clockClass,
d2ba27dd 263 const std::uint64_t clockSnapshotValue) const
5003e61f
SM
264 {
265 const auto libObjPtr = bt_message_message_iterator_inactivity_create(
266 this->libObjPtr(), clockClass.libObjPtr(), clockSnapshotValue);
267
268 if (!libObjPtr) {
269 throw MemoryError {};
270 }
271
272 return bt2::MessageIteratorInactivityMessage::Shared::createWithoutRef(libObjPtr);
273 }
73b3ea14
PP
274};
275
276} /* namespace bt2 */
277
278#endif /* BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP */
This page took 0.038046 seconds and 4 git commands to generate.