[llvm] r226428 - [tinyptrvector] Add in a MutableArrayRef implicit conversion operator to complement the ArrayRef implicit conversion operator.

Michael Gottesman mgottesman at apple.com
Sun Jan 18 19:25:33 PST 2015


Author: mgottesman
Date: Sun Jan 18 21:25:33 2015
New Revision: 226428

URL: http://llvm.org/viewvc/llvm-project?rev=226428&view=rev
Log:
[tinyptrvector] Add in a MutableArrayRef implicit conversion operator to complement the ArrayRef implicit conversion operator.

Modified:
    llvm/trunk/include/llvm/ADT/TinyPtrVector.h
    llvm/trunk/unittests/ADT/TinyPtrVectorTest.cpp

Modified: llvm/trunk/include/llvm/ADT/TinyPtrVector.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/TinyPtrVector.h?rev=226428&r1=226427&r2=226428&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/TinyPtrVector.h (original)
+++ llvm/trunk/include/llvm/ADT/TinyPtrVector.h Sun Jan 18 21:25:33 2015
@@ -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.

Modified: llvm/trunk/unittests/ADT/TinyPtrVectorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/TinyPtrVectorTest.cpp?rev=226428&r1=226427&r2=226428&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/TinyPtrVectorTest.cpp (original)
+++ llvm/trunk/unittests/ADT/TinyPtrVectorTest.cpp Sun Jan 18 21:25:33 2015
@@ -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]));
+  }
+}





More information about the llvm-commits mailing list