Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeguard_p.h
index a085258..fdd5c01 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 QtCore module of the Qt Toolkit.
 **
@@ -35,6 +34,7 @@
 **
 **
 **
+**
 ** $QT_END_LICENSE$
 **
 ****************************************************************************/
 
 #include <QtCore/qglobal.h>
 #include <QtCore/qvariant.h>
+#include <private/qdeclarativedata_p.h>
 
 QT_BEGIN_NAMESPACE
 
+class QDeclarativeGuardImpl 
+{
+public:
+    inline QDeclarativeGuardImpl();
+    inline QDeclarativeGuardImpl(QObject *);
+    inline QDeclarativeGuardImpl(const QDeclarativeGuardImpl &);
+    inline ~QDeclarativeGuardImpl();
+
+    QObject *o;
+    QDeclarativeGuardImpl  *next;
+    QDeclarativeGuardImpl **prev;
+
+    inline void addGuard();
+    inline void remGuard();
+};
+
 class QObject;
 template<class T>
-class QDeclarativeGuard
+class QDeclarativeGuard : private QDeclarativeGuardImpl
 {
-    QObject *o;
-    QDeclarativeGuard<QObject> *next;
-    QDeclarativeGuard<QObject> **prev;
     friend class QDeclarativeData;
 public:
     inline QDeclarativeGuard();
@@ -74,6 +88,9 @@ public:
 
     inline QDeclarativeGuard<T> &operator=(const QDeclarativeGuard<T> &o);
     inline QDeclarativeGuard<T> &operator=(T *);
+
+    inline T *object() const;
+    inline void setObject(T *g);
     
     inline bool isNull() const
         { return !o; }
@@ -89,67 +106,111 @@ public:
 
 protected:
     virtual void objectDestroyed(T *) {}
-
-private:
-    inline void addGuard();
-    inline void remGuard();
 };
 
 QT_END_NAMESPACE
 
 Q_DECLARE_METATYPE(QDeclarativeGuard<QObject>)
 
-#include "private/qdeclarativedata_p.h"
-
 QT_BEGIN_NAMESPACE
 
+QDeclarativeGuardImpl::QDeclarativeGuardImpl()
+: o(0), next(0), prev(0)
+{
+}
+
+QDeclarativeGuardImpl::QDeclarativeGuardImpl(QObject *g)
+: o(g), next(0), prev(0)
+{
+    if (o) addGuard();
+}
+
+QDeclarativeGuardImpl::QDeclarativeGuardImpl(const QDeclarativeGuardImpl &g)
+: o(g.o), next(0), prev(0)
+{
+    if (o) addGuard();
+}
+
+QDeclarativeGuardImpl::~QDeclarativeGuardImpl()
+{
+    if (prev) remGuard();
+    o = 0;
+}
+
+void QDeclarativeGuardImpl::addGuard()
+{
+    Q_ASSERT(!prev);
+
+    if (QObjectPrivate::get(o)->wasDeleted) 
+        return;
+
+    QDeclarativeData *data = QDeclarativeData::get(o, true);
+    next = data->guards;
+    if (next) next->prev = &next;
+    data->guards = this;
+    prev = &data->guards;
+}
+
+void QDeclarativeGuardImpl::remGuard()
+{
+    Q_ASSERT(prev);
+
+    if (next) next->prev = prev;
+    *prev = next;
+    next = 0;
+    prev = 0;
+}
+
 template<class T>
 QDeclarativeGuard<T>::QDeclarativeGuard()
-: o(0), next(0), prev(0)
 {
 }
 
 template<class T>
 QDeclarativeGuard<T>::QDeclarativeGuard(T *g)
-: o(g), next(0), prev(0)
+: QDeclarativeGuardImpl(g)
 {
-    if (o) addGuard();
 }
 
 template<class T>
 QDeclarativeGuard<T>::QDeclarativeGuard(const QDeclarativeGuard<T> &g)
-: o(g.o), next(0), prev(0)
+: QDeclarativeGuardImpl(g)
 {
-    if (o) addGuard();
 }
 
 template<class T>
 QDeclarativeGuard<T>::~QDeclarativeGuard()
 {
-    if (prev) remGuard();
-    o = 0;
 }
 
 template<class T>
 QDeclarativeGuard<T> &QDeclarativeGuard<T>::operator=(const QDeclarativeGuard<T> &g)
 {
-    if (g.o != o) {
-        if (prev) remGuard();
-        o = g.o;
-        if (o) addGuard();
-    }
+    setObject(g.object());
     return *this;
 }
 
 template<class T>
 QDeclarativeGuard<T> &QDeclarativeGuard<T>::operator=(T *g)
 {
+    setObject(g);
+    return *this;
+}
+
+template<class T>
+T *QDeclarativeGuard<T>::object() const 
+{ 
+    return static_cast<T *>(o); 
+};
+
+template<class T>
+void QDeclarativeGuard<T>::setObject(T *g) 
+{
     if (g != o) {
         if (prev) remGuard();
         o = g;
         if (o) addGuard();
     }
-    return *this;
 }
 
 QT_END_NAMESPACE