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 the | 5 // Scopers help you manage ownership of a pointer, helping you easily manage the |
6 // a pointer within a scope, and automatically destroying the pointer at the | 6 // a pointer within a scope, and automatically destroying the pointer at the |
7 // end of a scope. There are two main classes you will use, which correspond | 7 // end of a scope. There are two main classes you will use, which correspond |
8 // to the operators new/delete and new[]/delete[]. | 8 // to the operators new/delete and new[]/delete[]. |
9 // | 9 // |
10 // Example usage (scoped_ptr): | 10 // Example usage (scoped_ptr): |
(...skipping 588 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 enum { type_must_be_complete = sizeof(C) }; | 599 enum { type_must_be_complete = sizeof(C) }; |
600 delete[] array_; | 600 delete[] array_; |
601 } | 601 } |
602 | 602 |
603 // operator=. Move operator= for C++03 move emulation of this type. | 603 // operator=. Move operator= for C++03 move emulation of this type. |
604 scoped_array& operator=(RValue rhs) { | 604 scoped_array& operator=(RValue rhs) { |
605 reset(rhs.object->release()); | 605 reset(rhs.object->release()); |
606 return *this; | 606 return *this; |
607 } | 607 } |
608 | 608 |
609 // Reset. Deletes the current owned object, if any. | 609 // Reset. Deletes the current owned object, if any. |
610 // Then takes ownership of a new object, if given. | |
611 // this->reset(this->get()) works. | |
612 void reset(C* p = NULL) { | 610 void reset(C* p = NULL) { |
613 if (p != array_) { | 611 // This is a self-reset, which is no longer allowed: http://crbug.com/162971 |
614 enum { type_must_be_complete = sizeof(C) }; | 612 if (p != NULL && p == array_) |
615 delete[] array_; | 613 abort(); |
616 array_ = p; | 614 |
617 } | 615 C* old = array_; |
| 616 array_ = NULL; |
| 617 if (old != NULL) |
| 618 delete[] old; |
| 619 array_ = p; |
618 } | 620 } |
619 | 621 |
620 // Get one element of the current object. | 622 // Get one element of the current object. |
621 // Will assert() if there is no current object, or index i is negative. | 623 // Will assert() if there is no current object, or index i is negative. |
622 C& operator[](ptrdiff_t i) const { | 624 C& operator[](ptrdiff_t i) const { |
623 assert(i >= 0); | 625 assert(i >= 0); |
624 assert(array_ != NULL); | 626 assert(array_ != NULL); |
625 return array_[i]; | 627 return array_[i]; |
626 } | 628 } |
627 | 629 |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 | 816 |
815 // A function to convert T* into scoped_ptr<T> | 817 // A function to convert T* into scoped_ptr<T> |
816 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation | 818 // Doing e.g. make_scoped_ptr(new FooBarBaz<type>(arg)) is a shorter notation |
817 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) | 819 // for scoped_ptr<FooBarBaz<type> >(new FooBarBaz<type>(arg)) |
818 template <typename T> | 820 template <typename T> |
819 scoped_ptr<T> make_scoped_ptr(T* ptr) { | 821 scoped_ptr<T> make_scoped_ptr(T* ptr) { |
820 return scoped_ptr<T>(ptr); | 822 return scoped_ptr<T>(ptr); |
821 } | 823 } |
822 | 824 |
823 #endif // BASE_MEMORY_SCOPED_PTR_H_ | 825 #endif // BASE_MEMORY_SCOPED_PTR_H_ |
OLD | NEW |