Unweaken vtables as per http://llvm.org/docs/CodingStandards.html#ll_virtual_anch
[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
26 #include "llvm/Support/Casting.h"
27
28 namespace llvm {
29
30   template <class T>
31   class IntrusiveRefCntPtr;
32
33 //===----------------------------------------------------------------------===//
34 /// RefCountedBase - A generic base class for objects that wish to
35 ///  have their lifetimes managed using reference counts. Classes
36 ///  subclass RefCountedBase to obtain such functionality, and are
37 ///  typically handled with IntrusivePtr "smart pointers" (see below)
38 ///  which automatically handle the management of reference counts.
39 ///  Objects that subclass RefCountedBase should not be allocated on
40 ///  the stack, as invoking "delete" (which is called when the
41 ///  reference count hits 0) on such objects is an error.
42 //===----------------------------------------------------------------------===//
43   template <class Derived>
44   class RefCountedBase {
45     mutable unsigned ref_cnt;
46
47   public:
48     RefCountedBase() : ref_cnt(0) {}
49     RefCountedBase(const RefCountedBase &) : ref_cnt(0) {}
50
51     void Retain() const { ++ref_cnt; }
52     void Release() const {
53       assert (ref_cnt > 0 && "Reference count is already zero.");
54       if (--ref_cnt == 0) delete static_cast<const Derived*>(this);
55     }
56   };
57
58 //===----------------------------------------------------------------------===//
59 /// RefCountedBaseVPTR - A class that has the same function as
60 ///  RefCountedBase, but with a virtual destructor. Should be used
61 ///  instead of RefCountedBase for classes that already have virtual
62 ///  methods to enforce dynamic allocation via 'new'. Classes that
63 ///  inherit from RefCountedBaseVPTR can't be allocated on stack -
64 ///  attempting to do this will produce a compile error.
65 //===----------------------------------------------------------------------===//
66   class RefCountedBaseVPTR {
67     mutable unsigned ref_cnt;
68     virtual void anchor();
69
70   protected:
71     RefCountedBaseVPTR() : ref_cnt(0) {}
72     RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
73
74     virtual ~RefCountedBaseVPTR() {}
75
76     void Retain() const { ++ref_cnt; }
77     void Release() const {
78       assert (ref_cnt > 0 && "Reference count is already zero.");
79       if (--ref_cnt == 0) delete this;
80     }
81
82     template <typename T>
83     friend class IntrusiveRefCntPtr;
84   };
85
86 //===----------------------------------------------------------------------===//
87 /// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
88 ///  that assumes the wrapped object has a reference count associated
89 ///  with it that can be managed via calls to
90 ///  IntrusivePtrAddRef/IntrusivePtrRelease.  The smart pointers
91 ///  manage reference counts via the RAII idiom: upon creation of
92 ///  smart pointer the reference count of the wrapped object is
93 ///  incremented and upon destruction of the smart pointer the
94 ///  reference count is decremented.  This class also safely handles
95 ///  wrapping NULL pointers.
96 ///
97 /// Reference counting is implemented via calls to
98 ///  Obj->Retain()/Obj->Release(). Release() is required to destroy
99 ///  the object when the reference count reaches zero. Inheriting from
100 ///  RefCountedBase/RefCountedBaseVPTR takes care of this
101 ///  automatically.
102 //===----------------------------------------------------------------------===//
103   template <typename T>
104   class IntrusiveRefCntPtr {
105     T* Obj;
106     typedef IntrusiveRefCntPtr this_type;
107   public:
108     typedef T element_type;
109
110     explicit IntrusiveRefCntPtr() : Obj(0) {}
111
112     explicit IntrusiveRefCntPtr(T* obj) : Obj(obj) {
113       retain();
114     }
115
116     IntrusiveRefCntPtr(const IntrusiveRefCntPtr& S) : Obj(S.Obj) {
117       retain();
118     }
119
120     template <class X>
121     IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
122       : Obj(S.getPtr()) {
123       retain();
124     }
125
126     IntrusiveRefCntPtr& operator=(const IntrusiveRefCntPtr& S) {
127       replace(S.getPtr());
128       return *this;
129     }
130
131     template <class X>
132     IntrusiveRefCntPtr& operator=(const IntrusiveRefCntPtr<X>& S) {
133       replace(S.getPtr());
134       return *this;
135     }
136
137     IntrusiveRefCntPtr& operator=(T * S) {
138       replace(S);
139       return *this;
140     }
141
142     ~IntrusiveRefCntPtr() { release(); }
143
144     T& operator*() const { return *Obj; }
145
146     T* operator->() const { return Obj; }
147
148     T* getPtr() const { return Obj; }
149
150     typedef T* (IntrusiveRefCntPtr::*unspecified_bool_type) () const;
151     operator unspecified_bool_type() const {
152       return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
153     }
154
155     void swap(IntrusiveRefCntPtr& other) {
156       T* tmp = other.Obj;
157       other.Obj = Obj;
158       Obj = tmp;
159     }
160
161     void reset() {
162       release();
163       Obj = 0;
164     }
165
166     void resetWithoutRelease() {
167       Obj = 0;
168     }
169
170   private:
171     void retain() { if (Obj) Obj->Retain(); }
172     void release() { if (Obj) Obj->Release(); }
173
174     void replace(T* S) {
175       this_type(S).swap(*this);
176     }
177   };
178
179   template<class T, class U>
180   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
181                          const IntrusiveRefCntPtr<U>& B)
182   {
183     return A.getPtr() == B.getPtr();
184   }
185
186   template<class T, class U>
187   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
188                          const IntrusiveRefCntPtr<U>& B)
189   {
190     return A.getPtr() != B.getPtr();
191   }
192
193   template<class T, class U>
194   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
195                          U* B)
196   {
197     return A.getPtr() == B;
198   }
199
200   template<class T, class U>
201   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
202                          U* B)
203   {
204     return A.getPtr() != B;
205   }
206
207   template<class T, class U>
208   inline bool operator==(T* A,
209                          const IntrusiveRefCntPtr<U>& B)
210   {
211     return A == B.getPtr();
212   }
213
214   template<class T, class U>
215   inline bool operator!=(T* A,
216                          const IntrusiveRefCntPtr<U>& B)
217   {
218     return A != B.getPtr();
219   }
220
221 //===----------------------------------------------------------------------===//
222 // LLVM-style downcasting support for IntrusiveRefCntPtr objects
223 //===----------------------------------------------------------------------===//
224
225   template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
226     typedef T* SimpleType;
227     static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
228       return Val.getPtr();
229     }
230   };
231
232   template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
233     typedef T* SimpleType;
234     static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
235       return Val.getPtr();
236     }
237   };
238
239 } // end namespace llvm
240
241 #endif // LLVM_ADT_INTRUSIVE_REF_CNT_PTR