Add smart refcounting pointer class to ADT back (known before as IntrusiveSPtr.h).
[oota-llvm.git] / include / llvm / ADT / IntrusiveRefCntPtr.h
1 //== llvm/ADT/IntrusiveRefCntPtr.h - Smart Refcounting Pointer ----*- 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 IntrusiveRefCntPtr, a template class that
11 // implements a "smart" pointer for objects that maintain their own
12 // internal reference count, and RefCountedBase/RefCountedBaseVPTR, two
13 // generic base classes for objects that wish to have their lifetimes
14 // managed using reference counting.
15 //
16 // IntrusiveRefCntPtr is similar to Boost's intrusive_ptr with added
17 // LLVM-style casting.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_ADT_INTRUSIVE_REF_CNT_PTR
22 #define LLVM_ADT_INTRUSIVE_REF_CNT_PTR
23
24 #include <cassert>
25 #include <cstddef>
26
27 #include "llvm/Support/Casting.h"
28
29 // Forward declarations
30
31 namespace llvm {
32   template <class T>
33   class RefCountedBase;
34
35   template <class T>
36   class RefCountedBaseVPTR;
37 }
38
39 template <class T>
40 void IntrusivePtrAddRef(llvm::RefCountedBase<T>*);
41
42 template <class T>
43 void IntrusivePtrRelease(llvm::RefCountedBase<T>*);
44
45 template <class T>
46 void IntrusivePtrAddRef(llvm::RefCountedBaseVPTR<T>*);
47
48 template <class T>
49 void IntrusivePtrRelease(llvm::RefCountedBaseVPTR<T>*);
50
51
52 namespace llvm {
53
54 //===----------------------------------------------------------------------===//
55 /// RefCountedBase - A generic base class for objects that wish to
56 ///  have their lifetimes managed using reference counts. Classes
57 ///  subclass RefCountedBase to obtain such functionality, and are
58 ///  typically handled with IntrusivePtr "smart pointers" (see below)
59 ///  which automatically handle the management of reference counts.
60 ///  Objects that subclass RefCountedBase should not be allocated on
61 ///  the stack, as invoking "delete" (which is called when the
62 ///  reference count hits 0) on such objects is an error.
63 //===----------------------------------------------------------------------===//
64   template <class Derived>
65   class RefCountedBase {
66     unsigned ref_cnt;
67
68   protected:
69     RefCountedBase() : ref_cnt(0) {}
70
71     void Retain() { ++ref_cnt; }
72     void Release() {
73       assert (ref_cnt > 0 && "Reference count is already zero.");
74       if (--ref_cnt == 0) delete static_cast<Derived*>(this);
75     }
76
77     friend void IntrusivePtrAddRef<Derived>(RefCountedBase<Derived>*);
78     friend void IntrusivePtrRelease<Derived>(RefCountedBase<Derived>*);
79   };
80
81 //===----------------------------------------------------------------------===//
82 /// RefCountedBaseVPTR - A class that has the same function as
83 ///  RefCountedBase, but with a virtual destructor. Should be used
84 ///  instead of RefCountedBase for classes that have virtual
85 ///  destructors. Classes that inherit from RefCountedBaseVPTR can't
86 ///  be allocated on stack.
87 //===----------------------------------------------------------------------===//
88   template <class Derived>
89   class RefCountedBaseVPTR {
90     unsigned ref_cnt;
91
92   protected:
93     RefCountedBaseVPTR() : ref_cnt(0) {}
94     virtual ~RefCountedBaseVPTR() {}
95
96     void Retain() { ++ref_cnt; }
97     void Release() {
98       assert (ref_cnt > 0 && "Reference count is already zero.");
99       if (--ref_cnt == 0) delete this;
100     }
101
102     friend void IntrusivePtrAddRef<Derived>(RefCountedBaseVPTR<Derived>*);
103     friend void IntrusivePtrRelease<Derived>(RefCountedBaseVPTR<Derived>*);
104   };
105
106 }
107
108 //===----------------------------------------------------------------------===//
109 /// IntrusivePtrAddRef - A utility function used by IntrusiveRefCntPtr
110 ///  to increment the reference count of an RefCountedBase-derived object.
111 //===----------------------------------------------------------------------===//
112 template <class T>
113 void IntrusivePtrAddRef(llvm::RefCountedBase<T>* O) {
114   O->Retain();
115 }
116
117 //===----------------------------------------------------------------------===//
118 /// IntrusivePtrRelease - The complement of IntrusivePtrAddRef;
119 ///  decrements the reference count of a RefCounted object.
120 //===----------------------------------------------------------------------===//
121 template <class T>
122 void IntrusivePtrRelease(llvm::RefCountedBase<T>* O) {
123   O->Release();
124 }
125
126
127 namespace llvm {
128
129 //===----------------------------------------------------------------------===//
130 /// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
131 ///  that assumes the wrapped object has a reference count associated
132 ///  with it that can be managed via calls to
133 ///  IntrusivePtrAddRef/IntrusivePtrRelease.  The smart pointers
134 ///  manage reference counts via the RAII idiom: upon creation of
135 ///  smart pointer the reference count of the wrapped object is
136 ///  incremented and upon destruction of the smart pointer the
137 ///  reference count is decremented.  This class also safely handles
138 ///  wrapping NULL pointers.
139 //===----------------------------------------------------------------------===//
140   template <typename T>
141   class IntrusiveRefCntPtr {
142     T* Obj;
143     typedef IntrusiveRefCntPtr this_type;
144   public:
145     typedef T element_type;
146
147     explicit IntrusiveRefCntPtr() : Obj(NULL) {}
148
149     explicit IntrusiveRefCntPtr(T* obj) : Obj(obj) {
150       retain();
151     }
152
153     IntrusiveRefCntPtr(const IntrusiveRefCntPtr& S) : Obj(S.Obj) {
154       retain();
155     }
156
157     template <class X>
158     IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
159       : Obj(S.getPtr()) {
160       retain();
161     }
162
163     template <class X>
164     IntrusiveRefCntPtr& operator=(const IntrusiveRefCntPtr<X>& S) {
165       replace(S.getPtr());
166       return *this;
167     }
168
169     IntrusiveRefCntPtr& operator=(T * S) {
170       replace(S);
171       return *this;
172     }
173
174     ~IntrusiveRefCntPtr() { release(); }
175
176     T& operator*() const { return *Obj; }
177
178     T* operator->() const { return Obj; }
179
180     T* getPtr() const { return Obj; }
181
182     typedef T * IntrusiveRefCntPtr::*unspecified_bool_type;
183     operator unspecified_bool_type() const {
184       return Obj == NULL ? NULL : &IntrusiveRefCntPtr::getPtr;
185     }
186
187     void swap(IntrusiveRefCntPtr& other) {
188       T* tmp = other.Obj;
189       other.Obj = Obj;
190       Obj = tmp;
191     }
192
193   private:
194     void retain() { if (Obj) IntrusivePtrAddRef(Obj); }
195     void release() { if (Obj) IntrusivePtrRelease(Obj); }
196
197     void replace(T* S) {
198       this_type(S).swap(this);
199     }
200   };
201
202   template<class T, class U>
203   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
204                          const IntrusiveRefCntPtr<U>& B)
205   {
206     return A.getPtr() == B.getPtr();
207   }
208
209   template<class T, class U>
210   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
211                          const IntrusiveRefCntPtr<U>& B)
212   {
213     return A.getPtr() != B.getPtr();
214   }
215
216   template<class T, class U>
217   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
218                          U* B)
219   {
220     return A.getPtr() == B;
221   }
222
223   template<class T, class U>
224   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
225                          U* B)
226   {
227     return A.getPtr() != B;
228   }
229
230   template<class T, class U>
231   inline bool operator==(T* A,
232                          const IntrusiveRefCntPtr<U>& B)
233   {
234     return A == B.getPtr();
235   }
236
237   template<class T, class U>
238   inline bool operator!=(T* A,
239                          const IntrusiveRefCntPtr<U>& B)
240   {
241     return A != B.getPtr();
242   }
243
244 //===----------------------------------------------------------------------===//
245 // LLVM-style downcasting support for IntrusiveRefCntPtr objects
246 //===----------------------------------------------------------------------===//
247
248   template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
249     typedef T* SimpleType;
250     static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
251       return Val.getPtr();
252     }
253   };
254
255   template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
256     typedef T* SimpleType;
257     static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
258       return Val.getPtr();
259     }
260   };
261
262 } // end namespace llvm
263
264 #endif // LLVM_ADT_INTRUSIVE_REF_CNT_PTR