blob: 2a6d9b1d296eee0d3b732bf7534db521e0c238fa (
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
|
#pragma once
#include <AK/Assertions.h>
#include <AK/LogStream.h>
#include <AK/Types.h>
namespace AK {
template<typename T>
class OwnPtr;
template<typename T>
inline void ref_if_not_null(T* ptr)
{
if (ptr)
ptr->ref();
}
template<typename T>
inline void deref_if_not_null(T* ptr)
{
if (ptr)
ptr->deref();
}
template<typename T>
class CONSUMABLE(unconsumed) NonnullRefPtr {
public:
typedef T ElementType;
enum AdoptTag { Adopt };
RETURN_TYPESTATE(unconsumed)
NonnullRefPtr(const T& object)
: m_ptr(const_cast<T*>(&object))
{
m_ptr->ref();
}
template<typename U>
RETURN_TYPESTATE(unconsumed)
NonnullRefPtr(const U& object)
: m_ptr(&const_cast<T&>(static_cast<const T&>(object)))
{
m_ptr->ref();
}
RETURN_TYPESTATE(unconsumed)
NonnullRefPtr(AdoptTag, T& object)
: m_ptr(&object)
{
}
RETURN_TYPESTATE(unconsumed)
NonnullRefPtr(NonnullRefPtr&& other)
: m_ptr(&other.leak_ref())
{
}
template<typename U>
RETURN_TYPESTATE(unconsumed)
NonnullRefPtr(NonnullRefPtr<U>&& other)
: m_ptr(static_cast<T*>(&other.leak_ref()))
{
}
RETURN_TYPESTATE(unconsumed)
NonnullRefPtr(const NonnullRefPtr& other)
: m_ptr(const_cast<T*>(other.ptr()))
{
m_ptr->ref();
}
template<typename U>
RETURN_TYPESTATE(unconsumed)
NonnullRefPtr(const NonnullRefPtr<U>& other)
: m_ptr(const_cast<T*>(static_cast<const T*>((other.ptr()))))
{
m_ptr->ref();
}
~NonnullRefPtr()
{
deref_if_not_null(m_ptr);
m_ptr = nullptr;
#ifdef SANITIZE_PTRS
if constexpr (sizeof(T*) == 8)
m_ptr = (T*)(0xb0b0b0b0b0b0b0b0);
else
m_ptr = (T*)(0xb0b0b0b0);
#endif
}
template<typename U>
NonnullRefPtr(const OwnPtr<U>&) = delete;
template<typename U>
NonnullRefPtr& operator=(const OwnPtr<U>&) = delete;
NonnullRefPtr& operator=(const NonnullRefPtr& other)
{
if (m_ptr != other.m_ptr) {
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(other.ptr());
m_ptr->ref();
}
return *this;
}
template<typename U>
NonnullRefPtr& operator=(const NonnullRefPtr<U>& other)
{
if (m_ptr != other.ptr()) {
deref_if_not_null(m_ptr);
m_ptr = const_cast<T*>(static_cast<const T*>(other.ptr()));
m_ptr->ref();
}
return *this;
}
NonnullRefPtr& operator=(NonnullRefPtr&& other)
{
if (this != &other) {
deref_if_not_null(m_ptr);
m_ptr = &other.leak_ref();
}
return *this;
}
template<typename U>
NonnullRefPtr& operator=(NonnullRefPtr<U>&& other)
{
if (this != static_cast<void*>(&other)) {
deref_if_not_null(m_ptr);
m_ptr = static_cast<T*>(&other.leak_ref());
}
return *this;
}
NonnullRefPtr& operator=(T& object)
{
if (m_ptr != &object)
deref_if_not_null(m_ptr);
m_ptr = &object;
m_ptr->ref();
return *this;
}
CALLABLE_WHEN(unconsumed)
SET_TYPESTATE(consumed)
T& leak_ref()
{
ASSERT(m_ptr);
T* leakedPtr = m_ptr;
m_ptr = nullptr;
return *leakedPtr;
}
CALLABLE_WHEN("unconsumed","unknown")
T* ptr()
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN("unconsumed","unknown")
const T* ptr() const
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed)
T* operator->()
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed)
const T* operator->() const
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed)
T& operator*()
{
ASSERT(m_ptr);
return *m_ptr;
}
CALLABLE_WHEN(unconsumed)
const T& operator*() const
{
ASSERT(m_ptr);
return *m_ptr;
}
CALLABLE_WHEN(unconsumed)
operator T*()
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed)
operator const T*() const
{
ASSERT(m_ptr);
return m_ptr;
}
CALLABLE_WHEN(unconsumed)
operator T&()
{
ASSERT(m_ptr);
return *m_ptr;
}
CALLABLE_WHEN(unconsumed)
operator const T&() const
{
ASSERT(m_ptr);
return *m_ptr;
}
private:
NonnullRefPtr() = delete;
T* m_ptr { nullptr };
};
template<typename T>
inline NonnullRefPtr<T> adopt(T& object)
{
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, object);
}
template<typename T>
inline const LogStream& operator<<(const LogStream& stream, const NonnullRefPtr<T>& value)
{
return stream << value.ptr();
}
}
using AK::adopt;
using AK::NonnullRefPtr;
|