cpp-common/bt2: add C++ bindings around `bt_error *`
[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
a65c7bf4
SM
55 enum class Type
56 {
57 SOURCE = BT_COMPONENT_CLASS_TYPE_SOURCE,
58 FILTER = BT_COMPONENT_CLASS_TYPE_FILTER,
59 SINK = BT_COMPONENT_CLASS_TYPE_SINK,
60 };
61
cb036945
SM
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
137using ComponentClass = CommonComponentClass<bt_component_class>;
138using ConstComponentClass = CommonComponentClass<const bt_component_class>;
139
140namespace internal {
141
142struct 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
157template <typename LibObjT>
158class CommonSourceComponentClass final : public BorrowedObject<LibObjT>
159{
160private:
161 using _ThisBorrowedObject = BorrowedObject<LibObjT>;
162
163public:
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
208private:
209 ConstComponentClass _constComponentClass() const noexcept
210 {
211 return ConstComponentClass {
212 bt_component_class_source_as_component_class_const(this->libObjPtr())};
213 }
214};
215
216template <typename LibObjT>
217CommonComponentClass<LibObjT>::CommonComponentClass(
218 const CommonSourceComponentClass<const bt_component_class_source> other) noexcept :
219 _ThisBorrowedObject {bt_component_class_source_as_component_class_const(other.libObjPtr())}
220{
221}
222
223template <typename LibObjT>
224CommonComponentClass<LibObjT>::CommonComponentClass(
225 const CommonSourceComponentClass<bt_component_class_source> other) noexcept :
226 _ThisBorrowedObject {bt_component_class_source_as_component_class(other.libObjPtr())}
227{
228}
229
230template <typename LibObjT>
231CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=(
232 const CommonSourceComponentClass<const bt_component_class_source> other) noexcept
233{
234 BorrowedObject<LibObjT>::operator=(ConstComponentClass {
235 bt_component_class_source_as_component_class_const(other.libObjPtr())});
236 return *this;
237}
238
239template <typename LibObjT>
240CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=(
241 const CommonSourceComponentClass<bt_component_class_source> other) noexcept
242{
243 BorrowedObject<LibObjT>::operator=(
244 ComponentClass {bt_component_class_source_as_component_class(other.libObjPtr())});
245 return *this;
246}
247
248using SourceComponentClass = CommonSourceComponentClass<bt_component_class_source>;
249using ConstSourceComponentClass = CommonSourceComponentClass<const bt_component_class_source>;
250
251namespace internal {
252
253struct FilterComponentClassRefFuncs final
254{
255 static void get(const bt_component_class_filter * const libObjPtr) noexcept
256 {
257 bt_component_class_filter_get_ref(libObjPtr);
258 }
259
260 static void put(const bt_component_class_filter * const libObjPtr) noexcept
261 {
262 bt_component_class_filter_put_ref(libObjPtr);
263 }
264};
265
266} /* namespace internal */
267
268template <typename LibObjT>
269class CommonFilterComponentClass final : public BorrowedObject<LibObjT>
270{
271private:
272 using _ThisBorrowedObject = BorrowedObject<LibObjT>;
273
274public:
275 using typename _ThisBorrowedObject::LibObjPtr;
276 using Shared =
277 SharedObject<CommonFilterComponentClass, LibObjT, internal::FilterComponentClassRefFuncs>;
278
279 CommonFilterComponentClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
280 {
281 }
282
283 template <typename OtherLibObjT>
284 CommonFilterComponentClass(const CommonFilterComponentClass<OtherLibObjT> compCls) noexcept :
285 _ThisBorrowedObject {compCls}
286 {
287 }
288
289 template <typename OtherLibObjT>
290 CommonFilterComponentClass
291 operator=(const CommonFilterComponentClass<OtherLibObjT> compCls) noexcept
292 {
293 _ThisBorrowedObject::operator=(compCls);
294 return *this;
295 }
296
ee137c4e
PP
297 template <typename UserComponentT>
298 static CommonFilterComponentClass<LibObjT>::Shared create()
299 {
300 return CommonFilterComponentClass::Shared::createWithoutRef(
301 internal::createFilterCompCls<UserComponentT>());
302 }
303
cb036945
SM
304 bt2c::CStringView name() const noexcept
305 {
306 return this->_constComponentClass().name();
307 }
308
309 bt2c::CStringView description() const noexcept
310 {
311 return this->_constComponentClass().description();
312 }
313
314 bt2c::CStringView help() const noexcept
315 {
316 return this->_constComponentClass().help();
317 }
318
319private:
320 ConstComponentClass _constComponentClass() const noexcept
321 {
322 return ConstComponentClass {
323 bt_component_class_filter_as_component_class_const(this->libObjPtr())};
324 }
325};
326
327template <typename LibObjT>
328CommonComponentClass<LibObjT>::CommonComponentClass(
329 const CommonFilterComponentClass<const bt_component_class_filter> other) noexcept :
330 _ThisBorrowedObject {bt_component_class_filter_as_component_class_const(other.libObjPtr())}
331{
332}
333
334template <typename LibObjT>
335CommonComponentClass<LibObjT>::CommonComponentClass(
336 const CommonFilterComponentClass<bt_component_class_filter> other) noexcept :
337 _ThisBorrowedObject {bt_component_class_filter_as_component_class(other.libObjPtr())}
338{
339}
340
341template <typename LibObjT>
342CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=(
343 const CommonFilterComponentClass<const bt_component_class_filter> other) noexcept
344{
345 BorrowedObject<LibObjT>::operator=(ConstComponentClass {
346 bt_component_class_filter_as_component_class_const(other.libObjPtr())});
347 return *this;
348}
349
350template <typename LibObjT>
351CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=(
352 const CommonFilterComponentClass<bt_component_class_filter> other) noexcept
353{
354 BorrowedObject<LibObjT>::operator=(
355 ComponentClass {bt_component_class_filter_as_component_class(other.libObjPtr())});
356 return *this;
357}
358
359using FilterComponentClass = CommonFilterComponentClass<bt_component_class_filter>;
360using ConstFilterComponentClass = CommonFilterComponentClass<const bt_component_class_filter>;
361
362namespace internal {
363
364struct SinkComponentClassRefFuncs final
365{
366 static void get(const bt_component_class_sink * const libObjPtr) noexcept
367 {
368 bt_component_class_sink_get_ref(libObjPtr);
369 }
370
371 static void put(const bt_component_class_sink * const libObjPtr) noexcept
372 {
373 bt_component_class_sink_put_ref(libObjPtr);
374 }
375};
376
377} /* namespace internal */
378
379template <typename LibObjT>
380class CommonSinkComponentClass final : public BorrowedObject<LibObjT>
381{
382private:
383 using _ThisBorrowedObject = BorrowedObject<LibObjT>;
384
385public:
386 using typename _ThisBorrowedObject::LibObjPtr;
387 using Shared =
388 SharedObject<CommonSinkComponentClass, LibObjT, internal::SinkComponentClassRefFuncs>;
389
390 CommonSinkComponentClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
391 {
392 }
393
394 template <typename OtherLibObjT>
395 CommonSinkComponentClass(const CommonSinkComponentClass<OtherLibObjT> compCls) noexcept :
396 _ThisBorrowedObject {compCls}
397 {
398 }
399
400 template <typename OtherLibObjT>
401 CommonSinkComponentClass
402 operator=(const CommonSinkComponentClass<OtherLibObjT> compCls) noexcept
403 {
404 _ThisBorrowedObject::operator=(compCls);
405 return *this;
406 }
407
ee137c4e
PP
408 template <typename UserComponentT>
409 static CommonSinkComponentClass<LibObjT>::Shared create()
410 {
411 return CommonSinkComponentClass::Shared::createWithoutRef(
412 internal::createSinkCompCls<UserComponentT>());
413 }
414
cb036945
SM
415 bt2c::CStringView name() const noexcept
416 {
417 return this->_constComponentClass().name();
418 }
419
420 bt2c::CStringView description() const noexcept
421 {
422 return this->_constComponentClass().description();
423 }
424
425 bt2c::CStringView help() const noexcept
426 {
427 return this->_constComponentClass().help();
428 }
429
430private:
431 ConstComponentClass _constComponentClass() const noexcept
432 {
433 return ConstComponentClass {
434 bt_component_class_sink_as_component_class_const(this->libObjPtr())};
435 }
436};
437
438template <typename LibObjT>
439CommonComponentClass<LibObjT>::CommonComponentClass(
440 const CommonSinkComponentClass<const bt_component_class_sink> other) noexcept :
441 _ThisBorrowedObject {bt_component_class_sink_as_component_class_const(other.libObjPtr())}
442{
443}
444
445template <typename LibObjT>
446CommonComponentClass<LibObjT>::CommonComponentClass(
447 const CommonSinkComponentClass<bt_component_class_sink> other) noexcept :
448 _ThisBorrowedObject {bt_component_class_sink_as_component_class(other.libObjPtr())}
449{
450}
451
452template <typename LibObjT>
453CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=(
454 const CommonSinkComponentClass<const bt_component_class_sink> other) noexcept
455{
456 BorrowedObject<LibObjT>::operator=(
457 ConstComponentClass {bt_component_class_sink_as_component_class_const(other.libObjPtr())});
458 return *this;
459}
460
461template <typename LibObjT>
462CommonComponentClass<LibObjT> CommonComponentClass<LibObjT>::operator=(
463 const CommonSinkComponentClass<bt_component_class_sink> other) noexcept
464{
465 BorrowedObject<LibObjT>::operator=(
466 ComponentClass {bt_component_class_sink_as_component_class(other.libObjPtr())});
467 return *this;
468}
469
470using SinkComponentClass = CommonSinkComponentClass<bt_component_class_sink>;
471using ConstSinkComponentClass = CommonSinkComponentClass<const bt_component_class_sink>;
472
473} /* namespace bt2 */
474
475#endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_CLASS_HPP */
This page took 0.039001 seconds and 4 git commands to generate.