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
12 #include <babeltrace2/babeltrace.h>
14 #include "logging.hpp"
16 #include "cpp-common/bt2c/c-string-view.hpp"
18 #include "borrowed-object-iterator.hpp"
19 #include "borrowed-object.hpp"
20 #include "shared-object.hpp"
25 struct ConstComponentRefFuncs final
27 static void get(const bt_component * const libObjPtr) noexcept
29 bt_component_get_ref(libObjPtr);
32 static void put(const bt_component * const libObjPtr) noexcept
34 bt_component_put_ref(libObjPtr);
38 } /* namespace internal */
40 class ConstSourceComponent;
41 class ConstFilterComponent;
42 class ConstSinkComponent;
44 class ConstComponent final : public BorrowedObject<const bt_component>
47 using typename BorrowedObject<const bt_component>::_ThisBorrowedObject;
51 SharedObject<ConstComponent, const bt_component, internal::ConstComponentRefFuncs>;
53 explicit ConstComponent(const bt_component * const libObjPtr) noexcept :
54 _ThisBorrowedObject {libObjPtr}
58 explicit ConstComponent(const bt_component_source * const libObjPtr) noexcept :
59 _ThisBorrowedObject {bt_component_source_as_component_const(libObjPtr)}
63 explicit ConstComponent(const bt_component_filter * const libObjPtr) noexcept :
64 _ThisBorrowedObject {bt_component_filter_as_component_const(libObjPtr)}
68 explicit ConstComponent(const bt_component_sink * const libObjPtr) noexcept :
69 _ThisBorrowedObject {bt_component_sink_as_component_const(libObjPtr)}
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;
78 ConstComponent operator=(ConstSourceComponent other) noexcept;
79 ConstComponent operator=(ConstFilterComponent other) noexcept;
80 ConstComponent operator=(ConstSinkComponent other) noexcept;
82 bool isSource() const noexcept
84 return static_cast<bool>(bt_component_is_source(this->libObjPtr()));
87 bool isFilter() const noexcept
89 return static_cast<bool>(bt_component_is_filter(this->libObjPtr()));
92 bool isSink() const noexcept
94 return static_cast<bool>(bt_component_is_sink(this->libObjPtr()));
97 bt2c::CStringView name() const noexcept
99 return bt_component_get_name(this->libObjPtr());
102 LoggingLevel loggingLevel() const noexcept
104 return static_cast<LoggingLevel>(bt_component_get_logging_level(this->libObjPtr()));
107 Shared shared() const noexcept
109 return Shared::createWithRef(*this);
113 template <typename LibObjT>
114 class ConstSpecificComponent : public BorrowedObject<LibObjT>
117 using typename BorrowedObject<LibObjT>::LibObjPtr;
120 explicit ConstSpecificComponent(const LibObjPtr libObjPtr) noexcept :
121 BorrowedObject<LibObjT> {libObjPtr}
126 bt2c::CStringView name() const noexcept
128 return this->_constComponent().name();
131 LoggingLevel loggingLevel() const noexcept
133 return this->_constComponent().loggingLevel();
136 ConstComponent::Shared sharedComponent() const noexcept
138 return this->_constComponent().shared();
142 ConstComponent _constComponent() const noexcept
144 return ConstComponent {this->libObjPtr()};
150 template <typename LibCompT, typename LibPortPtrT>
151 struct ConstComponentPortsSpec;
154 struct ConstComponentPortsSpec<const bt_component_source, const bt_port_output> final
156 static std::uint64_t portCount(const bt_component_source * const libCompPtr) noexcept
158 return bt_component_source_get_output_port_count(libCompPtr);
161 static const bt_port_output *portByIndex(const bt_component_source * const libCompPtr,
162 const std::uint64_t index) noexcept
164 return bt_component_source_borrow_output_port_by_index_const(libCompPtr, index);
167 static const bt_port_output *portByName(const bt_component_source * const libCompPtr,
168 const char * const name) noexcept
170 return bt_component_source_borrow_output_port_by_name_const(libCompPtr, name);
175 struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_output> final
177 static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept
179 return bt_component_filter_get_output_port_count(libCompPtr);
182 static const bt_port_output *portByIndex(const bt_component_filter * const libCompPtr,
183 const std::uint64_t index) noexcept
185 return bt_component_filter_borrow_output_port_by_index_const(libCompPtr, index);
188 static const bt_port_output *portByName(const bt_component_filter * const libCompPtr,
189 const char * const name) noexcept
191 return bt_component_filter_borrow_output_port_by_name_const(libCompPtr, name);
196 struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_input> final
198 static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept
200 return bt_component_filter_get_input_port_count(libCompPtr);
203 static const bt_port_input *portByIndex(const bt_component_filter * const libCompPtr,
204 const std::uint64_t index) noexcept
206 return bt_component_filter_borrow_input_port_by_index_const(libCompPtr, index);
209 static const bt_port_input *portByName(const bt_component_filter * const libCompPtr,
210 const char * const name) noexcept
212 return bt_component_filter_borrow_input_port_by_name_const(libCompPtr, name);
217 struct ConstComponentPortsSpec<const bt_component_sink, const bt_port_input> final
219 static std::uint64_t portCount(const bt_component_sink * const libCompPtr) noexcept
221 return bt_component_sink_get_input_port_count(libCompPtr);
224 static const bt_port_input *portByIndex(const bt_component_sink * const libCompPtr,
225 const std::uint64_t index) noexcept
227 return bt_component_sink_borrow_input_port_by_index_const(libCompPtr, index);
230 static const bt_port_input *portByName(const bt_component_sink * const libCompPtr,
231 const char * const name) noexcept
233 return bt_component_sink_borrow_input_port_by_name_const(libCompPtr, name);
237 } /* namespace internal */
242 template <typename LibCompT, typename LibPortT>
243 class ConstComponentPorts final : public BorrowedObject<LibCompT>
246 using _Spec = internal::ConstComponentPortsSpec<LibCompT, LibPortT>;
249 using typename BorrowedObject<LibCompT>::LibObjPtr;
250 using Port = ConstPort<LibPortT>;
251 using Iterator = BorrowedObjectIterator<ConstComponentPorts>;
253 explicit ConstComponentPorts(const LibObjPtr libObjPtr) noexcept :
254 BorrowedObject<LibCompT> {libObjPtr}
258 std::uint64_t length() const noexcept
260 return _Spec::portCount(this->libObjPtr());
263 Port operator[](std::uint64_t index) const noexcept;
264 Port operator[](bt2c::CStringView name) const noexcept;
265 Iterator begin() const noexcept;
266 Iterator end() const noexcept;
271 struct ConstSourceComponentRefFuncs final
273 static void get(const bt_component_source * const libObjPtr) noexcept
275 bt_component_source_get_ref(libObjPtr);
278 static void put(const bt_component_source * const libObjPtr) noexcept
280 bt_component_source_put_ref(libObjPtr);
284 } /* namespace internal */
286 class ConstSourceComponent final : public ConstSpecificComponent<const bt_component_source>
289 using Shared = SharedObject<ConstSourceComponent, const bt_component_source,
290 internal::ConstSourceComponentRefFuncs>;
292 using OutputPorts = ConstComponentPorts<const bt_component_source, const bt_port_output>;
294 explicit ConstSourceComponent(const bt_component_source * const libObjPtr) noexcept :
295 ConstSpecificComponent {libObjPtr}
299 OutputPorts outputPorts() const noexcept;
301 Shared shared() const noexcept
303 return Shared::createWithRef(*this);
309 struct ConstFilterComponentRefFuncs final
311 static void get(const bt_component_filter * const libObjPtr) noexcept
313 bt_component_filter_get_ref(libObjPtr);
316 static void put(const bt_component_filter * const libObjPtr) noexcept
318 bt_component_filter_put_ref(libObjPtr);
322 } /* namespace internal */
324 class ConstFilterComponent final : public ConstSpecificComponent<const bt_component_filter>
327 using Shared = SharedObject<ConstFilterComponent, const bt_component_filter,
328 internal::ConstFilterComponentRefFuncs>;
330 using InputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_input>;
331 using OutputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_output>;
333 explicit ConstFilterComponent(const bt_component_filter * const libObjPtr) noexcept :
334 ConstSpecificComponent {libObjPtr}
338 InputPorts inputPorts() const noexcept;
339 OutputPorts outputPorts() const noexcept;
341 Shared shared() const noexcept
343 return Shared::createWithRef(*this);
349 struct ConstSinkComponentRefFuncs final
351 static void get(const bt_component_sink * const libObjPtr) noexcept
353 bt_component_sink_get_ref(libObjPtr);
356 static void put(const bt_component_sink * const libObjPtr) noexcept
358 bt_component_sink_put_ref(libObjPtr);
362 } /* namespace internal */
364 class ConstSinkComponent final : public ConstSpecificComponent<const bt_component_sink>
367 using Shared = SharedObject<ConstSinkComponent, const bt_component_sink,
368 internal::ConstSinkComponentRefFuncs>;
370 using InputPorts = ConstComponentPorts<const bt_component_sink, const bt_port_input>;
372 explicit ConstSinkComponent(const bt_component_sink * const libObjPtr) noexcept :
373 ConstSpecificComponent {libObjPtr}
377 InputPorts inputPorts() const noexcept;
379 Shared shared() const noexcept
381 return Shared::createWithRef(*this);
385 inline ConstComponent::ConstComponent(const ConstSourceComponent other) noexcept :
386 ConstComponent {other.libObjPtr()}
390 inline ConstComponent::ConstComponent(const ConstFilterComponent other) noexcept :
391 ConstComponent {other.libObjPtr()}
395 inline ConstComponent::ConstComponent(const ConstSinkComponent other) noexcept :
396 ConstComponent {other.libObjPtr()}
400 inline ConstComponent ConstComponent::operator=(const ConstSourceComponent other) noexcept
402 *this = ConstComponent {other.libObjPtr()};
406 inline ConstComponent ConstComponent::operator=(const ConstFilterComponent other) noexcept
408 *this = ConstComponent {other.libObjPtr()};
412 inline ConstComponent ConstComponent::operator=(const ConstSinkComponent other) noexcept
414 *this = ConstComponent {other.libObjPtr()};
420 template <typename LibObjT>
421 struct ConstPortSpec;
423 /* Functions specific to constant input ports */
425 struct ConstPortSpec<const bt_port_input> final
427 static const bt_port *asPort(const bt_port_input * const libObjPtr) noexcept
429 return bt_port_input_as_port_const(libObjPtr);
433 /* Functions specific to constant output ports */
435 struct ConstPortSpec<const bt_port_output> final
437 static const bt_port *asPort(const bt_port_output * const libObjPtr) noexcept
439 return bt_port_output_as_port_const(libObjPtr);
443 template <typename LibObjT>
444 struct ConstPortRefFuncs final
446 static void get(LibObjT * const libObjPtr) noexcept
448 bt_port_get_ref(ConstPortSpec<LibObjT>::port(libObjPtr));
451 static void put(LibObjT * const libObjPtr) noexcept
453 bt_port_put_ref(ConstPortSpec<LibObjT>::port(libObjPtr));
457 } /* namespace internal */
459 template <typename LibObjT>
460 class ConstPort final : public BorrowedObject<LibObjT>
463 using typename BorrowedObject<LibObjT>::LibObjPtr;
464 using Shared = SharedObject<ConstPort, LibObjT, internal::ConstPortRefFuncs<LibObjT>>;
466 explicit ConstPort(const LibObjPtr libObjPtr) noexcept : BorrowedObject<LibObjT> {libObjPtr}
470 bt2c::CStringView name() const noexcept
472 return bt_port_get_name(this->_libConstPortPtr());
475 bool isConnected() const noexcept
477 return static_cast<bool>(bt_port_is_connected(this->_libConstPortPtr()));
480 ConstComponent component() const noexcept
482 return ConstComponent {bt_port_borrow_component_const(this->_libConstPortPtr())};
485 Shared shared() const noexcept
487 return Shared::createWithRef(*this);
491 const bt_port *_libConstPortPtr() const noexcept
493 return internal::ConstPortSpec<LibObjT>::asPort(this->libObjPtr());
497 template <typename LibCompT, typename LibPortT>
498 typename ConstComponentPorts<LibCompT, LibPortT>::Port
499 ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::uint64_t index) const noexcept
501 return Port {_Spec::portByIndex(this->libObjPtr(), index)};
504 template <typename LibCompT, typename LibPortT>
505 typename ConstComponentPorts<LibCompT, LibPortT>::Port
506 ConstComponentPorts<LibCompT, LibPortT>::operator[](const bt2c::CStringView name) const noexcept
508 return Port {_Spec::portByName(this->libObjPtr(), name)};
511 template <typename LibCompT, typename LibPortT>
512 typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
513 ConstComponentPorts<LibCompT, LibPortT>::begin() const noexcept
515 return Iterator {*this, 0};
518 template <typename LibCompT, typename LibPortT>
519 typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
520 ConstComponentPorts<LibCompT, LibPortT>::end() const noexcept
522 return Iterator {*this, this->length()};
525 using ConstInputPort = ConstPort<const bt_port_input>;
526 using ConstOutputPort = ConstPort<const bt_port_output>;
528 inline ConstSourceComponent::OutputPorts ConstSourceComponent::outputPorts() const noexcept
530 return OutputPorts {this->libObjPtr()};
533 inline ConstFilterComponent::OutputPorts ConstFilterComponent::outputPorts() const noexcept
535 return OutputPorts {this->libObjPtr()};
538 inline ConstFilterComponent::InputPorts ConstFilterComponent::inputPorts() const noexcept
540 return InputPorts {this->libObjPtr()};
543 inline ConstSinkComponent::InputPorts ConstSinkComponent::inputPorts() const noexcept
545 return InputPorts {this->libObjPtr()};
548 } /* namespace bt2 */
550 #endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP */