| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef PlatformSTL_h | 5 #ifndef PlatformSTL_h |
| 6 #define PlatformSTL_h | 6 #define PlatformSTL_h |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #define PLATFORM_EXPORT | 10 #define PLATFORM_EXPORT |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 static_assert(sizeof(T) < 0, "do not use array with size as type"); | 115 static_assert(sizeof(T) < 0, "do not use array with size as type"); |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 template <typename T> class unique_ptr { | 118 template <typename T> class unique_ptr { |
| 119 public: | 119 public: |
| 120 typedef typename remove_extent<T>::type ValueType; | 120 typedef typename remove_extent<T>::type ValueType; |
| 121 typedef ValueType* PtrType; | 121 typedef ValueType* PtrType; |
| 122 | 122 |
| 123 unique_ptr() : m_ptr(nullptr) {} | 123 unique_ptr() : m_ptr(nullptr) {} |
| 124 unique_ptr(std::nullptr_t) : m_ptr(nullptr) {} | 124 unique_ptr(std::nullptr_t) : m_ptr(nullptr) {} |
| 125 unique_ptr(const unique_ptr&); |
| 125 unique_ptr(unique_ptr&&); | 126 unique_ptr(unique_ptr&&); |
| 126 template <typename U, typename = typename enable_if<is_convertible<U*, T*>::
value>::type> unique_ptr(unique_ptr<U>&&); | 127 template <typename U, typename = typename enable_if<is_convertible<U*, T*>::
value>::type> unique_ptr(unique_ptr<U>&&); |
| 127 | 128 |
| 128 ~unique_ptr() | 129 ~unique_ptr() |
| 129 { | 130 { |
| 130 OwnedPtrDeleter<T>::deletePtr(m_ptr); | 131 OwnedPtrDeleter<T>::deletePtr(m_ptr); |
| 131 m_ptr = nullptr; | 132 m_ptr = nullptr; |
| 132 } | 133 } |
| 133 | 134 |
| 134 PtrType get() const { return m_ptr; } | 135 PtrType get() const { return m_ptr; } |
| 135 | 136 |
| 136 void reset(PtrType = nullptr); | 137 void reset(PtrType = nullptr); |
| 137 PtrType release(); | 138 PtrType release() |
| 139 { |
| 140 return this->internalRelease(); |
| 141 } |
| 138 | 142 |
| 139 ValueType& operator*() const { DCHECK(m_ptr); return *m_ptr; } | 143 ValueType& operator*() const { DCHECK(m_ptr); return *m_ptr; } |
| 140 PtrType operator->() const { DCHECK(m_ptr); return m_ptr; } | 144 PtrType operator->() const { DCHECK(m_ptr); return m_ptr; } |
| 141 | 145 |
| 142 ValueType& operator[](std::ptrdiff_t i) const; | 146 ValueType& operator[](std::ptrdiff_t i) const; |
| 143 | 147 |
| 144 bool operator!() const { return !m_ptr; } | 148 bool operator!() const { return !m_ptr; } |
| 145 explicit operator bool() const { return m_ptr; } | 149 explicit operator bool() const { return m_ptr; } |
| 146 | 150 |
| 147 unique_ptr& operator=(std::nullptr_t) { reset(); return *this; } | 151 unique_ptr& operator=(std::nullptr_t) { reset(); return *this; } |
| 148 | 152 |
| 149 | 153 unique_ptr& operator=(const unique_ptr&); |
| 150 unique_ptr& operator=(unique_ptr&&); | 154 unique_ptr& operator=(unique_ptr&&); |
| 151 template <typename U> unique_ptr& operator=(unique_ptr<U>&&); | 155 template <typename U> unique_ptr& operator=(unique_ptr<U>&&); |
| 152 | 156 |
| 153 void swap(unique_ptr& o) { std::swap(m_ptr, o.m_ptr); } | 157 void swap(unique_ptr& o) { std::swap(m_ptr, o.m_ptr); } |
| 154 | 158 |
| 155 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } | 159 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); } |
| 156 | 160 |
| 157 explicit unique_ptr(PtrType ptr) : m_ptr(ptr) {} | 161 explicit unique_ptr(PtrType ptr) : m_ptr(ptr) {} |
| 158 | 162 |
| 159 private: | 163 private: |
| 164 PtrType internalRelease() const |
| 165 { |
| 166 PtrType ptr = m_ptr; |
| 167 m_ptr = nullptr; |
| 168 return ptr; |
| 169 } |
| 160 | 170 |
| 161 // We should never have two unique_ptrs for the same underlying object | 171 // We should never have two unique_ptrs for the same underlying object |
| 162 // (otherwise we'll get double-destruction), so these equality operators | 172 // (otherwise we'll get double-destruction), so these equality operators |
| 163 // should never be needed. | 173 // should never be needed. |
| 164 template <typename U> bool operator==(const unique_ptr<U>&) const | 174 template <typename U> bool operator==(const unique_ptr<U>&) const |
| 165 { | 175 { |
| 166 static_assert(!sizeof(U*), "unique_ptrs should never be equal"); | 176 static_assert(!sizeof(U*), "unique_ptrs should never be equal"); |
| 167 return false; | 177 return false; |
| 168 } | 178 } |
| 169 template <typename U> bool operator!=(const unique_ptr<U>&) const | 179 template <typename U> bool operator!=(const unique_ptr<U>&) const |
| 170 { | 180 { |
| 171 static_assert(!sizeof(U*), "unique_ptrs should never be equal"); | 181 static_assert(!sizeof(U*), "unique_ptrs should never be equal"); |
| 172 return false; | 182 return false; |
| 173 } | 183 } |
| 174 | 184 |
| 175 PtrType m_ptr; | 185 mutable PtrType m_ptr; |
| 176 }; | 186 }; |
| 177 | 187 |
| 178 | 188 |
| 179 template <typename T> inline void unique_ptr<T>::reset(PtrType ptr) | 189 template <typename T> inline void unique_ptr<T>::reset(PtrType ptr) |
| 180 { | 190 { |
| 181 PtrType p = m_ptr; | 191 PtrType p = m_ptr; |
| 182 m_ptr = ptr; | 192 m_ptr = ptr; |
| 193 DCHECK(!p || m_ptr != p); |
| 183 OwnedPtrDeleter<T>::deletePtr(p); | 194 OwnedPtrDeleter<T>::deletePtr(p); |
| 184 } | 195 } |
| 185 | 196 |
| 186 template <typename T> inline typename unique_ptr<T>::PtrType unique_ptr<T>::rele
ase() | |
| 187 { | |
| 188 PtrType ptr = m_ptr; | |
| 189 m_ptr = nullptr; | |
| 190 return ptr; | |
| 191 } | |
| 192 | |
| 193 template <typename T> inline typename unique_ptr<T>::ValueType& unique_ptr<T>::o
perator[](std::ptrdiff_t i) const | 197 template <typename T> inline typename unique_ptr<T>::ValueType& unique_ptr<T>::o
perator[](std::ptrdiff_t i) const |
| 194 { | 198 { |
| 195 static_assert(is_array<T>::value, "elements access is possible for arrays on
ly"); | 199 static_assert(is_array<T>::value, "elements access is possible for arrays on
ly"); |
| 196 DCHECK(m_ptr); | 200 DCHECK(m_ptr); |
| 197 DCHECK_GE(i, 0); | 201 DCHECK_GE(i, 0); |
| 198 return m_ptr[i]; | 202 return m_ptr[i]; |
| 199 } | 203 } |
| 200 | 204 |
| 201 template <typename T> inline unique_ptr<T>::unique_ptr(unique_ptr<T>&& o) | 205 template <typename T> inline unique_ptr<T>::unique_ptr(const unique_ptr<T>& o) |
| 202 : m_ptr(o.release()) | 206 : m_ptr(o.internalRelease()) |
| 203 { | 207 { |
| 204 } | 208 } |
| 205 | 209 |
| 210 template <typename T> inline unique_ptr<T>::unique_ptr(unique_ptr<T>&& o) |
| 211 : m_ptr(o.internalRelease()) |
| 212 { |
| 213 } |
| 214 |
| 206 template <typename T> | 215 template <typename T> |
| 207 template <typename U, typename> inline unique_ptr<T>::unique_ptr(unique_ptr<U>&&
o) | 216 template <typename U, typename> inline unique_ptr<T>::unique_ptr(unique_ptr<U>&&
o) |
| 208 : m_ptr(o.release()) | 217 : m_ptr(o.release()) |
| 209 { | 218 { |
| 210 static_assert(!is_array<T>::value, "pointers to array must never be converte
d"); | 219 static_assert(!is_array<T>::value, "pointers to array must never be converte
d"); |
| 211 } | 220 } |
| 212 | 221 |
| 222 template <typename T> inline unique_ptr<T>& unique_ptr<T>::operator=(const uniqu
e_ptr<T>& o) |
| 223 { |
| 224 reset(o.internalRelease()); |
| 225 return *this; |
| 226 } |
| 227 |
| 213 template <typename T> inline unique_ptr<T>& unique_ptr<T>::operator=(unique_ptr<
T>&& o) | 228 template <typename T> inline unique_ptr<T>& unique_ptr<T>::operator=(unique_ptr<
T>&& o) |
| 214 { | 229 { |
| 215 PtrType ptr = m_ptr; | 230 reset(o.internalRelease()); |
| 216 m_ptr = o.release(); | |
| 217 DCHECK(!ptr || m_ptr != ptr); | |
| 218 OwnedPtrDeleter<T>::deletePtr(ptr); | |
| 219 | |
| 220 return *this; | 231 return *this; |
| 221 } | 232 } |
| 222 | 233 |
| 223 template <typename T> | 234 template <typename T> |
| 224 template <typename U> inline unique_ptr<T>& unique_ptr<T>::operator=(unique_ptr<
U>&& o) | 235 template <typename U> inline unique_ptr<T>& unique_ptr<T>::operator=(unique_ptr<
U>&& o) |
| 225 { | 236 { |
| 226 static_assert(!is_array<T>::value, "pointers to array must never be converte
d"); | 237 static_assert(!is_array<T>::value, "pointers to array must never be converte
d"); |
| 227 PtrType ptr = m_ptr; | 238 PtrType ptr = m_ptr; |
| 228 m_ptr = o.release(); | 239 m_ptr = o.release(); |
| 229 DCHECK(!ptr || m_ptr != ptr); | 240 DCHECK(!ptr || m_ptr != ptr); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 | 283 |
| 273 #endif // defined(__APPLE__) && !defined(_LIBCPP_VERSION) | 284 #endif // defined(__APPLE__) && !defined(_LIBCPP_VERSION) |
| 274 | 285 |
| 275 template <typename T> | 286 template <typename T> |
| 276 std::unique_ptr<T> wrapUnique(T* ptr) | 287 std::unique_ptr<T> wrapUnique(T* ptr) |
| 277 { | 288 { |
| 278 return std::unique_ptr<T>(ptr); | 289 return std::unique_ptr<T>(ptr); |
| 279 } | 290 } |
| 280 | 291 |
| 281 #endif // PlatformSTL_h | 292 #endif // PlatformSTL_h |
| OLD | NEW |