//===----------------------------------------------------------------------===//
template <class Derived>
class RefCountedBase {
- unsigned ref_cnt;
+ mutable unsigned ref_cnt;
public:
RefCountedBase() : ref_cnt(0) {}
+ RefCountedBase(const RefCountedBase &) : ref_cnt(0) {}
- void Retain() { ++ref_cnt; }
- void Release() {
+ void Retain() const { ++ref_cnt; }
+ void Release() const {
assert (ref_cnt > 0 && "Reference count is already zero.");
- if (--ref_cnt == 0) delete static_cast<Derived*>(this);
+ if (--ref_cnt == 0) delete static_cast<const Derived*>(this);
}
};
/// attempting to do this will produce a compile error.
//===----------------------------------------------------------------------===//
class RefCountedBaseVPTR {
- unsigned ref_cnt;
+ mutable unsigned ref_cnt;
+ virtual void anchor();
protected:
RefCountedBaseVPTR() : ref_cnt(0) {}
+ RefCountedBaseVPTR(const RefCountedBaseVPTR &) : ref_cnt(0) {}
+
virtual ~RefCountedBaseVPTR() {}
- void Retain() { ++ref_cnt; }
- void Release() {
+ void Retain() const { ++ref_cnt; }
+ void Release() const {
assert (ref_cnt > 0 && "Reference count is already zero.");
if (--ref_cnt == 0) delete this;
}
template <typename T>
- friend class IntrusiveRefCntPtr;
+ friend struct IntrusiveRefCntPtrInfo;
};
+
+ template <typename T> struct IntrusiveRefCntPtrInfo {
+ static void retain(T *obj) { obj->Retain(); }
+ static void release(T *obj) { obj->Release(); }
+ };
+
//===----------------------------------------------------------------------===//
/// IntrusiveRefCntPtr - A template class that implements a "smart pointer"
/// that assumes the wrapped object has a reference count associated
explicit IntrusiveRefCntPtr() : Obj(0) {}
- explicit IntrusiveRefCntPtr(T* obj) : Obj(obj) {
+ IntrusiveRefCntPtr(T* obj) : Obj(obj) {
retain();
}
other.Obj = Obj;
Obj = tmp;
}
-
+
+ void reset() {
+ release();
+ Obj = 0;
+ }
+
void resetWithoutRelease() {
Obj = 0;
}
private:
- void retain() { if (Obj) Obj->Retain(); }
- void release() { if (Obj) Obj->Release(); }
+ void retain() { if (Obj) IntrusiveRefCntPtrInfo<T>::retain(Obj); }
+ void release() { if (Obj) IntrusiveRefCntPtrInfo<T>::release(Obj); }
void replace(T* S) {
this_type(S).swap(*this);