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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 template <typename U, typename V> | 138 template <typename U, typename V> |
139 scoped_ptr_impl(scoped_ptr_impl<U, V>* other) | 139 scoped_ptr_impl(scoped_ptr_impl<U, V>* other) |
140 : data_(other->release(), other->get_deleter()) { | 140 : data_(other->release(), other->get_deleter()) { |
141 // We do not support move-only deleters. We could modify our move | 141 // We do not support move-only deleters. We could modify our move |
142 // emulation to have base::subtle::move() and base::subtle::forward() | 142 // emulation to have base::subtle::move() and base::subtle::forward() |
143 // functions that are imperfect emulations of their C++11 equivalents, | 143 // functions that are imperfect emulations of their C++11 equivalents, |
144 // but until there's a requirement, just assume deleters are copyable. | 144 // but until there's a requirement, just assume deleters are copyable. |
145 } | 145 } |
146 | 146 |
147 template <typename U, typename V> | 147 template <typename U, typename V> |
| 148 void TakeState(std::unique_ptr<U, V>* other) { |
| 149 reset(other->release()); |
| 150 get_deleter() = other->get_deleter(); |
| 151 } |
| 152 |
| 153 template <typename U, typename V> |
148 void TakeState(scoped_ptr_impl<U, V>* other) { | 154 void TakeState(scoped_ptr_impl<U, V>* other) { |
149 // See comment in templated constructor above regarding lack of support | 155 // See comment in templated constructor above regarding lack of support |
150 // for move-only deleters. | 156 // for move-only deleters. |
151 reset(other->release()); | 157 reset(other->release()); |
152 get_deleter() = other->get_deleter(); | 158 get_deleter() = other->get_deleter(); |
153 } | 159 } |
154 | 160 |
155 ~scoped_ptr_impl() { | 161 ~scoped_ptr_impl() { |
156 // Match libc++, which calls reset() in its destructor. | 162 // Match libc++, which calls reset() in its destructor. |
157 // Use nullptr as the new value for three reasons: | 163 // Use nullptr as the new value for three reasons: |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 } | 393 } |
388 | 394 |
389 // Release a pointer. | 395 // Release a pointer. |
390 // The return value is the current pointer held by this object. If this object | 396 // The return value is the current pointer held by this object. If this object |
391 // holds a nullptr, the return value is nullptr. After this operation, this | 397 // holds a nullptr, the return value is nullptr. After this operation, this |
392 // object will hold a nullptr, and will not own the object any more. | 398 // object will hold a nullptr, and will not own the object any more. |
393 element_type* release() WARN_UNUSED_RESULT { | 399 element_type* release() WARN_UNUSED_RESULT { |
394 return impl_.release(); | 400 return impl_.release(); |
395 } | 401 } |
396 | 402 |
| 403 // Conversion shims to help with the std::unique_ptr transition. |
| 404 template <typename U, typename V> |
| 405 scoped_ptr(std::unique_ptr<U, V>&& p) |
| 406 : impl_(p.release(), p.get_deleter()) {} |
| 407 |
| 408 template <typename U, typename V> |
| 409 scoped_ptr& operator=(std::unique_ptr<U, V>&& p) { |
| 410 impl_.TakeState(&p); |
| 411 return *this; |
| 412 } |
| 413 |
| 414 template <typename U, typename V> |
| 415 operator std::unique_ptr<U, V>() && { |
| 416 return std::unique_ptr<U, V>(release(), get_deleter()); |
| 417 } |
| 418 |
397 private: | 419 private: |
398 // Needed to reach into |impl_| in the constructor. | 420 // Needed to reach into |impl_| in the constructor. |
399 template <typename U, typename V> friend class scoped_ptr; | 421 template <typename U, typename V> friend class scoped_ptr; |
400 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | 422 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; |
401 | 423 |
402 // Forbidden for API compatibility with std::unique_ptr. | 424 // Forbidden for API compatibility with std::unique_ptr. |
403 explicit scoped_ptr(int disallow_construction_from_null); | 425 explicit scoped_ptr(int disallow_construction_from_null); |
404 }; | 426 }; |
405 | 427 |
406 template <class T, class D> | 428 template <class T, class D> |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
479 } | 501 } |
480 | 502 |
481 // Release a pointer. | 503 // Release a pointer. |
482 // The return value is the current pointer held by this object. If this object | 504 // The return value is the current pointer held by this object. If this object |
483 // holds a nullptr, the return value is nullptr. After this operation, this | 505 // holds a nullptr, the return value is nullptr. After this operation, this |
484 // object will hold a nullptr, and will not own the object any more. | 506 // object will hold a nullptr, and will not own the object any more. |
485 element_type* release() WARN_UNUSED_RESULT { | 507 element_type* release() WARN_UNUSED_RESULT { |
486 return impl_.release(); | 508 return impl_.release(); |
487 } | 509 } |
488 | 510 |
| 511 // Conversion shims to help with the std::unique_ptr transition. |
| 512 scoped_ptr(std::unique_ptr<T[], D>&& p) |
| 513 : impl_(p.release(), p.get_deleter()) {} |
| 514 |
| 515 scoped_ptr& operator=(std::unique_ptr<T[], D>&& p) { |
| 516 impl_.TakeState(&p); |
| 517 return *this; |
| 518 } |
| 519 |
| 520 operator std::unique_ptr<T[], D>() && { |
| 521 return std::unique_ptr<T[], D>(release(), get_deleter()); |
| 522 } |
| 523 |
489 private: | 524 private: |
490 // Force element_type to be a complete type. | 525 // Force element_type to be a complete type. |
491 enum { type_must_be_complete = sizeof(element_type) }; | 526 enum { type_must_be_complete = sizeof(element_type) }; |
492 | 527 |
493 // Actually hold the data. | 528 // Actually hold the data. |
494 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; | 529 base::internal::scoped_ptr_impl<element_type, deleter_type> impl_; |
495 | 530 |
496 // Disable initialization from any type other than element_type*, by | 531 // Disable initialization from any type other than element_type*, by |
497 // providing a constructor that matches such an initialization, but is | 532 // providing a constructor that matches such an initialization, but is |
498 // private and has no definition. This is disabled because it is not safe to | 533 // private and has no definition. This is disabled because it is not safe to |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 633 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
599 return scoped_ptr<T>(ptr); | 634 return scoped_ptr<T>(ptr); |
600 } | 635 } |
601 | 636 |
602 template <typename T> | 637 template <typename T> |
603 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { | 638 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { |
604 return out << p.get(); | 639 return out << p.get(); |
605 } | 640 } |
606 | 641 |
607 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 642 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |