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 int methodReturnType(QObject *, const QQmlPropertyData &data,
300 QByteArray *unknownTypeError);
301 static QList<QByteArray> signalParameterNames(QObject *, int index);
303 const char *className() const;
305 inline int propertyCount() const;
306 inline int propertyOffset() const;
307 inline int methodCount() const;
308 inline int methodOffset() const;
309 inline int signalCount() const;
310 inline int signalOffset() const;
312 static bool isDynamicMetaObject(const QMetaObject *);
314 void toMetaObjectBuilder(QMetaObjectBuilder &);
317 virtual void destroy();
318 virtual void clear();
321 friend class QQmlEnginePrivate;
322 friend class QV8QObjectWrapper;
323 friend class QQmlCompiler;
325 inline QQmlPropertyCache *copy(int reserve);
327 void append(QQmlEngine *, const QMetaObject *, int revision,
328 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
329 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
330 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
332 // Implemented in v8/qv8qobjectwrapper.cpp
333 v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
335 typedef QVector<QQmlPropertyData> IndexCache;
336 typedef QStringHash<QQmlPropertyData *> StringCache;
337 typedef QVector<int> AllowedRevisionCache;
339 void resolve(QQmlPropertyData *) const;
340 void updateRecur(QQmlEngine *, const QMetaObject *);
344 QQmlPropertyCache *_parent;
345 int propertyIndexCacheStart;
346 int methodIndexCacheStart;
347 int signalHandlerIndexCacheStart;
349 IndexCache propertyIndexCache;
350 IndexCache methodIndexCache;
351 IndexCache signalHandlerIndexCache;
352 StringCache stringCache;
353 AllowedRevisionCache allowedRevisionCache;
354 v8::Persistent<v8::Function> constructor;
357 const QMetaObject *_metaObject;
358 QByteArray _dynamicClassName;
359 QByteArray _dynamicStringData;
360 QString _defaultPropertyName;
361 QQmlPropertyCacheMethodArguments *argumentsCache;
364 // QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
365 // This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
366 // we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
367 // QObject type used in assignment or when we don't have a QQmlEngine etc.
369 // This class does NOT reference the propertycache.
370 class QQmlEnginePrivate;
371 class Q_QML_EXPORT QQmlMetaObject
374 inline QQmlMetaObject();
375 inline QQmlMetaObject(QObject *);
376 inline QQmlMetaObject(const QMetaObject *);
377 inline QQmlMetaObject(QQmlPropertyCache *);
378 inline QQmlMetaObject(const QQmlMetaObject &);
380 inline QQmlMetaObject &operator=(const QQmlMetaObject &);
382 inline bool isNull() const;
384 inline const char *className() const;
385 inline int propertyCount() const;
387 inline bool hasMetaObject() const;
388 inline const QMetaObject *metaObject() const;
390 QQmlPropertyCache *propertyCache(QQmlEnginePrivate *) const;
392 static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to);
395 QBiPointer<QQmlPropertyCache, const QMetaObject> _m;
398 QQmlPropertyData::QQmlPropertyData()
403 overrideIndexIsProperty = false;
406 metaObjectOffset = -1;
410 QQmlPropertyData::QQmlPropertyData(const QQmlPropertyRawData &d)
412 *(static_cast<QQmlPropertyRawData *>(this)) = d;
415 bool QQmlPropertyData::operator==(const QQmlPropertyRawData &other)
417 return flags == other.flags &&
418 propType == other.propType &&
419 coreIndex == other.coreIndex &&
420 notifyIndex == other.notifyIndex &&
421 revision == other.revision &&
422 (!isValueTypeVirtual() ||
423 (valueTypeCoreIndex == other.valueTypeCoreIndex &&
424 valueTypePropType == other.valueTypePropType));
427 int QQmlPropertyRawData::getValueTypeCoreIndex() const
429 return isValueTypeVirtual()?valueTypeCoreIndex:-1;
432 int QQmlPropertyRawData::encodedIndex() const
434 return isValueTypeVirtual()?(coreIndex | (valueTypeCoreIndex << 24)):coreIndex;
438 QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
440 if (!data->hasOverride())
443 if (data->overrideIndexIsProperty)
444 return property(data->overrideIndex);
446 return method(data->overrideIndex);
449 bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
451 return (data->hasAccessors() || (data->metaObjectOffset == -1 && data->revision == 0)) ||
452 (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
455 QQmlEngine *QQmlPropertyCache::qmlEngine() const
460 QQmlPropertyData *QQmlPropertyCache::property(const QHashedV8String &str) const
462 QQmlPropertyData **rv = stringCache.value(str);
463 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
467 int QQmlPropertyCache::propertyCount() const
469 return propertyIndexCacheStart + propertyIndexCache.count();
472 int QQmlPropertyCache::propertyOffset() const
474 return propertyIndexCacheStart;
477 int QQmlPropertyCache::methodCount() const
479 return methodIndexCacheStart + methodIndexCache.count();
482 int QQmlPropertyCache::methodOffset() const
484 return methodIndexCacheStart;
487 int QQmlPropertyCache::signalCount() const
489 return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
492 int QQmlPropertyCache::signalOffset() const
494 return signalHandlerIndexCacheStart;
497 QQmlMetaObject::QQmlMetaObject()
501 QQmlMetaObject::QQmlMetaObject(QObject *o)
504 QQmlData *ddata = QQmlData::get(o, false);
505 if (ddata && ddata->propertyCache) _m = ddata->propertyCache;
506 else _m = o->metaObject();
510 QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
515 QQmlMetaObject::QQmlMetaObject(QQmlPropertyCache *m)
520 QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
525 QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
531 bool QQmlMetaObject::isNull() const
536 const char *QQmlMetaObject::className() const
540 } else if (_m.isT1()) {
541 return _m.asT1()->className();
543 return _m.asT2()->className();
547 int QQmlMetaObject::propertyCount() const
551 } else if (_m.isT1()) {
552 return _m.asT1()->propertyCount();
554 return _m.asT2()->propertyCount();
558 bool QQmlMetaObject::hasMetaObject() const
560 return _m.isT2() || (!_m.isNull() && _m.asT1()->metaObject());
563 const QMetaObject *QQmlMetaObject::metaObject() const
565 if (_m.isNull()) return 0;
566 if (_m.isT1()) return _m.asT1()->createMetaObject();
567 else return _m.asT2();
572 #endif // QQMLPROPERTYCACHE_P_H