summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authordcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-17 19:10:18 +0000
committerdcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-17 19:10:18 +0000
commitc37cc3ed715ab40a2ef2d328fe3fda31adfd8781 (patch)
tree781036e84a8840ec84fa18f5e3a1c339df1e2800 /base
parent1716058100e9511c206a8ec3dcbd4c34c52fdb6a (diff)
downloadchromium_src-c37cc3ed715ab40a2ef2d328fe3fda31adfd8781.zip
chromium_src-c37cc3ed715ab40a2ef2d328fe3fda31adfd8781.tar.gz
chromium_src-c37cc3ed715ab40a2ef2d328fe3fda31adfd8781.tar.bz2
Revert 194649 "Remove scoped_array from Chromium."
> Remove scoped_array from Chromium. > > C++11 provides unique_ptr<T[]>, and Chromium has implemented > scoped_ptr<T[]> to match its behavior during the transition period. As a > result, scoped_array<T> is now redundant and is being removed. > > BUG=171111 > > Review URL: https://codereview.chromium.org/14081006 TBR=dcheng@chromium.org Review URL: https://codereview.chromium.org/14225009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@194653 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/callback_internal.h9
-rw-r--r--base/memory/scoped_ptr.h148
-rw-r--r--base/memory/scoped_ptr_unittest.cc76
3 files changed, 227 insertions, 6 deletions
diff --git a/base/callback_internal.h b/base/callback_internal.h
index 5993824..d9aba39 100644
--- a/base/callback_internal.h
+++ b/base/callback_internal.h
@@ -136,6 +136,12 @@ struct CallbackParamTraits<scoped_ptr<T, D> > {
typedef scoped_ptr<T, D> StorageType;
};
+template <typename T>
+struct CallbackParamTraits<scoped_array<T> > {
+ typedef scoped_array<T> ForwardType;
+ typedef scoped_array<T> StorageType;
+};
+
template <typename T, typename R>
struct CallbackParamTraits<scoped_ptr_malloc<T, R> > {
typedef scoped_ptr_malloc<T, R> ForwardType;
@@ -170,6 +176,9 @@ T& CallbackForward(T& t) { return t; }
template <typename T, typename D>
scoped_ptr<T, D> CallbackForward(scoped_ptr<T, D>& p) { return p.Pass(); }
+template <typename T>
+scoped_array<T> CallbackForward(scoped_array<T>& p) { return p.Pass(); }
+
template <typename T, typename R>
scoped_ptr_malloc<T, R> CallbackForward(scoped_ptr_malloc<T, R>& p) {
return p.Pass();
diff --git a/base/memory/scoped_ptr.h b/base/memory/scoped_ptr.h
index 5edde8f..41fafc9 100644
--- a/base/memory/scoped_ptr.h
+++ b/base/memory/scoped_ptr.h
@@ -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 @@ bool operator!=(T* p1, const scoped_ptr<T, D>& p2) {
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
diff --git a/base/memory/scoped_ptr_unittest.cc b/base/memory/scoped_ptr_unittest.cc
index 22da53d..59b3e1c 100644
--- a/base/memory/scoped_ptr_unittest.cc
+++ b/base/memory/scoped_ptr_unittest.cc
@@ -378,6 +378,82 @@ TEST(ScopedPtrTest, ScopedPtrWithArray) {
EXPECT_EQ(0, constructed);
}
+TEST(ScopedPtrTest, ScopedArray) {
+ static const int kNumLoggers = 12;
+
+ int constructed = 0;
+
+ {
+ scoped_array<ConDecLogger> scoper(new ConDecLogger[kNumLoggers]);
+ EXPECT_TRUE(scoper.get());
+ EXPECT_EQ(&scoper[0], scoper.get());
+ for (int i = 0; i < kNumLoggers; ++i) {
+ scoper[i].SetPtr(&constructed);
+ }
+ EXPECT_EQ(12, constructed);
+
+ EXPECT_EQ(10, scoper.get()->SomeMeth(10));
+ EXPECT_EQ(10, scoper[2].SomeMeth(10));
+ }
+ EXPECT_EQ(0, constructed);
+
+ // Test reset() and release()
+ {
+ scoped_array<ConDecLogger> scoper;
+ EXPECT_FALSE(scoper.get());
+ EXPECT_FALSE(scoper.release());
+ EXPECT_FALSE(scoper.get());
+ scoper.reset();
+ EXPECT_FALSE(scoper.get());
+
+ scoper.reset(new ConDecLogger[kNumLoggers]);
+ for (int i = 0; i < kNumLoggers; ++i) {
+ scoper[i].SetPtr(&constructed);
+ }
+ EXPECT_EQ(12, constructed);
+ scoper.reset();
+ EXPECT_EQ(0, constructed);
+
+ scoper.reset(new ConDecLogger[kNumLoggers]);
+ for (int i = 0; i < kNumLoggers; ++i) {
+ scoper[i].SetPtr(&constructed);
+ }
+ EXPECT_EQ(12, constructed);
+ ConDecLogger* ptr = scoper.release();
+ EXPECT_EQ(12, constructed);
+ delete[] ptr;
+ EXPECT_EQ(0, constructed);
+ }
+ EXPECT_EQ(0, constructed);
+
+ // Test swap(), ==, !=, and type-safe Boolean.
+ {
+ scoped_array<ConDecLogger> scoper1;
+ scoped_array<ConDecLogger> scoper2;
+ EXPECT_TRUE(scoper1 == scoper2.get());
+ EXPECT_FALSE(scoper1 != scoper2.get());
+
+ ConDecLogger* loggers = new ConDecLogger[kNumLoggers];
+ for (int i = 0; i < kNumLoggers; ++i) {
+ loggers[i].SetPtr(&constructed);
+ }
+ scoper1.reset(loggers);
+ EXPECT_TRUE(scoper1);
+ EXPECT_EQ(loggers, scoper1.get());
+ EXPECT_FALSE(scoper2);
+ EXPECT_FALSE(scoper2.get());
+ EXPECT_FALSE(scoper1 == scoper2.get());
+ EXPECT_TRUE(scoper1 != scoper2.get());
+
+ scoper2.swap(scoper1);
+ EXPECT_EQ(loggers, scoper2.get());
+ EXPECT_FALSE(scoper1.get());
+ EXPECT_FALSE(scoper1 == scoper2.get());
+ EXPECT_TRUE(scoper1 != scoper2.get());
+ }
+ EXPECT_EQ(0, constructed);
+}
+
TEST(ScopedPtrTest, PassBehavior) {
int constructed = 0;
{