diff options
author | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-19 00:38:58 +0000 |
---|---|---|
committer | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-19 00:38:58 +0000 |
commit | 3ebea920942e11fb0c81c83718699ab4e6a6a370 (patch) | |
tree | bb65049c51fd35a354ab4505802822cb884e3ff5 /base | |
parent | e19d265c10c73b01717054f2bc7da1797d937bfd (diff) | |
download | chromium_src-3ebea920942e11fb0c81c83718699ab4e6a6a370.zip chromium_src-3ebea920942e11fb0c81c83718699ab4e6a6a370.tar.gz chromium_src-3ebea920942e11fb0c81c83718699ab4e6a6a370.tar.bz2 |
base: Make ScopedVector::clear() destroy elements.
This makes ScopedVector's clear() method destroy the
elements before clearing the internal vector, matching the
behavior of the erase() method. I'm moving clear()'s
previous element-preserving behavior into a new weak_clear()
method, matching weak_erase().
I'm also removing ScopedVector::reset(), as it duplicated
clear()'s new behavior and isn't a part of std::vector.
BUG=137909
TEST=added
Review URL: https://chromiumcodereview.appspot.com/10797017
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@147360 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/memory/scoped_vector.h | 8 | ||||
-rw-r--r-- | base/memory/scoped_vector_unittest.cc | 19 |
2 files changed, 21 insertions, 6 deletions
diff --git a/base/memory/scoped_vector.h b/base/memory/scoped_vector.h index f8afcdd..5470097 100644 --- a/base/memory/scoped_vector.h +++ b/base/memory/scoped_vector.h @@ -33,7 +33,7 @@ class ScopedVector { const_reverse_iterator; ScopedVector() {} - ~ScopedVector() { reset(); } + ~ScopedVector() { clear(); } ScopedVector(RValue& other) { swap(other); } ScopedVector& operator=(RValue& rhs) { @@ -73,7 +73,6 @@ class ScopedVector { v.clear(); } - void reset() { STLDeleteElements(&v); } void reserve(size_t capacity) { v.reserve(capacity); } void resize(size_t new_size) { v.resize(new_size); } @@ -82,7 +81,10 @@ class ScopedVector { v.assign(begin, end); } - void clear() { v.clear(); } + void clear() { STLDeleteElements(&v); } + + // Like |clear()|, but doesn't delete any elements. + void weak_clear() { v.clear(); } // Lets the ScopedVector take ownership of |x|. iterator insert(iterator position, T* x) { diff --git a/base/memory/scoped_vector_unittest.cc b/base/memory/scoped_vector_unittest.cc index f6c7167..03774c5 100644 --- a/base/memory/scoped_vector_unittest.cc +++ b/base/memory/scoped_vector_unittest.cc @@ -107,14 +107,27 @@ TEST(ScopedVectorTest, LifeCycleWatcher) { EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); } -TEST(ScopedVectorTest, Reset) { +TEST(ScopedVectorTest, Clear) { LifeCycleWatcher watcher; EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); ScopedVector<LifeCycleObject> scoped_vector; scoped_vector.push_back(watcher.NewLifeCycleObject()); EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); - scoped_vector.reset(); + scoped_vector.clear(); EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); + EXPECT_EQ(static_cast<size_t>(0), scoped_vector.size()); +} + +TEST(ScopedVectorTest, WeakClear) { + LifeCycleWatcher watcher; + EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); + ScopedVector<LifeCycleObject> scoped_vector; + scoped_ptr<LifeCycleObject> object(watcher.NewLifeCycleObject()); + scoped_vector.push_back(object.get()); + EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); + scoped_vector.weak_clear(); + EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); + EXPECT_EQ(static_cast<size_t>(0), scoped_vector.size()); } TEST(ScopedVectorTest, Scope) { @@ -196,7 +209,7 @@ TEST(ScopedVectorTest, Passed) { EXPECT_EQ(0, deletes); ScopedVector<DeleteCounter> result = callback.Run(); EXPECT_EQ(0, deletes); - result.reset(); + result.clear(); EXPECT_EQ(1, deletes); }; |