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 "cpp-common/bt2c/c-string-view.hpp"
18 #include "borrowed-object-iterator.hpp"
19 #include "borrowed-object.hpp"
20 #include "component-port.hpp"
21 #include "message-iterator.hpp"
25 class SelfSourceComponent;
26 class SelfFilterComponent;
27 class SelfSinkComponent;
29 class SelfComponent final : public BorrowedObject<bt_self_component>
32 explicit SelfComponent(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
36 explicit SelfComponent(bt_self_component_source * const libObjPtr) noexcept :
37 _ThisBorrowedObject {bt_self_component_source_as_self_component(libObjPtr)}
41 explicit SelfComponent(bt_self_component_filter * const libObjPtr) noexcept :
42 _ThisBorrowedObject {bt_self_component_filter_as_self_component(libObjPtr)}
46 explicit SelfComponent(bt_self_component_sink * const libObjPtr) noexcept :
47 _ThisBorrowedObject {bt_self_component_sink_as_self_component(libObjPtr)}
51 /* Not `explicit` to make them behave like copy constructors */
52 SelfComponent(SelfSourceComponent other) noexcept;
53 SelfComponent(SelfFilterComponent other) noexcept;
54 SelfComponent(SelfSinkComponent other) noexcept;
56 SelfComponent operator=(SelfSourceComponent other) noexcept;
57 SelfComponent operator=(SelfFilterComponent other) noexcept;
58 SelfComponent operator=(SelfSinkComponent other) noexcept;
60 ConstComponent asConstComponent() const noexcept
62 return ConstComponent {bt_self_component_as_component(this->libObjPtr())};
65 bool isSource() const noexcept
67 return this->asConstComponent().isSource();
70 bool isFilter() const noexcept
72 return this->asConstComponent().isFilter();
75 bool isSink() const noexcept
77 return this->asConstComponent().isSink();
80 bt2c::CStringView name() const noexcept
82 return this->asConstComponent().name();
85 LoggingLevel loggingLevel() const noexcept
87 return this->asConstComponent().loggingLevel();
90 std::uint64_t graphMipVersion() const noexcept
92 return bt_self_component_get_graph_mip_version(this->libObjPtr());
96 T& data() const noexcept
98 return *static_cast<T *>(bt_self_component_get_data(this->libObjPtr()));
101 template <typename T>
102 void data(T& obj) const noexcept
104 bt_self_component_set_data(this->libObjPtr(),
105 const_cast<void *>(static_cast<const void *>(&obj)));
108 bt2::TraceClass::Shared createTraceClass() const
110 const auto libObjPtr = bt_trace_class_create(this->libObjPtr());
113 throw MemoryError {};
116 return bt2::TraceClass::Shared::createWithoutRef(libObjPtr);
119 bt2::ClockClass::Shared createClockClass() const
121 const auto libObjPtr = bt_clock_class_create(this->libObjPtr());
124 throw MemoryError {};
127 return bt2::ClockClass::Shared::createWithoutRef(libObjPtr);
131 template <typename LibObjT>
132 class SelfSpecificComponent : public BorrowedObject<LibObjT>
135 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
138 using typename BorrowedObject<LibObjT>::LibObjPtr;
141 explicit SelfSpecificComponent(const LibObjPtr libObjPtr) noexcept :
142 _ThisBorrowedObject {libObjPtr}
146 template <typename PortT, typename LibPortT, typename AddPortFuncT, typename DataT>
147 PortT _addPort(const char * const name, DataT * const data, AddPortFuncT&& func) const
149 LibPortT *libPortPtr;
151 const auto status = func(this->libObjPtr(), name,
152 const_cast<void *>(static_cast<const void *>(data)), &libPortPtr);
155 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
156 return PortT {libPortPtr};
157 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
158 throw MemoryError {};
159 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
167 bt2c::CStringView name() const noexcept
169 return this->_selfComponent().name();
172 LoggingLevel loggingLevel() const noexcept
174 return this->_selfComponent().loggingLevel();
177 std::uint64_t graphMipVersion() const noexcept
179 return this->_selfComponent().graphMipVersion();
182 template <typename T>
183 T& data() const noexcept
185 return this->_selfComponent().template data<T>();
188 template <typename T>
189 void data(T& obj) const noexcept
191 this->_selfComponent().data(obj);
195 SelfComponent _selfComponent() const noexcept
197 return SelfComponent {this->libObjPtr()};
203 template <typename LibSelfCompT, typename LibSelfCompPortPtrT>
204 struct SelfComponentPortsSpec;
207 struct SelfComponentPortsSpec<bt_self_component_source, bt_self_component_port_output> final
209 static std::uint64_t portCount(bt_self_component_source * const libCompPtr) noexcept
211 return bt_component_source_get_output_port_count(
212 bt_self_component_source_as_component_source(libCompPtr));
215 static bt_self_component_port_output *portByIndex(bt_self_component_source * const libCompPtr,
216 const std::uint64_t index) noexcept
218 return bt_self_component_source_borrow_output_port_by_index(libCompPtr, index);
221 static bt_self_component_port_output *portByName(bt_self_component_source * const libCompPtr,
222 const char * const name) noexcept
224 return bt_self_component_source_borrow_output_port_by_name(libCompPtr, name);
229 struct SelfComponentPortsSpec<bt_self_component_filter, bt_self_component_port_output> final
231 static std::uint64_t portCount(bt_self_component_filter * const libCompPtr) noexcept
233 return bt_component_filter_get_output_port_count(
234 bt_self_component_filter_as_component_filter(libCompPtr));
237 static bt_self_component_port_output *portByIndex(bt_self_component_filter * const libCompPtr,
238 const std::uint64_t index) noexcept
240 return bt_self_component_filter_borrow_output_port_by_index(libCompPtr, index);
243 static bt_self_component_port_output *portByName(bt_self_component_filter * const libCompPtr,
244 const char * const name) noexcept
246 return bt_self_component_filter_borrow_output_port_by_name(libCompPtr, name);
251 struct SelfComponentPortsSpec<bt_self_component_filter, bt_self_component_port_input> final
253 static std::uint64_t portCount(bt_self_component_filter * const libCompPtr) noexcept
255 return bt_component_filter_get_input_port_count(
256 bt_self_component_filter_as_component_filter(libCompPtr));
259 static bt_self_component_port_input *portByIndex(bt_self_component_filter * const libCompPtr,
260 const std::uint64_t index) noexcept
262 return bt_self_component_filter_borrow_input_port_by_index(libCompPtr, index);
265 static bt_self_component_port_input *portByName(bt_self_component_filter * const libCompPtr,
266 const char * const name) noexcept
268 return bt_self_component_filter_borrow_input_port_by_name(libCompPtr, name);
273 struct SelfComponentPortsSpec<bt_self_component_sink, bt_self_component_port_input> final
275 static std::uint64_t portCount(bt_self_component_sink * const libCompPtr) noexcept
277 return bt_component_sink_get_input_port_count(
278 bt_self_component_sink_as_component_sink(libCompPtr));
281 static bt_self_component_port_input *portByIndex(bt_self_component_sink * const libCompPtr,
282 const std::uint64_t index) noexcept
284 return bt_self_component_sink_borrow_input_port_by_index(libCompPtr, index);
287 static bt_self_component_port_input *portByName(bt_self_component_sink * const libCompPtr,
288 const char * const name) noexcept
290 return bt_self_component_sink_borrow_input_port_by_name(libCompPtr, name);
294 } /* namespace internal */
296 template <typename LibSelfCompPortT, typename LibPortT>
297 class SelfComponentPort;
299 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
300 class SelfComponentPorts final : public BorrowedObject<LibSelfCompT>
303 using typename BorrowedObject<LibSelfCompT>::_ThisBorrowedObject;
304 using _Spec = internal::SelfComponentPortsSpec<LibSelfCompT, LibSelfCompPortT>;
307 using typename BorrowedObject<LibSelfCompT>::LibObjPtr;
308 using Port = SelfComponentPort<LibSelfCompPortT, LibPortT>;
309 using Iterator = BorrowedObjectIterator<SelfComponentPorts>;
311 explicit SelfComponentPorts(const LibObjPtr libObjPtr) noexcept :
312 _ThisBorrowedObject {libObjPtr}
316 std::uint64_t length() const noexcept
318 return _Spec::portCount(this->libObjPtr());
321 Port operator[](std::uint64_t index) const noexcept;
322 Port operator[](bt2c::CStringView name) const noexcept;
323 Iterator begin() const noexcept;
324 Iterator end() const noexcept;
325 Port front() const noexcept;
326 Port back() const noexcept;
329 class SelfSourceComponent final : public SelfSpecificComponent<bt_self_component_source>
332 using OutputPorts = SelfComponentPorts<bt_self_component_source, bt_self_component_port_output,
333 const bt_port_output>;
335 explicit SelfSourceComponent(bt_self_component_source * const libObjPtr) noexcept :
336 SelfSpecificComponent {libObjPtr}
340 ConstSourceComponent asConstComponent() const noexcept
342 return ConstSourceComponent {
343 bt_self_component_source_as_component_source(this->libObjPtr())};
346 template <typename DataT>
347 OutputPorts::Port addOutputPort(bt2c::CStringView name, DataT& data) const;
349 OutputPorts::Port addOutputPort(bt2c::CStringView name) const;
351 OutputPorts outputPorts() const noexcept;
354 template <typename DataT>
355 OutputPorts::Port _addOutputPort(const char *name, DataT *data) const;
358 class SelfFilterComponent final : public SelfSpecificComponent<bt_self_component_filter>
361 using InputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_input,
362 const bt_port_input>;
363 using OutputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_output,
364 const bt_port_output>;
366 explicit SelfFilterComponent(bt_self_component_filter * const libObjPtr) noexcept :
367 SelfSpecificComponent {libObjPtr}
371 ConstFilterComponent asConstComponent() const noexcept
373 return ConstFilterComponent {
374 bt_self_component_filter_as_component_filter(this->libObjPtr())};
377 template <typename DataT>
378 InputPorts::Port addInputPort(bt2c::CStringView name, DataT& data) const;
380 InputPorts::Port addInputPort(bt2c::CStringView name) const;
382 InputPorts inputPorts() const noexcept;
384 template <typename DataT>
385 OutputPorts::Port addOutputPort(bt2c::CStringView name, DataT& data) const;
387 OutputPorts::Port addOutputPort(bt2c::CStringView name) const;
389 OutputPorts outputPorts() const noexcept;
392 template <typename DataT>
393 InputPorts::Port _addInputPort(const char *name, DataT *data) const;
395 template <typename DataT>
396 OutputPorts::Port _addOutputPort(const char *name, DataT *data) const;
399 class SelfSinkComponent final : public SelfSpecificComponent<bt_self_component_sink>
402 using InputPorts = SelfComponentPorts<bt_self_component_sink, bt_self_component_port_input,
403 const bt_port_input>;
405 explicit SelfSinkComponent(bt_self_component_sink * const libObjPtr) noexcept :
406 SelfSpecificComponent {libObjPtr}
410 ConstSinkComponent asConstComponent() const noexcept
412 return ConstSinkComponent {bt_self_component_sink_as_component_sink(this->libObjPtr())};
415 MessageIterator::Shared createMessageIterator(InputPorts::Port port) const;
417 bool isInterrupted() const noexcept
419 return static_cast<bool>(bt_self_component_sink_is_interrupted(this->libObjPtr()));
422 template <typename DataT>
423 InputPorts::Port addInputPort(bt2c::CStringView name, DataT& data) const;
425 InputPorts::Port addInputPort(bt2c::CStringView name) const;
427 InputPorts inputPorts() const noexcept;
430 template <typename DataT>
431 InputPorts::Port _addInputPort(const char *name, DataT *data) const;
434 inline SelfComponent::SelfComponent(const SelfSourceComponent other) noexcept :
435 SelfComponent {other.libObjPtr()}
439 inline SelfComponent::SelfComponent(const SelfFilterComponent other) noexcept :
440 SelfComponent {other.libObjPtr()}
444 inline SelfComponent::SelfComponent(const SelfSinkComponent other) noexcept :
445 SelfComponent {other.libObjPtr()}
449 inline SelfComponent SelfComponent::operator=(const SelfSourceComponent other) noexcept
451 *this = SelfComponent {other.libObjPtr()};
455 inline SelfComponent SelfComponent::operator=(const SelfFilterComponent other) noexcept
457 *this = SelfComponent {other.libObjPtr()};
461 inline SelfComponent SelfComponent::operator=(const SelfSinkComponent other) noexcept
463 *this = SelfComponent {other.libObjPtr()};
469 template <typename LibObjT>
470 struct SelfComponentPortSpec;
472 /* Functions specific to self component input ports */
474 struct SelfComponentPortSpec<bt_self_component_port_input> final
476 static bt_self_component_port *
477 asSelfCompPort(bt_self_component_port_input * const libObjPtr) noexcept
479 return bt_self_component_port_input_as_self_component_port(libObjPtr);
482 static const bt_port_input *
483 asConstPort(const bt_self_component_port_input * const libObjPtr) noexcept
485 return bt_self_component_port_input_as_port_input(libObjPtr);
489 /* Functions specific to self component output ports */
491 struct SelfComponentPortSpec<bt_self_component_port_output> final
493 static bt_self_component_port *
494 asSelfCompPort(bt_self_component_port_output * const libObjPtr) noexcept
496 return bt_self_component_port_output_as_self_component_port(libObjPtr);
499 static const bt_port_output *
500 asConstPort(bt_self_component_port_output * const libObjPtr) noexcept
502 return bt_self_component_port_output_as_port_output(libObjPtr);
506 } /* namespace internal */
508 template <typename LibSelfCompPortT, typename LibPortT>
509 class SelfComponentPort final : public BorrowedObject<LibSelfCompPortT>
512 using typename BorrowedObject<LibSelfCompPortT>::LibObjPtr;
514 explicit SelfComponentPort(const LibObjPtr libObjPtr) noexcept :
515 BorrowedObject<LibSelfCompPortT> {libObjPtr}
519 ConstPort<LibPortT> asConstPort() const noexcept
521 return ConstPort<LibPortT> {
522 internal::SelfComponentPortSpec<LibSelfCompPortT>::asConstPort(this->libObjPtr())};
525 bt2c::CStringView name() const noexcept
527 return this->asConstPort().name();
530 bool isConnected() const noexcept
532 return this->asConstPort().isConnected();
535 SelfComponent component() const noexcept
537 return SelfComponent {bt_self_component_port_borrow_component(this->_libSelfCompPortPtr())};
540 template <typename T>
541 T& data() const noexcept
543 return *static_cast<T *>(bt_self_component_port_get_data(this->_libSelfCompPortPtr()));
547 bt_self_component_port *_libSelfCompPortPtr() const noexcept
549 return internal::SelfComponentPortSpec<LibSelfCompPortT>::asSelfCompPort(this->libObjPtr());
553 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
554 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
555 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[](
556 const std::uint64_t index) const noexcept
558 return Port {_Spec::portByIndex(this->libObjPtr(), index)};
561 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
562 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
563 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[](
564 const bt2c::CStringView name) const noexcept
566 return Port {_Spec::portByName(this->libObjPtr(), name)};
569 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
570 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Iterator
571 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::begin() const noexcept
573 return Iterator {*this, 0};
576 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
577 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Iterator
578 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::end() const noexcept
580 return Iterator {*this, this->length()};
583 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
584 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
585 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::front() const noexcept
590 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
591 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
592 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::back() const noexcept
594 return (*this)[this->length() - 1];
597 using SelfComponentInputPort = SelfComponentPort<bt_self_component_port_input, const bt_port_input>;
599 using SelfComponentOutputPort =
600 SelfComponentPort<bt_self_component_port_output, const bt_port_output>;
602 template <typename DataT>
603 SelfSourceComponent::OutputPorts::Port SelfSourceComponent::_addOutputPort(const char * const name,
604 DataT * const data) const
606 return this->_addPort<SelfSourceComponent::OutputPorts::Port, bt_self_component_port_output>(
607 name, data, bt_self_component_source_add_output_port);
610 template <typename DataT>
611 SelfSourceComponent::OutputPorts::Port
612 SelfSourceComponent::addOutputPort(const bt2c::CStringView name, DataT& data) const
614 return this->_addOutputPort(name, &data);
617 inline SelfSourceComponent::OutputPorts::Port
618 SelfSourceComponent::addOutputPort(const bt2c::CStringView name) const
620 return this->_addOutputPort<void>(name, nullptr);
623 inline SelfSourceComponent::OutputPorts SelfSourceComponent::outputPorts() const noexcept
625 return OutputPorts {this->libObjPtr()};
628 template <typename DataT>
629 SelfFilterComponent::OutputPorts::Port SelfFilterComponent::_addOutputPort(const char * const name,
630 DataT * const data) const
632 return this->_addPort<SelfFilterComponent::OutputPorts::Port, bt_self_component_port_output>(
633 name, data, bt_self_component_filter_add_output_port);
636 template <typename DataT>
637 SelfFilterComponent::OutputPorts::Port
638 SelfFilterComponent::addOutputPort(const bt2c::CStringView name, DataT& data) const
640 return this->_addOutputPort(name, &data);
643 inline SelfFilterComponent::OutputPorts::Port
644 SelfFilterComponent::addOutputPort(const bt2c::CStringView name) const
646 return this->_addOutputPort<void>(name, nullptr);
649 inline SelfFilterComponent::OutputPorts SelfFilterComponent::outputPorts() const noexcept
651 return OutputPorts {this->libObjPtr()};
654 template <typename DataT>
655 SelfFilterComponent::InputPorts::Port SelfFilterComponent::_addInputPort(const char * const name,
656 DataT * const data) const
658 return this->_addPort<SelfFilterComponent::InputPorts::Port, bt_self_component_port_input>(
659 name, data, bt_self_component_filter_add_input_port);
662 template <typename DataT>
663 SelfFilterComponent::InputPorts::Port
664 SelfFilterComponent::addInputPort(const bt2c::CStringView name, DataT& data) const
666 return this->_addInputPort(name, &data);
669 inline SelfFilterComponent::InputPorts::Port
670 SelfFilterComponent::addInputPort(const bt2c::CStringView name) const
672 return this->_addInputPort<void>(name, nullptr);
675 inline SelfFilterComponent::InputPorts SelfFilterComponent::inputPorts() const noexcept
677 return InputPorts {this->libObjPtr()};
680 inline MessageIterator::Shared
681 SelfSinkComponent::createMessageIterator(const InputPorts::Port port) const
683 bt_message_iterator *libMsgIterPtr = nullptr;
685 const auto status = bt_message_iterator_create_from_sink_component(
686 this->libObjPtr(), port.libObjPtr(), &libMsgIterPtr);
689 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK:
690 return MessageIterator::Shared::createWithoutRef(libMsgIterPtr);
691 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_MEMORY_ERROR:
692 throw MemoryError {};
693 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_ERROR:
700 template <typename DataT>
701 SelfSinkComponent::InputPorts::Port SelfSinkComponent::_addInputPort(const char * const name,
702 DataT * const data) const
704 return this->_addPort<SelfSinkComponent::InputPorts::Port, bt_self_component_port_input>(
705 name, data, bt_self_component_sink_add_input_port);
708 template <typename DataT>
709 SelfSinkComponent::InputPorts::Port SelfSinkComponent::addInputPort(const bt2c::CStringView name,
712 return this->_addInputPort(name, &data);
715 inline SelfSinkComponent::InputPorts::Port
716 SelfSinkComponent::addInputPort(const bt2c::CStringView name) const
718 return this->_addInputPort<void>(name, nullptr);
721 inline SelfSinkComponent::InputPorts SelfSinkComponent::inputPorts() const noexcept
723 return InputPorts {this->libObjPtr()};
726 } /* namespace bt2 */
728 #endif /* BABELTRACE_CPP_COMMON_BT2_SELF_COMPONENT_PORT_HPP */