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 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
50 PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } | 50 PassRefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } |
51 // It somewhat breaks the type system to allow transfer of ownership out
of | 51 // It somewhat breaks the type system to allow transfer of ownership out
of |
52 // a const PassRefPtr. However, it makes it much easier to work with Pas
sRefPtr | 52 // a const PassRefPtr. However, it makes it much easier to work with Pas
sRefPtr |
53 // temporaries, and we don't have a need to use real const PassRefPtrs a
nyway. | 53 // temporaries, and we don't have a need to use real const PassRefPtrs a
nyway. |
54 PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) { } | 54 PassRefPtr(const PassRefPtr& o) : m_ptr(o.leakRef()) { } |
55 template<typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.leakRe
f()) { } | 55 template<typename U> PassRefPtr(const PassRefPtr<U>& o) : m_ptr(o.leakRe
f()) { } |
56 | 56 |
57 ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(m_ptr); } | 57 ALWAYS_INLINE ~PassRefPtr() { derefIfNotNull(m_ptr); } |
58 | 58 |
59 template<typename U> PassRefPtr(const RefPtr<U>&); | 59 template<typename U> PassRefPtr(const RefPtr<U>&); |
60 | 60 |
61 T* get() const { return m_ptr; } | 61 T* get() const { return m_ptr; } |
62 | 62 |
63 T* leakRef() const WARN_UNUSED_RETURN; | 63 T* leakRef() const WARN_UNUSED_RETURN; |
64 | 64 |
65 T& operator*() const { return *m_ptr; } | 65 T& operator*() const { return *m_ptr; } |
66 T* operator->() const { return m_ptr; } | 66 T* operator->() const { return m_ptr; } |
67 | 67 |
68 bool operator!() const { return !m_ptr; } | 68 bool operator!() const { return !m_ptr; } |
69 | 69 |
70 // This conversion operator allows implicit conversion to bool but not t
o other integer types. | 70 // This conversion operator allows implicit conversion to bool but not t
o other integer types. |
71 typedef T* (PassRefPtr::*UnspecifiedBoolType); | 71 typedef T* (PassRefPtr::*UnspecifiedBoolType); |
72 operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::m_ptr
: 0; } | 72 operator UnspecifiedBoolType() const { return m_ptr ? &PassRefPtr::m_ptr
: 0; } |
73 | 73 |
74 PassRefPtr& operator=(const PassRefPtr&) { COMPILE_ASSERT(!sizeof(T*), P
assRefPtr_should_never_be_assigned_to); return *this; } | 74 PassRefPtr& operator=(const PassRefPtr&) { COMPILE_ASSERT(!sizeof(T*), P
assRefPtr_should_never_be_assigned_to); return *this; } |
75 | 75 |
76 friend PassRefPtr adoptRef<T>(T*); | 76 friend PassRefPtr adoptRef<T>(T*); |
77 | 77 |
78 private: | 78 private: |
79 // adopting constructor | 79 // adopting constructor |
80 PassRefPtr(T* ptr, bool) : m_ptr(ptr) { } | 80 PassRefPtr(T* ptr, bool) : m_ptr(ptr) { } |
81 | 81 |
82 mutable T* m_ptr; | 82 mutable T* m_ptr; |
83 }; | 83 }; |
84 | 84 |
85 template<typename T> template<typename U> inline PassRefPtr<T>::PassRefPtr(c
onst RefPtr<U>& o) | 85 template<typename T> template<typename U> inline PassRefPtr<T>::PassRefPtr(c
onst RefPtr<U>& o) |
86 : m_ptr(o.get()) | 86 : m_ptr(o.get()) |
87 { | 87 { |
88 T* ptr = m_ptr; | 88 T* ptr = m_ptr; |
89 refIfNotNull(ptr); | 89 refIfNotNull(ptr); |
90 } | 90 } |
91 | 91 |
92 template<typename T> inline T* PassRefPtr<T>::leakRef() const | 92 template<typename T> inline T* PassRefPtr<T>::leakRef() const |
93 { | 93 { |
94 T* ptr = m_ptr; | 94 T* ptr = m_ptr; |
95 m_ptr = 0; | 95 m_ptr = 0; |
96 return ptr; | 96 return ptr; |
97 } | 97 } |
98 | 98 |
99 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>&
a, const PassRefPtr<U>& b) | 99 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>&
a, const PassRefPtr<U>& b) |
100 { | 100 { |
101 return a.get() == b.get(); | 101 return a.get() == b.get(); |
102 } | 102 } |
103 | 103 |
104 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>&
a, const RefPtr<U>& b) | 104 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>&
a, const RefPtr<U>& b) |
105 { | 105 { |
106 return a.get() == b.get(); | 106 return a.get() == b.get(); |
107 } | 107 } |
108 | 108 |
109 template<typename T, typename U> inline bool operator==(const RefPtr<T>& a,
const PassRefPtr<U>& b) | 109 template<typename T, typename U> inline bool operator==(const RefPtr<T>& a,
const PassRefPtr<U>& b) |
110 { | 110 { |
111 return a.get() == b.get(); | 111 return a.get() == b.get(); |
112 } | 112 } |
113 | 113 |
114 template<typename T, typename U> inline bool operator==(const PassRefPtr<T>&
a, U* b) | 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 PassRefP
tr<U>& b) | |
120 { | 115 { |
121 return a == b.get(); | 116 return a.get() == b; |
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 } | 117 } |
128 | 118 |
129 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>&
a, const RefPtr<U>& b) | 119 template<typename T, typename U> inline bool operator==(T* a, const PassRefP
tr<U>& b) |
130 { | 120 { |
131 return a.get() != b.get(); | 121 return a == b.get(); |
132 } | 122 } |
133 | 123 |
134 template<typename T, typename U> inline bool operator!=(const RefPtr<T>& a,
const PassRefPtr<U>& b) | 124 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>&
a, const PassRefPtr<U>& b) |
135 { | 125 { |
136 return a.get() != b.get(); | 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 } | 137 } |
138 | 138 |
139 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>&
a, U* b) | 139 template<typename T, typename U> inline bool operator!=(const PassRefPtr<T>&
a, U* b) |
140 { | 140 { |
141 return a.get() != b; | 141 return a.get() != b; |
142 } | 142 } |
143 | 143 |
144 template<typename T, typename U> inline bool operator!=(T* a, const PassRefP
tr<U>& b) | 144 template<typename T, typename U> inline bool operator!=(T* a, const PassRefP
tr<U>& b) |
145 { | 145 { |
146 return a != b.get(); | 146 return a != b.get(); |
147 } | 147 } |
148 | 148 |
149 template<typename T> inline PassRefPtr<T> adoptRef(T* p) | 149 template<typename T> inline PassRefPtr<T> adoptRef(T* p) |
150 { | 150 { |
151 adopted(p); | 151 adopted(p); |
152 return PassRefPtr<T>(p, true); | 152 return PassRefPtr<T>(p, true); |
153 } | 153 } |
154 | 154 |
155 template<typename T, typename U> inline PassRefPtr<T> static_pointer_cast(co
nst PassRefPtr<U>& p) | 155 template<typename T, typename U> inline PassRefPtr<T> static_pointer_cast(co
nst PassRefPtr<U>& p) |
156 { | 156 { |
157 return adoptRef(static_cast<T*>(p.leakRef())); | 157 return adoptRef(static_cast<T*>(p.leakRef())); |
158 } | 158 } |
159 | 159 |
160 template<typename T, typename U> inline PassRefPtr<T> const_pointer_cast(con
st PassRefPtr<U>& p) | 160 template<typename T, typename U> inline PassRefPtr<T> const_pointer_cast(con
st PassRefPtr<U>& p) |
161 { | 161 { |
162 return adoptRef(const_cast<T*>(p.leakRef())); | 162 return adoptRef(const_cast<T*>(p.leakRef())); |
163 } | 163 } |
164 | 164 |
165 template<typename T> inline T* getPtr(const PassRefPtr<T>& p) | 165 template<typename T> inline T* getPtr(const PassRefPtr<T>& p) |
166 { | 166 { |
167 return p.get(); | 167 return p.get(); |
168 } | 168 } |
169 | 169 |
170 } // namespace WTF | 170 } // namespace WTF |
171 | 171 |
172 using WTF::PassRefPtr; | 172 using WTF::PassRefPtr; |
173 using WTF::adoptRef; | 173 using WTF::adoptRef; |
174 using WTF::static_pointer_cast; | 174 using WTF::static_pointer_cast; |
175 using WTF::const_pointer_cast; | 175 using WTF::const_pointer_cast; |
176 | 176 |
177 #endif // WTF_PassRefPtr_h | 177 #endif // WTF_PassRefPtr_h |
OLD | NEW |