cpp-common/bt2/shared-object.hpp: make the optional part public
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 17 Nov 2023 20:04:27 +0000 (15:04 -0500)
committerSimon Marchi <simon.marchi@efficios.com>
Thu, 14 Dec 2023 15:57:04 +0000 (10:57 -0500)
Make it possible to have an empty shared object instead of needing two
`bt2s::optional` (the `bt2::SharedObject` one and the user one).

Adding a default constructor to build an empty shared object and the
`bool` operator to check whether or not it's empty; similar to
`std::shared_ptr`.

Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: I646cf883eba47ff1058c95d1b027396ee713fa0b
Reviewed-on: https://review.lttng.org/c/babeltrace/+/11399
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
CI-Build: Simon Marchi <simon.marchi@efficios.com>

src/cpp-common/bt2/shared-object.hpp

index 8eb156523e993a16878b4586cb86085ee53d6be2..3b5ccfa259d93996a7c1b925385608d8180d1146 100644 (file)
@@ -17,9 +17,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 +48,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.
@@ -242,12 +254,28 @@ public:
         return &*_mObj;
     }
 
+    operator bool() const noexcept
+    {
+        return _mObj.has_value();
+    }
+
+    /*
+     * Makes this shared object empty.
+     */
+    void reset() noexcept
+    {
+        if (_mObj) {
+            this->_putRef();
+            this->_reset();
+        }
+    }
+
     /*
      * Transfers the reference of the object which this shared object
      * 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
     {
This page took 0.026539 seconds and 4 git commands to generate.