| 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 | |
| 76 | } /* namespace bt2 */ |
| 77 | |
| 78 | #endif /* BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP */ |