From: Philippe Proulx Date: Mon, 13 Nov 2023 18:52:39 +0000 (-0500) Subject: cpp-common/bt2: `bt2::internal::SharedObj` -> `bt2::SharedObj` X-Git-Url: https://git.efficios.com/?a=commitdiff_plain;h=26b9d24c236956140a3bd8f79d19768b0a5aa006;p=babeltrace.git cpp-common/bt2: `bt2::internal::SharedObj` -> `bt2::SharedObj` `SharedObj` is not completely internal: it offers public methods, including creation methods. Also, we don't need to hide anything from the end user in this file. Therefore make this class not internal. Also apply to specific shared object aliases, for example `bt2::SharedValue`. Signed-off-by: Philippe Proulx Change-Id: I8bf1718a95de87b7c506ffab4868e63bc57a7391 Reviewed-on: https://review.lttng.org/c/babeltrace/+/11367 Reviewed-by: Simon Marchi --- diff --git a/src/Makefile.am b/src/Makefile.am index 523274bf..2154e5ac 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -22,10 +22,10 @@ noinst_HEADERS = \ cpp-common/bt2/field-path.hpp \ cpp-common/bt2/integer-range.hpp \ cpp-common/bt2/integer-range-set.hpp \ - cpp-common/bt2/internal/shared-obj.hpp \ 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/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 16bfd23b..1aac1459 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -19,8 +19,8 @@ #include "borrowed-object.hpp" #include "exc.hpp" -#include "internal/shared-obj.hpp" #include "internal/utils.hpp" +#include "shared-obj.hpp" #include "value.hpp" namespace bt2 { @@ -100,8 +100,7 @@ private: using _ThisCommonClockClass = CommonClockClass; public: - using Shared = - internal::SharedObj<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>; + using Shared = SharedObj<_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 67bf9e8a..f8f3d850 100644 --- a/src/cpp-common/bt2/field-class.hpp +++ b/src/cpp-common/bt2/field-class.hpp @@ -21,8 +21,8 @@ #include "exc.hpp" #include "field-path.hpp" #include "integer-range-set.hpp" -#include "internal/shared-obj.hpp" #include "internal/utils.hpp" +#include "shared-obj.hpp" #include "value.hpp" namespace bt2 { @@ -41,9 +41,6 @@ struct FieldClassRefFuncs final } }; -template -using SharedFieldClass = internal::SharedObj; - template struct CommonFieldClassSpec; @@ -69,6 +66,9 @@ struct CommonFieldClassSpec final } /* namespace internal */ +template +using SharedFieldClass = SharedObj; + template class CommonBitArrayFieldClass; @@ -194,7 +194,7 @@ protected: using _ThisCommonFieldClass = CommonFieldClass; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; using UserAttributes = typename std::conditional::value, ConstMapValue, MapValue>::type; @@ -483,7 +483,7 @@ private: using typename CommonFieldClass::_ThisCommonFieldClass; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonBitArrayFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonFieldClass {libObjPtr} @@ -558,7 +558,7 @@ protected: using _ThisCommonIntegerFieldClass = CommonIntegerFieldClass; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonIntegerFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonFieldClass {libObjPtr} @@ -791,7 +791,7 @@ protected: using _ThisCommonBaseEnumerationFieldClass = CommonBaseEnumerationFieldClass; public: - using Shared = internal::SharedFieldClass<_ThisCommonBaseEnumerationFieldClass, LibObjT>; + using Shared = SharedFieldClass<_ThisCommonBaseEnumerationFieldClass, LibObjT>; explicit CommonBaseEnumerationFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonIntegerFieldClass {libObjPtr} @@ -834,7 +834,7 @@ private: using _ThisCommonEnumerationFieldClass = CommonEnumerationFieldClass; public: - using Shared = internal::SharedFieldClass<_ThisCommonEnumerationFieldClass, LibObjT>; + using Shared = SharedFieldClass<_ThisCommonEnumerationFieldClass, LibObjT>; using Iterator = CommonIterator; using Mapping = MappingT; @@ -1143,7 +1143,7 @@ private: using typename CommonFieldClass::_ThisCommonFieldClass; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; using Member = typename std::conditional::value, ConstStructureFieldClassMember, StructureFieldClassMember>::type; @@ -1317,7 +1317,7 @@ protected: using _ThisCommonArrayFieldClass = CommonArrayFieldClass; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonArrayFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonFieldClass {libObjPtr} @@ -1388,7 +1388,7 @@ private: using typename CommonFieldClass::_LibObjPtr; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonStaticArrayFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonArrayFieldClass {libObjPtr} @@ -1452,8 +1452,7 @@ private: using typename CommonFieldClass::_LibObjPtr; public: - using Shared = - internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonDynamicArrayWithLengthFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonArrayFieldClass {libObjPtr} @@ -1553,7 +1552,7 @@ protected: using _ThisCommonOptionFieldClass = CommonOptionFieldClass; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonOptionFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonFieldClass {libObjPtr} @@ -1627,7 +1626,7 @@ protected: using _ThisCommonOptionWithSelectorFieldClass = CommonOptionWithSelectorFieldClass; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonOptionWithSelectorFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonOptionFieldClass {libObjPtr} @@ -1696,8 +1695,7 @@ private: LibObjT>::_ThisCommonOptionWithSelectorFieldClass; public: - using Shared = - internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonOptionWithBoolSelectorFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonOptionWithSelectorFieldClass {libObjPtr} @@ -1799,8 +1797,7 @@ private: CommonOptionWithIntegerSelectorFieldClass; public: - using Shared = - internal::SharedFieldClass<_ThisCommonOptionWithIntegerSelectorFieldClass, LibObjT>; + using Shared = SharedFieldClass<_ThisCommonOptionWithIntegerSelectorFieldClass, LibObjT>; using RangeSet = RangeSetT; @@ -2191,7 +2188,7 @@ protected: using _ThisCommonVariantFieldClass = CommonVariantFieldClass; public: - using Shared = internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; using Option = typename std::conditional::value, ConstVariantFieldClassOption, @@ -2320,8 +2317,7 @@ private: using typename CommonFieldClass::_LibObjPtr; public: - using Shared = - internal::SharedFieldClass, LibObjT>; + using Shared = SharedFieldClass, LibObjT>; explicit CommonVariantWithoutSelectorFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonVariantFieldClass {libObjPtr} @@ -2468,7 +2464,7 @@ protected: using _ThisCommonVariantWithSelectorFieldClass = CommonVariantWithSelectorFieldClass; public: - using Shared = internal::SharedFieldClass<_ThisCommonVariantWithSelectorFieldClass, LibObjT>; + using Shared = SharedFieldClass<_ThisCommonVariantWithSelectorFieldClass, LibObjT>; explicit CommonVariantWithSelectorFieldClass(const _LibObjPtr libObjPtr) noexcept : _ThisCommonVariantFieldClass {libObjPtr} @@ -2522,8 +2518,7 @@ private: using _Spec = internal::CommonVariantWithIntegerSelectorFieldClassSpec; public: - using Shared = - internal::SharedFieldClass<_ThisCommonVariantWithIntegerSelectorFieldClass, LibObjT>; + using Shared = SharedFieldClass<_ThisCommonVariantWithIntegerSelectorFieldClass, LibObjT>; using Option = OptionT; using Iterator = diff --git a/src/cpp-common/bt2/field-path.hpp b/src/cpp-common/bt2/field-path.hpp index 0416b00a..caf1de00 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 "internal/shared-obj.hpp" +#include "shared-obj.hpp" namespace bt2 { @@ -127,8 +127,7 @@ struct FieldPathRefFuncs final class ConstFieldPath final : public BorrowedObject { public: - using Shared = - internal::SharedObj; + using Shared = SharedObj; using Iterator = CommonIterator; diff --git a/src/cpp-common/bt2/integer-range-set.hpp b/src/cpp-common/bt2/integer-range-set.hpp index eb109921..3a05e1d3 100644 --- a/src/cpp-common/bt2/integer-range-set.hpp +++ b/src/cpp-common/bt2/integer-range-set.hpp @@ -17,6 +17,7 @@ #include "exc.hpp" #include "integer-range.hpp" #include "internal/utils.hpp" +#include "shared-obj.hpp" namespace bt2 { @@ -172,7 +173,7 @@ private: using _ThisCommonIntegerRangeSet = CommonIntegerRangeSet; public: - using Shared = internal::SharedObj<_ThisCommonIntegerRangeSet, LibObjT, _RefFuncs>; + using Shared = SharedObj<_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/internal/shared-obj.hpp b/src/cpp-common/bt2/internal/shared-obj.hpp deleted file mode 100644 index 7846b324..00000000 --- a/src/cpp-common/bt2/internal/shared-obj.hpp +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Copyright (c) 2019-2020 Philippe Proulx - * - * SPDX-License-Identifier: MIT - */ - -#ifndef BABELTRACE_CPP_COMMON_BT2_INTERNAL_SHARED_OBJ_HPP -#define BABELTRACE_CPP_COMMON_BT2_INTERNAL_SHARED_OBJ_HPP - -#include "common/assert.h" -#include "cpp-common/optional.hpp" - -namespace bt2 { -namespace internal { - -/* - * 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 internal */ -} /* namespace bt2 */ - -#endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_SHARED_OBJ_HPP */ diff --git a/src/cpp-common/bt2/message.hpp b/src/cpp-common/bt2/message.hpp index e284afe2..aee0e7c9 100644 --- a/src/cpp-common/bt2/message.hpp +++ b/src/cpp-common/bt2/message.hpp @@ -18,8 +18,8 @@ #include "cpp-common/optional.hpp" #include "borrowed-object.hpp" -#include "internal/shared-obj.hpp" #include "internal/utils.hpp" +#include "shared-obj.hpp" namespace bt2 { namespace internal { @@ -37,11 +37,11 @@ struct MessageRefFuncs final } }; -template -using SharedMessage = internal::SharedObj; - } /* namespace internal */ +template +using SharedMessage = SharedObj; + template class CommonStreamBeginningMessage; @@ -89,7 +89,7 @@ protected: using _ThisCommonMessage = CommonMessage; public: - using Shared = internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonMessage(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { @@ -231,7 +231,7 @@ private: CommonStream>::type; public: - using Shared = internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonStreamBeginningMessage(const _LibObjPtr libObjPtr) noexcept : _ThisCommonMessage {libObjPtr} @@ -349,7 +349,7 @@ private: CommonStream>::type; public: - using Shared = internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonStreamEndMessage(const _LibObjPtr libObjPtr) noexcept : _ThisCommonMessage {libObjPtr} @@ -466,7 +466,7 @@ private: CommonPacket>::type; public: - using Shared = internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonPacketBeginningMessage(const _LibObjPtr libObjPtr) noexcept : _ThisCommonMessage {libObjPtr} @@ -579,7 +579,7 @@ private: CommonPacket>::type; public: - using Shared = internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonPacketEndMessage(const _LibObjPtr libObjPtr) noexcept : _ThisCommonMessage {libObjPtr} @@ -691,7 +691,7 @@ private: CommonEvent>::type; public: - using Shared = internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonEventMessage(const _LibObjPtr libObjPtr) noexcept : _ThisCommonMessage {libObjPtr} @@ -795,7 +795,7 @@ private: CommonStream>::type; public: - using Shared = internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonDiscardedEventsMessage(const _LibObjPtr libObjPtr) noexcept : _ThisCommonMessage {libObjPtr} @@ -929,7 +929,7 @@ private: CommonStream>::type; public: - using Shared = internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonDiscardedPacketsMessage(const _LibObjPtr libObjPtr) noexcept : _ThisCommonMessage {libObjPtr} @@ -1036,8 +1036,7 @@ private: using typename CommonMessage::_ThisCommonMessage; public: - using Shared = - internal::SharedMessage, LibObjT>; + using Shared = SharedMessage, LibObjT>; explicit CommonMessageIteratorInactivityMessage(const _LibObjPtr libObjPtr) noexcept : _ThisCommonMessage {libObjPtr} diff --git a/src/cpp-common/bt2/shared-obj.hpp b/src/cpp-common/bt2/shared-obj.hpp new file mode 100644 index 00000000..d0a2d7f9 --- /dev/null +++ b/src/cpp-common/bt2/shared-obj.hpp @@ -0,0 +1,298 @@ +/* + * 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/trace-ir.hpp b/src/cpp-common/bt2/trace-ir.hpp index 88ac7b0f..293e9954 100644 --- a/src/cpp-common/bt2/trace-ir.hpp +++ b/src/cpp-common/bt2/trace-ir.hpp @@ -20,6 +20,7 @@ #include "field-class.hpp" #include "field.hpp" #include "internal/utils.hpp" +#include "shared-obj.hpp" #include "value.hpp" namespace bt2 { @@ -324,7 +325,7 @@ private: ConstStructureField, StructureField>::type; public: - using Shared = internal::SharedObj<_ThisCommonPacket, LibObjT, internal::PacketRefFuncs>; + using Shared = SharedObj<_ThisCommonPacket, LibObjT, internal::PacketRefFuncs>; explicit CommonPacket(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { @@ -495,7 +496,7 @@ private: CommonTrace>::type; public: - using Shared = internal::SharedObj<_ThisCommonStream, LibObjT, internal::StreamRefFuncs>; + using Shared = SharedObj<_ThisCommonStream, LibObjT, internal::StreamRefFuncs>; using Class = typename std::conditional::value, CommonStreamClass, @@ -728,7 +729,7 @@ private: CommonStream>::type; public: - using Shared = internal::SharedObj<_ThisCommonTrace, LibObjT, internal::TraceRefFuncs>; + using Shared = SharedObj<_ThisCommonTrace, LibObjT, internal::TraceRefFuncs>; using Class = typename std::conditional::value, CommonTraceClass, @@ -1065,8 +1066,7 @@ private: StructureFieldClass>::type; public: - using Shared = - internal::SharedObj<_ThisCommonEventClass, LibObjT, internal::EventClassRefFuncs>; + using Shared = SharedObj<_ThisCommonEventClass, LibObjT, internal::EventClassRefFuncs>; using UserAttributes = typename std::conditional::value, ConstMapValue, MapValue>::type; @@ -1448,8 +1448,7 @@ private: typename std::conditional::value, ConstClockClass, ClockClass>::type; public: - using Shared = - internal::SharedObj<_ThisCommonStreamClass, LibObjT, internal::StreamClassRefFuncs>; + using Shared = SharedObj<_ThisCommonStreamClass, LibObjT, internal::StreamClassRefFuncs>; using UserAttributes = typename std::conditional::value, ConstMapValue, MapValue>::type; @@ -1931,8 +1930,7 @@ private: CommonStreamClass>::type; public: - using Shared = - internal::SharedObj<_ThisCommonTraceClass, LibObjT, internal::TraceClassRefFuncs>; + using Shared = SharedObj<_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 c78b5701..19bc357d 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -21,8 +21,8 @@ #include "borrowed-object.hpp" #include "common-iter.hpp" #include "exc.hpp" -#include "internal/shared-obj.hpp" #include "internal/utils.hpp" +#include "shared-obj.hpp" namespace bt2 { namespace internal { @@ -40,11 +40,11 @@ struct ValueRefFuncs final } }; -template -using SharedValue = internal::SharedObj; - } /* namespace internal */ +template +using SharedValue = SharedObj; + template class CommonNullValue; @@ -128,7 +128,7 @@ protected: using _ThisCommonValue = CommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; explicit CommonValue(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { @@ -265,7 +265,7 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; CommonNullValue() noexcept : _ThisCommonValue {bt_value_null} { @@ -320,7 +320,7 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; using Value = bool; explicit CommonBoolValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} @@ -403,7 +403,7 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; using Value = std::uint64_t; explicit CommonUnsignedIntegerValue(const _LibObjPtr libObjPtr) noexcept : @@ -489,7 +489,7 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; using Value = std::int64_t; explicit CommonSignedIntegerValue(const _LibObjPtr libObjPtr) noexcept : @@ -575,7 +575,7 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; using Value = double; explicit CommonRealValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} @@ -658,7 +658,7 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; explicit CommonStringValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { @@ -774,7 +774,7 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; using Iterator = CommonIterator, CommonValue>; explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} @@ -1085,7 +1085,7 @@ private: using typename CommonValue::_ThisCommonValue; public: - using Shared = internal::SharedValue, LibObjT>; + using Shared = SharedValue, LibObjT>; explicit CommonMapValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} {