| 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/common.h" |
| 13 | |
| 14 | #include "borrowed-object.hpp" |
| 15 | #include "message-iterator.hpp" |
| 16 | #include "self-component-port.hpp" |
| 17 | |
| 18 | namespace bt2 { |
| 19 | |
| 20 | class SelfMessageIterator final : public BorrowedObject<bt_self_message_iterator> |
| 21 | { |
| 22 | public: |
| 23 | explicit SelfMessageIterator(const LibObjPtr libObjPtr) noexcept : |
| 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: |
| 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 | SelfMessageIterator data(T& obj) const noexcept |
| 69 | { |
| 70 | bt_self_message_iterator_set_data(this->libObjPtr(), |
| 71 | const_cast<void *>(static_cast<const void *>(&obj))); |
| 72 | return *this; |
| 73 | } |
| 74 | |
| 75 | StreamBeginningMessage::Shared createStreamBeginningMessage(const 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 StreamBeginningMessage::Shared::createWithoutRef(libObjPtr); |
| 85 | } |
| 86 | |
| 87 | StreamEndMessage::Shared createStreamEndMessage(const 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 StreamEndMessage::Shared::createWithoutRef(libObjPtr); |
| 96 | } |
| 97 | |
| 98 | EventMessage::Shared createEventMessage(const ConstEventClass eventCls, |
| 99 | const 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 EventMessage::Shared::createWithoutRef(libObjPtr); |
| 109 | } |
| 110 | |
| 111 | EventMessage::Shared createEventMessage(const ConstEventClass eventCls, |
| 112 | const 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 EventMessage::Shared::createWithoutRef(libObjPtr); |
| 123 | } |
| 124 | |
| 125 | EventMessage::Shared createEventMessage(const ConstEventClass eventCls, |
| 126 | const 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 EventMessage::Shared::createWithoutRef(libObjPtr); |
| 136 | } |
| 137 | |
| 138 | EventMessage::Shared createEventMessage(const ConstEventClass eventCls, |
| 139 | const 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 EventMessage::Shared::createWithoutRef(libObjPtr); |
| 150 | } |
| 151 | |
| 152 | PacketBeginningMessage::Shared createPacketBeginningMessage(const ConstPacket packet) const |
| 153 | { |
| 154 | const auto libObjPtr = |
| 155 | bt_message_packet_beginning_create(this->libObjPtr(), packet.libObjPtr()); |
| 156 | |
| 157 | if (!libObjPtr) { |
| 158 | throw MemoryError {}; |
| 159 | } |
| 160 | |
| 161 | return PacketBeginningMessage::Shared::createWithoutRef(libObjPtr); |
| 162 | } |
| 163 | |
| 164 | PacketBeginningMessage::Shared |
| 165 | createPacketBeginningMessage(const ConstPacket packet, |
| 166 | const std::uint64_t clockSnapshotValue) const |
| 167 | { |
| 168 | const auto libObjPtr = bt_message_packet_beginning_create_with_default_clock_snapshot( |
| 169 | this->libObjPtr(), packet.libObjPtr(), clockSnapshotValue); |
| 170 | |
| 171 | if (!libObjPtr) { |
| 172 | throw MemoryError {}; |
| 173 | } |
| 174 | |
| 175 | return PacketBeginningMessage::Shared::createWithoutRef(libObjPtr); |
| 176 | } |
| 177 | |
| 178 | PacketEndMessage::Shared createPacketEndMessage(const ConstPacket packet) const |
| 179 | { |
| 180 | const auto libObjPtr = bt_message_packet_end_create(this->libObjPtr(), packet.libObjPtr()); |
| 181 | |
| 182 | if (!libObjPtr) { |
| 183 | throw MemoryError {}; |
| 184 | } |
| 185 | |
| 186 | return PacketEndMessage::Shared::createWithoutRef(libObjPtr); |
| 187 | } |
| 188 | |
| 189 | PacketEndMessage::Shared createPacketEndMessage(const ConstPacket packet, |
| 190 | const std::uint64_t clockSnapshotValue) const |
| 191 | { |
| 192 | const auto libObjPtr = bt_message_packet_end_create_with_default_clock_snapshot( |
| 193 | this->libObjPtr(), packet.libObjPtr(), clockSnapshotValue); |
| 194 | |
| 195 | if (!libObjPtr) { |
| 196 | throw MemoryError {}; |
| 197 | } |
| 198 | |
| 199 | return PacketEndMessage::Shared::createWithoutRef(libObjPtr); |
| 200 | } |
| 201 | |
| 202 | DiscardedEventsMessage::Shared createDiscardedEventsMessage(const ConstStream stream) |
| 203 | { |
| 204 | const auto libObjPtr = |
| 205 | bt_message_discarded_events_create(this->libObjPtr(), stream.libObjPtr()); |
| 206 | |
| 207 | if (!libObjPtr) { |
| 208 | throw MemoryError {}; |
| 209 | } |
| 210 | |
| 211 | return DiscardedEventsMessage::Shared::createWithoutRef(libObjPtr); |
| 212 | } |
| 213 | |
| 214 | DiscardedEventsMessage::Shared |
| 215 | createDiscardedEventsMessage(const ConstStream stream, |
| 216 | const std::uint64_t beginningClockSnapshotValue, |
| 217 | const std::uint64_t endClockSnapshotValue) const |
| 218 | { |
| 219 | const auto libObjPtr = bt_message_discarded_events_create_with_default_clock_snapshots( |
| 220 | this->libObjPtr(), stream.libObjPtr(), beginningClockSnapshotValue, |
| 221 | endClockSnapshotValue); |
| 222 | |
| 223 | if (!libObjPtr) { |
| 224 | throw MemoryError {}; |
| 225 | } |
| 226 | |
| 227 | return DiscardedEventsMessage::Shared::createWithoutRef(libObjPtr); |
| 228 | } |
| 229 | |
| 230 | DiscardedPacketsMessage::Shared createDiscardedPacketsMessage(const ConstStream stream) const |
| 231 | { |
| 232 | const auto libObjPtr = |
| 233 | bt_message_discarded_packets_create(this->libObjPtr(), stream.libObjPtr()); |
| 234 | |
| 235 | if (!libObjPtr) { |
| 236 | throw MemoryError {}; |
| 237 | } |
| 238 | |
| 239 | return DiscardedPacketsMessage::Shared::createWithoutRef(libObjPtr); |
| 240 | } |
| 241 | |
| 242 | DiscardedPacketsMessage::Shared |
| 243 | createDiscardedPacketsMessage(const ConstStream stream, |
| 244 | const std::uint64_t beginningClockSnapshotValue, |
| 245 | const std::uint64_t endClockSnapshotValue) const |
| 246 | { |
| 247 | const auto libObjPtr = bt_message_discarded_packets_create_with_default_clock_snapshots( |
| 248 | this->libObjPtr(), stream.libObjPtr(), beginningClockSnapshotValue, |
| 249 | endClockSnapshotValue); |
| 250 | |
| 251 | if (!libObjPtr) { |
| 252 | throw MemoryError {}; |
| 253 | } |
| 254 | |
| 255 | return DiscardedPacketsMessage::Shared::createWithoutRef(libObjPtr); |
| 256 | } |
| 257 | |
| 258 | MessageIteratorInactivityMessage::Shared |
| 259 | createMessageIteratorInactivityMessage(const ConstClockClass clockClass, |
| 260 | const std::uint64_t clockSnapshotValue) const |
| 261 | { |
| 262 | const auto libObjPtr = bt_message_message_iterator_inactivity_create( |
| 263 | this->libObjPtr(), clockClass.libObjPtr(), clockSnapshotValue); |
| 264 | |
| 265 | if (!libObjPtr) { |
| 266 | throw MemoryError {}; |
| 267 | } |
| 268 | |
| 269 | return MessageIteratorInactivityMessage::Shared::createWithoutRef(libObjPtr); |
| 270 | } |
| 271 | }; |
| 272 | |
| 273 | } /* namespace bt2 */ |
| 274 | |
| 275 | #endif /* BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP */ |