aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/ADT/OwningPtr.h
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2007-12-20 19:14:02 +0000
committerChris Lattner <sabre@nondot.org>2007-12-20 19:14:02 +0000
commit714307207ffb8b435e090e94d0efa4c0e991df36 (patch)
tree78d07f3bb4f60f0f8913f6c1390c33445ceeae6b /include/llvm/ADT/OwningPtr.h
parentf6cb6eeb5a0e2037c9fd573ef3002f56fc2d130d (diff)
downloadexternal_llvm-714307207ffb8b435e090e94d0efa4c0e991df36.zip
external_llvm-714307207ffb8b435e090e94d0efa4c0e991df36.tar.gz
external_llvm-714307207ffb8b435e090e94d0efa4c0e991df36.tar.bz2
add new smart pointer for clang.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45261 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT/OwningPtr.h')
-rw-r--r--include/llvm/ADT/OwningPtr.h79
1 files changed, 79 insertions, 0 deletions
diff --git a/include/llvm/ADT/OwningPtr.h b/include/llvm/ADT/OwningPtr.h
new file mode 100644
index 0000000..76250ed
--- /dev/null
+++ b/include/llvm/ADT/OwningPtr.h
@@ -0,0 +1,79 @@
+//===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file was developed by Chris Lattner and is distributed under the
+// University of Illinois Open Source License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines and implements the OwningPtr class.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ADT_OWNING_PTR_H
+#define LLVM_ADT_OWNING_PTR_H
+
+#include <cassert>
+
+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.
+template<class T>
+class OwningPtr {
+ OwningPtr(OwningPtr const &); // DO NOT IMPLEMENT
+ OwningPtr &operator=(OwningPtr const &); // DO NOT IMPLEMENT
+ 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.
+ 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*() 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);
+}
+
+} // end namespace llvm
+
+#endif