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/Assertions.h" | 24 #include "wtf/Assertions.h" |
25 #include "wtf/NullPtr.h" | 25 #include "wtf/NullPtr.h" |
26 #include "wtf/TypeTraits.h" | 26 #include "wtf/TypeTraits.h" |
27 | 27 |
28 #define EnsureRefPtrConvertibleArgDefn \ | |
Nico
2013/08/29 19:58:47
This is identical to EnsureOwnArrayPtrConvertibleA
| |
29 typename EnableIf<IsPointerConvertible<U, T>::Value, bool>::Type | |
30 #define EnsureRefPtrConvertibleArgDecl \ | |
31 EnsureRefPtrConvertibleArgDefn = true | |
32 | |
28 namespace WTF { | 33 namespace WTF { |
29 | 34 |
30 template<typename T> class RefPtr; | 35 template<typename T> class RefPtr; |
31 template<typename T> class PassRefPtr; | 36 template<typename T> class PassRefPtr; |
32 template<typename T> PassRefPtr<T> adoptRef(T*); | 37 template<typename T> PassRefPtr<T> adoptRef(T*); |
33 | 38 |
34 inline void adopted(const void*) { } | 39 inline void adopted(const void*) { } |
35 | 40 |
36 template<typename T> ALWAYS_INLINE void refIfNotNull(T* ptr) | 41 template<typename T> ALWAYS_INLINE void refIfNotNull(T* ptr) |
37 { | 42 { |
38 if (LIKELY(ptr != 0)) | 43 if (LIKELY(ptr != 0)) |
39 ptr->ref(); | 44 ptr->ref(); |
40 } | 45 } |
41 | 46 |
42 template<typename T> ALWAYS_INLINE void derefIfNotNull(T* ptr) | 47 template<typename T> ALWAYS_INLINE void derefIfNotNull(T* ptr) |
43 { | 48 { |
44 if (LIKELY(ptr != 0)) | 49 if (LIKELY(ptr != 0)) |
45 ptr->deref(); | 50 ptr->deref(); |
46 } | 51 } |
47 | 52 |
48 template<typename T> class PassRefPtr { | 53 template<typename T> class PassRefPtr { |
49 public: | 54 public: |
50 PassRefPtr() : m_ptr(0) { } | 55 PassRefPtr() : m_ptr(0) { } |
51 PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } | 56 PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } |
52 // It somewhat breaks the type system to allow transfer of ownership out of | 57 // 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 Pas sRefPtr | 58 // a const PassRefPtr. However, it makes it much easier to work with Pas sRefPtr |
54 // temporaries, and we don't have a need to use real const PassRefPtrs a nyway. | 59 // temporaries, and we don't have a need to use real const PassRefPtrs a nyway. |
55 PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) { } | 60 PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) { } |
56 template<typename U> PassRefPtr(const PassRefPtr<U>& o, EnsurePtrConvert ibleArgDecl(U, T)) : m_ptr(o.leakRef()) { } | 61 template<typename U> PassRefPtr(const PassRefPtr<U>& o, EnsureRefPtrConv ertibleArgDecl) : m_ptr(o.leakRef()) { } |
57 | 62 |
58 ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(m_ptr); } | 63 ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(m_ptr); } |
59 | 64 |
60 template<typename U> PassRefPtr(const RefPtr<U>&, EnsurePtrConvertibleAr gDecl(U, T)); | 65 template<typename U> PassRefPtr(const RefPtr<U>&, EnsureRefPtrConvertibl eArgDecl); |
61 | 66 |
62 T* get() const { return m_ptr; } | 67 T* get() const { return m_ptr; } |
63 | 68 |
64 T* leakRef() const WARN_UNUSED_RETURN; | 69 T* leakRef() const WARN_UNUSED_RETURN; |
65 | 70 |
66 T& operator*() const { return *m_ptr; } | 71 T& operator*() const { return *m_ptr; } |
67 T* operator->() const { return m_ptr; } | 72 T* operator->() const { return m_ptr; } |
68 | 73 |
69 bool operator!() const { return !m_ptr; } | 74 bool operator!() const { return !m_ptr; } |
70 | 75 |
71 // This conversion operator allows implicit conversion to bool but not t o other integer types. | 76 // This conversion operator allows implicit conversion to bool but not t o other integer types. |
72 typedef T* (PassRefPtr::*UnspecifiedBoolType); | 77 typedef T* (PassRefPtr::*UnspecifiedBoolType); |
73 operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::m_ptr : 0; } | 78 operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::m_ptr : 0; } |
74 | 79 |
75 PassRefPtr& operator=(const PassRefPtr&) { COMPILE_ASSERT(!sizeof(T*), P assRefPtr_should_never_be_assigned_to); return *this; } | 80 PassRefPtr& operator=(const PassRefPtr&) { COMPILE_ASSERT(!sizeof(T*), P assRefPtr_should_never_be_assigned_to); return *this; } |
76 | 81 |
77 friend PassRefPtr adoptRef<T>(T*); | 82 friend PassRefPtr adoptRef<T>(T*); |
78 | 83 |
79 private: | 84 private: |
80 // adopting constructor | 85 // adopting constructor |
81 PassRefPtr(T* ptr, bool) : m_ptr(ptr) { } | 86 PassRefPtr(T* ptr, bool) : m_ptr(ptr) { } |
82 | 87 |
83 mutable T* m_ptr; | 88 mutable T* m_ptr; |
84 }; | 89 }; |
85 | 90 |
86 template<typename T> template<typename U> inline PassRefPtr<T>::PassRefPtr(c onst RefPtr<U>& o, EnsurePtrConvertibleArgDefn(U, T)) | 91 template<typename T> template<typename U> inline PassRefPtr<T>::PassRefPtr(c onst RefPtr<U>& o, EnsureRefPtrConvertibleArgDefn) |
87 : m_ptr(o.get()) | 92 : m_ptr(o.get()) |
88 { | 93 { |
89 T* ptr = m_ptr; | 94 T* ptr = m_ptr; |
90 refIfNotNull(ptr); | 95 refIfNotNull(ptr); |
91 } | 96 } |
92 | 97 |
93 template<typename T> inline T* PassRefPtr<T>::leakRef() const | 98 template<typename T> inline T* PassRefPtr<T>::leakRef() const |
94 { | 99 { |
95 T* ptr = m_ptr; | 100 T* ptr = m_ptr; |
96 m_ptr = 0; | 101 m_ptr = 0; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
169 } | 174 } |
170 | 175 |
171 } // namespace WTF | 176 } // namespace WTF |
172 | 177 |
173 using WTF::PassRefPtr; | 178 using WTF::PassRefPtr; |
174 using WTF::adoptRef; | 179 using WTF::adoptRef; |
175 using WTF::static_pointer_cast; | 180 using WTF::static_pointer_cast; |
176 using WTF::const_pointer_cast; | 181 using WTF::const_pointer_cast; |
177 | 182 |
178 #endif // WTF_PassRefPtr_h | 183 #endif // WTF_PassRefPtr_h |
OLD | NEW |