Debian Codegraph @

clang/test/Analysis/Checkers/WebKit/mock-types.h

raw
1 #ifndef std_move
2 #define std_move
3
4 namespace std {
5
6 template <typename T> struct remove_reference {
7 typedef T type;
8 };
9
10 template <typename T> struct remove_reference<T&> {
11 typedef T type;
12 };
13
14 template<typename T> typename remove_reference<T>::type&& move(T&& t);
15
16 }
17
18 #endif
19
20 namespace WTF {
21
22 template<typename T> typename std::remove_reference<T>::type&& move(T&& t);
23
24 }
25
26 #ifndef mock_types_1103988513531
27 #define mock_types_1103988513531
28
29 namespace std {
30
31 template <typename T>
32 class unique_ptr {
33 private:
34 void *t;
35
36 public:
37 unique_ptr() : t(nullptr) { }
38 unique_ptr(T *t) : t(t) { }
39 ~unique_ptr() {
40 if (t)
41 delete static_cast<T*>(t);
42 }
43 template <typename U> unique_ptr(unique_ptr<U>&& u)
44 : t(u.t)
45 {
46 u.t = nullptr;
47 }
48 T *get() const { return static_cast<T*>(t); }
49 T *operator->() const { return get(); }
50 T &operator*() const { return *get(); }
51 unique_ptr &operator=(T *) { return *this; }
52 explicit operator bool() const { return !!t; }
53 };
54
55 };
56
57 template<typename T>
58 struct RawPtrTraits {
59 using StorageType = T*;
60
61 template<typename U>
62 static T* exchange(StorageType& ptr, U&& newValue)
63 {
64 StorageType oldValue = static_cast<StorageType&&>(ptr);
65 ptr = static_cast<U&&>(newValue);
66 return oldValue;
67 }
68
69 static void swap(StorageType& a, StorageType& b)
70 {
71 StorageType temp = static_cast<StorageType&&>(a);
72 a = static_cast<StorageType&&>(b);
73 b = static_cast<StorageType&&>(temp);
74 }
75 static T* unwrap(const StorageType& ptr) { return ptr; }
76 };
77
78 template<typename T> struct DefaultRefDerefTraits {
79 static T* refIfNotNull(T* ptr)
80 {
81 if (ptr)
82 ptr->ref();
83 return ptr;
84 }
85
86 static T& ref(T& ref)
87 {
88 ref.ref();
89 return ref;
90 }
91
92 static void derefIfNotNull(T* ptr)
93 {
94 if (ptr)
95 ptr->deref();
96 }
97 };
98
99 template <typename T, typename PtrTraits = RawPtrTraits<T>, typename RefDerefTraits = DefaultRefDerefTraits<T>> struct Ref {
100 typename PtrTraits::StorageType t;
101
102 enum AdoptTag { Adopt };
103
104 Ref() : t{} {};
105 Ref(T &t, AdoptTag) : t(&t) { }
106 Ref(T &t) : t(&RefDerefTraits::ref(t)) { }
107 Ref(const Ref& o) : t(RefDerefTraits::refIfNotNull(PtrTraits::unwrap(o.t))) { }
108 Ref(Ref&& o) : t(o.leakRef()) { }
109 ~Ref() { RefDerefTraits::derefIfNotNull(PtrTraits::exchange(t, nullptr)); }
110 Ref& operator=(T &t) {
111 Ref o(t);
112 swap(o);
113 return *this;
114 }
115 Ref& operator=(Ref &&o) {
116 Ref m(o);
117 swap(m);
118 return *this;
119 }
120 void swap(Ref& o) {
121 typename PtrTraits::StorageType tmp = t;
122 t = o.t;
123 o.t = tmp;
124 }
125 T &get() const { return *PtrTraits::unwrap(t); }
126 T *ptr() const { return PtrTraits::unwrap(t); }
127 T *operator->() const { return PtrTraits::unwrap(t); }
128 operator T &() const { return *PtrTraits::unwrap(t); }
129 T* leakRef() { return PtrTraits::exchange(t, nullptr); }
130 };
131
132 template <typename T> Ref<T> adoptRef(T& t) {
133 using Ref = Ref<T>;
134 return Ref(t, Ref::Adopt);
135 }
136
137 template<typename T> class RefPtr;
138 template<typename T> RefPtr<T> adoptRef(T*);
139
140 template <typename T> struct RefPtr {
141 T *t;
142
143 RefPtr() : t(nullptr) { }
144
145 RefPtr(T *t)
146 : t(t) {
147 if (t)
148 t->ref();
149 }
150 RefPtr(Ref<T>&& o)
151 : t(o.leakRef())
152 { }
153 RefPtr(RefPtr&& o)
154 : t(o.t)
155 {
156 o.t = nullptr;
157 }
158 RefPtr(const RefPtr& o)
159 : t(o.t)
160 {
161 if (t)
162 t->ref();
163 }
164 RefPtr operator=(const RefPtr& o)
165 {
166 if (t)
167 t->deref();
168 t = o.t;
169 if (t)
170 t->ref();
171 return *this;
172 }
173 ~RefPtr() {
174 if (t)
175 t->deref();
176 }
177 Ref<T> releaseNonNull() {
178 Ref<T> tmp(*t);
179 if (t)
180 t->deref();
181 t = nullptr;
182 return tmp;
183 }
184 void swap(RefPtr& o) {
185 T* tmp = t;
186 t = o.t;
187 o.t = tmp;
188 }
189 T *get() const { return t; }
190 T *operator->() const { return t; }
191 T &operator*() const { return *t; }
192 RefPtr &operator=(T *t) {
193 RefPtr o(t);
194 swap(o);
195 return *this;
196 }
197 operator bool() const { return t; }
198
199 private:
200 friend RefPtr adoptRef<T>(T*);
201
202 // call_with_adopt_ref in call-args.cpp requires this method to be private.
203 enum AdoptTag { Adopt };
204 RefPtr(T *t, AdoptTag) : t(t) { }
205 };
206
207 template <typename T> RefPtr<T> adoptRef(T* t) {
208 return RefPtr<T>(t, RefPtr<T>::Adopt);
209 }
210
211 template <typename T> bool operator==(const RefPtr<T> &, const RefPtr<T> &) {
212 return false;
213 }
214
215 template <typename T> bool operator==(const RefPtr<T> &, T *) { return false; }
216
217 template <typename T> bool operator==(const RefPtr<T> &, T &) { return false; }
218
219 template <typename T> bool operator!=(const RefPtr<T> &, const RefPtr<T> &) {
220 return false;
221 }
222
223 template <typename T> bool operator!=(const RefPtr<T> &, T *) { return false; }
224
225 template <typename T> bool operator!=(const RefPtr<T> &, T &) { return false; }
226
227 struct RefCountable {
228 static Ref<RefCountable> create();
229 static std::unique_ptr<RefCountable> makeUnique();
230 void ref() {}
231 void deref() {}
232 void method();
233 void constMethod() const;
234 int trivial() { return 123; }
235 RefCountable* next();
236 };
237
238 template <typename T> T *downcast(T *t) { return t; }
239
240 template <typename T> struct CheckedRef {
241 private:
242 T *t;
243
244 public:
245 CheckedRef() : t{} {};
246 CheckedRef(T &t) : t(&t) { t.incrementCheckedPtrCount(); }
247 CheckedRef(const CheckedRef &o) : t(o.t) { if (t) t->incrementCheckedPtrCount(); }
248 ~CheckedRef() { if (t) t->decrementCheckedPtrCount(); }
249 T &get() const { return *t; }
250 T *ptr() const { return t; }
251 T *operator->() const { return t; }
252 operator const T &() const { return *t; }
253 operator T &() { return *t; }
254 };
255
256 template <typename T> struct CheckedPtr {
257 private:
258 T *t;
259
260 public:
261 CheckedPtr() : t(nullptr) {}
262 CheckedPtr(T *t)
263 : t(t) {
264 if (t)
265 t->incrementCheckedPtrCount();
266 }
267 CheckedPtr(Ref<T> &&o)
268 : t(o.leakRef())
269 { }
270 ~CheckedPtr() {
271 if (t)
272 t->decrementCheckedPtrCount();
273 }
274 T *get() const { return t; }
275 T *operator->() const { return t; }
276 T &operator*() const { return *t; }
277 CheckedPtr &operator=(T *);
278 operator bool() const { return t; }
279 };
280
281 class CheckedObj {
282 public:
283 void incrementCheckedPtrCount();
284 void decrementCheckedPtrCount();
285 void method();
286 int trivial() { return 123; }
287 CheckedObj* next();
288 };
289
290 class RefCountableAndCheckable {
291 public:
292 void incrementCheckedPtrCount() const;
293 void decrementCheckedPtrCount() const;
294 void ref() const;
295 void deref() const;
296 void method();
297 int trivial() { return 0; }
298 };
299
300 template <typename T>
301 class UniqueRef {
302 private:
303 T *t;
304
305 public:
306 UniqueRef(T &t) : t(&t) { }
307 ~UniqueRef() {
308 if (t)
309 delete t;
310 }
311 template <typename U> UniqueRef(UniqueRef<U>&& u)
312 : t(u.t)
313 {
314 u.t = nullptr;
315 }
316 T &get() const { return *t; }
317 operator T&() const { return *t; }
318 T *operator->() const { return t; }
319 UniqueRef &operator=(T &) { return *this; }
320 };
321
322 class WeakPtrImpl {
323 private:
324 void* ptr { nullptr };
325 mutable unsigned m_refCount { 0 };
326
327 template <typename U> friend class CanMakeWeakPtr;
328 template <typename U> friend class WeakPtr;
329
330 public:
331 template <typename T>
332 static Ref<WeakPtrImpl> create(T& t)
333 {
334 return adoptRef(*new WeakPtrImpl(t));
335 }
336
337 void ref() const { m_refCount++; }
338 void deref() const {
339 m_refCount--;
340 if (!m_refCount)
341 delete const_cast<WeakPtrImpl*>(this);
342 }
343
344 template <typename T>
345 T* get() { return static_cast<T*>(ptr); }
346 operator bool() const { return !!ptr; }
347 void clear() { ptr = nullptr; }
348
349 private:
350 template <typename T>
351 WeakPtrImpl(T* t)
352 : ptr(static_cast<void*>(t))
353 { }
354 };
355
356 template <typename T>
357 class CanMakeWeakPtr {
358 private:
359 RefPtr<WeakPtrImpl> impl;
360
361 template <typename U> friend class CanMakeWeakPtr;
362 template <typename U> friend class WeakPtr;
363
364 Ref<WeakPtrImpl> createWeakPtrImpl() {
365 if (!impl)
366 impl = WeakPtrImpl::create(static_cast<T>(*this));
367 return *impl;
368 }
369
370 public:
371 ~CanMakeWeakPtr() {
372 if (!impl)
373 return;
374 impl->clear();
375 impl = nullptr;
376 }
377 };
378
379 template <typename T>
380 class WeakPtr {
381 private:
382 RefPtr<WeakPtrImpl> impl;
383
384 public:
385 WeakPtr(T& t) {
386 *this = t;
387 }
388 WeakPtr(T* t) {
389 *this = t;
390 }
391
392 template <typename U>
393 WeakPtr<T> operator=(U& obj) {
394 impl = obj.createWeakPtrImpl();
395 }
396
397 template <typename U>
398 WeakPtr<T> operator=(U* obj) {
399 impl = obj ? obj->createWeakPtrImpl() : nullptr;
400 }
401
402 T* get() {
403 return impl ? impl->get<T>() : nullptr;
404 }
405
406 };
407
408 #endif