Commit | Line | Data |
---|---|---|
3641e3ec SM |
1 | /* |
2 | * Copyright (c) 2024 EfficiOS, Inc. | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP | |
9 | ||
10 | #include <cstdint> | |
11 | ||
12 | #include <babeltrace2/babeltrace.h> | |
13 | ||
14 | #include "cpp-common/bt2c/c-string-view.hpp" | |
15 | #include "cpp-common/bt2s/optional.hpp" | |
16 | ||
17 | #include "borrowed-object-iterator.hpp" | |
18 | #include "borrowed-object.hpp" | |
19 | #include "component-class.hpp" | |
20 | #include "shared-object.hpp" | |
21 | ||
22 | namespace bt2 { | |
23 | ||
24 | template <typename PluginSpecCompClsT> | |
25 | class ConstPluginComponentClasses final : public BorrowedObject<const bt_plugin> | |
26 | { | |
27 | public: | |
28 | using Iterator = BorrowedObjectIterator<ConstPluginComponentClasses<PluginSpecCompClsT>>; | |
29 | ||
30 | explicit ConstPluginComponentClasses(const LibObjPtr libPluginPtr) noexcept : | |
31 | _ThisBorrowedObject {libPluginPtr} | |
32 | { | |
33 | } | |
34 | ||
35 | std::uint64_t length() const noexcept | |
36 | { | |
37 | return PluginSpecCompClsT::getCompClsCount(this->libObjPtr()); | |
38 | } | |
39 | ||
40 | Iterator begin() const noexcept | |
41 | { | |
42 | return Iterator {*this, 0}; | |
43 | } | |
44 | ||
45 | Iterator end() const noexcept | |
46 | { | |
47 | return Iterator {*this, this->length()}; | |
48 | } | |
49 | ||
50 | typename PluginSpecCompClsT::CompCls operator[](const std::uint64_t index) const noexcept | |
51 | { | |
52 | return PluginSpecCompClsT::borrowCompClsByIndex(this->libObjPtr(), index); | |
53 | } | |
54 | ||
55 | OptionalBorrowedObject<typename PluginSpecCompClsT::CompCls> | |
56 | operator[](const bt2c::CStringView name) const noexcept | |
57 | { | |
58 | return PluginSpecCompClsT::borrowCompClsByName(this->libObjPtr(), name); | |
59 | } | |
60 | }; | |
61 | ||
62 | namespace internal { | |
63 | ||
64 | struct PluginRefFuncs final | |
65 | { | |
66 | static void get(const bt_plugin * const libObjPtr) noexcept | |
67 | { | |
68 | bt_plugin_get_ref(libObjPtr); | |
69 | } | |
70 | ||
71 | static void put(const bt_plugin * const libObjPtr) noexcept | |
72 | { | |
73 | bt_plugin_put_ref(libObjPtr); | |
74 | } | |
75 | }; | |
76 | ||
77 | struct PluginSourceCompClsFuncs final | |
78 | { | |
79 | using CompCls = ConstSourceComponentClass; | |
80 | ||
81 | static std::uint64_t getCompClsCount(const bt_plugin * const plugin) noexcept | |
82 | { | |
83 | return bt_plugin_get_source_component_class_count(plugin); | |
84 | } | |
85 | ||
86 | static const bt_component_class_source *borrowCompClsByIndex(const bt_plugin * const plugin, | |
87 | const std::uint64_t index) noexcept | |
88 | { | |
89 | return bt_plugin_borrow_source_component_class_by_index_const(plugin, index); | |
90 | } | |
91 | ||
92 | static const bt_component_class_source *borrowCompClsByName(const bt_plugin * const plugin, | |
93 | const char *name) noexcept | |
94 | { | |
95 | return bt_plugin_borrow_source_component_class_by_name_const(plugin, name); | |
96 | } | |
97 | }; | |
98 | ||
99 | struct PluginFilterCompClsFuncs final | |
100 | { | |
101 | using CompCls = ConstFilterComponentClass; | |
102 | ||
103 | static std::uint64_t getCompClsCount(const bt_plugin * const plugin) noexcept | |
104 | { | |
105 | return bt_plugin_get_filter_component_class_count(plugin); | |
106 | } | |
107 | ||
108 | static const bt_component_class_filter *borrowCompClsByIndex(const bt_plugin * const plugin, | |
109 | const std::uint64_t index) noexcept | |
110 | { | |
111 | return bt_plugin_borrow_filter_component_class_by_index_const(plugin, index); | |
112 | } | |
113 | ||
114 | static const bt_component_class_filter *borrowCompClsByName(const bt_plugin * const plugin, | |
115 | const char *name) noexcept | |
116 | { | |
117 | return bt_plugin_borrow_filter_component_class_by_name_const(plugin, name); | |
118 | } | |
119 | }; | |
120 | ||
121 | struct PluginSinkCompClsFuncs final | |
122 | { | |
123 | using CompCls = ConstSinkComponentClass; | |
124 | ||
125 | static std::uint64_t getCompClsCount(const bt_plugin * const plugin) noexcept | |
126 | { | |
127 | return bt_plugin_get_sink_component_class_count(plugin); | |
128 | } | |
129 | ||
130 | static const bt_component_class_sink *borrowCompClsByIndex(const bt_plugin * const plugin, | |
131 | const std::uint64_t index) noexcept | |
132 | { | |
133 | return bt_plugin_borrow_sink_component_class_by_index_const(plugin, index); | |
134 | } | |
135 | ||
136 | static const bt_component_class_sink *borrowCompClsByName(const bt_plugin * const plugin, | |
137 | const char *name) noexcept | |
138 | { | |
139 | return bt_plugin_borrow_sink_component_class_by_name_const(plugin, name); | |
140 | } | |
141 | }; | |
142 | ||
143 | } /* namespace internal */ | |
144 | ||
145 | class ConstPlugin final : public BorrowedObject<const bt_plugin> | |
146 | { | |
147 | public: | |
148 | using SourceComponementClasses = | |
149 | ConstPluginComponentClasses<internal::PluginSourceCompClsFuncs>; | |
150 | using FilterComponementClasses = | |
151 | ConstPluginComponentClasses<internal::PluginFilterCompClsFuncs>; | |
152 | using SinkComponementClasses = ConstPluginComponentClasses<internal::PluginSinkCompClsFuncs>; | |
153 | ||
154 | class Version final | |
155 | { | |
156 | public: | |
157 | explicit Version(const unsigned int major, const unsigned int minor, | |
158 | const unsigned int patch, const bt2c::CStringView extra) noexcept : | |
159 | _mMajor {major}, | |
160 | _mMinor {minor}, _mPatch {patch}, _mExtra {extra} | |
161 | { | |
162 | } | |
163 | ||
164 | unsigned int major() const noexcept | |
165 | { | |
166 | return _mMajor; | |
167 | } | |
168 | ||
169 | unsigned int minor() const noexcept | |
170 | { | |
171 | return _mMinor; | |
172 | } | |
173 | ||
174 | unsigned int patch() const noexcept | |
175 | { | |
176 | return _mPatch; | |
177 | } | |
178 | ||
179 | bt2c::CStringView extra() const noexcept | |
180 | { | |
181 | return _mExtra; | |
182 | } | |
183 | ||
184 | private: | |
185 | unsigned int _mMajor, _mMinor, _mPatch; | |
186 | bt2c::CStringView _mExtra; | |
187 | }; | |
188 | ||
189 | using Shared = SharedObject<ConstPlugin, const bt_plugin, internal::PluginRefFuncs>; | |
190 | ||
191 | explicit ConstPlugin(const LibObjPtr plugin) : _ThisBorrowedObject {plugin} | |
192 | { | |
193 | } | |
194 | ||
195 | bt2c::CStringView name() const noexcept | |
196 | { | |
197 | return bt_plugin_get_name(this->libObjPtr()); | |
198 | } | |
199 | ||
200 | bt2c::CStringView description() const noexcept | |
201 | { | |
202 | return bt_plugin_get_description(this->libObjPtr()); | |
203 | } | |
204 | ||
205 | bt2c::CStringView author() const noexcept | |
206 | { | |
207 | return bt_plugin_get_author(this->libObjPtr()); | |
208 | } | |
209 | ||
210 | bt2c::CStringView license() const noexcept | |
211 | { | |
212 | return bt_plugin_get_license(this->libObjPtr()); | |
213 | } | |
214 | ||
215 | bt2c::CStringView path() const noexcept | |
216 | { | |
217 | return bt_plugin_get_path(this->libObjPtr()); | |
218 | } | |
219 | ||
220 | bt2s::optional<Version> version() const noexcept | |
221 | { | |
222 | unsigned int major, minor, patch; | |
223 | const char *extra; | |
224 | ||
225 | if (bt_plugin_get_version(this->libObjPtr(), &major, &minor, &patch, &extra) == | |
226 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) { | |
227 | return bt2s::nullopt; | |
228 | } | |
229 | ||
230 | return Version {major, minor, patch, extra}; | |
231 | } | |
232 | ||
233 | SourceComponementClasses sourceComponentClasses() const noexcept | |
234 | { | |
235 | return SourceComponementClasses {this->libObjPtr()}; | |
236 | } | |
237 | ||
238 | FilterComponementClasses filterComponentClasses() const noexcept | |
239 | { | |
240 | return FilterComponementClasses {this->libObjPtr()}; | |
241 | } | |
242 | ||
243 | SinkComponementClasses sinkComponentClasses() const noexcept | |
244 | { | |
245 | return SinkComponementClasses {this->libObjPtr()}; | |
246 | } | |
247 | }; | |
248 | ||
249 | } /* namespace bt2 */ | |
250 | ||
251 | #endif /* BABELTRACE_CPP_COMMON_BT2_PLUGIN_HPP */ |