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             T *saved = d;
00079             d = 0;
00080             delete saved;
00081         }
00082     }
00083 
00084     inline void reset()
00085     {
00086         SharedPtr<T>().swap(*this);
00087     }
00088 
00089     inline T *data() const { return d; }
00090     inline const T *constData() const { return d; }
00091     inline T *operator->() { return d; }
00092     inline T *operator->() const { return d; }
00093 
00094     inline bool isNull() const { return !d; }
00095     inline bool operator!() const { return isNull(); }
00096     operator UnspecifiedBoolType() const { return !isNull() ? &SharedPtr<T>::operator! : 0; }
00097 
00098     inline bool operator==(const SharedPtr<T> &o) const { return d == o.d; }
00099     inline bool operator!=(const SharedPtr<T> &o) const { return d != o.d; }
00100     inline bool operator==(const T *ptr) const { return d == ptr; }
00101     inline bool operator!=(const T *ptr) const { return d != ptr; }
00102 
00103     inline SharedPtr<T> &operator=(const SharedPtr<T> &o)
00104     {
00105         SharedPtr<T>(o).swap(*this);
00106         return *this;
00107     }
00108 
00109     inline void swap(SharedPtr<T> &o)
00110     {
00111         T *tmp = d;
00112         d = o.d;
00113         o.d = tmp;
00114     }
00115 
00116     template <class X>
00117     static inline SharedPtr<T> staticCast(const SharedPtr<X> &src)
00118     {
00119         return SharedPtr<T>(static_cast<T*>(src.data()));
00120     }
00121 
00122     template <class X>
00123     static inline SharedPtr<T> dynamicCast(const SharedPtr<X> &src)
00124     {
00125         return SharedPtr<T>(dynamic_cast<T*>(src.data()));
00126     }
00127 
00128     template <class X>
00129     static inline SharedPtr<T> constCast(const SharedPtr<X> &src)
00130     {
00131         return SharedPtr<T>(const_cast<T*>(src.data()));
00132     }
00133 
00134     template <class X>
00135     static inline SharedPtr<T> qObjectCast(const SharedPtr<X> &src)
00136     {
00137         return SharedPtr<T>(qobject_cast<T*>(src.data()));
00138     }
00139 
00140 private:
00141     T *d;
00142 };
00143 
00144 template<typename T>
00145 inline uint qHash(const SharedPtr<T> &ptr)
00146 {
00147     return QT_PREPEND_NAMESPACE(qHash<T>(ptr.data()));
00148 }
00149 
00150 } // Tp
00151 
00152 #endif


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