Support/FileSystem: Implement recursive_directory_iterator and make
[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
69   protected:
70     RefCountedBaseVPTR() : ref_cnt(0) {}
71     RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
72
73     virtual ~RefCountedBaseVPTR() {}
74
75     void Retain() const { ++ref_cnt; }
76     void Release() const {
77       assert (ref_cnt > 0 && "Reference count is already zero.");
78       if (--ref_cnt == 0) delete this;
79     }
80
81     template <typename T>
82     friend class IntrusiveRefCntPtr;
83   };
84
85 //===----------------------------------------------------------------------===//
86 /// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
87 ///  that assumes the wrapped object has a reference count associated
88 ///  with it that can be managed via calls to
89 ///  IntrusivePtrAddRef/IntrusivePtrRelease.  The smart pointers
90 ///  manage reference counts via the RAII idiom: upon creation of
91 ///  smart pointer the reference count of the wrapped object is
92 ///  incremented and upon destruction of the smart pointer the
93 ///  reference count is decremented.  This class also safely handles
94 ///  wrapping NULL pointers.
95 ///
96 /// Reference counting is implemented via calls to
97 ///  Obj->Retain()/Obj->Release(). Release() is required to destroy
98 ///  the object when the reference count reaches zero. Inheriting from
99 ///  RefCountedBase/RefCountedBaseVPTR takes care of this
100 ///  automatically.
101 //===----------------------------------------------------------------------===//
102   template <typename T>
103   class IntrusiveRefCntPtr {
104     T* Obj;
105     typedef IntrusiveRefCntPtr this_type;
106   public:
107     typedef T element_type;
108
109     explicit IntrusiveRefCntPtr() : Obj(0) {}
110
111     explicit IntrusiveRefCntPtr(T* obj) : Obj(obj) {
112       retain();
113     }
114
115     IntrusiveRefCntPtr(const IntrusiveRefCntPtr& S) : Obj(S.Obj) {
116       retain();
117     }
118
119     template <class X>
120     IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
121       : Obj(S.getPtr()) {
122       retain();
123     }
124
125     IntrusiveRefCntPtr& operator=(const IntrusiveRefCntPtr& S) {
126       replace(S.getPtr());
127       return *this;
128     }
129
130     template <class X>
131     IntrusiveRefCntPtr& operator=(const IntrusiveRefCntPtr<X>& S) {
132       replace(S.getPtr());
133       return *this;
134     }
135
136     IntrusiveRefCntPtr& operator=(T * S) {
137       replace(S);
138       return *this;
139     }
140
141     ~IntrusiveRefCntPtr() { release(); }
142
143     T& operator*() const { return *Obj; }
144
145     T* operator->() const { return Obj; }
146
147     T* getPtr() const { return Obj; }
148
149     typedef T* (IntrusiveRefCntPtr::*unspecified_bool_type) () const;
150     operator unspecified_bool_type() const {
151       return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
152     }
153
154     void swap(IntrusiveRefCntPtr& other) {
155       T* tmp = other.Obj;
156       other.Obj = Obj;
157       Obj = tmp;
158     }
159
160     void reset() {
161       release();
162       Obj = 0;
163     }
164
165     void resetWithoutRelease() {
166       Obj = 0;
167     }
168
169   private:
170     void retain() { if (Obj) Obj->Retain(); }
171     void release() { if (Obj) Obj->Release(); }
172
173     void replace(T* S) {
174       this_type(S).swap(*this);
175     }
176   };
177
178   template<class T, class U>
179   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
180                          const IntrusiveRefCntPtr<U>& B)
181   {
182     return A.getPtr() == B.getPtr();
183   }
184
185   template<class T, class U>
186   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
187                          const IntrusiveRefCntPtr<U>& B)
188   {
189     return A.getPtr() != B.getPtr();
190   }
191
192   template<class T, class U>
193   inline bool operator==(const IntrusiveRefCntPtr<T>& A,
194                          U* B)
195   {
196     return A.getPtr() == B;
197   }
198
199   template<class T, class U>
200   inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
201                          U* B)
202   {
203     return A.getPtr() != B;
204   }
205
206   template<class T, class U>
207   inline bool operator==(T* A,
208                          const IntrusiveRefCntPtr<U>& B)
209   {
210     return A == B.getPtr();
211   }
212
213   template<class T, class U>
214   inline bool operator!=(T* A,
215                          const IntrusiveRefCntPtr<U>& B)
216   {
217     return A != B.getPtr();
218   }
219
220 //===----------------------------------------------------------------------===//
221 // LLVM-style downcasting support for IntrusiveRefCntPtr objects
222 //===----------------------------------------------------------------------===//
223
224   template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
225     typedef T* SimpleType;
226     static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
227       return Val.getPtr();
228     }
229   };
230
231   template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
232     typedef T* SimpleType;
233     static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
234       return Val.getPtr();
235     }
236   };
237
238 } // end namespace llvm
239
240 #endif // LLVM_ADT_INTRUSIVE_REF_CNT_PTR