2 * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_MESSAGE_ITERATOR_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_MESSAGE_ITERATOR_HPP
10 #include <babeltrace2/babeltrace.h>
12 #include "common/common.h"
13 #include "cpp-common/bt2s/optional.hpp"
15 #include "component-port.hpp"
17 #include "message-array.hpp"
18 #include "shared-object.hpp"
23 struct MessageIteratorRefFuncs final
25 static void get(const bt_message_iterator * const libObjPtr) noexcept
27 bt_message_iterator_get_ref(libObjPtr);
30 static void put(const bt_message_iterator * const libObjPtr) noexcept
32 bt_message_iterator_put_ref(libObjPtr);
36 } /* namespace internal */
38 class MessageIterator final : public BorrowedObject<bt_message_iterator>
42 SharedObject<MessageIterator, bt_message_iterator, internal::MessageIteratorRefFuncs>;
44 explicit MessageIterator(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
48 ConstComponent component() const noexcept
50 return ConstComponent {bt_message_iterator_borrow_component(this->libObjPtr())};
53 bt2s::optional<ConstMessageArray> next() const
55 bt_message_array_const libMsgsPtr;
57 const auto status = bt_message_iterator_next(this->libObjPtr(), &libMsgsPtr, &count);
60 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
61 /* Caller becomes the owner of the contained messages */
62 return ConstMessageArray::wrapExisting(libMsgsPtr, count);
63 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
65 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
67 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
69 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
76 bool canSeekBeginning() const
80 const auto status = bt_message_iterator_can_seek_beginning(this->libObjPtr(), &canSeek);
83 case BT_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_STATUS_OK:
84 return static_cast<bool>(canSeek);
85 case BT_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_STATUS_AGAIN:
87 case BT_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_STATUS_MEMORY_ERROR:
89 case BT_MESSAGE_ITERATOR_CAN_SEEK_BEGINNING_STATUS_ERROR:
96 void seekBeginning() const
98 const auto status = bt_message_iterator_seek_beginning(this->libObjPtr());
101 case BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK:
103 case BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_AGAIN:
105 case BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_MEMORY_ERROR:
106 throw MemoryError {};
107 case BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_ERROR:
114 bool canSeekNsFromOrigin(const std::int64_t nsFromOrigin) const
119 bt_message_iterator_can_seek_ns_from_origin(this->libObjPtr(), nsFromOrigin, &canSeek);
122 case BT_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_STATUS_OK:
123 return static_cast<bool>(canSeek);
124 case BT_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_STATUS_AGAIN:
126 case BT_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_STATUS_MEMORY_ERROR:
127 throw MemoryError {};
128 case BT_MESSAGE_ITERATOR_CAN_SEEK_NS_FROM_ORIGIN_STATUS_ERROR:
135 void seekNsFromOrigin(const std::int64_t nsFromOrigin) const
138 bt_message_iterator_seek_ns_from_origin(this->libObjPtr(), nsFromOrigin);
141 case BT_MESSAGE_ITERATOR_SEEK_NS_FROM_ORIGIN_STATUS_OK:
143 case BT_MESSAGE_ITERATOR_SEEK_NS_FROM_ORIGIN_STATUS_AGAIN:
145 case BT_MESSAGE_ITERATOR_SEEK_NS_FROM_ORIGIN_STATUS_MEMORY_ERROR:
146 throw MemoryError {};
147 case BT_MESSAGE_ITERATOR_SEEK_NS_FROM_ORIGIN_STATUS_ERROR:
154 bool canSeekForward() const noexcept
156 return static_cast<bool>(bt_message_iterator_can_seek_forward(this->libObjPtr()));
159 Shared shared() const noexcept
161 return Shared::createWithRef(*this);
165 } /* namespace bt2 */
167 #endif /* BABELTRACE_CPP_COMMON_BT2_MESSAGE_ITERATOR_HPP */