OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reser
ved. | 2 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reser
ved. |
3 * | 3 * |
4 * This library is free software; you can redistribute it and/or | 4 * This library is free software; you can redistribute it and/or |
5 * modify it under the terms of the GNU Library General Public | 5 * modify it under the terms of the GNU Library General Public |
6 * License as published by the Free Software Foundation; either | 6 * License as published by the Free Software Foundation; either |
7 * version 2 of the License, or (at your option) any later version. | 7 * version 2 of the License, or (at your option) any later version. |
8 * | 8 * |
9 * This library is distributed in the hope that it will be useful, | 9 * This library is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 * Library General Public License for more details. | 12 * Library General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 * Boston, MA 02110-1301, USA. | 17 * Boston, MA 02110-1301, USA. |
18 * | 18 * |
19 */ | 19 */ |
20 | 20 |
21 #ifndef WTF_PassRefPtr_h | 21 #ifndef WTF_PassRefPtr_h |
22 #define WTF_PassRefPtr_h | 22 #define WTF_PassRefPtr_h |
23 | 23 |
24 #include "wtf/Allocator.h" | 24 #include "wtf/Allocator.h" |
25 #include "wtf/Assertions.h" | 25 #include "wtf/Assertions.h" |
26 #include "wtf/RawPtr.h" | |
27 #include "wtf/TypeTraits.h" | 26 #include "wtf/TypeTraits.h" |
28 | 27 |
29 namespace WTF { | 28 namespace WTF { |
30 | 29 |
31 template <typename T> class RefPtr; | 30 template <typename T> class RefPtr; |
32 template <typename T> class PassRefPtr; | 31 template <typename T> class PassRefPtr; |
33 template <typename T> PassRefPtr<T> adoptRef(T*); | 32 template <typename T> PassRefPtr<T> adoptRef(T*); |
34 | 33 |
35 inline void adopted(const void*) {} | 34 inline void adopted(const void*) {} |
36 | 35 |
(...skipping 18 matching lines...) Expand all Loading... |
55 if (LIKELY(ptr != 0)) | 54 if (LIKELY(ptr != 0)) |
56 ptr->deref(); | 55 ptr->deref(); |
57 } | 56 } |
58 | 57 |
59 template <typename T> class PassRefPtr { | 58 template <typename T> class PassRefPtr { |
60 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); | 59 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
61 public: | 60 public: |
62 PassRefPtr() : m_ptr(nullptr) {} | 61 PassRefPtr() : m_ptr(nullptr) {} |
63 PassRefPtr(std::nullptr_t) : m_ptr(nullptr) {} | 62 PassRefPtr(std::nullptr_t) : m_ptr(nullptr) {} |
64 PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } | 63 PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } |
65 template <typename U> PassRefPtr(const RawPtr<U>& ptr, EnsurePtrConvertibleA
rgDecl(U, T)) : m_ptr(ptr.get()) { refIfNotNull(m_ptr); } | |
66 explicit PassRefPtr(T& ptr) : m_ptr(&ptr) { m_ptr->ref(); } | 64 explicit PassRefPtr(T& ptr) : m_ptr(&ptr) { m_ptr->ref(); } |
67 // It somewhat breaks the type system to allow transfer of ownership out of | 65 // It somewhat breaks the type system to allow transfer of ownership out of |
68 // a const PassRefPtr. However, it makes it much easier to work with | 66 // a const PassRefPtr. However, it makes it much easier to work with |
69 // PassRefPtr temporaries, and we don't have a need to use real const | 67 // PassRefPtr temporaries, and we don't have a need to use real const |
70 // PassRefPtrs anyway. | 68 // PassRefPtrs anyway. |
71 PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) {} | 69 PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) {} |
72 template <typename U> PassRefPtr(const PassRefPtr<U>& o, EnsurePtrConvertibl
eArgDecl(U, T)) : m_ptr(o.leakRef()) {} | 70 template <typename U> PassRefPtr(const PassRefPtr<U>& o, EnsurePtrConvertibl
eArgDecl(U, T)) : m_ptr(o.leakRef()) {} |
73 | 71 |
74 ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(m_ptr); } | 72 ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(m_ptr); } |
75 | 73 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a,
U* b) | 135 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a,
U* b) |
138 { | 136 { |
139 return a.get() == b; | 137 return a.get() == b; |
140 } | 138 } |
141 | 139 |
142 template <typename T, typename U> inline bool operator==(T* a, const PassRefPtr<
U>& b) | 140 template <typename T, typename U> inline bool operator==(T* a, const PassRefPtr<
U>& b) |
143 { | 141 { |
144 return a == b.get(); | 142 return a == b.get(); |
145 } | 143 } |
146 | 144 |
147 template <typename T, typename U> inline bool operator==(const PassRefPtr<T>& a,
const RawPtr<U>& b) | |
148 { | |
149 return a.get() == b.get(); | |
150 } | |
151 | |
152 template <typename T, typename U> inline bool operator==(const RawPtr<T>& a, con
st PassRefPtr<U>& b) | |
153 { | |
154 return a.get() == b.get(); | |
155 } | |
156 | |
157 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a,
const PassRefPtr<U>& b) | 145 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a,
const PassRefPtr<U>& b) |
158 { | 146 { |
159 return a.get() != b.get(); | 147 return a.get() != b.get(); |
160 } | 148 } |
161 | 149 |
162 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a,
const RefPtr<U>& b) | 150 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a,
const RefPtr<U>& b) |
163 { | 151 { |
164 return a.get() != b.get(); | 152 return a.get() != b.get(); |
165 } | 153 } |
166 | 154 |
167 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, con
st PassRefPtr<U>& b) | 155 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, con
st PassRefPtr<U>& b) |
168 { | 156 { |
169 return a.get() != b.get(); | 157 return a.get() != b.get(); |
170 } | 158 } |
171 | 159 |
172 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a,
U* b) | 160 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a,
U* b) |
173 { | 161 { |
174 return a.get() != b; | 162 return a.get() != b; |
175 } | 163 } |
176 | 164 |
177 template <typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<
U>& b) | 165 template <typename T, typename U> inline bool operator!=(T* a, const PassRefPtr<
U>& b) |
178 { | 166 { |
179 return a != b.get(); | 167 return a != b.get(); |
180 } | 168 } |
181 | 169 |
182 template <typename T, typename U> inline bool operator!=(const PassRefPtr<T>& a,
const RawPtr<U>& b) | |
183 { | |
184 return a.get() != b.get(); | |
185 } | |
186 | |
187 template <typename T, typename U> inline bool operator!=(const RawPtr<T>& a, con
st PassRefPtr<U>& b) | |
188 { | |
189 return a.get() != b.get(); | |
190 } | |
191 | |
192 template <typename T> PassRefPtr<T> adoptRef(T* p) | 170 template <typename T> PassRefPtr<T> adoptRef(T* p) |
193 { | 171 { |
194 adopted(p); | 172 adopted(p); |
195 return PassRefPtr<T>(p, PassRefPtr<T>::AdoptRef); | 173 return PassRefPtr<T>(p, PassRefPtr<T>::AdoptRef); |
196 } | 174 } |
197 | 175 |
198 template <typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const
PassRefPtr<U>& p) | 176 template <typename T, typename U> inline PassRefPtr<T> static_pointer_cast(const
PassRefPtr<U>& p) |
199 { | 177 { |
200 return adoptRef(static_cast<T*>(p.leakRef())); | 178 return adoptRef(static_cast<T*>(p.leakRef())); |
201 } | 179 } |
202 | 180 |
203 template <typename T> inline T* getPtr(const PassRefPtr<T>& p) | 181 template <typename T> inline T* getPtr(const PassRefPtr<T>& p) |
204 { | 182 { |
205 return p.get(); | 183 return p.get(); |
206 } | 184 } |
207 | 185 |
208 } // namespace WTF | 186 } // namespace WTF |
209 | 187 |
210 using WTF::PassRefPtr; | 188 using WTF::PassRefPtr; |
211 using WTF::adoptRef; | 189 using WTF::adoptRef; |
212 using WTF::static_pointer_cast; | 190 using WTF::static_pointer_cast; |
213 | 191 |
214 #endif // WTF_PassRefPtr_h | 192 #endif // WTF_PassRefPtr_h |
OLD | NEW |