2 * Copyright (c) 2024 EfficiOS, Inc.
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP
10 #include <babeltrace2/babeltrace.h>
12 #include "cpp-common/bt2c/c-string-view.hpp"
14 #include "borrowed-object.hpp"
15 #include "component-class.hpp"
16 #include "shared-object.hpp"
22 struct QueryExecutorRefFuncs final
24 static void get(const bt_query_executor * const libObjPtr) noexcept
26 bt_query_executor_get_ref(libObjPtr);
29 static void put(const bt_query_executor * const libObjPtr) noexcept
31 bt_query_executor_put_ref(libObjPtr);
35 } /* namespace internal */
37 template <typename LibObjT>
38 class CommonQueryExecutor final : public BorrowedObject<LibObjT>
41 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
44 using typename _ThisBorrowedObject::LibObjPtr;
45 using Shared = SharedObject<CommonQueryExecutor, LibObjT, internal::QueryExecutorRefFuncs>;
47 explicit CommonQueryExecutor(const LibObjPtr libObjPtr) noexcept :
48 _ThisBorrowedObject {libObjPtr}
52 template <typename OtherLibObjT>
53 CommonQueryExecutor(const CommonQueryExecutor<OtherLibObjT> queryExec) noexcept :
54 _ThisBorrowedObject {queryExec}
58 template <typename OtherLibObjT>
59 CommonQueryExecutor operator=(const CommonQueryExecutor<OtherLibObjT> queryExec) noexcept
61 _ThisBorrowedObject::operator=(queryExec);
65 static Shared create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
66 const OptionalBorrowedObject<ConstMapValue> params = {})
68 return CommonQueryExecutor::_create(compCls, objectName, params,
69 static_cast<void *>(nullptr));
72 template <typename QueryDataT>
73 static Shared create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
74 QueryDataT& queryData,
75 const OptionalBorrowedObject<ConstMapValue> params = {})
77 return CommonQueryExecutor::_create(compCls, objectName, params, &queryData);
80 ConstValue::Shared query() const
82 static_assert(!std::is_const<LibObjT>::value,
83 "Not available with `bt2::ConstQueryExecutor`.");
86 const auto status = bt_query_executor_query(this->libObjPtr(), &res);
88 if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR) {
90 } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR) {
92 } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN) {
94 } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_UNKNOWN_OBJECT) {
95 throw UnknownObject {};
98 return ConstValue::Shared::createWithoutRef(res);
102 template <typename QueryDataT>
103 static Shared _create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
104 const OptionalBorrowedObject<ConstMapValue> params,
105 QueryDataT * const queryData)
107 const auto libObjPtr = bt_query_executor_create_with_method_data(
108 compCls.libObjPtr(), objectName, params ? params->libObjPtr() : nullptr,
109 const_cast<void *>(static_cast<const void *>(queryData)));
112 throw MemoryError {};
115 return Shared::createWithoutRef(libObjPtr);
119 using QueryExecutor = CommonQueryExecutor<bt_query_executor>;
120 using ConstQueryExecutor = CommonQueryExecutor<const bt_query_executor>;
122 } /* namespace bt2 */
124 #endif /* BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP */