]> 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 6285d0ef4031e2e1bc29a1a0495b3e67bc5721dc..1a164c1a90eafe69d3eab755b971400ff1f3e275 100644 (file)
@@ -10,10 +10,8 @@ struct RefCounts
                KEEP = 1U<<(sizeof(unsigned)*8-1)
        };
 
-       unsigned count;
-       unsigned weak_count;
-
-       RefCounts(): count(0), weak_count(0) { }
+       unsigned count = 0;
+       unsigned weak_count = 0;
 };
 
 template<typename T>
@@ -31,14 +29,14 @@ class RefPtr
        template<typename U> friend class WeakPtr;
 
 private:
-       T *data = 0;
-       RefCounts *counts = 0;
+       T *data = nullptr;
+       RefCounts *counts = nullptr;
 
 public:
-       RefPtr() { }
-       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
@@ -49,7 +47,7 @@ public:
        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 : 0) { incref(); }
+       RefPtr(const WeakPtr<U> &p): data(p.get()), counts(data ? p.counts : nullptr) { incref(); }
 
        ~RefPtr() { decref(); }
 
@@ -81,7 +79,7 @@ public:
        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); }
 
@@ -102,19 +100,19 @@ class WeakPtr
        template<typename U> friend class WeakPtr;
 
 private:
-       T *data = 0;
-       RefCounts *counts = 0;
+       T *data = nullptr;
+       RefCounts *counts = nullptr;
 
 public:
-       WeakPtr() { }
+       WeakPtr() = default;
 private:
-       WeakPtr(T *d, RefCounts *c): data(d), counts(d ? c : 0) { incref(); }
+       WeakPtr(T *d, RefCounts *c): data(d), counts(d ? c : nullptr) { incref(); }
 
 public:
-       WeakPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : 0) { incref(); }
+       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 : 0) { incref(); }
+       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(); }
@@ -131,7 +129,7 @@ private:
        template<typename U>
        WeakPtr &assign(const WeakPtr<U> &);
 
-       T *get() const { return (counts && counts->count ? data : 0); }
+       T *get() const { return (counts && counts->count ? data : nullptr); }
        void incref() { if(counts) ++counts->weak_count; }
        void decref();
 };
@@ -142,7 +140,7 @@ RefPtr<T> &RefPtr<T>::operator=(T *d)
 {
        decref();
        data = d;
-       counts = (d ? new RefCounts : 0);
+       counts = (d ? new RefCounts : nullptr);
        incref();
        return *this;
 }
@@ -151,6 +149,9 @@ 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;
@@ -162,9 +163,9 @@ template<typename T>
 T *RefPtr<T>::release()
 {
        T *d = data;
-       data = 0;
+       data = nullptr;
        decref();
-       counts = 0;
+       counts = nullptr;
        return d;
 }
 
@@ -176,17 +177,17 @@ void RefPtr<T>::decref()
        if(!counts->count)
        {
                delete data;
-               data = 0;
+               data = nullptr;
        }
        else if(counts->count==RefCounts::KEEP)
-               data = 0;
+               data = nullptr;
        else
                return;
 
        if(!counts->weak_count)
        {
                delete counts;
-               counts = 0;
+               counts = nullptr;
        }
 }
 
@@ -195,9 +196,12 @@ 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 : 0);
+       counts = (data ? p.counts : nullptr);
        incref();
        return *this;
 }
@@ -210,8 +214,8 @@ void WeakPtr<T>::decref()
        if(!counts->weak_count && (!counts->count || counts->count==RefCounts::KEEP))
        {
                delete counts;
-               data = 0;
-               counts = 0;
+               data = nullptr;
+               counts = nullptr;
        }
 }