1/*
2 * Copyright (C) 2005-2019 Apple Inc. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21// RefPtr is documented at http://webkit.org/coding/RefPtr.html
22
23#pragma once
24
25#include <algorithm>
26#include <utility>
27#include <wtf/FastMalloc.h>
28#include <wtf/Ref.h>
29
30namespace WTF {
31
32template<typename T, typename PtrTraits> class RefPtr;
33template<typename T, typename PtrTraits = DumbPtrTraits<T>> RefPtr<T, PtrTraits> adoptRef(T*);
34
35template<typename T> ALWAYS_INLINE void refIfNotNull(T* ptr)
36{
37 if (LIKELY(ptr != nullptr))
38 ptr->ref();
39}
40
41template<typename T> ALWAYS_INLINE void derefIfNotNull(T* ptr)
42{
43 if (LIKELY(ptr != nullptr))
44 ptr->deref();
45}
46
47template<typename T, typename PtrTraits>
48class RefPtr {
49 WTF_MAKE_FAST_ALLOCATED;
50public:
51 typedef T ValueType;
52 typedef ValueType* PtrType;
53
54 static constexpr bool isRefPtr = true;
55
56 ALWAYS_INLINE constexpr RefPtr() : m_ptr(nullptr) { }
57 ALWAYS_INLINE RefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); }
58 ALWAYS_INLINE RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { refIfNotNull(PtrTraits::unwrap(m_ptr)); }
59 template<typename X, typename Y> RefPtr(const RefPtr<X, Y>& o) : m_ptr(o.get()) { refIfNotNull(PtrTraits::unwrap(m_ptr)); }
60
61 ALWAYS_INLINE RefPtr(RefPtr&& o) : m_ptr(o.leakRef()) { }
62 template<typename X, typename Y> RefPtr(RefPtr<X, Y>&& o) : m_ptr(o.leakRef()) { }
63 template<typename X, typename Y> RefPtr(Ref<X, Y>&&);
64
65 // Hash table deleted values, which are only constructed and never copied or destroyed.
66 RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
67 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
68
69 ALWAYS_INLINE ~RefPtr() { derefIfNotNull(PtrTraits::exchange(m_ptr, nullptr)); }
70
71 T* get() const { return PtrTraits::unwrap(m_ptr); }
72
73 Ref<T> releaseNonNull() { ASSERT(m_ptr); Ref<T> tmp(adoptRef(*m_ptr)); m_ptr = nullptr; return tmp; }
74 Ref<const T> releaseConstNonNull() { ASSERT(m_ptr); Ref<const T> tmp(adoptRef(*m_ptr)); m_ptr = nullptr; return tmp; }
75
76 T* leakRef() WARN_UNUSED_RETURN;
77
78 T& operator*() const { ASSERT(m_ptr); return *PtrTraits::unwrap(m_ptr); }
79 ALWAYS_INLINE T* operator->() const { return PtrTraits::unwrap(m_ptr); }
80
81 bool operator!() const { return !m_ptr; }
82
83 // This conversion operator allows implicit conversion to bool but not to other integer types.
84 typedef T* (RefPtr::*UnspecifiedBoolType);
85 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : nullptr; }
86
87 explicit operator bool() const { return !!m_ptr; }
88
89 RefPtr& operator=(const RefPtr&);
90 RefPtr& operator=(T*);
91 RefPtr& operator=(std::nullptr_t);
92 template<typename X, typename Y> RefPtr& operator=(const RefPtr<X, Y>&);
93 RefPtr& operator=(RefPtr&&);
94 template<typename X, typename Y> RefPtr& operator=(RefPtr<X, Y>&&);
95 template<typename X> RefPtr& operator=(Ref<X>&&);
96
97 template<typename X, typename Y> void swap(RefPtr<X, Y>&);
98
99 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
100
101 RefPtr copyRef() && = delete;
102 RefPtr copyRef() const & WARN_UNUSED_RETURN { return RefPtr(m_ptr); }
103
104private:
105 friend RefPtr adoptRef<T, PtrTraits>(T*);
106 template<typename X, typename Y> friend class RefPtr;
107
108 enum AdoptTag { Adopt };
109 RefPtr(T* ptr, AdoptTag) : m_ptr(ptr) { }
110
111 typename PtrTraits::StorageType m_ptr;
112};
113
114template<typename T, typename U>
115template<typename X, typename Y>
116inline RefPtr<T, U>::RefPtr(Ref<X, Y>&& reference)
117 : m_ptr(&reference.leakRef())
118{
119}
120
121template<typename T, typename U>
122inline T* RefPtr<T, U>::leakRef()
123{
124 return U::exchange(m_ptr, nullptr);
125}
126
127template<typename T, typename U>
128inline RefPtr<T, U>& RefPtr<T, U>::operator=(const RefPtr& o)
129{
130 RefPtr ptr = o;
131 swap(ptr);
132 return *this;
133}
134
135template<typename T, typename U>
136template<typename X, typename Y>
137inline RefPtr<T, U>& RefPtr<T, U>::operator=(const RefPtr<X, Y>& o)
138{
139 RefPtr ptr = o;
140 swap(ptr);
141 return *this;
142}
143
144template<typename T, typename U>
145inline RefPtr<T, U>& RefPtr<T, U>::operator=(T* optr)
146{
147 RefPtr ptr = optr;
148 swap(ptr);
149 return *this;
150}
151
152template<typename T, typename U>
153inline RefPtr<T, U>& RefPtr<T, U>::operator=(std::nullptr_t)
154{
155 derefIfNotNull(U::exchange(m_ptr, nullptr));
156 return *this;
157}
158
159template<typename T, typename U>
160inline RefPtr<T, U>& RefPtr<T, U>::operator=(RefPtr&& o)
161{
162 RefPtr ptr = WTFMove(o);
163 swap(ptr);
164 return *this;
165}
166
167template<typename T, typename U>
168template<typename X, typename Y>
169inline RefPtr<T, U>& RefPtr<T, U>::operator=(RefPtr<X, Y>&& o)
170{
171 RefPtr ptr = WTFMove(o);
172 swap(ptr);
173 return *this;
174}
175
176template<typename T, typename V>
177template<typename U>
178inline RefPtr<T, V>& RefPtr<T, V>::operator=(Ref<U>&& reference)
179{
180 RefPtr ptr = WTFMove(reference);
181 swap(ptr);
182 return *this;
183}
184
185template<class T, typename U>
186template<typename X, typename Y>
187inline void RefPtr<T, U>::swap(RefPtr<X, Y>& o)
188{
189 U::swap(m_ptr, o.m_ptr);
190}
191
192template<typename T, typename U, typename X, typename Y, typename = std::enable_if_t<!std::is_same<U, DumbPtrTraits<T>>::value || !std::is_same<Y, DumbPtrTraits<X>>::value>>
193inline void swap(RefPtr<T, U>& a, RefPtr<X, Y>& b)
194{
195 a.swap(b);
196}
197
198template<typename T, typename U, typename X, typename Y>
199inline bool operator==(const RefPtr<T, U>& a, const RefPtr<X, Y>& b)
200{
201 return a.get() == b.get();
202}
203
204template<typename T, typename U, typename X>
205inline bool operator==(const RefPtr<T, U>& a, X* b)
206{
207 return a.get() == b;
208}
209
210template<typename T, typename X, typename Y>
211inline bool operator==(T* a, const RefPtr<X, Y>& b)
212{
213 return a == b.get();
214}
215
216template<typename T, typename U, typename X, typename Y>
217inline bool operator!=(const RefPtr<T, U>& a, const RefPtr<X, Y>& b)
218{
219 return a.get() != b.get();
220}
221
222template<typename T, typename U, typename X>
223inline bool operator!=(const RefPtr<T, U>& a, X* b)
224{
225 return a.get() != b;
226}
227
228template<typename T, typename X, typename Y>
229inline bool operator!=(T* a, const RefPtr<X, Y>& b)
230{
231 return a != b.get();
232}
233
234template<typename T, typename U = DumbPtrTraits<T>, typename X, typename Y>
235inline RefPtr<T, U> static_pointer_cast(const RefPtr<X, Y>& p)
236{
237 return RefPtr<T, U>(static_cast<T*>(p.get()));
238}
239
240template <typename T, typename U>
241struct IsSmartPtr<RefPtr<T, U>> {
242 static const bool value = true;
243};
244
245template<typename T, typename U>
246inline RefPtr<T, U> adoptRef(T* p)
247{
248 adopted(p);
249 return RefPtr<T, U>(p, RefPtr<T, U>::Adopt);
250}
251
252template<typename T> inline RefPtr<T> makeRefPtr(T* pointer)
253{
254 return pointer;
255}
256
257template<typename T> inline RefPtr<T> makeRefPtr(T& reference)
258{
259 return &reference;
260}
261
262template<typename ExpectedType, typename ArgType, typename PtrTraits>
263inline bool is(RefPtr<ArgType, PtrTraits>& source)
264{
265 return is<ExpectedType>(source.get());
266}
267
268template<typename ExpectedType, typename ArgType, typename PtrTraits>
269inline bool is(const RefPtr<ArgType, PtrTraits>& source)
270{
271 return is<ExpectedType>(source.get());
272}
273
274} // namespace WTF
275
276using WTF::RefPtr;
277using WTF::adoptRef;
278using WTF::makeRefPtr;
279using WTF::static_pointer_cast;
280