2 * Copyright (c) 2019-2020 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_INTERNAL_SHARED_OBJ_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_INTERNAL_SHARED_OBJ_HPP
10 #include "common/assert.h"
11 #include "cpp-common/optional.hpp"
17 * An instance of this class wraps an optional instance of `ObjT` and
18 * manages the reference counting of the underlying libbabeltrace2
21 * When you move a shared object, it becomes invalid, in that
22 * operator*() and operator->() will either fail to assert in debug mode
23 * or trigger a segmentation fault.
25 * `LibObjT` is the direct libbabeltrace2 object type, for example
26 * `bt_stream_class` or `const bt_value`.
28 * RefFuncsT::get() must accept a `const LibObjT *` value and increment
29 * its reference count.
31 * RefFuncsT::put() must accept a `const LibObjT *` value and decrement
32 * its reference count.
34 template <typename ObjT, typename LibObjT, typename RefFuncsT>
38 * This makes it possible for a
39 * `SharedObj<Something, bt_something, ...>` instance to get
40 * assigned an instance of
41 * `SharedObj<SpecificSomething, bt_something, ...>` (copy/move
42 * constructor and assignment operator), given that
43 * `SpecificSomething` inherits `Something`.
45 template <typename AnyObjT, typename AnyLibObjT, typename AnyRefFuncsT>
46 friend class SharedObj;
50 * Builds a shared object from `obj` without getting a reference.
52 explicit SharedObj(const ObjT& obj) noexcept : _mObj {obj}
58 * Builds a shared object from `obj` without getting a reference.
60 static SharedObj createWithoutRef(const ObjT& obj) noexcept
62 return SharedObj {obj};
66 * Builds a shared object from `libObjPtr` without getting a
69 static SharedObj createWithoutRef(LibObjT * const libObjPtr) noexcept
71 return SharedObj::createWithoutRef(ObjT {libObjPtr});
75 * Builds a shared object from `obj`, immediately getting a new
78 static SharedObj createWithRef(const ObjT& obj) noexcept
80 SharedObj sharedObj {obj};
87 * Builds a shared object from `libObjPtr`, immediately getting a new
90 static SharedObj createWithRef(LibObjT * const libObjPtr) noexcept
92 return SharedObj::createWithRef(ObjT {libObjPtr});
96 * Generic copy constructor.
98 * See the `friend class SharedObj` comment above.
100 template <typename OtherObjT, typename OtherLibObjT>
101 SharedObj(const SharedObj<OtherObjT, OtherLibObjT, RefFuncsT>& other) noexcept :
108 * Generic move constructor.
110 * See the `friend class SharedObj` comment above.
112 template <typename OtherObjT, typename OtherLibObjT>
113 SharedObj(SharedObj<OtherObjT, OtherLibObjT, RefFuncsT>&& other) noexcept : _mObj {other._mObj}
115 /* Reset moved-from object */
120 * Generic copy assignment operator.
122 * See the `friend class SharedObj` comment above.
124 template <typename OtherObjT, typename OtherLibObjT>
125 SharedObj& operator=(const SharedObj<OtherObjT, OtherLibObjT, RefFuncsT>& other) noexcept
127 /* Put current object's reference */
130 /* Set new current object and get a reference */
138 * Generic move assignment operator.
140 * See the `friend class SharedObj` comment above.
142 template <typename OtherObjT, typename OtherLibObjT>
143 SharedObj& operator=(SharedObj<OtherObjT, OtherLibObjT, RefFuncsT>&& other) noexcept
145 /* Put current object's reference */
148 /* Set new current object */
151 /* Reset moved-from object */
162 ObjT& operator*() noexcept
164 BT_ASSERT_DBG(_mObj);
168 const ObjT& operator*() const noexcept
170 BT_ASSERT_DBG(_mObj);
174 ObjT *operator->() noexcept
176 BT_ASSERT_DBG(_mObj);
180 const ObjT *operator->() const noexcept
182 BT_ASSERT_DBG(_mObj);
188 * Resets this shared object.
190 * To be used when moving it.
192 void _reset() noexcept
198 * Gets a new reference using the configured libbabeltrace2
199 * reference incrementation function.
201 void _getRef() const noexcept
204 RefFuncsT::get(_mObj->libObjPtr());
209 * Puts a reference using the configured libbabeltrace2 reference
210 * decrementation function.
212 void _putRef() const noexcept
215 RefFuncsT::put(_mObj->libObjPtr());
219 nonstd::optional<ObjT> _mObj;
222 } /* namespace internal */
223 } /* namespace bt2 */
225 #endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_SHARED_OBJ_HPP */