X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Frefptr.h;h=21da15df6a21d2581f640e88245c969da5eb0f80;hb=99b9121e2158603372c7313400283b622e6754d8;hp=8ab1b7cbfbdb6e752afd4b7634e807a8a836e10a;hpb=74e62aabadda67d02b3eab09baa37c9d7640b794;p=libs%2Fcore.git diff --git a/source/core/refptr.h b/source/core/refptr.h index 8ab1b7c..21da15d 100644 --- a/source/core/refptr.h +++ b/source/core/refptr.h @@ -11,10 +11,14 @@ struct RefCounts }; unsigned count; + unsigned weak_count; - RefCounts(): count(0) { } + RefCounts(): count(0), weak_count(0) { } }; +template +class WeakPtr; + /** A reference counting smart pointer. When the last RefPtr for the data gets @@ -24,13 +28,14 @@ template class RefPtr { template friend class RefPtr; + template friend class WeakPtr; private: - T *data; - RefCounts *counts; + T *data = 0; + RefCounts *counts = 0; public: - RefPtr(): data(0), counts(0) { } + RefPtr() = default; RefPtr(T *d): data(d), counts(data ? new RefCounts : 0) { incref(); } private: RefPtr(T *d, RefCounts *c): data(d), counts(d ? c : 0) { incref(); } @@ -43,16 +48,12 @@ 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 : 0) { incref(); } + ~RefPtr() { decref(); } - RefPtr &operator=(T *d) - { - decref(); - data = d; - counts = (d ? new RefCounts : 0); - incref(); - return *this; - } + RefPtr &operator=(T *); // Likewise for the assignment operator RefPtr &operator=(const RefPtr &p) { return assign(p); } @@ -60,37 +61,22 @@ 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 &p) - { - decref(); - data = p.data; - counts = p.counts; - incref(); - return *this; - } + RefPtr &assign(const RefPtr &); public: /** Makes the RefPtr release its reference of the data without deleting it. Note that if there are other RefPtrs left with the same data, it might still get deleted automatically. */ - T *release() - { - T *d = data; - data = 0; - decref(); - counts = 0; - return d; - } + T *release(); /** Marks the data to not be deleted. This affects all RefPtrs with the same data. */ - void keep() - { - if(counts) - counts->count |= RefCounts::KEEP; - } + void keep() { if(counts) counts->count |= RefCounts::KEEP; } T *get() const { return data; } T &operator*() const { return *data; } @@ -104,31 +90,130 @@ public: { return RefPtr(dynamic_cast(p.data), p.counts); } private: - void incref() + void incref() { if(counts) ++counts->count; } + void decref(); +}; + + +template +class WeakPtr +{ + template friend class RefPtr; + template friend class WeakPtr; + +private: + T *data = 0; + RefCounts *counts = 0; + +public: + WeakPtr() = default; +private: + WeakPtr(T *d, RefCounts *c): data(d), counts(d ? c : 0) { incref(); } + +public: + WeakPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : 0) { incref(); } + + template + WeakPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : 0) { 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 : 0); } + void incref() { if(counts) ++counts->weak_count; } + void decref(); +}; + + +template +RefPtr &RefPtr::operator=(T *d) +{ + decref(); + data = d; + counts = (d ? new RefCounts : 0); + incref(); + return *this; +} + +template +template +RefPtr &RefPtr::assign(const RefPtr &p) +{ + decref(); + data = p.data; + counts = p.counts; + incref(); + return *this; +} + +template +T *RefPtr::release() +{ + T *d = data; + data = 0; + decref(); + counts = 0; + return d; +} + +template +void RefPtr::decref() +{ + if(!counts) return; + --counts->count; + if(!counts->count) { - if(!counts) return; - ++counts->count; + delete data; + data = 0; } + else if(counts->count==RefCounts::KEEP) + data = 0; + else + return; - void decref() + if(!counts->weak_count) { - if(!counts) return; - --counts->count; - if(!counts->count) - { - delete data; - delete counts; - data = 0; - counts = 0; - } - else if(counts->count==RefCounts::KEEP) - { - delete counts; - data = 0; - counts = 0; - } + delete counts; + counts = 0; } -}; +} + + +template +template +WeakPtr &WeakPtr::assign(const WeakPtr &p) +{ + decref(); + data = p.get(); + counts = (data ? p.counts : 0); + 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; + } +} } // namespace Msp