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


Copyright © 2009 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.1.10