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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "private/qdeclarativepropertycache_p.h"
44 #include "private/qdeclarativeengine_p.h"
45 #include "private/qdeclarativebinding_p.h"
46 #include <QtCore/qdebug.h>
48 Q_DECLARE_METATYPE(QScriptValue)
52 QDeclarativePropertyCache::Data::Flags QDeclarativePropertyCache::Data::flagsForProperty(const QMetaProperty &p, QDeclarativeEngine *engine)
54 int propType = p.userType();
59 flags |= Data::IsConstant;
61 flags |= Data::IsWritable;
63 flags |= Data::IsResettable;
65 flags |= Data::IsFinal;
67 if (propType == qMetaTypeId<QDeclarativeBinding *>()) {
68 flags |= Data::IsQmlBinding;
69 } else if (propType == qMetaTypeId<QScriptValue>()) {
70 flags |= Data::IsQScriptValue;
71 } else if (p.isEnumType()) {
72 flags |= Data::IsEnumType;
74 QDeclarativeMetaType::TypeCategory cat = engine ? QDeclarativeEnginePrivate::get(engine)->typeCategory(propType)
75 : QDeclarativeMetaType::typeCategory(propType);
76 if (cat == QDeclarativeMetaType::Object)
77 flags |= Data::IsQObjectDerived;
78 else if (cat == QDeclarativeMetaType::List)
79 flags |= Data::IsQList;
85 void QDeclarativePropertyCache::Data::load(const QMetaProperty &p, QDeclarativeEngine *engine)
87 propType = p.userType();
88 if (QVariant::Type(propType) == QVariant::LastType)
89 propType = qMetaTypeId<QVariant>();
90 coreIndex = p.propertyIndex();
91 notifyIndex = p.notifySignalIndex();
92 flags = flagsForProperty(p, engine);
93 revision = p.revision();
96 void QDeclarativePropertyCache::Data::load(const QMetaMethod &m)
98 coreIndex = m.methodIndex();
100 flags |= Data::IsFunction;
101 if (m.methodType() == QMetaMethod::Signal)
102 flags |= Data::IsSignal;
103 propType = QVariant::Invalid;
105 const char *returnType = m.typeName();
107 propType = QMetaType::type(returnType);
109 QList<QByteArray> params = m.parameterTypes();
110 if (!params.isEmpty())
111 flags |= Data::HasArguments;
112 revision = m.revision();
117 Creates a new empty QDeclarativePropertyCache.
119 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e)
120 : QDeclarativeCleanup(e), engine(e)
126 Creates a new QDeclarativePropertyCache of \a metaObject.
128 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e, const QMetaObject *metaObject)
129 : QDeclarativeCleanup(e), engine(e)
132 Q_ASSERT(metaObject);
134 update(engine, metaObject);
137 QDeclarativePropertyCache::~QDeclarativePropertyCache()
142 void QDeclarativePropertyCache::clear()
144 for (int ii = 0; ii < indexCache.count(); ++ii) {
145 if (indexCache.at(ii)) indexCache.at(ii)->release();
148 for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
149 RData *data = methodIndexCache.at(ii);
150 if (data) data->release();
153 for (StringCache::ConstIterator iter = stringCache.begin();
154 iter != stringCache.end(); ++iter) {
155 RData *data = (*iter);
159 for (IdentifierCache::ConstIterator iter = identifierCache.begin();
160 iter != identifierCache.end(); ++iter) {
161 RData *data = (*iter);
166 methodIndexCache.clear();
168 identifierCache.clear();
171 QDeclarativePropertyCache::Data QDeclarativePropertyCache::create(const QMetaObject *metaObject,
172 const QString &property)
174 Q_ASSERT(metaObject);
176 QDeclarativePropertyCache::Data rv;
178 const QMetaObject *cmo = metaObject;
180 int idx = metaObject->indexOfProperty(property.toUtf8());
182 QMetaProperty p = metaObject->property(idx);
183 if (p.isScriptable()) {
184 rv.load(metaObject->property(idx));
187 while (cmo && cmo->propertyOffset() >= idx)
188 cmo = cmo->superClass();
196 int methodCount = metaObject->methodCount();
197 for (int ii = methodCount - 1; ii >= 3; --ii) { // >=3 to block the destroyed signal and deleteLater() slot
198 QMetaMethod m = metaObject->method(ii);
199 if (m.access() == QMetaMethod::Private)
201 QString methodName = QString::fromUtf8(m.signature());
203 int parenIdx = methodName.indexOf(QLatin1Char('('));
204 Q_ASSERT(parenIdx != -1);
205 QStringRef methodNameRef = methodName.leftRef(parenIdx);
207 if (methodNameRef == property) {
216 QDeclarativePropertyCache *QDeclarativePropertyCache::copy() const
218 QDeclarativePropertyCache *cache = new QDeclarativePropertyCache(engine);
219 cache->indexCache = indexCache;
220 cache->methodIndexCache = methodIndexCache;
221 cache->stringCache = stringCache;
222 cache->identifierCache = identifierCache;
223 cache->allowedRevisionCache = allowedRevisionCache;
225 for (int ii = 0; ii < indexCache.count(); ++ii) {
226 if (indexCache.at(ii)) indexCache.at(ii)->addref();
228 for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
229 if (methodIndexCache.at(ii)) methodIndexCache.at(ii)->addref();
231 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
233 for (IdentifierCache::ConstIterator iter = identifierCache.begin(); iter != identifierCache.end(); ++iter)
239 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
240 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
242 append(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
245 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
247 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
251 allowedRevisionCache.append(0);
253 QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
254 int methodCount = metaObject->methodCount();
255 // 3 to block the destroyed signal and the deleteLater() slot
256 int methodOffset = qMax(3, metaObject->methodOffset());
258 methodIndexCache.resize(methodCount);
259 for (int ii = methodOffset; ii < methodCount; ++ii) {
260 QMetaMethod m = metaObject->method(ii);
261 if (m.access() == QMetaMethod::Private)
263 QString methodName = QString::fromUtf8(m.signature());
265 int parenIdx = methodName.indexOf(QLatin1Char('('));
266 Q_ASSERT(parenIdx != -1);
267 methodName = methodName.left(parenIdx);
269 RData *data = new RData;
270 data->identifier = enginePriv->objectClass->createPersistentIdentifier(methodName);
271 methodIndexCache[ii] = data;
274 if (m.methodType() == QMetaMethod::Slot || m.methodType() == QMetaMethod::Method)
275 data->flags |= methodFlags;
276 else if (m.methodType() == QMetaMethod::Signal)
277 data->flags |= signalFlags;
279 data->metaObjectOffset = allowedRevisionCache.count() - 1;
281 if (stringCache.contains(methodName)) {
282 RData *old = stringCache[methodName];
283 // We only overload methods in the same class, exactly like C++
284 if (old->flags & Data::IsFunction && old->coreIndex >= methodOffset)
285 data->relatedIndex = old->coreIndex;
286 data->overrideIndexIsProperty = !bool(old->flags & Data::IsFunction);
287 data->overrideIndex = old->coreIndex;
288 stringCache[methodName]->release();
289 identifierCache[data->identifier.identifier]->release();
292 stringCache.insert(methodName, data);
293 identifierCache.insert(data->identifier.identifier, data);
298 int propCount = metaObject->propertyCount();
299 int propOffset = metaObject->propertyOffset();
301 indexCache.resize(propCount);
302 for (int ii = propOffset; ii < propCount; ++ii) {
303 QMetaProperty p = metaObject->property(ii);
304 if (!p.isScriptable())
307 QString propName = QString::fromUtf8(p.name());
309 RData *data = new RData;
310 data->identifier = enginePriv->objectClass->createPersistentIdentifier(propName);
311 indexCache[ii] = data;
313 data->load(p, engine);
314 data->flags |= propertyFlags;
316 data->metaObjectOffset = allowedRevisionCache.count() - 1;
318 if (stringCache.contains(propName)) {
319 RData *old = stringCache[propName];
320 data->overrideIndexIsProperty = !bool(old->flags & Data::IsFunction);
321 data->overrideIndex = old->coreIndex;
322 stringCache[propName]->release();
323 identifierCache[data->identifier.identifier]->release();
326 stringCache.insert(propName, data);
327 identifierCache.insert(data->identifier.identifier, data);
333 void QDeclarativePropertyCache::updateRecur(QDeclarativeEngine *engine, const QMetaObject *metaObject)
338 updateRecur(engine, metaObject->superClass());
340 append(engine, metaObject);
343 void QDeclarativePropertyCache::update(QDeclarativeEngine *engine, const QMetaObject *metaObject)
346 Q_ASSERT(metaObject);
350 // Optimization to prevent unnecessary reallocation of lists
351 indexCache.reserve(metaObject->propertyCount());
352 methodIndexCache.reserve(metaObject->methodCount());
354 updateRecur(engine,metaObject);
357 QDeclarativePropertyCache::Data *
358 QDeclarativePropertyCache::property(int index) const
360 if (index < 0 || index >= indexCache.count())
363 return indexCache.at(index);
366 QDeclarativePropertyCache::Data *
367 QDeclarativePropertyCache::method(int index) const
369 if (index < 0 || index >= methodIndexCache.count())
372 return methodIndexCache.at(index);
375 QDeclarativePropertyCache::Data *
376 QDeclarativePropertyCache::property(const QString &str) const
378 return stringCache.value(str);
381 QString QDeclarativePropertyCache::Data::name(QObject *object)
386 return name(object->metaObject());
389 QString QDeclarativePropertyCache::Data::name(const QMetaObject *metaObject)
391 if (!metaObject || coreIndex == -1)
394 if (flags & IsFunction) {
395 QMetaMethod m = metaObject->method(coreIndex);
397 QString name = QString::fromUtf8(m.signature());
398 int parenIdx = name.indexOf(QLatin1Char('('));
400 name = name.left(parenIdx);
403 QMetaProperty p = metaObject->property(coreIndex);
404 return QString::fromUtf8(p.name());
408 QStringList QDeclarativePropertyCache::propertyNames() const
410 return stringCache.keys();
413 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
414 const QScriptDeclarativeClass::Identifier &name, Data &local)
416 QDeclarativePropertyCache::Data *rv = 0;
418 QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
420 QDeclarativePropertyCache *cache = 0;
421 QDeclarativeData *ddata = QDeclarativeData::get(obj);
422 if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
423 cache = ddata->propertyCache;
425 cache = enginePrivate->cache(obj);
426 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
430 rv = cache->property(name);
432 local = QDeclarativePropertyCache::create(obj->metaObject(), enginePrivate->objectClass->toString(name));
440 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
441 const QString &name, Data &local)
443 QDeclarativePropertyCache::Data *rv = 0;
446 local = QDeclarativePropertyCache::create(obj->metaObject(), name);
450 QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
452 QDeclarativePropertyCache *cache = 0;
453 QDeclarativeData *ddata = QDeclarativeData::get(obj);
454 if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
455 cache = ddata->propertyCache;
457 cache = enginePrivate->cache(obj);
458 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
462 rv = cache->property(name);
464 local = QDeclarativePropertyCache::create(obj->metaObject(), name);