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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDECLARATIVEPROPERTYCACHE_P_H
43 #define QDECLARATIVEPROPERTYCACHE_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/qdeclarativerefcount_p.h"
57 #include "private/qdeclarativecleanup_p.h"
58 #include "private/qdeclarativenotifier_p.h"
60 #include "private/qhashedstring_p.h"
61 #include <QtCore/qvector.h>
65 class QDeclarativeEngine;
68 class QV8QObjectWrapper;
70 class Q_DECLARATIVE_EXPORT QDeclarativePropertyCache : public QDeclarativeRefCount, public QDeclarativeCleanup
73 QDeclarativePropertyCache(QDeclarativeEngine *);
74 QDeclarativePropertyCache(QDeclarativeEngine *, const QMetaObject *);
75 virtual ~QDeclarativePropertyCache();
77 // We have this somewhat aweful 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.
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
94 // These are mutualy exclusive
95 IsFunction = 0x00000040, // Is an invokable
96 IsQObjectDerived = 0x00000080, // Property type is a QObject* derived type
97 IsEnumType = 0x00000100, // Property type is an enum
98 IsQList = 0x00000200, // Property type is a QML list
99 IsQmlBinding = 0x00000400, // Property type is a QDeclarativeBinding*
100 IsQJSValue = 0x00000800, // Property type is a QScriptValue
101 IsV8Handle = 0x00001000, // Property type is a QDeclarativeV8Handle
102 IsVMEProperty = 0x00002000, // Property type is a "var" property of VMEMO
103 IsValueTypeVirtual = 0x00004000, // Property is a value type "virtual" property
105 // Apply only to IsFunctions
106 IsVMEFunction = 0x00008000, // Function was added by QML
107 HasArguments = 0x00010000, // Function takes arguments
108 IsSignal = 0x00020000, // Function is a signal
109 IsVMESignal = 0x00040000, // Signal was added by QML
110 IsV8Function = 0x00080000, // Function takes QDeclarativeV8Function* args
111 IsSignalHandler = 0x00100000, // Function is a signal handler
113 // Internal QDeclarativePropertyCache flags
114 NotFullyResolved = 0x00200000 // True if the type data is to be lazily resolved
116 Q_DECLARE_FLAGS(Flags, Flag)
118 Flags getFlags() const { return Flag(flags); }
119 void setFlags(Flags f) { flags = f; }
121 bool isValid() const { return coreIndex != -1; }
123 bool isConstant() const { return flags & IsConstant; }
124 bool isWritable() const { return flags & IsWritable; }
125 bool isResettable() const { return flags & IsResettable; }
126 bool isAlias() const { return flags & IsAlias; }
127 bool isFinal() const { return flags & IsFinal; }
128 bool isDirect() const { return flags & IsDirect; }
129 bool isFunction() const { return flags & IsFunction; }
130 bool isQObject() const { return flags & IsQObjectDerived; }
131 bool isEnum() const { return flags & IsEnumType; }
132 bool isQList() const { return flags & IsQList; }
133 bool isQmlBinding() const { return flags & IsQmlBinding; }
134 bool isQJSValue() const { return flags & IsQJSValue; }
135 bool isV8Handle() const { return flags & IsV8Handle; }
136 bool isVMEProperty() const { return flags & IsVMEProperty; }
137 bool isValueTypeVirtual() const { return flags & IsValueTypeVirtual; }
138 bool isVMEFunction() const { return flags & IsVMEFunction; }
139 bool hasArguments() const { return flags & HasArguments; }
140 bool isSignal() const { return flags & IsSignal; }
141 bool isVMESignal() const { return flags & IsVMESignal; }
142 bool isV8Function() const { return flags & IsV8Function; }
143 bool isSignalHandler() const { return flags & IsSignalHandler; }
146 int propType; // When !NotFullyResolved
147 const char *propTypeName; // When NotFullyResolved
151 int notifyIndex; // When !IsFunction
152 int relatedIndex; // When IsFunction
155 struct { // When !IsValueTypeVirtual
156 uint overrideIndexIsProperty : 1;
157 signed int overrideIndex : 31;
159 struct { // When IsValueTypeVirtual
160 quint16 valueTypeFlags; // flags of the access property on the value type proxy object
161 quint8 valueTypePropType; // The QVariant::Type of access property on the value type
163 quint8 valueTypeCoreIndex; // The prop index of the access property on the value type
168 int metaObjectOffset;
172 friend class QDeclarativePropertyCache;
176 struct Data : public RawData {
178 inline Data(const RawData &);
180 inline bool operator==(const RawData &);
182 static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
183 void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
184 void load(const QMetaMethod &);
185 QString name(QObject *);
186 QString name(const QMetaObject *);
188 // Returns -1 if not a value type virtual property
189 inline int getValueTypeCoreIndex() const;
192 friend class QDeclarativePropertyCache;
193 void lazyLoad(const QMetaProperty &, QDeclarativeEngine *engine = 0);
194 void lazyLoad(const QMetaMethod &);
195 bool notFullyResolved() const { return flags & NotFullyResolved; }
198 void update(QDeclarativeEngine *, const QMetaObject *);
200 QDeclarativePropertyCache *copy(int reserve = 0);
201 void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
202 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
203 void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
204 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
206 static Data create(const QMetaObject *, const QString &);
208 inline Data *property(const QHashedV8String &) const;
209 Data *property(const QHashedStringRef &) const;
210 Data *property(const QHashedCStringRef &) const;
211 Data *property(const QString &) const;
212 Data *property(int) const;
213 Data *method(int) const;
214 QStringList propertyNames() const;
216 inline Data *overrideData(Data *) const;
217 inline bool isAllowedInRevision(Data *) const;
219 inline QDeclarativeEngine *qmlEngine() const;
220 static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
221 static Data *property(QDeclarativeEngine *, QObject *, const QHashedV8String &, Data &);
223 static bool isDynamicMetaObject(const QMetaObject *);
226 virtual void destroy();
227 virtual void clear();
230 friend class QDeclarativeEnginePrivate;
231 friend class QV8QObjectWrapper;
233 // Implemented in v8/qv8qobjectwrapper.cpp
234 v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
236 typedef QVector<Data> IndexCache;
237 typedef QStringHash<Data *> StringCache;
238 typedef QVector<int> AllowedRevisionCache;
240 void resolve(Data *) const;
241 void updateRecur(QDeclarativeEngine *, const QMetaObject *);
243 QDeclarativeEngine *engine;
245 QDeclarativePropertyCache *parent;
246 int propertyIndexCacheStart;
247 int methodIndexCacheStart;
249 IndexCache propertyIndexCache;
250 IndexCache methodIndexCache;
251 IndexCache signalHandlerIndexCache;
252 StringCache stringCache;
253 AllowedRevisionCache allowedRevisionCache;
254 v8::Persistent<v8::Function> constructor;
256 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
258 QDeclarativePropertyCache::Data::Data()
263 overrideIndexIsProperty = false;
266 metaObjectOffset = -1;
270 QDeclarativePropertyCache::Data::Data(const QDeclarativePropertyCache::RawData &d)
272 *(static_cast<RawData *>(this)) = d;
275 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::RawData &other)
277 return flags == other.flags &&
278 propType == other.propType &&
279 coreIndex == other.coreIndex &&
280 notifyIndex == other.notifyIndex &&
281 revision == other.revision &&
282 (!isValueTypeVirtual() ||
283 (valueTypeCoreIndex == other.valueTypeCoreIndex &&
284 valueTypePropType == other.valueTypePropType));
287 int QDeclarativePropertyCache::Data::getValueTypeCoreIndex() const
289 return isValueTypeVirtual()?valueTypeCoreIndex:-1;
292 QDeclarativePropertyCache::Data *
293 QDeclarativePropertyCache::overrideData(Data *data) const
295 if (data->overrideIndex < 0)
298 if (data->overrideIndexIsProperty)
299 return property(data->overrideIndex);
301 return method(data->overrideIndex);
304 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
306 return (data->metaObjectOffset == -1 && data->revision == 0) ||
307 (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
310 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
315 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(const QHashedV8String &str) const
317 QDeclarativePropertyCache::Data **rv = stringCache.value(str);
318 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
324 #endif // QDECLARATIVEPROPERTYCACHE_P_H