llvm.org GIT mirror llvm / 3a818a3
Added OwningArrayPtr smart pointer class to provide an analogous class to OwningPtr except that it works for pointers to arrays. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45266 91177308-0d34-0410-b5e6-96231b3b80d8 Ted Kremenek 12 years ago
1 changed file(s) with 54 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
7373 a.swap(b);
7474 }
7575
76 /// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
77 /// functionality as OwningPtr, except that it works for array types.
78 template
79 class OwningArrayPtr {
80 OwningArrayPtr(OwningArrayPtr const &); // DO NOT IMPLEMENT
81 OwningArrayPtr &operator=(OwningArrayPtr const &); // DO NOT IMPLEMENT
82 T *Ptr;
83 public:
84 explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
85
86 ~OwningArrayPtr() {
87 delete [] Ptr;
88 }
89
90 /// reset - Change the current pointee to the specified pointer. Note that
91 /// calling this with any pointer (including a null pointer) deletes the
92 /// current pointer.
93 void reset(T *P = 0) {
94 if (P == Ptr) return;
95 T *Tmp = Ptr;
96 Ptr = P;
97 delete Tmp;
98 }
99
100 /// take - Reset the owning pointer to null and return its pointer. This does
101 /// not delete the pointer before returning it.
102 T *take() {
103 T *Tmp = Ptr;
104 Ptr = 0;
105 return Tmp;
106 }
107
108 T &operator[](std::ptrdiff_t i) const {
109 assert(Ptr && "Cannot dereference null pointer");
110 return Ptr[i];
111 }
112
113 T *get() const { return Ptr; }
114 operator bool() const { return Ptr != 0; }
115 bool operator!() const { return Ptr == 0; }
116
117 void swap(OwningArrayPtr &RHS) {
118 T *Tmp = RHS.Ptr;
119 RHS.Ptr = Ptr;
120 Ptr = Tmp;
121 }
122 };
123
124 template
125 inline void swap(OwningArrayPtr &a, OwningArrayPtr &b) {
126 a.swap(b);
127 }
128
129
76130 } // end namespace llvm
77131
78132 #endif