| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Scopers help you manage ownership of a pointer, helping you easily manage a | 5 // Scopers help you manage ownership of a pointer, helping you easily manage a |
| 6 // pointer within a scope, and automatically destroying the pointer at the end | 6 // pointer within a scope, and automatically destroying the pointer at the end |
| 7 // of a scope. There are two main classes you will use, which correspond to the | 7 // of a scope. There are two main classes you will use, which correspond to the |
| 8 // operators new/delete and new[]/delete[]. | 8 // operators new/delete and new[]/delete[]. |
| 9 // | 9 // |
| 10 // Example usage (scoped_ptr<T>): | 10 // Example usage (scoped_ptr<T>): |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 namespace internal { | 177 namespace internal { |
| 178 | 178 |
| 179 template <typename T> struct IsNotRefCounted { | 179 template <typename T> struct IsNotRefCounted { |
| 180 enum { | 180 enum { |
| 181 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && | 181 value = !base::is_convertible<T*, base::subtle::RefCountedBase*>::value && |
| 182 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: | 182 !base::is_convertible<T*, base::subtle::RefCountedThreadSafeBase*>:: |
| 183 value | 183 value |
| 184 }; | 184 }; |
| 185 }; | 185 }; |
| 186 | 186 |
| 187 template <typename T> |
| 188 struct ShouldAbortOnSelfReset { |
| 189 template <typename U> |
| 190 static NoType Test(const typename U::AllowSelfReset*); |
| 191 |
| 192 template <typename U> |
| 193 static YesType Test(...); |
| 194 |
| 195 static const bool value = sizeof(Test<T>(0)) == sizeof(YesType); |
| 196 }; |
| 197 |
| 187 // Minimal implementation of the core logic of scoped_ptr, suitable for | 198 // Minimal implementation of the core logic of scoped_ptr, suitable for |
| 188 // reuse in both scoped_ptr and its specializations. | 199 // reuse in both scoped_ptr and its specializations. |
| 189 template <class T, class D> | 200 template <class T, class D> |
| 190 class scoped_ptr_impl { | 201 class scoped_ptr_impl { |
| 191 public: | 202 public: |
| 192 explicit scoped_ptr_impl(T* p) : data_(p) {} | 203 explicit scoped_ptr_impl(T* p) : data_(p) {} |
| 193 | 204 |
| 194 // Initializer for deleters that have data parameters. | 205 // Initializer for deleters that have data parameters. |
| 195 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {} | 206 scoped_ptr_impl(T* p, const D& d) : data_(p, d) {} |
| 196 | 207 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 215 | 226 |
| 216 ~scoped_ptr_impl() { | 227 ~scoped_ptr_impl() { |
| 217 if (data_.ptr != nullptr) { | 228 if (data_.ptr != nullptr) { |
| 218 // Not using get_deleter() saves one function call in non-optimized | 229 // Not using get_deleter() saves one function call in non-optimized |
| 219 // builds. | 230 // builds. |
| 220 static_cast<D&>(data_)(data_.ptr); | 231 static_cast<D&>(data_)(data_.ptr); |
| 221 } | 232 } |
| 222 } | 233 } |
| 223 | 234 |
| 224 void reset(T* p) { | 235 void reset(T* p) { |
| 225 // This is a self-reset, which is no longer allowed: http://crbug.com/162971 | 236 // This is a self-reset, which is no longer allowed for default deleters: |
| 226 if (p != nullptr && p == data_.ptr) | 237 // https://crbug.com/162971 |
| 227 abort(); | 238 assert(!ShouldAbortOnSelfReset<D>::value || p == nullptr || p != data_.ptr); |
| 228 | 239 |
| 229 // Note that running data_.ptr = p can lead to undefined behavior if | 240 // Note that running data_.ptr = p can lead to undefined behavior if |
| 230 // get_deleter()(get()) deletes this. In order to prevent this, reset() | 241 // get_deleter()(get()) deletes this. In order to prevent this, reset() |
| 231 // should update the stored pointer before deleting its old value. | 242 // should update the stored pointer before deleting its old value. |
| 232 // | 243 // |
| 233 // However, changing reset() to use that behavior may cause current code to | 244 // However, changing reset() to use that behavior may cause current code to |
| 234 // break in unexpected ways. If the destruction of the owned object | 245 // break in unexpected ways. If the destruction of the owned object |
| 235 // dereferences the scoped_ptr when it is destroyed by a call to reset(), | 246 // dereferences the scoped_ptr when it is destroyed by a call to reset(), |
| 236 // then it will incorrectly dispatch calls to |p| rather than the original | 247 // then it will incorrectly dispatch calls to |p| rather than the original |
| 237 // value of |data_.ptr|. | 248 // value of |data_.ptr|. |
| (...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 | 600 |
| 590 // A function to convert T* into scoped_ptr<T> | 601 // A function to convert T* into scoped_ptr<T> |
| 591 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 602 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
| 592 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 603 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
| 593 template <typename T> | 604 template <typename T> |
| 594 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 605 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
| 595 return scoped_ptr<T>(ptr); | 606 return scoped_ptr<T>(ptr); |
| 596 } | 607 } |
| 597 | 608 |
| 598 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 609 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
| OLD | NEW |