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 | * | |
341a67c4 | 30 | * Methods of a derived class can call libObjPtr() to access the |
01bf7a3a PP |
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 | */ | |
96643d28 | 46 | template <typename> |
01bf7a3a PP |
47 | friend class BorrowedObj; |
48 | ||
01bf7a3a | 49 | protected: |
b5f55e9f | 50 | /* libbabeltrace2 object pointer */ |
01bf7a3a PP |
51 | using _LibObjPtr = LibObjT *; |
52 | ||
b5f55e9f | 53 | /* This complete borrowed object */ |
01bf7a3a PP |
54 | using _ThisBorrowedObj = BorrowedObj<LibObjT>; |
55 | ||
56 | /* | |
57 | * Builds a borrowed object to wrap the libbabeltrace2 object | |
58 | * pointer `libObjPtr`. | |
59 | * | |
60 | * `libObjPtr` must not be `nullptr`. | |
61 | */ | |
62 | explicit BorrowedObj(const _LibObjPtr libObjPtr) noexcept : _mLibObjPtr {libObjPtr} | |
63 | { | |
64 | BT_ASSERT(libObjPtr); | |
65 | } | |
66 | ||
67 | /* | |
68 | * Generic copy constructor. | |
69 | * | |
70 | * This converting constructor accepts both an instance of | |
71 | * `_ThisBorrowedObj` and an instance (`other`) of | |
72 | * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const` | |
73 | * version of `LibObjT`, if applicable. | |
74 | * | |
75 | * This makes it possible for a `BorrowedObj<const bt_something>` | |
76 | * instance to be built from an instance of | |
77 | * `BorrowedObj<bt_something>`. C++ forbids the other way around. | |
78 | */ | |
79 | template <typename OtherLibObjT> | |
80 | BorrowedObj(const BorrowedObj<OtherLibObjT>& other) noexcept : BorrowedObj {other._mLibObjPtr} | |
81 | { | |
82 | } | |
83 | ||
84 | /* | |
85 | * Generic assignment operator. | |
86 | * | |
87 | * This operator accepts both an instance of | |
88 | * `_ThisBorrowedObj` and an instance (`other`) of | |
89 | * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const` | |
90 | * version of `LibObjT`, if applicable. | |
91 | * | |
92 | * This makes it possible for a `BorrowedObj<const bt_something>` | |
93 | * instance to get assigned an instance of | |
94 | * `BorrowedObj<bt_something>`. C++ forbids the other way around. | |
95 | */ | |
96 | template <typename OtherLibObjT> | |
97 | _ThisBorrowedObj& operator=(const BorrowedObj<OtherLibObjT>& other) noexcept | |
98 | { | |
99 | _mLibObjPtr = other._mLibObjPtr; | |
100 | return *this; | |
101 | } | |
102 | ||
feeaa247 FD |
103 | public: |
104 | /* | |
105 | * Returns a hash of this object, solely based on its raw libbabeltrace2 | |
106 | * pointer. | |
107 | */ | |
108 | std::size_t hash() const noexcept | |
109 | { | |
110 | return std::hash<_LibObjPtr> {}(_mLibObjPtr); | |
111 | } | |
112 | ||
113 | /* | |
114 | * Returns whether or not this object is the exact same as `other`, | |
115 | * solely based on the raw libbabeltrace2 pointers. | |
116 | */ | |
117 | bool isSame(const _ThisBorrowedObj& other) const noexcept | |
118 | { | |
119 | return _mLibObjPtr == other._mLibObjPtr; | |
120 | } | |
121 | ||
b5f55e9f | 122 | /* Wrapped libbabeltrace2 object pointer */ |
341a67c4 | 123 | _LibObjPtr libObjPtr() const noexcept |
01bf7a3a PP |
124 | { |
125 | return _mLibObjPtr; | |
126 | } | |
127 | ||
128 | private: | |
129 | _LibObjPtr _mLibObjPtr; | |
130 | }; | |
131 | ||
b5f55e9f PP |
132 | } /* namespace internal */ |
133 | } /* namespace bt2 */ | |
01bf7a3a | 134 | |
b5f55e9f | 135 | #endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP */ |