1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQMLPROPERTYCACHE_P_H
41#define QQMLPROPERTYCACHE_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <private/qqmlrefcount_p.h>
55#include <private/qflagpointer_p.h>
56#include "qqmlcleanup_p.h"
57#include "qqmlnotifier_p.h"
58#include <private/qqmlpropertyindex_p.h>
59
60#include <private/qlinkedstringhash_p.h>
61#include <QtCore/qvarlengtharray.h>
62#include <QtCore/qvector.h>
63
64#include <private/qv4value_p.h>
65#include <private/qqmlpropertydata_p.h>
66#include <private/qqmlenumdata_p.h>
67#include <private/qqmlenumvalue_p.h>
68
69#include <limits>
70
71QT_BEGIN_NAMESPACE
72
73class QCryptographicHash;
74class QJSEngine;
75class QMetaObjectBuilder;
76class QQmlVMEMetaObject;
77class QQmlPropertyCacheMethodArguments;
78
79class Q_QML_PRIVATE_EXPORT QQmlPropertyCache : public QQmlRefCount
80{
81public:
82 QQmlPropertyCache();
83 QQmlPropertyCache(const QMetaObject *, int metaObjectRevision = 0);
84 ~QQmlPropertyCache() override;
85
86 void update(const QMetaObject *);
87 void invalidate(const QMetaObject *);
88
89 QQmlPropertyCache *copy();
90
91 QQmlPropertyCache *copyAndAppend(const QMetaObject *,
92 QQmlPropertyData::Flags propertyFlags = QQmlPropertyData::Flags(),
93 QQmlPropertyData::Flags methodFlags = QQmlPropertyData::Flags(),
94 QQmlPropertyData::Flags signalFlags = QQmlPropertyData::Flags());
95 QQmlPropertyCache *copyAndAppend(const QMetaObject *, int typeMinorVersion,
96 QQmlPropertyData::Flags propertyFlags = QQmlPropertyData::Flags(),
97 QQmlPropertyData::Flags methodFlags = QQmlPropertyData::Flags(),
98 QQmlPropertyData::Flags signalFlags = QQmlPropertyData::Flags());
99
100 QQmlPropertyCache *copyAndReserve(int propertyCount,
101 int methodCount, int signalCount, int enumCount);
102 void appendProperty(const QString &, QQmlPropertyData::Flags flags, int coreIndex,
103 int propType, int revision, int notifyIndex);
104 void appendSignal(const QString &, QQmlPropertyData::Flags, int coreIndex,
105 const int *types = nullptr, const QList<QByteArray> &names = QList<QByteArray>());
106 void appendMethod(const QString &, QQmlPropertyData::Flags flags, int coreIndex, int returnType,
107 const QList<QByteArray> &names, const QVector<int> &parameterTypes);
108 void appendEnum(const QString &, const QVector<QQmlEnumValue> &);
109
110 const QMetaObject *metaObject() const;
111 const QMetaObject *createMetaObject();
112 const QMetaObject *firstCppMetaObject() const;
113
114 template<typename K>
115 QQmlPropertyData *property(const K &key, QObject *object, QQmlContextData *context) const
116 {
117 return findProperty(stringCache.find(key), object, context);
118 }
119
120 QQmlPropertyData *property(int) const;
121 QQmlPropertyData *maybeUnresolvedProperty(int) const;
122 QQmlPropertyData *method(int) const;
123 QQmlPropertyData *signal(int index) const;
124 QQmlEnumData *qmlEnum(int) const;
125 int methodIndexToSignalIndex(int) const;
126
127 QString defaultPropertyName() const;
128 QQmlPropertyData *defaultProperty() const;
129 QQmlPropertyCache *parent() const;
130 // is used by the Qml Designer
131 void setParent(QQmlPropertyCache *newParent);
132
133 inline QQmlPropertyData *overrideData(QQmlPropertyData *) const;
134 inline bool isAllowedInRevision(QQmlPropertyData *) const;
135
136 static QQmlPropertyData *property(QJSEngine *, QObject *, const QStringRef &,
137 QQmlContextData *, QQmlPropertyData &);
138 static QQmlPropertyData *property(QJSEngine *, QObject *, const QLatin1String &,
139 QQmlContextData *, QQmlPropertyData &);
140 static QQmlPropertyData *property(QJSEngine *, QObject *, const QV4::String *,
141 QQmlContextData *, QQmlPropertyData &);
142
143 static QQmlPropertyData *property(QJSEngine *engine, QObject *obj, const QString &name,
144 QQmlContextData *context, QQmlPropertyData &local)
145 {
146 return property(engine, obj, QStringRef(&name), context, local);
147 }
148
149 //see QMetaObjectPrivate::originalClone
150 int originalClone(int index);
151 static int originalClone(QObject *, int index);
152
153 QList<QByteArray> signalParameterNames(int index) const;
154 static QString signalParameterStringForJS(QV4::ExecutionEngine *engine, const QList<QByteArray> &parameterNameList, QString *errorString = nullptr);
155
156 const char *className() const;
157
158 inline int propertyCount() const;
159 inline int propertyOffset() const;
160 inline int methodCount() const;
161 inline int methodOffset() const;
162 inline int signalCount() const;
163 inline int signalOffset() const;
164 inline int qmlEnumCount() const;
165
166 static bool isDynamicMetaObject(const QMetaObject *);
167
168 void toMetaObjectBuilder(QMetaObjectBuilder &);
169
170 inline bool callJSFactoryMethod(QObject *object, void **args) const;
171
172 static bool determineMetaObjectSizes(const QMetaObject &mo, int *fieldCount, int *stringCount);
173 static bool addToHash(QCryptographicHash &hash, const QMetaObject &mo);
174
175 QByteArray checksum(bool *ok);
176
177 int allowedRevision(int index) const { return allowedRevisionCache[index]; }
178 void setAllowedRevision(int index, int allowed) { allowedRevisionCache[index] = allowed; }
179
180private:
181 friend class QQmlEnginePrivate;
182 friend class QQmlCompiler;
183 template <typename T> friend class QQmlPropertyCacheCreator;
184 template <typename T> friend class QQmlPropertyCacheAliasCreator;
185 friend class QQmlComponentAndAliasResolver;
186 friend class QQmlMetaObject;
187
188 inline QQmlPropertyCache *copy(int reserve);
189
190 void append(const QMetaObject *, int typeMinorVersion,
191 QQmlPropertyData::Flags propertyFlags = QQmlPropertyData::Flags(),
192 QQmlPropertyData::Flags methodFlags = QQmlPropertyData::Flags(),
193 QQmlPropertyData::Flags signalFlags = QQmlPropertyData::Flags());
194
195 QQmlPropertyCacheMethodArguments *createArgumentsObject(int count, const QList<QByteArray> &names);
196
197 typedef QVector<QQmlPropertyData> IndexCache;
198 typedef QLinkedStringMultiHash<QPair<int, QQmlPropertyData *> > StringCache;
199 typedef QVector<int> AllowedRevisionCache;
200
201 QQmlPropertyData *findProperty(StringCache::ConstIterator it, QObject *, QQmlContextData *) const;
202 QQmlPropertyData *findProperty(StringCache::ConstIterator it, const QQmlVMEMetaObject *, QQmlContextData *) const;
203
204 QQmlPropertyData *ensureResolved(QQmlPropertyData*) const;
205
206 Q_NEVER_INLINE void resolve(QQmlPropertyData *) const;
207 void updateRecur(const QMetaObject *);
208
209 template<typename K>
210 QQmlPropertyData *findNamedProperty(const K &key) const
211 {
212 StringCache::mapped_type *it = stringCache.value(key);
213 return it ? it->second : 0;
214 }
215
216 template<typename K>
217 void setNamedProperty(const K &key, int index, QQmlPropertyData *data, bool isOverride)
218 {
219 stringCache.insert(key, qMakePair(index, data));
220 _hasPropertyOverrides |= isOverride;
221 }
222
223private:
224 QQmlPropertyCache *_parent;
225 int propertyIndexCacheStart;
226 int methodIndexCacheStart;
227 int signalHandlerIndexCacheStart;
228
229 IndexCache propertyIndexCache;
230 IndexCache methodIndexCache;
231 IndexCache signalHandlerIndexCache;
232 StringCache stringCache;
233 AllowedRevisionCache allowedRevisionCache;
234 QVector<QQmlEnumData> enumCache;
235
236 bool _hasPropertyOverrides : 1;
237 bool _ownMetaObject : 1;
238 const QMetaObject *_metaObject;
239 QByteArray _dynamicClassName;
240 QByteArray _dynamicStringData;
241 QString _defaultPropertyName;
242 QQmlPropertyCacheMethodArguments *argumentsCache;
243 int _jsFactoryMethodIndex;
244 QByteArray _checksum;
245};
246
247inline QQmlPropertyData *QQmlPropertyCache::ensureResolved(QQmlPropertyData *p) const
248{
249 if (p && Q_UNLIKELY(p->notFullyResolved()))
250 resolve(p);
251
252 return p;
253}
254
255// Returns this property cache's metaObject. May be null if it hasn't been created yet.
256inline const QMetaObject *QQmlPropertyCache::metaObject() const
257{
258 return _metaObject;
259}
260
261// Returns the first C++ type's QMetaObject - that is, the first QMetaObject not created by
262// QML
263inline const QMetaObject *QQmlPropertyCache::firstCppMetaObject() const
264{
265 while (_parent && (_metaObject == nullptr || _ownMetaObject))
266 return _parent->firstCppMetaObject();
267 return _metaObject;
268}
269
270inline QQmlPropertyData *QQmlPropertyCache::property(int index) const
271{
272 if (index < 0 || index >= (propertyIndexCacheStart + propertyIndexCache.count()))
273 return nullptr;
274
275 if (index < propertyIndexCacheStart)
276 return _parent->property(index);
277
278 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
279 return ensureResolved(rv);
280}
281
282inline QQmlPropertyData *QQmlPropertyCache::method(int index) const
283{
284 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
285 return nullptr;
286
287 if (index < methodIndexCacheStart)
288 return _parent->method(index);
289
290 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - methodIndexCacheStart));
291 return ensureResolved(rv);
292}
293
294/*! \internal
295 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
296 This is different from QMetaMethod::methodIndex().
297*/
298inline QQmlPropertyData *QQmlPropertyCache::signal(int index) const
299{
300 if (index < 0 || index >= (signalHandlerIndexCacheStart + signalHandlerIndexCache.count()))
301 return nullptr;
302
303 if (index < signalHandlerIndexCacheStart)
304 return _parent->signal(index);
305
306 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - signalHandlerIndexCacheStart));
307 Q_ASSERT(rv->isSignal() || rv->coreIndex() == -1);
308 return ensureResolved(rv);
309}
310
311inline QQmlEnumData *QQmlPropertyCache::qmlEnum(int index) const
312{
313 if (index < 0 || index >= enumCache.count())
314 return nullptr;
315
316 return const_cast<QQmlEnumData *>(&enumCache.at(index));
317}
318
319inline int QQmlPropertyCache::methodIndexToSignalIndex(int index) const
320{
321 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
322 return index;
323
324 if (index < methodIndexCacheStart)
325 return _parent->methodIndexToSignalIndex(index);
326
327 return index - methodIndexCacheStart + signalHandlerIndexCacheStart;
328}
329
330// Returns the name of the default property for this cache
331inline QString QQmlPropertyCache::defaultPropertyName() const
332{
333 return _defaultPropertyName;
334}
335
336inline QQmlPropertyCache *QQmlPropertyCache::parent() const
337{
338 return _parent;
339}
340
341QQmlPropertyData *
342QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
343{
344 if (!data->hasOverride())
345 return nullptr;
346
347 if (data->overrideIndexIsProperty())
348 return property(data->overrideIndex());
349 else
350 return method(data->overrideIndex());
351}
352
353bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
354{
355 return (data->metaObjectOffset() == -1 && data->revision() == 0) ||
356 (allowedRevisionCache[data->metaObjectOffset()] >= data->revision());
357}
358
359int QQmlPropertyCache::propertyCount() const
360{
361 return propertyIndexCacheStart + propertyIndexCache.count();
362}
363
364int QQmlPropertyCache::propertyOffset() const
365{
366 return propertyIndexCacheStart;
367}
368
369int QQmlPropertyCache::methodCount() const
370{
371 return methodIndexCacheStart + methodIndexCache.count();
372}
373
374int QQmlPropertyCache::methodOffset() const
375{
376 return methodIndexCacheStart;
377}
378
379int QQmlPropertyCache::signalCount() const
380{
381 return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
382}
383
384int QQmlPropertyCache::signalOffset() const
385{
386 return signalHandlerIndexCacheStart;
387}
388
389int QQmlPropertyCache::qmlEnumCount() const
390{
391 return enumCache.count();
392}
393
394bool QQmlPropertyCache::callJSFactoryMethod(QObject *object, void **args) const
395{
396 if (_jsFactoryMethodIndex != -1) {
397 _metaObject->d.static_metacall(object, QMetaObject::InvokeMetaMethod, _jsFactoryMethodIndex, args);
398 return true;
399 }
400 if (_parent)
401 return _parent->callJSFactoryMethod(object, args);
402 return false;
403}
404
405QT_END_NAMESPACE
406
407#endif // QQMLPROPERTYCACHE_P_H
408

source code of qtdeclarative/src/qml/qml/qqmlpropertycache_p.h