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 "borrowed-object-iterator.hpp"
18 #include "borrowed-object.hpp"
19 #include "shared-object.hpp"
24 struct ConstComponentRefFuncs final
26 static void get(const bt_component * const libObjPtr) noexcept
28 bt_component_get_ref(libObjPtr);
31 static void put(const bt_component * const libObjPtr) noexcept
33 bt_component_put_ref(libObjPtr);
37 } /* namespace internal */
39 class ConstSourceComponent;
40 class ConstFilterComponent;
41 class ConstSinkComponent;
43 class ConstComponent final : public BorrowedObject<const bt_component>
46 using typename BorrowedObject<const bt_component>::_ThisBorrowedObject;
50 SharedObject<ConstComponent, const bt_component, internal::ConstComponentRefFuncs>;
52 explicit ConstComponent(const bt_component * const libObjPtr) noexcept :
53 _ThisBorrowedObject {libObjPtr}
57 explicit ConstComponent(const bt_component_source * const libObjPtr) noexcept :
58 _ThisBorrowedObject {bt_component_source_as_component_const(libObjPtr)}
62 explicit ConstComponent(const bt_component_filter * const libObjPtr) noexcept :
63 _ThisBorrowedObject {bt_component_filter_as_component_const(libObjPtr)}
67 explicit ConstComponent(const bt_component_sink * const libObjPtr) noexcept :
68 _ThisBorrowedObject {bt_component_sink_as_component_const(libObjPtr)}
72 /* Not `explicit` to make them behave like copy constructors */
73 ConstComponent(ConstSourceComponent other) noexcept;
74 ConstComponent(ConstFilterComponent other) noexcept;
75 ConstComponent(ConstSinkComponent other) noexcept;
77 ConstComponent operator=(ConstSourceComponent other) noexcept;
78 ConstComponent operator=(ConstFilterComponent other) noexcept;
79 ConstComponent operator=(ConstSinkComponent other) noexcept;
81 bool isSource() const noexcept
83 return static_cast<bool>(bt_component_is_source(this->libObjPtr()));
86 bool isFilter() const noexcept
88 return static_cast<bool>(bt_component_is_filter(this->libObjPtr()));
91 bool isSink() const noexcept
93 return static_cast<bool>(bt_component_is_sink(this->libObjPtr()));
96 const char *name() const noexcept
98 return bt_component_get_name(this->libObjPtr());
101 LoggingLevel loggingLevel() const noexcept
103 return static_cast<LoggingLevel>(bt_component_get_logging_level(this->libObjPtr()));
106 Shared shared() const noexcept
108 return Shared::createWithRef(*this);
112 template <typename LibObjT>
113 class ConstSpecificComponent : public BorrowedObject<LibObjT>
116 using typename BorrowedObject<LibObjT>::_LibObjPtr;
119 explicit ConstSpecificComponent(const _LibObjPtr libObjPtr) noexcept :
120 BorrowedObject<LibObjT> {libObjPtr}
125 const char *name() const noexcept
127 return this->_constComponent().name();
130 LoggingLevel loggingLevel() const noexcept
132 return this->_constComponent().loggingLevel();
135 ConstComponent::Shared sharedComponent() const noexcept
137 return this->_constComponent().shared();
141 ConstComponent _constComponent() const noexcept
143 return ConstComponent {this->libObjPtr()};
149 template <typename LibCompT, typename LibPortPtrT>
150 struct ConstComponentPortsSpec;
153 struct ConstComponentPortsSpec<const bt_component_source, const bt_port_output> final
155 static std::uint64_t portCount(const bt_component_source * const libCompPtr) noexcept
157 return bt_component_source_get_output_port_count(libCompPtr);
160 static const bt_port_output *portByIndex(const bt_component_source * const libCompPtr,
161 const std::uint64_t index) noexcept
163 return bt_component_source_borrow_output_port_by_index_const(libCompPtr, index);
166 static const bt_port_output *portByName(const bt_component_source * const libCompPtr,
167 const char * const name) noexcept
169 return bt_component_source_borrow_output_port_by_name_const(libCompPtr, name);
174 struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_output> final
176 static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept
178 return bt_component_filter_get_output_port_count(libCompPtr);
181 static const bt_port_output *portByIndex(const bt_component_filter * const libCompPtr,
182 const std::uint64_t index) noexcept
184 return bt_component_filter_borrow_output_port_by_index_const(libCompPtr, index);
187 static const bt_port_output *portByName(const bt_component_filter * const libCompPtr,
188 const char * const name) noexcept
190 return bt_component_filter_borrow_output_port_by_name_const(libCompPtr, name);
195 struct ConstComponentPortsSpec<const bt_component_filter, const bt_port_input> final
197 static std::uint64_t portCount(const bt_component_filter * const libCompPtr) noexcept
199 return bt_component_filter_get_input_port_count(libCompPtr);
202 static const bt_port_input *portByIndex(const bt_component_filter * const libCompPtr,
203 const std::uint64_t index) noexcept
205 return bt_component_filter_borrow_input_port_by_index_const(libCompPtr, index);
208 static const bt_port_input *portByName(const bt_component_filter * const libCompPtr,
209 const char * const name) noexcept
211 return bt_component_filter_borrow_input_port_by_name_const(libCompPtr, name);
216 struct ConstComponentPortsSpec<const bt_component_sink, const bt_port_input> final
218 static std::uint64_t portCount(const bt_component_sink * const libCompPtr) noexcept
220 return bt_component_sink_get_input_port_count(libCompPtr);
223 static const bt_port_input *portByIndex(const bt_component_sink * const libCompPtr,
224 const std::uint64_t index) noexcept
226 return bt_component_sink_borrow_input_port_by_index_const(libCompPtr, index);
229 static const bt_port_input *portByName(const bt_component_sink * const libCompPtr,
230 const char * const name) noexcept
232 return bt_component_sink_borrow_input_port_by_name_const(libCompPtr, name);
236 } /* namespace internal */
241 template <typename LibCompT, typename LibPortT>
242 class ConstComponentPorts final : public BorrowedObject<LibCompT>
245 using _Spec = internal::ConstComponentPortsSpec<LibCompT, LibPortT>;
248 using typename BorrowedObject<LibCompT>::_LibObjPtr;
249 using Port = ConstPort<LibPortT>;
250 using Iterator = BorrowedObjectIterator<ConstComponentPorts>;
252 explicit ConstComponentPorts(const _LibObjPtr libObjPtr) noexcept :
253 BorrowedObject<LibCompT> {libObjPtr}
257 std::uint64_t length() const noexcept
259 return _Spec::portCount(this->libObjPtr());
262 Port operator[](std::uint64_t index) const noexcept;
263 Port operator[](const char *name) const noexcept;
264 Port operator[](const std::string& 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 const char *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 char * const name) const noexcept
508 return Port {_Spec::portByName(this->libObjPtr(), name)};
511 template <typename LibCompT, typename LibPortT>
512 typename ConstComponentPorts<LibCompT, LibPortT>::Port
513 ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::string& name) const noexcept
515 return (*this)[name.data()];
518 template <typename LibCompT, typename LibPortT>
519 typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
520 ConstComponentPorts<LibCompT, LibPortT>::begin() const noexcept
522 return Iterator {*this, 0};
525 template <typename LibCompT, typename LibPortT>
526 typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
527 ConstComponentPorts<LibCompT, LibPortT>::end() const noexcept
529 return Iterator {*this, this->length()};
532 using ConstInputPort = ConstPort<const bt_port_input>;
533 using ConstOutputPort = ConstPort<const bt_port_output>;
535 inline ConstSourceComponent::OutputPorts ConstSourceComponent::outputPorts() const noexcept
537 return OutputPorts {this->libObjPtr()};
540 inline ConstFilterComponent::OutputPorts ConstFilterComponent::outputPorts() const noexcept
542 return OutputPorts {this->libObjPtr()};
545 inline ConstFilterComponent::InputPorts ConstFilterComponent::inputPorts() const noexcept
547 return InputPorts {this->libObjPtr()};
550 inline ConstSinkComponent::InputPorts ConstSinkComponent::inputPorts() const noexcept
552 return InputPorts {this->libObjPtr()};
555 } /* namespace bt2 */
557 #endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP */