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 Q_QML_PRIVATE_EXPORT QQmlPropertyCache : public QQmlRefCount, public QQmlCleanup
244 QQmlPropertyCache(QQmlEngine *);
245 QQmlPropertyCache(QQmlEngine *, const QMetaObject *);
246 virtual ~QQmlPropertyCache();
248 void update(QQmlEngine *, const QMetaObject *);
250 QQmlPropertyCache *copy();
252 QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *,
253 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
254 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
255 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
256 QQmlPropertyCache *copyAndAppend(QQmlEngine *, const QMetaObject *, int revision,
257 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
258 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
259 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
261 QQmlPropertyCache *copyAndReserve(QQmlEngine *, int propertyCount,
262 int methodCount, int signalCount);
263 void appendProperty(const QString &,
264 quint32 flags, int coreIndex, int propType, int notifyIndex);
265 void appendProperty(const QHashedCStringRef &,
266 quint32 flags, int coreIndex, int propType, int notifyIndex);
267 void appendSignal(const QString &, quint32, int coreIndex, const int *types = 0,
268 const QList<QByteArray> &names = QList<QByteArray>());
269 void appendSignal(const QHashedCStringRef &, quint32, int coreIndex, const int *types = 0,
270 const QList<QByteArray> &names = QList<QByteArray>());
271 void appendMethod(const QString &, quint32 flags, int coreIndex,
272 const QList<QByteArray> &names = QList<QByteArray>());
273 void appendMethod(const QHashedCStringRef &, quint32 flags, int coreIndex,
274 const QList<QByteArray> &names = QList<QByteArray>());
276 const QMetaObject *metaObject() const;
277 const QMetaObject *createMetaObject();
278 const QMetaObject *firstCppMetaObject() const;
281 QQmlPropertyData *property(const K &key, QObject *object, QQmlContextData *context) const
283 return findProperty(stringCache.find(key), object, context);
286 QQmlPropertyData *property(int) const;
287 QQmlPropertyData *method(int) const;
288 QQmlPropertyData *signal(int) const;
289 int methodIndexToSignalIndex(int) const;
290 QStringList propertyNames() const;
292 QString defaultPropertyName() const;
293 QQmlPropertyData *defaultProperty() const;
294 QQmlPropertyCache *parent() const;
296 inline QQmlPropertyData *overrideData(QQmlPropertyData *) const;
297 inline bool isAllowedInRevision(QQmlPropertyData *) const;
299 inline QQmlEngine *qmlEngine() const;
300 static QQmlPropertyData *property(QQmlEngine *, QObject *, const QString &,
301 QQmlContextData *, QQmlPropertyData &);
302 static QQmlPropertyData *property(QQmlEngine *, QObject *, const QHashedV8String &,
303 QQmlContextData *, QQmlPropertyData &);
304 static int *methodParameterTypes(QObject *, int index, QVarLengthArray<int, 9> &dummy,
305 QByteArray *unknownTypeError);
306 static int methodReturnType(QObject *, const QQmlPropertyData &data,
307 QByteArray *unknownTypeError);
309 //see QMetaObjectPrivate::originalClone
310 int originalClone(int index);
311 static int originalClone(QObject *, int index);
313 QList<QByteArray> signalParameterNames(int index) const;
314 QString signalParameterStringForJS(int index, int *count = 0, QString *errorString = 0);
316 const char *className() const;
318 inline int propertyCount() const;
319 inline int propertyOffset() const;
320 inline int methodCount() const;
321 inline int methodOffset() const;
322 inline int signalCount() const;
323 inline int signalOffset() const;
325 static bool isDynamicMetaObject(const QMetaObject *);
327 void toMetaObjectBuilder(QMetaObjectBuilder &);
330 virtual void destroy();
331 virtual void clear();
334 friend class QQmlEnginePrivate;
335 friend class QV8QObjectWrapper;
336 friend class QQmlCompiler;
338 inline QQmlPropertyCache *copy(int reserve);
340 void append(QQmlEngine *, const QMetaObject *, int revision,
341 QQmlPropertyData::Flag propertyFlags = QQmlPropertyData::NoFlags,
342 QQmlPropertyData::Flag methodFlags = QQmlPropertyData::NoFlags,
343 QQmlPropertyData::Flag signalFlags = QQmlPropertyData::NoFlags);
345 // Implemented in v8/qv8qobjectwrapper.cpp
346 v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
348 typedef QVector<QQmlPropertyData> IndexCache;
349 typedef QStringMultiHash<QPair<int, QQmlPropertyData *> > StringCache;
350 typedef QVector<int> AllowedRevisionCache;
352 QQmlPropertyData *findProperty(StringCache::ConstIterator it, QObject *, QQmlContextData *) const;
353 QQmlPropertyData *findProperty(StringCache::ConstIterator it, const QQmlVMEMetaObject *, QQmlContextData *) const;
355 QQmlPropertyData *ensureResolved(QQmlPropertyData*) const;
357 void resolve(QQmlPropertyData *) const;
358 void updateRecur(QQmlEngine *, const QMetaObject *);
361 QQmlPropertyData *findNamedProperty(const K &key)
363 StringCache::mapped_type *it = stringCache.value(key);
364 return it ? it->second : 0;
368 void setNamedProperty(const K &key, int index, QQmlPropertyData *data, bool isOverride)
370 stringCache.insert(key, qMakePair(index, data));
371 _hasPropertyOverrides |= isOverride;
376 QQmlPropertyCache *_parent;
377 int propertyIndexCacheStart;
378 int methodIndexCacheStart;
379 int signalHandlerIndexCacheStart;
381 IndexCache propertyIndexCache;
382 IndexCache methodIndexCache;
383 IndexCache signalHandlerIndexCache;
384 StringCache stringCache;
385 AllowedRevisionCache allowedRevisionCache;
386 v8::Persistent<v8::Function> constructor;
388 bool _hasPropertyOverrides : 1;
389 bool _ownMetaObject : 1;
390 const QMetaObject *_metaObject;
391 QByteArray _dynamicClassName;
392 QByteArray _dynamicStringData;
393 QString _defaultPropertyName;
394 QQmlPropertyCacheMethodArguments *argumentsCache;
397 // QQmlMetaObject serves as a wrapper around either QMetaObject or QQmlPropertyCache.
398 // This is necessary as we delay creation of QMetaObject for synthesized QObjects, but
399 // we don't want to needlessly generate QQmlPropertyCaches every time we encounter a
400 // QObject type used in assignment or when we don't have a QQmlEngine etc.
402 // This class does NOT reference the propertycache.
403 class QQmlEnginePrivate;
404 class Q_QML_EXPORT QQmlMetaObject
407 inline QQmlMetaObject();
408 inline QQmlMetaObject(QObject *);
409 inline QQmlMetaObject(const QMetaObject *);
410 inline QQmlMetaObject(QQmlPropertyCache *);
411 inline QQmlMetaObject(const QQmlMetaObject &);
413 inline QQmlMetaObject &operator=(const QQmlMetaObject &);
415 inline bool isNull() const;
417 inline const char *className() const;
418 inline int propertyCount() const;
420 inline bool hasMetaObject() const;
421 inline const QMetaObject *metaObject() const;
423 QQmlPropertyCache *propertyCache(QQmlEnginePrivate *) const;
425 static bool canConvert(const QQmlMetaObject &from, const QQmlMetaObject &to);
428 QBiPointer<QQmlPropertyCache, const QMetaObject> _m;
431 QQmlPropertyData::QQmlPropertyData()
436 overrideIndexIsProperty = false;
439 metaObjectOffset = -1;
443 QQmlPropertyData::QQmlPropertyData(const QQmlPropertyRawData &d)
445 *(static_cast<QQmlPropertyRawData *>(this)) = d;
448 bool QQmlPropertyData::operator==(const QQmlPropertyRawData &other)
450 return flags == other.flags &&
451 propType == other.propType &&
452 coreIndex == other.coreIndex &&
453 notifyIndex == other.notifyIndex &&
454 revision == other.revision &&
455 (!isValueTypeVirtual() ||
456 (valueTypeCoreIndex == other.valueTypeCoreIndex &&
457 valueTypePropType == other.valueTypePropType));
460 int QQmlPropertyRawData::getValueTypeCoreIndex() const
462 return isValueTypeVirtual()?valueTypeCoreIndex:-1;
465 int QQmlPropertyRawData::encodedIndex() const
467 return isValueTypeVirtual()?(coreIndex | (valueTypeCoreIndex << 16)):coreIndex;
471 QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
473 if (!data->hasOverride())
476 if (data->overrideIndexIsProperty)
477 return property(data->overrideIndex);
479 return method(data->overrideIndex);
482 bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
484 return (data->hasAccessors() || (data->metaObjectOffset == -1 && data->revision == 0)) ||
485 (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
488 QQmlEngine *QQmlPropertyCache::qmlEngine() const
493 int QQmlPropertyCache::propertyCount() const
495 return propertyIndexCacheStart + propertyIndexCache.count();
498 int QQmlPropertyCache::propertyOffset() const
500 return propertyIndexCacheStart;
503 int QQmlPropertyCache::methodCount() const
505 return methodIndexCacheStart + methodIndexCache.count();
508 int QQmlPropertyCache::methodOffset() const
510 return methodIndexCacheStart;
513 int QQmlPropertyCache::signalCount() const
515 return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
518 int QQmlPropertyCache::signalOffset() const
520 return signalHandlerIndexCacheStart;
523 QQmlMetaObject::QQmlMetaObject()
527 QQmlMetaObject::QQmlMetaObject(QObject *o)
530 QQmlData *ddata = QQmlData::get(o, false);
531 if (ddata && ddata->propertyCache) _m = ddata->propertyCache;
532 else _m = o->metaObject();
536 QQmlMetaObject::QQmlMetaObject(const QMetaObject *m)
541 QQmlMetaObject::QQmlMetaObject(QQmlPropertyCache *m)
546 QQmlMetaObject::QQmlMetaObject(const QQmlMetaObject &o)
551 QQmlMetaObject &QQmlMetaObject::operator=(const QQmlMetaObject &o)
557 bool QQmlMetaObject::isNull() const
562 const char *QQmlMetaObject::className() const
566 } else if (_m.isT1()) {
567 return _m.asT1()->className();
569 return _m.asT2()->className();
573 int QQmlMetaObject::propertyCount() const
577 } else if (_m.isT1()) {
578 return _m.asT1()->propertyCount();
580 return _m.asT2()->propertyCount();
584 bool QQmlMetaObject::hasMetaObject() const
586 return _m.isT2() || (!_m.isNull() && _m.asT1()->metaObject());
589 const QMetaObject *QQmlMetaObject::metaObject() const
591 if (_m.isNull()) return 0;
592 if (_m.isT1()) return _m.asT1()->createMetaObject();
593 else return _m.asT2();
598 #endif // QQMLPROPERTYCACHE_P_H