ADT: Add OwningArrayRef class.
authorPeter Collingbourne <peter@pcc.me.uk>
Tue, 13 Dec 2016 20:24:24 +0000 (20:24 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Tue, 13 Dec 2016 20:24:24 +0000 (20:24 +0000)
This is a MutableArrayRef that owns its array.
I plan to use this in D22296.

Differential Revision: https://reviews.llvm.org/D27723

llvm-svn: 289579

llvm/include/llvm/ADT/ArrayRef.h

index c12688e..a8480d6 100644 (file)
@@ -413,6 +413,25 @@ namespace llvm {
     }
   };
 
+  /// This is a MutableArrayRef that owns its array.
+  template <typename T> class OwningArrayRef : public MutableArrayRef<T> {
+  public:
+    OwningArrayRef() {}
+    OwningArrayRef(size_t Size) : MutableArrayRef<T>(new T[Size], Size) {}
+    OwningArrayRef(ArrayRef<T> Data)
+        : MutableArrayRef<T>(new T[Data.size()], Data.size()) {
+      std::copy(Data.begin(), Data.end(), this->begin());
+    }
+    OwningArrayRef(OwningArrayRef &&Other) { *this = Other; }
+    OwningArrayRef &operator=(OwningArrayRef &&Other) {
+      delete this->data();
+      this->MutableArrayRef<T>::operator=(Other);
+      Other.MutableArrayRef<T>::operator=(MutableArrayRef<T>());
+      return *this;
+    }
+    ~OwningArrayRef() { delete this->data(); }
+  };
+
   /// @name ArrayRef Convenience constructors
   /// @{