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
11 #include <type_traits>
13 #include "common/assert.h"
18 template <typename ObjT, typename LibObjT, typename RefFuncsT>
22 * An instance of this class wraps a pointer to a libbabeltrace2 object
23 * of type `LibObjT` without managing any reference counting.
25 * This is an abstract base class for any libbabeltrace2 object wrapper.
27 * `LibObjT` is the direct libbabeltrace2 object type, for example
28 * `bt_stream_class` or `const bt_value`.
30 * Methods of a derived class can call libObjPtr() to access the
31 * libbabeltrace2 object pointer.
33 template <typename LibObjT>
36 static_assert(!std::is_pointer<LibObjT>::value, "`LibObjT` must not be a pointer");
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"
44 * C++ forbids the other way around.
47 friend class BorrowedObj;
50 /* libbabeltrace2 object pointer */
51 using _LibObjPtr = LibObjT *;
53 /* This complete borrowed object */
54 using _ThisBorrowedObj = BorrowedObj<LibObjT>;
57 * Builds a borrowed object to wrap the libbabeltrace2 object
58 * pointer `libObjPtr`.
60 * `libObjPtr` must not be `nullptr`.
62 explicit BorrowedObj(const _LibObjPtr libObjPtr) noexcept : _mLibObjPtr {libObjPtr}
67 /* Default copy operations */
68 BorrowedObj(const BorrowedObj&) noexcept = default;
69 BorrowedObj& operator=(const BorrowedObj&) noexcept = default;
72 * Generic "copy" constructor.
74 * This converting constructor accepts both an instance of
75 * `_ThisBorrowedObj` and an instance (`other`) of
76 * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const`
77 * version of `LibObjT`, if applicable.
79 * This makes it possible for a `BorrowedObj<const bt_something>`
80 * instance to be built from an instance of
81 * `BorrowedObj<bt_something>`. C++ forbids the other way around.
83 template <typename OtherLibObjT>
84 BorrowedObj(const BorrowedObj<OtherLibObjT>& other) noexcept : BorrowedObj {other._mLibObjPtr}
89 * Generic "assignment" operator.
91 * This operator accepts both an instance of
92 * `_ThisBorrowedObj` and an instance (`other`) of
93 * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const`
94 * version of `LibObjT`, if applicable.
96 * This makes it possible for a `BorrowedObj<const bt_something>`
97 * instance to get assigned an instance of
98 * `BorrowedObj<bt_something>`. C++ forbids the other way around.
100 template <typename OtherLibObjT>
101 _ThisBorrowedObj& operator=(const BorrowedObj<OtherLibObjT>& other) noexcept
103 _mLibObjPtr = other._mLibObjPtr;
109 * Returns a hash of this object, solely based on its raw libbabeltrace2
112 std::size_t hash() const noexcept
114 return std::hash<_LibObjPtr> {}(_mLibObjPtr);
118 * Returns whether or not this object is the exact same as `other`,
119 * solely based on the raw libbabeltrace2 pointers.
121 bool isSame(const _ThisBorrowedObj& other) const noexcept
123 return _mLibObjPtr == other._mLibObjPtr;
126 /* Wrapped libbabeltrace2 object pointer */
127 _LibObjPtr libObjPtr() const noexcept
133 _LibObjPtr _mLibObjPtr;
136 } /* namespace internal */
137 } /* namespace bt2 */
139 #endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP */