cpp-common/bt2: add C++ query executor bindings
[babeltrace.git] / src / cpp-common / bt2 / query-executor.hpp
1 /*
2 * Copyright (c) 2024 EfficiOS, Inc.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
7 #ifndef BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP
9
10 #include <babeltrace2/babeltrace.h>
11
12 #include "cpp-common/bt2c/c-string-view.hpp"
13
14 #include "borrowed-object.hpp"
15 #include "component-class.hpp"
16 #include "shared-object.hpp"
17 #include "value.hpp"
18
19 namespace bt2 {
20 namespace internal {
21
22 struct QueryExecutorRefFuncs final
23 {
24 static void get(const bt_query_executor * const libObjPtr) noexcept
25 {
26 bt_query_executor_get_ref(libObjPtr);
27 }
28
29 static void put(const bt_query_executor * const libObjPtr) noexcept
30 {
31 bt_query_executor_put_ref(libObjPtr);
32 }
33 };
34
35 } /* namespace internal */
36
37 template <typename LibObjT>
38 class CommonQueryExecutor final : public BorrowedObject<LibObjT>
39 {
40 private:
41 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
42
43 public:
44 using typename _ThisBorrowedObject::LibObjPtr;
45 using Shared = SharedObject<CommonQueryExecutor, LibObjT, internal::QueryExecutorRefFuncs>;
46
47 explicit CommonQueryExecutor(const LibObjPtr libObjPtr) noexcept :
48 _ThisBorrowedObject {libObjPtr}
49 {
50 }
51
52 template <typename OtherLibObjT>
53 CommonQueryExecutor(const CommonQueryExecutor<OtherLibObjT> queryExec) noexcept :
54 _ThisBorrowedObject {queryExec}
55 {
56 }
57
58 template <typename OtherLibObjT>
59 CommonQueryExecutor operator=(const CommonQueryExecutor<OtherLibObjT> queryExec) noexcept
60 {
61 _ThisBorrowedObject::operator=(queryExec);
62 return *this;
63 }
64
65 static Shared create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
66 const OptionalBorrowedObject<ConstMapValue> params = {})
67 {
68 return CommonQueryExecutor::_create(compCls, objectName, params,
69 static_cast<void *>(nullptr));
70 }
71
72 template <typename QueryDataT>
73 static Shared create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
74 QueryDataT& queryData,
75 const OptionalBorrowedObject<ConstMapValue> params = {})
76 {
77 return CommonQueryExecutor::_create(compCls, objectName, params, &queryData);
78 }
79
80 ConstValue::Shared query() const
81 {
82 static_assert(!std::is_const<LibObjT>::value,
83 "Not available with `bt2::ConstQueryExecutor`.");
84
85 const bt_value *res;
86 const auto status = bt_query_executor_query(this->libObjPtr(), &res);
87
88 if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_ERROR) {
89 throw Error {};
90 } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_MEMORY_ERROR) {
91 throw MemoryError {};
92 } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_AGAIN) {
93 throw TryAgain {};
94 } else if (status == BT_QUERY_EXECUTOR_QUERY_STATUS_UNKNOWN_OBJECT) {
95 throw UnknownObject {};
96 }
97
98 return ConstValue::Shared::createWithoutRef(res);
99 }
100
101 private:
102 template <typename QueryDataT>
103 static Shared _create(const ConstComponentClass compCls, const bt2c::CStringView objectName,
104 const OptionalBorrowedObject<ConstMapValue> params,
105 QueryDataT * const queryData)
106 {
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)));
110
111 if (!libObjPtr) {
112 throw MemoryError {};
113 }
114
115 return Shared::createWithoutRef(libObjPtr);
116 }
117 };
118
119 using QueryExecutor = CommonQueryExecutor<bt_query_executor>;
120 using ConstQueryExecutor = CommonQueryExecutor<const bt_query_executor>;
121
122 } /* namespace bt2 */
123
124 #endif /* BABELTRACE_CPP_COMMON_BT2_QUERY_EXECUTOR_HPP */
This page took 0.03145 seconds and 4 git commands to generate.