cpp-common: add SharedObj::release
authorSimon Marchi <simon.marchi@efficios.com>
Sat, 28 May 2022 14:01:44 +0000 (10:01 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
It will sometimes be needed to extract the ownership of a lib object
from a SharedObj, and get the underlying lib object pointer.  For
example:

 - The value returned by queries
 - Messages returned by iterators

Add a SharedObj::release method for that.  The return type is a borrowed
object (e.g. bt2::Value::Shared::release returns a bt2::Value), but
conceptually the caller then owns the reference and is responsible for
manually managing it.

After a release, a Shared object becomes invalid, same as when it is moved
from.

Change-Id: Id31855512b8dd3d5223f2e759ff48f81ceadbfbf
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/8169
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10825
CI-Build: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/bt2/internal/shared-obj.hpp

index b4d92494af523a202d86d6ae9c1f4c0af0d3393d..7846b32482dadbfbca9a51b0ba91554f79ca06db 100644 (file)
@@ -243,6 +243,21 @@ public:
         return &*_mObj;
     }
 
+    /*
+     * 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.
+     */
+    ObjT release() noexcept
+    {
+        BT_ASSERT_DBG(_mObj);
+        const auto obj = *_mObj;
+        this->_reset();
+        return obj;
+    }
+
 private:
     /*
      * Resets this shared object.
This page took 0.035721 seconds and 4 git commands to generate.