[tinyptrvector] Add in a MutableArrayRef implicit conversion operator to complement...
authorMichael Gottesman <mgottesman@apple.com>
Mon, 19 Jan 2015 03:25:33 +0000 (03:25 +0000)
committerMichael Gottesman <mgottesman@apple.com>
Mon, 19 Jan 2015 03:25:33 +0000 (03:25 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226428 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/TinyPtrVector.h
unittests/ADT/TinyPtrVectorTest.cpp

index bb31540c508b80372f629ae7c3bae8930af75392..f29608f3d3d1e9dad3efca7975ee94ccd1640030 100644 (file)
@@ -116,6 +116,15 @@ public:
     return *Val.template get<VecTy*>();
   }
 
+  // implicit conversion operator to MutableArrayRef.
+  operator MutableArrayRef<EltTy>() {
+    if (Val.isNull())
+      return None;
+    if (Val.template is<EltTy>())
+      return *Val.getAddrOfPtr1();
+    return *Val.template get<VecTy*>();
+  }
+
   bool empty() const {
     // This vector can be empty if it contains no element, or if it
     // contains a pointer to an empty vector.
index a402e8be9df6d06dcd3d278673f9fe1ef845cf1d..294dfac0c6336e7b8570324a67af885ed23482ae 100644 (file)
@@ -438,3 +438,24 @@ TEST(TinyPtrVectorTest, ArrayRefCtorTest) {
     EXPECT_TRUE(V[i] == data[i]);
   }
 }
+
+TEST(TinyPtrVectorTest, MutableArrayRefTest) {
+  int data_array[128];
+  std::vector<int *> data;
+
+  for (unsigned i = 0, e = 128; i != e; ++i) {
+    data_array[i] = 324 - int(i);
+    data.push_back(&data_array[i]);
+  }
+
+  TinyPtrVector<int *> V(data);
+  EXPECT_TRUE(V.size() == 128);
+  EXPECT_FALSE(V.empty());
+
+  MutableArrayRef<int *> mut_array = V;
+  for (unsigned i = 0, e = 128; i != e; ++i) {
+    EXPECT_TRUE(mut_array[i] == data[i]);
+    mut_array[i] = 324 + mut_array[i];
+    EXPECT_TRUE(mut_array[i] == (324 + data[i]));
+  }
+}