Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
73 lines (60 sloc) 1.3 KB
#pragma once
namespace clay {
template<class T>
class Pointer {
T *p;
public :
Pointer()
: p(0) {}
Pointer(T *p)
: p(p) {
if (p)
p->incRef();
}
Pointer(const Pointer<T> &other)
: p(other.p) {
if (p)
p->incRef();
}
~Pointer() {
if (p)
p->decRef();
}
Pointer<T> &operator=(const Pointer<T> &other) {
T *q = other.p;
if (q) q->incRef();
if (p) p->decRef();
p = q;
return *this;
}
T &operator*() const { return *p; }
T *operator->() const { return p; }
T *ptr() const { return p; }
bool operator!() const { return p == 0; }
bool operator==(const Pointer<T> &other) const {
return p == other.p;
}
bool operator!=(const Pointer<T> &other) const {
return p != other.p;
}
bool operator<(const Pointer<T> &other) const {
return p < other.p;
}
};
struct RefCounted {
private:
int refCount;
public:
RefCounted()
: refCount(0) {}
RefCounted(const RefCounted& that): refCount(0) {}
void incRef() { ++refCount; }
void decRef() {
if (--refCount == 0) {
delete this;
}
}
int getRefCount() { return refCount; }
virtual ~RefCounted() {}
};
}