• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- llvm/ADT/OwningPtr.h - Smart ptr that owns the pointee ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines and implements the OwningPtr class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ADT_OWNING_PTR_H
15 #define LLVM_ADT_OWNING_PTR_H
16 
17 #include <cassert>
18 #include <cstddef>
19 
20 namespace llvm {
21 
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.
26 template<class T>
27 class OwningPtr {
28   OwningPtr(OwningPtr const &);             // DO NOT IMPLEMENT
29   OwningPtr &operator=(OwningPtr const &);  // DO NOT IMPLEMENT
30   T *Ptr;
31 public:
Ptr(P)32   explicit OwningPtr(T *P = 0) : Ptr(P) {}
33 
~OwningPtr()34   ~OwningPtr() {
35     delete Ptr;
36   }
37 
38   /// reset - Change the current pointee to the specified pointer.  Note that
39   /// calling this with any pointer (including a null pointer) deletes the
40   /// current pointer.
41   void reset(T *P = 0) {
42     if (P == Ptr) return;
43     T *Tmp = Ptr;
44     Ptr = P;
45     delete Tmp;
46   }
47 
48   /// take - Reset the owning pointer to null and return its pointer.  This does
49   /// not delete the pointer before returning it.
take()50   T *take() {
51     T *Tmp = Ptr;
52     Ptr = 0;
53     return Tmp;
54   }
55 
56   T &operator*() const {
57     assert(Ptr && "Cannot dereference null pointer");
58     return *Ptr;
59   }
60 
61   T *operator->() const { return Ptr; }
get()62   T *get() const { return Ptr; }
63   operator bool() const { return Ptr != 0; }
64   bool operator!() const { return Ptr == 0; }
65 
swap(OwningPtr & RHS)66   void swap(OwningPtr &RHS) {
67     T *Tmp = RHS.Ptr;
68     RHS.Ptr = Ptr;
69     Ptr = Tmp;
70   }
71 };
72 
73 template<class T>
swap(OwningPtr<T> & a,OwningPtr<T> & b)74 inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
75   a.swap(b);
76 }
77 
78 /// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
79 ///  functionality as OwningPtr, except that it works for array types.
80 template<class T>
81 class OwningArrayPtr {
82   OwningArrayPtr(OwningArrayPtr const &);            // DO NOT IMPLEMENT
83   OwningArrayPtr &operator=(OwningArrayPtr const &); // DO NOT IMPLEMENT
84   T *Ptr;
85 public:
Ptr(P)86   explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
87 
~OwningArrayPtr()88   ~OwningArrayPtr() {
89     delete [] Ptr;
90   }
91 
92   /// reset - Change the current pointee to the specified pointer.  Note that
93   /// calling this with any pointer (including a null pointer) deletes the
94   /// current pointer.
95   void reset(T *P = 0) {
96     if (P == Ptr) return;
97     T *Tmp = Ptr;
98     Ptr = P;
99     delete [] Tmp;
100   }
101 
102   /// take - Reset the owning pointer to null and return its pointer.  This does
103   /// not delete the pointer before returning it.
take()104   T *take() {
105     T *Tmp = Ptr;
106     Ptr = 0;
107     return Tmp;
108   }
109 
110   T &operator[](std::ptrdiff_t i) const {
111     assert(Ptr && "Cannot dereference null pointer");
112     return Ptr[i];
113   }
114 
get()115   T *get() const { return Ptr; }
116   operator bool() const { return Ptr != 0; }
117   bool operator!() const { return Ptr == 0; }
118 
swap(OwningArrayPtr & RHS)119   void swap(OwningArrayPtr &RHS) {
120     T *Tmp = RHS.Ptr;
121     RHS.Ptr = Ptr;
122     Ptr = Tmp;
123   }
124 };
125 
126 template<class T>
swap(OwningArrayPtr<T> & a,OwningArrayPtr<T> & b)127 inline void swap(OwningArrayPtr<T> &a, OwningArrayPtr<T> &b) {
128   a.swap(b);
129 }
130 
131 } // end namespace llvm
132 
133 #endif
134