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

shared-ptr.h

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


Copyright © 2008-2011 Collabora Ltd. and Nokia Corporation
Telepathy-Qt4 0.5.9