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