Commit | Line | Data |
---|---|---|
cb036945 SM |
1 | /* |
2 | * Copyright (c) 2024 EfficiOS, Inc. | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_COMPONENT_CLASS_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_COMPONENT_CLASS_HPP | |
9 | ||
10 | #include <babeltrace2/babeltrace.h> | |
11 | ||
12 | #include "cpp-common/bt2c/c-string-view.hpp" | |
13 | ||
14 | #include "borrowed-object.hpp" | |
ee137c4e | 15 | #include "component-class-dev.hpp" |
cb036945 SM |
16 | #include "shared-object.hpp" |
17 | ||
18 | namespace bt2 { | |
19 | namespace internal { | |
20 | ||
21 | struct ComponentClassRefFuncs final | |
22 | { | |
23 | static void get(const bt_component_class * const libObjPtr) noexcept | |
24 | { | |
25 | bt_component_class_get_ref(libObjPtr); | |
26 | } | |
27 | ||
28 | static void put(const bt_component_class * const libObjPtr) noexcept | |
29 | { | |
30 | bt_component_class_put_ref(libObjPtr); | |
31 | } | |
32 | }; | |
33 | ||
34 | } /* namespace internal */ | |
35 | ||
33f20928 SM |
36 | enum class ComponentClassType |
37 | { | |
38 | Source = BT_COMPONENT_CLASS_TYPE_SOURCE, | |
39 | Filter = BT_COMPONENT_CLASS_TYPE_FILTER, | |
40 | Sink = BT_COMPONENT_CLASS_TYPE_SINK, | |
41 | }; | |
42 | ||
cb036945 SM |
43 | template <typename LibObjT> |
44 | class CommonSourceComponentClass; | |
45 | ||
46 | template <typename LibObjT> | |
47 | class CommonFilterComponentClass; | |
48 | ||
49 | template <typename LibObjT> | |
50 | class CommonSinkComponentClass; | |
51 | ||
52 | template <typename LibObjT> | |
53 | class CommonComponentClass : public BorrowedObject<LibObjT> | |
54 | { | |
55 | private: | |
56 | using _ThisBorrowedObject = BorrowedObject<LibObjT>; | |
57 | ||
58 | public: | |
59 | using typename _ThisBorrowedObject::LibObjPtr; | |
60 | using Shared = SharedObject<CommonComponentClass, LibObjT, internal::ComponentClassRefFuncs>; | |
61 | ||
62 | explicit CommonComponentClass(const LibObjPtr libObjPtr) noexcept : | |
63 | _ThisBorrowedObject {libObjPtr} | |
64 | { | |
65 | } | |
66 | ||
67 | template <typename OtherLibObjT> | |
68 | CommonComponentClass(const CommonComponentClass<OtherLibObjT> compCls) noexcept : | |
69 | _ThisBorrowedObject {compCls} | |
70 | { | |
71 | } | |
72 | ||
73 | template <typename OtherLibObjT> | |
74 | CommonComponentClass operator=(const CommonComponentClass<OtherLibObjT> compCls) noexcept | |
75 | { | |
76 | _ThisBorrowedObject::operator=(compCls); | |
77 | return *this; | |
78 | } | |
79 | ||
80 | /* Not `explicit` to make them behave like copy constructors */ | |
81 | CommonComponentClass( | |
82 | const CommonSourceComponentClass<const bt_component_class_source> other) noexcept; | |
83 | CommonComponentClass( | |
84 | const CommonSourceComponentClass<bt_component_class_source> other) noexcept; | |
85 | CommonComponentClass( | |
86 | const CommonFilterComponentClass<const bt_component_class_filter> other) noexcept; | |
87 | CommonComponentClass( | |
88 | const CommonFilterComponentClass<bt_component_class_filter> other) noexcept; | |
89 | CommonComponentClass( | |
90 | const CommonSinkComponentClass<const bt_component_class_sink> other) noexcept; | |
91 | CommonComponentClass(const CommonSinkComponentClass<bt_component_class_sink> other) noexcept; | |
92 | ||
93 | CommonComponentClass | |
94 | operator=(CommonSourceComponentClass<const bt_component_class_source> other) noexcept; | |
95 | CommonComponentClass | |
96 | operator=(CommonSourceComponentClass<bt_component_class_source> other) noexcept; | |
97 | CommonComponentClass | |
98 | operator=(CommonFilterComponentClass<const bt_component_class_filter> other) noexcept; | |
99 | CommonComponentClass | |
100 | operator=(CommonFilterComponentClass<bt_component_class_filter> other) noexcept; | |
101 | CommonComponentClass | |
102 | operator=(CommonSinkComponentClass<const bt_component_class_sink> other) noexcept; | |
103 | CommonComponentClass | |
104 | operator=(CommonSinkComponentClass<bt_component_class_sink> other) noexcept; | |
105 | ||
106 | bool isSource() const noexcept | |
107 | { | |
108 | return static_cast<bool>(bt_component_class_is_source(this->libObjPtr())); | |
109 | } | |
110 | ||
111 | bool isFilter() const noexcept | |
112 | { | |
113 | return static_cast<bool>(bt_component_class_is_filter(this->libObjPtr())); | |
114 | } | |
115 | ||
116 | bool isSink() const noexcept | |
117 | { | |
118 | return static_cast<bool>(bt_component_class_is_sink(this->libObjPtr())); | |
119 | } | |
120 | ||
121 | bt2c::CStringView name() const noexcept | |
122 | { | |
123 | return bt_component_class_get_name(this->libObjPtr()); | |
124 | } | |
125 | ||
126 | bt2c::CStringView description() const noexcept | |
127 | { | |
128 | return bt_component_class_get_description(this->libObjPtr()); | |
129 | } | |
130 | ||
131 | bt2c::CStringView help() const noexcept | |
132 | { | |
133 | return bt_component_class_get_help(this->libObjPtr()); | |
134 | } | |
135 | }; | |
136 | ||
137 | using ComponentClass = CommonComponentClass<bt_component_class>; | |
138 | using ConstComponentClass = CommonComponentClass<const bt_component_class>; | |
139 | ||
140 | namespace internal { | |
141 | ||
142 | struct SourceComponentClassRefFuncs final | |
143 | { | |
144 | static void get(const bt_component_class_source * const libObjPtr) noexcept | |
145 | { | |
146 | bt_component_class_source_get_ref(libObjPtr); | |
147 | } | |
148 | ||
149 | static void put(const bt_component_class_source * const libObjPtr) noexcept | |
150 | { | |
151 | bt_component_class_source_put_ref(libObjPtr); | |
152 | } | |
153 | }; | |
154 | ||
155 | } /* namespace internal */ | |
156 | ||
157 | template <typename LibObjT> | |
158 | class CommonSourceComponentClass final : public BorrowedObject<LibObjT> | |
159 | { | |
160 | private: | |
161 | using _ThisBorrowedObject = BorrowedObject<LibObjT>; | |
162 | ||
163 | public: | |
164 | using typename _ThisBorrowedObject::LibObjPtr; | |
165 | using Shared = | |
166 | SharedObject<CommonSourceComponentClass, LibObjT, internal::SourceComponentClassRefFuncs>; | |
167 | ||
168 | CommonSourceComponentClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} | |
169 | { | |
170 | } | |
171 | ||
172 | template <typename OtherLibObjT> | |
173 | CommonSourceComponentClass(const CommonSourceComponentClass<OtherLibObjT> compCls) noexcept : | |
174 | _ThisBorrowedObject {compCls} | |
175 | { | |
176 | } | |
177 | ||
178 | template <typename OtherLibObjT> | |
179 | CommonSourceComponentClass | |
180 | operator=(const CommonSourceComponentClass<OtherLibObjT> compCls) noexcept | |
181 | { | |
182 | _ThisBorrowedObject::operator=(compCls); | |
183 | return *this; | |
184 | } | |
185 | ||
ee137c4e PP |
186 | template <typename UserComponentT> |
187 | static CommonSourceComponentClass<LibObjT>::Shared create() | |
188 | { | |
189 | return CommonSourceComponentClass::Shared::createWithoutRef( | |
190 | internal::createSourceCompCls<UserComponentT>()); | |
191 | } | |
192 | ||
cb036945 SM |
193 | bt2c::CStringView name() const noexcept |
194 | { | |
195 | return this->_constComponentClass().name(); | |
196 | } | |
197 | ||
198 | bt2c::CStringView description() const noexcept | |
199 | { | |
200 | return this->_constComponentClass().description(); | |
201 | } | |
202 | ||
203 | bt2c::CStringView help() const noexcept | |
204 | { | |
205 | return this->_constComponentClass().help(); | |
206 | } | |
207 | ||
2c8e2496 SM |
208 | Shared shared() const noexcept |
209 | { | |
210 | return Shared::createWithRef(*this); | |
211 | } | |
212 | ||
cb036945 SM |
213 | private: |
214 | ConstComponentClass _constComponentClass() const noexcept | |
215 | { | |
216 | return ConstComponentClass { | |
217 | bt_component_class_source_as_component_class_const(this->libObjPtr())}; | |
218 | } | |
219 | }; | |
220 | ||
221 | template <typename LibObjT> | |
222 | CommonComponentClass<LibObjT>::CommonComponentClass( | |
223 | const CommonSourceComponentClass<const bt_component_class_source> other) noexcept : | |
224 | _ThisBorrowedObject {bt_component_class_source_as_component_class_const(other.libObjPtr())} | |
225 | { | |
226 | } | |
227 | ||
228 | template <typename LibObjT> | |
229 | CommonComponentClass<LibObjT>::CommonComponentClass( | |
230 | const CommonSourceComponentClass<bt_component_class_source> other) noexcept : | |
231 | _ThisBorrowedObject {bt_component_class_source_as_component_class(other.libObjPtr())} | |
232 | { | |
233 | } | |
234 | ||
235 | template <typename LibObjT> | |
236 | CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=( | |
237 | const CommonSourceComponentClass<const bt_component_class_source> other) noexcept | |
238 | { | |
239 | BorrowedObject<LibObjT>::operator=(ConstComponentClass { | |
240 | bt_component_class_source_as_component_class_const(other.libObjPtr())}); | |
241 | return *this; | |
242 | } | |
243 | ||
244 | template <typename LibObjT> | |
245 | CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=( | |
246 | const CommonSourceComponentClass<bt_component_class_source> other) noexcept | |
247 | { | |
248 | BorrowedObject<LibObjT>::operator=( | |
249 | ComponentClass {bt_component_class_source_as_component_class(other.libObjPtr())}); | |
250 | return *this; | |
251 | } | |
252 | ||
253 | using SourceComponentClass = CommonSourceComponentClass<bt_component_class_source>; | |
254 | using ConstSourceComponentClass = CommonSourceComponentClass<const bt_component_class_source>; | |
255 | ||
256 | namespace internal { | |
257 | ||
258 | struct FilterComponentClassRefFuncs final | |
259 | { | |
260 | static void get(const bt_component_class_filter * const libObjPtr) noexcept | |
261 | { | |
262 | bt_component_class_filter_get_ref(libObjPtr); | |
263 | } | |
264 | ||
265 | static void put(const bt_component_class_filter * const libObjPtr) noexcept | |
266 | { | |
267 | bt_component_class_filter_put_ref(libObjPtr); | |
268 | } | |
269 | }; | |
270 | ||
271 | } /* namespace internal */ | |
272 | ||
273 | template <typename LibObjT> | |
274 | class CommonFilterComponentClass final : public BorrowedObject<LibObjT> | |
275 | { | |
276 | private: | |
277 | using _ThisBorrowedObject = BorrowedObject<LibObjT>; | |
278 | ||
279 | public: | |
280 | using typename _ThisBorrowedObject::LibObjPtr; | |
281 | using Shared = | |
282 | SharedObject<CommonFilterComponentClass, LibObjT, internal::FilterComponentClassRefFuncs>; | |
283 | ||
284 | CommonFilterComponentClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} | |
285 | { | |
286 | } | |
287 | ||
288 | template <typename OtherLibObjT> | |
289 | CommonFilterComponentClass(const CommonFilterComponentClass<OtherLibObjT> compCls) noexcept : | |
290 | _ThisBorrowedObject {compCls} | |
291 | { | |
292 | } | |
293 | ||
294 | template <typename OtherLibObjT> | |
295 | CommonFilterComponentClass | |
296 | operator=(const CommonFilterComponentClass<OtherLibObjT> compCls) noexcept | |
297 | { | |
298 | _ThisBorrowedObject::operator=(compCls); | |
299 | return *this; | |
300 | } | |
301 | ||
ee137c4e PP |
302 | template <typename UserComponentT> |
303 | static CommonFilterComponentClass<LibObjT>::Shared create() | |
304 | { | |
305 | return CommonFilterComponentClass::Shared::createWithoutRef( | |
306 | internal::createFilterCompCls<UserComponentT>()); | |
307 | } | |
308 | ||
cb036945 SM |
309 | bt2c::CStringView name() const noexcept |
310 | { | |
311 | return this->_constComponentClass().name(); | |
312 | } | |
313 | ||
314 | bt2c::CStringView description() const noexcept | |
315 | { | |
316 | return this->_constComponentClass().description(); | |
317 | } | |
318 | ||
319 | bt2c::CStringView help() const noexcept | |
320 | { | |
321 | return this->_constComponentClass().help(); | |
322 | } | |
323 | ||
2c8e2496 SM |
324 | Shared shared() const noexcept |
325 | { | |
326 | return Shared::createWithRef(*this); | |
327 | } | |
328 | ||
cb036945 SM |
329 | private: |
330 | ConstComponentClass _constComponentClass() const noexcept | |
331 | { | |
332 | return ConstComponentClass { | |
333 | bt_component_class_filter_as_component_class_const(this->libObjPtr())}; | |
334 | } | |
335 | }; | |
336 | ||
337 | template <typename LibObjT> | |
338 | CommonComponentClass<LibObjT>::CommonComponentClass( | |
339 | const CommonFilterComponentClass<const bt_component_class_filter> other) noexcept : | |
340 | _ThisBorrowedObject {bt_component_class_filter_as_component_class_const(other.libObjPtr())} | |
341 | { | |
342 | } | |
343 | ||
344 | template <typename LibObjT> | |
345 | CommonComponentClass<LibObjT>::CommonComponentClass( | |
346 | const CommonFilterComponentClass<bt_component_class_filter> other) noexcept : | |
347 | _ThisBorrowedObject {bt_component_class_filter_as_component_class(other.libObjPtr())} | |
348 | { | |
349 | } | |
350 | ||
351 | template <typename LibObjT> | |
352 | CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=( | |
353 | const CommonFilterComponentClass<const bt_component_class_filter> other) noexcept | |
354 | { | |
355 | BorrowedObject<LibObjT>::operator=(ConstComponentClass { | |
356 | bt_component_class_filter_as_component_class_const(other.libObjPtr())}); | |
357 | return *this; | |
358 | } | |
359 | ||
360 | template <typename LibObjT> | |
361 | CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=( | |
362 | const CommonFilterComponentClass<bt_component_class_filter> other) noexcept | |
363 | { | |
364 | BorrowedObject<LibObjT>::operator=( | |
365 | ComponentClass {bt_component_class_filter_as_component_class(other.libObjPtr())}); | |
366 | return *this; | |
367 | } | |
368 | ||
369 | using FilterComponentClass = CommonFilterComponentClass<bt_component_class_filter>; | |
370 | using ConstFilterComponentClass = CommonFilterComponentClass<const bt_component_class_filter>; | |
371 | ||
372 | namespace internal { | |
373 | ||
374 | struct SinkComponentClassRefFuncs final | |
375 | { | |
376 | static void get(const bt_component_class_sink * const libObjPtr) noexcept | |
377 | { | |
378 | bt_component_class_sink_get_ref(libObjPtr); | |
379 | } | |
380 | ||
381 | static void put(const bt_component_class_sink * const libObjPtr) noexcept | |
382 | { | |
383 | bt_component_class_sink_put_ref(libObjPtr); | |
384 | } | |
385 | }; | |
386 | ||
387 | } /* namespace internal */ | |
388 | ||
389 | template <typename LibObjT> | |
390 | class CommonSinkComponentClass final : public BorrowedObject<LibObjT> | |
391 | { | |
392 | private: | |
393 | using _ThisBorrowedObject = BorrowedObject<LibObjT>; | |
394 | ||
395 | public: | |
396 | using typename _ThisBorrowedObject::LibObjPtr; | |
397 | using Shared = | |
398 | SharedObject<CommonSinkComponentClass, LibObjT, internal::SinkComponentClassRefFuncs>; | |
399 | ||
400 | CommonSinkComponentClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} | |
401 | { | |
402 | } | |
403 | ||
404 | template <typename OtherLibObjT> | |
405 | CommonSinkComponentClass(const CommonSinkComponentClass<OtherLibObjT> compCls) noexcept : | |
406 | _ThisBorrowedObject {compCls} | |
407 | { | |
408 | } | |
409 | ||
410 | template <typename OtherLibObjT> | |
411 | CommonSinkComponentClass | |
412 | operator=(const CommonSinkComponentClass<OtherLibObjT> compCls) noexcept | |
413 | { | |
414 | _ThisBorrowedObject::operator=(compCls); | |
415 | return *this; | |
416 | } | |
417 | ||
ee137c4e PP |
418 | template <typename UserComponentT> |
419 | static CommonSinkComponentClass<LibObjT>::Shared create() | |
420 | { | |
421 | return CommonSinkComponentClass::Shared::createWithoutRef( | |
422 | internal::createSinkCompCls<UserComponentT>()); | |
423 | } | |
424 | ||
cb036945 SM |
425 | bt2c::CStringView name() const noexcept |
426 | { | |
427 | return this->_constComponentClass().name(); | |
428 | } | |
429 | ||
430 | bt2c::CStringView description() const noexcept | |
431 | { | |
432 | return this->_constComponentClass().description(); | |
433 | } | |
434 | ||
435 | bt2c::CStringView help() const noexcept | |
436 | { | |
437 | return this->_constComponentClass().help(); | |
438 | } | |
439 | ||
2c8e2496 SM |
440 | Shared shared() const noexcept |
441 | { | |
442 | return Shared::createWithRef(*this); | |
443 | } | |
444 | ||
cb036945 SM |
445 | private: |
446 | ConstComponentClass _constComponentClass() const noexcept | |
447 | { | |
448 | return ConstComponentClass { | |
449 | bt_component_class_sink_as_component_class_const(this->libObjPtr())}; | |
450 | } | |
451 | }; | |
452 | ||
453 | template <typename LibObjT> | |
454 | CommonComponentClass<LibObjT>::CommonComponentClass( | |
455 | const CommonSinkComponentClass<const bt_component_class_sink> other) noexcept : | |
456 | _ThisBorrowedObject {bt_component_class_sink_as_component_class_const(other.libObjPtr())} | |
457 | { | |
458 | } | |
459 | ||
460 | template <typename LibObjT> | |
461 | CommonComponentClass<LibObjT>::CommonComponentClass( | |
462 | const CommonSinkComponentClass<bt_component_class_sink> other) noexcept : | |
463 | _ThisBorrowedObject {bt_component_class_sink_as_component_class(other.libObjPtr())} | |
464 | { | |
465 | } | |
466 | ||
467 | template <typename LibObjT> | |
468 | CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=( | |
469 | const CommonSinkComponentClass<const bt_component_class_sink> other) noexcept | |
470 | { | |
471 | BorrowedObject<LibObjT>::operator=( | |
472 | ConstComponentClass {bt_component_class_sink_as_component_class_const(other.libObjPtr())}); | |
473 | return *this; | |
474 | } | |
475 | ||
476 | template <typename LibObjT> | |
477 | CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=( | |
478 | const CommonSinkComponentClass<bt_component_class_sink> other) noexcept | |
479 | { | |
480 | BorrowedObject<LibObjT>::operator=( | |
481 | ComponentClass {bt_component_class_sink_as_component_class(other.libObjPtr())}); | |
482 | return *this; | |
483 | } | |
484 | ||
485 | using SinkComponentClass = CommonSinkComponentClass<bt_component_class_sink>; | |
486 | using ConstSinkComponentClass = CommonSinkComponentClass<const bt_component_class_sink>; | |
487 | ||
488 | } /* namespace bt2 */ | |
489 | ||
490 | #endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_CLASS_HPP */ |