]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/refptr.h
Add move semantics to Variant
[libs/core.git] / source / core / refptr.h
index 8ab1b7cbfbdb6e752afd4b7634e807a8a836e10a..1a164c1a90eafe69d3eab755b971400ff1f3e275 100644 (file)
@@ -10,11 +10,13 @@ struct RefCounts
                KEEP = 1U<<(sizeof(unsigned)*8-1)
        };
 
-       unsigned count;
-
-       RefCounts(): count(0) { }
+       unsigned count = 0;
+       unsigned weak_count = 0;
 };
 
+template<typename T>
+class WeakPtr;
+
 
 /**
 A reference counting smart pointer.  When the last RefPtr for the data gets
@@ -24,16 +26,17 @@ template<typename T>
 class RefPtr
 {
        template<typename U> friend class RefPtr;
+       template<typename U> friend class WeakPtr;
 
 private:
-       T *data;
-       RefCounts *counts;
+       T *data = nullptr;
+       RefCounts *counts = nullptr;
 
 public:
-       RefPtr(): data(0), counts(0) { }
-       RefPtr(T *d): data(d), counts(data ? new RefCounts : 0) { incref(); }
+       RefPtr() = default;
+       RefPtr(T *d): data(d), counts(data ? new RefCounts : nullptr) { incref(); }
 private:
-       RefPtr(T *d, RefCounts *c): data(d), counts(d ? c : 0) { incref(); }
+       RefPtr(T *d, RefCounts *c): data(d), counts(d ? c : nullptr) { incref(); }
 
 public:
        /* Must have this or the compiler will generate a default copy-c'tor despite
@@ -43,16 +46,12 @@ public:
        template<typename U>
        RefPtr(const RefPtr<U> &p): data(p.data), counts(p.counts) { incref(); }
 
+       template<typename U>
+       RefPtr(const WeakPtr<U> &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); }
+
        ~RefPtr() { decref(); }
 
-       RefPtr &operator=(T *d)
-       {
-               decref();
-               data = d;
-               counts = (d ? new RefCounts : 0);
-               incref();
-               return *this;
-       }
+       RefPtr &operator=(T *);
 
        // Likewise for the assignment operator
        RefPtr &operator=(const RefPtr &p) { return assign(p); }
@@ -60,42 +59,27 @@ public:
        template<typename U>
        RefPtr &operator=(const RefPtr<U> &p) { return assign(p); }
 
+       template<typename U>
+       RefPtr &operator=(const WeakPtr<U> &p) { return assign(RefPtr(p)); }
+
 private:
        template<typename U>
-       RefPtr &assign(const RefPtr<U> &p)
-       {
-               decref();
-               data = p.data;
-               counts = p.counts;
-               incref();
-               return *this;
-       }
+       RefPtr &assign(const RefPtr<U> &);
 
 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();
-               counts = 0;
-               return d;
-       }
+       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;
-       }
+       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; }
+       explicit operator bool() const { return data; }
 
        unsigned refcount() const { return (data ? counts->count : 0); }
 
@@ -104,31 +88,136 @@ public:
        { return RefPtr<T>(dynamic_cast<T *>(p.data), p.counts); }
 
 private:
-       void incref()
+       void incref() { if(counts) ++counts->count; }
+       void decref();
+};
+
+
+template<typename T>
+class WeakPtr
+{
+       template<typename U> friend class RefPtr;
+       template<typename U> friend class WeakPtr;
+
+private:
+       T *data = nullptr;
+       RefCounts *counts = nullptr;
+
+public:
+       WeakPtr() = default;
+private:
+       WeakPtr(T *d, RefCounts *c): data(d), counts(d ? c : nullptr) { incref(); }
+
+public:
+       WeakPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); }
+
+       template<typename U>
+       WeakPtr(const WeakPtr<U> &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); }
+
+       template<typename U>
+       WeakPtr(const RefPtr<U> &p): data(p.data), counts(p.counts) { incref(); }
+
+       WeakPtr &operator=(const WeakPtr &p) { return assign(p); }
+
+       template<typename U>
+       WeakPtr &operator=(const WeakPtr<U> &p) { return assign(p); }
+
+       template<typename U>
+       WeakPtr &operator=(const RefPtr<U> &p) { return assign(WeakPtr(p)); }
+
+private:
+       template<typename U>
+       WeakPtr &assign(const WeakPtr<U> &);
+
+       T *get() const { return (counts && counts->count ? data : nullptr); }
+       void incref() { if(counts) ++counts->weak_count; }
+       void decref();
+};
+
+
+template<typename T>
+RefPtr<T> &RefPtr<T>::operator=(T *d)
+{
+       decref();
+       data = d;
+       counts = (d ? new RefCounts : nullptr);
+       incref();
+       return *this;
+}
+
+template<typename T>
+template<typename U>
+RefPtr<T> &RefPtr<T>::assign(const RefPtr<U> &p)
+{
+       if(static_cast<const void *>(&p)==this)
+               return *this;
+
+       decref();
+       data = p.data;
+       counts = p.counts;
+       incref();
+       return *this;
+}
+
+template<typename T>
+T *RefPtr<T>::release()
+{
+       T *d = data;
+       data = nullptr;
+       decref();
+       counts = nullptr;
+       return d;
+}
+
+template<typename T>
+void RefPtr<T>::decref()
+{
+       if(!counts) return;
+       --counts->count;
+       if(!counts->count)
        {
-               if(!counts) return;
-               ++counts->count;
+               delete data;
+               data = nullptr;
        }
+       else if(counts->count==RefCounts::KEEP)
+               data = nullptr;
+       else
+               return;
 
-       void decref()
+       if(!counts->weak_count)
        {
-               if(!counts) return;
-               --counts->count;
-               if(!counts->count)
-               {
-                       delete data;
-                       delete counts;
-                       data = 0;
-                       counts = 0;
-               }
-               else if(counts->count==RefCounts::KEEP)
-               {
-                       delete counts;
-                       data = 0;
-                       counts = 0;
-               }
+               delete counts;
+               counts = nullptr;
        }
-};
+}
+
+
+template<typename T>
+template<typename U>
+WeakPtr<T> &WeakPtr<T>::assign(const WeakPtr<U> &p)
+{
+       if(&p==this)
+               return *this;
+
+       decref();
+       data = p.get();
+       counts = (data ? p.counts : nullptr);
+       incref();
+       return *this;
+}
+
+template<typename T>
+void WeakPtr<T>::decref()
+{
+       if(!counts) return;
+       --counts->weak_count;
+       if(!counts->weak_count && (!counts->count || counts->count==RefCounts::KEEP))
+       {
+               delete counts;
+               data = nullptr;
+               counts = nullptr;
+       }
+}
 
 } // namespace Msp