2007-12-20 20:14:02 +01:00
|
|
|
//===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-12-20 20:14:02 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines and implements the OwningPtr class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_OWNING_PTR_H
|
|
|
|
#define LLVM_ADT_OWNING_PTR_H
|
|
|
|
|
|
|
|
#include <cassert>
|
2008-02-26 00:35:32 +01:00
|
|
|
#include <cstddef>
|
2007-12-20 20:14:02 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// OwningPtr smart pointer - OwningPtr mimics a built-in pointer except that it
|
|
|
|
/// guarantees deletion of the object pointed to, either on destruction of the
|
|
|
|
/// OwningPtr or via an explicit reset(). Once created, ownership of the
|
|
|
|
/// pointee object can be taken away from OwningPtr by using the take method.
|
2009-01-09 20:25:42 +01:00
|
|
|
template<class T>
|
2007-12-20 20:14:02 +01:00
|
|
|
class OwningPtr {
|
|
|
|
OwningPtr(OwningPtr const &); // DO NOT IMPLEMENT
|
2009-02-20 23:20:18 +01:00
|
|
|
OwningPtr &operator=(OwningPtr const &); // DO NOT IMPLEMENT
|
2007-12-20 20:14:02 +01:00
|
|
|
T *Ptr;
|
|
|
|
public:
|
|
|
|
explicit OwningPtr(T *P = 0) : Ptr(P) {}
|
|
|
|
|
|
|
|
~OwningPtr() {
|
|
|
|
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.
|
2009-01-09 20:25:42 +01:00
|
|
|
void reset(T *P = 0) {
|
2007-12-20 20:14:02 +01:00
|
|
|
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.
|
2009-01-09 20:25:42 +01:00
|
|
|
T *take() {
|
2007-12-20 20:14:02 +01:00
|
|
|
T *Tmp = Ptr;
|
|
|
|
Ptr = 0;
|
|
|
|
return Tmp;
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2007-12-20 20:14:02 +01:00
|
|
|
T &operator*() const {
|
|
|
|
assert(Ptr && "Cannot dereference null pointer");
|
|
|
|
return *Ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *operator->() const { return Ptr; }
|
|
|
|
T *get() const { return Ptr; }
|
|
|
|
operator bool() const { return Ptr != 0; }
|
|
|
|
bool operator!() const { return Ptr == 0; }
|
|
|
|
|
|
|
|
void swap(OwningPtr &RHS) {
|
|
|
|
T *Tmp = RHS.Ptr;
|
|
|
|
RHS.Ptr = Ptr;
|
|
|
|
Ptr = Tmp;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2007-12-20 20:53:47 +01:00
|
|
|
/// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
|
|
|
|
/// functionality as OwningPtr, except that it works for array types.
|
2009-01-09 20:25:42 +01:00
|
|
|
template<class T>
|
2007-12-20 20:53:47 +01:00
|
|
|
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.
|
2009-01-09 20:25:42 +01:00
|
|
|
void reset(T *P = 0) {
|
2007-12-20 20:53:47 +01:00
|
|
|
if (P == Ptr) return;
|
|
|
|
T *Tmp = Ptr;
|
|
|
|
Ptr = P;
|
2008-12-08 18:28:11 +01:00
|
|
|
delete [] Tmp;
|
2007-12-20 20:53:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// take - Reset the owning pointer to null and return its pointer. This does
|
|
|
|
/// not delete the pointer before returning it.
|
2009-01-09 20:25:42 +01:00
|
|
|
T *take() {
|
2007-12-20 20:53:47 +01:00
|
|
|
T *Tmp = Ptr;
|
|
|
|
Ptr = 0;
|
|
|
|
return Tmp;
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2007-12-20 20:53:47 +01:00
|
|
|
T &operator[](std::ptrdiff_t i) const {
|
|
|
|
assert(Ptr && "Cannot dereference null pointer");
|
|
|
|
return Ptr[i];
|
|
|
|
}
|
2009-01-09 20:25:42 +01:00
|
|
|
|
2007-12-20 20:53:47 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-12-20 20:14:02 +01:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|