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 <QHash>
00032 #include <QWeakPointer>
00033 
00034 namespace Tp
00035 {
00036 
00037 class RefCounted;
00038 template <class T> class SharedPtr;
00039 
00040 class TELEPATHY_QT4_EXPORT RefCounted
00041 {
00042     Q_DISABLE_COPY(RefCounted)
00043 
00044 public:
00045     inline RefCounted() : strongref(0) { }
00046     inline virtual ~RefCounted() { }
00047 
00048     inline void ref() const { strongref.ref(); }
00049     inline bool deref() const { return strongref.deref(); }
00050 
00051     mutable QAtomicInt strongref;
00052 };
00053 
00054 template <class T>
00055 class SharedPtr
00056 {
00057     typedef bool (SharedPtr<T>::*UnspecifiedBoolType)() const;
00058 
00059 public:
00060     inline SharedPtr() : d(0) { }
00061     explicit inline SharedPtr(T *d) : d(d) { if (d) { d->ref(); } }
00062     template <typename Subclass>
00063         inline SharedPtr(const SharedPtr<Subclass> &o) : d(o.data()) { if (d) { d->ref(); } }
00064     inline SharedPtr(const SharedPtr<T> &o) : d(o.d) { if (d) { d->ref(); } }
00065     explicit inline SharedPtr(const QWeakPointer<T> &o)
00066     {
00067         if (o.data() && o.data()->strongref > 0) {
00068             d = static_cast<T*>(o.data());
00069             d->ref();
00070         } else {
00071             d = 0;
00072         }
00073     }
00074     inline ~SharedPtr()
00075     {
00076         if (d && !d->deref()) {
00077             delete d;
00078         }
00079     }
00080 
00081     inline void reset()
00082     {
00083         SharedPtr<T>().swap(*this);
00084     }
00085 
00086     inline T *data() const { return d; }
00087     inline const T *constData() const { return d; }
00088     inline T *operator->() { return d; }
00089     inline T *operator->() const { return d; }
00090 
00091     inline bool isNull() const { return !d; }
00092     inline bool operator!() const { return isNull(); }
00093     operator UnspecifiedBoolType() const { return !isNull() ? &SharedPtr<T>::operator! : 0; }
00094 
00095     inline bool operator==(const SharedPtr<T> &o) const { return d == o.d; }
00096     inline bool operator!=(const SharedPtr<T> &o) const { return d != o.d; }
00097     inline bool operator==(const T *ptr) const { return d == ptr; }
00098     inline bool operator!=(const T *ptr) const { return d != ptr; }
00099 
00100     inline SharedPtr<T> &operator=(const SharedPtr<T> &o)
00101     {
00102         SharedPtr<T>(o).swap(*this);
00103         return *this;
00104     }
00105 
00106     inline void swap(SharedPtr<T> &o)
00107     {
00108         T *tmp = d;
00109         d = o.d;
00110         o.d = tmp;
00111     }
00112 
00113     template <class X>
00114     static inline SharedPtr<T> staticCast(const SharedPtr<X> &src)
00115     {
00116         return SharedPtr<T>(static_cast<T*>(src.data()));
00117     }
00118 
00119     template <class X>
00120     static inline SharedPtr<T> dynamicCast(const SharedPtr<X> &src)
00121     {
00122         return SharedPtr<T>(dynamic_cast<T*>(src.data()));
00123     }
00124 
00125     template <class X>
00126     static inline SharedPtr<T> constCast(const SharedPtr<X> &src)
00127     {
00128         return SharedPtr<T>(const_cast<T*>(src.data()));
00129     }
00130 
00131     template <class X>
00132     static inline SharedPtr<T> qObjectCast(const SharedPtr<X> &src)
00133     {
00134         return SharedPtr<T>(qobject_cast<T*>(src.data()));
00135     }
00136 
00137 private:
00138     T *d;
00139 };
00140 
00141 template<typename T>
00142 inline uint qHash(const SharedPtr<T> &ptr)
00143 {
00144     return QT_PREPEND_NAMESPACE(qHash<T>(ptr.data()));
00145 }
00146 
00147 } // Tp
00148 
00149 #endif


Copyright © 2008-2010 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.5.8