2 * Copyright (c) 2023 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_SELF_COMPONENT_PORT_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_SELF_COMPONENT_PORT_HPP
12 #include <babeltrace2/babeltrace.h>
14 #include "logging.hpp"
16 #include "common/assert.h"
17 #include "cpp-common/bt2c/c-string-view.hpp"
19 #include "borrowed-object-iterator.hpp"
20 #include "borrowed-object.hpp"
21 #include "component-port.hpp"
22 #include "message-iterator.hpp"
26 class SelfSourceComponent;
27 class SelfFilterComponent;
28 class SelfSinkComponent;
30 class SelfComponent final : public BorrowedObject<bt_self_component>
33 explicit SelfComponent(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
37 explicit SelfComponent(bt_self_component_source * const libObjPtr) noexcept :
38 _ThisBorrowedObject {bt_self_component_source_as_self_component(libObjPtr)}
42 explicit SelfComponent(bt_self_component_filter * const libObjPtr) noexcept :
43 _ThisBorrowedObject {bt_self_component_filter_as_self_component(libObjPtr)}
47 explicit SelfComponent(bt_self_component_sink * const libObjPtr) noexcept :
48 _ThisBorrowedObject {bt_self_component_sink_as_self_component(libObjPtr)}
52 /* Not `explicit` to make them behave like copy constructors */
53 SelfComponent(SelfSourceComponent other) noexcept;
54 SelfComponent(SelfFilterComponent other) noexcept;
55 SelfComponent(SelfSinkComponent other) noexcept;
57 SelfComponent operator=(SelfSourceComponent other) noexcept;
58 SelfComponent operator=(SelfFilterComponent other) noexcept;
59 SelfComponent operator=(SelfSinkComponent other) noexcept;
61 ConstComponent asConstComponent() const noexcept
63 return ConstComponent {bt_self_component_as_component(this->libObjPtr())};
66 bool isSource() const noexcept
68 return this->asConstComponent().isSource();
71 bool isFilter() const noexcept
73 return this->asConstComponent().isFilter();
76 bool isSink() const noexcept
78 return this->asConstComponent().isSink();
81 bt2c::CStringView name() const noexcept
83 return this->asConstComponent().name();
86 LoggingLevel loggingLevel() const noexcept
88 return this->asConstComponent().loggingLevel();
91 std::uint64_t graphMipVersion() const noexcept
93 return bt_self_component_get_graph_mip_version(this->libObjPtr());
97 T& data() const noexcept
99 return *static_cast<T *>(bt_self_component_get_data(this->libObjPtr()));
102 template <typename T>
103 void data(T& obj) const noexcept
105 bt_self_component_set_data(this->libObjPtr(), static_cast<void *>(&obj));
109 template <typename LibObjT>
110 class SelfSpecificComponent : public BorrowedObject<LibObjT>
113 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
116 using typename BorrowedObject<LibObjT>::LibObjPtr;
119 explicit SelfSpecificComponent(const LibObjPtr libObjPtr) noexcept :
120 _ThisBorrowedObject {libObjPtr}
124 template <typename PortT, typename LibPortT, typename AddPortFuncT, typename DataT>
125 PortT _addPort(const char * const name, DataT * const data, AddPortFuncT&& func) const
127 LibPortT *libPortPtr;
129 const auto status = func(this->libObjPtr(), name, static_cast<void *>(data), &libPortPtr);
132 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
133 return PortT {libPortPtr};
134 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
135 throw MemoryError {};
136 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
144 bt2c::CStringView name() const noexcept
146 return this->_selfComponent().name();
149 LoggingLevel loggingLevel() const noexcept
151 return this->_selfComponent().loggingLevel();
154 std::uint64_t graphMipVersion() const noexcept
156 return this->_selfComponent().graphMipVersion();
159 template <typename T>
160 T& data() const noexcept
162 return this->_selfComponent().template data<T>();
165 template <typename T>
166 void data(T& obj) const noexcept
168 this->_selfComponent().data(obj);
172 SelfComponent _selfComponent() const noexcept
174 return SelfComponent {this->libObjPtr()};
180 template <typename LibSelfCompT, typename LibSelfCompPortPtrT>
181 struct SelfComponentPortsSpec;
184 struct SelfComponentPortsSpec<bt_self_component_source, bt_self_component_port_output> final
186 static std::uint64_t portCount(bt_self_component_source * const libCompPtr) noexcept
188 return bt_component_source_get_output_port_count(
189 bt_self_component_source_as_component_source(libCompPtr));
192 static bt_self_component_port_output *portByIndex(bt_self_component_source * const libCompPtr,
193 const std::uint64_t index) noexcept
195 return bt_self_component_source_borrow_output_port_by_index(libCompPtr, index);
198 static bt_self_component_port_output *portByName(bt_self_component_source * const libCompPtr,
199 const char * const name) noexcept
201 return bt_self_component_source_borrow_output_port_by_name(libCompPtr, name);
206 struct SelfComponentPortsSpec<bt_self_component_filter, bt_self_component_port_output> final
208 static std::uint64_t portCount(bt_self_component_filter * const libCompPtr) noexcept
210 return bt_component_filter_get_output_port_count(
211 bt_self_component_filter_as_component_filter(libCompPtr));
214 static bt_self_component_port_output *portByIndex(bt_self_component_filter * const libCompPtr,
215 const std::uint64_t index) noexcept
217 return bt_self_component_filter_borrow_output_port_by_index(libCompPtr, index);
220 static bt_self_component_port_output *portByName(bt_self_component_filter * const libCompPtr,
221 const char * const name) noexcept
223 return bt_self_component_filter_borrow_output_port_by_name(libCompPtr, name);
228 struct SelfComponentPortsSpec<bt_self_component_filter, bt_self_component_port_input> final
230 static std::uint64_t portCount(bt_self_component_filter * const libCompPtr) noexcept
232 return bt_component_filter_get_input_port_count(
233 bt_self_component_filter_as_component_filter(libCompPtr));
236 static bt_self_component_port_input *portByIndex(bt_self_component_filter * const libCompPtr,
237 const std::uint64_t index) noexcept
239 return bt_self_component_filter_borrow_input_port_by_index(libCompPtr, index);
242 static bt_self_component_port_input *portByName(bt_self_component_filter * const libCompPtr,
243 const char * const name) noexcept
245 return bt_self_component_filter_borrow_input_port_by_name(libCompPtr, name);
250 struct SelfComponentPortsSpec<bt_self_component_sink, bt_self_component_port_input> final
252 static std::uint64_t portCount(bt_self_component_sink * const libCompPtr) noexcept
254 return bt_component_sink_get_input_port_count(
255 bt_self_component_sink_as_component_sink(libCompPtr));
258 static bt_self_component_port_input *portByIndex(bt_self_component_sink * const libCompPtr,
259 const std::uint64_t index) noexcept
261 return bt_self_component_sink_borrow_input_port_by_index(libCompPtr, index);
264 static bt_self_component_port_input *portByName(bt_self_component_sink * const libCompPtr,
265 const char * const name) noexcept
267 return bt_self_component_sink_borrow_input_port_by_name(libCompPtr, name);
271 } /* namespace internal */
273 template <typename LibSelfCompPortT, typename LibPortT>
274 class SelfComponentPort;
276 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
277 class SelfComponentPorts final : public BorrowedObject<LibSelfCompT>
280 using typename BorrowedObject<LibSelfCompT>::_ThisBorrowedObject;
281 using _Spec = internal::SelfComponentPortsSpec<LibSelfCompT, LibSelfCompPortT>;
284 using typename BorrowedObject<LibSelfCompT>::LibObjPtr;
285 using Port = SelfComponentPort<LibSelfCompPortT, LibPortT>;
286 using Iterator = BorrowedObjectIterator<SelfComponentPorts>;
288 explicit SelfComponentPorts(const LibObjPtr libObjPtr) noexcept :
289 _ThisBorrowedObject {libObjPtr}
293 std::uint64_t length() const noexcept
295 return _Spec::portCount(this->libObjPtr());
298 Port operator[](std::uint64_t index) const noexcept;
299 Port operator[](bt2c::CStringView name) const noexcept;
300 Iterator begin() const noexcept;
301 Iterator end() const noexcept;
302 Port front() const noexcept;
303 Port back() const noexcept;
306 class SelfSourceComponent final : public SelfSpecificComponent<bt_self_component_source>
309 using OutputPorts = SelfComponentPorts<bt_self_component_source, bt_self_component_port_output,
310 const bt_port_output>;
312 explicit SelfSourceComponent(bt_self_component_source * const libObjPtr) noexcept :
313 SelfSpecificComponent {libObjPtr}
317 ConstSourceComponent asConstComponent() const noexcept
319 return ConstSourceComponent {
320 bt_self_component_source_as_component_source(this->libObjPtr())};
323 template <typename DataT>
324 OutputPorts::Port addOutputPort(bt2c::CStringView name, DataT& data) const;
326 OutputPorts::Port addOutputPort(bt2c::CStringView name) const;
328 OutputPorts outputPorts() const noexcept;
331 template <typename DataT>
332 OutputPorts::Port _addOutputPort(const char *name, DataT *data) const;
335 class SelfFilterComponent final : public SelfSpecificComponent<bt_self_component_filter>
338 using InputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_input,
339 const bt_port_input>;
340 using OutputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_output,
341 const bt_port_output>;
343 explicit SelfFilterComponent(bt_self_component_filter * const libObjPtr) noexcept :
344 SelfSpecificComponent {libObjPtr}
348 ConstFilterComponent asConstComponent() const noexcept
350 return ConstFilterComponent {
351 bt_self_component_filter_as_component_filter(this->libObjPtr())};
354 template <typename DataT>
355 InputPorts::Port addInputPort(bt2c::CStringView name, DataT& data) const;
357 InputPorts::Port addInputPort(bt2c::CStringView name) const;
359 InputPorts inputPorts() const noexcept;
361 template <typename DataT>
362 OutputPorts::Port addOutputPort(bt2c::CStringView name, DataT& data) const;
364 OutputPorts::Port addOutputPort(bt2c::CStringView name) const;
366 OutputPorts outputPorts() const noexcept;
369 template <typename DataT>
370 InputPorts::Port _addInputPort(const char *name, DataT *data) const;
372 template <typename DataT>
373 OutputPorts::Port _addOutputPort(const char *name, DataT *data) const;
376 class SelfSinkComponent final : public SelfSpecificComponent<bt_self_component_sink>
379 using InputPorts = SelfComponentPorts<bt_self_component_sink, bt_self_component_port_input,
380 const bt_port_input>;
382 explicit SelfSinkComponent(bt_self_component_sink * const libObjPtr) noexcept :
383 SelfSpecificComponent {libObjPtr}
387 ConstSinkComponent asConstComponent() const noexcept
389 return ConstSinkComponent {bt_self_component_sink_as_component_sink(this->libObjPtr())};
392 MessageIterator::Shared createMessageIterator(InputPorts::Port port) const;
394 bool isInterrupted() const noexcept
396 return static_cast<bool>(bt_self_component_sink_is_interrupted(this->libObjPtr()));
399 template <typename DataT>
400 InputPorts::Port addInputPort(bt2c::CStringView name, DataT& data) const;
402 InputPorts::Port addInputPort(bt2c::CStringView name) const;
404 InputPorts inputPorts() const noexcept;
407 template <typename DataT>
408 InputPorts::Port _addInputPort(const char *name, DataT *data) const;
411 inline SelfComponent::SelfComponent(const SelfSourceComponent other) noexcept :
412 SelfComponent {other.libObjPtr()}
416 inline SelfComponent::SelfComponent(const SelfFilterComponent other) noexcept :
417 SelfComponent {other.libObjPtr()}
421 inline SelfComponent::SelfComponent(const SelfSinkComponent other) noexcept :
422 SelfComponent {other.libObjPtr()}
426 inline SelfComponent SelfComponent::operator=(const SelfSourceComponent other) noexcept
428 *this = SelfComponent {other.libObjPtr()};
432 inline SelfComponent SelfComponent::operator=(const SelfFilterComponent other) noexcept
434 *this = SelfComponent {other.libObjPtr()};
438 inline SelfComponent SelfComponent::operator=(const SelfSinkComponent other) noexcept
440 *this = SelfComponent {other.libObjPtr()};
446 template <typename LibObjT>
447 struct SelfComponentPortSpec;
449 /* Functions specific to self component input ports */
451 struct SelfComponentPortSpec<bt_self_component_port_input> final
453 static bt_self_component_port *
454 asSelfCompPort(bt_self_component_port_input * const libObjPtr) noexcept
456 return bt_self_component_port_input_as_self_component_port(libObjPtr);
459 static const bt_port_input *
460 asConstPort(const bt_self_component_port_input * const libObjPtr) noexcept
462 return bt_self_component_port_input_as_port_input(libObjPtr);
466 /* Functions specific to self component output ports */
468 struct SelfComponentPortSpec<bt_self_component_port_output> final
470 static bt_self_component_port *
471 asSelfCompPort(bt_self_component_port_output * const libObjPtr) noexcept
473 return bt_self_component_port_output_as_self_component_port(libObjPtr);
476 static const bt_port_output *
477 asConstPort(bt_self_component_port_output * const libObjPtr) noexcept
479 return bt_self_component_port_output_as_port_output(libObjPtr);
483 } /* namespace internal */
485 template <typename LibSelfCompPortT, typename LibPortT>
486 class SelfComponentPort final : public BorrowedObject<LibSelfCompPortT>
489 using typename BorrowedObject<LibSelfCompPortT>::LibObjPtr;
491 explicit SelfComponentPort(const LibObjPtr libObjPtr) noexcept :
492 BorrowedObject<LibSelfCompPortT> {libObjPtr}
496 ConstPort<LibPortT> asConstPort() const noexcept
498 return ConstPort<LibPortT> {
499 internal::SelfComponentPortSpec<LibSelfCompPortT>::asConstPort(this->libObjPtr())};
502 bt2c::CStringView name() const noexcept
504 return this->asConstPort().name();
507 bool isConnected() const noexcept
509 return this->asConstPort().isConnected();
512 SelfComponent component() const noexcept
514 return SelfComponent {bt_self_component_port_borrow_component(this->_libSelfCompPortPtr())};
517 template <typename T>
518 T& data() const noexcept
520 *static_cast<T *>(bt_self_component_port_get_data(this->_libSelfCompPortPtr()));
524 bt_self_component_port *_libSelfCompPortPtr() const noexcept
526 return internal::SelfComponentPortSpec<LibSelfCompPortT>::asSelfCompPort(this->libObjPtr());
530 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
531 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
532 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[](
533 const std::uint64_t index) const noexcept
535 return Port {_Spec::portByIndex(this->libObjPtr(), index)};
538 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
539 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
540 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[](
541 const bt2c::CStringView name) const noexcept
543 return Port {_Spec::portByName(this->libObjPtr(), name)};
546 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
547 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Iterator
548 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::begin() const noexcept
550 return Iterator {*this, 0};
553 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
554 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Iterator
555 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::end() const noexcept
557 return Iterator {*this, this->length()};
560 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
561 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
562 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::front() const noexcept
567 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
568 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
569 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::back() const noexcept
571 return (*this)[this->length() - 1];
574 using SelfComponentInputPort = SelfComponentPort<bt_self_component_port_input, const bt_port_input>;
576 using SelfComponentOutputPort =
577 SelfComponentPort<bt_self_component_port_output, const bt_port_output>;
579 template <typename DataT>
580 SelfSourceComponent::OutputPorts::Port SelfSourceComponent::_addOutputPort(const char * const name,
581 DataT * const data) const
583 return this->_addPort<SelfSourceComponent::OutputPorts::Port, bt_self_component_port_output>(
584 name, data, bt_self_component_source_add_output_port);
587 template <typename DataT>
588 SelfSourceComponent::OutputPorts::Port
589 SelfSourceComponent::addOutputPort(const bt2c::CStringView name, DataT& data) const
591 return this->_addOutputPort(name, &data);
594 inline SelfSourceComponent::OutputPorts::Port
595 SelfSourceComponent::addOutputPort(const bt2c::CStringView name) const
597 return this->_addOutputPort<void>(name, nullptr);
600 inline SelfSourceComponent::OutputPorts SelfSourceComponent::outputPorts() const noexcept
602 return OutputPorts {this->libObjPtr()};
605 template <typename DataT>
606 SelfFilterComponent::OutputPorts::Port SelfFilterComponent::_addOutputPort(const char * const name,
607 DataT * const data) const
609 return this->_addPort<SelfFilterComponent::OutputPorts::Port, bt_self_component_port_output>(
610 name, data, bt_self_component_filter_add_output_port);
613 template <typename DataT>
614 SelfFilterComponent::OutputPorts::Port
615 SelfFilterComponent::addOutputPort(const bt2c::CStringView name, DataT& data) const
617 return this->_addOutputPort(name, &data);
620 inline SelfFilterComponent::OutputPorts::Port
621 SelfFilterComponent::addOutputPort(const bt2c::CStringView name) const
623 return this->_addOutputPort<void>(name, nullptr);
626 inline SelfFilterComponent::OutputPorts SelfFilterComponent::outputPorts() const noexcept
628 return OutputPorts {this->libObjPtr()};
631 template <typename DataT>
632 SelfFilterComponent::InputPorts::Port SelfFilterComponent::_addInputPort(const char * const name,
633 DataT * const data) const
635 return this->_addPort<SelfFilterComponent::InputPorts::Port, bt_self_component_port_input>(
636 name, data, bt_self_component_filter_add_input_port);
639 template <typename DataT>
640 SelfFilterComponent::InputPorts::Port
641 SelfFilterComponent::addInputPort(const bt2c::CStringView name, DataT& data) const
643 return this->_addInputPort(name, &data);
646 inline SelfFilterComponent::InputPorts::Port
647 SelfFilterComponent::addInputPort(const bt2c::CStringView name) const
649 return this->_addInputPort<void>(name, nullptr);
652 inline SelfFilterComponent::InputPorts SelfFilterComponent::inputPorts() const noexcept
654 return InputPorts {this->libObjPtr()};
657 inline MessageIterator::Shared
658 SelfSinkComponent::createMessageIterator(const InputPorts::Port port) const
660 bt_message_iterator *libMsgIterPtr = nullptr;
662 const auto status = bt_message_iterator_create_from_sink_component(
663 this->libObjPtr(), port.libObjPtr(), &libMsgIterPtr);
666 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK:
667 BT_ASSERT(libMsgIterPtr);
668 return MessageIterator::Shared::createWithoutRef(libMsgIterPtr);
669 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_MEMORY_ERROR:
670 throw MemoryError {};
671 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_ERROR:
678 template <typename DataT>
679 SelfSinkComponent::InputPorts::Port SelfSinkComponent::_addInputPort(const char * const name,
680 DataT * const data) const
682 return this->_addPort<SelfSinkComponent::InputPorts::Port, bt_self_component_port_input>(
683 name, data, bt_self_component_sink_add_input_port);
686 template <typename DataT>
687 SelfSinkComponent::InputPorts::Port SelfSinkComponent::addInputPort(const bt2c::CStringView name,
690 return this->_addInputPort(name, &data);
693 inline SelfSinkComponent::InputPorts::Port
694 SelfSinkComponent::addInputPort(const bt2c::CStringView name) const
696 return this->_addInputPort<void>(name, nullptr);
699 inline SelfSinkComponent::InputPorts SelfSinkComponent::inputPorts() const noexcept
701 return InputPorts {this->libObjPtr()};
704 } /* namespace bt2 */
706 #endif /* BABELTRACE_CPP_COMMON_BT2_SELF_COMPONENT_PORT_HPP */