| Index: trunk/src/base/memory/scoped_ptr.h
|
| ===================================================================
|
| --- trunk/src/base/memory/scoped_ptr.h (revision 194652)
|
| +++ trunk/src/base/memory/scoped_ptr.h (working copy)
|
| @@ -7,7 +7,7 @@
|
| // end of a scope. There are two main classes you will use, which correspond
|
| // to the operators new/delete and new[]/delete[].
|
| //
|
| -// Example usage (scoped_ptr<T>):
|
| +// Example usage (scoped_ptr):
|
| // {
|
| // scoped_ptr<Foo> foo(new Foo("wee"));
|
| // } // foo goes out of scope, releasing the pointer with it.
|
| @@ -26,9 +26,9 @@
|
| // // manages a pointer.
|
| // } // foo wasn't managing a pointer, so nothing was destroyed.
|
| //
|
| -// Example usage (scoped_ptr<T[]>):
|
| +// Example usage (scoped_array):
|
| // {
|
| -// scoped_ptr<Foo[]> foo(new Foo[100]);
|
| +// scoped_array<Foo> foo(new Foo[100]);
|
| // foo.get()->Method(); // Foo::Method on the 0th element.
|
| // foo[10].Method(); // Foo::Method on the 10th element.
|
| // }
|
| @@ -81,14 +81,15 @@
|
| // return result.PassAs<Foo>();
|
| // }
|
| //
|
| -// Note that PassAs<>() is implemented only for scoped_ptr<T>, but not for
|
| -// scoped_ptr<T[]>. This is because casting array pointers may not be safe.
|
| +// Note that PassAs<>() is implemented only for scoped_ptr, but not for
|
| +// scoped_array. This is because casting array pointers may not be safe.
|
|
|
| #ifndef BASE_MEMORY_SCOPED_PTR_H_
|
| #define BASE_MEMORY_SCOPED_PTR_H_
|
|
|
| // This is an implementation designed to match the anticipated future TR2
|
| -// implementation of the scoped_ptr class and scoped_ptr_malloc (deprecated).
|
| +// implementation of the scoped_ptr class, and its closely-related brethren,
|
| +// scoped_array, scoped_ptr_malloc.
|
|
|
| #include <assert.h>
|
| #include <stddef.h>
|
| @@ -562,6 +563,141 @@
|
| return p1 != p2.get();
|
| }
|
|
|
| +// DEPRECATED: Use scoped_ptr<C[]> instead.
|
| +//
|
| +// scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
|
| +// with new [] and the destructor deletes objects with delete [].
|
| +//
|
| +// As with scoped_ptr<C>, a scoped_array<C> either points to an object
|
| +// or is NULL. A scoped_array<C> owns the object that it points to.
|
| +// scoped_array<T> is thread-compatible, and once you index into it,
|
| +// the returned objects have only the thread safety guarantees of T.
|
| +//
|
| +// Size: sizeof(scoped_array<C>) == sizeof(C*)
|
| +template <class C>
|
| +class scoped_array {
|
| + MOVE_ONLY_TYPE_FOR_CPP_03(scoped_array, RValue)
|
| +
|
| + public:
|
| +
|
| + // The element type
|
| + typedef C element_type;
|
| +
|
| + // Constructor. Defaults to initializing with NULL.
|
| + // There is no way to create an uninitialized scoped_array.
|
| + // The input parameter must be allocated with new [].
|
| + explicit scoped_array(C* p = NULL) : array_(p) { }
|
| +
|
| + // Constructor. Move constructor for C++03 move emulation of this type.
|
| + scoped_array(RValue rvalue)
|
| + : array_(rvalue.object->release()) {
|
| + }
|
| +
|
| + // Destructor. If there is a C object, delete it.
|
| + // We don't need to test ptr_ == NULL because C++ does that for us.
|
| + ~scoped_array() {
|
| + enum { type_must_be_complete = sizeof(C) };
|
| + delete[] array_;
|
| + }
|
| +
|
| + // operator=. Move operator= for C++03 move emulation of this type.
|
| + scoped_array& operator=(RValue rhs) {
|
| + reset(rhs.object->release());
|
| + return *this;
|
| + }
|
| +
|
| + // Reset. Deletes the current owned object, if any.
|
| + void reset(C* p = NULL) {
|
| + // This is a self-reset, which is no longer allowed: http://crbug.com/162971
|
| + if (p != NULL && p == array_)
|
| + abort();
|
| +
|
| + C* old = array_;
|
| + array_ = NULL;
|
| + if (old != NULL)
|
| + delete[] old;
|
| + array_ = p;
|
| + }
|
| +
|
| + // Get one element of the current object.
|
| + // Will assert() if there is no current object, or index i is negative.
|
| + C& operator[](ptrdiff_t i) const {
|
| + assert(i >= 0);
|
| + assert(array_ != NULL);
|
| + return array_[i];
|
| + }
|
| +
|
| + // Get a pointer to the zeroth element of the current object.
|
| + // If there is no current object, return NULL.
|
| + C* get() const {
|
| + return array_;
|
| + }
|
| +
|
| + // Allow scoped_array<C> to be used in boolean expressions, but not
|
| + // implicitly convertible to a real bool (which is dangerous).
|
| + typedef C* scoped_array::*Testable;
|
| + operator Testable() const { return array_ ? &scoped_array::array_ : NULL; }
|
| +
|
| + // Comparison operators.
|
| + // These return whether two scoped_array refer to the same object, not just to
|
| + // two different but equal objects.
|
| + bool operator==(C* p) const { return array_ == p; }
|
| + bool operator!=(C* p) const { return array_ != p; }
|
| +
|
| + // Swap two scoped arrays.
|
| + void swap(scoped_array& p2) {
|
| + C* tmp = array_;
|
| + array_ = p2.array_;
|
| + p2.array_ = tmp;
|
| + }
|
| +
|
| + // Release an array.
|
| + // The return value is the current pointer held by this object.
|
| + // If this object holds a NULL pointer, the return value is NULL.
|
| + // After this operation, this object will hold a NULL pointer,
|
| + // and will not own the object any more.
|
| + C* release() WARN_UNUSED_RESULT {
|
| + C* retVal = array_;
|
| + array_ = NULL;
|
| + return retVal;
|
| + }
|
| +
|
| + private:
|
| + C* array_;
|
| +
|
| + // Disable initialization from any type other than C*, by providing a
|
| + // constructor that matches such an initialization, but is private and has no
|
| + // definition. This is disabled because it is not safe to call delete[] on an
|
| + // array whose static type does not match its dynamic type.
|
| + template <typename C2> explicit scoped_array(C2* array);
|
| + explicit scoped_array(int disallow_construction_from_null);
|
| +
|
| + // Disable reset() from any type other than C*, for the same reasons as the
|
| + // constructor above.
|
| + template <typename C2> void reset(C2* array);
|
| + void reset(int disallow_reset_from_null);
|
| +
|
| + // Forbid comparison of different scoped_array types.
|
| + template <class C2> bool operator==(scoped_array<C2> const& p2) const;
|
| + template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
|
| +};
|
| +
|
| +// Free functions
|
| +template <class C>
|
| +void swap(scoped_array<C>& p1, scoped_array<C>& p2) {
|
| + p1.swap(p2);
|
| +}
|
| +
|
| +template <class C>
|
| +bool operator==(C* p1, const scoped_array<C>& p2) {
|
| + return p1 == p2.get();
|
| +}
|
| +
|
| +template <class C>
|
| +bool operator!=(C* p1, const scoped_array<C>& p2) {
|
| + return p1 != p2.get();
|
| +}
|
| +
|
| // DEPRECATED: Use scoped_ptr<C, base::FreeDeleter> instead.
|
| //
|
| // scoped_ptr_malloc<> is similar to scoped_ptr<>, but it accepts a
|
|
|