blob: 5ba8b6e43625367044ac3386dda0102d41f5c384 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
#pragma once
#include <AK/LogStream.h>
#include <AK/NonnullRefPtr.h>
#include <AK/StdLibExtras.h>
#include <AK/Types.h>
namespace AK {
template<typename T>
class OwnPtr;
template<typename T>
class RefPtr {
public:
enum AdoptTag {
Adopt
};
RefPtr() {}
RefPtr(const T* ptr)
: m_ptr(const_cast<T*>(ptr))
{
ref_if_not_null(m_ptr);
}
RefPtr(const T& object)
: m_ptr(const_cast<T*>(&object))
{
m_ptr->ref();
}
RefPtr(AdoptTag, T& object)
: m_ptr(&object)
{
}
RefPtr(RefPtr&& other)
: m_ptr(other.leak_ref())
{
}
RefPtr(const NonnullRefPtr<T>& other)
: m_ptr(const_cast<T*>(other.ptr()))
{
ASSERT(m_ptr);
m_ptr->ref();
}
template<typename U>
RefPtr(const NonnullRefPtr<U>& other)
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
{
ASSERT(m_ptr);
m_ptr->ref();
}
template<typename U>
RefPtr(NonnullRefPtr<U>&& other)
: m_ptr(static_cast<T*>(&other.leak_ref()))
{
ASSERT(m_ptr);
}
template<typename U>
RefPtr(RefPtr<U>&& other)
: m_ptr(static_cast<T*>(other.leak_ref()))
{
}
RefPtr(const RefPtr& other)
: m_ptr(const_cast<T*>(other.ptr()))
{
ref_if_not_null(m_ptr);
}
template<typename U>
RefPtr(const RefPtr<U>& other)
: m_ptr(static_cast<T*>(const_cast<U*>(other.ptr())))
{
ref_if_not_null(m_ptr);
}
~RefPtr()
{
clear();
#ifdef SANITIZE_PTRS
if constexpr (sizeof(T*) == 8)
m_ptr = (T*)(0xe0e0e0e0e0e0e0e0);
else
m_ptr = (T*)(0xe0e0e0e0);
#endif
}
RefPtr(std::nullptr_t) {}
template<typename U>
RefPtr(const OwnPtr<U>&) = delete;
template<typename U>
RefPtr& operator=(const OwnPtr<U>&) = delete;
template<typename U>
void swap(RefPtr<U>& other)
{
::swap(m_ptr, other.m_ptr);
}
RefPtr& operator=(RefPtr&& other)
{
RefPtr tmp = move(other);
swap(tmp);
return *this;
}
template<typename U>
RefPtr& operator=(RefPtr<U>&& other)
{
RefPtr tmp = move(other);
swap(tmp);
return *this;
}
template<typename U>
RefPtr& operator=(NonnullRefPtr<U>&& other)
{
RefPtr tmp = move(other);
swap(tmp);
ASSERT(m_ptr);
return *this;
}
RefPtr& operator=(const NonnullRefPtr<T>& other)
{
RefPtr tmp = other;
swap(tmp);
ASSERT(m_ptr);
return *this;
}
template<typename U>
RefPtr& operator=(const NonnullRefPtr<U>& other)
{
RefPtr tmp = other;
swap(tmp);
ASSERT(m_ptr);
return *this;
}
RefPtr& operator=(const RefPtr& other)
{
RefPtr tmp = other;
swap(tmp);
return *this;
}
template<typename U>
RefPtr& operator=(const RefPtr<U>& other)
{
RefPtr tmp = other;
swap(tmp);
return *this;
}
RefPtr& operator=(const T* ptr)
{
RefPtr tmp = ptr;
swap(tmp);
return *this;
}
RefPtr& operator=(const T& object)
{
RefPtr tmp = object;
swap(tmp);
return *this;
}
RefPtr& operator=(std::nullptr_t)
{
clear();
return *this;
}
void clear()
{
deref_if_not_null(m_ptr);
m_ptr = nullptr;
}
bool operator!() const { return !m_ptr; }
[[nodiscard]] T* leak_ref()
{
return exchange(m_ptr, nullptr);
}
NonnullRefPtr<T> release_nonnull()
{
ASSERT(m_ptr);
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, *leak_ref());
}
T* ptr() { return m_ptr; }
const T* ptr() const { return m_ptr; }
T* operator->()
{
ASSERT(m_ptr);
return m_ptr;
}
const T* operator->() const
{
ASSERT(m_ptr);
return m_ptr;
}
T& operator*()
{
ASSERT(m_ptr);
return *m_ptr;
}
const T& operator*() const
{
ASSERT(m_ptr);
return *m_ptr;
}
operator const T*() const { return m_ptr; }
operator T*() { return m_ptr; }
operator bool() { return !!m_ptr; }
bool operator==(std::nullptr_t) const { return !m_ptr; }
bool operator!=(std::nullptr_t) const { return m_ptr; }
bool operator==(const RefPtr& other) const { return m_ptr == other.m_ptr; }
bool operator!=(const RefPtr& other) const { return m_ptr != other.m_ptr; }
bool operator==(RefPtr& other) { return m_ptr == other.m_ptr; }
bool operator!=(RefPtr& other) { return m_ptr != other.m_ptr; }
bool operator==(const T* other) const { return m_ptr == other; }
bool operator!=(const T* other) const { return m_ptr != other; }
bool operator==(T* other) { return m_ptr == other; }
bool operator!=(T* other) { return m_ptr != other; }
bool is_null() const { return !m_ptr; }
private:
T* m_ptr = nullptr;
};
template<typename T>
inline const LogStream& operator<<(const LogStream& stream, const RefPtr<T>& value)
{
return stream << value.ptr();
}
}
using AK::RefPtr;
|