Add libbabeltrace2 C++ interface base
[babeltrace.git] / src / cpp-common / bt2 / internal / borrowed-obj.hpp
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
10 #include <type_traits>
11
12 #include "common/assert.h"
13
14 namespace bt2 {
15 namespace internal {
16
17 template <typename ObjT, typename LibObjT, typename RefFuncsT>
18 class SharedObj;
19
20 /*
21 * An instance of this class wraps a pointer to a libbabeltrace2 object
22 * of type `LibObjT` without managing any reference counting.
23 *
24 * This is an abstract base class for any libbabeltrace2 object wrapper.
25 *
26 * `LibObjT` is the direct libbabeltrace2 object type, for example
27 * `bt_stream_class` or `const bt_value`.
28 *
29 * Methods of a derived class can call _libObjPtr() to access the
30 * libbabeltrace2 object pointer.
31 */
32 template <typename LibObjT>
33 class BorrowedObj
34 {
35 static_assert(!std::is_pointer<LibObjT>::value, "`LibObjT` must not be a pointer");
36
37 /*
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
41 * operator).
42 *
43 * C++ forbids the other way around.
44 */
45 template <typename AnyLibObjT>
46 friend class BorrowedObj;
47
48 /*
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
52 * reference count.
53 */
54 template <typename ObjT, typename AnyLibObjT, typename RefFuncsT>
55 friend class SharedObj;
56
57 protected:
58 // libbabeltrace2 object pointer
59 using _LibObjPtr = LibObjT *;
60
61 // This complete borrowed object
62 using _ThisBorrowedObj = BorrowedObj<LibObjT>;
63
64 /*
65 * Builds a borrowed object to wrap the libbabeltrace2 object
66 * pointer `libObjPtr`.
67 *
68 * `libObjPtr` must not be `nullptr`.
69 */
70 explicit BorrowedObj(const _LibObjPtr libObjPtr) noexcept : _mLibObjPtr {libObjPtr}
71 {
72 BT_ASSERT(libObjPtr);
73 }
74
75 /*
76 * Generic copy constructor.
77 *
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.
82 *
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.
86 */
87 template <typename OtherLibObjT>
88 BorrowedObj(const BorrowedObj<OtherLibObjT>& other) noexcept : BorrowedObj {other._mLibObjPtr}
89 {
90 }
91
92 /*
93 * Generic assignment operator.
94 *
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.
99 *
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.
103 */
104 template <typename OtherLibObjT>
105 _ThisBorrowedObj& operator=(const BorrowedObj<OtherLibObjT>& other) noexcept
106 {
107 _mLibObjPtr = other._mLibObjPtr;
108 return *this;
109 }
110
111 // Wrapped libbabeltrace2 object pointer
112 _LibObjPtr _libObjPtr() const noexcept
113 {
114 return _mLibObjPtr;
115 }
116
117 private:
118 _LibObjPtr _mLibObjPtr;
119 };
120
121 } // namespace internal
122 } // namespace bt2
123
124 #endif // BABELTRACE_CPP_COMMON_BT2_INTERNAL_BORROWED_OBJ_HPP
This page took 0.032884 seconds and 4 git commands to generate.