cpp-common/bt2: make setters return `*this`
[babeltrace.git] / src / cpp-common / bt2 / self-component-port.hpp
index 2551064ecbd1cdcbf17e53abc95343ac2229ac4f..e1ab809859461f618510b9e9526ee2637e057aee 100644 (file)
@@ -13,7 +13,6 @@
 
 #include "logging.hpp"
 
-#include "common/assert.h"
 #include "cpp-common/bt2c/c-string-view.hpp"
 
 #include "borrowed-object-iterator.hpp"
@@ -100,9 +99,11 @@ public:
     }
 
     template <typename T>
-    void data(T& obj) const noexcept
+    SelfComponent data(T& obj) const noexcept
     {
-        bt_self_component_set_data(this->libObjPtr(), static_cast<void *>(&obj));
+        bt_self_component_set_data(this->libObjPtr(),
+                                   const_cast<void *>(static_cast<const void *>(&obj)));
+        return *this;
     }
 
     bt2::TraceClass::Shared createTraceClass() const
@@ -128,6 +129,8 @@ public:
     }
 };
 
+namespace internal {
+
 template <typename LibObjT>
 class SelfSpecificComponent : public BorrowedObject<LibObjT>
 {
@@ -148,7 +151,8 @@ protected:
     {
         LibPortT *libPortPtr;
 
-        const auto status = func(this->libObjPtr(), name, static_cast<void *>(data), &libPortPtr);
+        const auto status = func(this->libObjPtr(), name,
+                                 const_cast<void *>(static_cast<const void *>(data)), &libPortPtr);
 
         switch (status) {
         case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK:
@@ -184,21 +188,13 @@ public:
         return this->_selfComponent().template data<T>();
     }
 
-    template <typename T>
-    void data(T& obj) const noexcept
-    {
-        this->_selfComponent().data(obj);
-    }
-
-private:
+protected:
     SelfComponent _selfComponent() const noexcept
     {
         return SelfComponent {this->libObjPtr()};
     }
 };
 
-namespace internal {
-
 template <typename LibSelfCompT, typename LibSelfCompPortPtrT>
 struct SelfComponentPortsSpec;
 
@@ -325,8 +321,10 @@ public:
     Port back() const noexcept;
 };
 
-class SelfSourceComponent final : public SelfSpecificComponent<bt_self_component_source>
+class SelfSourceComponent final : public internal::SelfSpecificComponent<bt_self_component_source>
 {
+    using _ThisSelfSpecificComponent = internal::SelfSpecificComponent<bt_self_component_source>;
+
 public:
     using OutputPorts = SelfComponentPorts<bt_self_component_source, bt_self_component_port_output,
                                            const bt_port_output>;
@@ -342,6 +340,15 @@ public:
             bt_self_component_source_as_component_source(this->libObjPtr())};
     }
 
+    using _ThisSelfSpecificComponent::data;
+
+    template <typename T>
+    SelfSourceComponent data(T& obj) const noexcept
+    {
+        this->_selfComponent().data(obj);
+        return *this;
+    }
+
     template <typename DataT>
     OutputPorts::Port addOutputPort(bt2c::CStringView name, DataT& data) const;
 
@@ -354,8 +361,10 @@ private:
     OutputPorts::Port _addOutputPort(const char *name, DataT *data) const;
 };
 
-class SelfFilterComponent final : public SelfSpecificComponent<bt_self_component_filter>
+class SelfFilterComponent final : public internal::SelfSpecificComponent<bt_self_component_filter>
 {
+    using _ThisSelfSpecificComponent = internal::SelfSpecificComponent<bt_self_component_filter>;
+
 public:
     using InputPorts = SelfComponentPorts<bt_self_component_filter, bt_self_component_port_input,
                                           const bt_port_input>;
@@ -373,6 +382,15 @@ public:
             bt_self_component_filter_as_component_filter(this->libObjPtr())};
     }
 
+    using _ThisSelfSpecificComponent::data;
+
+    template <typename T>
+    SelfFilterComponent data(T& obj) const noexcept
+    {
+        this->_selfComponent().data(obj);
+        return *this;
+    }
+
     template <typename DataT>
     InputPorts::Port addInputPort(bt2c::CStringView name, DataT& data) const;
 
@@ -395,8 +413,10 @@ private:
     OutputPorts::Port _addOutputPort(const char *name, DataT *data) const;
 };
 
-class SelfSinkComponent final : public SelfSpecificComponent<bt_self_component_sink>
+class SelfSinkComponent final : public internal::SelfSpecificComponent<bt_self_component_sink>
 {
+    using _ThisSelfSpecificComponent = internal::SelfSpecificComponent<bt_self_component_sink>;
+
 public:
     using InputPorts = SelfComponentPorts<bt_self_component_sink, bt_self_component_port_input,
                                           const bt_port_input>;
@@ -411,6 +431,15 @@ public:
         return ConstSinkComponent {bt_self_component_sink_as_component_sink(this->libObjPtr())};
     }
 
+    using _ThisSelfSpecificComponent::data;
+
+    template <typename T>
+    SelfSinkComponent data(T& obj) const noexcept
+    {
+        this->_selfComponent().data(obj);
+        return *this;
+    }
+
     MessageIterator::Shared createMessageIterator(InputPorts::Port port) const;
 
     bool isInterrupted() const noexcept
@@ -686,7 +715,6 @@ SelfSinkComponent::createMessageIterator(const InputPorts::Port port) const
 
     switch (status) {
     case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK:
-        BT_ASSERT(libMsgIterPtr);
         return MessageIterator::Shared::createWithoutRef(libMsgIterPtr);
     case BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_MEMORY_ERROR:
         throw MemoryError {};
This page took 0.024272 seconds and 4 git commands to generate.