| 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 13 matching lines...) Expand all Loading... |
| 24 | 24 |
| 25 #include "wtf/Allocator.h" | 25 #include "wtf/Allocator.h" |
| 26 #include "wtf/HashTableDeletedValueType.h" | 26 #include "wtf/HashTableDeletedValueType.h" |
| 27 #include "wtf/Noncopyable.h" | 27 #include "wtf/Noncopyable.h" |
| 28 #include "wtf/OwnPtrCommon.h" | 28 #include "wtf/OwnPtrCommon.h" |
| 29 #include <algorithm> | 29 #include <algorithm> |
| 30 #include <utility> | 30 #include <utility> |
| 31 | 31 |
| 32 namespace WTF { | 32 namespace WTF { |
| 33 | 33 |
| 34 template <typename T> class PassOwnPtr; | 34 template <typename T> |
| 35 | 35 class PassOwnPtr; |
| 36 template <typename T> class OwnPtr { | 36 |
| 37 USING_FAST_MALLOC(OwnPtr); | 37 template <typename T> |
| 38 WTF_MAKE_NONCOPYABLE(OwnPtr); | 38 class OwnPtr { |
| 39 public: | 39 USING_FAST_MALLOC(OwnPtr); |
| 40 typedef typename std::remove_extent<T>::type ValueType; | 40 WTF_MAKE_NONCOPYABLE(OwnPtr); |
| 41 typedef ValueType* PtrType; | 41 |
| 42 | 42 public: |
| 43 OwnPtr() : m_ptr(nullptr) {} | 43 typedef typename std::remove_extent<T>::type ValueType; |
| 44 OwnPtr(std::nullptr_t) : m_ptr(nullptr) {} | 44 typedef ValueType* PtrType; |
| 45 | 45 |
| 46 // See comment in PassOwnPtr.h for why this takes a const reference. | 46 OwnPtr() : m_ptr(nullptr) {} |
| 47 OwnPtr(const PassOwnPtr<T>&); | 47 OwnPtr(std::nullptr_t) : m_ptr(nullptr) {} |
| 48 template <typename U> OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleArgDe
cl(U, T)); | 48 |
| 49 | 49 // See comment in PassOwnPtr.h for why this takes a const reference. |
| 50 // Hash table deleted values, which are only constructed and never copied or | 50 OwnPtr(const PassOwnPtr<T>&); |
| 51 // destroyed. | 51 template <typename U> |
| 52 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) {} | 52 OwnPtr(const PassOwnPtr<U>&, EnsurePtrConvertibleArgDecl(U, T)); |
| 53 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue
(); } | 53 |
| 54 | 54 // Hash table deleted values, which are only constructed and never copied or |
| 55 ~OwnPtr() | 55 // destroyed. |
| 56 { | 56 OwnPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) {} |
| 57 OwnedPtrDeleter<T>::deletePtr(m_ptr); | 57 bool isHashTableDeletedValue() const { |
| 58 m_ptr = nullptr; | 58 return m_ptr == hashTableDeletedValue(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 PtrType get() const { return m_ptr; } | 61 ~OwnPtr() { |
| 62 | 62 OwnedPtrDeleter<T>::deletePtr(m_ptr); |
| 63 void clear(); | 63 m_ptr = nullptr; |
| 64 PassOwnPtr<T> release(); | 64 } |
| 65 PtrType leakPtr() WARN_UNUSED_RETURN; | 65 |
| 66 | 66 PtrType get() const { return m_ptr; } |
| 67 ValueType& operator*() const { ASSERT(m_ptr); return *m_ptr; } | 67 |
| 68 PtrType operator->() const { ASSERT(m_ptr); return m_ptr; } | 68 void clear(); |
| 69 | 69 PassOwnPtr<T> release(); |
| 70 ValueType& operator[](std::ptrdiff_t i) const; | 70 PtrType leakPtr() WARN_UNUSED_RETURN; |
| 71 | 71 |
| 72 bool operator!() const { return !m_ptr; } | 72 ValueType& operator*() const { |
| 73 | 73 ASSERT(m_ptr); |
| 74 // This conversion operator allows implicit conversion to bool but not to | 74 return *m_ptr; |
| 75 // other integer types. | 75 } |
| 76 typedef PtrType OwnPtr::*UnspecifiedBoolType; | 76 PtrType operator->() const { |
| 77 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; } | 77 ASSERT(m_ptr); |
| 78 | 78 return m_ptr; |
| 79 OwnPtr& operator=(const PassOwnPtr<T>&); | 79 } |
| 80 OwnPtr& operator=(std::nullptr_t) { clear(); return *this; } | 80 |
| 81 template <typename U> OwnPtr& operator=(const PassOwnPtr<U>&); | 81 ValueType& operator[](std::ptrdiff_t i) const; |
| 82 | 82 |
| 83 OwnPtr(OwnPtr&&); | 83 bool operator!() const { return !m_ptr; } |
| 84 template <typename U> OwnPtr(OwnPtr<U>&&); | 84 |
| 85 | 85 // This conversion operator allows implicit conversion to bool but not to |
| 86 OwnPtr& operator=(OwnPtr&&); | 86 // other integer types. |
| 87 template <typename U> OwnPtr& operator=(OwnPtr<U>&&); | 87 typedef PtrType OwnPtr::*UnspecifiedBoolType; |
| 88 | 88 operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::m_ptr : 0; } |
| 89 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } | 89 |
| 90 | 90 OwnPtr& operator=(const PassOwnPtr<T>&); |
| 91 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | 91 OwnPtr& operator=(std::nullptr_t) { |
| 92 | 92 clear(); |
| 93 private: | 93 return *this; |
| 94 // We should never have two OwnPtrs for the same underlying object | 94 } |
| 95 // (otherwise we'll get double-destruction), so these equality operators | 95 template <typename U> |
| 96 // should never be needed. | 96 OwnPtr& operator=(const PassOwnPtr<U>&); |
| 97 template <typename U> bool operator==(const OwnPtr<U>&) const | 97 |
| 98 { | 98 OwnPtr(OwnPtr&&); |
| 99 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 99 template <typename U> |
| 100 return false; | 100 OwnPtr(OwnPtr<U>&&); |
| 101 } | 101 |
| 102 template <typename U> bool operator!=(const OwnPtr<U>&) const | 102 OwnPtr& operator=(OwnPtr&&); |
| 103 { | 103 template <typename U> |
| 104 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 104 OwnPtr& operator=(OwnPtr<U>&&); |
| 105 return false; | 105 |
| 106 } | 106 void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); } |
| 107 template <typename U> bool operator==(const PassOwnPtr<U>&) const | 107 |
| 108 { | 108 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 109 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 109 |
| 110 return false; | 110 private: |
| 111 } | 111 // We should never have two OwnPtrs for the same underlying object |
| 112 template <typename U> bool operator!=(const PassOwnPtr<U>&) const | 112 // (otherwise we'll get double-destruction), so these equality operators |
| 113 { | 113 // should never be needed. |
| 114 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); | 114 template <typename U> |
| 115 return false; | 115 bool operator==(const OwnPtr<U>&) const { |
| 116 } | 116 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 117 | 117 return false; |
| 118 PtrType m_ptr; | 118 } |
| 119 template <typename U> |
| 120 bool operator!=(const OwnPtr<U>&) const { |
| 121 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 122 return false; |
| 123 } |
| 124 template <typename U> |
| 125 bool operator==(const PassOwnPtr<U>&) const { |
| 126 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 127 return false; |
| 128 } |
| 129 template <typename U> |
| 130 bool operator!=(const PassOwnPtr<U>&) const { |
| 131 static_assert(!sizeof(U*), "OwnPtrs should never be equal"); |
| 132 return false; |
| 133 } |
| 134 |
| 135 PtrType m_ptr; |
| 119 }; | 136 }; |
| 120 | 137 |
| 121 template <typename T> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<T>& o) | 138 template <typename T> |
| 122 : m_ptr(o.leakPtr()) | 139 inline OwnPtr<T>::OwnPtr(const PassOwnPtr<T>& o) : m_ptr(o.leakPtr()) {} |
| 123 { | 140 |
| 124 } | 141 template <typename T> |
| 125 | 142 template <typename U> |
| 126 template <typename T> | 143 inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o, |
| 127 template <typename U> inline OwnPtr<T>::OwnPtr(const PassOwnPtr<U>& o, EnsurePtr
ConvertibleArgDefn(U, T)) | 144 EnsurePtrConvertibleArgDefn(U, T)) |
| 128 : m_ptr(o.leakPtr()) | 145 : m_ptr(o.leakPtr()) { |
| 129 { | 146 static_assert(!std::is_array<T>::value, |
| 130 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 147 "pointers to array must never be converted"); |
| 131 } | 148 } |
| 132 | 149 |
| 133 template <typename T> inline void OwnPtr<T>::clear() | 150 template <typename T> |
| 134 { | 151 inline void OwnPtr<T>::clear() { |
| 135 PtrType ptr = m_ptr; | 152 PtrType ptr = m_ptr; |
| 136 m_ptr = nullptr; | 153 m_ptr = nullptr; |
| 137 OwnedPtrDeleter<T>::deletePtr(ptr); | 154 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 138 } | 155 } |
| 139 | 156 |
| 140 template <typename T> inline PassOwnPtr<T> OwnPtr<T>::release() | 157 template <typename T> |
| 141 { | 158 inline PassOwnPtr<T> OwnPtr<T>::release() { |
| 142 PtrType ptr = m_ptr; | 159 PtrType ptr = m_ptr; |
| 143 m_ptr = nullptr; | 160 m_ptr = nullptr; |
| 144 return PassOwnPtr<T>(ptr); | 161 return PassOwnPtr<T>(ptr); |
| 145 } | 162 } |
| 146 | 163 |
| 147 template <typename T> inline typename OwnPtr<T>::PtrType OwnPtr<T>::leakPtr() | 164 template <typename T> |
| 148 { | 165 inline typename OwnPtr<T>::PtrType OwnPtr<T>::leakPtr() { |
| 149 PtrType ptr = m_ptr; | 166 PtrType ptr = m_ptr; |
| 150 m_ptr = nullptr; | 167 m_ptr = nullptr; |
| 151 return ptr; | 168 return ptr; |
| 152 } | 169 } |
| 153 | 170 |
| 154 template <typename T> inline typename OwnPtr<T>::ValueType& OwnPtr<T>::operator[
](std::ptrdiff_t i) const | 171 template <typename T> |
| 155 { | 172 inline typename OwnPtr<T>::ValueType& OwnPtr<T>::operator[]( |
| 156 static_assert(std::is_array<T>::value, "elements access is possible for arra
ys only"); | 173 std::ptrdiff_t i) const { |
| 157 ASSERT(m_ptr); | 174 static_assert(std::is_array<T>::value, |
| 158 ASSERT(i >= 0); | 175 "elements access is possible for arrays only"); |
| 159 return m_ptr[i]; | 176 ASSERT(m_ptr); |
| 160 } | 177 ASSERT(i >= 0); |
| 161 | 178 return m_ptr[i]; |
| 162 template <typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>
& o) | 179 } |
| 163 { | 180 |
| 164 PtrType ptr = m_ptr; | 181 template <typename T> |
| 165 m_ptr = o.leakPtr(); | 182 inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<T>& o) { |
| 166 ASSERT(!ptr || m_ptr != ptr); | 183 PtrType ptr = m_ptr; |
| 167 OwnedPtrDeleter<T>::deletePtr(ptr); | 184 m_ptr = o.leakPtr(); |
| 168 return *this; | 185 ASSERT(!ptr || m_ptr != ptr); |
| 169 } | 186 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 170 | 187 return *this; |
| 171 template <typename T> | 188 } |
| 172 template <typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<U>
& o) | 189 |
| 173 { | 190 template <typename T> |
| 174 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 191 template <typename U> |
| 175 PtrType ptr = m_ptr; | 192 inline OwnPtr<T>& OwnPtr<T>::operator=(const PassOwnPtr<U>& o) { |
| 176 m_ptr = o.leakPtr(); | 193 static_assert(!std::is_array<T>::value, |
| 177 ASSERT(!ptr || m_ptr != ptr); | 194 "pointers to array must never be converted"); |
| 178 OwnedPtrDeleter<T>::deletePtr(ptr); | 195 PtrType ptr = m_ptr; |
| 179 return *this; | 196 m_ptr = o.leakPtr(); |
| 180 } | 197 ASSERT(!ptr || m_ptr != ptr); |
| 181 | 198 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 182 template <typename T> inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) | 199 return *this; |
| 183 : m_ptr(o.leakPtr()) | 200 } |
| 184 { | 201 |
| 185 } | 202 template <typename T> |
| 186 | 203 inline OwnPtr<T>::OwnPtr(OwnPtr<T>&& o) : m_ptr(o.leakPtr()) {} |
| 187 template <typename T> | 204 |
| 188 template <typename U> inline OwnPtr<T>::OwnPtr(OwnPtr<U>&& o) | 205 template <typename T> |
| 189 : m_ptr(o.leakPtr()) | 206 template <typename U> |
| 190 { | 207 inline OwnPtr<T>::OwnPtr(OwnPtr<U>&& o) : m_ptr(o.leakPtr()) { |
| 191 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 208 static_assert(!std::is_array<T>::value, |
| 192 } | 209 "pointers to array must never be converted"); |
| 193 | 210 } |
| 194 template <typename T> inline OwnPtr<T>& OwnPtr<T>::operator=(OwnPtr<T>&& o) | 211 |
| 195 { | 212 template <typename T> |
| 196 PtrType ptr = m_ptr; | 213 inline OwnPtr<T>& OwnPtr<T>::operator=(OwnPtr<T>&& o) { |
| 197 m_ptr = o.leakPtr(); | 214 PtrType ptr = m_ptr; |
| 198 ASSERT(!ptr || m_ptr != ptr); | 215 m_ptr = o.leakPtr(); |
| 199 OwnedPtrDeleter<T>::deletePtr(ptr); | 216 ASSERT(!ptr || m_ptr != ptr); |
| 200 | 217 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 201 return *this; | 218 |
| 202 } | 219 return *this; |
| 203 | 220 } |
| 204 template <typename T> | 221 |
| 205 template <typename U> inline OwnPtr<T>& OwnPtr<T>::operator=(OwnPtr<U>&& o) | 222 template <typename T> |
| 206 { | 223 template <typename U> |
| 207 static_assert(!std::is_array<T>::value, "pointers to array must never be con
verted"); | 224 inline OwnPtr<T>& OwnPtr<T>::operator=(OwnPtr<U>&& o) { |
| 208 PtrType ptr = m_ptr; | 225 static_assert(!std::is_array<T>::value, |
| 209 m_ptr = o.leakPtr(); | 226 "pointers to array must never be converted"); |
| 210 ASSERT(!ptr || m_ptr != ptr); | 227 PtrType ptr = m_ptr; |
| 211 OwnedPtrDeleter<T>::deletePtr(ptr); | 228 m_ptr = o.leakPtr(); |
| 212 | 229 ASSERT(!ptr || m_ptr != ptr); |
| 213 return *this; | 230 OwnedPtrDeleter<T>::deletePtr(ptr); |
| 214 } | 231 |
| 215 | 232 return *this; |
| 216 template <typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) | 233 } |
| 217 { | 234 |
| 218 a.swap(b); | 235 template <typename T> |
| 219 } | 236 inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) { |
| 220 | 237 a.swap(b); |
| 221 template <typename T, typename U> inline bool operator==(const OwnPtr<T>& a, U*
b) | 238 } |
| 222 { | 239 |
| 223 return a.get() == b; | 240 template <typename T, typename U> |
| 224 } | 241 inline bool operator==(const OwnPtr<T>& a, U* b) { |
| 225 | 242 return a.get() == b; |
| 226 template <typename T, typename U> inline bool operator==(T* a, const OwnPtr<U>&
b) | 243 } |
| 227 { | 244 |
| 228 return a == b.get(); | 245 template <typename T, typename U> |
| 229 } | 246 inline bool operator==(T* a, const OwnPtr<U>& b) { |
| 230 | 247 return a == b.get(); |
| 231 template <typename T, typename U> inline bool operator!=(const OwnPtr<T>& a, U*
b) | 248 } |
| 232 { | 249 |
| 233 return a.get() != b; | 250 template <typename T, typename U> |
| 234 } | 251 inline bool operator!=(const OwnPtr<T>& a, U* b) { |
| 235 | 252 return a.get() != b; |
| 236 template <typename T, typename U> inline bool operator!=(T* a, const OwnPtr<U>&
b) | 253 } |
| 237 { | 254 |
| 238 return a != b.get(); | 255 template <typename T, typename U> |
| 239 } | 256 inline bool operator!=(T* a, const OwnPtr<U>& b) { |
| 240 | 257 return a != b.get(); |
| 241 template <typename T> inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>&
p) | 258 } |
| 242 { | 259 |
| 243 return p.get(); | 260 template <typename T> |
| 244 } | 261 inline typename OwnPtr<T>::PtrType getPtr(const OwnPtr<T>& p) { |
| 245 | 262 return p.get(); |
| 246 } // namespace WTF | 263 } |
| 264 |
| 265 } // namespace WTF |
| 247 | 266 |
| 248 using WTF::OwnPtr; | 267 using WTF::OwnPtr; |
| 249 | 268 |
| 250 #endif // WTF_OwnPtr_h | 269 #endif // WTF_OwnPtr_h |
| OLD | NEW |