From 64fbd3f734a0e5d5b5cc63602dcd6c6fe0bc4333 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 17 Nov 2023 15:04:27 -0500 Subject: [PATCH] cpp-common/bt2/shared-object.hpp: make the optional part public 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 Change-Id: I646cf883eba47ff1058c95d1b027396ee713fa0b Reviewed-on: https://review.lttng.org/c/babeltrace/+/11399 Tested-by: jenkins Reviewed-by: Simon Marchi CI-Build: Simon Marchi --- src/cpp-common/bt2/shared-object.hpp | 36 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/src/cpp-common/bt2/shared-object.hpp b/src/cpp-common/bt2/shared-object.hpp index 8eb15652..3b5ccfa2 100644 --- a/src/cpp-common/bt2/shared-object.hpp +++ b/src/cpp-common/bt2/shared-object.hpp @@ -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 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 { -- 2.34.1