From 230ec73afe8bf139f205562225932611d0e4e256 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 14 Feb 2024 11:12:07 -0500 Subject: [PATCH] cpp-common/bt2: add C++ query executor bindings 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 Signed-off-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/11808 Tested-by: jenkins --- src/Makefile.am | 1 + src/cpp-common/bt2/query-executor.hpp | 124 ++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 src/cpp-common/bt2/query-executor.hpp diff --git a/src/Makefile.am b/src/Makefile.am index 01ae18b5..f9b2b3c3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 index 00000000..4b3fb264 --- /dev/null +++ b/src/cpp-common/bt2/query-executor.hpp @@ -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 + +#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 +class CommonQueryExecutor final : public BorrowedObject +{ +private: + using typename BorrowedObject::_ThisBorrowedObject; + +public: + using typename _ThisBorrowedObject::LibObjPtr; + using Shared = SharedObject; + + explicit CommonQueryExecutor(const LibObjPtr libObjPtr) noexcept : + _ThisBorrowedObject {libObjPtr} + { + } + + template + CommonQueryExecutor(const CommonQueryExecutor queryExec) noexcept : + _ThisBorrowedObject {queryExec} + { + } + + template + CommonQueryExecutor operator=(const CommonQueryExecutor queryExec) noexcept + { + _ThisBorrowedObject::operator=(queryExec); + return *this; + } + + static Shared create(const ConstComponentClass compCls, const bt2c::CStringView objectName, + const OptionalBorrowedObject params = {}) + { + return CommonQueryExecutor::_create(compCls, objectName, params, + static_cast(nullptr)); + } + + template + static Shared create(const ConstComponentClass compCls, const bt2c::CStringView objectName, + QueryDataT& queryData, + const OptionalBorrowedObject params = {}) + { + return CommonQueryExecutor::_create(compCls, objectName, params, &queryData); + } + + ConstValue::Shared query() const + { + static_assert(!std::is_const::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 + static Shared _create(const ConstComponentClass compCls, const bt2c::CStringView objectName, + const OptionalBorrowedObject params, + QueryDataT * const queryData) + { + const auto libObjPtr = bt_query_executor_create_with_method_data( + compCls.libObjPtr(), objectName, params ? params->libObjPtr() : nullptr, + const_cast(static_cast(queryData))); + + if (!libObjPtr) { + throw MemoryError {}; + } + + return Shared::createWithoutRef(libObjPtr); + } +}; + +using QueryExecutor = CommonQueryExecutor; +using ConstQueryExecutor = CommonQueryExecutor; + +} /* namespace bt2 */ + +#endif /* BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP */ -- 2.34.1