Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qdeclarativerefcount_p.h
index 3813270..05aede4 100644 (file)
@@ -1,8 +1,7 @@
 /****************************************************************************
 **
-** 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.
 **
@@ -35,6 +34,7 @@
 **
 **
 **
+**
 ** $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>
@@ -92,10 +95,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.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) 
@@ -140,6 +173,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