1 //===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines and implements the OwningPtr class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_OWNING_PTR_H
15 #define LLVM_ADT_OWNING_PTR_H
22 /// OwningPtr smart pointer - OwningPtr mimics a built-in pointer except that it
23 /// guarantees deletion of the object pointed to, either on destruction of the
24 /// OwningPtr or via an explicit reset(). Once created, ownership of the
25 /// pointee object can be taken away from OwningPtr by using the take method.
28 OwningPtr &operator=(const OwningPtr &); // DO NOT IMPLEMENT
31 explicit OwningPtr(T *P = 0) : Ptr(P) {}
33 OwningPtr(const OwningPtr &RHS) : Ptr(0) {
34 assert(RHS.Ptr == 0 && "Only null OwningPtr's are copyable!");
41 /// reset - Change the current pointee to the specified pointer. Note that
42 /// calling this with any pointer (including a null pointer) deletes the
44 void reset(T *P = 0) {
51 /// take - Reset the owning pointer to null and return its pointer. This does
52 /// not delete the pointer before returning it.
59 T &operator*() const {
60 assert(Ptr && "Cannot dereference null pointer");
64 T *operator->() const { return Ptr; }
65 T *get() const { return Ptr; }
66 operator bool() const { return Ptr != 0; }
67 bool operator!() const { return Ptr == 0; }
69 void swap(OwningPtr &RHS) {
77 inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
81 /// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
82 /// functionality as OwningPtr, except that it works for array types.
84 class OwningArrayPtr {
85 OwningArrayPtr(OwningArrayPtr const &); // DO NOT IMPLEMENT
86 OwningArrayPtr &operator=(OwningArrayPtr const &); // DO NOT IMPLEMENT
89 explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
95 /// reset - Change the current pointee to the specified pointer. Note that
96 /// calling this with any pointer (including a null pointer) deletes the
98 void reset(T *P = 0) {
105 /// take - Reset the owning pointer to null and return its pointer. This does
106 /// not delete the pointer before returning it.
113 T &operator[](std::ptrdiff_t i) const {
114 assert(Ptr && "Cannot dereference null pointer");
118 T *get() const { return Ptr; }
119 operator bool() const { return Ptr != 0; }
120 bool operator!() const { return Ptr == 0; }
122 void swap(OwningArrayPtr &RHS) {
130 inline void swap(OwningArrayPtr<T> &a, OwningArrayPtr<T> &b) {
134 } // end namespace llvm