Chromium Code Reviews| 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 // RefPtr and PassRefPtr are documented at http://webkit.org/coding/RefPtr.html | 21 // RefPtr and PassRefPtr are documented at http://webkit.org/coding/RefPtr.html |
| 22 | 22 |
| 23 #ifndef WTF_RefPtr_h | 23 #ifndef WTF_RefPtr_h |
| 24 #define WTF_RefPtr_h | 24 #define WTF_RefPtr_h |
| 25 | 25 |
| 26 #include <algorithm> | 26 #include <algorithm> |
| 27 #include <utility> | |
| 27 #include <wtf/FastAllocBase.h> | 28 #include <wtf/FastAllocBase.h> |
| 28 #include <wtf/PassRefPtr.h> | 29 #include <wtf/PassRefPtr.h> |
| 29 | 30 |
| 30 namespace WTF { | 31 namespace WTF { |
| 31 | 32 |
| 32 enum PlacementNewAdoptType { PlacementNewAdopt }; | 33 enum PlacementNewAdoptType { PlacementNewAdopt }; |
| 33 | 34 |
| 34 template<typename T> class PassRefPtr; | 35 template<typename T> class PassRefPtr; |
| 35 | 36 |
| 36 enum HashTableDeletedValueType { HashTableDeletedValue }; | 37 enum HashTableDeletedValueType { HashTableDeletedValue }; |
| 37 | 38 |
| 38 template<typename T> class RefPtr { | 39 template<typename T> class RefPtr { |
| 39 WTF_MAKE_FAST_ALLOCATED; | 40 WTF_MAKE_FAST_ALLOCATED; |
| 40 public: | 41 public: |
| 41 ALWAYS_INLINE RefPtr() : m_ptr(0) { } | 42 ALWAYS_INLINE RefPtr() : m_ptr(0) { } |
| 42 ALWAYS_INLINE RefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } | 43 ALWAYS_INLINE RefPtr(T* ptr) : m_ptr(ptr) { refIfNotNull(ptr); } |
| 43 ALWAYS_INLINE RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { refIfNotNull(m_ ptr); } | 44 ALWAYS_INLINE RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { refIfNotNull(m_ ptr); } |
| 44 template<typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { refIf NotNull(m_ptr); } | 45 template<typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { refIf NotNull(m_ptr); } |
| 45 | 46 |
| 47 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | |
| 48 ALWAYS_INLINE RefPtr(RefPtr&& o) : m_ptr(o.release().leakRef()) { } | |
|
Jeffrey Yasskin
2013/04/26 19:56:46
This looks correct, but you're not getting much be
| |
| 49 template<typename U> RefPtr(RefPtr<U>&& o) : m_ptr(o.release().leakRef() ) { } | |
|
Jeffrey Yasskin
2013/04/26 19:56:46
In the long run, we'll probably want to enable_if
| |
| 50 #endif | |
| 51 | |
| 46 // See comments in PassRefPtr.h for an explanation of why this takes a c onst reference. | 52 // See comments in PassRefPtr.h for an explanation of why this takes a c onst reference. |
| 47 template<typename U> RefPtr(const PassRefPtr<U>&); | 53 template<typename U> RefPtr(const PassRefPtr<U>&); |
| 48 | 54 |
| 49 // Special constructor for cases where we overwrite an object in place. | 55 // Special constructor for cases where we overwrite an object in place. |
| 50 ALWAYS_INLINE RefPtr(PlacementNewAdoptType) { } | 56 ALWAYS_INLINE RefPtr(PlacementNewAdoptType) { } |
| 51 | 57 |
| 52 // Hash table deleted values, which are only constructed and never copie d or destroyed. | 58 // Hash table deleted values, which are only constructed and never copie d or destroyed. |
| 53 RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } | 59 RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } |
| 54 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedV alue(); } | 60 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedV alue(); } |
| 55 | 61 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 70 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0 ; } | 76 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0 ; } |
| 71 | 77 |
| 72 RefPtr& operator=(const RefPtr&); | 78 RefPtr& operator=(const RefPtr&); |
| 73 RefPtr& operator=(T*); | 79 RefPtr& operator=(T*); |
| 74 RefPtr& operator=(const PassRefPtr<T>&); | 80 RefPtr& operator=(const PassRefPtr<T>&); |
| 75 #if !COMPILER_SUPPORTS(CXX_NULLPTR) | 81 #if !COMPILER_SUPPORTS(CXX_NULLPTR) |
| 76 RefPtr& operator=(std::nullptr_t) { clear(); return *this; } | 82 RefPtr& operator=(std::nullptr_t) { clear(); return *this; } |
| 77 #endif | 83 #endif |
| 78 template<typename U> RefPtr& operator=(const RefPtr<U>&); | 84 template<typename U> RefPtr& operator=(const RefPtr<U>&); |
| 79 template<typename U> RefPtr& operator=(const PassRefPtr<U>&); | 85 template<typename U> RefPtr& operator=(const PassRefPtr<U>&); |
| 80 | 86 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 87 RefPtr& operator=(RefPtr&&); | |
|
Jeffrey Yasskin
2013/04/26 19:56:46
I usually recommend writing operator= as
RefPtr&
| |
| 88 template<typename U> RefPtr& operator=(RefPtr<U>&&); | |
| 89 #endif | |
| 81 void swap(RefPtr&); | 90 void swap(RefPtr&); |
| 82 | 91 |
| 83 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | 92 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 84 | 93 |
| 85 private: | 94 private: |
| 86 T* m_ptr; | 95 T* m_ptr; |
| 87 }; | 96 }; |
| 88 | 97 |
| 89 template<typename T> template<typename U> inline RefPtr<T>::RefPtr(const Pas sRefPtr<U>& o) | 98 template<typename T> template<typename U> inline RefPtr<T>::RefPtr(const Pas sRefPtr<U>& o) |
| 90 : m_ptr(o.leakRef()) | 99 : m_ptr(o.leakRef()) |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 return *this; | 144 return *this; |
| 136 } | 145 } |
| 137 | 146 |
| 138 template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::opera tor=(const PassRefPtr<U>& o) | 147 template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::opera tor=(const PassRefPtr<U>& o) |
| 139 { | 148 { |
| 140 T* ptr = m_ptr; | 149 T* ptr = m_ptr; |
| 141 m_ptr = o.leakRef(); | 150 m_ptr = o.leakRef(); |
| 142 derefIfNotNull(ptr); | 151 derefIfNotNull(ptr); |
| 143 return *this; | 152 return *this; |
| 144 } | 153 } |
| 154 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | |
| 155 template<typename T> inline RefPtr<T>& RefPtr<T>::operator=(RefPtr<T>&& o) | |
| 156 { | |
| 157 RefPtr<T> ptr = std::move(o); | |
| 158 swap(ptr); | |
| 159 return *this; | |
| 160 } | |
| 145 | 161 |
| 162 template<typename T> template<typename U> inline RefPtr<T>& RefPtr<T>::opera tor=(RefPtr<U>&& o) | |
| 163 { | |
| 164 RefPtr<T> ptr = std::move(o); | |
| 165 swap(ptr); | |
| 166 return *this; | |
| 167 } | |
| 168 #endif | |
| 146 template<class T> inline void RefPtr<T>::swap(RefPtr<T>& o) | 169 template<class T> inline void RefPtr<T>::swap(RefPtr<T>& o) |
| 147 { | 170 { |
| 148 std::swap(m_ptr, o.m_ptr); | 171 std::swap(m_ptr, o.m_ptr); |
| 149 } | 172 } |
| 150 | 173 |
| 151 template<class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b) | 174 template<class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b) |
| 152 { | 175 { |
| 153 a.swap(b); | 176 a.swap(b); |
| 154 } | 177 } |
| 155 | 178 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 198 return p.get(); | 221 return p.get(); |
| 199 } | 222 } |
| 200 | 223 |
| 201 } // namespace WTF | 224 } // namespace WTF |
| 202 | 225 |
| 203 using WTF::RefPtr; | 226 using WTF::RefPtr; |
| 204 using WTF::static_pointer_cast; | 227 using WTF::static_pointer_cast; |
| 205 using WTF::const_pointer_cast; | 228 using WTF::const_pointer_cast; |
| 206 | 229 |
| 207 #endif // WTF_RefPtr_h | 230 #endif // WTF_RefPtr_h |
| OLD | NEW |