]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/refptr.h
Move RefPtr refcount into a struct
[libs/core.git] / source / core / refptr.h
index a44c13c84e00da087d35f8a6a7776b2e1281c1e2..8ab1b7cbfbdb6e752afd4b7634e807a8a836e10a 100644 (file)
@@ -1,14 +1,21 @@
-/* $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;
+
+       RefCounts(): count(0) { }
+};
+
+
 /**
 A reference counting smart pointer.  When the last RefPtr for the data gets
 destroyed, the data is deleted as well.
@@ -16,65 +23,109 @@ destroyed, the data is deleted as well.
 template<typename T>
 class RefPtr
 {
+       template<typename U> friend class RefPtr;
+
+private:
+       T *data;
+       RefCounts *counts;
+
+public:
+       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:
-       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)
+       /* 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<typename U>
+       RefPtr(const RefPtr<U> &p): data(p.data), counts(p.counts) { incref(); }
+
+       ~RefPtr() { decref(); }
+
+       RefPtr &operator=(T *d)
        {
                decref();
-               data=p.data;
-               count=p.count;
+               data = d;
+               counts = (d ? new RefCounts : 0);
                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.
-       */
+       // Likewise for the assignment operator
+       RefPtr &operator=(const RefPtr &p) { return assign(p); }
+
+       template<typename U>
+       RefPtr &operator=(const RefPtr<U> &p) { return assign(p); }
+
+private:
+       template<typename U>
+       RefPtr &assign(const RefPtr<U> &p)
+       {
+               decref();
+               data = p.data;
+               counts = p.counts;
+               incref();
+               return *this;
+       }
+
+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;
+               counts = 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(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; }
 
-       template<typename U>
-       static RefPtr<T> cast_dynamic(const RefPtr<U> &p)  { return RefPtr<T>(dynamic_cast<T *>(p.data), p.count); }
-       template<typename U> friend class RefPtr;
-private:
-       T        *data;
-       unsigned *count;
+       unsigned refcount() const { return (data ? counts->count : 0); }
 
-       RefPtr(T *d, unsigned *c): data(d), count(c) { incref(); }
+       template<typename U>
+       static RefPtr<T> cast_dynamic(const RefPtr<U> &p)
+       { return RefPtr<T>(dynamic_cast<T *>(p.data), p.counts); }
 
-       void  incref()
+private:
+       void incref()
        {
-               if(!count) return;
-               ++*count;
+               if(!counts) return;
+               ++counts->count;
        }
 
-       void  decref()
+       void decref()
        {
-               if(!count) return;
-               --*count;
-               if(!*count)
+               if(!counts) return;
+               --counts->count;
+               if(!counts->count)
                {
                        delete data;
-                       delete count;
-                       data=0;
-                       count=0;
+                       delete counts;
+                       data = 0;
+                       counts = 0;
+               }
+               else if(counts->count==RefCounts::KEEP)
+               {
+                       delete counts;
+                       data = 0;
+                       counts = 0;
                }
        }
 };