2 * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP
13 #include <babeltrace2/babeltrace.h>
15 #include "logging.hpp"
17 #include "cpp-common/bt2c/c-string-view.hpp"
19 #include "borrowed-object-iterator.hpp"
20 #include "borrowed-object.hpp"
21 #include "shared-object.hpp"
26 struct ConstComponentRefFuncs final
28 static void get(const bt_component * const libObjPtr) noexcept
30 bt_component_get_ref(libObjPtr);
33 static void put(const bt_component * const libObjPtr) noexcept
35 bt_component_put_ref(libObjPtr);
39 } /* namespace internal */
41 class ConstSourceComponent;
42 class ConstFilterComponent;
43 class ConstSinkComponent;
45 class ConstComponent final : public BorrowedObject<const bt_component>
48 using typename BorrowedObject<const bt_component>::_ThisBorrowedObject;
52 SharedObject<ConstComponent, const bt_component, internal::ConstComponentRefFuncs>;
54 explicit ConstComponent(const bt_component * const libObjPtr) noexcept :
55 _ThisBorrowedObject {libObjPtr}
59 explicit ConstComponent(const bt_component_source * const libObjPtr) noexcept :
60 _ThisBorrowedObject {bt_component_source_as_component_const(libObjPtr)}
64 explicit ConstComponent(const bt_component_filter * const libObjPtr) noexcept :
65 _ThisBorrowedObject {bt_component_filter_as_component_const(libObjPtr)}
69 explicit ConstComponent(const bt_component_sink * const libObjPtr) noexcept :
70 _ThisBorrowedObject {bt_component_sink_as_component_const(libObjPtr)}
74 /* Not `explicit` to make them behave like copy constructors */
75 ConstComponent(ConstSourceComponent other) noexcept;
76 ConstComponent(ConstFilterComponent other) noexcept;
77 ConstComponent(ConstSinkComponent other) noexcept;
79 ConstComponent operator=(ConstSourceComponent other) noexcept;
80 ConstComponent operator=(ConstFilterComponent other) noexcept;
81 ConstComponent operator=(ConstSinkComponent other) noexcept;
83 bool isSource() const noexcept
85 return static_cast<bool>(bt_component_is_source(this->libObjPtr()));
88 bool isFilter() const noexcept
90 return static_cast<bool>(bt_component_is_filter(this->libObjPtr()));
93 bool isSink() const noexcept
95 return static_cast<bool>(bt_component_is_sink(this->libObjPtr()));
98 bt2c::CStringView name() const noexcept
100 return bt_component_get_name(this->libObjPtr());
103 LoggingLevel loggingLevel() const noexcept
105 return static_cast<LoggingLevel>(bt_component_get_logging_level(this->libObjPtr()));
108 Shared shared() const noexcept
110 return Shared::createWithRef(*this);
114 template <typename LibObjT>
115 class ConstSpecificComponent : public BorrowedObject<LibObjT>
118 using typename BorrowedObject<LibObjT>::LibObjPtr;
121 explicit ConstSpecificComponent(const LibObjPtr libObjPtr) noexcept :
122 BorrowedObject<LibObjT> {libObjPtr}
127 bt2c::CStringView name() const noexcept
129 return this->_constComponent().name();
132 LoggingLevel loggingLevel() const noexcept
134 return this->_constComponent().loggingLevel();
137 ConstComponent::Shared sharedComponent() const noexcept
139 return this->_constComponent().shared();
143 ConstComponent _constComponent() const noexcept
145 return ConstComponent {this->libObjPtr()};
151 template <typename LibCompT, typename LibPortPtrT>
152 struct ConstComponentPortsSpec;
155 struct ConstComponentPortsSpec<const bt_component_source, const bt_port_output> final
157 static std::uint64_t portCount(const bt_component_source * const libCompPtr) noexcept
159 return bt_component_source_get_output_port_count(libCompPtr);
162 static const bt_port_output *portByIndex(const bt_component_source * const libCompPtr,
163 const std::uint64_t index) noexcept
165 return bt_component_source_borrow_output_port_by_index_const(libCompPtr, index);
168 static const bt_port_output *portByName(const bt_component_source * const libCompPtr,
169 const char * const name) noexcept
171 return bt_component_source_borrow_output_port_by_name_const(libCompPtr, name);
176 struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_output> final
178 static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept
180 return bt_component_filter_get_output_port_count(libCompPtr);
183 static const bt_port_output *portByIndex(const bt_component_filter * const libCompPtr,
184 const std::uint64_t index) noexcept
186 return bt_component_filter_borrow_output_port_by_index_const(libCompPtr, index);
189 static const bt_port_output *portByName(const bt_component_filter * const libCompPtr,
190 const char * const name) noexcept
192 return bt_component_filter_borrow_output_port_by_name_const(libCompPtr, name);
197 struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_input> final
199 static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept
201 return bt_component_filter_get_input_port_count(libCompPtr);
204 static const bt_port_input *portByIndex(const bt_component_filter * const libCompPtr,
205 const std::uint64_t index) noexcept
207 return bt_component_filter_borrow_input_port_by_index_const(libCompPtr, index);
210 static const bt_port_input *portByName(const bt_component_filter * const libCompPtr,
211 const char * const name) noexcept
213 return bt_component_filter_borrow_input_port_by_name_const(libCompPtr, name);
218 struct ConstComponentPortsSpec<const bt_component_sink, const bt_port_input> final
220 static std::uint64_t portCount(const bt_component_sink * const libCompPtr) noexcept
222 return bt_component_sink_get_input_port_count(libCompPtr);
225 static const bt_port_input *portByIndex(const bt_component_sink * const libCompPtr,
226 const std::uint64_t index) noexcept
228 return bt_component_sink_borrow_input_port_by_index_const(libCompPtr, index);
231 static const bt_port_input *portByName(const bt_component_sink * const libCompPtr,
232 const char * const name) noexcept
234 return bt_component_sink_borrow_input_port_by_name_const(libCompPtr, name);
238 } /* namespace internal */
243 template <typename LibCompT, typename LibPortT>
244 class ConstComponentPorts final : public BorrowedObject<LibCompT>
247 using _Spec = internal::ConstComponentPortsSpec<LibCompT, LibPortT>;
250 using typename BorrowedObject<LibCompT>::LibObjPtr;
251 using Port = ConstPort<LibPortT>;
252 using Iterator = BorrowedObjectIterator<ConstComponentPorts>;
254 explicit ConstComponentPorts(const LibObjPtr libObjPtr) noexcept :
255 BorrowedObject<LibCompT> {libObjPtr}
259 std::uint64_t length() const noexcept
261 return _Spec::portCount(this->libObjPtr());
264 Port operator[](std::uint64_t index) const noexcept;
265 Port operator[](const char *name) const noexcept;
266 Port operator[](const std::string& name) const noexcept;
267 Iterator begin() const noexcept;
268 Iterator end() const noexcept;
273 struct ConstSourceComponentRefFuncs final
275 static void get(const bt_component_source * const libObjPtr) noexcept
277 bt_component_source_get_ref(libObjPtr);
280 static void put(const bt_component_source * const libObjPtr) noexcept
282 bt_component_source_put_ref(libObjPtr);
286 } /* namespace internal */
288 class ConstSourceComponent final : public ConstSpecificComponent<const bt_component_source>
291 using Shared = SharedObject<ConstSourceComponent, const bt_component_source,
292 internal::ConstSourceComponentRefFuncs>;
294 using OutputPorts = ConstComponentPorts<const bt_component_source, const bt_port_output>;
296 explicit ConstSourceComponent(const bt_component_source * const libObjPtr) noexcept :
297 ConstSpecificComponent {libObjPtr}
301 OutputPorts outputPorts() const noexcept;
303 Shared shared() const noexcept
305 return Shared::createWithRef(*this);
311 struct ConstFilterComponentRefFuncs final
313 static void get(const bt_component_filter * const libObjPtr) noexcept
315 bt_component_filter_get_ref(libObjPtr);
318 static void put(const bt_component_filter * const libObjPtr) noexcept
320 bt_component_filter_put_ref(libObjPtr);
324 } /* namespace internal */
326 class ConstFilterComponent final : public ConstSpecificComponent<const bt_component_filter>
329 using Shared = SharedObject<ConstFilterComponent, const bt_component_filter,
330 internal::ConstFilterComponentRefFuncs>;
332 using InputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_input>;
333 using OutputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_output>;
335 explicit ConstFilterComponent(const bt_component_filter * const libObjPtr) noexcept :
336 ConstSpecificComponent {libObjPtr}
340 InputPorts inputPorts() const noexcept;
341 OutputPorts outputPorts() const noexcept;
343 Shared shared() const noexcept
345 return Shared::createWithRef(*this);
351 struct ConstSinkComponentRefFuncs final
353 static void get(const bt_component_sink * const libObjPtr) noexcept
355 bt_component_sink_get_ref(libObjPtr);
358 static void put(const bt_component_sink * const libObjPtr) noexcept
360 bt_component_sink_put_ref(libObjPtr);
364 } /* namespace internal */
366 class ConstSinkComponent final : public ConstSpecificComponent<const bt_component_sink>
369 using Shared = SharedObject<ConstSinkComponent, const bt_component_sink,
370 internal::ConstSinkComponentRefFuncs>;
372 using InputPorts = ConstComponentPorts<const bt_component_sink, const bt_port_input>;
374 explicit ConstSinkComponent(const bt_component_sink * const libObjPtr) noexcept :
375 ConstSpecificComponent {libObjPtr}
379 InputPorts inputPorts() const noexcept;
381 Shared shared() const noexcept
383 return Shared::createWithRef(*this);
387 inline ConstComponent::ConstComponent(const ConstSourceComponent other) noexcept :
388 ConstComponent {other.libObjPtr()}
392 inline ConstComponent::ConstComponent(const ConstFilterComponent other) noexcept :
393 ConstComponent {other.libObjPtr()}
397 inline ConstComponent::ConstComponent(const ConstSinkComponent other) noexcept :
398 ConstComponent {other.libObjPtr()}
402 inline ConstComponent ConstComponent::operator=(const ConstSourceComponent other) noexcept
404 *this = ConstComponent {other.libObjPtr()};
408 inline ConstComponent ConstComponent::operator=(const ConstFilterComponent other) noexcept
410 *this = ConstComponent {other.libObjPtr()};
414 inline ConstComponent ConstComponent::operator=(const ConstSinkComponent other) noexcept
416 *this = ConstComponent {other.libObjPtr()};
422 template <typename LibObjT>
423 struct ConstPortSpec;
425 /* Functions specific to constant input ports */
427 struct ConstPortSpec<const bt_port_input> final
429 static const bt_port *asPort(const bt_port_input * const libObjPtr) noexcept
431 return bt_port_input_as_port_const(libObjPtr);
435 /* Functions specific to constant output ports */
437 struct ConstPortSpec<const bt_port_output> final
439 static const bt_port *asPort(const bt_port_output * const libObjPtr) noexcept
441 return bt_port_output_as_port_const(libObjPtr);
445 template <typename LibObjT>
446 struct ConstPortRefFuncs final
448 static void get(LibObjT * const libObjPtr) noexcept
450 bt_port_get_ref(ConstPortSpec<LibObjT>::port(libObjPtr));
453 static void put(LibObjT * const libObjPtr) noexcept
455 bt_port_put_ref(ConstPortSpec<LibObjT>::port(libObjPtr));
459 } /* namespace internal */
461 template <typename LibObjT>
462 class ConstPort final : public BorrowedObject<LibObjT>
465 using typename BorrowedObject<LibObjT>::LibObjPtr;
466 using Shared = SharedObject<ConstPort, LibObjT, internal::ConstPortRefFuncs<LibObjT>>;
468 explicit ConstPort(const LibObjPtr libObjPtr) noexcept : BorrowedObject<LibObjT> {libObjPtr}
472 bt2c::CStringView name() const noexcept
474 return bt_port_get_name(this->_libConstPortPtr());
477 bool isConnected() const noexcept
479 return static_cast<bool>(bt_port_is_connected(this->_libConstPortPtr()));
482 ConstComponent component() const noexcept
484 return ConstComponent {bt_port_borrow_component_const(this->_libConstPortPtr())};
487 Shared shared() const noexcept
489 return Shared::createWithRef(*this);
493 const bt_port *_libConstPortPtr() const noexcept
495 return internal::ConstPortSpec<LibObjT>::asPort(this->libObjPtr());
499 template <typename LibCompT, typename LibPortT>
500 typename ConstComponentPorts<LibCompT, LibPortT>::Port
501 ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::uint64_t index) const noexcept
503 return Port {_Spec::portByIndex(this->libObjPtr(), index)};
506 template <typename LibCompT, typename LibPortT>
507 typename ConstComponentPorts<LibCompT, LibPortT>::Port
508 ConstComponentPorts<LibCompT, LibPortT>::operator[](const char * const name) const noexcept
510 return Port {_Spec::portByName(this->libObjPtr(), name)};
513 template <typename LibCompT, typename LibPortT>
514 typename ConstComponentPorts<LibCompT, LibPortT>::Port
515 ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::string& name) const noexcept
517 return (*this)[name.data()];
520 template <typename LibCompT, typename LibPortT>
521 typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
522 ConstComponentPorts<LibCompT, LibPortT>::begin() const noexcept
524 return Iterator {*this, 0};
527 template <typename LibCompT, typename LibPortT>
528 typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
529 ConstComponentPorts<LibCompT, LibPortT>::end() const noexcept
531 return Iterator {*this, this->length()};
534 using ConstInputPort = ConstPort<const bt_port_input>;
535 using ConstOutputPort = ConstPort<const bt_port_output>;
537 inline ConstSourceComponent::OutputPorts ConstSourceComponent::outputPorts() const noexcept
539 return OutputPorts {this->libObjPtr()};
542 inline ConstFilterComponent::OutputPorts ConstFilterComponent::outputPorts() const noexcept
544 return OutputPorts {this->libObjPtr()};
547 inline ConstFilterComponent::InputPorts ConstFilterComponent::inputPorts() const noexcept
549 return InputPorts {this->libObjPtr()};
552 inline ConstSinkComponent::InputPorts ConstSinkComponent::inputPorts() const noexcept
554 return InputPorts {this->libObjPtr()};
557 } /* namespace bt2 */
559 #endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP */