IntrusiveRefCntPtr.h [plain text]
#ifndef LLVM_ADT_INTRUSIVE_REF_CNT_PTR
#define LLVM_ADT_INTRUSIVE_REF_CNT_PTR
#include <cassert>
#include "llvm/Support/Casting.h"
namespace llvm {
template <class T>
class IntrusiveRefCntPtr;
template <class Derived>
class RefCountedBase {
unsigned ref_cnt;
protected:
RefCountedBase() : ref_cnt(0) {}
void Retain() { ++ref_cnt; }
void Release() {
assert (ref_cnt > 0 && "Reference count is already zero.");
if (--ref_cnt == 0) delete static_cast<Derived*>(this);
}
friend class IntrusiveRefCntPtr<Derived>;
};
template <class Derived>
class RefCountedBaseVPTR {
unsigned ref_cnt;
protected:
RefCountedBaseVPTR() : ref_cnt(0) {}
virtual ~RefCountedBaseVPTR() {}
void Retain() { ++ref_cnt; }
void Release() {
assert (ref_cnt > 0 && "Reference count is already zero.");
if (--ref_cnt == 0) delete this;
}
friend class IntrusiveRefCntPtr<Derived>;
};
template <typename T>
class IntrusiveRefCntPtr {
T* Obj;
typedef IntrusiveRefCntPtr this_type;
public:
typedef T element_type;
explicit IntrusiveRefCntPtr() : Obj(0) {}
explicit IntrusiveRefCntPtr(T* obj) : Obj(obj) {
retain();
}
IntrusiveRefCntPtr(const IntrusiveRefCntPtr& S) : Obj(S.Obj) {
retain();
}
template <class X>
IntrusiveRefCntPtr(const IntrusiveRefCntPtr<X>& S)
: Obj(S.getPtr()) {
retain();
}
IntrusiveRefCntPtr& operator=(const IntrusiveRefCntPtr& S) {
replace(S.getPtr());
return *this;
}
template <class X>
IntrusiveRefCntPtr& operator=(const IntrusiveRefCntPtr<X>& S) {
replace(S.getPtr());
return *this;
}
IntrusiveRefCntPtr& operator=(T * S) {
replace(S);
return *this;
}
~IntrusiveRefCntPtr() { release(); }
T& operator*() const { return *Obj; }
T* operator->() const { return Obj; }
T* getPtr() const { return Obj; }
typedef T* (IntrusiveRefCntPtr::*unspecified_bool_type) () const;
operator unspecified_bool_type() const {
return Obj == 0 ? 0 : &IntrusiveRefCntPtr::getPtr;
}
void swap(IntrusiveRefCntPtr& other) {
T* tmp = other.Obj;
other.Obj = Obj;
Obj = tmp;
}
private:
void retain() { if (Obj) Obj->Retain(); }
void release() { if (Obj) Obj->Release(); }
void replace(T* S) {
this_type(S).swap(*this);
}
};
template<class T, class U>
inline bool operator==(const IntrusiveRefCntPtr<T>& A,
const IntrusiveRefCntPtr<U>& B)
{
return A.getPtr() == B.getPtr();
}
template<class T, class U>
inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
const IntrusiveRefCntPtr<U>& B)
{
return A.getPtr() != B.getPtr();
}
template<class T, class U>
inline bool operator==(const IntrusiveRefCntPtr<T>& A,
U* B)
{
return A.getPtr() == B;
}
template<class T, class U>
inline bool operator!=(const IntrusiveRefCntPtr<T>& A,
U* B)
{
return A.getPtr() != B;
}
template<class T, class U>
inline bool operator==(T* A,
const IntrusiveRefCntPtr<U>& B)
{
return A == B.getPtr();
}
template<class T, class U>
inline bool operator!=(T* A,
const IntrusiveRefCntPtr<U>& B)
{
return A != B.getPtr();
}
template<class T> struct simplify_type<IntrusiveRefCntPtr<T> > {
typedef T* SimpleType;
static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
return Val.getPtr();
}
};
template<class T> struct simplify_type<const IntrusiveRefCntPtr<T> > {
typedef T* SimpleType;
static SimpleType getSimplifiedValue(const IntrusiveRefCntPtr<T>& Val) {
return Val.getPtr();
}
};
}
#endif // LLVM_ADT_INTRUSIVE_REF_CNT_PTR