1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#ifndef __REF_H
#define __REF_H
class refcounted {
public:
int __refcount;
protected:
virtual ~refcounted() {}
public:
refcounted(): __refcount(0) {};
void __destroy();
};
class null_ref;
template<class T> class ref {
private:
T *ptr;
public:
#define null (*(class null_ref *)0)
void __ref() {
ptr->__refcount++;
}
void __unref() {
if (--ptr->__refcount == 0) {
ptr->__destroy();
}
}
ref(): ptr(0) {}
ref(class null_ref &): ptr(0) {}
explicit ref(T *r) : ptr(r) { if (ptr) __ref(); }
ref(const ref<T> &p): ptr(p.ptr) { if (ptr) __ref(); }
template<class T2>
ref(const ref<T2> &p): ptr(static_cast<T *>(p._ptr())) { if (ptr) __ref(); }
~ref() { if (ptr) { __unref(); ptr = 0; } }
const T& operator*() const { return *ptr; }
T& operator*() { return *ptr; }
const T* operator->() const { return ptr; }
T *operator->() { return ptr; }
ref<T>& operator=(const ref<T>& rv) {
if (this != &rv) {
if (ptr) __unref();
ptr = rv.ptr;
if (ptr) __ref();
}
return *this;
}
template<class T2>
ref<T>& operator=(const ref<T2>& rv) {
if (this->ptr != rv._ptr()) {
if (ptr) __unref();
ptr = (T *)rv._ptr();
if (ptr) __ref();
}
return *this;
}
ref<T>& init(T *rv) {
if (this->ptr != rv) {
if (ptr) __unref();
ptr = rv;
if (ptr) __ref();
}
return *this;
}
bool operator==(const ref<T> &r) const { return ptr == r.ptr; }
bool operator!=(const ref<T> &r) const { return ptr != r.ptr; }
bool operator==(const class null_ref &) const { return ptr == 0; }
bool operator!=(const class null_ref &) const { return ptr != 0; }
ref<T>& operator=(const class null_ref &) {
if (ptr)
__unref();
ptr = 0;
return *this;
}
T *_ptr() const { return ptr; }
};
#endif
|