From 7c452691dffbc1f0a7d51a9e96d04f50e47fda3f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 6 Feb 2021 13:09:28 +0200 Subject: [PATCH] Implement weak pointers --- source/core/refptr.h | 86 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-) diff --git a/source/core/refptr.h b/source/core/refptr.h index 28666ea..a2160a5 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,6 +28,7 @@ template class RefPtr { template friend class RefPtr; + template friend class WeakPtr; private: T *data; @@ -43,6 +48,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 : 0) { incref(); } + ~RefPtr() { decref(); } RefPtr &operator=(T *); @@ -53,6 +61,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 &); @@ -84,6 +95,48 @@ private: }; +template +class WeakPtr +{ + template friend class RefPtr; + template friend class WeakPtr; + +private: + T *data; + RefCounts *counts; + +public: + WeakPtr(): data(0), counts(0) { } +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) { @@ -123,11 +176,38 @@ void RefPtr::decref() if(!counts->count) { delete data; - delete counts; data = 0; - counts = 0; } else if(counts->count==RefCounts::KEEP) + data = 0; + else + return; + + if(!counts->weak_count) + { + 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; -- 2.43.0