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
13 #include <babeltrace2/babeltrace.h>
15 #include "logging.hpp"
17 #include "common/assert.h"
18 #include "cpp-common/bt2c/c-string-view.hpp"
20 #include "borrowed-object-iterator.hpp"
21 #include "borrowed-object.hpp"
22 #include "component-port.hpp"
23 #include "message-iterator.hpp"
27 class SelfSourceComponent;
28 class SelfFilterComponent;
29 class SelfSinkComponent;
31 class SelfComponent final : public BorrowedObject<bt_self_component>
34 explicit SelfComponent(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr}
38 explicit SelfComponent(bt_self_component_source * const libObjPtr) noexcept :
39 _ThisBorrowedObject {bt_self_component_source_as_self_component(libObjPtr)}
43 explicit SelfComponent(bt_self_component_filter * const libObjPtr) noexcept :
44 _ThisBorrowedObject {bt_self_component_filter_as_self_component(libObjPtr)}
48 explicit SelfComponent(bt_self_component_sink * const libObjPtr) noexcept :
49 _ThisBorrowedObject {bt_self_component_sink_as_self_component(libObjPtr)}
53 /* Not `explicit` to make them behave like copy constructors */
54 SelfComponent(SelfSourceComponent other) noexcept;
55 SelfComponent(SelfFilterComponent other) noexcept;
56 SelfComponent(SelfSinkComponent other) noexcept;
58 SelfComponent operator=(SelfSourceComponent other) noexcept;
59 SelfComponent operator=(SelfFilterComponent other) noexcept;
60 SelfComponent operator=(SelfSinkComponent other) noexcept;
62 ConstComponent asConstComponent() const noexcept
64 return ConstComponent {bt_self_component_as_component(this->libObjPtr())};
67 bool isSource() const noexcept
69 return this->asConstComponent().isSource();
72 bool isFilter() const noexcept
74 return this->asConstComponent().isFilter();
77 bool isSink() const noexcept
79 return this->asConstComponent().isSink();
82 bt2c::CStringView name() const noexcept
84 return this->asConstComponent().name();
87 LoggingLevel loggingLevel() const noexcept
89 return this->asConstComponent().loggingLevel();
92 std::uint64_t graphMipVersion() const noexcept
94 return bt_self_component_get_graph_mip_version(this->libObjPtr());
98 T& data() const noexcept
100 return *static_cast<T *>(bt_self_component_get_data(this->libObjPtr()));
103 template <typename T>
104 void data(T& obj) const noexcept
106 bt_self_component_set_data(this->libObjPtr(), static_cast<void *>(&obj));
110 template <typename LibObjT>
111 class SelfSpecificComponent : public BorrowedObject<LibObjT>
114 using typename BorrowedObject<LibObjT>::_ThisBorrowedObject;
117 using typename BorrowedObject<LibObjT>::LibObjPtr;
120 explicit SelfSpecificComponent(const LibObjPtr libObjPtr) noexcept :
121 _ThisBorrowedObject {libObjPtr}
125 template <typename PortT, typename LibPortT, typename AddPortFuncT, typename DataT>
126 PortT _addPort(const char * const name, DataT * const data, AddPortFuncT&& func) const
128 LibPortT *libPortPtr;
130 const auto status = func(this->libObjPtr(), name, static_cast<void *>(data), &libPortPtr);
133 case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
134 return PortT {libPortPtr};
135 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
136 throw MemoryError {};
137 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
145 bt2c::CStringView name() const noexcept
147 return this->_selfComponent().name();
150 LoggingLevel loggingLevel() const noexcept
152 return this->_selfComponent().loggingLevel();
155 std::uint64_t graphMipVersion() const noexcept
157 return this->_selfComponent().graphMipVersion();
160 template <typename T>
161 T& data() const noexcept
163 return this->_selfComponent().template data<T>();
166 template <typename T>
167 void data(T& obj) const noexcept
169 this->_selfComponent().data(obj);
173 SelfComponent _selfComponent() const noexcept
175 return SelfComponent {this->libObjPtr()};
181 template <typename LibSelfCompT, typename LibSelfCompPortPtrT>
182 struct SelfComponentPortsSpec;
185 struct SelfComponentPortsSpec<bt_self_component_source, bt_self_component_port_output> final
187 static std::uint64_t portCount(bt_self_component_source * const libCompPtr) noexcept
189 return bt_component_source_get_output_port_count(
190 bt_self_component_source_as_component_source(libCompPtr));
193 static bt_self_component_port_output *portByIndex(bt_self_component_source * const libCompPtr,
194 const std::uint64_t index) noexcept
196 return bt_self_component_source_borrow_output_port_by_index(libCompPtr, index);
199 static bt_self_component_port_output *portByName(bt_self_component_source * const libCompPtr,
200 const char * const name) noexcept
202 return bt_self_component_source_borrow_output_port_by_name(libCompPtr, name);
207 struct SelfComponentPortsSpec<bt_self_component_filter, bt_self_component_port_output> final
209 static std::uint64_t portCount(bt_self_component_filter * const libCompPtr) noexcept
211 return bt_component_filter_get_output_port_count(
212 bt_self_component_filter_as_component_filter(libCompPtr));
215 static bt_self_component_port_output *portByIndex(bt_self_component_filter * const libCompPtr,
216 const std::uint64_t index) noexcept
218 return bt_self_component_filter_borrow_output_port_by_index(libCompPtr, index);
221 static bt_self_component_port_output *portByName(bt_self_component_filter * const libCompPtr,
222 const char * const name) noexcept
224 return bt_self_component_filter_borrow_output_port_by_name(libCompPtr, name);
229 struct SelfComponentPortsSpec<bt_self_component_filter, bt_self_component_port_input> final
231 static std::uint64_t portCount(bt_self_component_filter * const libCompPtr) noexcept
233 return bt_component_filter_get_input_port_count(
234 bt_self_component_filter_as_component_filter(libCompPtr));
237 static bt_self_component_port_input *portByIndex(bt_self_component_filter * const libCompPtr,
238 const std::uint64_t index) noexcept
240 return bt_self_component_filter_borrow_input_port_by_index(libCompPtr, index);
243 static bt_self_component_port_input *portByName(bt_self_component_filter * const libCompPtr,
244 const char * const name) noexcept
246 return bt_self_component_filter_borrow_input_port_by_name(libCompPtr, name);
251 struct SelfComponentPortsSpec<bt_self_component_sink, bt_self_component_port_input> final
253 static std::uint64_t portCount(bt_self_component_sink * const libCompPtr) noexcept
255 return bt_component_sink_get_input_port_count(
256 bt_self_component_sink_as_component_sink(libCompPtr));
259 static bt_self_component_port_input *portByIndex(bt_self_component_sink * const libCompPtr,
260 const std::uint64_t index) noexcept
262 return bt_self_component_sink_borrow_input_port_by_index(libCompPtr, index);
265 static bt_self_component_port_input *portByName(bt_self_component_sink * const libCompPtr,
266 const char * const name) noexcept
268 return bt_self_component_sink_borrow_input_port_by_name(libCompPtr, name);
272 } /* namespace internal */
274 template <typename LibSelfCompPortT, typename LibPortT>
275 class SelfComponentPort;
277 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
278 class SelfComponentPorts final : public BorrowedObject<LibSelfCompT>
281 using typename BorrowedObject<LibSelfCompT>::_ThisBorrowedObject;
282 using _Spec = internal::SelfComponentPortsSpec<LibSelfCompT, LibSelfCompPortT>;
285 using typename BorrowedObject<LibSelfCompT>::LibObjPtr;
286 using Port = SelfComponentPort<LibSelfCompPortT, LibPortT>;
287 using Iterator = BorrowedObjectIterator<SelfComponentPorts>;
289 explicit SelfComponentPorts(const LibObjPtr libObjPtr) noexcept :
290 _ThisBorrowedObject {libObjPtr}
294 std::uint64_t length() const noexcept
296 return _Spec::portCount(this->libObjPtr());
299 Port operator[](std::uint64_t index) const noexcept;
300 Port operator[](bt2c::CStringView name) const noexcept;
301 Iterator begin() const noexcept;
302 Iterator end() const noexcept;
303 Port front() const noexcept;
304 Port back() const noexcept;
307 class SelfSourceComponent final : public SelfSpecificComponent<bt_self_component_source>
310 using OutputPorts = SelfComponentPorts<bt_self_component_source, bt_self_component_port_output,
311 const bt_port_output>;
313 explicit SelfSourceComponent(bt_self_component_source * const libObjPtr) noexcept :
314 SelfSpecificComponent {libObjPtr}
318 ConstSourceComponent asConstComponent() const noexcept
320 return ConstSourceComponent {
321 bt_self_component_source_as_component_source(this->libObjPtr())};
324 template <typename DataT>
325 OutputPorts::Port addOutputPort(bt2c::CStringView name, DataT& data) const;
327 OutputPorts::Port addOutputPort(bt2c::CStringView name) const;
329 OutputPorts outputPorts() const noexcept;
332 template <typename DataT>
333 OutputPorts::Port _addOutputPort(const char *name, DataT *data) const;
336 class SelfFilterComponent final : public SelfSpecificComponent<bt_self_component_filter>
339 using InputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_input,
340 const bt_port_input>;
341 using OutputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_output,
342 const bt_port_output>;
344 explicit SelfFilterComponent(bt_self_component_filter * const libObjPtr) noexcept :
345 SelfSpecificComponent {libObjPtr}
349 ConstFilterComponent asConstComponent() const noexcept
351 return ConstFilterComponent {
352 bt_self_component_filter_as_component_filter(this->libObjPtr())};
355 template <typename DataT>
356 InputPorts::Port addInputPort(bt2c::CStringView name, DataT& data) const;
358 InputPorts::Port addInputPort(bt2c::CStringView name) const;
360 InputPorts inputPorts() const noexcept;
362 template <typename DataT>
363 OutputPorts::Port addOutputPort(bt2c::CStringView name, DataT& data) const;
365 OutputPorts::Port addOutputPort(bt2c::CStringView name) const;
367 OutputPorts outputPorts() const noexcept;
370 template <typename DataT>
371 InputPorts::Port _addInputPort(const char *name, DataT *data) const;
373 template <typename DataT>
374 OutputPorts::Port _addOutputPort(const char *name, DataT *data) const;
377 class SelfSinkComponent final : public SelfSpecificComponent<bt_self_component_sink>
380 using InputPorts = SelfComponentPorts<bt_self_component_sink, bt_self_component_port_input,
381 const bt_port_input>;
383 explicit SelfSinkComponent(bt_self_component_sink * const libObjPtr) noexcept :
384 SelfSpecificComponent {libObjPtr}
388 ConstSinkComponent asConstComponent() const noexcept
390 return ConstSinkComponent {bt_self_component_sink_as_component_sink(this->libObjPtr())};
393 MessageIterator::Shared createMessageIterator(InputPorts::Port port) const;
395 bool isInterrupted() const noexcept
397 return static_cast<bool>(bt_self_component_sink_is_interrupted(this->libObjPtr()));
400 template <typename DataT>
401 InputPorts::Port addInputPort(bt2c::CStringView name, DataT& data) const;
403 InputPorts::Port addInputPort(bt2c::CStringView name) const;
405 InputPorts inputPorts() const noexcept;
408 template <typename DataT>
409 InputPorts::Port _addInputPort(const char *name, DataT *data) const;
412 inline SelfComponent::SelfComponent(const SelfSourceComponent other) noexcept :
413 SelfComponent {other.libObjPtr()}
417 inline SelfComponent::SelfComponent(const SelfFilterComponent other) noexcept :
418 SelfComponent {other.libObjPtr()}
422 inline SelfComponent::SelfComponent(const SelfSinkComponent other) noexcept :
423 SelfComponent {other.libObjPtr()}
427 inline SelfComponent SelfComponent::operator=(const SelfSourceComponent other) noexcept
429 *this = SelfComponent {other.libObjPtr()};
433 inline SelfComponent SelfComponent::operator=(const SelfFilterComponent other) noexcept
435 *this = SelfComponent {other.libObjPtr()};
439 inline SelfComponent SelfComponent::operator=(const SelfSinkComponent other) noexcept
441 *this = SelfComponent {other.libObjPtr()};
447 template <typename LibObjT>
448 struct SelfComponentPortSpec;
450 /* Functions specific to self component input ports */
452 struct SelfComponentPortSpec<bt_self_component_port_input> final
454 static bt_self_component_port *
455 asSelfCompPort(bt_self_component_port_input * const libObjPtr) noexcept
457 return bt_self_component_port_input_as_self_component_port(libObjPtr);
460 static const bt_port_input *
461 asConstPort(const bt_self_component_port_input * const libObjPtr) noexcept
463 return bt_self_component_port_input_as_port_input(libObjPtr);
467 /* Functions specific to self component output ports */
469 struct SelfComponentPortSpec<bt_self_component_port_output> final
471 static bt_self_component_port *
472 asSelfCompPort(bt_self_component_port_output * const libObjPtr) noexcept
474 return bt_self_component_port_output_as_self_component_port(libObjPtr);
477 static const bt_port_output *
478 asConstPort(bt_self_component_port_output * const libObjPtr) noexcept
480 return bt_self_component_port_output_as_port_output(libObjPtr);
484 } /* namespace internal */
486 template <typename LibSelfCompPortT, typename LibPortT>
487 class SelfComponentPort final : public BorrowedObject<LibSelfCompPortT>
490 using typename BorrowedObject<LibSelfCompPortT>::LibObjPtr;
492 explicit SelfComponentPort(const LibObjPtr libObjPtr) noexcept :
493 BorrowedObject<LibSelfCompPortT> {libObjPtr}
497 ConstPort<LibPortT> asConstPort() const noexcept
499 return ConstPort<LibPortT> {
500 internal::SelfComponentPortSpec<LibSelfCompPortT>::asConstPort(this->libObjPtr())};
503 bt2c::CStringView name() const noexcept
505 return this->asConstPort().name();
508 bool isConnected() const noexcept
510 return this->asConstPort().isConnected();
513 SelfComponent component() const noexcept
515 return SelfComponent {bt_self_component_port_borrow_component(this->_libSelfCompPortPtr())};
518 template <typename T>
519 T& data() const noexcept
521 *static_cast<T *>(bt_self_component_port_get_data(this->_libSelfCompPortPtr()));
525 bt_self_component_port *_libSelfCompPortPtr() const noexcept
527 return internal::SelfComponentPortSpec<LibSelfCompPortT>::asSelfCompPort(this->libObjPtr());
531 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
532 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
533 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[](
534 const std::uint64_t index) const noexcept
536 return Port {_Spec::portByIndex(this->libObjPtr(), index)};
539 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
540 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
541 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::operator[](
542 const bt2c::CStringView name) const noexcept
544 return Port {_Spec::portByName(this->libObjPtr(), name)};
547 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
548 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Iterator
549 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::begin() const noexcept
551 return Iterator {*this, 0};
554 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
555 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Iterator
556 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::end() const noexcept
558 return Iterator {*this, this->length()};
561 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
562 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
563 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::front() const noexcept
568 template <typename LibSelfCompT, typename LibSelfCompPortT, typename LibPortT>
569 typename SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::Port
570 SelfComponentPorts<LibSelfCompT, LibSelfCompPortT, LibPortT>::back() const noexcept
572 return (*this)[this->length() - 1];
575 using SelfComponentInputPort = SelfComponentPort<bt_self_component_port_input, const bt_port_input>;
577 using SelfComponentOutputPort =
578 SelfComponentPort<bt_self_component_port_output, const bt_port_output>;
580 template <typename DataT>
581 SelfSourceComponent::OutputPorts::Port SelfSourceComponent::_addOutputPort(const char * const name,
582 DataT * const data) const
584 return this->_addPort<SelfSourceComponent::OutputPorts::Port, bt_self_component_port_output>(
585 name, data, bt_self_component_source_add_output_port);
588 template <typename DataT>
589 SelfSourceComponent::OutputPorts::Port
590 SelfSourceComponent::addOutputPort(const bt2c::CStringView name, DataT& data) const
592 return this->_addOutputPort(name, &data);
595 inline SelfSourceComponent::OutputPorts::Port
596 SelfSourceComponent::addOutputPort(const bt2c::CStringView name) const
598 return this->_addOutputPort<void>(name, nullptr);
601 inline SelfSourceComponent::OutputPorts SelfSourceComponent::outputPorts() const noexcept
603 return OutputPorts {this->libObjPtr()};
606 template <typename DataT>
607 SelfFilterComponent::OutputPorts::Port SelfFilterComponent::_addOutputPort(const char * const name,
608 DataT * const data) const
610 return this->_addPort<SelfFilterComponent::OutputPorts::Port, bt_self_component_port_output>(
611 name, data, bt_self_component_filter_add_output_port);
614 template <typename DataT>
615 SelfFilterComponent::OutputPorts::Port
616 SelfFilterComponent::addOutputPort(const bt2c::CStringView name, DataT& data) const
618 return this->_addOutputPort(name, &data);
621 inline SelfFilterComponent::OutputPorts::Port
622 SelfFilterComponent::addOutputPort(const bt2c::CStringView name) const
624 return this->_addOutputPort<void>(name, nullptr);
627 inline SelfFilterComponent::OutputPorts SelfFilterComponent::outputPorts() const noexcept
629 return OutputPorts {this->libObjPtr()};
632 template <typename DataT>
633 SelfFilterComponent::InputPorts::Port SelfFilterComponent::_addInputPort(const char * const name,
634 DataT * const data) const
636 return this->_addPort<SelfFilterComponent::InputPorts::Port, bt_self_component_port_input>(
637 name, data, bt_self_component_filter_add_input_port);
640 template <typename DataT>
641 SelfFilterComponent::InputPorts::Port
642 SelfFilterComponent::addInputPort(const bt2c::CStringView name, DataT& data) const
644 return this->_addInputPort(name, &data);
647 inline SelfFilterComponent::InputPorts::Port
648 SelfFilterComponent::addInputPort(const bt2c::CStringView name) const
650 return this->_addInputPort<void>(name, nullptr);
653 inline SelfFilterComponent::InputPorts SelfFilterComponent::inputPorts() const noexcept
655 return InputPorts {this->libObjPtr()};
658 inline MessageIterator::Shared
659 SelfSinkComponent::createMessageIterator(const InputPorts::Port port) const
661 bt_message_iterator *libMsgIterPtr = nullptr;
663 const auto status = bt_message_iterator_create_from_sink_component(
664 this->libObjPtr(), port.libObjPtr(), &libMsgIterPtr);
667 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK:
668 BT_ASSERT(libMsgIterPtr);
669 return MessageIterator::Shared::createWithoutRef(libMsgIterPtr);
670 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_MEMORY_ERROR:
671 throw MemoryError {};
672 case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_ERROR:
679 template <typename DataT>
680 SelfSinkComponent::InputPorts::Port SelfSinkComponent::_addInputPort(const char * const name,
681 DataT * const data) const
683 return this->_addPort<SelfSinkComponent::InputPorts::Port, bt_self_component_port_input>(
684 name, data, bt_self_component_sink_add_input_port);
687 template <typename DataT>
688 SelfSinkComponent::InputPorts::Port SelfSinkComponent::addInputPort(const bt2c::CStringView name,
691 return this->_addInputPort(name, &data);
694 inline SelfSinkComponent::InputPorts::Port
695 SelfSinkComponent::addInputPort(const bt2c::CStringView name) const
697 return this->_addInputPort<void>(name, nullptr);
700 inline SelfSinkComponent::InputPorts SelfSinkComponent::inputPorts() const noexcept
702 return InputPorts {this->libObjPtr()};
705 } /* namespace bt2 */
707 #endif /* BABELTRACE_CPP_COMMON_BT2_SELF_COMPONENT_PORT_HPP */