From: Simon Marchi Date: Sat, 28 May 2022 14:01:44 +0000 (-0400) Subject: cpp-common: add SharedObj::release X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=d0b6533a5d3c3da3c347a5898cbaa7098ba1bd3e;p=babeltrace.git cpp-common: add SharedObj::release 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 Reviewed-on: https://review.lttng.org/c/babeltrace/+/8169 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/10825 CI-Build: Philippe Proulx Tested-by: jenkins --- diff --git a/src/cpp-common/bt2/internal/shared-obj.hpp b/src/cpp-common/bt2/internal/shared-obj.hpp index b4d92494..7846b324 100644 --- a/src/cpp-common/bt2/internal/shared-obj.hpp +++ b/src/cpp-common/bt2/internal/shared-obj.hpp @@ -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.