X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Frefptr.h;h=79401dce5be40094a9f55212c0c3fb4abd8f7dc9;hp=28666ea816380e0547205a1f6d83b14da0e13016;hb=HEAD;hpb=be8ea216d23bf36bdfb2d3e302638782575fc136 diff --git a/source/core/refptr.h b/source/core/refptr.h index 28666ea..1a164c1 100644 --- a/source/core/refptr.h +++ b/source/core/refptr.h @@ -10,11 +10,13 @@ struct RefCounts KEEP = 1U<<(sizeof(unsigned)*8-1) }; - unsigned count; - - RefCounts(): count(0) { } + unsigned count = 0; + unsigned weak_count = 0; }; +template +class WeakPtr; + /** A reference counting smart pointer. When the last RefPtr for the data gets @@ -24,16 +26,17 @@ template class RefPtr { template friend class RefPtr; + template friend class WeakPtr; private: - T *data; - RefCounts *counts; + T *data = nullptr; + RefCounts *counts = nullptr; public: - RefPtr(): data(0), counts(0) { } - RefPtr(T *d): data(d), counts(data ? new RefCounts : 0) { incref(); } + RefPtr() = default; + RefPtr(T *d): data(d), counts(data ? new RefCounts : nullptr) { incref(); } private: - RefPtr(T *d, RefCounts *c): data(d), counts(d ? c : 0) { incref(); } + RefPtr(T *d, RefCounts *c): data(d), counts(d ? c : nullptr) { incref(); } public: /* Must have this or the compiler will generate a default copy-c'tor despite @@ -43,6 +46,9 @@ public: template RefPtr(const RefPtr &p): data(p.data), counts(p.counts) { incref(); } + template + RefPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); } + ~RefPtr() { decref(); } RefPtr &operator=(T *); @@ -53,6 +59,9 @@ public: template RefPtr &operator=(const RefPtr &p) { return assign(p); } + template + RefPtr &operator=(const WeakPtr &p) { return assign(RefPtr(p)); } + private: template RefPtr &assign(const RefPtr &); @@ -70,7 +79,7 @@ public: T *get() const { return data; } T &operator*() const { return *data; } T *operator->() const { return data; } - operator bool() const { return data!=0; } + explicit operator bool() const { return data; } unsigned refcount() const { return (data ? counts->count : 0); } @@ -84,12 +93,54 @@ private: }; +template +class WeakPtr +{ + template friend class RefPtr; + template friend class WeakPtr; + +private: + T *data = nullptr; + RefCounts *counts = nullptr; + +public: + WeakPtr() = default; +private: + WeakPtr(T *d, RefCounts *c): data(d), counts(d ? c : nullptr) { incref(); } + +public: + WeakPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); } + + template + WeakPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); } + + template + WeakPtr(const RefPtr &p): data(p.data), counts(p.counts) { incref(); } + + WeakPtr &operator=(const WeakPtr &p) { return assign(p); } + + template + WeakPtr &operator=(const WeakPtr &p) { return assign(p); } + + template + WeakPtr &operator=(const RefPtr &p) { return assign(WeakPtr(p)); } + +private: + template + WeakPtr &assign(const WeakPtr &); + + T *get() const { return (counts && counts->count ? data : nullptr); } + void incref() { if(counts) ++counts->weak_count; } + void decref(); +}; + + template RefPtr &RefPtr::operator=(T *d) { decref(); data = d; - counts = (d ? new RefCounts : 0); + counts = (d ? new RefCounts : nullptr); incref(); return *this; } @@ -98,6 +149,9 @@ template template RefPtr &RefPtr::assign(const RefPtr &p) { + if(static_cast(&p)==this) + return *this; + decref(); data = p.data; counts = p.counts; @@ -109,9 +163,9 @@ template T *RefPtr::release() { T *d = data; - data = 0; + data = nullptr; decref(); - counts = 0; + counts = nullptr; return d; } @@ -123,15 +177,45 @@ void RefPtr::decref() if(!counts->count) { delete data; - delete counts; - data = 0; - counts = 0; + data = nullptr; } else if(counts->count==RefCounts::KEEP) + data = nullptr; + else + return; + + if(!counts->weak_count) + { + delete counts; + counts = nullptr; + } +} + + +template +template +WeakPtr &WeakPtr::assign(const WeakPtr &p) +{ + if(&p==this) + return *this; + + decref(); + data = p.get(); + counts = (data ? p.counts : nullptr); + incref(); + return *this; +} + +template +void WeakPtr::decref() +{ + if(!counts) return; + --counts->weak_count; + if(!counts->weak_count && (!counts->count || counts->count==RefCounts::KEEP)) { delete counts; - data = 0; - counts = 0; + data = nullptr; + counts = nullptr; } }