cpp-common/bt2: add C++ query executor bindings
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 14 Feb 2024 16:12:07 +0000 (11:12 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Mon, 19 Feb 2024 18:10:15 +0000 (13:10 -0500)
Add C++ bindings for query executors.

Create a query executor with the `bt2::QueryExecutor::create()` static
methods (with or without query data).

Perform a query with the `bt2::QueryExecutor::query()` method.

Change-Id: I1db634dfd423b031be7577291d4de02b7fb0df5b
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11808
Tested-by: jenkins <jenkins@lttng.org>
src/Makefile.am
src/cpp-common/bt2/query-executor.hpp [new file with mode: 0644]

index 01ae18b5f64040cfe233bc53747314f114b58972..f9b2b3c3414eff5e64b94d46ce91b9d795bff556 100644 (file)
@@ -137,6 +137,7 @@ cpp_common_libcpp_common_la_SOURCES = \
        cpp-common/bt2/plugin-dev.hpp \
        cpp-common/bt2/plugin.hpp \
        cpp-common/bt2/private-query-executor.hpp \
+       cpp-common/bt2/query-executor.hpp \
        cpp-common/bt2/raw-value-proxy.hpp \
        cpp-common/bt2/self-component-class.hpp \
        cpp-common/bt2/self-component-port.hpp \
diff --git a/src/cpp-common/bt2/query-executor.hpp b/src/cpp-common/bt2/query-executor.hpp
new file mode 100644 (file)
index 0000000..4b3fb26
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2024 EfficiOS, Inc.
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP
+#define BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP
+
+#include <babeltrace2/babeltrace.h>
+
+#include "cpp-common/bt2c/c-string-view.hpp"
+
+#include "borrowed-object.hpp"
+#include "component-class.hpp"
+#include "shared-object.hpp"
+#include "value.hpp"
+
+namespace bt2 {
+namespace internal {
+
+struct QueryExecutorRefFuncs final
+{
+    static void get(const bt_query_executor * const libObjPtr) noexcept
+    {
+        bt_query_executor_get_ref(libObjPtr);
+    }
+
+    static void put(const bt_query_executor * const libObjPtr) noexcept
+    {
+        bt_query_executor_put_ref(libObjPtr);
+    }
+};
+
+} /* namespace internal */
+
+template <typename LibObjT>
+class CommonQueryExecutor final : public BorrowedObject<LibObjT>
+{
+private:
+    using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
+
+public:
+    using typename _ThisBorrowedObject::LibObjPtr;
+    using Shared = SharedObject<CommonQueryExecutor, LibObjT, internal::QueryExecutorRefFuncs>;
+
+    explicit CommonQueryExecutor(const LibObjPtr libObjPtr) noexcept :
+        _ThisBorrowedObject {libObjPtr}
+    {
+    }
+
+    template <typename OtherLibObjT>
+    CommonQueryExecutor(const CommonQueryExecutor<OtherLibObjT> queryExec) noexcept :
+        _ThisBorrowedObject {queryExec}
+    {
+    }
+
+    template <typename OtherLibObjT>
+    CommonQueryExecutor operator=(const CommonQueryExecutor<OtherLibObjT> queryExec) noexcept
+    {
+        _ThisBorrowedObject::operator=(queryExec);
+        return *this;
+    }
+
+    static Shared create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
+                         const OptionalBorrowedObject<ConstMapValue> params = {})
+    {
+        return CommonQueryExecutor::_create(compCls, objectName, params,
+                                            static_cast<void *>(nullptr));
+    }
+
+    template <typename QueryDataT>
+    static Shared create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
+                         QueryDataT& queryData,
+                         const OptionalBorrowedObject<ConstMapValue> params = {})
+    {
+        return CommonQueryExecutor::_create(compCls, objectName, params, &queryData);
+    }
+
+    ConstValue::Shared query() const
+    {
+        static_assert(!std::is_const<LibObjT>::value,
+                      "Not available with `bt2::ConstQueryExecutor`.");
+
+        const bt_value *res;
+        const auto status = bt_query_executor_query(this->libObjPtr(), &res);
+
+        if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR) {
+            throw Error {};
+        } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR) {
+            throw MemoryError {};
+        } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN) {
+            throw TryAgain {};
+        } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_UNKNOWN_OBJECT) {
+            throw UnknownObject {};
+        }
+
+        return ConstValue::Shared::createWithoutRef(res);
+    }
+
+private:
+    template <typename QueryDataT>
+    static Shared _create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
+                          const OptionalBorrowedObject<ConstMapValue> params,
+                          QueryDataT * const queryData)
+    {
+        const auto libObjPtr = bt_query_executor_create_with_method_data(
+            compCls.libObjPtr(), objectName, params ? params->libObjPtr() : nullptr,
+            const_cast<void *>(static_cast<const void *>(queryData)));
+
+        if (!libObjPtr) {
+            throw MemoryError {};
+        }
+
+        return Shared::createWithoutRef(libObjPtr);
+    }
+};
+
+using QueryExecutor = CommonQueryExecutor<bt_query_executor>;
+using ConstQueryExecutor = CommonQueryExecutor<const bt_query_executor>;
+
+} /* namespace bt2 */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP  */
This page took 0.026508 seconds and 4 git commands to generate.