From: Philippe Proulx Date: Mon, 13 Nov 2023 19:49:26 +0000 (-0500) Subject: cpp-common/bt2: rename `bt2::SharedObj` -> `bt2::SharedObject` X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=7f5cdaf0da71769252d4ae548c25be5d1ffc49a1;p=babeltrace.git cpp-common/bt2: rename `bt2::SharedObj` -> `bt2::SharedObject` This is public now, so use the full name without abbreviation like everything else does. Signed-off-by: Philippe Proulx Change-Id: I1328a9c195061558ca442aed6e177ca43836b99c Reviewed-on: https://review.lttng.org/c/babeltrace/+/11368 Reviewed-by: Simon Marchi --- diff --git a/src/Makefile.am b/src/Makefile.am index 2154e5ac..1f90d14b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -25,7 +25,7 @@ noinst_HEADERS = \ cpp-common/bt2/internal/utils.hpp \ cpp-common/bt2/logging.hpp \ cpp-common/bt2/message.hpp \ - cpp-common/bt2/shared-obj.hpp \ + cpp-common/bt2/shared-object.hpp \ cpp-common/bt2/trace-ir.hpp \ cpp-common/bt2/type-traits.hpp \ cpp-common/bt2/value.hpp \ diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp index 1aac1459..82691b39 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -20,7 +20,7 @@ #include "borrowed-object.hpp" #include "exc.hpp" #include "internal/utils.hpp" -#include "shared-obj.hpp" +#include "shared-object.hpp" #include "value.hpp" namespace bt2 { @@ -100,7 +100,7 @@ private: using _ThisCommonClockClass = CommonClockClass; public: - using Shared = SharedObj<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>; + using Shared = SharedObject<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>; using UserAttributes = typename std::conditional::value, ConstMapValue, MapValue>::type; diff --git a/src/cpp-common/bt2/field-class.hpp b/src/cpp-common/bt2/field-class.hpp index f8f3d850..168af69c 100644 --- a/src/cpp-common/bt2/field-class.hpp +++ b/src/cpp-common/bt2/field-class.hpp @@ -22,7 +22,7 @@ #include "field-path.hpp" #include "integer-range-set.hpp" #include "internal/utils.hpp" -#include "shared-obj.hpp" +#include "shared-object.hpp" #include "value.hpp" namespace bt2 { @@ -67,7 +67,7 @@ struct CommonFieldClassSpec final } /* namespace internal */ template -using SharedFieldClass = SharedObj; +using SharedFieldClass = SharedObject; template class CommonBitArrayFieldClass; diff --git a/src/cpp-common/bt2/field-path.hpp b/src/cpp-common/bt2/field-path.hpp index caf1de00..97c1addd 100644 --- a/src/cpp-common/bt2/field-path.hpp +++ b/src/cpp-common/bt2/field-path.hpp @@ -15,7 +15,7 @@ #include "borrowed-object.hpp" #include "common-iter.hpp" -#include "shared-obj.hpp" +#include "shared-object.hpp" namespace bt2 { @@ -127,7 +127,7 @@ struct FieldPathRefFuncs final class ConstFieldPath final : public BorrowedObject { public: - using Shared = SharedObj; + using Shared = SharedObject; using Iterator = CommonIterator; diff --git a/src/cpp-common/bt2/integer-range-set.hpp b/src/cpp-common/bt2/integer-range-set.hpp index 3a05e1d3..6a6a4b4d 100644 --- a/src/cpp-common/bt2/integer-range-set.hpp +++ b/src/cpp-common/bt2/integer-range-set.hpp @@ -17,7 +17,7 @@ #include "exc.hpp" #include "integer-range.hpp" #include "internal/utils.hpp" -#include "shared-obj.hpp" +#include "shared-object.hpp" namespace bt2 { @@ -173,7 +173,7 @@ private: using _ThisCommonIntegerRangeSet = CommonIntegerRangeSet; public: - using Shared = SharedObj<_ThisCommonIntegerRangeSet, LibObjT, _RefFuncs>; + using Shared = SharedObject<_ThisCommonIntegerRangeSet, LibObjT, _RefFuncs>; using Range = typename std::conditional< std::is_same<_ConstLibObjT, const bt_integer_range_set_unsigned>::value, diff --git a/src/cpp-common/bt2/message.hpp b/src/cpp-common/bt2/message.hpp index aee0e7c9..1b49bed5 100644 --- a/src/cpp-common/bt2/message.hpp +++ b/src/cpp-common/bt2/message.hpp @@ -19,7 +19,7 @@ #include "borrowed-object.hpp" #include "internal/utils.hpp" -#include "shared-obj.hpp" +#include "shared-object.hpp" namespace bt2 { namespace internal { @@ -40,7 +40,7 @@ struct MessageRefFuncs final } /* namespace internal */ template -using SharedMessage = SharedObj; +using SharedMessage = SharedObject; template class CommonStreamBeginningMessage; diff --git a/src/cpp-common/bt2/shared-obj.hpp b/src/cpp-common/bt2/shared-obj.hpp deleted file mode 100644 index d0a2d7f9..00000000 --- a/src/cpp-common/bt2/shared-obj.hpp +++ /dev/null @@ -1,298 +0,0 @@ -/* - * Copyright (c) 2019-2020 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_BT2_SHARED_OBJ_HPP -#define BABELTRACE_CPP_COMMON_BT2_SHARED_OBJ_HPP - -#include "common/assert.h" -#include "cpp-common/optional.hpp" - -namespace bt2 { - -/* - * An instance of this class wraps an optional instance of `ObjT` and - * 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. - * - * `LibObjT` is the direct libbabeltrace2 object type, for example - * `bt_stream_class` or `const bt_value`. - * - * RefFuncsT::get() must accept a `const LibObjT *` value and increment - * its reference count. - * - * RefFuncsT::put() must accept a `const LibObjT *` value and decrement - * its reference count. - */ -template -class SharedObj final -{ - /* - * This makes it possible for a - * `SharedObj` instance to get - * assigned an instance of - * `SharedObj` (copy/move - * constructors and assignment operators), given that - * `SpecificSomething` inherits `Something`. - */ - template - friend class SharedObj; - -private: - /* - * Builds a shared object from `obj` without getting a reference. - */ - explicit SharedObj(const ObjT& obj) noexcept : _mObj {obj} - { - } - - /* - * Common generic "copy" constructor. - * - * This constructor is meant to be delegated to by the copy - * constructor and the generic "copy" constructor. - * - * The second parameter, of type `int`, makes it possible to - * delegate by deduction as you can't explicit the template - * parameters when delegating to a constructor template. - */ - template - SharedObj(const SharedObj& other, int) noexcept : - _mObj {other._mObj} - { - this->_getRef(); - } - - /* - * Common generic "move" constructor. - * - * See the comment of the common generic "copy" constructor above. - */ - template - SharedObj(SharedObj&& other, int) noexcept : - _mObj {other._mObj} - { - /* Reset moved-from object */ - other._reset(); - } - -public: - /* - * Builds a shared object from `obj` without getting a reference. - */ - static SharedObj createWithoutRef(const ObjT& obj) noexcept - { - return SharedObj {obj}; - } - - /* - * Builds a shared object from `libObjPtr` without getting a - * reference. - */ - static SharedObj createWithoutRef(LibObjT * const libObjPtr) noexcept - { - return SharedObj::createWithoutRef(ObjT {libObjPtr}); - } - - /* - * Builds a shared object from `obj`, immediately getting a new - * reference. - */ - static SharedObj createWithRef(const ObjT& obj) noexcept - { - SharedObj sharedObj {obj}; - - sharedObj._getRef(); - return sharedObj; - } - - /* - * Builds a shared object from `libObjPtr`, immediately getting a new - * reference. - */ - static SharedObj createWithRef(LibObjT * const libObjPtr) noexcept - { - return SharedObj::createWithRef(ObjT {libObjPtr}); - } - - /* - * Copy constructor. - */ - SharedObj(const SharedObj& other) noexcept : SharedObj {other, 0} - { - } - - /* - * Move constructor. - */ - SharedObj(SharedObj&& other) noexcept : SharedObj {std::move(other), 0} - { - } - - /* - * Copy assignment operator. - */ - SharedObj& operator=(const SharedObj& other) noexcept - { - /* Use generic "copy" assignment operator */ - return this->operator=(other); - } - - /* - * Move assignment operator. - */ - SharedObj& operator=(SharedObj&& other) noexcept - { - /* Use generic "move" assignment operator */ - return this->operator=(std::move(other)); - } - - /* - * Generic "copy" constructor. - * - * See the `friend class SharedObj` comment above. - */ - template - SharedObj(const SharedObj& other) noexcept : - SharedObj {other, 0} - { - } - - /* - * Generic "move" constructor. - * - * See the `friend class SharedObj` comment above. - */ - template - SharedObj(SharedObj&& other) noexcept : - SharedObj {std::move(other), 0} - { - } - - /* - * Generic "copy" assignment operator. - * - * See the `friend class SharedObj` comment above. - */ - template - SharedObj& operator=(const SharedObj& other) noexcept - { - /* Put current object's reference */ - this->_putRef(); - - /* Set new current object and get a reference */ - _mObj = other._mObj; - this->_getRef(); - - return *this; - } - - /* - * Generic "move" assignment operator. - * - * See the `friend class SharedObj` comment above. - */ - template - SharedObj& operator=(SharedObj&& other) noexcept - { - /* Put current object's reference */ - this->_putRef(); - - /* Set new current object */ - _mObj = other._mObj; - - /* Reset moved-from object */ - other._reset(); - - return *this; - } - - ~SharedObj() - { - this->_putRef(); - } - - ObjT& operator*() noexcept - { - BT_ASSERT_DBG(_mObj); - return *_mObj; - } - - const ObjT& operator*() const noexcept - { - BT_ASSERT_DBG(_mObj); - return *_mObj; - } - - ObjT *operator->() noexcept - { - BT_ASSERT_DBG(_mObj); - return &*_mObj; - } - - const ObjT *operator->() const noexcept - { - BT_ASSERT_DBG(_mObj); - 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. - * - * To be used when moving it. - */ - void _reset() noexcept - { - _mObj.reset(); - } - - /* - * Gets a new reference using the configured libbabeltrace2 - * reference incrementation function. - */ - void _getRef() const noexcept - { - if (_mObj) { - RefFuncsT::get(_mObj->libObjPtr()); - } - } - - /* - * Puts a reference using the configured libbabeltrace2 reference - * decrementation function. - */ - void _putRef() const noexcept - { - if (_mObj) { - RefFuncsT::put(_mObj->libObjPtr()); - } - } - - nonstd::optional _mObj; -}; - -} /* namespace bt2 */ - -#endif /* BABELTRACE_CPP_COMMON_BT2_SHARED_OBJ_HPP */ diff --git a/src/cpp-common/bt2/shared-object.hpp b/src/cpp-common/bt2/shared-object.hpp new file mode 100644 index 00000000..1ffbe8b1 --- /dev/null +++ b/src/cpp-common/bt2/shared-object.hpp @@ -0,0 +1,298 @@ +/* + * Copyright (c) 2019-2020 Philippe Proulx + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP +#define BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP + +#include "common/assert.h" +#include "cpp-common/optional.hpp" + +namespace bt2 { + +/* + * An instance of this class wraps an optional instance of `ObjT` and + * 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. + * + * `LibObjT` is the direct libbabeltrace2 object type, for example + * `bt_stream_class` or `const bt_value`. + * + * RefFuncsT::get() must accept a `const LibObjT *` value and increment + * its reference count. + * + * RefFuncsT::put() must accept a `const LibObjT *` value and decrement + * its reference count. + */ +template +class SharedObject final +{ + /* + * This makes it possible for a + * `SharedObject` instance to get + * assigned an instance of + * `SharedObject` (copy/move + * constructors and assignment operators), given that + * `SpecificSomething` inherits `Something`. + */ + template + friend class SharedObject; + +private: + /* + * Builds a shared object from `obj` without getting a reference. + */ + explicit SharedObject(const ObjT& obj) noexcept : _mObj {obj} + { + } + + /* + * Common generic "copy" constructor. + * + * This constructor is meant to be delegated to by the copy + * constructor and the generic "copy" constructor. + * + * The second parameter, of type `int`, makes it possible to + * delegate by deduction as you can't explicit the template + * parameters when delegating to a constructor template. + */ + template + SharedObject(const SharedObject& other, int) noexcept : + _mObj {other._mObj} + { + this->_getRef(); + } + + /* + * Common generic "move" constructor. + * + * See the comment of the common generic "copy" constructor above. + */ + template + SharedObject(SharedObject&& other, int) noexcept : + _mObj {other._mObj} + { + /* Reset moved-from object */ + other._reset(); + } + +public: + /* + * Builds a shared object from `obj` without getting a reference. + */ + static SharedObject createWithoutRef(const ObjT& obj) noexcept + { + return SharedObject {obj}; + } + + /* + * Builds a shared object from `libObjPtr` without getting a + * reference. + */ + static SharedObject createWithoutRef(LibObjT * const libObjPtr) noexcept + { + return SharedObject::createWithoutRef(ObjT {libObjPtr}); + } + + /* + * Builds a shared object from `obj`, immediately getting a new + * reference. + */ + static SharedObject createWithRef(const ObjT& obj) noexcept + { + SharedObject sharedObj {obj}; + + sharedObj._getRef(); + return sharedObj; + } + + /* + * Builds a shared object from `libObjPtr`, immediately getting a + * new reference. + */ + static SharedObject createWithRef(LibObjT * const libObjPtr) noexcept + { + return SharedObject::createWithRef(ObjT {libObjPtr}); + } + + /* + * Copy constructor. + */ + SharedObject(const SharedObject& other) noexcept : SharedObject {other, 0} + { + } + + /* + * Move constructor. + */ + SharedObject(SharedObject&& other) noexcept : SharedObject {std::move(other), 0} + { + } + + /* + * Copy assignment operator. + */ + SharedObject& operator=(const SharedObject& other) noexcept + { + /* Use generic "copy" assignment operator */ + return this->operator=(other); + } + + /* + * Move assignment operator. + */ + SharedObject& operator=(SharedObject&& other) noexcept + { + /* Use generic "move" assignment operator */ + return this->operator=(std::move(other)); + } + + /* + * Generic "copy" constructor. + * + * See the `friend class SharedObject` comment above. + */ + template + SharedObject(const SharedObject& other) noexcept : + SharedObject {other, 0} + { + } + + /* + * Generic "move" constructor. + * + * See the `friend class SharedObject` comment above. + */ + template + SharedObject(SharedObject&& other) noexcept : + SharedObject {std::move(other), 0} + { + } + + /* + * Generic "copy" assignment operator. + * + * See the `friend class SharedObject` comment above. + */ + template + SharedObject& operator=(const SharedObject& other) noexcept + { + /* Put current object's reference */ + this->_putRef(); + + /* Set new current object and get a reference */ + _mObj = other._mObj; + this->_getRef(); + + return *this; + } + + /* + * Generic "move" assignment operator. + * + * See the `friend class SharedObject` comment above. + */ + template + SharedObject& operator=(SharedObject&& other) noexcept + { + /* Put current object's reference */ + this->_putRef(); + + /* Set new current object */ + _mObj = other._mObj; + + /* Reset moved-from object */ + other._reset(); + + return *this; + } + + ~SharedObject() + { + this->_putRef(); + } + + ObjT& operator*() noexcept + { + BT_ASSERT_DBG(_mObj); + return *_mObj; + } + + const ObjT& operator*() const noexcept + { + BT_ASSERT_DBG(_mObj); + return *_mObj; + } + + ObjT *operator->() noexcept + { + BT_ASSERT_DBG(_mObj); + return &*_mObj; + } + + const ObjT *operator->() const noexcept + { + BT_ASSERT_DBG(_mObj); + 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. + * + * To be used when moving it. + */ + void _reset() noexcept + { + _mObj.reset(); + } + + /* + * Gets a new reference using the configured libbabeltrace2 + * reference incrementation function. + */ + void _getRef() const noexcept + { + if (_mObj) { + RefFuncsT::get(_mObj->libObjPtr()); + } + } + + /* + * Puts a reference using the configured libbabeltrace2 reference + * decrementation function. + */ + void _putRef() const noexcept + { + if (_mObj) { + RefFuncsT::put(_mObj->libObjPtr()); + } + } + + nonstd::optional _mObj; +}; + +} /* namespace bt2 */ + +#endif /* BABELTRACE_CPP_COMMON_BT2_SHARED_OBJECT_HPP */ diff --git a/src/cpp-common/bt2/trace-ir.hpp b/src/cpp-common/bt2/trace-ir.hpp index 293e9954..790735aa 100644 --- a/src/cpp-common/bt2/trace-ir.hpp +++ b/src/cpp-common/bt2/trace-ir.hpp @@ -20,7 +20,7 @@ #include "field-class.hpp" #include "field.hpp" #include "internal/utils.hpp" -#include "shared-obj.hpp" +#include "shared-object.hpp" #include "value.hpp" namespace bt2 { @@ -325,7 +325,7 @@ private: ConstStructureField, StructureField>::type; public: - using Shared = SharedObj<_ThisCommonPacket, LibObjT, internal::PacketRefFuncs>; + using Shared = SharedObject<_ThisCommonPacket, LibObjT, internal::PacketRefFuncs>; explicit CommonPacket(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { @@ -496,7 +496,7 @@ private: CommonTrace>::type; public: - using Shared = SharedObj<_ThisCommonStream, LibObjT, internal::StreamRefFuncs>; + using Shared = SharedObject<_ThisCommonStream, LibObjT, internal::StreamRefFuncs>; using Class = typename std::conditional::value, CommonStreamClass, @@ -729,7 +729,7 @@ private: CommonStream>::type; public: - using Shared = SharedObj<_ThisCommonTrace, LibObjT, internal::TraceRefFuncs>; + using Shared = SharedObject<_ThisCommonTrace, LibObjT, internal::TraceRefFuncs>; using Class = typename std::conditional::value, CommonTraceClass, @@ -1066,7 +1066,7 @@ private: StructureFieldClass>::type; public: - using Shared = SharedObj<_ThisCommonEventClass, LibObjT, internal::EventClassRefFuncs>; + using Shared = SharedObject<_ThisCommonEventClass, LibObjT, internal::EventClassRefFuncs>; using UserAttributes = typename std::conditional::value, ConstMapValue, MapValue>::type; @@ -1448,7 +1448,7 @@ private: typename std::conditional::value, ConstClockClass, ClockClass>::type; public: - using Shared = SharedObj<_ThisCommonStreamClass, LibObjT, internal::StreamClassRefFuncs>; + using Shared = SharedObject<_ThisCommonStreamClass, LibObjT, internal::StreamClassRefFuncs>; using UserAttributes = typename std::conditional::value, ConstMapValue, MapValue>::type; @@ -1930,7 +1930,7 @@ private: CommonStreamClass>::type; public: - using Shared = SharedObj<_ThisCommonTraceClass, LibObjT, internal::TraceClassRefFuncs>; + using Shared = SharedObject<_ThisCommonTraceClass, LibObjT, internal::TraceClassRefFuncs>; using UserAttributes = typename std::conditional::value, ConstMapValue, MapValue>::type; diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index 19bc357d..7596694c 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -22,7 +22,7 @@ #include "common-iter.hpp" #include "exc.hpp" #include "internal/utils.hpp" -#include "shared-obj.hpp" +#include "shared-object.hpp" namespace bt2 { namespace internal { @@ -43,7 +43,7 @@ struct ValueRefFuncs final } /* namespace internal */ template -using SharedValue = SharedObj; +using SharedValue = SharedObject; template class CommonNullValue;