Merge branch 'qtquick2' of scm.dev.nokia.troll.no:qt/qtdeclarative-staging into v8
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativedata_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEDATA_P_H
43 #define QDECLARATIVEDATA_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include <QtScript/qscriptvalue.h>
57 #include <private/qobject_p.h>
58 #include "private/qdeclarativeguard_p.h"
59
60 #include <private/qv8_p.h>
61
62 QT_BEGIN_NAMESPACE
63
64 class QDeclarativeCompiledData;
65 class QDeclarativeAbstractBinding;
66 class QDeclarativeContext;
67 class QDeclarativePropertyCache;
68 class QDeclarativeContextData;
69 class QDeclarativeNotifier;
70 class QDeclarativeDataExtended;
71 // This class is structured in such a way, that simply zero'ing it is the
72 // default state for elemental object allocations.  This is crucial in the
73 // workings of the QDeclarativeInstruction::CreateSimpleObject instruction.
74 // Don't change anything here without first considering that case!
75 class Q_AUTOTEST_EXPORT QDeclarativeData : public QAbstractDeclarativeData
76 {
77 public:
78     QDeclarativeData()
79         : ownMemory(true), ownContext(false), indestructible(true), explicitIndestructibleSet(false), 
80           hasTaintedV8Object(false), context(0), outerContext(0), bindings(0), nextContextObject(0), 
81           prevContextObject(0), bindingBitsSize(0), bindingBits(0), lineNumber(0), columnNumber(0), 
82           deferredComponent(0), deferredIdx(0), v8objectid(0), propertyCache(0), guards(0), 
83           extendedData(0) {
84           init(); 
85       }
86
87     static inline void init() {
88         QAbstractDeclarativeData::destroyed = destroyed;
89         QAbstractDeclarativeData::parentChanged = parentChanged;
90         QAbstractDeclarativeData::objectNameChanged = objectNameChanged;
91     }
92
93     static void destroyed(QAbstractDeclarativeData *, QObject *);
94     static void parentChanged(QAbstractDeclarativeData *, QObject *, QObject *);
95     static void objectNameChanged(QAbstractDeclarativeData *, QObject *);
96
97     void destroyed(QObject *);
98     void parentChanged(QObject *, QObject *);
99     void objectNameChanged(QObject *);
100
101     void setImplicitDestructible() {
102         if (!explicitIndestructibleSet) indestructible = false;
103     }
104
105     quint32 ownMemory:1;
106     quint32 ownContext:1;
107     quint32 indestructible:1;
108     quint32 explicitIndestructibleSet:1;
109     quint32 hasTaintedV8Object:1;
110     quint32 dummy:27;
111
112     // The context that created the C++ object
113     QDeclarativeContextData *context; 
114     // The outermost context in which this object lives
115     QDeclarativeContextData *outerContext;
116
117     QDeclarativeAbstractBinding *bindings;
118
119     // Linked list for QDeclarativeContext::contextObjects
120     QDeclarativeData *nextContextObject;
121     QDeclarativeData**prevContextObject;
122
123     int bindingBitsSize;
124     quint32 *bindingBits; 
125     bool hasBindingBit(int) const;
126     void clearBindingBit(int);
127     void setBindingBit(QObject *obj, int);
128
129     ushort lineNumber;
130     ushort columnNumber;
131
132     QDeclarativeCompiledData *deferredComponent; // Can't this be found from the context?
133     unsigned int deferredIdx;
134
135     quint32 v8objectid;
136     v8::Persistent<v8::Object> v8object;
137
138     QDeclarativePropertyCache *propertyCache;
139
140     QDeclarativeGuard<QObject> *guards;
141
142     static QDeclarativeData *get(const QObject *object, bool create = false) {
143         QObjectPrivate *priv = QObjectPrivate::get(const_cast<QObject *>(object));
144         if (priv->wasDeleted) {
145             Q_ASSERT(!create);
146             return 0;
147         } else if (priv->declarativeData) {
148             return static_cast<QDeclarativeData *>(priv->declarativeData);
149         } else if (create) {
150             priv->declarativeData = new QDeclarativeData;
151             return static_cast<QDeclarativeData *>(priv->declarativeData);
152         } else {
153             return 0;
154         }
155     }
156
157     bool hasExtendedData() const { return extendedData != 0; }
158     QDeclarativeNotifier *objectNameNotifier() const;
159     QHash<int, QObject *> *attachedProperties() const;
160
161 private:
162     // For objectNameNotifier and attachedProperties
163     mutable QDeclarativeDataExtended *extendedData;
164 };
165
166 template<class T>
167 void QDeclarativeGuard<T>::addGuard()
168 {
169     Q_ASSERT(!prev);
170
171     if (QObjectPrivate::get(o)->wasDeleted) 
172         return;
173
174     QDeclarativeData *data = QDeclarativeData::get(o, true);
175     next = data->guards;
176     if (next) reinterpret_cast<QDeclarativeGuard<T> *>(next)->prev = &next;
177     data->guards = reinterpret_cast<QDeclarativeGuard<QObject> *>(this);
178     prev = &data->guards;
179 }
180
181 template<class T>
182 void QDeclarativeGuard<T>::remGuard()
183 {
184     Q_ASSERT(prev);
185
186     if (next) reinterpret_cast<QDeclarativeGuard<T> *>(next)->prev = prev;
187     *prev = next;
188     next = 0;
189     prev = 0;
190 }
191
192 QT_END_NAMESPACE
193
194 #endif // QDECLARATIVEDATA_P_H