Added OwningArrayPtr smart pointer class to provide an analogous class to

OwningPtr except that it works for pointers to arrays.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45266 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ted Kremenek 2007-12-20 19:53:47 +00:00
parent 714307207f
commit 3a818a3953

View File

@ -74,6 +74,60 @@ inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
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