/****************************************************************************
**
-** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
**
**
**
+**
** $QT_END_LICENSE$
**
****************************************************************************/
//
#include <QtCore/qglobal.h>
+#include <QtCore/qatomic.h>
QT_BEGIN_HEADER
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>
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.load() == 0);
+}
+
+void QDeclarativeRefCount::addref()
+{
+ Q_ASSERT(refCount.load() > 0);
+ refCount.ref();
+}
+
+void QDeclarativeRefCount::release()
+{
+ Q_ASSERT(refCount.load() > 0);
+ if (!refCount.deref())
+ destroy();
+}
+
+void QDeclarativeRefCount::destroy()
+{
+ delete this;
+}
+
template<class T>
QDeclarativeRefPointer<T>::QDeclarativeRefPointer()
: o(0)
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