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(OwningPtr const &); // DO NOT IMPLEMENT
29 OwningPtr &operator=(OwningPtr const &); // DO NOT IMPLEMENT
32 explicit OwningPtr(T *P = 0) : Ptr(P) {}
38 /// reset - Change the current pointee to the specified pointer. Note that
39 /// calling this with any pointer (including a null pointer) deletes the
41 void reset(T *P = 0) {
48 /// take - Reset the owning pointer to null and return its pointer. This does
49 /// not delete the pointer before returning it.
56 T &operator*() const {
57 assert(Ptr && "Cannot dereference null pointer");
61 T *operator->() const { return Ptr; }
62 T *get() const { return Ptr; }
63 operator bool() const { return Ptr != 0; }
64 bool operator!() const { return Ptr == 0; }
66 void swap(OwningPtr &RHS) {
74 inline void swap(OwningPtr<T> &a, OwningPtr<T> &b) {
78 /// OwningArrayPtr smart pointer - OwningArrayPtr provides the same
79 /// functionality as OwningPtr, except that it works for array types.
81 class OwningArrayPtr {
82 OwningArrayPtr(OwningArrayPtr const &); // DO NOT IMPLEMENT
83 OwningArrayPtr &operator=(OwningArrayPtr const &); // DO NOT IMPLEMENT
86 explicit OwningArrayPtr(T *P = 0) : Ptr(P) {}
92 /// reset - Change the current pointee to the specified pointer. Note that
93 /// calling this with any pointer (including a null pointer) deletes the
95 void reset(T *P = 0) {
102 /// take - Reset the owning pointer to null and return its pointer. This does
103 /// not delete the pointer before returning it.
110 T &operator[](std::ptrdiff_t i) const {
111 assert(Ptr && "Cannot dereference null pointer");
115 T *get() const { return Ptr; }
116 operator bool() const { return Ptr != 0; }
117 bool operator!() const { return Ptr == 0; }
119 void swap(OwningArrayPtr &RHS) {
127 inline void swap(OwningArrayPtr<T> &a, OwningArrayPtr<T> &b) {
131 /// \brief A smart pointer that may own the object it points to.
133 /// An instance of \c MaybeOwningPtr may own the object it points to. If so,
134 /// it will guarantee that the object will be deleted either on destructin of
135 /// the OwningPtr or via an explicit reset(). Once created, ownership of the
136 /// pointee object can be taken away from OwningPtr by using the \c take()
139 class MaybeOwningPtr {
143 struct MaybeOwningPtrRef {
144 MaybeOwningPtrRef(T *Ptr, bool &Owned) : Ptr(Ptr), Owned(Owned) { }
151 MaybeOwningPtr() : Ptr(0), Owned(false) { }
153 explicit MaybeOwningPtr(T *P, bool OwnP) : Ptr(P), Owned(OwnP) {}
155 /// \brief Take ownership of the pointer stored in \c Other.
156 MaybeOwningPtr(MaybeOwningPtr& Other) : Ptr(Other.Ptr), Owned(Other.Owned) {
160 MaybeOwningPtr(MaybeOwningPtrRef Other) : Ptr(Other.Ptr), Owned(Other.Owned) {
164 /// \brief Take ownership of the ppinter stored in \c Other.
165 MaybeOwningPtr &operator=(MaybeOwningPtr &Other) {
166 reset(Other.Ptr, Other.Owned);
176 operator MaybeOwningPtrRef() { return MaybeOwningPtrRef(Ptr, Owned); }
178 /// reset - Change the current pointee to the specified pointer. Note that
179 /// calling this with any pointer (including a null pointer) deletes the
181 void reset(T *P, bool OwnP) {
190 /// take - Return the underlying pointer and take ownership of it. This
191 /// \c MaybeOwningPtr must have ownership before the call, and will
192 /// relinquish ownership as part of the call.
194 assert(Owned && "Cannot take ownership from a non-owning pointer");
199 T &operator*() const {
200 assert(Ptr && "Cannot dereference null pointer");
204 T *operator->() const { return Ptr; }
205 T *get() const { return Ptr; }
206 operator bool() const { return Ptr != 0; }
207 bool operator!() const { return Ptr == 0; }
209 void swap(MaybeOwningPtr &RHS) {
213 bool TmpOwned = RHS.Owned;
220 inline void swap(MaybeOwningPtr<T> &a, MaybeOwningPtr<T> &b) {
224 } // end namespace llvm