This is necessary to implement weak pointers
+struct RefCounts
+{
+ enum
+ {
+ KEEP = 1U<<(sizeof(unsigned)*8-1)
+ };
+
+ unsigned count;
+
+ RefCounts(): count(0) { }
+};
+
+
/**
A reference counting smart pointer. When the last RefPtr for the data gets
destroyed, the data is deleted as well.
/**
A reference counting smart pointer. When the last RefPtr for the data gets
destroyed, the data is deleted as well.
template<typename U> friend class RefPtr;
private:
template<typename U> friend class RefPtr;
private:
- enum
- {
- KEEP = 1U<<(sizeof(unsigned)*8-1)
- };
-
- RefPtr(): data(0), count(0) { }
- RefPtr(T *d): data(d), count(data ? new unsigned(1) : 0) { }
+ RefPtr(): data(0), counts(0) { }
+ RefPtr(T *d): data(d), counts(data ? new RefCounts : 0) { incref(); }
- RefPtr(T *d, unsigned *c): data(d), count(d ? c : 0) { incref(); }
+ RefPtr(T *d, RefCounts *c): data(d), counts(d ? c : 0) { incref(); }
public:
/* Must have this or the compiler will generate a default copy-c'tor despite
the template version */
public:
/* Must have this or the compiler will generate a default copy-c'tor despite
the template version */
- RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); }
+ RefPtr(const RefPtr &p): data(p.data), counts(p.counts) { incref(); }
- RefPtr(const RefPtr<U> &p): data(p.data), count(p.count) { incref(); }
+ RefPtr(const RefPtr<U> &p): data(p.data), counts(p.counts) { incref(); }
- count = (d ? new unsigned(1) : 0);
+ counts = (d ? new RefCounts : 0);
+ incref();
{
decref();
data = p.data;
{
decref();
data = p.data;
incref();
return *this;
}
incref();
return *this;
}
T *d = data;
data = 0;
decref();
T *d = data;
data = 0;
decref();
same data. */
void keep()
{
same data. */
void keep()
{
- if(count)
- *count |= KEEP;
+ if(counts)
+ counts->count |= RefCounts::KEEP;
}
T *get() const { return data; }
}
T *get() const { return data; }
T *operator->() const { return data; }
operator bool() const { return data!=0; }
T *operator->() const { return data; }
operator bool() const { return data!=0; }
- unsigned refcount() const { return (data ? *count : 0); }
+ unsigned refcount() const { return (data ? counts->count : 0); }
template<typename U>
static RefPtr<T> cast_dynamic(const RefPtr<U> &p)
template<typename U>
static RefPtr<T> cast_dynamic(const RefPtr<U> &p)
- { return RefPtr<T>(dynamic_cast<T *>(p.data), p.count); }
+ { return RefPtr<T>(dynamic_cast<T *>(p.data), p.counts); }
- if(!count) return;
- ++*count;
+ if(!counts) return;
+ ++counts->count;
- if(!count) return;
- --*count;
- if(!*count)
+ if(!counts) return;
+ --counts->count;
+ if(!counts->count)
+ else if(counts->count==RefCounts::KEEP)