| 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     inline void ref() { strongref.ref(); }
    00061     inline bool deref() { return strongref.deref(); }
    00062
    00063     mutable QAtomicInt strongref;
    00064     WeakData *wd;
    00065 };
    00066
    00067 template <class T>
    00068 class SharedPtr
    00069 {
    00070 public:
    00071     inline SharedPtr() : d(0) { }
    00072     explicit inline SharedPtr(T *d) : d(d) { if (d) { d->ref(); } }
    00073     inline SharedPtr(const SharedPtr<T> &o) : d(o.d) { if (d) { d->ref(); } }
    00074     explicit inline SharedPtr(const WeakPtr<T> &o)
    00075     {
    00076         if (o.wd && o.wd->d) {
    00077             d = static_cast<T*>(o.wd->d);
    00078             d->ref();
    00079         } else {
    00080             d = 0;
    00081         }
    00082     }
    00083     inline ~SharedPtr()
    00084     {
    00085         if (d && !d->deref()) {
    00086             delete d;
    00087         }
    00088     }
    00089
    00090     inline void reset()
    00091     {
    00092         SharedPtr<T>().swap(*this);
    00093     }
    00094
    00095     inline T *data() const { return d; }
    00096     inline const T *constData() const { return d; }
    00097     inline T *operator->() { return d; }
    00098     inline T *operator->() const { return d; }
    00099
    00100     inline bool isNull() const { return !d; }
    00101     inline operator bool() const { return !isNull(); }
    00102     inline bool operator!() const { return isNull(); }
    00103
    00104     inline bool operator==(const SharedPtr<T> &o) const { return d == o.d; }
    00105     inline bool operator!=(const SharedPtr<T> &o) const { return d != o.d; }
    00106     inline bool operator==(const T *ptr) const { return d == ptr; }
    00107     inline bool operator!=(const T *ptr) const { return d != ptr; }
    00108
    00109     inline SharedPtr<T> &operator=(const SharedPtr<T> &o)
    00110     {
    00111         SharedPtr<T>(o).swap(*this);
    00112         return *this;
    00113     }
    00114
    00115     inline void swap(SharedPtr<T> &o)
    00116     {
    00117         T *tmp = d;
    00118         d = o.d;
    00119         o.d = tmp;
    00120     }
    00121
    00122     template <class X>
    00123     static inline SharedPtr<T> staticCast(const SharedPtr<X> &src)
    00124     {
    00125         return SharedPtr<T>(static_cast<T*>(src.data()));
    00126     }
    00127
    00128     template <class X>
    00129     static inline SharedPtr<T> dynamicCast(const SharedPtr<X> &src)
    00130     {
    00131         return SharedPtr<T>(dynamic_cast<T*>(src.data()));
    00132     }
    00133
    00134     template <class X>
    00135     static inline SharedPtr<T> constCast(const SharedPtr<X> &src)
    00136     {
    00137         return SharedPtr<T>(const_cast<T*>(src.data()));
    00138     }
    00139
    00140 private:
    00141     friend class WeakPtr<T>;
    00142
    00143     T *d;
    00144 };
    00145
    00146 template <class T>
    00147 class WeakPtr
    00148 {
    00149 public:
    00150     inline WeakPtr() : wd(0) { }
    00151     inline WeakPtr(const WeakPtr<T> &o) : wd(o.wd) { if (wd) { wd->weakref.ref(); } }
    00152     inline WeakPtr(const SharedPtr<T> &o)
    00153     {
    00154         if (o.d) {
    00155             if (!o.d->wd) {
    00156                 o.d->wd = new WeakData(o.d);
    00157             }
    00158             wd = o.d->wd;
    00159             wd->weakref.ref();
    00160         } else {
    00161             wd = 0;
    00162         }
    00163     }
    00164     inline ~WeakPtr()
    00165     {
    00166         if (wd && !wd->weakref.deref()) {
    00167             if (wd->d) {
    00168                 wd->d->wd = 0;
    00169             }
    00170             delete wd;
    00171         }
    00172     }
    00173
    00174     inline bool isNull() const { return !wd || !wd->d || wd->d->strongref == 0; }
    00175     inline operator bool() const { return !isNull(); }
    00176     inline bool operator!() const { return isNull(); }
    00177
    00178     inline WeakPtr<T> &operator=(const WeakPtr<T> &o)
    00179     {
    00180         WeakPtr<T>(o).swap(*this);
    00181         return *this;
    00182     }
    00183
    00184     inline WeakPtr<T> &operator=(const SharedPtr<T> &o)
    00185     {
    00186         WeakPtr<T>(o).swap(*this);
    00187         return *this;
    00188     }
    00189
    00190     inline void swap(WeakPtr<T> &o)
    00191     {
    00192         WeakData *tmp = wd;
    00193         wd = o.wd;
    00194         o.wd = tmp;
    00195     }
    00196
    00197     SharedPtr<T> toStrongRef() const { return SharedPtr<T>(*this); }
    00198
    00199 private:
    00200     friend class SharedPtr<T>;
    00201
    00202     WeakData *wd;
    00203 };
    00204
    00205 } // Tp
    00206
    00207 #endif