From 73b3ea14767ddf80489a501b242bdfe6d7e763be Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 4 Nov 2023 13:40:43 -0400 Subject: [PATCH] cpp-common/bt2: add `bt2::SelfMessageIterator` This patch introduces `bt2::SelfMessageIterator` which wraps `bt_self_message_iterator`. This is the last piece of C++ binding needed to write a complete component class in C++, immediately wrapping the component class and message iterator class methods parameters. `bt2::SelfMessageIterator` offers: createMessageIterator(): Create a message iterator from this message iterator: auto msgIter = selfMsgIter.createMessageIterator( selfMsgIter.component().inputPorts()["in"]); component(): Borrow the parent self component. This is usually used to retrieve some component data: selfMsgIter.component().data(); port(): Borrow the output port from which this message iterator operates. isInterrupted(): Whether or not this message iterator is interrupted. data(): Get and set user data. Signed-off-by: Philippe Proulx Change-Id: I02747ffb7a31743a310bdeff120f1d1042bf11e6 Reviewed-on: https://review.lttng.org/c/babeltrace/+/11237 Reviewed-by: Simon Marchi Tested-by: jenkins CI-Build: Simon Marchi --- src/Makefile.am | 1 + src/cpp-common/bt2/self-message-iterator.hpp | 78 ++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/cpp-common/bt2/self-message-iterator.hpp diff --git a/src/Makefile.am b/src/Makefile.am index 5494041a..ec1a473d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -32,6 +32,7 @@ noinst_HEADERS = \ cpp-common/bt2/shared-object.hpp \ cpp-common/bt2/raw-value-proxy.hpp \ cpp-common/bt2/self-component-port.hpp \ + cpp-common/bt2/self-message-iterator.hpp \ cpp-common/bt2/trace-ir.hpp \ cpp-common/bt2/type-traits.hpp \ cpp-common/bt2/value.hpp \ diff --git a/src/cpp-common/bt2/self-message-iterator.hpp b/src/cpp-common/bt2/self-message-iterator.hpp new file mode 100644 index 00000000..5d296eea --- /dev/null +++ b/src/cpp-common/bt2/self-message-iterator.hpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP +#define BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP + +#include + +#include "common/assert.h" +#include "common/common.h" + +#include "borrowed-object.hpp" +#include "message-iterator.hpp" +#include "self-component-port.hpp" + +namespace bt2 { + +class SelfMessageIterator final : public BorrowedObject +{ +public: + explicit SelfMessageIterator(const _LibObjPtr libObjPtr) noexcept : + _ThisBorrowedObject {libObjPtr} + { + } + + MessageIterator::Shared createMessageIterator(const SelfComponentInputPort port) const + { + bt_message_iterator *libMsgIterPtr = nullptr; + const auto status = bt_message_iterator_create_from_message_iterator( + this->libObjPtr(), port.libObjPtr(), &libMsgIterPtr); + + switch (status) { + case BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK: + BT_ASSERT(libMsgIterPtr); + return MessageIterator::Shared::createWithoutRef(libMsgIterPtr); + case BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_MEMORY_ERROR: + throw MemoryError {}; + case BT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_ERROR: + throw Error {}; + default: + bt_common_abort(); + } + } + + SelfComponent component() const noexcept + { + return SelfComponent {bt_self_message_iterator_borrow_component(this->libObjPtr())}; + } + + SelfComponentOutputPort port() const noexcept + { + return SelfComponentOutputPort {bt_self_message_iterator_borrow_port(this->libObjPtr())}; + } + + bool isInterrupted() const noexcept + { + return static_cast(bt_self_message_iterator_is_interrupted(this->libObjPtr())); + } + + template + T& data() const noexcept + { + return *static_cast(bt_self_message_iterator_get_data(this->libObjPtr())); + } + + template + void data(T& obj) const noexcept + { + bt_self_message_iterator_set_data(this->libObjPtr(), static_cast(&obj)); + } +}; + +} /* namespace bt2 */ + +#endif /* BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP */ -- 2.34.1