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;
75 class QQmlVMEMetaObject;
77 // We have this somewhat awful split between RawData and Data so that RawData can be
78 // used in unions. In normal code, you should always use Data which initializes RawData
79 // to an invalid state on construction.
80 class QQmlPropertyRawData
85 ValueTypeFlagMask = 0x0000FFFF, // Flags in valueTypeFlags must fit in this mask
87 // Can apply to all properties, except IsFunction
88 IsConstant = 0x00000001, // Has CONST flag
89 IsWritable = 0x00000002, // Has WRITE function
90 IsResettable = 0x00000004, // Has RESET function
91 IsAlias = 0x00000008, // Is a QML alias to another property
92 IsFinal = 0x00000010, // Has FINAL flag
93 IsOverridden = 0x00000020, // Is overridden by a extension property
94 IsDirect = 0x00000040, // Exists on a C++ QMetaObject
95 HasAccessors = 0x00000080, // Has property accessors
97 // These are mutualy exclusive
98 IsFunction = 0x00000100, // Is an invokable
99 IsQObjectDerived = 0x00000200, // Property type is a QObject* derived type
100 IsEnumType = 0x00000400, // Property type is an enum
101 IsQList = 0x00000800, // Property type is a QML list
102 IsQmlBinding = 0x00001000, // Property type is a QQmlBinding*
103 IsQJSValue = 0x00002000, // Property type is a QScriptValue
104 IsV8Handle = 0x00004000, // Property type is a QQmlV8Handle
105 IsVarProperty = 0x00008000, // Property type is a "var" property of VMEMO
106 IsValueTypeVirtual = 0x00010000, // Property is a value type "virtual" property
107 IsQVariant = 0x00020000, // Property is a QVariant
109 // Apply only to IsFunctions
110 IsVMEFunction = 0x00040000, // Function was added by QML
111 HasArguments = 0x00080000, // Function takes arguments
112 IsSignal = 0x00100000, // Function is a signal
113 IsVMESignal = 0x00200000, // Signal was added by QML
114 IsV8Function = 0x00400000, // Function takes QQmlV8Function* args
115 IsSignalHandler = 0x00800000, // Function is a signal handler
116 IsOverload = 0x01000000, // Function is an overload of another function
117 IsCloned = 0x02000000, // The function was marked as cloned
119 // Internal QQmlPropertyCache flags
120 NotFullyResolved = 0x04000000, // True if the type data is to be lazily resolved
122 // Flags that are set based on the propType field
123 PropTypeFlagMask = IsQObjectDerived | IsEnumType | IsQList | IsQmlBinding | IsQJSValue |
124 IsV8Handle | IsQVariant,
126 Q_DECLARE_FLAGS(Flags, Flag)
128 Flags getFlags() const { return Flag(flags); }
129 void setFlags(Flags f) { flags = f; }
131 bool isValid() const { return coreIndex != -1; }
133 bool isConstant() const { return flags & IsConstant; }
134 bool isWritable() const { return flags & IsWritable; }
135 bool isResettable() const { return flags & IsResettable; }
136 bool isAlias() const { return flags & IsAlias; }
137 bool isFinal() const { return flags & IsFinal; }
138 bool isOverridden() const { return flags & IsOverridden; }
139 bool isDirect() const { return flags & IsDirect; }
140 bool hasAccessors() const { return flags & HasAccessors; }
141 bool isFunction() const { return flags & IsFunction; }
142 bool isQObject() const { return flags & IsQObjectDerived; }
143 bool isEnum() const { return flags & IsEnumType; }
144 bool isQList() const { return flags & IsQList; }
145 bool isQmlBinding() const { return flags & IsQmlBinding; }
146 bool isQJSValue() const { return flags & IsQJSValue; }
147 bool isV8Handle() const { return flags & IsV8Handle; }
148 bool isVarProperty() const { return flags & IsVarProperty; }
149 bool isValueTypeVirtual() const { return flags & IsValueTypeVirtual; }
150 bool isQVariant() const { return flags & IsQVariant; }
151 bool isVMEFunction() const { return flags & IsVMEFunction; }
152 bool hasArguments() const { return flags & HasArguments; }
153 bool isSignal() const { return flags & IsSignal; }
154 bool isVMESignal() const { return flags & IsVMESignal; }
155 bool isV8Function() const { return flags & IsV8Function; }
156 bool isSignalHandler() const { return flags & IsSignalHandler; }
157 bool isOverload() const { return flags & IsOverload; }
158 bool isCloned() const { return flags & IsCloned; }
160 bool hasOverride() const { return !(flags & IsValueTypeVirtual) &&
161 !(flags & HasAccessors) &&
162 overrideIndex >= 0; }
163 bool hasRevision() const { return !(flags & HasAccessors) && revision != 0; }
165 // Returns -1 if not a value type virtual property
166 inline int getValueTypeCoreIndex() const;
168 // Returns the "encoded" index for use with bindings. Encoding is:
169 // coreIndex | (valueTypeCoreIndex << 16)
170 inline int encodedIndex() const;
173 int propType; // When !NotFullyResolved
174 const char *propTypeName; // When NotFullyResolved
178 // The notify index is in the range returned by QObjectPrivate::signalIndex().
179 // This is different from QMetaMethod::methodIndex().
180 int notifyIndex; // When !IsFunction
181 void *arguments; // When IsFunction && HasArguments
185 struct { // When !HasAccessors
187 qint16 metaObjectOffset;
190 struct { // When IsValueTypeVirtual
191 quint16 valueTypeFlags; // flags of the access property on the value type proxy
193 quint16 valueTypePropType; // The QVariant::Type of access property on the value
195 quint16 valueTypeCoreIndex; // The prop index of the access property on the value
199 struct { // When !IsValueTypeVirtual
200 uint overrideIndexIsProperty : 1;
201 signed int overrideIndex : 31;
205 struct { // When HasAccessors
206 QQmlAccessors *accessors;
207 intptr_t accessorData;
212 friend class QQmlPropertyData;
213 friend class QQmlPropertyCache;
216 Q_DECLARE_OPERATORS_FOR_FLAGS(QQmlPropertyRawData::Flags);
218 class QQmlPropertyData : public QQmlPropertyRawData
221 inline QQmlPropertyData();
222 inline QQmlPropertyData(const QQmlPropertyRawData &);
224 inline bool operator==(const QQmlPropertyRawData &);
226 static Flags flagsForProperty(const QMetaProperty &, QQmlEngine *engine = 0);
227 void load(const QMetaProperty &, QQmlEngine *engine = 0);
228 void load(const QMetaMethod &);
229 QString name(QObject *);
230 QString name(const QMetaObject *);
232 void markAsOverrideOf(QQmlPropertyData *predecessor);
235 friend class QQmlPropertyCache;
236 void lazyLoad(const QMetaProperty &, QQmlEngine *engine = 0);
237 void lazyLoad(const QMetaMethod &);
238 bool notFullyResolved() const { return flags & NotFullyResolved; }
241 class QQmlPropertyCacheMethodArguments;
242 class Q_QML_PRIVATE_EXPORT QQmlPropertyCache : public QQmlRefCount, public QQmlCleanup
245 QQmlPropertyCache(QQmlEngine *);
246 QQmlPropertyCache(QQmlEngine *, const QMetaObject *);
247 virtual ~QQmlPropertyCache();
249 void update(QQmlEngine *, const QMetaObject *);
251 QQmlPropertyCache *copy();
253 QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *,
254 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
255 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
256 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
257 QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *, int revision,
258 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
259 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
260 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
262 QQmlPropertyCache *copyAndReserve(QQmlEngine *, int propertyCount,
263 int methodCount, int signalCount);
264 void appendProperty(const QString &,
265 quint32 flags, int coreIndex, int propType, int notifyIndex);
266 void appendProperty(const QHashedCStringRef &,
267 quint32 flags, int coreIndex, int propType, int notifyIndex);
268 void appendSignal(const QString &, quint32, int coreIndex, const int *types = 0,
269 const QList<QByteArray> &names = QList<QByteArray>());
270 void appendSignal(const QHashedCStringRef &, quint32, int coreIndex, const int *types = 0,
271 const QList<QByteArray> &names = QList<QByteArray>());
272 void appendMethod(const QString &, quint32 flags, int coreIndex,
273 const QList<QByteArray> &names = QList<QByteArray>());
274 void appendMethod(const QHashedCStringRef &, quint32 flags, int coreIndex,
275 const QList<QByteArray> &names = QList<QByteArray>());
277 const QMetaObject *metaObject() const;
278 const QMetaObject *createMetaObject();
279 const QMetaObject *firstCppMetaObject() const;
282 QQmlPropertyData *property(const K &key, QObject *object, QQmlContextData *context) const
284 return findProperty(stringCache.find(key), object, context);
287 QQmlPropertyData *property(int) const;
288 QQmlPropertyData *method(int) const;
289 QQmlPropertyData *signal(int index) const { return signal(index, 0); }
290 int methodIndexToSignalIndex(int) const;
291 QStringList propertyNames() const;
293 QString defaultPropertyName() const;
294 QQmlPropertyData *defaultProperty() const;
295 QQmlPropertyCache *parent() const;
297 inline QQmlPropertyData *overrideData(QQmlPropertyData *) const;
298 inline bool isAllowedInRevision(QQmlPropertyData *) const;
300 inline QQmlEngine *qmlEngine() const;
301 static QQmlPropertyData *property(QQmlEngine *, QObject *, const QString &,
302 QQmlContextData *, QQmlPropertyData &);
303 static QQmlPropertyData *property(QQmlEngine *, QObject *, const QHashedV8String &,
304 QQmlContextData *, QQmlPropertyData &);
305 static int *methodParameterTypes(QObject *, int index, QVarLengthArray<int, 9> &dummy,
306 QByteArray *unknownTypeError);
307 static int methodReturnType(QObject *, const QQmlPropertyData &data,
308 QByteArray *unknownTypeError);
310 //see QMetaObjectPrivate::originalClone
311 int originalClone(int index);
312 static int originalClone(QObject *, int index);
314 QList<QByteArray> signalParameterNames(int index) const;
315 QString signalParameterStringForJS(int index, int *count = 0, QString *errorString = 0);
317 const char *className() const;
319 inline int propertyCount() const;
320 inline int propertyOffset() const;
321 inline int methodCount() const;
322 inline int methodOffset() const;
323 inline int signalCount() const;
324 inline int signalOffset() const;
326 static bool isDynamicMetaObject(const QMetaObject *);
328 void toMetaObjectBuilder(QMetaObjectBuilder &);
331 virtual void destroy();
332 virtual void clear();
335 friend class QQmlEnginePrivate;
336 friend class QV8QObjectWrapper;
337 friend class QQmlCompiler;
339 inline QQmlPropertyCache *copy(int reserve);
341 void append(QQmlEngine *, const QMetaObject *, int revision,
342 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
343 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
344 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
346 // Implemented in v8/qv8qobjectwrapper.cpp
347 v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
349 QQmlPropertyCacheMethodArguments *createArgumentsObject(int count,
350 const QList<QByteArray> &names = QList<QByteArray>());
351 QQmlPropertyData *signal(int, QQmlPropertyCache **) const;
353 typedef QVector<QQmlPropertyData> IndexCache;
354 typedef QStringMultiHash<QPair<int, QQmlPropertyData *> > StringCache;
355 typedef QVector<int> AllowedRevisionCache;
357 QQmlPropertyData *findProperty(StringCache::ConstIterator it, QObject *, QQmlContextData *) const;
358 QQmlPropertyData *findProperty(StringCache::ConstIterator it, const QQmlVMEMetaObject *, QQmlContextData *) const;
360 QQmlPropertyData *ensureResolved(QQmlPropertyData*) const;
362 void resolve(QQmlPropertyData *) const;
363 void updateRecur(QQmlEngine *, const QMetaObject *);
366 QQmlPropertyData *findNamedProperty(const K &key)
368 StringCache::mapped_type *it = stringCache.value(key);
369 return it ? it->second : 0;
373 void setNamedProperty(const K &key, int index, QQmlPropertyData *data, bool isOverride)
375 stringCache.insert(key, qMakePair(index, data));
376 _hasPropertyOverrides |= isOverride;
381 QQmlPropertyCache *_parent;
382 int propertyIndexCacheStart;
383 int methodIndexCacheStart;
384 int signalHandlerIndexCacheStart;
386 IndexCache propertyIndexCache;
387 IndexCache methodIndexCache;
388 IndexCache signalHandlerIndexCache;
389 StringCache stringCache;
390 AllowedRevisionCache allowedRevisionCache;
391 v8::Persistent<v8::Function> constructor;
393 bool _hasPropertyOverrides : 1;
394 bool _ownMetaObject : 1;
395 const QMetaObject *_metaObject;
396 QByteArray _dynamicClassName;
397 QByteArray _dynamicStringData;
398 QString _defaultPropertyName;
399 QQmlPropertyCacheMethodArguments *argumentsCache;
402 // QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
403 // This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
404 // we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
405 // QObject type used in assignment or when we don't have a QQmlEngine etc.
407 // This class does NOT reference the propertycache.
408 class QQmlEnginePrivate;
409 class Q_QML_EXPORT QQmlMetaObject
412 inline QQmlMetaObject();
413 inline QQmlMetaObject(QObject *);
414 inline QQmlMetaObject(const QMetaObject *);
415 inline QQmlMetaObject(QQmlPropertyCache *);
416 inline QQmlMetaObject(const QQmlMetaObject &);
418 inline QQmlMetaObject &operator=(const QQmlMetaObject &);
420 inline bool isNull() const;
422 inline const char *className() const;
423 inline int propertyCount() const;
425 inline bool hasMetaObject() const;
426 inline const QMetaObject *metaObject() const;
428 QQmlPropertyCache *propertyCache(QQmlEnginePrivate *) const;
430 static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to);
433 QBiPointer<QQmlPropertyCache, const QMetaObject> _m;
436 QQmlPropertyData::QQmlPropertyData()
441 overrideIndexIsProperty = false;
444 metaObjectOffset = -1;
448 QQmlPropertyData::QQmlPropertyData(const QQmlPropertyRawData &d)
450 *(static_cast<QQmlPropertyRawData *>(this)) = d;
453 bool QQmlPropertyData::operator==(const QQmlPropertyRawData &other)
455 return flags == other.flags &&
456 propType == other.propType &&
457 coreIndex == other.coreIndex &&
458 notifyIndex == other.notifyIndex &&
459 revision == other.revision &&
460 (!isValueTypeVirtual() ||
461 (valueTypeCoreIndex == other.valueTypeCoreIndex &&
462 valueTypePropType == other.valueTypePropType));
465 int QQmlPropertyRawData::getValueTypeCoreIndex() const
467 return isValueTypeVirtual()?valueTypeCoreIndex:-1;
470 int QQmlPropertyRawData::encodedIndex() const
472 return isValueTypeVirtual()?(coreIndex | (valueTypeCoreIndex << 16)):coreIndex;
476 QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
478 if (!data->hasOverride())
481 if (data->overrideIndexIsProperty)
482 return property(data->overrideIndex);
484 return method(data->overrideIndex);
487 bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
489 return (data->hasAccessors() || (data->metaObjectOffset == -1 && data->revision == 0)) ||
490 (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
493 QQmlEngine *QQmlPropertyCache::qmlEngine() const
498 int QQmlPropertyCache::propertyCount() const
500 return propertyIndexCacheStart + propertyIndexCache.count();
503 int QQmlPropertyCache::propertyOffset() const
505 return propertyIndexCacheStart;
508 int QQmlPropertyCache::methodCount() const
510 return methodIndexCacheStart + methodIndexCache.count();
513 int QQmlPropertyCache::methodOffset() const
515 return methodIndexCacheStart;
518 int QQmlPropertyCache::signalCount() const
520 return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
523 int QQmlPropertyCache::signalOffset() const
525 return signalHandlerIndexCacheStart;
528 QQmlMetaObject::QQmlMetaObject()
532 QQmlMetaObject::QQmlMetaObject(QObject *o)
535 QQmlData *ddata = QQmlData::get(o, false);
536 if (ddata && ddata->propertyCache) _m = ddata->propertyCache;
537 else _m = o->metaObject();
541 QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
546 QQmlMetaObject::QQmlMetaObject(QQmlPropertyCache *m)
551 QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
556 QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
562 bool QQmlMetaObject::isNull() const
567 const char *QQmlMetaObject::className() const
571 } else if (_m.isT1()) {
572 return _m.asT1()->className();
574 return _m.asT2()->className();
578 int QQmlMetaObject::propertyCount() const
582 } else if (_m.isT1()) {
583 return _m.asT1()->propertyCount();
585 return _m.asT2()->propertyCount();
589 bool QQmlMetaObject::hasMetaObject() const
591 return _m.isT2() || (!_m.isNull() && _m.asT1()->metaObject());
594 const QMetaObject *QQmlMetaObject::metaObject() const
596 if (_m.isNull()) return 0;
597 if (_m.isT1()) return _m.asT1()->createMetaObject();
598 else return _m.asT2();
603 #endif // QQMLPROPERTYCACHE_P_H