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[](bt2c::CStringView name) const noexcept;
266 Iterator begin() const noexcept;
267 Iterator end() const noexcept;
272 struct ConstSourceComponentRefFuncs final
274 static void get(const bt_component_source * const libObjPtr) noexcept
276 bt_component_source_get_ref(libObjPtr);
279 static void put(const bt_component_source * const libObjPtr) noexcept
281 bt_component_source_put_ref(libObjPtr);
285 } /* namespace internal */
287 class ConstSourceComponent final : public ConstSpecificComponent<const bt_component_source>
290 using Shared = SharedObject<ConstSourceComponent, const bt_component_source,
291 internal::ConstSourceComponentRefFuncs>;
293 using OutputPorts = ConstComponentPorts<const bt_component_source, const bt_port_output>;
295 explicit ConstSourceComponent(const bt_component_source * const libObjPtr) noexcept :
296 ConstSpecificComponent {libObjPtr}
300 OutputPorts outputPorts() const noexcept;
302 Shared shared() const noexcept
304 return Shared::createWithRef(*this);
310 struct ConstFilterComponentRefFuncs final
312 static void get(const bt_component_filter * const libObjPtr) noexcept
314 bt_component_filter_get_ref(libObjPtr);
317 static void put(const bt_component_filter * const libObjPtr) noexcept
319 bt_component_filter_put_ref(libObjPtr);
323 } /* namespace internal */
325 class ConstFilterComponent final : public ConstSpecificComponent<const bt_component_filter>
328 using Shared = SharedObject<ConstFilterComponent, const bt_component_filter,
329 internal::ConstFilterComponentRefFuncs>;
331 using InputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_input>;
332 using OutputPorts = ConstComponentPorts<const bt_component_filter, const bt_port_output>;
334 explicit ConstFilterComponent(const bt_component_filter * const libObjPtr) noexcept :
335 ConstSpecificComponent {libObjPtr}
339 InputPorts inputPorts() const noexcept;
340 OutputPorts outputPorts() const noexcept;
342 Shared shared() const noexcept
344 return Shared::createWithRef(*this);
350 struct ConstSinkComponentRefFuncs final
352 static void get(const bt_component_sink * const libObjPtr) noexcept
354 bt_component_sink_get_ref(libObjPtr);
357 static void put(const bt_component_sink * const libObjPtr) noexcept
359 bt_component_sink_put_ref(libObjPtr);
363 } /* namespace internal */
365 class ConstSinkComponent final : public ConstSpecificComponent<const bt_component_sink>
368 using Shared = SharedObject<ConstSinkComponent, const bt_component_sink,
369 internal::ConstSinkComponentRefFuncs>;
371 using InputPorts = ConstComponentPorts<const bt_component_sink, const bt_port_input>;
373 explicit ConstSinkComponent(const bt_component_sink * const libObjPtr) noexcept :
374 ConstSpecificComponent {libObjPtr}
378 InputPorts inputPorts() const noexcept;
380 Shared shared() const noexcept
382 return Shared::createWithRef(*this);
386 inline ConstComponent::ConstComponent(const ConstSourceComponent other) noexcept :
387 ConstComponent {other.libObjPtr()}
391 inline ConstComponent::ConstComponent(const ConstFilterComponent other) noexcept :
392 ConstComponent {other.libObjPtr()}
396 inline ConstComponent::ConstComponent(const ConstSinkComponent other) noexcept :
397 ConstComponent {other.libObjPtr()}
401 inline ConstComponent ConstComponent::operator=(const ConstSourceComponent other) noexcept
403 *this = ConstComponent {other.libObjPtr()};
407 inline ConstComponent ConstComponent::operator=(const ConstFilterComponent other) noexcept
409 *this = ConstComponent {other.libObjPtr()};
413 inline ConstComponent ConstComponent::operator=(const ConstSinkComponent other) noexcept
415 *this = ConstComponent {other.libObjPtr()};
421 template <typename LibObjT>
422 struct ConstPortSpec;
424 /* Functions specific to constant input ports */
426 struct ConstPortSpec<const bt_port_input> final
428 static const bt_port *asPort(const bt_port_input * const libObjPtr) noexcept
430 return bt_port_input_as_port_const(libObjPtr);
434 /* Functions specific to constant output ports */
436 struct ConstPortSpec<const bt_port_output> final
438 static const bt_port *asPort(const bt_port_output * const libObjPtr) noexcept
440 return bt_port_output_as_port_const(libObjPtr);
444 template <typename LibObjT>
445 struct ConstPortRefFuncs final
447 static void get(LibObjT * const libObjPtr) noexcept
449 bt_port_get_ref(ConstPortSpec<LibObjT>::port(libObjPtr));
452 static void put(LibObjT * const libObjPtr) noexcept
454 bt_port_put_ref(ConstPortSpec<LibObjT>::port(libObjPtr));
458 } /* namespace internal */
460 template <typename LibObjT>
461 class ConstPort final : public BorrowedObject<LibObjT>
464 using typename BorrowedObject<LibObjT>::LibObjPtr;
465 using Shared = SharedObject<ConstPort, LibObjT, internal::ConstPortRefFuncs<LibObjT>>;
467 explicit ConstPort(const LibObjPtr libObjPtr) noexcept : BorrowedObject<LibObjT> {libObjPtr}
471 bt2c::CStringView name() const noexcept
473 return bt_port_get_name(this->_libConstPortPtr());
476 bool isConnected() const noexcept
478 return static_cast<bool>(bt_port_is_connected(this->_libConstPortPtr()));
481 ConstComponent component() const noexcept
483 return ConstComponent {bt_port_borrow_component_const(this->_libConstPortPtr())};
486 Shared shared() const noexcept
488 return Shared::createWithRef(*this);
492 const bt_port *_libConstPortPtr() const noexcept
494 return internal::ConstPortSpec<LibObjT>::asPort(this->libObjPtr());
498 template <typename LibCompT, typename LibPortT>
499 typename ConstComponentPorts<LibCompT, LibPortT>::Port
500 ConstComponentPorts<LibCompT, LibPortT>::operator[](const std::uint64_t index) const noexcept
502 return Port {_Spec::portByIndex(this->libObjPtr(), index)};
505 template <typename LibCompT, typename LibPortT>
506 typename ConstComponentPorts<LibCompT, LibPortT>::Port
507 ConstComponentPorts<LibCompT, LibPortT>::operator[](const bt2c::CStringView name) const noexcept
509 return Port {_Spec::portByName(this->libObjPtr(), name)};
512 template <typename LibCompT, typename LibPortT>
513 typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
514 ConstComponentPorts<LibCompT, LibPortT>::begin() const noexcept
516 return Iterator {*this, 0};
519 template <typename LibCompT, typename LibPortT>
520 typename ConstComponentPorts<LibCompT, LibPortT>::Iterator
521 ConstComponentPorts<LibCompT, LibPortT>::end() const noexcept
523 return Iterator {*this, this->length()};
526 using ConstInputPort = ConstPort<const bt_port_input>;
527 using ConstOutputPort = ConstPort<const bt_port_output>;
529 inline ConstSourceComponent::OutputPorts ConstSourceComponent::outputPorts() const noexcept
531 return OutputPorts {this->libObjPtr()};
534 inline ConstFilterComponent::OutputPorts ConstFilterComponent::outputPorts() const noexcept
536 return OutputPorts {this->libObjPtr()};
539 inline ConstFilterComponent::InputPorts ConstFilterComponent::inputPorts() const noexcept
541 return InputPorts {this->libObjPtr()};
544 inline ConstSinkComponent::InputPorts ConstSinkComponent::inputPorts() const noexcept
546 return InputPorts {this->libObjPtr()};
549 } /* namespace bt2 */
551 #endif /* BABELTRACE_CPP_COMMON_BT2_COMPONENT_PORT_HPP */