Docs - add missing images and code, clean up sections
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmldata_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQMLDATA_P_H
43 #define QQMLDATA_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 <private/qtqmlglobal_p.h>
57 #include <private/qobject_p.h>
58 #include <private/qv8_p.h>
59
60 QT_BEGIN_NAMESPACE
61
62 template <class Key, class T> class QHash;
63 class QQmlGuardImpl;
64 class QQmlCompiledData;
65 class QQmlAbstractBinding;
66 class QQmlAbstractBoundSignal;
67 class QQmlContext;
68 class QQmlPropertyCache;
69 class QQmlContextData;
70 class QQmlNotifier;
71 class QQmlDataExtended;
72 class QQmlNotifierEndpoint;
73 // This class is structured in such a way, that simply zero'ing it is the
74 // default state for elemental object allocations.  This is crucial in the
75 // workings of the QQmlInstruction::CreateSimpleObject instruction.
76 // Don't change anything here without first considering that case!
77 class Q_QML_PRIVATE_EXPORT QQmlData : public QAbstractDeclarativeData
78 {
79 public:
80     QQmlData()
81         : ownMemory(true), ownContext(false), indestructible(true), explicitIndestructibleSet(false), 
82           hasTaintedV8Object(false), isQueuedForDeletion(false), rootObjectInCreation(false),
83           hasVMEMetaObject(false), parentFrozen(false), notifyList(0), context(0), outerContext(0),
84           bindings(0), signalHandlers(0), nextContextObject(0), prevContextObject(0), bindingBitsSize(0), bindingBits(0),
85           lineNumber(0), columnNumber(0), compiledData(0), deferredIdx(0), v8objectid(0),
86           propertyCache(0), guards(0), extendedData(0) {
87         init();
88     }
89
90     static inline void init() {
91         static bool initialized = false;
92         if (!initialized) {
93             initialized = true;
94             QAbstractDeclarativeData::destroyed = destroyed;
95             QAbstractDeclarativeData::parentChanged = parentChanged;
96             QAbstractDeclarativeData::signalEmitted = signalEmitted;
97             QAbstractDeclarativeData::receivers = receivers;
98             QAbstractDeclarativeData::isSignalConnected = isSignalConnected;
99         }
100     }
101
102     static void destroyed(QAbstractDeclarativeData *, QObject *);
103     static void parentChanged(QAbstractDeclarativeData *, QObject *, QObject *);
104     static void signalEmitted(QAbstractDeclarativeData *, QObject *, int, void **);
105     static int receivers(QAbstractDeclarativeData *, const QObject *, int);
106     static bool isSignalConnected(QAbstractDeclarativeData *, const QObject *, int);
107
108     void destroyed(QObject *);
109     void parentChanged(QObject *, QObject *);
110
111     void setImplicitDestructible() {
112         if (!explicitIndestructibleSet) indestructible = false;
113     }
114
115     quint32 ownMemory:1;
116     quint32 ownContext:1;
117     quint32 indestructible:1;
118     quint32 explicitIndestructibleSet:1;
119     quint32 hasTaintedV8Object:1;
120     quint32 isQueuedForDeletion:1;
121     /*
122      * rootObjectInCreation should be true only when creating top level CPP and QML objects,
123      * v8 GC will check this flag, only deletes the objects when rootObjectInCreation is false.
124      */
125     quint32 rootObjectInCreation:1;
126     quint32 hasVMEMetaObject:1;
127     quint32 parentFrozen:1;
128     quint32 dummy:23;
129
130     struct NotifyList {
131         quint64 connectionMask;
132
133         quint16 maximumTodoIndex;
134         quint16 notifiesSize;
135
136         QQmlNotifierEndpoint *todo;
137         QQmlNotifierEndpoint**notifies;
138         void layout();
139     private:
140         void layout(QQmlNotifierEndpoint*);
141     };
142     NotifyList *notifyList;
143     
144     inline QQmlNotifierEndpoint *notify(int index);
145     void addNotify(int index, QQmlNotifierEndpoint *);
146     int endpointCount(int index);
147     bool signalHasEndpoint(int index);
148     void disconnectNotifiers();
149
150     // The context that created the C++ object
151     QQmlContextData *context; 
152     // The outermost context in which this object lives
153     QQmlContextData *outerContext;
154
155     QQmlAbstractBinding *bindings;
156     QQmlAbstractBoundSignal *signalHandlers;
157
158     // Linked list for QQmlContext::contextObjects
159     QQmlData *nextContextObject;
160     QQmlData**prevContextObject;
161
162     int bindingBitsSize;
163     quint32 *bindingBits; 
164     bool hasBindingBit(int) const;
165     void clearBindingBit(int);
166     void setBindingBit(QObject *obj, int);
167
168     quint16 lineNumber;
169     quint16 columnNumber;
170
171     QQmlCompiledData *compiledData;
172     unsigned int deferredIdx;
173
174     quint32 v8objectid;
175     v8::Persistent<v8::Object> v8object;
176
177     QQmlPropertyCache *propertyCache;
178
179     QQmlGuardImpl *guards;
180
181     static QQmlData *get(const QObject *object, bool create = false) {
182         QObjectPrivate *priv = QObjectPrivate::get(const_cast<QObject *>(object));
183         if (priv->wasDeleted) {
184             Q_ASSERT(!create);
185             return 0;
186         } else if (priv->declarativeData) {
187             return static_cast<QQmlData *>(priv->declarativeData);
188         } else if (create) {
189             priv->declarativeData = new QQmlData;
190             return static_cast<QQmlData *>(priv->declarativeData);
191         } else {
192             return 0;
193         }
194     }
195
196     bool hasExtendedData() const { return extendedData != 0; }
197     QHash<int, QObject *> *attachedProperties() const;
198
199     static inline bool wasDeleted(QObject *);
200
201     static void markAsDeleted(QObject *);
202     static void setQueuedForDeletion(QObject *);
203
204 private:
205     // For attachedProperties
206     mutable QQmlDataExtended *extendedData;
207 };
208
209 bool QQmlData::wasDeleted(QObject *object)
210 {
211     if (!object)
212         return true;
213
214     QObjectPrivate *priv = QObjectPrivate::get(object);
215     if (priv->wasDeleted)
216         return true;
217
218     return priv->declarativeData &&
219            static_cast<QQmlData *>(priv->declarativeData)->isQueuedForDeletion;
220 }
221
222 QQmlNotifierEndpoint *QQmlData::notify(int index)
223 {
224     Q_ASSERT(index <= 0xFFFF);
225
226     if (!notifyList || !(notifyList->connectionMask & (1ULL << quint64(index % 64)))) {
227         return 0;
228     } else if (index < notifyList->notifiesSize) {
229         return notifyList->notifies[index];
230     } else if (index <= notifyList->maximumTodoIndex) {
231         notifyList->layout();
232     }
233
234     if (index < notifyList->notifiesSize) {
235         return notifyList->notifies[index];
236     } else {
237         return 0;
238     }
239 }
240
241 QT_END_NAMESPACE
242
243 #endif // QQMLDATA_P_H