Docs - add missing images and code, clean up sections
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlvmemetaobject_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 QQMLVMEMETAOBJECT_P_H
43 #define QQMLVMEMETAOBJECT_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 "qqml.h"
57
58 #include <QtCore/QMetaObject>
59 #include <QtCore/QBitArray>
60 #include <QtCore/QPair>
61 #include <QtCore/QDate>
62 #include <QtCore/qlist.h>
63 #include <QtCore/qdebug.h>
64
65 #include <private/qobject_p.h>
66
67 #include "qqmlguard_p.h"
68 #include "qqmlcompiler_p.h"
69 #include "qqmlcontext_p.h"
70
71 #include <private/qv8engine_p.h>
72 #include <private/qflagpointer_p.h>
73
74 #include <private/qv8_p.h>
75
76 QT_BEGIN_NAMESPACE
77
78 #define QML_ALIAS_FLAG_PTR 0x00000001
79
80 struct QQmlVMEMetaData
81 {
82     short varPropertyCount;
83     short propertyCount;
84     short aliasCount;
85     short signalCount;
86     short methodCount;
87     short dummyForAlignment; // Add padding to ensure that the following
88                              // AliasData/PropertyData/MethodData is int aligned.
89
90     struct AliasData {
91         int contextIdx;
92         int propertyIdx;
93         int propType;
94         int flags;
95         int notifySignal;
96
97         bool isObjectAlias() const {
98             return propertyIdx == -1;
99         }
100         bool isPropertyAlias() const {
101             return !isObjectAlias() && !(propertyIdx & 0xFFFF0000);
102         }
103         bool isValueTypeAlias() const {
104             return !isObjectAlias() && (propertyIdx & 0xFFFF0000);
105         }
106         int propertyIndex() const {
107             return propertyIdx & 0x0000FFFF;
108         }
109         int valueTypeIndex() const {
110             return (propertyIdx & 0xFFFF0000) >> 16;
111         }
112         int valueType() const {
113             return (propertyIdx & 0xFFFF0000) ? propType : 0;
114         }
115     };
116     
117     struct PropertyData {
118         int propertyType;
119     };
120
121     struct MethodData {
122         int parameterCount;
123         int bodyOffset;
124         int bodyLength;
125         quint16 lineNumber;
126     };
127
128     PropertyData *propertyData() const {
129         return (PropertyData *)(((const char *)this) + sizeof(QQmlVMEMetaData));
130     }
131
132     AliasData *aliasData() const {
133         return (AliasData *)(propertyData() + propertyCount);
134     }
135
136     MethodData *methodData() const {
137         return (MethodData *)(aliasData() + aliasCount);
138     }
139 };
140
141 class QQmlVMEMetaObject;
142 class QQmlVMEVariantQObjectPtr : public QQmlGuard<QObject>
143 {
144 public:
145     inline QQmlVMEVariantQObjectPtr(bool isVar);
146     inline ~QQmlVMEVariantQObjectPtr();
147
148     inline void objectDestroyed(QObject *);
149     inline void setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index);
150
151     QQmlVMEMetaObject *m_target;
152     unsigned m_isVar : 1;
153     int m_index : 31;
154 };
155
156 class QV8QObjectWrapper;
157 class QQmlVMEVariant;
158 class QQmlRefCount;
159 class QQmlVMEMetaObjectEndpoint;
160 class Q_AUTOTEST_EXPORT QQmlVMEMetaObject : public QAbstractDynamicMetaObject,
161                                                     public QV8GCCallback::Node
162 {
163 public:
164     QQmlVMEMetaObject(QObject *obj, QQmlPropertyCache *cache, const QQmlVMEMetaData *data);
165     ~QQmlVMEMetaObject();
166
167     bool aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const;
168     void registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor);
169     v8::Handle<v8::Function> vmeMethod(int index);
170     quint16 vmeMethodLineNumber(int index);
171     void setVmeMethod(int index, v8::Persistent<v8::Function>);
172     v8::Handle<v8::Value> vmeProperty(int index);
173     void setVMEProperty(int index, v8::Handle<v8::Value> v);
174
175     void connectAliasSignal(int index, bool indexInSignalRange);
176
177     virtual QAbstractDynamicMetaObject *toDynamicMetaObject(QObject *o);
178
179     static inline QQmlVMEMetaObject *get(QObject *o);
180     static QQmlVMEMetaObject *getForProperty(QObject *o, int coreIndex);
181     static QQmlVMEMetaObject *getForMethod(QObject *o, int coreIndex);
182     static QQmlVMEMetaObject *getForSignal(QObject *o, int coreIndex);
183
184 protected:
185     virtual int metaCall(QMetaObject::Call _c, int _id, void **_a);
186
187 public:
188     friend class QQmlVMEMetaObjectEndpoint;
189     friend class QQmlVMEVariantQObjectPtr;
190     friend class QQmlPropertyCache;
191
192     QObject *object;
193     QQmlGuardedContextData ctxt;
194     QQmlPropertyCache *cache;
195
196     const QQmlVMEMetaData *metaData;
197     inline int propOffset() const;
198     inline int methodOffset() const;
199     inline int signalOffset() const;
200     inline int signalCount() const;
201
202     bool hasAssignedMetaObjectData;
203     QQmlVMEVariant *data;
204     QQmlVMEMetaObjectEndpoint *aliasEndpoints;
205
206     v8::Persistent<v8::Array> varProperties;
207     int firstVarPropertyIndex;
208     bool varPropertiesInitialized;
209     static void VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter);
210     static void GcPrologueCallback(QV8GCCallback::Node *node);
211     inline void allocateVarPropertiesArray();
212     inline bool ensureVarPropertiesAllocated();
213
214     void connectAlias(int aliasId);
215     QBitArray aConnected;
216
217     QQmlPropertyValueInterceptor *interceptors;
218
219     v8::Persistent<v8::Function> *v8methods;
220     v8::Handle<v8::Function> method(int);
221
222     v8::Handle<v8::Value> readVarProperty(int);
223     void writeVarProperty(int, v8::Handle<v8::Value>);
224     QVariant readPropertyAsVariant(int);
225     void writeProperty(int, const QVariant &);
226
227     QBiPointer<QDynamicMetaObjectData, const QMetaObject> parent;
228
229     inline QQmlVMEMetaObject *parentVMEMetaObject() const;
230
231     void listChanged(int);
232     class List : public QList<QObject*>
233     {
234     public:
235         List(int lpi, QQmlVMEMetaObject *mo) : notifyIndex(lpi), mo(mo) {}
236         int notifyIndex;
237         QQmlVMEMetaObject *mo;
238     };
239     QList<List> listProperties;
240
241     static void list_append(QQmlListProperty<QObject> *, QObject *);
242     static int list_count(QQmlListProperty<QObject> *);
243     static QObject *list_at(QQmlListProperty<QObject> *, int);
244     static void list_clear(QQmlListProperty<QObject> *);
245
246     void activate(QObject *, int, void **);
247
248     QList<QQmlVMEVariantQObjectPtr *> varObjectGuards;
249
250     QQmlVMEVariantQObjectPtr *getQObjectGuardForProperty(int) const;
251
252     friend class QV8GCCallback;
253     friend class QV8QObjectWrapper;
254 };
255
256 QQmlVMEMetaObject *QQmlVMEMetaObject::get(QObject *obj)
257 {
258     if (obj) {
259         if (QQmlData *data = QQmlData::get(obj)) {
260             if (data->hasVMEMetaObject)
261                 return static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(obj)->metaObject);
262         }
263     }
264
265     return 0;
266 }
267
268 int QQmlVMEMetaObject::propOffset() const
269 {
270     return cache->propertyOffset();
271 }
272
273 int QQmlVMEMetaObject::methodOffset() const
274 {
275     return cache->methodOffset();
276 }
277
278 int QQmlVMEMetaObject::signalOffset() const
279 {
280     return cache->signalOffset();
281 }
282
283 int QQmlVMEMetaObject::signalCount() const
284 {
285     return cache->signalCount();
286 }
287
288 QQmlVMEMetaObject *QQmlVMEMetaObject::parentVMEMetaObject() const
289 {
290     if (parent.isT1())
291         return static_cast<QQmlVMEMetaObject *>(parent.asT1());
292
293     return 0;
294 }
295
296 QT_END_NAMESPACE
297
298 #endif // QQMLVMEMETAOBJECT_P_H