Remove the QQmlVMEVariant array member
[platform/upstream/qtdeclarative.git] / src / qml / qml / qqmlvmemetaobject_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL21$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** $QT_END_LICENSE$
31 **
32 ****************************************************************************/
33
34 #ifndef QQMLVMEMETAOBJECT_P_H
35 #define QQMLVMEMETAOBJECT_P_H
36
37 //
38 //  W A R N I N G
39 //  -------------
40 //
41 // This file is not part of the Qt API.  It exists purely as an
42 // implementation detail.  This header file may change from version to
43 // version without notice, or even be removed.
44 //
45 // We mean it.
46 //
47
48 #include "qqml.h"
49
50 #include <QtCore/QMetaObject>
51 #include <QtCore/QBitArray>
52 #include <QtCore/QPair>
53 #include <QtCore/QDate>
54 #include <QtCore/qlist.h>
55 #include <QtCore/qdebug.h>
56
57 #include <private/qobject_p.h>
58
59 #include "qqmlguard_p.h"
60 #include "qqmlcompiler_p.h"
61 #include "qqmlcontext_p.h"
62
63 #include <private/qv8engine_p.h>
64 #include <private/qflagpointer_p.h>
65
66 #include <private/qv4value_p.h>
67
68 QT_BEGIN_NAMESPACE
69
70 #define QML_ALIAS_FLAG_PTR 0x00000001
71
72 struct QQmlVMEMetaData
73 {
74     short varPropertyCount;
75     short propertyCount;
76     short aliasCount;
77     short signalCount;
78     short methodCount;
79     short dummyForAlignment; // Add padding to ensure that the following
80                              // AliasData/PropertyData/MethodData is int aligned.
81
82     struct AliasData {
83         int contextIdx;
84         int propertyIdx;
85         int propType;
86         int flags;
87         int notifySignal;
88
89         bool isObjectAlias() const {
90             return propertyIdx == -1;
91         }
92         bool isPropertyAlias() const {
93             return !isObjectAlias() && valueTypeIndex() == -1;
94         }
95         bool isValueTypeAlias() const {
96             return !isObjectAlias() && valueTypeIndex() != -1;
97         }
98         int propertyIndex() const {
99             int index;
100             QQmlPropertyData::decodeValueTypePropertyIndex(propertyIdx, &index);
101             return index;
102         }
103         int valueTypeIndex() const {
104             return QQmlPropertyData::decodeValueTypePropertyIndex(propertyIdx);
105         }
106         int valueType() const {
107             return (valueTypeIndex() != -1) ? propType : 0;
108         }
109     };
110
111     struct PropertyData {
112         int propertyType;
113     };
114
115     struct MethodData {
116         int runtimeFunctionIndex;
117         int parameterCount;
118         quint16 lineNumber;
119     };
120
121     PropertyData *propertyData() const {
122         return (PropertyData *)(((char *)const_cast<QQmlVMEMetaData *>(this)) + sizeof(QQmlVMEMetaData));
123     }
124
125     AliasData *aliasData() const {
126         return (AliasData *)(propertyData() + propertyCount);
127     }
128
129     MethodData *methodData() const {
130         return (MethodData *)(aliasData() + aliasCount);
131     }
132 };
133
134 class QQmlVMEMetaObject;
135 class QQmlVMEVariantQObjectPtr : public QQmlGuard<QObject>
136 {
137 public:
138     inline QQmlVMEVariantQObjectPtr(bool isVar);
139     inline ~QQmlVMEVariantQObjectPtr();
140
141     inline void objectDestroyed(QObject *);
142     inline void setGuardedValue(QObject *obj, QQmlVMEMetaObject *target, int index);
143
144     QQmlVMEMetaObject *m_target;
145     unsigned m_isVar : 1; // TODO: remove?
146     int m_index : 31;
147 };
148
149 class QQmlVMEVariant;
150 class QQmlRefCount;
151 class QQmlVMEMetaObjectEndpoint;
152 class Q_QML_PRIVATE_EXPORT QQmlVMEMetaObject : public QAbstractDynamicMetaObject
153 {
154 public:
155     QQmlVMEMetaObject(QObject *obj, QQmlPropertyCache *cache, const QQmlVMEMetaData *data,
156                       QV4::ExecutionContext *qmlBindingContext = 0, QQmlCompiledData *compiledData = 0);
157     ~QQmlVMEMetaObject();
158
159     bool aliasTarget(int index, QObject **target, int *coreIndex, int *valueTypeIndex) const;
160     void registerInterceptor(int index, int valueIndex, QQmlPropertyValueInterceptor *interceptor);
161     QV4::ReturnedValue vmeMethod(int index);
162     quint16 vmeMethodLineNumber(int index);
163     void setVmeMethod(int index, const QV4::Value &function);
164     QV4::ReturnedValue vmeProperty(int index);
165     void setVMEProperty(int index, const QV4::Value &v);
166
167     void connectAliasSignal(int index, bool indexInSignalRange);
168
169     virtual QAbstractDynamicMetaObject *toDynamicMetaObject(QObject *o);
170
171     // Used by auto-tests for inspection
172     QQmlPropertyCache *propertyCache() const { return cache; }
173
174     static inline QQmlVMEMetaObject *get(QObject *o);
175     static QQmlVMEMetaObject *getForProperty(QObject *o, int coreIndex);
176     static QQmlVMEMetaObject *getForMethod(QObject *o, int coreIndex);
177     static QQmlVMEMetaObject *getForSignal(QObject *o, int coreIndex);
178
179 protected:
180     virtual int metaCall(QMetaObject::Call _c, int _id, void **_a);
181
182 public:
183     friend class QQmlVMEMetaObjectEndpoint;
184     friend class QQmlVMEVariantQObjectPtr;
185     friend class QQmlPropertyCache;
186
187     QObject *object;
188     QQmlGuardedContextData ctxt;
189     QQmlPropertyCache *cache;
190
191     const QQmlVMEMetaData *metaData;
192     inline int propOffset() const;
193     inline int methodOffset() const;
194     inline int signalOffset() const;
195     inline int signalCount() const;
196
197     bool hasAssignedMetaObjectData;
198     QQmlVMEMetaObjectEndpoint *aliasEndpoints;
199
200     QV4::WeakValue properties;
201     int firstVarPropertyIndex;
202     bool propertiesInitialized;
203     inline void allocateProperties();
204     inline bool ensurePropertiesAllocated();
205
206     int readPropertyAsInt(int id);
207     bool readPropertyAsBool(int id);
208     double readPropertyAsDouble(int id);
209     QString readPropertyAsString(int id);
210     QSizeF readPropertyAsSizeF(int id);
211     QPointF readPropertyAsPointF(int id);
212     QUrl readPropertyAsUrl(int id);
213     QDate readPropertyAsDate(int id);
214     QDateTime readPropertyAsDateTime(int id);
215     QRectF readPropertyAsRectF(int id);
216     QObject* readPropertyAsQObject(int id);
217
218     void writeProperty(int id, int v);
219     void writeProperty(int id, bool v);
220     void writeProperty(int id, double v);
221     void writeProperty(int id, const QString& v);
222     void writeProperty(int id, const QPointF& v);
223     void writeProperty(int id, const QSizeF& v);
224     void writeProperty(int id, const QUrl& v);
225     void writeProperty(int id, const QDate& v);
226     void writeProperty(int id, const QDateTime& v);
227     void writeProperty(int id, const QRectF& v);
228     void writeProperty(int id, QObject *v);
229
230     void ensureQObjectWrapper();
231
232     void mark(QV4::ExecutionEngine *e);
233
234     void connectAlias(int aliasId);
235     QBitArray aConnected;
236
237     QQmlPropertyValueInterceptor *interceptors;
238
239     QV4::PersistentValue *v8methods;
240     QV4::ReturnedValue method(int);
241
242     QV4::ReturnedValue readVarProperty(int);
243     void writeVarProperty(int, const QV4::Value &);
244     QVariant readPropertyAsVariant(int);
245     void writeProperty(int, const QVariant &);
246
247     QBiPointer<QDynamicMetaObjectData, const QMetaObject> parent;
248
249     inline QQmlVMEMetaObject *parentVMEMetaObject() const;
250
251     void listChanged(int);
252     class List : public QList<QObject*>
253     {
254     public:
255         List(int lpi, QQmlVMEMetaObject *mo) : notifyIndex(lpi), mo(mo) {}
256         int notifyIndex;
257         QQmlVMEMetaObject *mo;
258     };
259     QList<List> listProperties;
260
261     static void list_append(QQmlListProperty<QObject> *, QObject *);
262     static int list_count(QQmlListProperty<QObject> *);
263     static QObject *list_at(QQmlListProperty<QObject> *, int);
264     static void list_clear(QQmlListProperty<QObject> *);
265
266     void activate(QObject *, int, void **);
267
268     QList<QQmlVMEVariantQObjectPtr *> varObjectGuards;
269
270     QQmlVMEVariantQObjectPtr *getQObjectGuardForProperty(int) const;
271
272     friend class QV8GCCallback;
273 };
274
275 QQmlVMEMetaObject *QQmlVMEMetaObject::get(QObject *obj)
276 {
277     if (obj) {
278         if (QQmlData *data = QQmlData::get(obj)) {
279             if (data->hasVMEMetaObject)
280                 return static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(obj)->metaObject);
281         }
282     }
283
284     return 0;
285 }
286
287 int QQmlVMEMetaObject::propOffset() const
288 {
289     return cache->propertyOffset();
290 }
291
292 int QQmlVMEMetaObject::methodOffset() const
293 {
294     return cache->methodOffset();
295 }
296
297 int QQmlVMEMetaObject::signalOffset() const
298 {
299     return cache->signalOffset();
300 }
301
302 int QQmlVMEMetaObject::signalCount() const
303 {
304     return cache->signalCount();
305 }
306
307 QQmlVMEMetaObject *QQmlVMEMetaObject::parentVMEMetaObject() const
308 {
309     if (parent.isT1() && parent.flag())
310         return static_cast<QQmlVMEMetaObject *>(parent.asT1());
311
312     return 0;
313 }
314
315 QT_END_NAMESPACE
316
317 #endif // QQMLVMEMETAOBJECT_P_H