2007-12-20 19:14:02 +00:00
|
|
|
//===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-12-20 19:14:02 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines and implements the OwningPtr class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_ADT_OWNINGPTR_H
|
|
|
|
#define LLVM_ADT_OWNINGPTR_H
|
2007-12-20 19:14:02 +00:00
|
|
|
|
2012-09-17 06:31:17 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-12-20 19:14:02 +00:00
|
|
|
#include <cassert>
|
2008-02-25 23:35:32 +00:00
|
|
|
#include <cstddef>
|
2007-12-20 19:14:02 +00: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 19:25:42 +00:00
|
|
|
template<class T>
|
2007-12-20 19:14:02 +00:00
|
|
|
class OwningPtr {
|
2012-09-17 06:31:17 +00:00
|
|
|
OwningPtr(OwningPtr const &) LLVM_DELETED_FUNCTION;
|
|
|
|
OwningPtr &operator=(OwningPtr const &) LLVM_DELETED_FUNCTION;
|
2007-12-20 19:14:02 +00:00
|
|
|
T *Ptr;
|
|
|
|
public:
|
|
|
|
explicit OwningPtr(T *P = 0) : Ptr(P) {}
|
|
|
|
|
2012-11-30 11:45:22 +00:00
|
|
|
#if LLVM_HAS_RVALUE_REFERENCES
|
2012-10-18 22:22:58 +00:00
|
|
|
OwningPtr(OwningPtr &&Other) : Ptr(Other.take()) {}
|
|
|
|
|
|
|
|
OwningPtr &operator=(OwningPtr &&Other) {
|
|
|
|
reset(Other.take());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-20 19:14:02 +00:00
|
|
|
~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 19:25:42 +00:00
|
|
|
void reset(T *P = 0) {
|
2007-12-20 19:14:02 +00: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 19:25:42 +00:00
|
|
|
T *take() {
|
2007-12-20 19:14:02 +00:00
|
|
|
T *Tmp = Ptr;
|
|
|
|
Ptr = 0;
|
|
|
|
return Tmp;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-12-20 19:14:02 +00: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 19:53:47 +00:00
|
|
|
/// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
|
|
|
|
/// functionality as OwningPtr, except that it works for array types.
|
2009-01-09 19:25:42 +00:00
|
|
|
template<class T>
|
2007-12-20 19:53:47 +00:00
|
|
|
class OwningArrayPtr {
|
2012-09-17 06:31:17 +00:00
|
|
|
OwningArrayPtr(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
|
|
|
|
OwningArrayPtr &operator=(OwningArrayPtr const &) LLVM_DELETED_FUNCTION;
|
2007-12-20 19:53:47 +00:00
|
|
|
T *Ptr;
|
|
|
|
public:
|
|
|
|
explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
|
|
|
|
|
2012-11-30 11:45:22 +00:00
|
|
|
#if LLVM_HAS_RVALUE_REFERENCES
|
2012-10-18 22:22:58 +00:00
|
|
|
OwningArrayPtr(OwningArrayPtr &&Other) : Ptr(Other.take()) {}
|
|
|
|
|
|
|
|
OwningArrayPtr &operator=(OwningArrayPtr &&Other) {
|
|
|
|
reset(Other.take());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-20 19:53:47 +00:00
|
|
|
~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 19:25:42 +00:00
|
|
|
void reset(T *P = 0) {
|
2007-12-20 19:53:47 +00:00
|
|
|
if (P == Ptr) return;
|
|
|
|
T *Tmp = Ptr;
|
|
|
|
Ptr = P;
|
2008-12-08 17:28:11 +00:00
|
|
|
delete [] Tmp;
|
2007-12-20 19:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// take - Reset the owning pointer to null and return its pointer. This does
|
|
|
|
/// not delete the pointer before returning it.
|
2009-01-09 19:25:42 +00:00
|
|
|
T *take() {
|
2007-12-20 19:53:47 +00:00
|
|
|
T *Tmp = Ptr;
|
|
|
|
Ptr = 0;
|
|
|
|
return Tmp;
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-12-20 19:53:47 +00:00
|
|
|
T &operator[](std::ptrdiff_t i) const {
|
|
|
|
assert(Ptr && "Cannot dereference null pointer");
|
|
|
|
return Ptr[i];
|
|
|
|
}
|
2009-01-09 19:25:42 +00:00
|
|
|
|
2007-12-20 19:53:47 +00: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 19:14:02 +00:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif
|