Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlmetatype_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
5#ifndef QQMLMETATYPE_P_H
6#define QQMLMETATYPE_P_H
7
8//
9// W A R N I N G
10// -------------
11//
12// This file is not part of the Qt API. It exists purely as an
13// implementation detail. This header file may change from version to
14// version without notice, or even be removed.
15//
16// We mean it.
17//
18
19#include <private/qqmldirparser_p.h>
20#include <private/qqmlmetaobject_p.h>
21#include <private/qqmlproxymetaobject_p.h>
22#include <private/qqmltype_p.h>
23#include <private/qtqmlglobal_p.h>
24
25#include <QtCore/qset.h>
26#include <QtCore/qtyperevision.h>
27#include <QtCore/qvarlengtharray.h>
28
30
31class QQmlTypeModule;
32class QRecursiveMutex;
33class QQmlError;
34class QQmlValueType;
35
36namespace QV4 {
37namespace CompiledData {
38struct CompilationUnit;
39}
40}
41
42class Q_QML_EXPORT QQmlMetaType
43{
44 friend class QQmlDesignerMetaObject;
45
46public:
47
48 enum class RegistrationResult {
49 Success,
50 Failure,
51 NoRegistrationFunction
52 };
53
54 static QUrl inlineComponentUrl(const QUrl &baseUrl, const QString &name)
55 {
56 QUrl icUrl = baseUrl;
57 icUrl.setFragment(name);
58 return icUrl;
59 }
60
61 static bool equalBaseUrls(const QUrl &aUrl, const QUrl &bUrl)
62 {
63 // Everything but fragment has to match
64 return aUrl.port() == bUrl.port()
65 && aUrl.scheme() == bUrl.scheme()
66 && aUrl.userName() == bUrl.userName()
67 && aUrl.password() == bUrl.password()
68 && aUrl.host() == bUrl.host()
69 && aUrl.path() == bUrl.path()
70 && aUrl.query() == bUrl.query();
71 }
72
73 static QUrl normalizedUrl(const QUrl &unNormalizedUrl)
74 {
75 QUrl normalized(unNormalizedUrl);
76 if (normalized.scheme() == QLatin1String("qrc"))
77 normalized.setHost(QString()); // map qrc:///a.qml to qrc:/a.qml
78 return normalized;
79 }
80
81 enum CompositeTypeLookupMode {
82 NonSingleton,
83 Singleton,
84 JavaScript,
85 };
86
87 static QQmlType findCompositeType(
88 const QUrl &url,
89 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit,
90 CompositeTypeLookupMode mode = NonSingleton);
91 static QQmlType findOrCreateFactualInlineComponentType(
92 const QUrl &url,
93 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
94 static QQmlType findOrCreateFactualInlineComponentType(
95 const QUrl &baseUrl, const QString &name,
96 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit)
97 {
98 return findOrCreateFactualInlineComponentType(inlineComponentUrl(baseUrl, name), compilationUnit);
99 }
100
101 static QQmlType registerType(const QQmlPrivate::RegisterType &type);
102 static QQmlType registerInterface(const QQmlPrivate::RegisterInterface &type);
103 static QQmlType registerSingletonType(
104 const QQmlPrivate::RegisterSingletonType &type,
105 const QQmlType::SingletonInstanceInfo::ConstPtr &siinfo);
106 static QQmlType registerCompositeSingletonType(
107 const QQmlPrivate::RegisterCompositeSingletonType &type,
108 const QQmlType::SingletonInstanceInfo::ConstPtr &siinfo);
109 static QQmlType registerCompositeType(const QQmlPrivate::RegisterCompositeType &type);
110 static RegistrationResult registerPluginTypes(QObject *instance, const QString &basePath,
111 const QString &uri, const QString &typeNamespace,
112 QTypeRevision version, QList<QQmlError> *errors);
113
114 static QQmlType typeForUrl(
115 const QUrl &url, const QHashedStringRef &typeName, CompositeTypeLookupMode mode,
116 QList<QQmlError> *errors, QTypeRevision version = QTypeRevision());
117
118 static QQmlType findOrCreateSpeculativeInlineComponentType(const QUrl &url);
119 static QQmlType findOrCreateSpeculativeInlineComponentType(const QQmlType &outerType, const QString &name)
120 {
121 return (outerType.isComposite() && !outerType.isInlineComponent())
122 ? findOrCreateSpeculativeInlineComponentType(
123 inlineComponentUrl(outerType.sourceUrl(), name))
124 : QQmlType();
125 }
126
127 static void unregisterType(int type);
128
129 static void registerMetaObjectForType(const QMetaObject *metaobject, const QQmlTypePrivate *type);
130
131 static void registerModule(const char *uri, QTypeRevision version);
132 static bool protectModule(const QString &uri, QTypeRevision version,
133 bool weakProtectAllVersions = false);
134
135 static void registerModuleImport(const QString &uri, QTypeRevision version,
136 const QQmlDirParser::Import &import);
137 static void unregisterModuleImport(const QString &uri, QTypeRevision version,
138 const QQmlDirParser::Import &import);
139 static QList<QQmlDirParser::Import> moduleImports(const QString &uri, QTypeRevision version);
140
141 static int typeId(const char *uri, QTypeRevision version, const char *qmlName);
142
143 static void registerUndeletableType(const QQmlType &dtype);
144
145 static QList<QString> qmlTypeNames();
146 static QList<QQmlType> qmlTypes();
147 static QList<QQmlType> qmlSingletonTypes();
148 static QList<QQmlType> qmlAllTypes();
149
150 static QQmlType qmlType(const QString &qualifiedName, QTypeRevision version);
151 static QQmlType qmlType(const QHashedStringRef &name, const QHashedStringRef &module, QTypeRevision version);
152 static QQmlType qmlType(const QMetaObject *);
153 static QQmlType qmlType(const QMetaObject *metaObject, const QHashedStringRef &module, QTypeRevision version);
154 static QQmlType qmlTypeById(int qmlTypeId);
155 static QQmlType firstQmlTypeForAttachmentMetaObject(const QMetaObject *attachmentMetaObject);
156
157 static QQmlType qmlType(QMetaType metaType);
158 static QQmlType qmlListType(QMetaType metaType);
159
160 static QQmlType qmlType(const QUrl &unNormalizedUrl);
161
162 static QQmlPropertyCache::ConstPtr propertyCache(
163 QObject *object, QTypeRevision version = QTypeRevision());
164 static QQmlPropertyCache::ConstPtr propertyCache(
165 const QMetaObject *metaObject, QTypeRevision version = QTypeRevision());
166 static QQmlPropertyCache::ConstPtr propertyCache(
167 const QQmlType &type, QTypeRevision version);
168
169 // These methods may be called from the loader thread
170 static QQmlMetaObject rawMetaObjectForType(QMetaType metaType);
171 static QQmlMetaObject metaObjectForType(QMetaType metaType);
172 static QQmlPropertyCache::ConstPtr propertyCacheForType(QMetaType metaType);
173 static QQmlPropertyCache::ConstPtr rawPropertyCacheForType(QMetaType metaType);
174 static QQmlPropertyCache::ConstPtr rawPropertyCacheForType(
175 QMetaType metaType, QTypeRevision version);
176
177 // All property caches for a composite metatype, which may map to more than one of them.
178 static QVarLengthArray<QQmlPropertyCache::ConstPtr, 4> rawCompositePropertyCachesForType(
179 QMetaType metaType);
180
181 static bool canConvert(QObject *o, QMetaType metaType);
182 static bool canConvert(const QQmlPropertyCache::ConstPtr &from, QMetaType metaType);
183 static bool canLosslesslyHoldUndefined(QMetaType metaType);
184
185 static void freeUnusedTypesAndCaches();
186
187 static QMetaProperty defaultProperty(const QMetaObject *);
188 static QMetaProperty defaultProperty(QObject *);
189 static QMetaMethod defaultMethod(const QMetaObject *);
190 static QMetaMethod defaultMethod(QObject *);
191
192 static QObject *toQObject(const QVariant &, bool *ok = nullptr);
193
194 static QMetaType listValueType(QMetaType type);
195 static QQmlAttachedPropertiesFunc attachedPropertiesFunc(
196 QQmlTypeLoader *typeLoader, const QMetaObject *);
197 static bool isInterface(QMetaType type);
198 static const char *interfaceIId(QMetaType type);
199 static bool isList(QMetaType type);
200
201 static QTypeRevision latestModuleVersion(const QString &uri);
202 static bool isStronglyLockedModule(const QString &uri, QTypeRevision version);
203 static QTypeRevision matchingModuleVersion(const QString &module, QTypeRevision version);
204 static QQmlTypeModule *typeModule(const QString &uri, QTypeRevision version);
205
206 static QList<QQmlPrivate::AutoParentFunction> parentFunctions();
207
208 enum class CachedUnitLookupError {
209 NoError,
210 NoUnitFound,
211 VersionMismatch,
212 NotFullyTyped
213 };
214
215 enum CacheMode { RejectAll, AcceptUntyped, RequireFullyTyped };
216 static const QQmlPrivate::CachedQmlUnit *findCachedCompilationUnit(
217 const QUrl &uri, CacheMode mode, CachedUnitLookupError *status);
218
219 // used by tst_qqmlcachegen.cpp
220 static void prependCachedUnitLookupFunction(QQmlPrivate::QmlUnitCacheLookupFunction handler);
221 static void removeCachedUnitLookupFunction(QQmlPrivate::QmlUnitCacheLookupFunction handler);
222
223 static QString prettyTypeName(const QObject *object);
224
225 template <typename QQmlTypeContainer>
226 static void removeQQmlTypePrivate(QQmlTypeContainer &container,
227 const QQmlTypePrivate *reference)
228 {
229 for (typename QQmlTypeContainer::iterator it = container.begin(); it != container.end();) {
230 if (*it == reference)
231 it = container.erase(it);
232 else
233 ++it;
234 }
235 }
236
237 template <typename InlineComponentContainer>
238 static void removeFromInlineComponents(
239 InlineComponentContainer &container, const QQmlTypePrivate *reference)
240 {
241 const QUrl referenceUrl = QQmlType(reference).sourceUrl();
242 for (auto it = container.begin(), end = container.end(); it != end;) {
243 if (equalBaseUrls(it.key(), referenceUrl))
244 it = container.erase(it);
245 else
246 ++it;
247 }
248 }
249
250 static void removeFromInlineComponents(
251 QSet<QUrl> &container, const QQmlTypePrivate *reference)
252 {
253 const QUrl referenceUrl = QQmlType(reference).sourceUrl();
254 for (auto it = container.begin(), end = container.end(); it != end;) {
255 if (equalBaseUrls(*it, referenceUrl))
256 it = container.erase(it);
257 else
258 ++it;
259 }
260 }
261
262 static void registerTypeAlias(int typeId, const QString &name);
263
264 static int registerAutoParentFunction(const QQmlPrivate::RegisterAutoParent &autoparent);
265 static void unregisterAutoParentFunction(const QQmlPrivate::AutoParentFunction &function);
266
267 static QQmlType registerSequentialContainer(
268 const QQmlPrivate::RegisterSequentialContainer &sequenceRegistration);
269 static void unregisterSequentialContainer(int id);
270
271 static int registerUnitCacheHook(const QQmlPrivate::RegisterQmlUnitCacheHook &hookRegistration);
272 static void clearTypeRegistrations();
273
274 static QList<QQmlProxyMetaObject::ProxyData> proxyData(const QMetaObject *mo,
275 const QMetaObject *baseMetaObject,
276 QMetaObject *lastMetaObject);
277
278 enum ClonePolicy {
279 CloneAll, // default
280 CloneEnumsOnly, // skip properties and methods
281 };
282 static void clone(QMetaObjectBuilder &builder, const QMetaObject *mo,
283 const QMetaObject *ignoreStart, const QMetaObject *ignoreEnd,
284 ClonePolicy policy);
285
286 static void qmlInsertModuleRegistration(const QString &uri, void (*registerFunction)());
287 static void qmlRemoveModuleRegistration(const QString &uri);
288
289 static bool qmlRegisterModuleTypes(const QString &uri);
290
291 static bool isValueType(QMetaType type);
292 static QQmlValueType *valueType(QMetaType metaType);
293 static const QMetaObject *metaObjectForValueType(QMetaType type);
294
295 static QQmlPropertyCache::ConstPtr findPropertyCacheInCompositeTypes(QMetaType t);
296 static void registerInternalCompositeType(
297 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
298 static void unregisterInternalCompositeType(
299 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
300 static void deepClearCompositeType(
301 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &oldUnit);
302 static int countInternalCompositeTypeSelfReferences(
303 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
304 static QQmlRefPointer<QV4::CompiledData::CompilationUnit> obtainCompilationUnit(
305 QMetaType type);
306 static QQmlRefPointer<QV4::CompiledData::CompilationUnit> obtainCompilationUnit(
307 const QUrl &url);
308};
309
310Q_DECLARE_TYPEINFO(QQmlMetaType, Q_RELOCATABLE_TYPE);
311
312// used in QQmlListMetaType to tag the metatpye
313inline const QMetaObject *dynamicQmlListMarker(const QtPrivate::QMetaTypeInterface *) {
314 return nullptr;
315};
316
317inline const QMetaObject *dynamicQmlMetaObject(const QtPrivate::QMetaTypeInterface *iface) {
318 return QQmlMetaType::metaObjectForType(QMetaType(iface)).metaObject();
319};
320
321// metatype interface for composite QML types
323{
325QT_WARNING_PUSH
326QT_WARNING_DISABLE_CLANG("-Wdangling-field") // for .name
327 QQmlMetaTypeInterface(QByteArray name)
328 : QMetaTypeInterface {
329 /*.revision=*/ QMetaTypeInterface::CurrentRevision,
330 /*.alignment=*/ alignof(QObject *),
331 /*.size=*/ sizeof(QObject *),
332 /*.flags=*/ QtPrivate::QMetaTypeForType<QObject *>::flags(),
333 /*.typeId=*/ 0,
334 /*.metaObjectFn=*/ &dynamicQmlMetaObject,
335 /*.name=*/ name.constData(),
336 /*.defaultCtr=*/ [](const QMetaTypeInterface *, void *addr) {
337 *static_cast<QObject **>(addr) = nullptr;
338 },
339 /*.copyCtr=*/ [](const QMetaTypeInterface *, void *addr, const void *other) {
340 *static_cast<QObject **>(addr) = *static_cast<QObject *const *>(other);
341 },
342 /*.moveCtr=*/ [](const QMetaTypeInterface *, void *addr, void *other) {
343 *static_cast<QObject **>(addr) = *static_cast<QObject **>(other);
344 },
345 /*.dtor=*/ [](const QMetaTypeInterface *, void *) {},
346 /*.equals*/ nullptr,
347 /*.lessThan*/ nullptr,
348 /*.debugStream=*/ nullptr,
349 /*.dataStreamOut=*/ nullptr,
350 /*.dataStreamIn=*/ nullptr,
351 /*.legacyRegisterOp=*/ nullptr
352 }
353 , name(std::move(name)) { }
355};
356
357// metatype for qml list types
359{
361 // if this interface is for list<type>; valueType stores the interface for type
363QT_WARNING_PUSH
364QT_WARNING_DISABLE_CLANG("-Wdangling-field") // for .name
365 QQmlListMetaTypeInterface(QByteArray name, const QtPrivate::QMetaTypeInterface *valueType)
366 : QMetaTypeInterface {
367 /*.revision=*/ QMetaTypeInterface::CurrentRevision,
368 /*.alignment=*/ alignof(QQmlListProperty<QObject>),
369 /*.size=*/ sizeof(QQmlListProperty<QObject>),
370 /*.flags=*/ QtPrivate::QMetaTypeForType<QQmlListProperty<QObject>>::flags(),
371 /*.typeId=*/ 0,
372 /*.metaObjectFn=*/ &dynamicQmlListMarker,
373 /*.name=*/ name.constData(),
374 /*.defaultCtr=*/ [](const QMetaTypeInterface *, void *addr) {
375 new (addr) QQmlListProperty<QObject> ();
376 },
377 /*.copyCtr=*/ [](const QMetaTypeInterface *, void *addr, const void *other) {
378 new (addr) QQmlListProperty<QObject>(
379 *static_cast<const QQmlListProperty<QObject> *>(other));
380 },
381 /*.moveCtr=*/ [](const QMetaTypeInterface *, void *addr, void *other) {
382 new (addr) QQmlListProperty<QObject>(
383 std::move(*static_cast<QQmlListProperty<QObject> *>(other)));
384 },
385 /*.dtor=*/ [](const QMetaTypeInterface *, void *addr) {
386 static_cast<QQmlListProperty<QObject> *>(addr)->~QQmlListProperty<QObject>();
387 },
388 /*.equals*/ nullptr,
389 /*.lessThan*/ nullptr,
390 /*.debugStream=*/ nullptr,
391 /*.dataStreamOut=*/ nullptr,
392 /*.dataStreamIn=*/ nullptr,
393 /*.legacyRegisterOp=*/ nullptr
394 }
395 , name(std::move(name)), valueType(valueType) { }
397};
398
399QT_END_NAMESPACE
400
401#endif // QQMLMETATYPE_P_H
QList< QQmlError > errors
Definition qqmlinfo.cpp:159
QQmlInfoPrivate(QtMsgType type)
Definition qqmlinfo.cpp:149
const QObject * object
Definition qqmlinfo.cpp:157
QtMsgType msgType
Definition qqmlinfo.cpp:156
\inmodule QtQml
\inmodule QtCore
Definition qmutex.h:343
Combined button and popup list for selecting options.
Definition qjsvalue.h:24
Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE)
#define MESSAGE_FUNCS(FuncName, MessageLevel)
Definition qqmlinfo.cpp:224
Q_DECL_COLD_FUNCTION Q_QML_EXPORT QQmlInfo qmlWarning(const QObject *me, const QQmlError &error)
const QMetaObject * dynamicQmlListMarker(const QtPrivate::QMetaTypeInterface *)
const QMetaObject * dynamicQmlMetaObject(const QtPrivate::QMetaTypeInterface *iface)
const QtPrivate::QMetaTypeInterface * valueType