Optimize QDeclarativePropertyCache
[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 "private/qdeclarativecleanup_p.h"
58 #include "private/qdeclarativenotifier_p.h"
59
60 #include "private/qhashedstring_p.h"
61 #include <QtCore/qvector.h>
62
63 #include <QtScript/private/qscriptdeclarativeclass_p.h>
64 QT_BEGIN_NAMESPACE
65
66 class QDeclarativeEngine;
67 class QMetaProperty;
68 class QV8Engine;
69 class QV8QObjectWrapper;
70
71 class Q_AUTOTEST_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
72 {
73 public:
74     QDeclarativePropertyCache(QDeclarativeEngine *);
75     QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
76     virtual ~QDeclarativePropertyCache();
77
78     struct Data {
79         inline Data(); 
80         inline bool operator==(const Data &);
81
82         enum Flag { 
83                     NoFlags           = 0x00000000,
84
85                     // Can apply to all properties, except IsFunction
86                     IsConstant        = 0x00000001, // Has CONST flag
87                     IsWritable        = 0x00000002, // Has WRITE function
88                     IsResettable      = 0x00000004, // Has RESET function
89                     IsAlias           = 0x00000008, // Is a QML alias to another property
90                     IsFinal           = 0x00000010, // Has FINAL flag
91                     IsDirect          = 0x00000020, // Exists on a C++ QMetaObject
92
93                     // These are mutualy exclusive
94                     IsFunction        = 0x00000040, // Is an invokable
95                     IsQObjectDerived  = 0x00000080, // Property type is a QObject* derived type
96                     IsEnumType        = 0x00000100, // Property type is an enum
97                     IsQList           = 0x00000200, // Property type is a QML list
98                     IsQmlBinding      = 0x00000400, // Property type is a QDeclarativeBinding*
99                     IsQScriptValue    = 0x00000800, // Property type is a QScriptValue
100                     IsV8Handle        = 0x00001000, // Property type is a QDeclarativeV8Handle
101
102                     // Apply only to IsFunctions
103                     IsVMEFunction     = 0x00002000, // Function was added by QML
104                     HasArguments      = 0x00004000, // Function takes arguments
105                     IsSignal          = 0x00008000, // Function is a signal
106                     IsVMESignal       = 0x00010000, // Signal was added by QML
107                     IsV8Function      = 0x00020000, // Function takes QDeclarativeV8Function* args
108
109                     // Internal QDeclarativePropertyCache flags
110                     NotFullyResolved  = 0x00040000  // True if the type data is to be lazily resolved 
111         };
112         Q_DECLARE_FLAGS(Flags, Flag)
113
114         Flags getFlags() const { return flags; }
115         void setFlags(Flags f) { flags = f; }
116
117         bool isValid() const { return coreIndex != -1; } 
118
119         bool isConstant() const { return flags & IsConstant; }
120         bool isWritable() const { return flags & IsWritable; }
121         bool isResettable() const { return flags & IsResettable; }
122         bool isAlias() const { return flags & IsAlias; }
123         bool isFinal() const { return flags & IsFinal; }
124         bool isDirect() const { return flags & IsDirect; }
125         bool isFunction() const { return flags & IsFunction; }
126         bool isQObject() const { return flags & IsQObjectDerived; }
127         bool isEnum() const { return flags & IsEnumType; }
128         bool isQList() const { return flags & IsQList; }
129         bool isQmlBinding() const { return flags & IsQmlBinding; }
130         bool isQScriptValue() const { return flags & IsQScriptValue; }
131         bool isV8Handle() const { return flags & IsV8Handle; }
132         bool isVMEFunction() const { return flags & IsVMEFunction; }
133         bool hasArguments() const { return flags & HasArguments; }
134         bool isSignal() const { return flags & IsSignal; }
135         bool isVMESignal() const { return flags & IsVMESignal; }
136         bool isV8Function() const { return flags & IsV8Function; }
137
138         union {
139             int propType;             // When !NotFullyResolved
140             const char *propTypeName; // When NotFullyResolved
141         };
142         int coreIndex;
143         union {
144             int notifyIndex; // When !IsFunction
145             int relatedIndex; // When IsFunction
146         };
147         uint overrideIndexIsProperty : 1;
148         int overrideIndex : 31;
149         int revision; 
150         int metaObjectOffset;
151
152         static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
153         void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
154         void load(const QMetaMethod &);
155         QString name(QObject *);
156         QString name(const QMetaObject *);
157
158     private:
159         void lazyLoad(const QMetaProperty &, QDeclarativeEngine *engine = 0);
160         void lazyLoad(const QMetaMethod &);
161         bool notFullyResolved() const { return flags & NotFullyResolved; }
162         friend class QDeclarativePropertyCache;
163         Flags flags;
164     };
165
166     struct ValueTypeData {
167         inline ValueTypeData();
168         inline bool operator==(const ValueTypeData &);
169         Data::Flags flags;     // flags of the access property on the value type proxy object
170         int valueTypeCoreIdx;  // The prop index of the access property on the value type proxy object
171         int valueTypePropType; // The QVariant::Type of access property on the value type proxy object
172     };
173
174     void update(QDeclarativeEngine *, const QMetaObject *);
175
176     QDeclarativePropertyCache *copy();
177     void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
178                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
179     void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
180                 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
181
182     static Data create(const QMetaObject *, const QString &);
183
184     inline Data *property(const QHashedV8String &) const;
185     Data *property(const QString &) const;
186     Data *property(int) const;
187     Data *method(int) const;
188     QStringList propertyNames() const;
189
190     inline Data *overrideData(Data *) const;
191     inline bool isAllowedInRevision(Data *) const;
192
193     inline QDeclarativeEngine *qmlEngine() const;
194     static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
195     static Data *property(QDeclarativeEngine *, QObject *, const QHashedV8String &, Data &);
196
197     static bool isDynamicMetaObject(const QMetaObject *);
198 protected:
199     virtual void clear();
200
201 private:
202     friend class QDeclarativeEnginePrivate;
203     friend class QV8QObjectWrapper;
204
205     // Implemented in v8/qv8qobjectwrapper.cpp
206     v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
207
208     typedef QVector<Data> IndexCache;
209     typedef QStringHash<Data *> StringCache;
210     typedef QVector<int> AllowedRevisionCache;
211
212     void resolve(Data *) const;
213     void updateRecur(QDeclarativeEngine *, const QMetaObject *);
214
215     QDeclarativeEngine *engine;
216     
217     QDeclarativePropertyCache *parent;
218     int propertyIndexCacheStart;
219     int methodIndexCacheStart;
220
221     IndexCache propertyIndexCache;
222     IndexCache methodIndexCache;
223     StringCache stringCache;
224     AllowedRevisionCache allowedRevisionCache;
225     v8::Persistent<v8::Function> constructor;
226 };
227 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
228   
229 QDeclarativePropertyCache::Data::Data()
230 : propType(0), coreIndex(-1), notifyIndex(-1), overrideIndexIsProperty(false), overrideIndex(-1),
231   revision(0), metaObjectOffset(-1), flags(0)
232 {
233 }
234
235 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::Data &other)
236 {
237     return flags == other.flags &&
238            propType == other.propType &&
239            coreIndex == other.coreIndex &&
240            notifyIndex == other.notifyIndex &&
241            revision == other.revision;
242 }
243
244 QDeclarativePropertyCache::Data *
245 QDeclarativePropertyCache::overrideData(Data *data) const
246 {
247     if (data->overrideIndex < 0)
248         return 0;
249
250     if (data->overrideIndexIsProperty)
251         return property(data->overrideIndex);
252     else
253         return method(data->overrideIndex);
254 }
255
256 QDeclarativePropertyCache::ValueTypeData::ValueTypeData()
257 : flags(QDeclarativePropertyCache::Data::NoFlags), valueTypeCoreIdx(-1), valueTypePropType(0) 
258 {
259 }
260
261 bool QDeclarativePropertyCache::ValueTypeData::operator==(const ValueTypeData &o) 
262
263     return flags == o.flags &&
264            valueTypeCoreIdx == o.valueTypeCoreIdx &&
265            valueTypePropType == o.valueTypePropType; 
266 }
267
268 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
269 {
270     return (data->metaObjectOffset == -1 && data->revision == 0) ||
271            (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
272 }
273
274 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
275 {
276     return engine;
277 }
278
279 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(const QHashedV8String &str) const
280 {
281     QDeclarativePropertyCache::Data **rv = stringCache.value(str);
282     if (rv && (*rv)->notFullyResolved()) resolve(*rv);
283     return rv?*rv:0;
284 }
285
286 QT_END_NAMESPACE
287
288 #endif // QDECLARATIVEPROPERTYCACHE_P_H