Commit 0f85acf8 authored by gejun's avatar gejun

Move ScopedVector into namespace butil

parent 7318bee3
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
#include "butil/move.h" #include "butil/move.h"
#include "butil/stl_util.h" #include "butil/stl_util.h"
namespace butil {
// ScopedVector wraps a vector deleting the elements from its // ScopedVector wraps a vector deleting the elements from its
// destructor. // destructor.
template <class T> template <class T>
...@@ -134,4 +136,6 @@ class ScopedVector { ...@@ -134,4 +136,6 @@ class ScopedVector {
std::vector<T*> v_; std::vector<T*> v_;
}; };
} // namespace butil
#endif // BUTIL_MEMORY_SCOPED_VECTOR_H_ #endif // BUTIL_MEMORY_SCOPED_VECTOR_H_
...@@ -113,7 +113,7 @@ TEST(ScopedVectorTest, LifeCycleWatcher) { ...@@ -113,7 +113,7 @@ TEST(ScopedVectorTest, LifeCycleWatcher) {
TEST(ScopedVectorTest, PopBack) { TEST(ScopedVectorTest, PopBack) {
LifeCycleWatcher watcher; LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(watcher.NewLifeCycleObject()); scoped_vector.push_back(watcher.NewLifeCycleObject());
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
...@@ -125,7 +125,7 @@ TEST(ScopedVectorTest, PopBack) { ...@@ -125,7 +125,7 @@ TEST(ScopedVectorTest, PopBack) {
TEST(ScopedVectorTest, Clear) { TEST(ScopedVectorTest, Clear) {
LifeCycleWatcher watcher; LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(watcher.NewLifeCycleObject()); scoped_vector.push_back(watcher.NewLifeCycleObject());
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
...@@ -137,7 +137,7 @@ TEST(ScopedVectorTest, Clear) { ...@@ -137,7 +137,7 @@ TEST(ScopedVectorTest, Clear) {
TEST(ScopedVectorTest, WeakClear) { TEST(ScopedVectorTest, WeakClear) {
LifeCycleWatcher watcher; LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(watcher.NewLifeCycleObject()); scoped_vector.push_back(watcher.NewLifeCycleObject());
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
...@@ -151,7 +151,7 @@ TEST(ScopedVectorTest, ResizeShrink) { ...@@ -151,7 +151,7 @@ TEST(ScopedVectorTest, ResizeShrink) {
EXPECT_EQ(LC_INITIAL, first_watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, first_watcher.life_cycle_state());
LifeCycleWatcher second_watcher; LifeCycleWatcher second_watcher;
EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, second_watcher.life_cycle_state());
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(first_watcher.NewLifeCycleObject()); scoped_vector.push_back(first_watcher.NewLifeCycleObject());
EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state()); EXPECT_EQ(LC_CONSTRUCTED, first_watcher.life_cycle_state());
...@@ -176,7 +176,7 @@ TEST(ScopedVectorTest, ResizeShrink) { ...@@ -176,7 +176,7 @@ TEST(ScopedVectorTest, ResizeShrink) {
TEST(ScopedVectorTest, ResizeGrow) { TEST(ScopedVectorTest, ResizeGrow) {
LifeCycleWatcher watcher; LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(watcher.NewLifeCycleObject()); scoped_vector.push_back(watcher.NewLifeCycleObject());
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
...@@ -195,7 +195,7 @@ TEST(ScopedVectorTest, Scope) { ...@@ -195,7 +195,7 @@ TEST(ScopedVectorTest, Scope) {
LifeCycleWatcher watcher; LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
{ {
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(watcher.NewLifeCycleObject()); scoped_vector.push_back(watcher.NewLifeCycleObject());
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state()); EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
...@@ -207,12 +207,12 @@ TEST(ScopedVectorTest, MoveConstruct) { ...@@ -207,12 +207,12 @@ TEST(ScopedVectorTest, MoveConstruct) {
LifeCycleWatcher watcher; LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
{ {
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(watcher.NewLifeCycleObject()); scoped_vector.push_back(watcher.NewLifeCycleObject());
EXPECT_FALSE(scoped_vector.empty()); EXPECT_FALSE(scoped_vector.empty());
EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
ScopedVector<LifeCycleObject> scoped_vector_copy(scoped_vector.Pass()); butil::ScopedVector<LifeCycleObject> scoped_vector_copy(scoped_vector.Pass());
EXPECT_TRUE(scoped_vector.empty()); EXPECT_TRUE(scoped_vector.empty());
EXPECT_FALSE(scoped_vector_copy.empty()); EXPECT_FALSE(scoped_vector_copy.empty());
EXPECT_TRUE(watcher.IsWatching(scoped_vector_copy.back())); EXPECT_TRUE(watcher.IsWatching(scoped_vector_copy.back()));
...@@ -226,9 +226,9 @@ TEST(ScopedVectorTest, MoveAssign) { ...@@ -226,9 +226,9 @@ TEST(ScopedVectorTest, MoveAssign) {
LifeCycleWatcher watcher; LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
{ {
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.push_back(watcher.NewLifeCycleObject()); scoped_vector.push_back(watcher.NewLifeCycleObject());
ScopedVector<LifeCycleObject> scoped_vector_assign; butil::ScopedVector<LifeCycleObject> scoped_vector_assign;
EXPECT_FALSE(scoped_vector.empty()); EXPECT_FALSE(scoped_vector.empty());
EXPECT_TRUE(watcher.IsWatching(scoped_vector.back())); EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
...@@ -261,18 +261,18 @@ class DeleteCounter { ...@@ -261,18 +261,18 @@ class DeleteCounter {
}; };
template <typename T> template <typename T>
ScopedVector<T> PassThru(ScopedVector<T> scoper) { butil::ScopedVector<T> PassThru(butil::ScopedVector<T> scoper) {
return scoper.Pass(); return scoper.Pass();
} }
TEST(ScopedVectorTest, Passed) { TEST(ScopedVectorTest, Passed) {
int deletes = 0; int deletes = 0;
ScopedVector<DeleteCounter> deleter_vector; butil::ScopedVector<DeleteCounter> deleter_vector;
deleter_vector.push_back(new DeleteCounter(&deletes)); deleter_vector.push_back(new DeleteCounter(&deletes));
EXPECT_EQ(0, deletes); EXPECT_EQ(0, deletes);
EXPECT_EQ(0, deletes); EXPECT_EQ(0, deletes);
ScopedVector<DeleteCounter> result = deleter_vector.Pass(); butil::ScopedVector<DeleteCounter> result = deleter_vector.Pass();
EXPECT_EQ(0, deletes); EXPECT_EQ(0, deletes);
result.clear(); result.clear();
EXPECT_EQ(1, deletes); EXPECT_EQ(1, deletes);
...@@ -290,7 +290,7 @@ TEST(ScopedVectorTest, InsertRange) { ...@@ -290,7 +290,7 @@ TEST(ScopedVectorTest, InsertRange) {
} }
// Start scope for ScopedVector. // Start scope for ScopedVector.
{ {
ScopedVector<LifeCycleObject> scoped_vector; butil::ScopedVector<LifeCycleObject> scoped_vector;
scoped_vector.insert(scoped_vector.end(), vec.begin() + 1, vec.begin() + 3); scoped_vector.insert(scoped_vector.end(), vec.begin() + 1, vec.begin() + 3);
for(LifeCycleWatcher* it = watchers; it != watchers + arraysize(watchers); for(LifeCycleWatcher* it = watchers; it != watchers + arraysize(watchers);
++it) ++it)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment