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