cpp-common/bt2: add `bt2::SelfMessageIterator`
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 4 Nov 2023 17:40:43 +0000 (13:40 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
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<MyComponentData>();

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 <eeppeliteloop@gmail.com>
Change-Id: I02747ffb7a31743a310bdeff120f1d1042bf11e6
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11237
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

src/Makefile.am
src/cpp-common/bt2/self-message-iterator.hpp [new file with mode: 0644]

index 5494041a703779830685bc2381c4517ab1fa1715..ec1a473d675289478398c7d2028011e60318a78b 100644 (file)
@@ -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 (file)
index 0000000..5d296ee
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP
+#define BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP
+
+#include <babeltrace2/babeltrace.h>
+
+#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<bt_self_message_iterator>
+{
+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<bool>(bt_self_message_iterator_is_interrupted(this->libObjPtr()));
+    }
+
+    template <typename T>
+    T& data() const noexcept
+    {
+        return *static_cast<T *>(bt_self_message_iterator_get_data(this->libObjPtr()));
+    }
+
+    template <typename T>
+    void data(T& obj) const noexcept
+    {
+        bt_self_message_iterator_set_data(this->libObjPtr(), static_cast<void *>(&obj));
+    }
+};
+
+} /* namespace bt2 */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2_SELF_MESSAGE_ITERATOR_HPP */
This page took 0.026246 seconds and 4 git commands to generate.