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 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 // Constructor. Stores the given array. Note that the argument's type | 458 // Constructor. Stores the given array. Note that the argument's type |
459 // must exactly match T*. In particular: | 459 // must exactly match T*. In particular: |
460 // - it cannot be a pointer to a type derived from T, because it is | 460 // - it cannot be a pointer to a type derived from T, because it is |
461 // inherently unsafe in the general case to access an array through a | 461 // inherently unsafe in the general case to access an array through a |
462 // pointer whose dynamic type does not match its static type (eg., if | 462 // pointer whose dynamic type does not match its static type (eg., if |
463 // T and the derived types had different sizes access would be | 463 // T and the derived types had different sizes access would be |
464 // incorrectly calculated). Deletion is also always undefined | 464 // incorrectly calculated). Deletion is also always undefined |
465 // (C++98 [expr.delete]p3). If you're doing this, fix your code. | 465 // (C++98 [expr.delete]p3). If you're doing this, fix your code. |
466 // - it cannot be const-qualified differently from T per unique_ptr spec | 466 // - it cannot be const-qualified differently from T per unique_ptr spec |
467 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting | 467 // (http://cplusplus.github.com/LWG/lwg-active.html#2118). Users wanting |
468 // to work around this may use implicit_cast<const T*>(). | 468 // to work around this may use const_cast<const T*>(). |
469 // However, because of the first bullet in this comment, users MUST | |
470 // NOT use implicit_cast<Base*>() to upcast the static type of the array. | |
471 explicit scoped_ptr(element_type* array) : impl_(array) {} | 469 explicit scoped_ptr(element_type* array) : impl_(array) {} |
472 | 470 |
473 // Constructor. Allows construction from a nullptr. | 471 // Constructor. Allows construction from a nullptr. |
474 scoped_ptr(decltype(nullptr)) : impl_(nullptr) {} | 472 scoped_ptr(decltype(nullptr)) : impl_(nullptr) {} |
475 | 473 |
476 // Constructor. Allows construction from a scoped_ptr rvalue. | 474 // Constructor. Allows construction from a scoped_ptr rvalue. |
477 scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {} | 475 scoped_ptr(scoped_ptr&& other) : impl_(&other.impl_) {} |
478 | 476 |
479 // operator=. Allows assignment from a scoped_ptr rvalue. | 477 // operator=. Allows assignment from a scoped_ptr rvalue. |
480 scoped_ptr& operator=(scoped_ptr&& rhs) { | 478 scoped_ptr& operator=(scoped_ptr&& rhs) { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 583 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
586 return scoped_ptr<T>(ptr); | 584 return scoped_ptr<T>(ptr); |
587 } | 585 } |
588 | 586 |
589 template <typename T> | 587 template <typename T> |
590 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { | 588 std::ostream& operator<<(std::ostream& out, const scoped_ptr<T>& p) { |
591 return out << p.get(); | 589 return out << p.get(); |
592 } | 590 } |
593 | 591 |
594 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 592 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |