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 if (propType == qMetaTypeId<QDeclarativeBinding *>()) {
66 flags |= Data::IsQmlBinding;
67 } else if (propType == qMetaTypeId<QScriptValue>()) {
68 flags |= Data::IsQScriptValue;
69 } else if (p.isEnumType()) {
70 flags |= Data::IsEnumType;
72 QDeclarativeMetaType::TypeCategory cat = engine ? QDeclarativeEnginePrivate::get(engine)->typeCategory(propType)
73 : QDeclarativeMetaType::typeCategory(propType);
74 if (cat == QDeclarativeMetaType::Object)
75 flags |= Data::IsQObjectDerived;
76 else if (cat == QDeclarativeMetaType::List)
77 flags |= Data::IsQList;
83 void QDeclarativePropertyCache::Data::load(const QMetaProperty &p, QDeclarativeEngine *engine)
85 propType = p.userType();
86 if (QVariant::Type(propType) == QVariant::LastType)
87 propType = qMetaTypeId<QVariant>();
88 coreIndex = p.propertyIndex();
89 notifyIndex = p.notifySignalIndex();
90 flags = flagsForProperty(p, engine);
91 revision = p.revision();
94 void QDeclarativePropertyCache::Data::load(const QMetaMethod &m)
96 coreIndex = m.methodIndex();
98 flags |= Data::IsFunction;
99 if (m.methodType() == QMetaMethod::Signal)
100 flags |= Data::IsSignal;
101 propType = QVariant::Invalid;
103 const char *returnType = m.typeName();
105 propType = QMetaType::type(returnType);
107 QList<QByteArray> params = m.parameterTypes();
108 if (!params.isEmpty())
109 flags |= Data::HasArguments;
110 revision = m.revision();
115 Creates a new empty QDeclarativePropertyCache.
117 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e)
118 : QDeclarativeCleanup(e), engine(e)
124 Creates a new QDeclarativePropertyCache of \a metaObject.
126 QDeclarativePropertyCache::QDeclarativePropertyCache(QDeclarativeEngine *e, const QMetaObject *metaObject)
127 : QDeclarativeCleanup(e), engine(e)
130 Q_ASSERT(metaObject);
132 update(engine, metaObject);
135 QDeclarativePropertyCache::~QDeclarativePropertyCache()
140 void QDeclarativePropertyCache::clear()
142 for (int ii = 0; ii < indexCache.count(); ++ii) {
143 if (indexCache.at(ii)) indexCache.at(ii)->release();
146 for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
147 RData *data = methodIndexCache.at(ii);
148 if (data) data->release();
151 for (StringCache::ConstIterator iter = stringCache.begin();
152 iter != stringCache.end(); ++iter) {
153 RData *data = (*iter);
157 for (IdentifierCache::ConstIterator iter = identifierCache.begin();
158 iter != identifierCache.end(); ++iter) {
159 RData *data = (*iter);
164 methodIndexCache.clear();
166 identifierCache.clear();
169 QDeclarativePropertyCache::Data QDeclarativePropertyCache::create(const QMetaObject *metaObject,
170 const QString &property)
172 Q_ASSERT(metaObject);
174 QDeclarativePropertyCache::Data rv;
176 const QMetaObject *cmo = metaObject;
178 int idx = metaObject->indexOfProperty(property.toUtf8());
180 QMetaProperty p = metaObject->property(idx);
181 if (p.isScriptable()) {
182 rv.load(metaObject->property(idx));
185 while (cmo && cmo->propertyOffset() >= idx)
186 cmo = cmo->superClass();
194 int methodCount = metaObject->methodCount();
195 for (int ii = methodCount - 1; ii >= 3; --ii) { // >=3 to block the destroyed signal and deleteLater() slot
196 QMetaMethod m = metaObject->method(ii);
197 if (m.access() == QMetaMethod::Private)
199 QString methodName = QString::fromUtf8(m.signature());
201 int parenIdx = methodName.indexOf(QLatin1Char('('));
202 Q_ASSERT(parenIdx != -1);
203 QStringRef methodNameRef = methodName.leftRef(parenIdx);
205 if (methodNameRef == property) {
214 QDeclarativePropertyCache *QDeclarativePropertyCache::copy() const
216 QDeclarativePropertyCache *cache = new QDeclarativePropertyCache(engine);
217 cache->indexCache = indexCache;
218 cache->methodIndexCache = methodIndexCache;
219 cache->stringCache = stringCache;
220 cache->identifierCache = identifierCache;
221 cache->allowedRevisionCache = allowedRevisionCache;
223 for (int ii = 0; ii < indexCache.count(); ++ii) {
224 if (indexCache.at(ii)) indexCache.at(ii)->addref();
226 for (int ii = 0; ii < methodIndexCache.count(); ++ii) {
227 if (methodIndexCache.at(ii)) methodIndexCache.at(ii)->addref();
229 for (StringCache::ConstIterator iter = stringCache.begin(); iter != stringCache.end(); ++iter)
231 for (IdentifierCache::ConstIterator iter = identifierCache.begin(); iter != identifierCache.end(); ++iter)
237 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
238 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
240 append(engine, metaObject, -1, propertyFlags, methodFlags, signalFlags);
243 void QDeclarativePropertyCache::append(QDeclarativeEngine *engine, const QMetaObject *metaObject,
245 Data::Flag propertyFlags, Data::Flag methodFlags, Data::Flag signalFlags)
249 allowedRevisionCache.append(0);
251 QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine);
252 int methodCount = metaObject->methodCount();
253 // 3 to block the destroyed signal and the deleteLater() slot
254 int methodOffset = qMax(3, metaObject->methodOffset());
256 methodIndexCache.resize(methodCount);
257 for (int ii = methodOffset; ii < methodCount; ++ii) {
258 QMetaMethod m = metaObject->method(ii);
259 if (m.access() == QMetaMethod::Private)
261 QString methodName = QString::fromUtf8(m.signature());
263 int parenIdx = methodName.indexOf(QLatin1Char('('));
264 Q_ASSERT(parenIdx != -1);
265 methodName = methodName.left(parenIdx);
267 RData *data = new RData;
268 data->identifier = enginePriv->objectClass->createPersistentIdentifier(methodName);
269 methodIndexCache[ii] = data;
272 if (m.methodType() == QMetaMethod::Slot || m.methodType() == QMetaMethod::Method)
273 data->flags |= methodFlags;
274 else if (m.methodType() == QMetaMethod::Signal)
275 data->flags |= signalFlags;
277 data->metaObjectOffset = allowedRevisionCache.count() - 1;
279 if (stringCache.contains(methodName)) {
280 RData *old = stringCache[methodName];
281 // We only overload methods in the same class, exactly like C++
282 if (old->flags & Data::IsFunction && old->coreIndex >= methodOffset)
283 data->relatedIndex = old->coreIndex;
284 data->overrideIndexIsProperty = !bool(old->flags & Data::IsFunction);
285 data->overrideIndex = old->coreIndex;
286 stringCache[methodName]->release();
287 identifierCache[data->identifier.identifier]->release();
290 stringCache.insert(methodName, data);
291 identifierCache.insert(data->identifier.identifier, data);
296 int propCount = metaObject->propertyCount();
297 int propOffset = metaObject->propertyOffset();
299 indexCache.resize(propCount);
300 for (int ii = propOffset; ii < propCount; ++ii) {
301 QMetaProperty p = metaObject->property(ii);
302 if (!p.isScriptable())
305 QString propName = QString::fromUtf8(p.name());
307 RData *data = new RData;
308 data->identifier = enginePriv->objectClass->createPersistentIdentifier(propName);
309 indexCache[ii] = data;
311 data->load(p, engine);
312 data->flags |= propertyFlags;
314 data->metaObjectOffset = allowedRevisionCache.count() - 1;
316 if (stringCache.contains(propName)) {
317 RData *old = stringCache[propName];
318 data->overrideIndexIsProperty = !bool(old->flags & Data::IsFunction);
319 data->overrideIndex = old->coreIndex;
320 stringCache[propName]->release();
321 identifierCache[data->identifier.identifier]->release();
324 stringCache.insert(propName, data);
325 identifierCache.insert(data->identifier.identifier, data);
331 void QDeclarativePropertyCache::updateRecur(QDeclarativeEngine *engine, const QMetaObject *metaObject)
336 updateRecur(engine, metaObject->superClass());
338 append(engine, metaObject);
341 void QDeclarativePropertyCache::update(QDeclarativeEngine *engine, const QMetaObject *metaObject)
344 Q_ASSERT(metaObject);
348 // Optimization to prevent unnecessary reallocation of lists
349 indexCache.reserve(metaObject->propertyCount());
350 methodIndexCache.reserve(metaObject->methodCount());
352 updateRecur(engine,metaObject);
355 QDeclarativePropertyCache::Data *
356 QDeclarativePropertyCache::property(int index) const
358 if (index < 0 || index >= indexCache.count())
361 return indexCache.at(index);
364 QDeclarativePropertyCache::Data *
365 QDeclarativePropertyCache::method(int index) const
367 if (index < 0 || index >= methodIndexCache.count())
370 return methodIndexCache.at(index);
373 QDeclarativePropertyCache::Data *
374 QDeclarativePropertyCache::property(const QString &str) const
376 return stringCache.value(str);
379 QString QDeclarativePropertyCache::Data::name(QObject *object)
384 return name(object->metaObject());
387 QString QDeclarativePropertyCache::Data::name(const QMetaObject *metaObject)
389 if (!metaObject || coreIndex == -1)
392 if (flags & IsFunction) {
393 QMetaMethod m = metaObject->method(coreIndex);
395 QString name = QString::fromUtf8(m.signature());
396 int parenIdx = name.indexOf(QLatin1Char('('));
398 name = name.left(parenIdx);
401 QMetaProperty p = metaObject->property(coreIndex);
402 return QString::fromUtf8(p.name());
406 QStringList QDeclarativePropertyCache::propertyNames() const
408 return stringCache.keys();
411 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
412 const QScriptDeclarativeClass::Identifier &name, Data &local)
414 QDeclarativePropertyCache::Data *rv = 0;
416 QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
418 QDeclarativePropertyCache *cache = 0;
419 QDeclarativeData *ddata = QDeclarativeData::get(obj);
420 if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
421 cache = ddata->propertyCache;
423 cache = enginePrivate->cache(obj);
424 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
428 rv = cache->property(name);
430 local = QDeclarativePropertyCache::create(obj->metaObject(), enginePrivate->objectClass->toString(name));
438 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(QDeclarativeEngine *engine, QObject *obj,
439 const QString &name, Data &local)
441 QDeclarativePropertyCache::Data *rv = 0;
444 local = QDeclarativePropertyCache::create(obj->metaObject(), name);
448 QDeclarativeEnginePrivate *enginePrivate = QDeclarativeEnginePrivate::get(engine);
450 QDeclarativePropertyCache *cache = 0;
451 QDeclarativeData *ddata = QDeclarativeData::get(obj);
452 if (ddata && ddata->propertyCache && ddata->propertyCache->qmlEngine() == engine)
453 cache = ddata->propertyCache;
455 cache = enginePrivate->cache(obj);
456 if (cache && ddata && !ddata->propertyCache) { cache->addref(); ddata->propertyCache = cache; }
460 rv = cache->property(name);
462 local = QDeclarativePropertyCache::create(obj->metaObject(), name);