| 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" |
| 15 | #include "component-class-dev.hpp" |
| 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 | |
| 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 | |
| 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 | |
| 186 | template <typename UserComponentT> |
| 187 | static CommonSourceComponentClass<LibObjT>::Shared create() |
| 188 | { |
| 189 | return CommonSourceComponentClass::Shared::createWithoutRef( |
| 190 | internal::createSourceCompCls<UserComponentT>()); |
| 191 | } |
| 192 | |
| 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 | |
| 208 | private: |
| 209 | ConstComponentClass _constComponentClass() const noexcept |
| 210 | { |
| 211 | return ConstComponentClass { |
| 212 | bt_component_class_source_as_component_class_const(this->libObjPtr())}; |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | template <typename LibObjT> |
| 217 | CommonComponentClass<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 | |
| 223 | template <typename LibObjT> |
| 224 | CommonComponentClass<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 | |
| 230 | template <typename LibObjT> |
| 231 | CommonComponentClass<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 | |
| 239 | template <typename LibObjT> |
| 240 | CommonComponentClass<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 | |
| 248 | using SourceComponentClass = CommonSourceComponentClass<bt_component_class_source>; |
| 249 | using ConstSourceComponentClass = CommonSourceComponentClass<const bt_component_class_source>; |
| 250 | |
| 251 | namespace internal { |
| 252 | |
| 253 | struct 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 | |
| 268 | template <typename LibObjT> |
| 269 | class CommonFilterComponentClass final : public BorrowedObject<LibObjT> |
| 270 | { |
| 271 | private: |
| 272 | using _ThisBorrowedObject = BorrowedObject<LibObjT>; |
| 273 | |
| 274 | public: |
| 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 | |
| 297 | template <typename UserComponentT> |
| 298 | static CommonFilterComponentClass<LibObjT>::Shared create() |
| 299 | { |
| 300 | return CommonFilterComponentClass::Shared::createWithoutRef( |
| 301 | internal::createFilterCompCls<UserComponentT>()); |
| 302 | } |
| 303 | |
| 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 | |
| 319 | private: |
| 320 | ConstComponentClass _constComponentClass() const noexcept |
| 321 | { |
| 322 | return ConstComponentClass { |
| 323 | bt_component_class_filter_as_component_class_const(this->libObjPtr())}; |
| 324 | } |
| 325 | }; |
| 326 | |
| 327 | template <typename LibObjT> |
| 328 | CommonComponentClass<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 | |
| 334 | template <typename LibObjT> |
| 335 | CommonComponentClass<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 | |
| 341 | template <typename LibObjT> |
| 342 | CommonComponentClass<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 | |
| 350 | template <typename LibObjT> |
| 351 | CommonComponentClass<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 | |
| 359 | using FilterComponentClass = CommonFilterComponentClass<bt_component_class_filter>; |
| 360 | using ConstFilterComponentClass = CommonFilterComponentClass<const bt_component_class_filter>; |
| 361 | |
| 362 | namespace internal { |
| 363 | |
| 364 | struct 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 | |
| 379 | template <typename LibObjT> |
| 380 | class CommonSinkComponentClass final : public BorrowedObject<LibObjT> |
| 381 | { |
| 382 | private: |
| 383 | using _ThisBorrowedObject = BorrowedObject<LibObjT>; |
| 384 | |
| 385 | public: |
| 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 | |
| 408 | template <typename UserComponentT> |
| 409 | static CommonSinkComponentClass<LibObjT>::Shared create() |
| 410 | { |
| 411 | return CommonSinkComponentClass::Shared::createWithoutRef( |
| 412 | internal::createSinkCompCls<UserComponentT>()); |
| 413 | } |
| 414 | |
| 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 | |
| 430 | private: |
| 431 | ConstComponentClass _constComponentClass() const noexcept |
| 432 | { |
| 433 | return ConstComponentClass { |
| 434 | bt_component_class_sink_as_component_class_const(this->libObjPtr())}; |
| 435 | } |
| 436 | }; |
| 437 | |
| 438 | template <typename LibObjT> |
| 439 | CommonComponentClass<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 | |
| 445 | template <typename LibObjT> |
| 446 | CommonComponentClass<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 | |
| 452 | template <typename LibObjT> |
| 453 | CommonComponentClass<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 | |
| 461 | template <typename LibObjT> |
| 462 | CommonComponentClass<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 | |
| 470 | using SinkComponentClass = CommonSinkComponentClass<bt_component_class_sink>; |
| 471 | using ConstSinkComponentClass = CommonSinkComponentClass<const bt_component_class_sink>; |
| 472 | |
| 473 | } /* namespace bt2 */ |
| 474 | |
| 475 | #endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_CLASS_HPP */ |