| Home · All Classes · All Namespaces · Modules · Functions · Files

# shared-ptr.h

    00001 /*
    00002  * This file is part of TelepathyQt4
    00003  *
    00004  * Copyright (C) 2009 Collabora Ltd. <http://www.collabora.co.uk/>
    00005  * Copyright (C) 2009 Nokia Corporation
    00006  *
    00007  * This library is free software; you can redistribute it and/or
    00008  * modify it under the terms of the GNU Lesser General Public
    00009  * License as published by the Free Software Foundation; either
    00010  * version 2.1 of the License, or (at your option) any later version.
    00011  *
    00012  * This library is distributed in the hope that it will be useful,
    00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00015  * Lesser General Public License for more details.
    00016  *
    00017  * You should have received a copy of the GNU Lesser General Public
    00018  * License along with this library; if not, write to the Free Software
    00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    00020  */
    00021
    00022 #ifndef _TelepathyQt4_shared_ptr_h_HEADER_GUARD_
    00023 #define _TelepathyQt4_shared_ptr_h_HEADER_GUARD_
    00024
    00025 #ifndef IN_TELEPATHY_QT4_HEADER
    00026 #error IN_TELEPATHY_QT4_HEADER
    00027 #endif
    00028
    00029 #include <TelepathyQt4/Global>
    00030
    00031 #include <QObject>
    00032
    00033 namespace Tp
    00034 {
    00035
    00036 class RefCounted;
    00037 class WeakData;
    00038 template <class T> class SharedPtr;
    00039 template <class T> class WeakPtr;
    00040
    00041 class TELEPATHY_QT4_EXPORT WeakData
    00042 {
    00043     Q_DISABLE_COPY(WeakData)
    00044
    00045 public:
    00046     WeakData(RefCounted *d) : d(d), weakref(0) { }
    00047
    00048     RefCounted *d;
    00049     mutable QAtomicInt weakref;
    00050 };
    00051
    00052 class TELEPATHY_QT4_EXPORT RefCounted
    00053 {
    00054     Q_DISABLE_COPY(RefCounted)
    00055
    00056 public:
    00057     inline RefCounted() : strongref(0), wd(0) { }
    00058     inline virtual ~RefCounted() { if (wd) { wd->d = 0; } }
    00059
    00060     // So why were these not const in the first place although strongref was?
    00061     // API/ABI break TODO: remove, just leave the const variants in
    00062     inline void ref() { strongref.ref(); }
    00063     inline bool deref() { return strongref.deref(); }
    00064
    00065     inline void ref() const { strongref.ref(); }
    00066     inline bool deref() const { return strongref.deref(); }
    00067
    00068     mutable QAtomicInt strongref;
    00069     WeakData *wd;
    00070 };
    00071
    00072 template <class T>
    00073 class SharedPtr
    00074 {
    00075 public:
    00076     inline SharedPtr() : d(0) { }
    00077     explicit inline SharedPtr(T *d) : d(d) { if (d) { d->ref(); } }
    00078     template <typename Subclass>
    00079         inline SharedPtr(const SharedPtr<Subclass> &o) : d(o.data()) { if (d) { d->ref(); } }
    00080     inline SharedPtr(const SharedPtr<T> &o) : d(o.d) { if (d) { d->ref(); } }
    00081     explicit inline SharedPtr(const WeakPtr<T> &o)
    00082     {
    00083         if (o.wd && o.wd->d && o.wd->d->strongref > 0) {
    00084             d = static_cast<T*>(o.wd->d);
    00085             d->ref();
    00086         } else {
    00087             d = 0;
    00088         }
    00089     }
    00090     inline ~SharedPtr()
    00091     {
    00092         if (d && !d->deref()) {
    00093             delete d;
    00094         }
    00095     }
    00096
    00097     inline void reset()
    00098     {
    00099         SharedPtr<T>().swap(*this);
    00100     }
    00101
    00102     inline T *data() const { return d; }
    00103     inline const T *constData() const { return d; }
    00104     inline T *operator->() { return d; }
    00105     inline T *operator->() const { return d; }
    00106
    00107     inline bool isNull() const { return !d; }
    00108     inline operator bool() const { return !isNull(); }
    00109     inline bool operator!() const { return isNull(); }
    00110
    00111     inline bool operator==(const SharedPtr<T> &o) const { return d == o.d; }
    00112     inline bool operator!=(const SharedPtr<T> &o) const { return d != o.d; }
    00113     inline bool operator==(const T *ptr) const { return d == ptr; }
    00114     inline bool operator!=(const T *ptr) const { return d != ptr; }
    00115
    00116     inline SharedPtr<T> &operator=(const SharedPtr<T> &o)
    00117     {
    00118         SharedPtr<T>(o).swap(*this);
    00119         return *this;
    00120     }
    00121
    00122     inline void swap(SharedPtr<T> &o)
    00123     {
    00124         T *tmp = d;
    00125         d = o.d;
    00126         o.d = tmp;
    00127     }
    00128
    00129     template <class X>
    00130     static inline SharedPtr<T> staticCast(const SharedPtr<X> &src)
    00131     {
    00132         return SharedPtr<T>(static_cast<T*>(src.data()));
    00133     }
    00134
    00135     template <class X>
    00136     static inline SharedPtr<T> dynamicCast(const SharedPtr<X> &src)
    00137     {
    00138         return SharedPtr<T>(dynamic_cast<T*>(src.data()));
    00139     }
    00140
    00141     template <class X>
    00142     static inline SharedPtr<T> constCast(const SharedPtr<X> &src)
    00143     {
    00144         return SharedPtr<T>(const_cast<T*>(src.data()));
    00145     }
    00146
    00147     template <class X>
    00148     static inline SharedPtr<T> qObjectCast(const SharedPtr<X> &src)
    00149     {
    00150         return SharedPtr<T>(qobject_cast<T*>(src.data()));
    00151     }
    00152
    00153 private:
    00154     friend class WeakPtr<T>;
    00155
    00156     T *d;
    00157 };
    00158
    00159 template <class T>
    00160 class WeakPtr
    00161 {
    00162 public:
    00163     inline WeakPtr() : wd(0) { }
    00164     inline WeakPtr(const WeakPtr<T> &o) : wd(o.wd) { if (wd) { wd->weakref.ref(); } }
    00165     inline WeakPtr(const SharedPtr<T> &o)
    00166     {
    00167         if (o.d) {
    00168             if (!o.d->wd) {
    00169                 o.d->wd = new WeakData(o.d);
    00170             }
    00171             wd = o.d->wd;
    00172             wd->weakref.ref();
    00173         } else {
    00174             wd = 0;
    00175         }
    00176     }
    00177     inline ~WeakPtr()
    00178     {
    00179         if (wd && !wd->weakref.deref()) {
    00180             if (wd->d) {
    00181                 wd->d->wd = 0;
    00182             }
    00183             delete wd;
    00184         }
    00185     }
    00186
    00187     inline bool isNull() const { return !wd || !wd->d || wd->d->strongref == 0; }
    00188     inline operator bool() const { return !isNull(); }
    00189     inline bool operator!() const { return isNull(); }
    00190
    00191     inline WeakPtr<T> &operator=(const WeakPtr<T> &o)
    00192     {
    00193         WeakPtr<T>(o).swap(*this);
    00194         return *this;
    00195     }
    00196
    00197     inline WeakPtr<T> &operator=(const SharedPtr<T> &o)
    00198     {
    00199         WeakPtr<T>(o).swap(*this);
    00200         return *this;
    00201     }
    00202
    00203     inline void swap(WeakPtr<T> &o)
    00204     {
    00205         WeakData *tmp = wd;
    00206         wd = o.wd;
    00207         o.wd = tmp;
    00208     }
    00209
    00210     SharedPtr<T> toStrongRef() const { return SharedPtr<T>(*this); }
    00211
    00212 private:
    00213     friend class SharedPtr<T>;
    00214
    00215     WeakData *wd;
    00216 };
    00217
    00218 } // Tp
    00219
    00220 #endif