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();
79 inline bool operator==(const Data &);
84 // Can apply to all properties, except IsFunction
85 IsConstant = 0x00000001, // Has CONST flag
86 IsWritable = 0x00000002, // Has WRITE function
87 IsResettable = 0x00000004, // Has RESET function
88 IsAlias = 0x00000008, // Is a QML alias to another property
89 IsFinal = 0x00000010, // Has FINAL flag
90 IsDirect = 0x00000020, // Exists on a C++ QMetaObject
92 // These are mutualy exclusive
93 IsFunction = 0x00000040, // Is an invokable
94 IsQObjectDerived = 0x00000080, // Property type is a QObject* derived type
95 IsEnumType = 0x00000100, // Property type is an enum
96 IsQList = 0x00000200, // Property type is a QML list
97 IsQmlBinding = 0x00000400, // Property type is a QDeclarativeBinding*
98 IsQJSValue = 0x00000800, // Property type is a QScriptValue
99 IsV8Handle = 0x00001000, // Property type is a QDeclarativeV8Handle
101 // Apply only to IsFunctions
102 IsVMEFunction = 0x00002000, // Function was added by QML
103 HasArguments = 0x00004000, // Function takes arguments
104 IsSignal = 0x00008000, // Function is a signal
105 IsVMESignal = 0x00010000, // Signal was added by QML
106 IsV8Function = 0x00020000, // Function takes QDeclarativeV8Function* args
108 // Internal QDeclarativePropertyCache flags
109 NotFullyResolved = 0x00040000 // True if the type data is to be lazily resolved
111 Q_DECLARE_FLAGS(Flags, Flag)
113 Flags getFlags() const { return flags; }
114 void setFlags(Flags f) { flags = f; }
116 bool isValid() const { return coreIndex != -1; }
118 bool isConstant() const { return flags & IsConstant; }
119 bool isWritable() const { return flags & IsWritable; }
120 bool isResettable() const { return flags & IsResettable; }
121 bool isAlias() const { return flags & IsAlias; }
122 bool isFinal() const { return flags & IsFinal; }
123 bool isDirect() const { return flags & IsDirect; }
124 bool isFunction() const { return flags & IsFunction; }
125 bool isQObject() const { return flags & IsQObjectDerived; }
126 bool isEnum() const { return flags & IsEnumType; }
127 bool isQList() const { return flags & IsQList; }
128 bool isQmlBinding() const { return flags & IsQmlBinding; }
129 bool isQJSValue() const { return flags & IsQJSValue; }
130 bool isV8Handle() const { return flags & IsV8Handle; }
131 bool isVMEFunction() const { return flags & IsVMEFunction; }
132 bool hasArguments() const { return flags & HasArguments; }
133 bool isSignal() const { return flags & IsSignal; }
134 bool isVMESignal() const { return flags & IsVMESignal; }
135 bool isV8Function() const { return flags & IsV8Function; }
138 int propType; // When !NotFullyResolved
139 const char *propTypeName; // When NotFullyResolved
143 int notifyIndex; // When !IsFunction
144 int relatedIndex; // When IsFunction
146 uint overrideIndexIsProperty : 1;
147 int overrideIndex : 31;
149 int metaObjectOffset;
151 static Flags flagsForProperty(const QMetaProperty &, QDeclarativeEngine *engine = 0);
152 void load(const QMetaProperty &, QDeclarativeEngine *engine = 0);
153 void load(const QMetaMethod &);
154 QString name(QObject *);
155 QString name(const QMetaObject *);
158 void lazyLoad(const QMetaProperty &, QDeclarativeEngine *engine = 0);
159 void lazyLoad(const QMetaMethod &);
160 bool notFullyResolved() const { return flags & NotFullyResolved; }
161 friend class QDeclarativePropertyCache;
165 struct ValueTypeData {
166 inline ValueTypeData();
167 inline bool operator==(const ValueTypeData &);
168 Data::Flags flags; // flags of the access property on the value type proxy object
169 int valueTypeCoreIdx; // The prop index of the access property on the value type proxy object
170 int valueTypePropType; // The QVariant::Type of access property on the value type proxy object
173 void update(QDeclarativeEngine *, const QMetaObject *);
175 QDeclarativePropertyCache *copy(int reserve = 0);
176 void append(QDeclarativeEngine *, const QMetaObject *, Data::Flag propertyFlags = Data::NoFlags,
177 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
178 void append(QDeclarativeEngine *, const QMetaObject *, int revision, Data::Flag propertyFlags = Data::NoFlags,
179 Data::Flag methodFlags = Data::NoFlags, Data::Flag signalFlags = Data::NoFlags);
181 static Data create(const QMetaObject *, const QString &);
183 inline Data *property(const QHashedV8String &) const;
184 Data *property(const QHashedStringRef &) const;
185 Data *property(const QHashedCStringRef &) const;
186 Data *property(const QString &) const;
187 Data *property(int) const;
188 Data *method(int) const;
189 QStringList propertyNames() const;
191 inline Data *overrideData(Data *) const;
192 inline bool isAllowedInRevision(Data *) const;
194 inline QDeclarativeEngine *qmlEngine() const;
195 static Data *property(QDeclarativeEngine *, QObject *, const QString &, Data &);
196 static Data *property(QDeclarativeEngine *, QObject *, const QHashedV8String &, Data &);
198 static bool isDynamicMetaObject(const QMetaObject *);
200 virtual void clear();
203 friend class QDeclarativeEnginePrivate;
204 friend class QV8QObjectWrapper;
206 // Implemented in v8/qv8qobjectwrapper.cpp
207 v8::Local<v8::Object> newQObject(QObject *, QV8Engine *);
209 typedef QVector<Data> IndexCache;
210 typedef QStringHash<Data *> StringCache;
211 typedef QVector<int> AllowedRevisionCache;
213 void resolve(Data *) const;
214 void updateRecur(QDeclarativeEngine *, const QMetaObject *);
216 QDeclarativeEngine *engine;
218 QDeclarativePropertyCache *parent;
219 int propertyIndexCacheStart;
220 int methodIndexCacheStart;
222 IndexCache propertyIndexCache;
223 IndexCache methodIndexCache;
224 StringCache stringCache;
225 AllowedRevisionCache allowedRevisionCache;
226 v8::Persistent<v8::Function> constructor;
228 Q_DECLARE_OPERATORS_FOR_FLAGS(QDeclarativePropertyCache::Data::Flags);
230 QDeclarativePropertyCache::Data::Data()
231 : propType(0), coreIndex(-1), notifyIndex(-1), overrideIndexIsProperty(false), overrideIndex(-1),
232 revision(0), metaObjectOffset(-1), flags(0)
236 bool QDeclarativePropertyCache::Data::operator==(const QDeclarativePropertyCache::Data &other)
238 return flags == other.flags &&
239 propType == other.propType &&
240 coreIndex == other.coreIndex &&
241 notifyIndex == other.notifyIndex &&
242 revision == other.revision;
245 QDeclarativePropertyCache::Data *
246 QDeclarativePropertyCache::overrideData(Data *data) const
248 if (data->overrideIndex < 0)
251 if (data->overrideIndexIsProperty)
252 return property(data->overrideIndex);
254 return method(data->overrideIndex);
257 QDeclarativePropertyCache::ValueTypeData::ValueTypeData()
258 : flags(QDeclarativePropertyCache::Data::NoFlags), valueTypeCoreIdx(-1), valueTypePropType(0)
262 bool QDeclarativePropertyCache::ValueTypeData::operator==(const ValueTypeData &o)
264 return flags == o.flags &&
265 valueTypeCoreIdx == o.valueTypeCoreIdx &&
266 valueTypePropType == o.valueTypePropType;
269 bool QDeclarativePropertyCache::isAllowedInRevision(Data *data) const
271 return (data->metaObjectOffset == -1 && data->revision == 0) ||
272 (allowedRevisionCache[data->metaObjectOffset] >= data->revision);
275 QDeclarativeEngine *QDeclarativePropertyCache::qmlEngine() const
280 QDeclarativePropertyCache::Data *QDeclarativePropertyCache::property(const QHashedV8String &str) const
282 QDeclarativePropertyCache::Data **rv = stringCache.value(str);
283 if (rv && (*rv)->notFullyResolved()) resolve(*rv);
289 #endif // QDECLARATIVEPROPERTYCACHE_P_H