Commit 6d06398a authored by lrita's avatar lrita

make butil::ScopedVector<T> support initializer_list

parent 31eba304
...@@ -6,6 +6,9 @@ ...@@ -6,6 +6,9 @@
#define BUTIL_MEMORY_SCOPED_VECTOR_H_ #define BUTIL_MEMORY_SCOPED_VECTOR_H_
#include <vector> #include <vector>
#if __cplusplus >= 201103L // >= C++11
#include <initializer_list>
#endif
#include "butil/basictypes.h" #include "butil/basictypes.h"
#include "butil/logging.h" #include "butil/logging.h"
...@@ -38,6 +41,9 @@ class ScopedVector { ...@@ -38,6 +41,9 @@ class ScopedVector {
ScopedVector() {} ScopedVector() {}
~ScopedVector() { clear(); } ~ScopedVector() { clear(); }
ScopedVector(RValue other) { swap(*other.object); } ScopedVector(RValue other) { swap(*other.object); }
#if __cplusplus >= 201103L // < C++11
ScopedVector(std::initializer_list<value_type> il) : v_(il) {}
#endif // __cplusplus < 201103L
ScopedVector& operator=(RValue rhs) { ScopedVector& operator=(RValue rhs) {
swap(*rhs.object); swap(*rhs.object);
......
...@@ -203,6 +203,17 @@ TEST(ScopedVectorTest, Scope) { ...@@ -203,6 +203,17 @@ TEST(ScopedVectorTest, Scope) {
EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state()); EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
} }
TEST(ScopedVectorTest, Scope2) {
LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
{
butil::ScopedVector<LifeCycleObject> scoped_vector{ watcher.NewLifeCycleObject() };
EXPECT_EQ(LC_CONSTRUCTED, watcher.life_cycle_state());
EXPECT_TRUE(watcher.IsWatching(scoped_vector.back()));
}
EXPECT_EQ(LC_DESTROYED, watcher.life_cycle_state());
}
TEST(ScopedVectorTest, MoveConstruct) { TEST(ScopedVectorTest, MoveConstruct) {
LifeCycleWatcher watcher; LifeCycleWatcher watcher;
EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state()); EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());
......
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