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 *method(int) const;
122 QQmlPropertyData *signal(int index) const;
123 QQmlEnumData *qmlEnum(int) const;
124 int methodIndexToSignalIndex(int) const;
125
126 QString defaultPropertyName() const;
127 QQmlPropertyData *defaultProperty() const;
128 QQmlPropertyCache *parent() const;
129 // is used by the Qml Designer
130 void setParent(QQmlPropertyCache *newParent);
131
132 inline QQmlPropertyData *overrideData(QQmlPropertyData *) const;
133 inline bool isAllowedInRevision(QQmlPropertyData *) const;
134
135 static QQmlPropertyData *property(QJSEngine *, QObject *, const QStringRef &,
136 QQmlContextData *, QQmlPropertyData &);
137 static QQmlPropertyData *property(QJSEngine *, QObject *, const QLatin1String &,
138 QQmlContextData *, QQmlPropertyData &);
139 static QQmlPropertyData *property(QJSEngine *, QObject *, const QV4::String *,
140 QQmlContextData *, QQmlPropertyData &);
141
142 static QQmlPropertyData *property(QJSEngine *engine, QObject *obj, const QString &name,
143 QQmlContextData *context, QQmlPropertyData &local)
144 {
145 return property(engine, obj, QStringRef(&name), context, local);
146 }
147
148 //see QMetaObjectPrivate::originalClone
149 int originalClone(int index);
150 static int originalClone(QObject *, int index);
151
152 QList<QByteArray> signalParameterNames(int index) const;
153 static QString signalParameterStringForJS(QV4::ExecutionEngine *engine, const QList<QByteArray> &parameterNameList, QString *errorString = nullptr);
154
155 const char *className() const;
156
157 inline int propertyCount() const;
158 inline int propertyOffset() const;
159 inline int methodCount() const;
160 inline int methodOffset() const;
161 inline int signalCount() const;
162 inline int signalOffset() const;
163 inline int qmlEnumCount() const;
164
165 static bool isDynamicMetaObject(const QMetaObject *);
166
167 void toMetaObjectBuilder(QMetaObjectBuilder &);
168
169 inline bool callJSFactoryMethod(QObject *object, void **args) const;
170
171 static bool determineMetaObjectSizes(const QMetaObject &mo, int *fieldCount, int *stringCount);
172 static bool addToHash(QCryptographicHash &hash, const QMetaObject &mo);
173
174 QByteArray checksum(bool *ok);
175
176 int allowedRevision(int index) const { return allowedRevisionCache[index]; }
177 void setAllowedRevision(int index, int allowed) { allowedRevisionCache[index] = allowed; }
178
179private:
180 friend class QQmlEnginePrivate;
181 friend class QQmlCompiler;
182 template <typename T> friend class QQmlPropertyCacheCreator;
183 template <typename T> friend class QQmlPropertyCacheAliasCreator;
184 friend class QQmlComponentAndAliasResolver;
185 friend class QQmlMetaObject;
186
187 inline QQmlPropertyCache *copy(int reserve);
188
189 void append(const QMetaObject *, int typeMinorVersion,
190 QQmlPropertyData::Flags propertyFlags = QQmlPropertyData::Flags(),
191 QQmlPropertyData::Flags methodFlags = QQmlPropertyData::Flags(),
192 QQmlPropertyData::Flags signalFlags = QQmlPropertyData::Flags());
193
194 QQmlPropertyCacheMethodArguments *createArgumentsObject(int count, const QList<QByteArray> &names);
195
196 typedef QVector<QQmlPropertyData> IndexCache;
197 typedef QLinkedStringMultiHash<QPair<int, QQmlPropertyData *> > StringCache;
198 typedef QVector<int> AllowedRevisionCache;
199
200 QQmlPropertyData *findProperty(StringCache::ConstIterator it, QObject *, QQmlContextData *) const;
201 QQmlPropertyData *findProperty(StringCache::ConstIterator it, const QQmlVMEMetaObject *, QQmlContextData *) const;
202
203 QQmlPropertyData *ensureResolved(QQmlPropertyData*) const;
204
205 Q_NEVER_INLINE void resolve(QQmlPropertyData *) const;
206 void updateRecur(const QMetaObject *);
207
208 template<typename K>
209 QQmlPropertyData *findNamedProperty(const K &key) const
210 {
211 StringCache::mapped_type *it = stringCache.value(key);
212 return it ? it->second : 0;
213 }
214
215 template<typename K>
216 void setNamedProperty(const K &key, int index, QQmlPropertyData *data, bool isOverride)
217 {
218 stringCache.insert(key, qMakePair(index, data));
219 _hasPropertyOverrides |= isOverride;
220 }
221
222private:
223 QQmlPropertyCache *_parent;
224 int propertyIndexCacheStart;
225 int methodIndexCacheStart;
226 int signalHandlerIndexCacheStart;
227
228 IndexCache propertyIndexCache;
229 IndexCache methodIndexCache;
230 IndexCache signalHandlerIndexCache;
231 StringCache stringCache;
232 AllowedRevisionCache allowedRevisionCache;
233 QVector<QQmlEnumData> enumCache;
234
235 bool _hasPropertyOverrides : 1;
236 bool _ownMetaObject : 1;
237 const QMetaObject *_metaObject;
238 QByteArray _dynamicClassName;
239 QByteArray _dynamicStringData;
240 QString _defaultPropertyName;
241 QQmlPropertyCacheMethodArguments *argumentsCache;
242 int _jsFactoryMethodIndex;
243 QByteArray _checksum;
244};
245
246inline QQmlPropertyData *QQmlPropertyCache::ensureResolved(QQmlPropertyData *p) const
247{
248 if (p && Q_UNLIKELY(p->notFullyResolved()))
249 resolve(p);
250
251 return p;
252}
253
254// Returns this property cache's metaObject. May be null if it hasn't been created yet.
255inline const QMetaObject *QQmlPropertyCache::metaObject() const
256{
257 return _metaObject;
258}
259
260// Returns the first C++ type's QMetaObject - that is, the first QMetaObject not created by
261// QML
262inline const QMetaObject *QQmlPropertyCache::firstCppMetaObject() const
263{
264 while (_parent && (_metaObject == nullptr || _ownMetaObject))
265 return _parent->firstCppMetaObject();
266 return _metaObject;
267}
268
269inline QQmlPropertyData *QQmlPropertyCache::property(int index) const
270{
271 if (index < 0 || index >= (propertyIndexCacheStart + propertyIndexCache.count()))
272 return nullptr;
273
274 if (index < propertyIndexCacheStart)
275 return _parent->property(index);
276
277 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&propertyIndexCache.at(index - propertyIndexCacheStart));
278 return ensureResolved(rv);
279}
280
281inline QQmlPropertyData *QQmlPropertyCache::method(int index) const
282{
283 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
284 return nullptr;
285
286 if (index < methodIndexCacheStart)
287 return _parent->method(index);
288
289 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - methodIndexCacheStart));
290 return ensureResolved(rv);
291}
292
293/*! \internal
294 \a index MUST be in the signal index range (see QObjectPrivate::signalIndex()).
295 This is different from QMetaMethod::methodIndex().
296*/
297inline QQmlPropertyData *QQmlPropertyCache::signal(int index) const
298{
299 if (index < 0 || index >= (signalHandlerIndexCacheStart + signalHandlerIndexCache.count()))
300 return nullptr;
301
302 if (index < signalHandlerIndexCacheStart)
303 return _parent->signal(index);
304
305 QQmlPropertyData *rv = const_cast<QQmlPropertyData *>(&methodIndexCache.at(index - signalHandlerIndexCacheStart));
306 Q_ASSERT(rv->isSignal() || rv->coreIndex() == -1);
307 return ensureResolved(rv);
308}
309
310inline QQmlEnumData *QQmlPropertyCache::qmlEnum(int index) const
311{
312 if (index < 0 || index >= enumCache.count())
313 return nullptr;
314
315 return const_cast<QQmlEnumData *>(&enumCache.at(index));
316}
317
318inline int QQmlPropertyCache::methodIndexToSignalIndex(int index) const
319{
320 if (index < 0 || index >= (methodIndexCacheStart + methodIndexCache.count()))
321 return index;
322
323 if (index < methodIndexCacheStart)
324 return _parent->methodIndexToSignalIndex(index);
325
326 return index - methodIndexCacheStart + signalHandlerIndexCacheStart;
327}
328
329// Returns the name of the default property for this cache
330inline QString QQmlPropertyCache::defaultPropertyName() const
331{
332 return _defaultPropertyName;
333}
334
335inline QQmlPropertyCache *QQmlPropertyCache::parent() const
336{
337 return _parent;
338}
339
340QQmlPropertyData *
341QQmlPropertyCache::overrideData(QQmlPropertyData *data) const
342{
343 if (!data->hasOverride())
344 return nullptr;
345
346 if (data->overrideIndexIsProperty())
347 return property(data->overrideIndex());
348 else
349 return method(data->overrideIndex());
350}
351
352bool QQmlPropertyCache::isAllowedInRevision(QQmlPropertyData *data) const
353{
354 return (data->metaObjectOffset() == -1 && data->revision() == 0) ||
355 (allowedRevisionCache[data->metaObjectOffset()] >= data->revision());
356}
357
358int QQmlPropertyCache::propertyCount() const
359{
360 return propertyIndexCacheStart + propertyIndexCache.count();
361}
362
363int QQmlPropertyCache::propertyOffset() const
364{
365 return propertyIndexCacheStart;
366}
367
368int QQmlPropertyCache::methodCount() const
369{
370 return methodIndexCacheStart + methodIndexCache.count();
371}
372
373int QQmlPropertyCache::methodOffset() const
374{
375 return methodIndexCacheStart;
376}
377
378int QQmlPropertyCache::signalCount() const
379{
380 return signalHandlerIndexCacheStart + signalHandlerIndexCache.count();
381}
382
383int QQmlPropertyCache::signalOffset() const
384{
385 return signalHandlerIndexCacheStart;
386}
387
388int QQmlPropertyCache::qmlEnumCount() const
389{
390 return enumCache.count();
391}
392
393bool QQmlPropertyCache::callJSFactoryMethod(QObject *object, void **args) const
394{
395 if (_jsFactoryMethodIndex != -1) {
396 _metaObject->d.static_metacall(object, QMetaObject::InvokeMetaMethod, _jsFactoryMethodIndex, args);
397 return true;
398 }
399 if (_parent)
400 return _parent->callJSFactoryMethod(object, args);
401 return false;
402}
403
404QT_END_NAMESPACE
405
406#endif // QQMLPROPERTYCACHE_P_H
407