cpp-common/bt2: remove redundant assertions
[babeltrace.git] / src / cpp-common / bt2 / shared-object.hpp
index 8eb156523e993a16878b4586cb86085ee53d6be2..124cff48b49086ce16ede66647eecfdfa01a694b 100644 (file)
@@ -7,8 +7,9 @@
 #ifndef BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP
 #define BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP
 
-#include "common/assert.h"
-#include "cpp-common/bt2s/optional.hpp"
+#include <utility>
+
+#include "optional-borrowed-object.hpp"
 
 namespace bt2 {
 
@@ -17,9 +18,13 @@ namespace bt2 {
  * manages the reference counting of the underlying libbabeltrace2
  * object.
  *
- * When you move a shared object, it becomes invalid, in that
- * operator*() and operator->() will either fail to assert in debug mode
- * or trigger a segmentation fault.
+ * When you move a shared object, it becomes empty, in that operator*()
+ * and operator->() will either fail to assert in debug mode or trigger
+ * a segmentation fault.
+ *
+ * The default constructor builds an empty shared object. You may also
+ * call the reset() method to make a shared object empty. Check whether
+ * or not a shared object is empty with the `bool` operator.
  *
  * `LibObjT` is the direct libbabeltrace2 object type, for example
  * `bt_stream_class` or `const bt_value`.
@@ -44,6 +49,14 @@ class SharedObject final
     template <typename, typename, typename>
     friend class SharedObject;
 
+public:
+    /*
+     * Builds an empty shared object.
+     */
+    explicit SharedObject() noexcept
+    {
+    }
+
 private:
     /*
      * Builds a shared object from `obj` without getting a reference.
@@ -218,28 +231,30 @@ public:
         this->_putRef();
     }
 
-    ObjT& operator*() noexcept
+    ObjT operator*() const noexcept
     {
-        BT_ASSERT_DBG(_mObj);
         return *_mObj;
     }
 
-    const ObjT& operator*() const noexcept
+    BorrowedObjectProxy<ObjT> operator->() const noexcept
     {
-        BT_ASSERT_DBG(_mObj);
-        return *_mObj;
+        return _mObj.operator->();
     }
 
-    ObjT *operator->() noexcept
+    operator bool() const noexcept
     {
-        BT_ASSERT_DBG(_mObj);
-        return &*_mObj;
+        return _mObj.hasObject();
     }
 
-    const ObjT *operator->() const noexcept
+    /*
+     * Makes this shared object empty.
+     */
+    void reset() noexcept
     {
-        BT_ASSERT_DBG(_mObj);
-        return &*_mObj;
+        if (_mObj) {
+            this->_putRef();
+            this->_reset();
+        }
     }
 
     /*
@@ -247,12 +262,10 @@ public:
      * wrapper manages and returns it, making the caller become an
      * active owner.
      *
-     * This method makes this object invalid.
+     * This method makes this object empty.
      */
     ObjT release() noexcept
     {
-        BT_ASSERT_DBG(_mObj);
-
         const auto obj = *_mObj;
 
         this->_reset();
@@ -276,9 +289,7 @@ private:
      */
     void _getRef() const noexcept
     {
-        if (_mObj) {
-            RefFuncsT::get(_mObj->libObjPtr());
-        }
+        RefFuncsT::get(_mObj.libObjPtr());
     }
 
     /*
@@ -287,12 +298,10 @@ private:
      */
     void _putRef() const noexcept
     {
-        if (_mObj) {
-            RefFuncsT::put(_mObj->libObjPtr());
-        }
+        RefFuncsT::put(_mObj.libObjPtr());
     }
 
-    bt2s::optional<ObjT> _mObj;
+    OptionalBorrowedObject<ObjT> _mObj;
 };
 
 } /* namespace bt2 */
This page took 0.02416 seconds and 4 git commands to generate.