[llvm-commits] [llvm] r45266 - /llvm/trunk/include/llvm/ADT/OwningPtr.h
Ted Kremenek
kremenek at apple.com
Thu Dec 20 11:53:48 PST 2007
Author: kremenek
Date: Thu Dec 20 13:53:47 2007
New Revision: 45266
URL: http://llvm.org/viewvc/llvm-project?rev=45266&view=rev
Log:
Added OwningArrayPtr smart pointer class to provide an analogous class to
OwningPtr except that it works for pointers to arrays.
Modified:
llvm/trunk/include/llvm/ADT/OwningPtr.h
Modified: llvm/trunk/include/llvm/ADT/OwningPtr.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/OwningPtr.h?rev=45266&r1=45265&r2=45266&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/OwningPtr.h (original)
+++ llvm/trunk/include/llvm/ADT/OwningPtr.h Thu Dec 20 13:53:47 2007
@@ -74,6 +74,60 @@
a.swap(b);
}
+/// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
+/// functionality as OwningPtr, except that it works for array types.
+template<class T>
+class OwningArrayPtr {
+ OwningArrayPtr(OwningArrayPtr const &); // DO NOT IMPLEMENT
+ OwningArrayPtr &operator=(OwningArrayPtr const &); // DO NOT IMPLEMENT
+ T *Ptr;
+public:
+ explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
+
+ ~OwningArrayPtr() {
+ delete [] Ptr;
+ }
+
+ /// reset - Change the current pointee to the specified pointer. Note that
+ /// calling this with any pointer (including a null pointer) deletes the
+ /// current pointer.
+ void reset(T *P = 0) {
+ if (P == Ptr) return;
+ T *Tmp = Ptr;
+ Ptr = P;
+ delete Tmp;
+ }
+
+ /// take - Reset the owning pointer to null and return its pointer. This does
+ /// not delete the pointer before returning it.
+ T *take() {
+ T *Tmp = Ptr;
+ Ptr = 0;
+ return Tmp;
+ }
+
+ T &operator[](std::ptrdiff_t i) const {
+ assert(Ptr && "Cannot dereference null pointer");
+ return Ptr[i];
+ }
+
+ T *get() const { return Ptr; }
+ operator bool() const { return Ptr != 0; }
+ bool operator!() const { return Ptr == 0; }
+
+ void swap(OwningArrayPtr &RHS) {
+ T *Tmp = RHS.Ptr;
+ RHS.Ptr = Ptr;
+ Ptr = Tmp;
+ }
+};
+
+template<class T>
+inline void swap(OwningArrayPtr<T> &a, OwningArrayPtr<T> &b) {
+ a.swap(b);
+}
+
+
} // end namespace llvm
#endif
More information about the llvm-commits
mailing list