Jelajahi Sumber

Merge pull request #1291 from lrita/l_m

make butil::ScopedVector<T> support std::initializer_list
jamesge 3 tahun lalu
induk
melakukan
8ff49b304a
2 mengubah file dengan 14 tambahan dan 0 penghapusan
  1. 3 0
      src/butil/memory/scoped_vector.h
  2. 11 0
      test/scoped_vector_unittest.cc

+ 3 - 0
src/butil/memory/scoped_vector.h

@@ -38,6 +38,9 @@ class ScopedVector {
   ScopedVector() {}
   ~ScopedVector() { clear(); }
   ScopedVector(RValue other) { swap(*other.object); }
+#if __cplusplus >= 201103L  // >= C++11
+  ScopedVector(std::initializer_list<value_type> il) : v_(il) {}
+#endif
 
   ScopedVector& operator=(RValue rhs) {
     swap(*rhs.object);

+ 11 - 0
test/scoped_vector_unittest.cc

@@ -203,6 +203,17 @@ TEST(ScopedVectorTest, Scope) {
   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) {
   LifeCycleWatcher watcher;
   EXPECT_EQ(LC_INITIAL, watcher.life_cycle_state());