| 1 | /* |
| 2 | * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP |
| 8 | #define BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP |
| 9 | |
| 10 | #include <cstdint> |
| 11 | |
| 12 | #include <babeltrace2/babeltrace.h> |
| 13 | |
| 14 | #include "logging.hpp" |
| 15 | |
| 16 | #include "cpp-common/bt2c/c-string-view.hpp" |
| 17 | |
| 18 | #include "borrowed-object-iterator.hpp" |
| 19 | #include "borrowed-object.hpp" |
| 20 | #include "shared-object.hpp" |
| 21 | |
| 22 | namespace bt2 { |
| 23 | namespace internal { |
| 24 | |
| 25 | struct ConstComponentRefFuncs final |
| 26 | { |
| 27 | static void get(const bt_component * const libObjPtr) noexcept |
| 28 | { |
| 29 | bt_component_get_ref(libObjPtr); |
| 30 | } |
| 31 | |
| 32 | static void put(const bt_component * const libObjPtr) noexcept |
| 33 | { |
| 34 | bt_component_put_ref(libObjPtr); |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | } /* namespace internal */ |
| 39 | |
| 40 | class ConstSourceComponent; |
| 41 | class ConstFilterComponent; |
| 42 | class ConstSinkComponent; |
| 43 | |
| 44 | class ConstComponent final : public BorrowedObject<const bt_component> |
| 45 | { |
| 46 | private: |
| 47 | using typename BorrowedObject<const bt_component>::_ThisBorrowedObject; |
| 48 | |
| 49 | public: |
| 50 | using Shared = |
| 51 | SharedObject<ConstComponent, const bt_component, internal::ConstComponentRefFuncs>; |
| 52 | |
| 53 | explicit ConstComponent(const bt_component * const libObjPtr) noexcept : |
| 54 | _ThisBorrowedObject {libObjPtr} |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | explicit ConstComponent(const bt_component_source * const libObjPtr) noexcept : |
| 59 | _ThisBorrowedObject {bt_component_source_as_component_const(libObjPtr)} |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | explicit ConstComponent(const bt_component_filter * const libObjPtr) noexcept : |
| 64 | _ThisBorrowedObject {bt_component_filter_as_component_const(libObjPtr)} |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | explicit ConstComponent(const bt_component_sink * const libObjPtr) noexcept : |
| 69 | _ThisBorrowedObject {bt_component_sink_as_component_const(libObjPtr)} |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | /* Not `explicit` to make them behave like copy constructors */ |
| 74 | ConstComponent(ConstSourceComponent other) noexcept; |
| 75 | ConstComponent(ConstFilterComponent other) noexcept; |
| 76 | ConstComponent(ConstSinkComponent other) noexcept; |
| 77 | |
| 78 | ConstComponent operator=(ConstSourceComponent other) noexcept; |
| 79 | ConstComponent operator=(ConstFilterComponent other) noexcept; |
| 80 | ConstComponent operator=(ConstSinkComponent other) noexcept; |
| 81 | |
| 82 | bool isSource() const noexcept |
| 83 | { |
| 84 | return static_cast<bool>(bt_component_is_source(this->libObjPtr())); |
| 85 | } |
| 86 | |
| 87 | bool isFilter() const noexcept |
| 88 | { |
| 89 | return static_cast<bool>(bt_component_is_filter(this->libObjPtr())); |
| 90 | } |
| 91 | |
| 92 | bool isSink() const noexcept |
| 93 | { |
| 94 | return static_cast<bool>(bt_component_is_sink(this->libObjPtr())); |
| 95 | } |
| 96 | |
| 97 | bt2c::CStringView name() const noexcept |
| 98 | { |
| 99 | return bt_component_get_name(this->libObjPtr()); |
| 100 | } |
| 101 | |
| 102 | LoggingLevel loggingLevel() const noexcept |
| 103 | { |
| 104 | return static_cast<LoggingLevel>(bt_component_get_logging_level(this->libObjPtr())); |
| 105 | } |
| 106 | |
| 107 | Shared shared() const noexcept |
| 108 | { |
| 109 | return Shared::createWithRef(*this); |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | template <typename LibObjT> |
| 114 | class ConstSpecificComponent : public BorrowedObject<LibObjT> |
| 115 | { |
| 116 | public: |
| 117 | using typename BorrowedObject<LibObjT>::LibObjPtr; |
| 118 | |
| 119 | protected: |
| 120 | explicit ConstSpecificComponent(const LibObjPtr libObjPtr) noexcept : |
| 121 | BorrowedObject<LibObjT> {libObjPtr} |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | public: |
| 126 | bt2c::CStringView name() const noexcept |
| 127 | { |
| 128 | return this->_constComponent().name(); |
| 129 | } |
| 130 | |
| 131 | LoggingLevel loggingLevel() const noexcept |
| 132 | { |
| 133 | return this->_constComponent().loggingLevel(); |
| 134 | } |
| 135 | |
| 136 | ConstComponent::Shared sharedComponent() const noexcept |
| 137 | { |
| 138 | return this->_constComponent().shared(); |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | ConstComponent _constComponent() const noexcept |
| 143 | { |
| 144 | return ConstComponent {this->libObjPtr()}; |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | namespace internal { |
| 149 | |
| 150 | template <typename LibCompT, typename LibPortPtrT> |
| 151 | struct ConstComponentPortsSpec; |
| 152 | |
| 153 | template <> |
| 154 | struct ConstComponentPortsSpec<const bt_component_source, const bt_port_output> final |
| 155 | { |
| 156 | static std::uint64_t portCount(const bt_component_source * const libCompPtr) noexcept |
| 157 | { |
| 158 | return bt_component_source_get_output_port_count(libCompPtr); |
| 159 | } |
| 160 | |
| 161 | static const bt_port_output *portByIndex(const bt_component_source * const libCompPtr, |
| 162 | const std::uint64_t index) noexcept |
| 163 | { |
| 164 | return bt_component_source_borrow_output_port_by_index_const(libCompPtr, index); |
| 165 | } |
| 166 | |
| 167 | static const bt_port_output *portByName(const bt_component_source * const libCompPtr, |
| 168 | const char * const name) noexcept |
| 169 | { |
| 170 | return bt_component_source_borrow_output_port_by_name_const(libCompPtr, name); |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | template <> |
| 175 | struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_output> final |
| 176 | { |
| 177 | static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept |
| 178 | { |
| 179 | return bt_component_filter_get_output_port_count(libCompPtr); |
| 180 | } |
| 181 | |
| 182 | static const bt_port_output *portByIndex(const bt_component_filter * const libCompPtr, |
| 183 | const std::uint64_t index) noexcept |
| 184 | { |
| 185 | return bt_component_filter_borrow_output_port_by_index_const(libCompPtr, index); |
| 186 | } |
| 187 | |
| 188 | static const bt_port_output *portByName(const bt_component_filter * const libCompPtr, |
| 189 | const char * const name) noexcept |
| 190 | { |
| 191 | return bt_component_filter_borrow_output_port_by_name_const(libCompPtr, name); |
| 192 | } |
| 193 | }; |
| 194 | |
| 195 | template <> |
| 196 | struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_input> final |
| 197 | { |
| 198 | static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept |
| 199 | { |
| 200 | return bt_component_filter_get_input_port_count(libCompPtr); |
| 201 | } |
| 202 | |
| 203 | static const bt_port_input *portByIndex(const bt_component_filter * const libCompPtr, |
| 204 | const std::uint64_t index) noexcept |
| 205 | { |
| 206 | return bt_component_filter_borrow_input_port_by_index_const(libCompPtr, index); |
| 207 | } |
| 208 | |
| 209 | static const bt_port_input *portByName(const bt_component_filter * const libCompPtr, |
| 210 | const char * const name) noexcept |
| 211 | { |
| 212 | return bt_component_filter_borrow_input_port_by_name_const(libCompPtr, name); |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | template <> |
| 217 | struct ConstComponentPortsSpec<const bt_component_sink, const bt_port_input> final |
| 218 | { |
| 219 | static std::uint64_t portCount(const bt_component_sink * const libCompPtr) noexcept |
| 220 | { |
| 221 | return bt_component_sink_get_input_port_count(libCompPtr); |
| 222 | } |
| 223 | |
| 224 | static const bt_port_input *portByIndex(const bt_component_sink * const libCompPtr, |
| 225 | const std::uint64_t index) noexcept |
| 226 | { |
| 227 | return bt_component_sink_borrow_input_port_by_index_const(libCompPtr, index); |
| 228 | } |
| 229 | |
| 230 | static const bt_port_input *portByName(const bt_component_sink * const libCompPtr, |
| 231 | const char * const name) noexcept |
| 232 | { |
| 233 | return bt_component_sink_borrow_input_port_by_name_const(libCompPtr, name); |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | } /* namespace internal */ |
| 238 | |
| 239 | template <typename> |
| 240 | class ConstPort; |
| 241 | |
| 242 | template <typename LibCompT, typename LibPortT> |
| 243 | class ConstComponentPorts final : public BorrowedObject<LibCompT> |
| 244 | { |
| 245 | private: |
| 246 | using _Spec = internal::ConstComponentPortsSpec<LibCompT, LibPortT>; |
| 247 | |
| 248 | public: |
| 249 | using typename BorrowedObject<LibCompT>::LibObjPtr; |
| 250 | using Port = ConstPort<LibPortT>; |
| 251 | using Iterator = BorrowedObjectIterator<ConstComponentPorts>; |
| 252 | |
| 253 | explicit ConstComponentPorts(const LibObjPtr libObjPtr) noexcept : |
| 254 | BorrowedObject<LibCompT> {libObjPtr} |
| 255 | { |
| 256 | } |
| 257 | |
| 258 | std::uint64_t length() const noexcept |
| 259 | { |
| 260 | return _Spec::portCount(this->libObjPtr()); |
| 261 | } |
| 262 | |
| 263 | Port operator[](std::uint64_t index) const noexcept; |
| 264 | OptionalBorrowedObject<Port> operator[](bt2c::CStringView name) const noexcept; |
| 265 | Iterator begin() const noexcept; |
| 266 | Iterator end() const noexcept; |
| 267 | }; |
| 268 | |
| 269 | namespace internal { |
| 270 | |
| 271 | struct ConstSourceComponentRefFuncs final |
| 272 | { |
| 273 | static void get(const bt_component_source * const libObjPtr) noexcept |
| 274 | { |
| 275 | bt_component_source_get_ref(libObjPtr); |
| 276 | } |
| 277 | |
| 278 | static void put(const bt_component_source * const libObjPtr) noexcept |
| 279 | { |
| 280 | bt_component_source_put_ref(libObjPtr); |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | } /* namespace internal */ |
| 285 | |
| 286 | class ConstSourceComponent final : public ConstSpecificComponent<const bt_component_source> |
| 287 | { |
| 288 | public: |
| 289 | using Shared = SharedObject<ConstSourceComponent, const bt_component_source, |
| 290 | internal::ConstSourceComponentRefFuncs>; |
| 291 | |
| 292 | using OutputPorts = ConstComponentPorts<const bt_component_source, const bt_port_output>; |
| 293 | |
| 294 | explicit ConstSourceComponent(const bt_component_source * const libObjPtr) noexcept : |
| 295 | ConstSpecificComponent {libObjPtr} |
| 296 | { |
| 297 | } |
| 298 | |
| 299 | OutputPorts outputPorts() const noexcept; |
| 300 | |
| 301 | Shared shared() const noexcept |
| 302 | { |
| 303 | return Shared::createWithRef(*this); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | namespace internal { |
| 308 | |
| 309 | struct ConstFilterComponentRefFuncs final |
| 310 | { |
| 311 | static void get(const bt_component_filter * const libObjPtr) noexcept |
| 312 | { |
| 313 | bt_component_filter_get_ref(libObjPtr); |
| 314 | } |
| 315 | |
| 316 | static void put(const bt_component_filter * const libObjPtr) noexcept |
| 317 | { |
| 318 | bt_component_filter_put_ref(libObjPtr); |
| 319 | } |
| 320 | }; |
| 321 | |
| 322 | } /* namespace internal */ |
| 323 | |
| 324 | class ConstFilterComponent final : public ConstSpecificComponent<const bt_component_filter> |
| 325 | { |
| 326 | public: |
| 327 | using Shared = SharedObject<ConstFilterComponent, const bt_component_filter, |
| 328 | internal::ConstFilterComponentRefFuncs>; |
| 329 | |
| 330 | using InputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_input>; |
| 331 | using OutputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_output>; |
| 332 | |
| 333 | explicit ConstFilterComponent(const bt_component_filter * const libObjPtr) noexcept : |
| 334 | ConstSpecificComponent {libObjPtr} |
| 335 | { |
| 336 | } |
| 337 | |
| 338 | InputPorts inputPorts() const noexcept; |
| 339 | OutputPorts outputPorts() const noexcept; |
| 340 | |
| 341 | Shared shared() const noexcept |
| 342 | { |
| 343 | return Shared::createWithRef(*this); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | namespace internal { |
| 348 | |
| 349 | struct ConstSinkComponentRefFuncs final |
| 350 | { |
| 351 | static void get(const bt_component_sink * const libObjPtr) noexcept |
| 352 | { |
| 353 | bt_component_sink_get_ref(libObjPtr); |
| 354 | } |
| 355 | |
| 356 | static void put(const bt_component_sink * const libObjPtr) noexcept |
| 357 | { |
| 358 | bt_component_sink_put_ref(libObjPtr); |
| 359 | } |
| 360 | }; |
| 361 | |
| 362 | } /* namespace internal */ |
| 363 | |
| 364 | class ConstSinkComponent final : public ConstSpecificComponent<const bt_component_sink> |
| 365 | { |
| 366 | public: |
| 367 | using Shared = SharedObject<ConstSinkComponent, const bt_component_sink, |
| 368 | internal::ConstSinkComponentRefFuncs>; |
| 369 | |
| 370 | using InputPorts = ConstComponentPorts<const bt_component_sink, const bt_port_input>; |
| 371 | |
| 372 | explicit ConstSinkComponent(const bt_component_sink * const libObjPtr) noexcept : |
| 373 | ConstSpecificComponent {libObjPtr} |
| 374 | { |
| 375 | } |
| 376 | |
| 377 | InputPorts inputPorts() const noexcept; |
| 378 | |
| 379 | Shared shared() const noexcept |
| 380 | { |
| 381 | return Shared::createWithRef(*this); |
| 382 | } |
| 383 | }; |
| 384 | |
| 385 | inline ConstComponent::ConstComponent(const ConstSourceComponent other) noexcept : |
| 386 | ConstComponent {other.libObjPtr()} |
| 387 | { |
| 388 | } |
| 389 | |
| 390 | inline ConstComponent::ConstComponent(const ConstFilterComponent other) noexcept : |
| 391 | ConstComponent {other.libObjPtr()} |
| 392 | { |
| 393 | } |
| 394 | |
| 395 | inline ConstComponent::ConstComponent(const ConstSinkComponent other) noexcept : |
| 396 | ConstComponent {other.libObjPtr()} |
| 397 | { |
| 398 | } |
| 399 | |
| 400 | inline ConstComponent ConstComponent::operator=(const ConstSourceComponent other) noexcept |
| 401 | { |
| 402 | *this = ConstComponent {other.libObjPtr()}; |
| 403 | return *this; |
| 404 | } |
| 405 | |
| 406 | inline ConstComponent ConstComponent::operator=(const ConstFilterComponent other) noexcept |
| 407 | { |
| 408 | *this = ConstComponent {other.libObjPtr()}; |
| 409 | return *this; |
| 410 | } |
| 411 | |
| 412 | inline ConstComponent ConstComponent::operator=(const ConstSinkComponent other) noexcept |
| 413 | { |
| 414 | *this = ConstComponent {other.libObjPtr()}; |
| 415 | return *this; |
| 416 | } |
| 417 | |
| 418 | namespace internal { |
| 419 | |
| 420 | template <typename LibObjT> |
| 421 | struct ConstPortSpec; |
| 422 | |
| 423 | /* Functions specific to constant input ports */ |
| 424 | template <> |
| 425 | struct ConstPortSpec<const bt_port_input> final |
| 426 | { |
| 427 | static const bt_port *asPort(const bt_port_input * const libObjPtr) noexcept |
| 428 | { |
| 429 | return bt_port_input_as_port_const(libObjPtr); |
| 430 | } |
| 431 | }; |
| 432 | |
| 433 | /* Functions specific to constant output ports */ |
| 434 | template <> |
| 435 | struct ConstPortSpec<const bt_port_output> final |
| 436 | { |
| 437 | static const bt_port *asPort(const bt_port_output * const libObjPtr) noexcept |
| 438 | { |
| 439 | return bt_port_output_as_port_const(libObjPtr); |
| 440 | } |
| 441 | }; |
| 442 | |
| 443 | template <typename LibObjT> |
| 444 | struct ConstPortRefFuncs final |
| 445 | { |
| 446 | static void get(LibObjT * const libObjPtr) noexcept |
| 447 | { |
| 448 | bt_port_get_ref(ConstPortSpec<LibObjT>::port(libObjPtr)); |
| 449 | } |
| 450 | |
| 451 | static void put(LibObjT * const libObjPtr) noexcept |
| 452 | { |
| 453 | bt_port_put_ref(ConstPortSpec<LibObjT>::port(libObjPtr)); |
| 454 | } |
| 455 | }; |
| 456 | |
| 457 | } /* namespace internal */ |
| 458 | |
| 459 | template <typename LibObjT> |
| 460 | class ConstPort final : public BorrowedObject<LibObjT> |
| 461 | { |
| 462 | public: |
| 463 | using typename BorrowedObject<LibObjT>::LibObjPtr; |
| 464 | using Shared = SharedObject<ConstPort, LibObjT, internal::ConstPortRefFuncs<LibObjT>>; |
| 465 | |
| 466 | explicit ConstPort(const LibObjPtr libObjPtr) noexcept : BorrowedObject<LibObjT> {libObjPtr} |
| 467 | { |
| 468 | } |
| 469 | |
| 470 | bt2c::CStringView name() const noexcept |
| 471 | { |
| 472 | return bt_port_get_name(this->_libConstPortPtr()); |
| 473 | } |
| 474 | |
| 475 | bool isConnected() const noexcept |
| 476 | { |
| 477 | return static_cast<bool>(bt_port_is_connected(this->_libConstPortPtr())); |
| 478 | } |
| 479 | |
| 480 | ConstComponent component() const noexcept |
| 481 | { |
| 482 | return ConstComponent {bt_port_borrow_component_const(this->_libConstPortPtr())}; |
| 483 | } |
| 484 | |
| 485 | Shared shared() const noexcept |
| 486 | { |
| 487 | return Shared::createWithRef(*this); |
| 488 | } |
| 489 | |
| 490 | private: |
| 491 | const bt_port *_libConstPortPtr() const noexcept |
| 492 | { |
| 493 | return internal::ConstPortSpec<LibObjT>::asPort(this->libObjPtr()); |
| 494 | } |
| 495 | }; |
| 496 | |
| 497 | template <typename LibCompT, typename LibPortT> |
| 498 | typename ConstComponentPorts<LibCompT, LibPortT>::Port |
| 499 | ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::uint64_t index) const noexcept |
| 500 | { |
| 501 | return Port {_Spec::portByIndex(this->libObjPtr(), index)}; |
| 502 | } |
| 503 | |
| 504 | template <typename LibCompT, typename LibPortT> |
| 505 | OptionalBorrowedObject<typename ConstComponentPorts<LibCompT, LibPortT>::Port> |
| 506 | ConstComponentPorts<LibCompT, LibPortT>::operator[](const bt2c::CStringView name) const noexcept |
| 507 | { |
| 508 | return _Spec::portByName(this->libObjPtr(), name); |
| 509 | } |
| 510 | |
| 511 | template <typename LibCompT, typename LibPortT> |
| 512 | typename ConstComponentPorts<LibCompT, LibPortT>::Iterator |
| 513 | ConstComponentPorts<LibCompT, LibPortT>::begin() const noexcept |
| 514 | { |
| 515 | return Iterator {*this, 0}; |
| 516 | } |
| 517 | |
| 518 | template <typename LibCompT, typename LibPortT> |
| 519 | typename ConstComponentPorts<LibCompT, LibPortT>::Iterator |
| 520 | ConstComponentPorts<LibCompT, LibPortT>::end() const noexcept |
| 521 | { |
| 522 | return Iterator {*this, this->length()}; |
| 523 | } |
| 524 | |
| 525 | using ConstInputPort = ConstPort<const bt_port_input>; |
| 526 | using ConstOutputPort = ConstPort<const bt_port_output>; |
| 527 | |
| 528 | inline ConstSourceComponent::OutputPorts ConstSourceComponent::outputPorts() const noexcept |
| 529 | { |
| 530 | return OutputPorts {this->libObjPtr()}; |
| 531 | } |
| 532 | |
| 533 | inline ConstFilterComponent::OutputPorts ConstFilterComponent::outputPorts() const noexcept |
| 534 | { |
| 535 | return OutputPorts {this->libObjPtr()}; |
| 536 | } |
| 537 | |
| 538 | inline ConstFilterComponent::InputPorts ConstFilterComponent::inputPorts() const noexcept |
| 539 | { |
| 540 | return InputPorts {this->libObjPtr()}; |
| 541 | } |
| 542 | |
| 543 | inline ConstSinkComponent::InputPorts ConstSinkComponent::inputPorts() const noexcept |
| 544 | { |
| 545 | return InputPorts {this->libObjPtr()}; |
| 546 | } |
| 547 | |
| 548 | } /* namespace bt2 */ |
| 549 | |
| 550 | #endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP */ |