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;
51 * Provides `val` which indicates whether or not you can assign this
52 * object from a borrowed object of type `OtherLibObjT`.
54 template <typename OtherLibObjT>
55 struct _AssignableFromConst final
58 * If `LibObjT` is const (for example, `const bt_value`), then
59 * you may always assign from its non-const equivalent (for
60 * example, `bt_value`). In C (correct):
62 * bt_value * const meow = bt_value_bool_create_init(BT_TRUE);
63 * const bt_value * const mix = meow;
65 * If `LibObjT` is non-const, then you may not assign from its
66 * const equivalent. In C (not correct):
68 * const bt_value * const meow =
69 * bt_value_array_borrow_element_by_index_const(some_val, 17);
70 * bt_value * const mix = meow;
72 static constexpr bool val =
73 std::is_const<LibObjT>::value || !std::is_const<OtherLibObjT>::value;
77 /* libbabeltrace2 object pointer */
78 using _LibObjPtr = LibObjT *;
80 /* This complete borrowed object */
81 using _ThisBorrowedObj = BorrowedObj<LibObjT>;
84 * Builds a borrowed object to wrap the libbabeltrace2 object
85 * pointer `libObjPtr`.
87 * `libObjPtr` must not be `nullptr`.
89 explicit BorrowedObj(const _LibObjPtr libObjPtr) noexcept : _mLibObjPtr {libObjPtr}
94 /* Default copy operations */
95 BorrowedObj(const BorrowedObj&) noexcept = default;
96 BorrowedObj& operator=(const BorrowedObj&) noexcept = default;
99 * Generic "copy" constructor.
101 * This converting constructor accepts both an instance of
102 * `_ThisBorrowedObj` and an instance (`other`) of
103 * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const`
104 * version of `LibObjT`, if applicable.
106 * This makes it possible for a `BorrowedObj<const bt_something>`
107 * instance to be built from an instance of
108 * `BorrowedObj<bt_something>`. C++ forbids the other way around.
110 template <typename OtherLibObjT>
111 BorrowedObj(const BorrowedObj<OtherLibObjT>& other) noexcept : BorrowedObj {other._mLibObjPtr}
113 static_assert(_AssignableFromConst<OtherLibObjT>::val,
114 "Don't assign a non-const wrapper from a const wrapper.");
118 * Generic "assignment" operator.
120 * This operator accepts both an instance of
121 * `_ThisBorrowedObj` and an instance (`other`) of
122 * `BorrowedObj<ConstLibObjT>`, where `ConstLibObjT` is the `const`
123 * version of `LibObjT`, if applicable.
125 * This makes it possible for a `BorrowedObj<const bt_something>`
126 * instance to get assigned an instance of
127 * `BorrowedObj<bt_something>`. C++ forbids the other way around,
128 * therefore we use `_EnableIfAssignableT` to show a more relevant
129 * context in the compiler error message.
131 template <typename OtherLibObjT>
132 _ThisBorrowedObj& operator=(const BorrowedObj<OtherLibObjT>& other) noexcept
134 static_assert(_AssignableFromConst<OtherLibObjT>::val,
135 "Don't assign a non-const wrapper from a const wrapper.");
137 _mLibObjPtr = other._mLibObjPtr;
143 * Returns a hash of this object, solely based on its raw libbabeltrace2
146 std::size_t hash() const noexcept
148 return std::hash<_LibObjPtr> {}(_mLibObjPtr);
152 * Returns whether or not this object is the exact same as `other`,
153 * solely based on the raw libbabeltrace2 pointers.
155 bool isSame(const _ThisBorrowedObj& other) const noexcept
157 return _mLibObjPtr == other._mLibObjPtr;
160 /* Wrapped libbabeltrace2 object pointer */
161 _LibObjPtr libObjPtr() const noexcept
167 _LibObjPtr _mLibObjPtr;
170 } /* namespace internal */
171 } /* namespace bt2 */
173 #endif /* BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP */