| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2013 Intel Corporation. All rights reserved. | 3 * Copyright (C) 2013 Intel Corporation. All rights reserved. |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 #include "wtf/Noncopyable.h" | 26 #include "wtf/Noncopyable.h" |
| 27 #include "wtf/NullPtr.h" | 27 #include "wtf/NullPtr.h" |
| 28 #include "wtf/OwnPtrCommon.h" | 28 #include "wtf/OwnPtrCommon.h" |
| 29 #include <algorithm> | 29 #include <algorithm> |
| 30 | 30 |
| 31 namespace WTF { | 31 namespace WTF { |
| 32 | 32 |
| 33 template<typename T> class PassOwnPtr; | 33 template<typename T> class PassOwnPtr; |
| 34 | 34 |
| 35 template<typename T> class OwnPtr { | 35 template<typename T> class OwnPtr { |
| 36 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 36 // If rvalue references are not supported, the copy constructor is | 37 // If rvalue references are not supported, the copy constructor is |
| 37 // public so OwnPtr cannot be marked noncopyable. See note below. | 38 // public so OwnPtr cannot be marked noncopyable. See note below. |
| 38 WTF_MAKE_NONCOPYABLE(OwnPtr); | 39 WTF_MAKE_NONCOPYABLE(OwnPtr); |
| 40 #endif |
| 39 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(OwnPtr); | 41 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(OwnPtr); |
| 40 public: | 42 public: |
| 41 typedef typename RemoveExtent<T>::Type ValueType; | 43 typedef typename RemoveExtent<T>::Type ValueType; |
| 42 typedef ValueType* PtrType; | 44 typedef ValueType* PtrType; |
| 43 | 45 |
| 44 OwnPtr() : m_ptr(0) { } | 46 OwnPtr() : m_ptr(0) { } |
| 45 OwnPtr(std::nullptr_t) : m_ptr(0) { } | 47 OwnPtr(std::nullptr_t) : m_ptr(0) { } |
| 46 | 48 |
| 47 // See comment in PassOwnPtr.h for why this takes a const reference. | 49 // See comment in PassOwnPtr.h for why this takes a const reference. |
| 48 OwnPtr(const PassOwnPtr<T>&); | 50 OwnPtr(const PassOwnPtr<T>&); |
| 49 template<typename U> OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleAr
gDecl(U, T)); | 51 template<typename U> OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleAr
gDecl(U, T)); |
| 50 | 52 |
| 51 // Hash table deleted values, which are only constructed and never copie
d or destroyed. | 53 // Hash table deleted values, which are only constructed and never copie
d or destroyed. |
| 52 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } | 54 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } |
| 53 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedV
alue(); } | 55 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedV
alue(); } |
| 54 | 56 |
| 57 #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 58 // This copy constructor is used implicitly by gcc when it generates |
| 59 // transients for assigning a PassOwnPtr<T> object to a stack-allocated |
| 60 // OwnPtr<T> object. It should never be called explicitly and gcc |
| 61 // should optimize away the constructor when generating code. |
| 62 OwnPtr(const OwnPtr&); |
| 63 #endif |
| 64 |
| 55 ~OwnPtr() | 65 ~OwnPtr() |
| 56 { | 66 { |
| 57 OwnedPtrDeleter<T>::deletePtr(m_ptr); | 67 OwnedPtrDeleter<T>::deletePtr(m_ptr); |
| 58 m_ptr = 0; | 68 m_ptr = 0; |
| 59 } | 69 } |
| 60 | 70 |
| 61 PtrType get() const { return m_ptr; } | 71 PtrType get() const { return m_ptr; } |
| 62 | 72 |
| 63 void clear(); | 73 void clear(); |
| 64 PassOwnPtr<T> release(); | 74 PassOwnPtr<T> release(); |
| 65 PtrType leakPtr() WARN_UNUSED_RETURN; | 75 PtrType leakPtr() WARN_UNUSED_RETURN; |
| 66 | 76 |
| 67 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } | 77 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } |
| 68 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } | 78 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } |
| 69 | 79 |
| 70 ValueType& operator[](std::ptrdiff_t i) const; | 80 ValueType& operator[](std::ptrdiff_t i) const; |
| 71 | 81 |
| 72 bool operator!() const { return !m_ptr; } | 82 bool operator!() const { return !m_ptr; } |
| 73 | 83 |
| 74 // This conversion operator allows implicit conversion to bool but not t
o other integer types. | 84 // This conversion operator allows implicit conversion to bool but not t
o other integer types. |
| 75 typedef PtrType OwnPtr::*UnspecifiedBoolType; | 85 typedef PtrType OwnPtr::*UnspecifiedBoolType; |
| 76 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0
; } | 86 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0
; } |
| 77 | 87 |
| 78 OwnPtr& operator=(const PassOwnPtr<T>&); | 88 OwnPtr& operator=(const PassOwnPtr<T>&); |
| 79 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } | 89 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } |
| 80 template<typename U> OwnPtr& operator=(const PassOwnPtr<U>&); | 90 template<typename U> OwnPtr& operator=(const PassOwnPtr<U>&); |
| 81 | 91 |
| 92 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 82 OwnPtr(OwnPtr&&); | 93 OwnPtr(OwnPtr&&); |
| 83 template<typename U> OwnPtr(OwnPtr<U>&&); | 94 template<typename U> OwnPtr(OwnPtr<U>&&); |
| 84 | 95 |
| 85 OwnPtr& operator=(OwnPtr&&); | 96 OwnPtr& operator=(OwnPtr&&); |
| 86 template<typename U> OwnPtr& operator=(OwnPtr<U>&&); | 97 template<typename U> OwnPtr& operator=(OwnPtr<U>&&); |
| 98 #endif |
| 87 | 99 |
| 88 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } | 100 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } |
| 89 | 101 |
| 90 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | 102 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 91 | 103 |
| 92 private: | 104 private: |
| 105 #if !COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 106 // If rvalue references are supported, noncopyable takes care of this. |
| 107 OwnPtr& operator=(const OwnPtr&); |
| 108 #endif |
| 109 |
| 93 // We should never have two OwnPtrs for the same underlying object (othe
rwise we'll get | 110 // We should never have two OwnPtrs for the same underlying object (othe
rwise we'll get |
| 94 // double-destruction), so these equality operators should never be need
ed. | 111 // double-destruction), so these equality operators should never be need
ed. |
| 95 template<typename U> bool operator==(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | 112 template<typename U> bool operator==(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } |
| 96 template<typename U> bool operator!=(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | 113 template<typename U> bool operator!=(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } |
| 97 template<typename U> bool operator==(const PassOwnPtr<U>&) const { COMPI
LE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | 114 template<typename U> bool operator==(const PassOwnPtr<U>&) const { COMPI
LE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } |
| 98 template<typename U> bool operator!=(const PassOwnPtr<U>&) const { COMPI
LE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | 115 template<typename U> bool operator!=(const PassOwnPtr<U>&) const { COMPI
LE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } |
| 99 | 116 |
| 100 PtrType m_ptr; | 117 PtrType m_ptr; |
| 101 }; | 118 }; |
| 102 | 119 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::opera
tor=(const PassOwnPtr<U>& o) | 169 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::opera
tor=(const PassOwnPtr<U>& o) |
| 153 { | 170 { |
| 154 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); | 171 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); |
| 155 PtrType ptr = m_ptr; | 172 PtrType ptr = m_ptr; |
| 156 m_ptr = o.leakPtr(); | 173 m_ptr = o.leakPtr(); |
| 157 ASSERT(!ptr || m_ptr != ptr); | 174 ASSERT(!ptr || m_ptr != ptr); |
| 158 OwnedPtrDeleter<T>::deletePtr(ptr); | 175 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 159 return *this; | 176 return *this; |
| 160 } | 177 } |
| 161 | 178 |
| 179 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) |
| 162 template<typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) | 180 template<typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) |
| 163 : m_ptr(o.leakPtr()) | 181 : m_ptr(o.leakPtr()) |
| 164 { | 182 { |
| 165 } | 183 } |
| 166 | 184 |
| 167 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(OwnPtr<U>
&& o) | 185 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(OwnPtr<U>
&& o) |
| 168 : m_ptr(o.leakPtr()) | 186 : m_ptr(o.leakPtr()) |
| 169 { | 187 { |
| 170 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); | 188 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); |
| 171 } | 189 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 183 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::opera
tor=(OwnPtr<U>&& o) | 201 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::opera
tor=(OwnPtr<U>&& o) |
| 184 { | 202 { |
| 185 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); | 203 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); |
| 186 PtrType ptr = m_ptr; | 204 PtrType ptr = m_ptr; |
| 187 m_ptr = o.leakPtr(); | 205 m_ptr = o.leakPtr(); |
| 188 ASSERT(!ptr || m_ptr != ptr); | 206 ASSERT(!ptr || m_ptr != ptr); |
| 189 OwnedPtrDeleter<T>::deletePtr(ptr); | 207 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 190 | 208 |
| 191 return *this; | 209 return *this; |
| 192 } | 210 } |
| 211 #endif |
| 193 | 212 |
| 194 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) | 213 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) |
| 195 { | 214 { |
| 196 a.swap(b); | 215 a.swap(b); |
| 197 } | 216 } |
| 198 | 217 |
| 199 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a,
U* b) | 218 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a,
U* b) |
| 200 { | 219 { |
| 201 return a.get() == b; | 220 return a.get() == b; |
| 202 } | 221 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 219 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<
T>& p) | 238 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<
T>& p) |
| 220 { | 239 { |
| 221 return p.get(); | 240 return p.get(); |
| 222 } | 241 } |
| 223 | 242 |
| 224 } // namespace WTF | 243 } // namespace WTF |
| 225 | 244 |
| 226 using WTF::OwnPtr; | 245 using WTF::OwnPtr; |
| 227 | 246 |
| 228 #endif // WTF_OwnPtr_h | 247 #endif // WTF_OwnPtr_h |
| OLD | NEW |