Commit | Line | Data |
---|---|---|
01bf7a3a PP |
1 | /* |
2 | * Copyright 2019-2020 (c) Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: MIT | |
5 | */ | |
6 | ||
7 | #ifndef BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP | |
8 | #define BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP | |
9 | ||
feeaa247 | 10 | #include <functional> |
01bf7a3a PP |
11 | #include <type_traits> |
12 | ||
13 | #include "common/assert.h" | |
14 | ||
15 | namespace bt2 { | |
16 | namespace internal { | |
17 | ||
18 | template <typename ObjT, typename LibObjT, typename RefFuncsT> | |
19 | class SharedObj; | |
20 | ||
21 | /* | |
22 | * An instance of this class wraps a pointer to a libbabeltrace2 object | |
23 | * of type `LibObjT` without managing any reference counting. | |
24 | * | |
25 | * This is an abstract base class for any libbabeltrace2 object wrapper. | |
26 | * | |
27 | * `LibObjT` is the direct libbabeltrace2 object type, for example | |
28 | * `bt_stream_class` or `const bt_value`. | |
29 | * | |
30 | * Methods of a derived class can call _libObjPtr() to access the | |
31 | * libbabeltrace2 object pointer. | |
32 | */ | |
33 | template <typename LibObjT> | |
34 | class BorrowedObj | |
35 | { | |
36 | static_assert(!std::is_pointer<LibObjT>::value, "`LibObjT` must not be a pointer"); | |
37 | ||
38 | /* | |
39 | * This makes it possible for a `BorrowedObj<const bt_something>` | |
40 | * instance to get assigned an instance of | |
41 | * `BorrowedObj<bt_something>` (copy constructor and assignment | |
42 | * operator). | |
43 | * | |
44 | * C++ forbids the other way around. | |
45 | */ | |
46 | template <typename AnyLibObjT> | |
47 | friend class BorrowedObj; | |
48 | ||
49 | /* | |
50 | * This is to allow a `SharedObj<_ThisBorrowedObj, LibObjT, ...>` | |
51 | * instance containing a `BorrowedObj<LibObjT>` instance to access | |
52 | * _libObjPtr() in order to increment/decrement its libbabeltrace2 | |
53 | * reference count. | |
54 | */ | |
55 | template <typename ObjT, typename AnyLibObjT, typename RefFuncsT> | |
56 | friend class SharedObj; | |
57 | ||
58 | protected: | |
b5f55e9f | 59 | /* libbabeltrace2 object pointer */ |
01bf7a3a PP |
60 | using _LibObjPtr = LibObjT *; |
61 | ||
b5f55e9f | 62 | /* This complete borrowed object */ |
01bf7a3a PP |
63 | using _ThisBorrowedObj = BorrowedObj<LibObjT>; |
64 | ||
65 | /* | |
66 | * Builds a borrowed object to wrap the libbabeltrace2 object | |
67 | * pointer `libObjPtr`. | |
68 | * | |
69 | * `libObjPtr` must not be `nullptr`. | |
70 | */ | |
71 | explicit BorrowedObj(const _LibObjPtr libObjPtr) noexcept : _mLibObjPtr {libObjPtr} | |
72 | { | |
73 | BT_ASSERT(libObjPtr); | |
74 | } | |
75 | ||
76 | /* | |
77 | * Generic copy constructor. | |
78 | * | |
79 | * This converting constructor accepts both an instance of | |
80 | * `_ThisBorrowedObj` and an instance (`other`) of | |
81 | * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const` | |
82 | * version of `LibObjT`, if applicable. | |
83 | * | |
84 | * This makes it possible for a `BorrowedObj<const bt_something>` | |
85 | * instance to be built from an instance of | |
86 | * `BorrowedObj<bt_something>`. C++ forbids the other way around. | |
87 | */ | |
88 | template <typename OtherLibObjT> | |
89 | BorrowedObj(const BorrowedObj<OtherLibObjT>& other) noexcept : BorrowedObj {other._mLibObjPtr} | |
90 | { | |
91 | } | |
92 | ||
93 | /* | |
94 | * Generic assignment operator. | |
95 | * | |
96 | * This operator accepts both an instance of | |
97 | * `_ThisBorrowedObj` and an instance (`other`) of | |
98 | * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const` | |
99 | * version of `LibObjT`, if applicable. | |
100 | * | |
101 | * This makes it possible for a `BorrowedObj<const bt_something>` | |
102 | * instance to get assigned an instance of | |
103 | * `BorrowedObj<bt_something>`. C++ forbids the other way around. | |
104 | */ | |
105 | template <typename OtherLibObjT> | |
106 | _ThisBorrowedObj& operator=(const BorrowedObj<OtherLibObjT>& other) noexcept | |
107 | { | |
108 | _mLibObjPtr = other._mLibObjPtr; | |
109 | return *this; | |
110 | } | |
111 | ||
feeaa247 FD |
112 | public: |
113 | /* | |
114 | * Returns a hash of this object, solely based on its raw libbabeltrace2 | |
115 | * pointer. | |
116 | */ | |
117 | std::size_t hash() const noexcept | |
118 | { | |
119 | return std::hash<_LibObjPtr> {}(_mLibObjPtr); | |
120 | } | |
121 | ||
122 | /* | |
123 | * Returns whether or not this object is the exact same as `other`, | |
124 | * solely based on the raw libbabeltrace2 pointers. | |
125 | */ | |
126 | bool isSame(const _ThisBorrowedObj& other) const noexcept | |
127 | { | |
128 | return _mLibObjPtr == other._mLibObjPtr; | |
129 | } | |
130 | ||
131 | protected: | |
b5f55e9f | 132 | /* Wrapped libbabeltrace2 object pointer */ |
01bf7a3a PP |
133 | _LibObjPtr _libObjPtr() const noexcept |
134 | { | |
135 | return _mLibObjPtr; | |
136 | } | |
137 | ||
138 | private: | |
139 | _LibObjPtr _mLibObjPtr; | |
140 | }; | |
141 | ||
b5f55e9f PP |
142 | } /* namespace internal */ |
143 | } /* namespace bt2 */ | |
01bf7a3a | 144 | |
b5f55e9f | 145 | #endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP */ |