1/*
2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 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#ifndef WTF_PassRefPtr_h
22#define WTF_PassRefPtr_h
23
24#include <wtf/GetPtr.h>
25#include <wtf/Ref.h>
26
27namespace WTF {
28
29 template<typename T> class RefPtr;
30 template<typename T> class PassRefPtr;
31 template<typename T> PassRefPtr<T> adoptRef(T*);
32
33 template<typename T> ALWAYS_INLINE void refIfNotNull(T* ptr)
34 {
35 if (LIKELY(ptr != nullptr))
36 ptr->ref();
37 }
38
39 template<typename T> ALWAYS_INLINE void derefIfNotNull(T* ptr)
40 {
41 if (LIKELY(ptr != nullptr))
42 ptr->deref();
43 }
44
45 template<typename T> class PassRefPtr {
46 public:
47 typedef T ValueType;
48 typedef ValueType* PtrType;
49
50 PassRefPtr() : m_ptr(nullptr) { }
51 PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); }
52 // It somewhat breaks the type system to allow transfer of ownership out of
53 // a const PassRefPtr. However, it makes it much easier to work with PassRefPtr
54 // temporaries, and we don't have a need to use real const PassRefPtrs anyway.
55 PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) { }
56 template<typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.leakRef()) { }
57
58 ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(std::exchange(m_ptr, nullptr)); }
59
60 template<typename U> PassRefPtr(const RefPtr<U>&);
61 template<typename U> PassRefPtr(Ref<U>&& reference) : m_ptr(&reference.leakRef()) { }
62
63 T* get() const { return m_ptr; }
64
65 T* leakRef() const WARN_UNUSED_RETURN;
66
67 T& operator*() const { return *m_ptr; }
68 T* operator->() const { return m_ptr; }
69
70 bool operator!() const { return !m_ptr; }
71
72 // This conversion operator allows implicit conversion to bool but not to other integer types.
73 typedef T* (PassRefPtr::*UnspecifiedBoolType);
74 operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::m_ptr : nullptr; }
75
76 friend PassRefPtr adoptRef<T>(T*);
77
78 private:
79 PassRefPtr& operator=(const PassRefPtr&) = delete;
80
81 enum AdoptTag { Adopt };
82 PassRefPtr(T* ptr, AdoptTag) : m_ptr(ptr) { }
83
84 mutable T* m_ptr;
85 };
86
87 template<typename T> template<typename U> inline PassRefPtr<T>::PassRefPtr(const RefPtr<U>& o)
88 : m_ptr(o.get())
89 {
90 T* ptr = m_ptr;
91 refIfNotNull(ptr);
92 }
93
94 template<typename T> inline T* PassRefPtr<T>::leakRef() const
95 {
96 return std::exchange(m_ptr, nullptr);
97 }
98
99 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const PassRefPtr<U>& b)
100 {
101 return a.get() == b.get();
102 }
103
104 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, const RefPtr<U>& b)
105 {
106 return a.get() == b.get();
107 }
108
109 template<typename T, typename U> inline bool operator==(const RefPtr<T>& a, const PassRefPtr<U>& b)
110 {
111 return a.get() == b.get();
112 }
113
114 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>& a, U* b)
115 {
116 return a.get() == b;
117 }
118
119 template<typename T, typename U> inline bool operator==(T* a, const PassRefPtr<U>& b)
120 {
121 return a == b.get();
122 }
123
124 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const PassRefPtr<U>& b)
125 {
126 return a.get() != b.get();
127 }
128
129 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, const RefPtr<U>& b)
130 {
131 return a.get() != b.get();
132 }
133
134 template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const PassRefPtr<U>& b)
135 {
136 return a.get() != b.get();
137 }
138
139 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a, U* b)
140 {
141 return a.get() != b;
142 }
143
144 template<typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<U>& b)
145 {
146 return a != b.get();
147 }
148
149 template<typename T> inline PassRefPtr<T> adoptRef(T* p)
150 {
151 adopted(p);
152 return PassRefPtr<T>(p, PassRefPtr<T>::Adopt);
153 }
154
155 template<typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const PassRefPtr<U>& p)
156 {
157 return adoptRef(static_cast<T*>(p.leakRef()));
158 }
159
160 template <typename T> struct IsSmartPtr<PassRefPtr<T>> {
161 static const bool value = true;
162 };
163
164} // namespace WTF
165
166using WTF::PassRefPtr;
167using WTF::adoptRef;
168using WTF::static_pointer_cast;
169
170#endif // WTF_PassRefPtr_h
171