Introduce more generic fast property handling
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativepropertycache_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEPROPERTYCACHE_P_H
43 #define QDECLARATIVEPROPERTYCACHE_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/qdeclarativerefcount_p.h>
57 #include "qdeclarativecleanup_p.h"
58 #include "qdeclarativenotifier_p.h"
59
60 #include <private/qhashedstring_p.h>
61 #include <QtCore/qvarlengtharray.h>
62 #include <QtCore/qvector.h>
63
64 QT_BEGIN_NAMESPACE
65
66 class QV8Engine;
67 class QMetaProperty;
68 class QV8QObjectWrapper;
69 class QDeclarativeEngine;
70 class QDeclarativePropertyData;
71 class QDeclarativeAccessors;
72 class QDeclarativePropertyCacheMethodArguments;
73
74 // We have this somewhat awful split between RawData and Data so that RawData can be
75 // used in unions.  In normal code, you should always use Data which initializes RawData
76 // to an invalid state on construction.
77 class QDeclarativePropertyRawData
78 {
79 public:
80     enum Flag {
81         NoFlags           = 0x00000000,
82         ValueTypeFlagMask = 0x0000FFFF, // Flags in valueTypeFlags must fit in this mask
83
84         // Can apply to all properties, except IsFunction
85         IsConstant         = 0x00000001, // Has CONST flag
86         IsWritable         = 0x00000002, // Has WRITE function
87         IsResettable       = 0x00000004, // Has RESET function
88         IsAlias            = 0x00000008, // Is a QML alias to another property
89         IsFinal            = 0x00000010, // Has FINAL flag
90         IsDirect           = 0x00000020, // Exists on a C++ QMetaObject
91         HasAccessors       = 0x00000040, // Has property accessors
92
93         // These are mutualy exclusive
94         IsFunction         = 0x00000080, // Is an invokable
95         IsQObjectDerived   = 0x00000100, // Property type is a QObject* derived type
96         IsEnumType         = 0x00000200, // Property type is an enum
97         IsQList            = 0x00000400, // Property type is a QML list
98         IsQmlBinding       = 0x00000800, // Property type is a QDeclarativeBinding*
99         IsQJSValue         = 0x00001000, // Property type is a QScriptValue
100         IsV8Handle         = 0x00002000, // Property type is a QDeclarativeV8Handle
101         IsVMEProperty      = 0x00004000, // Property type is a "var" property of VMEMO
102         IsValueTypeVirtual = 0x00008000, // Property is a value type "virtual" property
103         IsQVariant         = 0x00010000, // Property is a QVariant
104
105         // Apply only to IsFunctions
106         IsVMEFunction      = 0x00020000, // Function was added by QML
107         HasArguments       = 0x00040000, // Function takes arguments
108         IsSignal           = 0x00080000, // Function is a signal
109         IsVMESignal        = 0x00100000, // Signal was added by QML
110         IsV8Function       = 0x00200000, // Function takes QDeclarativeV8Function* args
111         IsSignalHandler    = 0x00400000, // Function is a signal handler
112         IsOverload         = 0x00800000, // Function is an overload of another function
113
114         // Internal QDeclarativePropertyCache flags
115         NotFullyResolved   = 0x01000000  // True if the type data is to be lazily resolved
116     };
117     Q_DECLARE_FLAGS(Flags, Flag)
118
119     Flags getFlags() const { return Flag(flags); }
120     void setFlags(Flags f) { flags = f; }
121
122     bool isValid() const { return coreIndex != -1; }
123
124     bool isConstant() const { return flags & IsConstant; }
125     bool isWritable() const { return flags & IsWritable; }
126     bool isResettable() const { return flags & IsResettable; }
127     bool isAlias() const { return flags & IsAlias; }
128     bool isFinal() const { return flags & IsFinal; }
129     bool isDirect() const { return flags & IsDirect; }
130     bool hasAccessors() const { return flags & HasAccessors; }
131     bool isFunction() const { return flags & IsFunction; }
132     bool isQObject() const { return flags & IsQObjectDerived; }
133     bool isEnum() const { return flags & IsEnumType; }
134     bool isQList() const { return flags & IsQList; }
135     bool isQmlBinding() const { return flags & IsQmlBinding; }
136     bool isQJSValue() const { return flags & IsQJSValue; }
137     bool isV8Handle() const { return flags & IsV8Handle; }
138     bool isVMEProperty() const { return flags & IsVMEProperty; }
139     bool isValueTypeVirtual() const { return flags & IsValueTypeVirtual; }
140     bool isQVariant() const { return flags & IsQVariant; }
141     bool isVMEFunction() const { return flags & IsVMEFunction; }
142     bool hasArguments() const { return flags & HasArguments; }
143     bool isSignal() const { return flags & IsSignal; }
144     bool isVMESignal() const { return flags & IsVMESignal; }
145     bool isV8Function() const { return flags & IsV8Function; }
146     bool isSignalHandler() const { return flags & IsSignalHandler; }
147     bool isOverload() const { return flags & IsOverload; }
148
149     bool hasOverride() const { return !(flags & IsValueTypeVirtual) && overrideIndex >= 0; }
150
151     // Returns -1 if not a value type virtual property
152     inline int getValueTypeCoreIndex() const;
153
154     union {
155         int propType;             // When !NotFullyResolved
156         const char *propTypeName; // When NotFullyResolved
157     };
158     int coreIndex;
159     union {
160         int notifyIndex;  // When !IsFunction
161         void *arguments;  // When IsFunction && HasArguments
162     };
163     union {
164         struct { // When !IsValueTypeVirtual
165             uint overrideIndexIsProperty : 1;
166             signed int overrideIndex : 31;
167         };
168         struct { // When IsValueTypeVirtual
169             quint16 valueTypeFlags; // flags of the access property on the value type proxy object
170             quint8 valueTypePropType; // The QVariant::Type of access property on the value type
171                                       // proxy object
172             quint8 valueTypeCoreIndex; // The prop index of the access property on the value type
173                                        //proxy object
174         };
175     };
176
177     qint16 revision;
178     qint16 metaObjectOffset;
179
180     struct { // When HasAccessors
181         QDeclarativeAccessors *accessors;
182         intptr_t accessorData;
183     };
184
185 private:
186     friend class QDeclarativePropertyData;
187     friend class QDeclarativePropertyCache;
188     quint32 flags;
189 };
190 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyRawData::Flags);
191
192 class QDeclarativePropertyData : public QDeclarativePropertyRawData
193 {
194 public:
195     inline QDeclarativePropertyData();
196     inline QDeclarativePropertyData(const QDeclarativePropertyRawData &);
197
198     inline bool operator==(const QDeclarativePropertyRawData &);
199
200     static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
201     void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
202     void load(const QMetaMethod &);
203     QString name(QObject *);
204     QString name(const QMetaObject *);
205
206 private:
207     friend class QDeclarativePropertyCache;
208     void lazyLoad(const QMetaProperty &, QDeclarativeEngine *engine = 0);
209     void lazyLoad(const QMetaMethod &);
210     bool notFullyResolved() const { return flags & NotFullyResolved; }
211 };
212
213 class Q_DECLARATIVE_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
214 {
215 public:
216     QDeclarativePropertyCache(QDeclarativeEngine *);
217     QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
218     virtual ~QDeclarativePropertyCache();
219
220     void update(QDeclarativeEngine *, const QMetaObject *);
221
222     QDeclarativePropertyCache *copy(int reserve = 0);
223     void append(QDeclarativeEngine *, const QMetaObject *,
224                 QDeclarativePropertyData::Flag propertyFlags = QDeclarativePropertyData::NoFlags,
225                 QDeclarativePropertyData::Flag methodFlags = QDeclarativePropertyData::NoFlags,
226                 QDeclarativePropertyData::Flag signalFlags = QDeclarativePropertyData::NoFlags);
227     void append(QDeclarativeEngine *, const QMetaObject *, int revision,
228                 QDeclarativePropertyData::Flag propertyFlags = QDeclarativePropertyData::NoFlags,
229                 QDeclarativePropertyData::Flag methodFlags = QDeclarativePropertyData::NoFlags,
230                 QDeclarativePropertyData::Flag signalFlags = QDeclarativePropertyData::NoFlags);
231
232     inline QDeclarativePropertyData *property(const QHashedV8String &) const;
233     QDeclarativePropertyData *property(const QHashedStringRef &) const;
234     QDeclarativePropertyData *property(const QHashedCStringRef &) const;
235     QDeclarativePropertyData *property(const QString &) const;
236     QDeclarativePropertyData *property(int) const;
237     QDeclarativePropertyData *method(int) const;
238     QStringList propertyNames() const;
239
240     inline QDeclarativePropertyData *overrideData(QDeclarativePropertyData *) const;
241     inline bool isAllowedInRevision(QDeclarativePropertyData *) const;
242
243     inline QDeclarativeEngine *qmlEngine() const;
244     static QDeclarativePropertyData *property(QDeclarativeEngine *, QObject *, const QString &,
245                                               QDeclarativePropertyData &);
246     static QDeclarativePropertyData *property(QDeclarativeEngine *, QObject *, const QHashedV8String &,
247                                               QDeclarativePropertyData &);
248     static int *methodParameterTypes(QObject *, int index, QVarLengthArray<int, 9> &dummy,
249                                      QByteArray *unknownTypeError);
250
251     static bool isDynamicMetaObject(const QMetaObject *);
252 protected:
253     virtual void destroy();
254     virtual void clear();
255
256 private:
257     friend class QDeclarativeEnginePrivate;
258     friend class QV8QObjectWrapper;
259
260     // Implemented in v8/qv8qobjectwrapper.cpp
261     v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
262
263     typedef QVector<QDeclarativePropertyData> IndexCache;
264     typedef QStringHash<QDeclarativePropertyData *> StringCache;
265     typedef QVector<int> AllowedRevisionCache;
266
267     void resolve(QDeclarativePropertyData *) const;
268     void updateRecur(QDeclarativeEngine *, const QMetaObject *);
269
270     QDeclarativeEngine *engine;
271     
272     QDeclarativePropertyCache *parent;
273     int propertyIndexCacheStart;
274     int methodIndexCacheStart;
275
276     IndexCache propertyIndexCache;
277     IndexCache methodIndexCache;
278     IndexCache signalHandlerIndexCache;
279     StringCache stringCache;
280     AllowedRevisionCache allowedRevisionCache;
281     v8::Persistent<v8::Function> constructor;
282
283     const QMetaObject *metaObject;
284     QDeclarativePropertyCacheMethodArguments *argumentsCache;
285 };
286   
287 QDeclarativePropertyData::QDeclarativePropertyData()
288 {
289     propType = 0;
290     coreIndex = -1;
291     notifyIndex = -1;
292     overrideIndexIsProperty = false;
293     overrideIndex = -1;
294     revision = 0;
295     metaObjectOffset = -1; 
296     accessors = 0;
297     accessorData = 0;
298     flags = 0;
299 }
300
301 QDeclarativePropertyData::QDeclarativePropertyData(const QDeclarativePropertyRawData &d)
302 {
303     *(static_cast<QDeclarativePropertyRawData *>(this)) = d;
304 }
305
306 bool QDeclarativePropertyData::operator==(const QDeclarativePropertyRawData &other)
307 {
308     return flags == other.flags &&
309            propType == other.propType &&
310            coreIndex == other.coreIndex &&
311            notifyIndex == other.notifyIndex &&
312            revision == other.revision &&
313            (!isValueTypeVirtual() || 
314             (valueTypeCoreIndex == other.valueTypeCoreIndex && 
315              valueTypePropType == other.valueTypePropType));
316 }
317
318 int QDeclarativePropertyRawData::getValueTypeCoreIndex() const
319 {
320     return isValueTypeVirtual()?valueTypeCoreIndex:-1;
321 }
322
323 QDeclarativePropertyData *
324 QDeclarativePropertyCache::overrideData(QDeclarativePropertyData *data) const
325 {
326     if (!data->hasOverride())
327         return 0;
328
329     if (data->overrideIndexIsProperty)
330         return property(data->overrideIndex);
331     else
332         return method(data->overrideIndex);
333 }
334
335 bool QDeclarativePropertyCache::isAllowedInRevision(QDeclarativePropertyData *data) const
336 {
337     return (data->metaObjectOffset == -1 && data->revision == 0) ||
338            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
339 }
340
341 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
342 {
343     return engine;
344 }
345
346 QDeclarativePropertyData *QDeclarativePropertyCache::property(const QHashedV8String &str) const
347 {
348     QDeclarativePropertyData **rv = stringCache.value(str);
349     if (rv && (*rv)->notFullyResolved()) resolve(*rv);
350     return rv?*rv:0;
351 }
352
353 QT_END_NAMESPACE
354
355 #endif // QDECLARATIVEPROPERTYCACHE_P_H