Add missing QT_{BEGIN,END}_NAMESPACE
[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
191     QObject *object;
192     QQmlGuardedContextData ctxt;
193     QQmlPropertyCache *cache;
194
195     const QQmlVMEMetaData *metaData;
196     inline int propOffset() const;
197     inline int methodOffset() const;
198     inline int signalOffset() const;
199
200     bool hasAssignedMetaObjectData;
201     QQmlVMEVariant *data;
202     QQmlVMEMetaObjectEndpoint *aliasEndpoints;
203
204     v8::Persistent<v8::Array> varProperties;
205     int firstVarPropertyIndex;
206     bool varPropertiesInitialized;
207     static void VarPropertiesWeakReferenceCallback(v8::Persistent<v8::Value> object, void* parameter);
208     static void GcPrologueCallback(QV8GCCallback::Node *node);
209     inline void allocateVarPropertiesArray();
210     inline bool ensureVarPropertiesAllocated();
211
212     void connectAlias(int aliasId);
213     QBitArray aConnected;
214
215     QQmlPropertyValueInterceptor *interceptors;
216
217     v8::Persistent<v8::Function> *v8methods;
218     v8::Handle<v8::Function> method(int);
219
220     v8::Handle<v8::Value> readVarProperty(int);
221     void writeVarProperty(int, v8::Handle<v8::Value>);
222     QVariant readPropertyAsVariant(int);
223     void writeProperty(int, const QVariant &);
224
225     QBiPointer<QDynamicMetaObjectData, const QMetaObject> parent;
226
227     void listChanged(int);
228     class List : public QList<QObject*>
229     {
230     public:
231         List(int lpi, QQmlVMEMetaObject *mo) : notifyIndex(lpi), mo(mo) {}
232         int notifyIndex;
233         QQmlVMEMetaObject *mo;
234     };
235     QList<List> listProperties;
236
237     static void list_append(QQmlListProperty<QObject> *, QObject *);
238     static int list_count(QQmlListProperty<QObject> *);
239     static QObject *list_at(QQmlListProperty<QObject> *, int);
240     static void list_clear(QQmlListProperty<QObject> *);
241
242     void activate(QObject *, int, void **);
243
244     QList<QQmlVMEVariantQObjectPtr *> varObjectGuards;
245
246     QQmlVMEVariantQObjectPtr *getQObjectGuardForProperty(int) const;
247
248     friend class QV8GCCallback;
249     friend class QV8QObjectWrapper;
250 };
251
252 QQmlVMEMetaObject *QQmlVMEMetaObject::get(QObject *obj)
253 {
254     if (obj) {
255         if (QQmlData *data = QQmlData::get(obj)) {
256             if (data->hasVMEMetaObject)
257                 return static_cast<QQmlVMEMetaObject *>(QObjectPrivate::get(obj)->metaObject);
258         }
259     }
260
261     return 0;
262 }
263
264 int QQmlVMEMetaObject::propOffset() const
265 {
266     return cache->propertyOffset();
267 }
268
269 int QQmlVMEMetaObject::methodOffset() const
270 {
271     return cache->methodOffset();
272 }
273
274 int QQmlVMEMetaObject::signalOffset() const
275 {
276     return cache->signalOffset();
277 }
278
279 QT_END_NAMESPACE
280
281 #endif // QQMLVMEMETAOBJECT_P_H