2 * Copyright 2019-2020 (c) Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP
10 #include <type_traits>
12 #include "common/assert.h"
17 template <typename ObjT, typename LibObjT, typename RefFuncsT>
21 * An instance of this class wraps a pointer to a libbabeltrace2 object
22 * of type `LibObjT` without managing any reference counting.
24 * This is an abstract base class for any libbabeltrace2 object wrapper.
26 * `LibObjT` is the direct libbabeltrace2 object type, for example
27 * `bt_stream_class` or `const bt_value`.
29 * Methods of a derived class can call _libObjPtr() to access the
30 * libbabeltrace2 object pointer.
32 template <typename LibObjT>
35 static_assert(!std::is_pointer<LibObjT>::value, "`LibObjT` must not be a pointer");
38 * This makes it possible for a `BorrowedObj<const bt_something>`
39 * instance to get assigned an instance of
40 * `BorrowedObj<bt_something>` (copy constructor and assignment
43 * C++ forbids the other way around.
45 template <typename AnyLibObjT>
46 friend class BorrowedObj;
49 * This is to allow a `SharedObj<_ThisBorrowedObj, LibObjT, ...>`
50 * instance containing a `BorrowedObj<LibObjT>` instance to access
51 * _libObjPtr() in order to increment/decrement its libbabeltrace2
54 template <typename ObjT, typename AnyLibObjT, typename RefFuncsT>
55 friend class SharedObj;
58 // libbabeltrace2 object pointer
59 using _LibObjPtr = LibObjT *;
61 // This complete borrowed object
62 using _ThisBorrowedObj = BorrowedObj<LibObjT>;
65 * Builds a borrowed object to wrap the libbabeltrace2 object
66 * pointer `libObjPtr`.
68 * `libObjPtr` must not be `nullptr`.
70 explicit BorrowedObj(const _LibObjPtr libObjPtr) noexcept : _mLibObjPtr {libObjPtr}
76 * Generic copy constructor.
78 * This converting constructor accepts both an instance of
79 * `_ThisBorrowedObj` and an instance (`other`) of
80 * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const`
81 * version of `LibObjT`, if applicable.
83 * This makes it possible for a `BorrowedObj<const bt_something>`
84 * instance to be built from an instance of
85 * `BorrowedObj<bt_something>`. C++ forbids the other way around.
87 template <typename OtherLibObjT>
88 BorrowedObj(const BorrowedObj<OtherLibObjT>& other) noexcept : BorrowedObj {other._mLibObjPtr}
93 * Generic assignment operator.
95 * This operator accepts both an instance of
96 * `_ThisBorrowedObj` and an instance (`other`) of
97 * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const`
98 * version of `LibObjT`, if applicable.
100 * This makes it possible for a `BorrowedObj<const bt_something>`
101 * instance to get assigned an instance of
102 * `BorrowedObj<bt_something>`. C++ forbids the other way around.
104 template <typename OtherLibObjT>
105 _ThisBorrowedObj& operator=(const BorrowedObj<OtherLibObjT>& other) noexcept
107 _mLibObjPtr = other._mLibObjPtr;
111 // Wrapped libbabeltrace2 object pointer
112 _LibObjPtr _libObjPtr() const noexcept
118 _LibObjPtr _mLibObjPtr;
121 } // namespace internal
124 #endif // BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP