1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QDECLARATIVEPROPERTYCACHE_P_H
43 #define QDECLARATIVEPROPERTYCACHE_P_H
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.
56 #include <private/qdeclarativerefcount_p.h>
57 #include "qdeclarativecleanup_p.h"
58 #include "qdeclarativenotifier_p.h"
60 #include <private/qhashedstring_p.h>
61 #include <QtCore/qvarlengtharray.h>
62 #include <QtCore/qvector.h>
66 class QDeclarativeEngine;
69 class QV8QObjectWrapper;
70 class QDeclarativePropertyCacheMethodArguments;
72 class Q_DECLARATIVE_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
75 QDeclarativePropertyCache(QDeclarativeEngine *);
76 QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
77 virtual ~QDeclarativePropertyCache();
79 // We have this somewhat aweful split between RawData and Data so that RawData can be
80 // used in unions. In normal code, you should always use Data which initializes RawData
81 // to an invalid state on construction.
86 ValueTypeFlagMask = 0x0000FFFF, // Flags in valueTypeFlags must fit in this mask
88 // Can apply to all properties, except IsFunction
89 IsConstant = 0x00000001, // Has CONST flag
90 IsWritable = 0x00000002, // Has WRITE function
91 IsResettable = 0x00000004, // Has RESET function
92 IsAlias = 0x00000008, // Is a QML alias to another property
93 IsFinal = 0x00000010, // Has FINAL flag
94 IsDirect = 0x00000020, // Exists on a C++ QMetaObject
96 // These are mutualy exclusive
97 IsFunction = 0x00000040, // Is an invokable
98 IsQObjectDerived = 0x00000080, // Property type is a QObject* derived type
99 IsEnumType = 0x00000100, // Property type is an enum
100 IsQList = 0x00000200, // Property type is a QML list
101 IsQmlBinding = 0x00000400, // Property type is a QDeclarativeBinding*
102 IsQJSValue = 0x00000800, // Property type is a QScriptValue
103 IsV8Handle = 0x00001000, // Property type is a QDeclarativeV8Handle
104 IsVMEProperty = 0x00002000, // Property type is a "var" property of VMEMO
105 IsValueTypeVirtual = 0x00004000, // Property is a value type "virtual" property
107 // Apply only to IsFunctions
108 IsVMEFunction = 0x00008000, // Function was added by QML
109 HasArguments = 0x00010000, // Function takes arguments
110 IsSignal = 0x00020000, // Function is a signal
111 IsVMESignal = 0x00040000, // Signal was added by QML
112 IsV8Function = 0x00080000, // Function takes QDeclarativeV8Function* args
113 IsSignalHandler = 0x00100000, // Function is a signal handler
114 IsOverload = 0x00200000, // Function is an overload of another function
116 // Internal QDeclarativePropertyCache flags
117 NotFullyResolved = 0x00400000 // True if the type data is to be lazily resolved
119 Q_DECLARE_FLAGS(Flags, Flag)
121 Flags getFlags() const { return Flag(flags); }
122 void setFlags(Flags f) { flags = f; }
124 bool isValid() const { return coreIndex != -1; }
126 bool isConstant() const { return flags & IsConstant; }
127 bool isWritable() const { return flags & IsWritable; }
128 bool isResettable() const { return flags & IsResettable; }
129 bool isAlias() const { return flags & IsAlias; }
130 bool isFinal() const { return flags & IsFinal; }
131 bool isDirect() const { return flags & IsDirect; }
132 bool isFunction() const { return flags & IsFunction; }
133 bool isQObject() const { return flags & IsQObjectDerived; }
134 bool isEnum() const { return flags & IsEnumType; }
135 bool isQList() const { return flags & IsQList; }
136 bool isQmlBinding() const { return flags & IsQmlBinding; }
137 bool isQJSValue() const { return flags & IsQJSValue; }
138 bool isV8Handle() const { return flags & IsV8Handle; }
139 bool isVMEProperty() const { return flags & IsVMEProperty; }
140 bool isValueTypeVirtual() const { return flags & IsValueTypeVirtual; }
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; }
150 int propType; // When !NotFullyResolved
151 const char *propTypeName; // When NotFullyResolved
155 int notifyIndex; // When !IsFunction
156 void *arguments; // When IsFunction && HasArguments
159 struct { // When !IsValueTypeVirtual
160 uint overrideIndexIsProperty : 1;
161 signed int overrideIndex : 31;
163 struct { // When IsValueTypeVirtual
164 quint16 valueTypeFlags; // flags of the access property on the value type proxy object
165 quint8 valueTypePropType; // The QVariant::Type of access property on the value type
167 quint8 valueTypeCoreIndex; // The prop index of the access property on the value type
172 int metaObjectOffset;
176 friend class QDeclarativePropertyCache;
180 struct Data : public RawData {
182 inline Data(const RawData &);
184 inline bool operator==(const RawData &);
186 static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
187 void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
188 void load(const QMetaMethod &);
189 QString name(QObject *);
190 QString name(const QMetaObject *);
192 // Returns -1 if not a value type virtual property
193 inline int getValueTypeCoreIndex() const;
196 friend class QDeclarativePropertyCache;
197 void lazyLoad(const QMetaProperty &, QDeclarativeEngine *engine = 0);
198 void lazyLoad(const QMetaMethod &);
199 bool notFullyResolved() const { return flags & NotFullyResolved; }
202 void update(QDeclarativeEngine *, const QMetaObject *);
204 QDeclarativePropertyCache *copy(int reserve = 0);
205 void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
206 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
207 void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
208 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
210 static Data create(const QMetaObject *, const QString &);
212 inline Data *property(const QHashedV8String &) const;
213 Data *property(const QHashedStringRef &) const;
214 Data *property(const QHashedCStringRef &) const;
215 Data *property(const QString &) const;
216 Data *property(int) const;
217 Data *method(int) const;
218 QStringList propertyNames() const;
220 inline Data *overrideData(Data *) const;
221 inline bool isAllowedInRevision(Data *) const;
223 inline QDeclarativeEngine *qmlEngine() const;
224 static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
225 static Data *property(QDeclarativeEngine *, QObject *, const QHashedV8String &, Data &);
226 static int *methodParameterTypes(QObject *, int index, QVarLengthArray<int, 9> &dummy,
227 QByteArray *unknownTypeError);
229 static bool isDynamicMetaObject(const QMetaObject *);
231 virtual void destroy();
232 virtual void clear();
235 friend class QDeclarativeEnginePrivate;
236 friend class QV8QObjectWrapper;
238 // Implemented in v8/qv8qobjectwrapper.cpp
239 v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
241 typedef QVector<Data> IndexCache;
242 typedef QStringHash<Data *> StringCache;
243 typedef QVector<int> AllowedRevisionCache;
245 void resolve(Data *) const;
246 void updateRecur(QDeclarativeEngine *, const QMetaObject *);
248 QDeclarativeEngine *engine;
250 QDeclarativePropertyCache *parent;
251 int propertyIndexCacheStart;
252 int methodIndexCacheStart;
254 IndexCache propertyIndexCache;
255 IndexCache methodIndexCache;
256 IndexCache signalHandlerIndexCache;
257 StringCache stringCache;
258 AllowedRevisionCache allowedRevisionCache;
259 v8::Persistent<v8::Function> constructor;
261 const QMetaObject *metaObject;
262 QDeclarativePropertyCacheMethodArguments *argumentsCache;
264 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
266 QDeclarativePropertyCache::Data::Data()
271 overrideIndexIsProperty = false;
274 metaObjectOffset = -1;
278 QDeclarativePropertyCache::Data::Data(const QDeclarativePropertyCache::RawData &d)
280 *(static_cast<RawData *>(this)) = d;
283 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::RawData &other)
285 return flags == other.flags &&
286 propType == other.propType &&
287 coreIndex == other.coreIndex &&
288 notifyIndex == other.notifyIndex &&
289 revision == other.revision &&
290 (!isValueTypeVirtual() ||
291 (valueTypeCoreIndex == other.valueTypeCoreIndex &&
292 valueTypePropType == other.valueTypePropType));
295 int QDeclarativePropertyCache::Data::getValueTypeCoreIndex() const
297 return isValueTypeVirtual()?valueTypeCoreIndex:-1;
300 QDeclarativePropertyCache::Data *
301 QDeclarativePropertyCache::overrideData(Data *data) const
303 if (data->overrideIndex < 0)
306 if (data->overrideIndexIsProperty)
307 return property(data->overrideIndex);
309 return method(data->overrideIndex);
312 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
314 return (data->metaObjectOffset == -1 && data->revision == 0) ||
315 (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
318 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
323 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(const QHashedV8String &str) const
325 QDeclarativePropertyCache::Data **rv = stringCache.value(str);
326 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
332 #endif // QDECLARATIVEPROPERTYCACHE_P_H