X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Frefptr.h;h=28666ea816380e0547205a1f6d83b14da0e13016;hp=20450f1b412462e60d10c92b0de7c72b858c6cca;hb=be8ea216d23bf36bdfb2d3e302638782575fc136;hpb=b56eb5ec1da675da0c66abc53c1e4f6c4e4cccbd diff --git a/source/core/refptr.h b/source/core/refptr.h index 20450f1..28666ea 100644 --- a/source/core/refptr.h +++ b/source/core/refptr.h @@ -1,15 +1,21 @@ -/* $Id$ - -This file is part of libmspcore -Copyright © 2006-2007, 2010-2011 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; + + RefCounts(): count(0) { } +}; + + /** A reference counting smart pointer. When the last RefPtr for the data gets destroyed, the data is deleted as well. @@ -20,37 +26,26 @@ class RefPtr template friend class RefPtr; private: - enum - { - KEEP = 1U<<(sizeof(unsigned)*8-1) - }; - T *data; - unsigned *count; + RefCounts *counts; public: - 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(); } private: - 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 */ - RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); } + RefPtr(const RefPtr &p): data(p.data), counts(p.counts) { incref(); } template - RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); } + RefPtr(const RefPtr &p): data(p.data), counts(p.counts) { incref(); } ~RefPtr() { decref(); } - RefPtr &operator=(T *d) - { - decref(); - data = d; - count = (d ? new unsigned(1) : 0); - return *this; - } + RefPtr &operator=(T *); // Likewise for the assignment operator RefPtr &operator=(const RefPtr &p) { return assign(p); } @@ -60,71 +55,85 @@ public: private: template - RefPtr &assign(const RefPtr &p) - { - decref(); - data = p.data; - count = p.count; - 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(); - count = 0; - return d; - } + T *release(); /** Marks the data to not be deleted. This affects all RefPtrs with the same data. */ - void keep() - { - if(count) - *count |= KEEP; - } + void keep() { if(counts) counts->count |= RefCounts::KEEP; } 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); } + { return RefPtr(dynamic_cast(p.data), p.counts); } private: - void incref() + void incref() { if(counts) ++counts->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(!count) return; - ++*count; + delete data; + delete counts; + data = 0; + counts = 0; } - - void decref() + else if(counts->count==RefCounts::KEEP) { - if(!count) return; - --*count; - if(!*count) - { - delete data; - delete count; - data = 0; - count = 0; - } - else if(*count==KEEP) - { - delete count; - data = 0; - count = 0; - } + delete counts; + data = 0; + counts = 0; } -}; +} } // namespace Msp