| 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) | |
| 37 // If rvalue references are not supported, the copy constructor is | 36 // If rvalue references are not supported, the copy constructor is |
| 38 // public so OwnPtr cannot be marked noncopyable. See note below. | 37 // public so OwnPtr cannot be marked noncopyable. See note below. |
| 39 WTF_MAKE_NONCOPYABLE(OwnPtr); | 38 WTF_MAKE_NONCOPYABLE(OwnPtr); |
| 40 #endif | |
| 41 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(OwnPtr); | 39 WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(OwnPtr); |
| 42 public: | 40 public: |
| 43 typedef typename RemoveExtent<T>::Type ValueType; | 41 typedef typename RemoveExtent<T>::Type ValueType; |
| 44 typedef ValueType* PtrType; | 42 typedef ValueType* PtrType; |
| 45 | 43 |
| 46 OwnPtr() : m_ptr(0) { } | 44 OwnPtr() : m_ptr(0) { } |
| 47 OwnPtr(std::nullptr_t) : m_ptr(0) { } | 45 OwnPtr(std::nullptr_t) : m_ptr(0) { } |
| 48 | 46 |
| 49 // See comment in PassOwnPtr.h for why this takes a const reference. | 47 // See comment in PassOwnPtr.h for why this takes a const reference. |
| 50 OwnPtr(const PassOwnPtr<T>&); | 48 OwnPtr(const PassOwnPtr<T>&); |
| 51 template<typename U> OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleAr
gDecl(U, T)); | 49 template<typename U> OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleAr
gDecl(U, T)); |
| 52 | 50 |
| 53 // Hash table deleted values, which are only constructed and never copie
d or destroyed. | 51 // Hash table deleted values, which are only constructed and never copie
d or destroyed. |
| 54 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } | 52 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { } |
| 55 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedV
alue(); } | 53 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedV
alue(); } |
| 56 | 54 |
| 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 | |
| 65 ~OwnPtr() | 55 ~OwnPtr() |
| 66 { | 56 { |
| 67 OwnedPtrDeleter<T>::deletePtr(m_ptr); | 57 OwnedPtrDeleter<T>::deletePtr(m_ptr); |
| 68 m_ptr = 0; | 58 m_ptr = 0; |
| 69 } | 59 } |
| 70 | 60 |
| 71 PtrType get() const { return m_ptr; } | 61 PtrType get() const { return m_ptr; } |
| 72 | 62 |
| 73 void clear(); | 63 void clear(); |
| 74 PassOwnPtr<T> release(); | 64 PassOwnPtr<T> release(); |
| 75 PtrType leakPtr() WARN_UNUSED_RETURN; | 65 PtrType leakPtr() WARN_UNUSED_RETURN; |
| 76 | 66 |
| 77 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } | 67 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } |
| 78 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } | 68 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } |
| 79 | 69 |
| 80 ValueType& operator[](std::ptrdiff_t i) const; | 70 ValueType& operator[](std::ptrdiff_t i) const; |
| 81 | 71 |
| 82 bool operator!() const { return !m_ptr; } | 72 bool operator!() const { return !m_ptr; } |
| 83 | 73 |
| 84 // This conversion operator allows implicit conversion to bool but not t
o other integer types. | 74 // This conversion operator allows implicit conversion to bool but not t
o other integer types. |
| 85 typedef PtrType OwnPtr::*UnspecifiedBoolType; | 75 typedef PtrType OwnPtr::*UnspecifiedBoolType; |
| 86 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0
; } | 76 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0
; } |
| 87 | 77 |
| 88 OwnPtr& operator=(const PassOwnPtr<T>&); | 78 OwnPtr& operator=(const PassOwnPtr<T>&); |
| 89 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } | 79 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } |
| 90 template<typename U> OwnPtr& operator=(const PassOwnPtr<U>&); | 80 template<typename U> OwnPtr& operator=(const PassOwnPtr<U>&); |
| 91 | 81 |
| 92 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | |
| 93 OwnPtr(OwnPtr&&); | 82 OwnPtr(OwnPtr&&); |
| 94 template<typename U> OwnPtr(OwnPtr<U>&&); | 83 template<typename U> OwnPtr(OwnPtr<U>&&); |
| 95 | 84 |
| 96 OwnPtr& operator=(OwnPtr&&); | 85 OwnPtr& operator=(OwnPtr&&); |
| 97 template<typename U> OwnPtr& operator=(OwnPtr<U>&&); | 86 template<typename U> OwnPtr& operator=(OwnPtr<U>&&); |
| 98 #endif | |
| 99 | 87 |
| 100 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } | 88 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } |
| 101 | 89 |
| 102 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | 90 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 103 | 91 |
| 104 private: | 92 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 | |
| 110 // We should never have two OwnPtrs for the same underlying object (othe
rwise we'll get | 93 // We should never have two OwnPtrs for the same underlying object (othe
rwise we'll get |
| 111 // double-destruction), so these equality operators should never be need
ed. | 94 // double-destruction), so these equality operators should never be need
ed. |
| 112 template<typename U> bool operator==(const OwnPtr<U>&) const { COMPILE_A
SSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } | 95 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; } | 96 template<typename U> bool operator!=(const OwnPtr<U>&) const { COMPILE_A
SSERT(!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; } | 97 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; } | 98 template<typename U> bool operator!=(const PassOwnPtr<U>&) const { COMPI
LE_ASSERT(!sizeof(U*), OwnPtrs_should_never_be_equal); return false; } |
| 116 | 99 |
| 117 PtrType m_ptr; | 100 PtrType m_ptr; |
| 118 }; | 101 }; |
| 119 | 102 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::opera
tor=(const PassOwnPtr<U>& o) | 152 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::opera
tor=(const PassOwnPtr<U>& o) |
| 170 { | 153 { |
| 171 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); | 154 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); |
| 172 PtrType ptr = m_ptr; | 155 PtrType ptr = m_ptr; |
| 173 m_ptr = o.leakPtr(); | 156 m_ptr = o.leakPtr(); |
| 174 ASSERT(!ptr || m_ptr != ptr); | 157 ASSERT(!ptr || m_ptr != ptr); |
| 175 OwnedPtrDeleter<T>::deletePtr(ptr); | 158 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 176 return *this; | 159 return *this; |
| 177 } | 160 } |
| 178 | 161 |
| 179 #if COMPILER_SUPPORTS(CXX_RVALUE_REFERENCES) | |
| 180 template<typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) | 162 template<typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) |
| 181 : m_ptr(o.leakPtr()) | 163 : m_ptr(o.leakPtr()) |
| 182 { | 164 { |
| 183 } | 165 } |
| 184 | 166 |
| 185 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(OwnPtr<U>
&& o) | 167 template<typename T> template<typename U> inline OwnPtr<T>::OwnPtr(OwnPtr<U>
&& o) |
| 186 : m_ptr(o.leakPtr()) | 168 : m_ptr(o.leakPtr()) |
| 187 { | 169 { |
| 188 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); | 170 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); |
| 189 } | 171 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 201 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::opera
tor=(OwnPtr<U>&& o) | 183 template<typename T> template<typename U> inline OwnPtr<T>& OwnPtr<T>::opera
tor=(OwnPtr<U>&& o) |
| 202 { | 184 { |
| 203 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); | 185 COMPILE_ASSERT(!IsArray<T>::value, Pointers_to_array_must_never_be_conve
rted); |
| 204 PtrType ptr = m_ptr; | 186 PtrType ptr = m_ptr; |
| 205 m_ptr = o.leakPtr(); | 187 m_ptr = o.leakPtr(); |
| 206 ASSERT(!ptr || m_ptr != ptr); | 188 ASSERT(!ptr || m_ptr != ptr); |
| 207 OwnedPtrDeleter<T>::deletePtr(ptr); | 189 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 208 | 190 |
| 209 return *this; | 191 return *this; |
| 210 } | 192 } |
| 211 #endif | |
| 212 | 193 |
| 213 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) | 194 template<typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) |
| 214 { | 195 { |
| 215 a.swap(b); | 196 a.swap(b); |
| 216 } | 197 } |
| 217 | 198 |
| 218 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a,
U* b) | 199 template<typename T, typename U> inline bool operator==(const OwnPtr<T>& a,
U* b) |
| 219 { | 200 { |
| 220 return a.get() == b; | 201 return a.get() == b; |
| 221 } | 202 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 238 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<
T>& p) | 219 template<typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<
T>& p) |
| 239 { | 220 { |
| 240 return p.get(); | 221 return p.get(); |
| 241 } | 222 } |
| 242 | 223 |
| 243 } // namespace WTF | 224 } // namespace WTF |
| 244 | 225 |
| 245 using WTF::OwnPtr; | 226 using WTF::OwnPtr; |
| 246 | 227 |
| 247 #endif // WTF_OwnPtr_h | 228 #endif // WTF_OwnPtr_h |
| OLD | NEW |