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;
49 /* This complete shared object */
50 using ThisSharedObj = SharedObj<ObjT, LibObjT, RefFuncsT>;
53 * Builds a shared object from `obj` without an initial reference.
55 * Use this constructor to build a shared object wrapping a newly
56 * created libbabeltrace2 object.
58 * Use createWithInitialRef() to build a shared object having an
59 * initial reference count.
61 explicit SharedObj(const ObjT& obj) noexcept : _mObj {obj}
66 * Builds a shared object from `obj` with an initial reference.
68 * Use this constructor to build a shared object wrapping a newly
69 * created libbabeltrace2 object.
71 static ThisSharedObj createWithInitialRef(const ObjT& obj) noexcept
73 ThisSharedObj sharedObj {obj};
80 * Generic copy constructor.
82 * See the `friend class SharedObj` comment above.
84 template <typename OtherObjT, typename OtherLibObjT>
85 SharedObj(const SharedObj<OtherObjT, OtherLibObjT, RefFuncsT>& other) noexcept :
92 * Generic move constructor.
94 * See the `friend class SharedObj` comment above.
96 template <typename OtherObjT, typename OtherLibObjT>
97 SharedObj(SharedObj<OtherObjT, OtherLibObjT, RefFuncsT>&& other) noexcept : _mObj {other._mObj}
99 /* Reset moved-from object */
104 * Generic copy assignment operator.
106 * See the `friend class SharedObj` comment above.
108 template <typename OtherObjT, typename OtherLibObjT>
109 ThisSharedObj& operator=(const SharedObj<OtherObjT, OtherLibObjT, RefFuncsT>& other) noexcept
111 /* Put current object's reference */
114 /* Set new current object and get a reference */
122 * Generic move assignment operator.
124 * See the `friend class SharedObj` comment above.
126 template <typename OtherObjT, typename OtherLibObjT>
127 ThisSharedObj& operator=(SharedObj<OtherObjT, OtherLibObjT, RefFuncsT>&& other) noexcept
129 /* Put current object's reference */
132 /* Set new current object */
135 /* Reset moved-from object */
146 ObjT& operator*() noexcept
148 BT_ASSERT_DBG(_mObj);
152 const ObjT& operator*() const noexcept
154 BT_ASSERT_DBG(_mObj);
158 ObjT *operator->() noexcept
160 BT_ASSERT_DBG(_mObj);
164 const ObjT *operator->() const noexcept
166 BT_ASSERT_DBG(_mObj);
172 * Resets this shared object.
174 * To be used when moving it.
176 void _reset() noexcept
182 * Gets a new reference using the configured libbabeltrace2
183 * reference incrementation function.
185 void _getRef() const noexcept
188 RefFuncsT::get(_mObj->libObjPtr());
193 * Puts a reference using the configured libbabeltrace2 reference
194 * decrementation function.
196 void _putRef() const noexcept
199 RefFuncsT::put(_mObj->libObjPtr());
203 nonstd::optional<ObjT> _mObj;
206 } /* namespace internal */
207 } /* namespace bt2 */
209 #endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_SHARED_OBJ_HPP */