1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQMLPROPERTYCACHE_P_H
43 #define QQMLPROPERTYCACHE_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/qqmlrefcount_p.h>
57 #include <private/qflagpointer_p.h>
58 #include "qqmlcleanup_p.h"
59 #include "qqmlnotifier_p.h"
61 #include <private/qhashedstring_p.h>
62 #include <QtCore/qvarlengtharray.h>
63 #include <QtCore/qvector.h>
69 class QV8QObjectWrapper;
71 class QQmlPropertyData;
73 class QMetaObjectBuilder;
74 class QQmlPropertyCacheMethodArguments;
76 // We have this somewhat awful split between RawData and Data so that RawData can be
77 // used in unions. In normal code, you should always use Data which initializes RawData
78 // to an invalid state on construction.
79 class QQmlPropertyRawData
84 ValueTypeFlagMask = 0x0000FFFF, // Flags in valueTypeFlags must fit in this mask
86 // Can apply to all properties, except IsFunction
87 IsConstant = 0x00000001, // Has CONST flag
88 IsWritable = 0x00000002, // Has WRITE function
89 IsResettable = 0x00000004, // Has RESET function
90 IsAlias = 0x00000008, // Is a QML alias to another property
91 IsFinal = 0x00000010, // Has FINAL flag
92 IsDirect = 0x00000020, // Exists on a C++ QMetaObject
93 HasAccessors = 0x00000040, // Has property accessors
95 // These are mutualy exclusive
96 IsFunction = 0x00000080, // Is an invokable
97 IsQObjectDerived = 0x00000100, // Property type is a QObject* derived type
98 IsEnumType = 0x00000200, // Property type is an enum
99 IsQList = 0x00000400, // Property type is a QML list
100 IsQmlBinding = 0x00000800, // Property type is a QQmlBinding*
101 IsQJSValue = 0x00001000, // Property type is a QScriptValue
102 IsV8Handle = 0x00002000, // Property type is a QQmlV8Handle
103 IsVarProperty = 0x00004000, // Property type is a "var" property of VMEMO
104 IsValueTypeVirtual = 0x00008000, // Property is a value type "virtual" property
105 IsQVariant = 0x00010000, // Property is a QVariant
107 // Apply only to IsFunctions
108 IsVMEFunction = 0x00020000, // Function was added by QML
109 HasArguments = 0x00040000, // Function takes arguments
110 IsSignal = 0x00080000, // Function is a signal
111 IsVMESignal = 0x00100000, // Signal was added by QML
112 IsV8Function = 0x00200000, // Function takes QQmlV8Function* args
113 IsSignalHandler = 0x00400000, // Function is a signal handler
114 IsOverload = 0x00800000, // Function is an overload of another function
115 IsCloned = 0x01000000, // The function was marked as cloned
117 // Internal QQmlPropertyCache flags
118 NotFullyResolved = 0x02000000, // True if the type data is to be lazily resolved
120 // Flags that are set based on the propType field
121 PropTypeFlagMask = IsQObjectDerived | IsEnumType | IsQList | IsQmlBinding | IsQJSValue |
122 IsV8Handle | IsQVariant,
124 Q_DECLARE_FLAGS(Flags, Flag)
126 Flags getFlags() const { return Flag(flags); }
127 void setFlags(Flags f) { flags = f; }
129 bool isValid() const { return coreIndex != -1; }
131 bool isConstant() const { return flags & IsConstant; }
132 bool isWritable() const { return flags & IsWritable; }
133 bool isResettable() const { return flags & IsResettable; }
134 bool isAlias() const { return flags & IsAlias; }
135 bool isFinal() const { return flags & IsFinal; }
136 bool isDirect() const { return flags & IsDirect; }
137 bool hasAccessors() const { return flags & HasAccessors; }
138 bool isFunction() const { return flags & IsFunction; }
139 bool isQObject() const { return flags & IsQObjectDerived; }
140 bool isEnum() const { return flags & IsEnumType; }
141 bool isQList() const { return flags & IsQList; }
142 bool isQmlBinding() const { return flags & IsQmlBinding; }
143 bool isQJSValue() const { return flags & IsQJSValue; }
144 bool isV8Handle() const { return flags & IsV8Handle; }
145 bool isVarProperty() const { return flags & IsVarProperty; }
146 bool isValueTypeVirtual() const { return flags & IsValueTypeVirtual; }
147 bool isQVariant() const { return flags & IsQVariant; }
148 bool isVMEFunction() const { return flags & IsVMEFunction; }
149 bool hasArguments() const { return flags & HasArguments; }
150 bool isSignal() const { return flags & IsSignal; }
151 bool isVMESignal() const { return flags & IsVMESignal; }
152 bool isV8Function() const { return flags & IsV8Function; }
153 bool isSignalHandler() const { return flags & IsSignalHandler; }
154 bool isOverload() const { return flags & IsOverload; }
155 bool isCloned() const { return flags & IsCloned; }
157 bool hasOverride() const { return !(flags & IsValueTypeVirtual) &&
158 !(flags & HasAccessors) &&
159 overrideIndex >= 0; }
160 bool hasRevision() const { return !(flags & HasAccessors) && revision != 0; }
162 // Returns -1 if not a value type virtual property
163 inline int getValueTypeCoreIndex() const;
165 // Returns the "encoded" index for use with bindings. Encoding is:
166 // coreIndex | (valueTypeCoreIndex << 24)
167 inline int encodedIndex() const;
170 int propType; // When !NotFullyResolved
171 const char *propTypeName; // When NotFullyResolved
175 // The notify index is in the range returned by QObjectPrivate::signalIndex().
176 // This is different from QMetaMethod::methodIndex().
177 int notifyIndex; // When !IsFunction
178 void *arguments; // When IsFunction && HasArguments
182 struct { // When !HasAccessors
184 qint16 metaObjectOffset;
187 struct { // When IsValueTypeVirtual
188 quint16 valueTypeFlags; // flags of the access property on the value type proxy
190 quint8 valueTypePropType; // The QVariant::Type of access property on the value
192 quint8 valueTypeCoreIndex; // The prop index of the access property on the value
196 struct { // When !IsValueTypeVirtual
197 uint overrideIndexIsProperty : 1;
198 signed int overrideIndex : 31;
202 struct { // When HasAccessors
203 QQmlAccessors *accessors;
204 intptr_t accessorData;
209 friend class QQmlPropertyData;
210 friend class QQmlPropertyCache;
213 Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlPropertyRawData::Flags);
215 class QQmlPropertyData : public QQmlPropertyRawData
218 inline QQmlPropertyData();
219 inline QQmlPropertyData(const QQmlPropertyRawData &);
221 inline bool operator==(const QQmlPropertyRawData &);
223 static Flags flagsForProperty(const QMetaProperty &, QQmlEngine *engine = 0);
224 void load(const QMetaProperty &, QQmlEngine *engine = 0);
225 void load(const QMetaMethod &);
226 QString name(QObject *);
227 QString name(const QMetaObject *);
230 friend class QQmlPropertyCache;
231 void lazyLoad(const QMetaProperty &, QQmlEngine *engine = 0);
232 void lazyLoad(const QMetaMethod &);
233 bool notFullyResolved() const { return flags & NotFullyResolved; }
236 class Q_QML_PRIVATE_EXPORT QQmlPropertyCache : public QQmlRefCount, public QQmlCleanup
239 QQmlPropertyCache(QQmlEngine *);
240 QQmlPropertyCache(QQmlEngine *, const QMetaObject *);
241 virtual ~QQmlPropertyCache();
243 void update(QQmlEngine *, const QMetaObject *);
245 QQmlPropertyCache *copy();
247 QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *,
248 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
249 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
250 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
251 QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *, int revision,
252 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
253 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
254 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
256 QQmlPropertyCache *copyAndReserve(QQmlEngine *, int propertyCount,
257 int methodCount, int signalCount);
258 void appendProperty(const QString &,
259 quint32 flags, int coreIndex, int propType, int notifyIndex);
260 void appendProperty(const QHashedCStringRef &,
261 quint32 flags, int coreIndex, int propType, int notifyIndex);
262 void appendSignal(const QString &, quint32, int coreIndex, const int *types = 0,
263 const QList<QByteArray> &names = QList<QByteArray>());
264 void appendSignal(const QHashedCStringRef &, quint32, int coreIndex, const int *types = 0,
265 const QList<QByteArray> &names = QList<QByteArray>());
266 void appendMethod(const QString &, quint32 flags, int coreIndex,
267 const QList<QByteArray> &names = QList<QByteArray>());
268 void appendMethod(const QHashedCStringRef &, quint32 flags, int coreIndex,
269 const QList<QByteArray> &names = QList<QByteArray>());
271 const QMetaObject *metaObject() const;
272 const QMetaObject *createMetaObject();
273 const QMetaObject *firstCppMetaObject() const;
275 inline QQmlPropertyData *property(const QHashedV8String &) const;
276 QQmlPropertyData *property(const QHashedStringRef &) const;
277 QQmlPropertyData *property(const QHashedCStringRef &) const;
278 QQmlPropertyData *property(const QString &) const;
279 QQmlPropertyData *property(int) const;
280 QQmlPropertyData *method(int) const;
281 QQmlPropertyData *signal(int) const;
282 int methodIndexToSignalIndex(int) const;
283 QStringList propertyNames() const;
285 QString defaultPropertyName() const;
286 QQmlPropertyData *defaultProperty() const;
287 QQmlPropertyCache *parent() const;
289 inline QQmlPropertyData *overrideData(QQmlPropertyData *) const;
290 inline bool isAllowedInRevision(QQmlPropertyData *) const;
292 inline QQmlEngine *qmlEngine() const;
293 static QQmlPropertyData *property(QQmlEngine *, QObject *, const QString &,
295 static QQmlPropertyData *property(QQmlEngine *, QObject *, const QHashedV8String &,
297 static int *methodParameterTypes(QObject *, int index, QVarLengthArray<int, 9> &dummy,
298 QByteArray *unknownTypeError);
299 static QList<QByteArray> signalParameterNames(QObject *, int index);
301 const char *className() const;
303 inline int propertyCount() const;
304 inline int propertyOffset() const;
305 inline int methodCount() const;
306 inline int methodOffset() const;
307 inline int signalCount() const;
308 inline int signalOffset() const;
310 static bool isDynamicMetaObject(const QMetaObject *);
312 void toMetaObjectBuilder(QMetaObjectBuilder &);
315 virtual void destroy();
316 virtual void clear();
319 friend class QQmlEnginePrivate;
320 friend class QV8QObjectWrapper;
321 friend class QQmlCompiler;
323 inline QQmlPropertyCache *copy(int reserve);
325 void append(QQmlEngine *, const QMetaObject *, int revision,
326 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
327 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
328 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
330 // Implemented in v8/qv8qobjectwrapper.cpp
331 v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
333 typedef QVector<QQmlPropertyData> IndexCache;
334 typedef QStringHash<QQmlPropertyData *> StringCache;
335 typedef QVector<int> AllowedRevisionCache;
337 void resolve(QQmlPropertyData *) const;
338 void updateRecur(QQmlEngine *, const QMetaObject *);
342 QQmlPropertyCache *_parent;
343 int propertyIndexCacheStart;
344 int methodIndexCacheStart;
345 int signalHandlerIndexCacheStart;
347 IndexCache propertyIndexCache;
348 IndexCache methodIndexCache;
349 IndexCache signalHandlerIndexCache;
350 StringCache stringCache;
351 AllowedRevisionCache allowedRevisionCache;
352 v8::Persistent<v8::Function> constructor;
355 const QMetaObject *_metaObject;
356 QByteArray _dynamicClassName;
357 QByteArray _dynamicStringData;
358 QString _defaultPropertyName;
359 QQmlPropertyCacheMethodArguments *argumentsCache;
362 // QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
363 // This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
364 // we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
365 // QObject type used in assignment or when we don't have a QQmlEngine etc.
367 // This class does NOT reference the propertycache.
368 class QQmlEnginePrivate;
369 class Q_QML_EXPORT QQmlMetaObject
372 inline QQmlMetaObject();
373 inline QQmlMetaObject(QObject *);
374 inline QQmlMetaObject(const QMetaObject *);
375 inline QQmlMetaObject(QQmlPropertyCache *);
376 inline QQmlMetaObject(const QQmlMetaObject &);
378 inline QQmlMetaObject &operator=(const QQmlMetaObject &);
380 inline bool isNull() const;
382 inline const char *className() const;
383 inline int propertyCount() const;
385 inline bool hasMetaObject() const;
386 inline const QMetaObject *metaObject() const;
388 QQmlPropertyCache *propertyCache(QQmlEnginePrivate *) const;
390 static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to);
393 QBiPointer<QQmlPropertyCache, const QMetaObject> _m;
396 QQmlPropertyData::QQmlPropertyData()
401 overrideIndexIsProperty = false;
404 metaObjectOffset = -1;
408 QQmlPropertyData::QQmlPropertyData(const QQmlPropertyRawData &d)
410 *(static_cast<QQmlPropertyRawData *>(this)) = d;
413 bool QQmlPropertyData::operator==(const QQmlPropertyRawData &other)
415 return flags == other.flags &&
416 propType == other.propType &&
417 coreIndex == other.coreIndex &&
418 notifyIndex == other.notifyIndex &&
419 revision == other.revision &&
420 (!isValueTypeVirtual() ||
421 (valueTypeCoreIndex == other.valueTypeCoreIndex &&
422 valueTypePropType == other.valueTypePropType));
425 int QQmlPropertyRawData::getValueTypeCoreIndex() const
427 return isValueTypeVirtual()?valueTypeCoreIndex:-1;
430 int QQmlPropertyRawData::encodedIndex() const
432 return isValueTypeVirtual()?(coreIndex | (valueTypeCoreIndex << 24)):coreIndex;
436 QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
438 if (!data->hasOverride())
441 if (data->overrideIndexIsProperty)
442 return property(data->overrideIndex);
444 return method(data->overrideIndex);
447 bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
449 return (data->hasAccessors() || (data->metaObjectOffset == -1 && data->revision == 0)) ||
450 (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
453 QQmlEngine *QQmlPropertyCache::qmlEngine() const
458 QQmlPropertyData *QQmlPropertyCache::property(const QHashedV8String &str) const
460 QQmlPropertyData **rv = stringCache.value(str);
461 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
465 int QQmlPropertyCache::propertyCount() const
467 return propertyIndexCacheStart + propertyIndexCache.count();
470 int QQmlPropertyCache::propertyOffset() const
472 return propertyIndexCacheStart;
475 int QQmlPropertyCache::methodCount() const
477 return methodIndexCacheStart + methodIndexCache.count();
480 int QQmlPropertyCache::methodOffset() const
482 return methodIndexCacheStart;
485 int QQmlPropertyCache::signalCount() const
487 return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
490 int QQmlPropertyCache::signalOffset() const
492 return signalHandlerIndexCacheStart;
495 QQmlMetaObject::QQmlMetaObject()
499 QQmlMetaObject::QQmlMetaObject(QObject *o)
502 QQmlData *ddata = QQmlData::get(o, false);
503 if (ddata && ddata->propertyCache) _m = ddata->propertyCache;
504 else _m = o->metaObject();
508 QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
513 QQmlMetaObject::QQmlMetaObject(QQmlPropertyCache *m)
518 QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
523 QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
529 bool QQmlMetaObject::isNull() const
534 const char *QQmlMetaObject::className() const
538 } else if (_m.isT1()) {
539 return _m.asT1()->className();
541 return _m.asT2()->className();
545 int QQmlMetaObject::propertyCount() const
549 } else if (_m.isT1()) {
550 return _m.asT1()->propertyCount();
552 return _m.asT2()->propertyCount();
556 bool QQmlMetaObject::hasMetaObject() const
558 return _m.isT2() || (!_m.isNull() && _m.asT1()->metaObject());
561 const QMetaObject *QQmlMetaObject::metaObject() const
563 if (_m.isNull()) return 0;
564 if (_m.isT1()) return _m.asT1()->createMetaObject();
565 else return _m.asT2();
570 #endif // QQMLPROPERTYCACHE_P_H