diff options
Diffstat (limited to 'src/declarative/qml/ftw/qdeclarativerefcount_p.h')
-rw-r--r-- | src/declarative/qml/ftw/qdeclarativerefcount_p.h | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/src/declarative/qml/ftw/qdeclarativerefcount_p.h b/src/declarative/qml/ftw/qdeclarativerefcount_p.h index 381327098c..8ae9618f30 100644 --- a/src/declarative/qml/ftw/qdeclarativerefcount_p.h +++ b/src/declarative/qml/ftw/qdeclarativerefcount_p.h @@ -54,6 +54,7 @@ // #include <QtCore/qglobal.h> +#include <QtCore/qatomic.h> QT_BEGIN_HEADER @@ -61,16 +62,19 @@ QT_BEGIN_NAMESPACE QT_MODULE(Declarative) -class Q_DECLARATIVE_EXPORT QDeclarativeRefCount +class QDeclarativeRefCount { public: - QDeclarativeRefCount(); - virtual ~QDeclarativeRefCount(); - void addref(); - void release(); + inline QDeclarativeRefCount(); + inline virtual ~QDeclarativeRefCount(); + inline void addref(); + inline void release(); + +protected: + inline virtual void destroy(); private: - int refCount; + QAtomicInt refCount; }; template<class T> @@ -92,10 +96,40 @@ public: inline operator T*() const { return o; } inline T* data() const { return o; } + inline QDeclarativeRefPointer<T> &take(T *); + private: T *o; }; +QDeclarativeRefCount::QDeclarativeRefCount() +: refCount(1) +{ +} + +QDeclarativeRefCount::~QDeclarativeRefCount() +{ + Q_ASSERT(refCount == 0); +} + +void QDeclarativeRefCount::addref() +{ + Q_ASSERT(refCount > 0); + refCount.ref(); +} + +void QDeclarativeRefCount::release() +{ + Q_ASSERT(refCount > 0); + if (!refCount.deref()) + destroy(); +} + +void QDeclarativeRefCount::destroy() +{ + delete this; +} + template<class T> QDeclarativeRefPointer<T>::QDeclarativeRefPointer() : o(0) @@ -140,6 +174,18 @@ QDeclarativeRefPointer<T> &QDeclarativeRefPointer<T>::operator=(T *other) return *this; } +/*! +Takes ownership of \a other. take() does *not* add a reference, as it assumes ownership +of the callers reference of other. +*/ +template<class T> +QDeclarativeRefPointer<T> &QDeclarativeRefPointer<T>::take(T *other) +{ + if (o) o->release(); + o = other; + return *this; +} + QT_END_NAMESPACE QT_END_HEADER |