X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Frefptr.h;h=d245b34a7e90bab8fb14f5e26cc691a1385f761c;hp=a44c13c84e00da087d35f8a6a7776b2e1281c1e2;hb=967785734be5c3fc6f75da122c2d93ebbb338271;hpb=d61b4678e4e4a2a3661a2472dc7349cc9fd5eaad diff --git a/source/core/refptr.h b/source/core/refptr.h index a44c13c..d245b34 100644 --- a/source/core/refptr.h +++ b/source/core/refptr.h @@ -1,9 +1,3 @@ -/* $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_ @@ -16,56 +10,96 @@ destroyed, the data is deleted as well. template class RefPtr { + template friend class RefPtr; + +private: + enum + { + KEEP = 1U<<(sizeof(unsigned)*8-1) + }; + + T *data; + unsigned *count; + public: RefPtr(): data(0), count(0) { } - RefPtr(T *d): data(d), count(0) { if(data) count=new unsigned(1); } + RefPtr(T *d): data(d), count(data ? new unsigned(1) : 0) { } +private: + RefPtr(T *d, unsigned *c): data(d), count(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 &operator=(const RefPtr &p) + + template + RefPtr(const RefPtr &p): data(p.data), count(p.count) { incref(); } + + ~RefPtr() { decref(); } + + RefPtr &operator=(T *d) + { + decref(); + data = d; + count = (d ? new unsigned(1) : 0); + return *this; + } + + // Likewise for the assignment operator + RefPtr &operator=(const RefPtr &p) { return assign(p); } + + template + RefPtr &operator=(const RefPtr &p) { return assign(p); } + +private: + template + RefPtr &assign(const RefPtr &p) { decref(); - data=p.data; - count=p.count; + data = p.data; + count = p.count; incref(); return *this; } - - ~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. - */ +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; + T *d = data; + data = 0; decref(); - count=0; + count = 0; return d; } - T *get() const { return data; } - T &operator*() const { return *data; } + /** Marks the data to not be deleted. This affects all RefPtrs with the + same data. */ + void keep() + { + if(count) + *count |= KEEP; + } + + T *get() const { return data; } + T &operator*() const { return *data; } T *operator->() const { return data; } operator bool() const { return data!=0; } template - static RefPtr cast_dynamic(const RefPtr &p) { return RefPtr(dynamic_cast(p.data), p.count); } - template friend class RefPtr; -private: - T *data; - unsigned *count; + static RefPtr cast_dynamic(const RefPtr &p) + { return RefPtr(dynamic_cast(p.data), p.count); } - RefPtr(T *d, unsigned *c): data(d), count(c) { incref(); } - - void incref() +private: + void incref() { if(!count) return; ++*count; } - void decref() + void decref() { if(!count) return; --*count; @@ -73,8 +107,14 @@ private: { delete data; delete count; - data=0; - count=0; + data = 0; + count = 0; + } + else if(*count==KEEP) + { + delete count; + data = 0; + count = 0; } } };