X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Frefptr.h;h=a2160a52c50b1799ce45d87903de94ff2474eaf5;hp=a44c13c84e00da087d35f8a6a7776b2e1281c1e2;hb=7c452691dffbc1f0a7d51a9e96d04f50e47fda3f;hpb=d61b4678e4e4a2a3661a2472dc7349cc9fd5eaad diff --git a/source/core/refptr.h b/source/core/refptr.h index a44c13c..a2160a5 100644 --- a/source/core/refptr.h +++ b/source/core/refptr.h @@ -1,14 +1,25 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ #ifndef MSP_CORE_REFPTR_H_ #define MSP_CORE_REFPTR_H_ namespace Msp { +struct RefCounts +{ + enum + { + KEEP = 1U<<(sizeof(unsigned)*8-1) + }; + + unsigned count; + unsigned weak_count; + + RefCounts(): count(0), weak_count(0) { } +}; + +template +class WeakPtr; + + /** A reference counting smart pointer. When the last RefPtr for the data gets destroyed, the data is deleted as well. @@ -16,68 +27,193 @@ destroyed, the data is deleted as well. template class RefPtr { + template friend class RefPtr; + template friend class WeakPtr; + +private: + T *data; + RefCounts *counts; + public: - RefPtr(): data(0), count(0) { } - RefPtr(T *d): data(d), count(0) { if(data) count=new unsigned(1); } - RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); } - RefPtr &operator=(const RefPtr &p) - { - decref(); - data=p.data; - count=p.count; - incref(); - return *this; - } - + RefPtr(): data(0), counts(0) { } + RefPtr(T *d): data(d), counts(data ? new RefCounts : 0) { incref(); } +private: + 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 */ + RefPtr(const RefPtr &p): data(p.data), counts(p.counts) { incref(); } + + 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(); } - /** - Makes the RefPtr release its reference of the data. 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(); - count=0; - return d; - } + RefPtr &operator=(T *); + + // Likewise for the assignment operator + RefPtr &operator=(const RefPtr &p) { return assign(p); } + + 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 &); + +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(); + + /** Marks the data to not be deleted. This affects all RefPtrs with the + same data. */ + void keep() { if(counts) counts->count |= RefCounts::KEEP; } - T *get() const { return data; } - T &operator*() const { return *data; } + T *get() const { return data; } + T &operator*() const { return *data; } T *operator->() const { return data; } operator bool() const { return data!=0; } + unsigned refcount() const { return (data ? counts->count : 0); } + template - static RefPtr cast_dynamic(const RefPtr &p) { return RefPtr(dynamic_cast(p.data), p.count); } + static RefPtr cast_dynamic(const RefPtr &p) + { return RefPtr(dynamic_cast(p.data), p.counts); } + +private: + void incref() { if(counts) ++counts->count; } + void decref(); +}; + + +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: - T *data; - unsigned *count; + template + WeakPtr &assign(const WeakPtr &); + + T *get() const { return (counts && counts->count ? data : 0); } + void incref() { if(counts) ++counts->weak_count; } + void decref(); +}; - RefPtr(T *d, unsigned *c): data(d), count(c) { incref(); } - void incref() +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(!count) return; - ++*count; + delete data; + data = 0; } + else if(counts->count==RefCounts::KEEP) + data = 0; + else + return; - void decref() + if(!counts->weak_count) { - if(!count) return; - --*count; - if(!*count) - { - delete data; - delete count; - data=0; - count=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