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
184 static void freeUnusedTypesAndCaches();
185
186 static QMetaProperty defaultProperty(const QMetaObject *);
187 static QMetaProperty defaultProperty(QObject *);
188 static QMetaMethod defaultMethod(const QMetaObject *);
189 static QMetaMethod defaultMethod(QObject *);
190
191 static QObject *toQObject(const QVariant &, bool *ok = nullptr);
192
193 static QMetaType listValueType(QMetaType type);
194 static QQmlAttachedPropertiesFunc attachedPropertiesFunc(
195 QQmlTypeLoader *typeLoader, const QMetaObject *);
196 static bool isInterface(QMetaType type);
197 static const char *interfaceIId(QMetaType type);
198 static bool isList(QMetaType type);
199
200 static QTypeRevision latestModuleVersion(const QString &uri);
201 static bool isStronglyLockedModule(const QString &uri, QTypeRevision version);
202 static QTypeRevision matchingModuleVersion(const QString &module, QTypeRevision version);
203 static QQmlTypeModule *typeModule(const QString &uri, QTypeRevision version);
204
205 static QList<QQmlPrivate::AutoParentFunction> parentFunctions();
206
207 enum class CachedUnitLookupError {
208 NoError,
209 NoUnitFound,
210 VersionMismatch,
211 NotFullyTyped
212 };
213
214 enum CacheMode { RejectAll, AcceptUntyped, RequireFullyTyped };
215 static const QQmlPrivate::CachedQmlUnit *findCachedCompilationUnit(
216 const QUrl &uri, CacheMode mode, CachedUnitLookupError *status);
217
218 // used by tst_qqmlcachegen.cpp
219 static void prependCachedUnitLookupFunction(QQmlPrivate::QmlUnitCacheLookupFunction handler);
220 static void removeCachedUnitLookupFunction(QQmlPrivate::QmlUnitCacheLookupFunction handler);
221
222 static QString prettyTypeName(const QObject *object);
223
224 template <typename QQmlTypeContainer>
225 static void removeQQmlTypePrivate(QQmlTypeContainer &container,
226 const QQmlTypePrivate *reference)
227 {
228 for (typename QQmlTypeContainer::iterator it = container.begin(); it != container.end();) {
229 if (*it == reference)
230 it = container.erase(it);
231 else
232 ++it;
233 }
234 }
235
236 template <typename InlineComponentContainer>
237 static void removeFromInlineComponents(
238 InlineComponentContainer &container, const QQmlTypePrivate *reference)
239 {
240 const QUrl referenceUrl = QQmlType(reference).sourceUrl();
241 for (auto it = container.begin(), end = container.end(); it != end;) {
242 if (equalBaseUrls(it.key(), referenceUrl))
243 it = container.erase(it);
244 else
245 ++it;
246 }
247 }
248
249 static void removeFromInlineComponents(
250 QSet<QUrl> &container, const QQmlTypePrivate *reference)
251 {
252 const QUrl referenceUrl = QQmlType(reference).sourceUrl();
253 for (auto it = container.begin(), end = container.end(); it != end;) {
254 if (equalBaseUrls(*it, referenceUrl))
255 it = container.erase(it);
256 else
257 ++it;
258 }
259 }
260
261 static void registerTypeAlias(int typeId, const QString &name);
262
263 static int registerAutoParentFunction(const QQmlPrivate::RegisterAutoParent &autoparent);
264 static void unregisterAutoParentFunction(const QQmlPrivate::AutoParentFunction &function);
265
266 static QQmlType registerSequentialContainer(
267 const QQmlPrivate::RegisterSequentialContainer &sequenceRegistration);
268 static void unregisterSequentialContainer(int id);
269
270 static int registerUnitCacheHook(const QQmlPrivate::RegisterQmlUnitCacheHook &hookRegistration);
271 static void clearTypeRegistrations();
272
273 static QList<QQmlProxyMetaObject::ProxyData> proxyData(const QMetaObject *mo,
274 const QMetaObject *baseMetaObject,
275 QMetaObject *lastMetaObject);
276
277 enum ClonePolicy {
278 CloneAll, // default
279 CloneEnumsOnly, // skip properties and methods
280 };
281 static void clone(QMetaObjectBuilder &builder, const QMetaObject *mo,
282 const QMetaObject *ignoreStart, const QMetaObject *ignoreEnd,
283 ClonePolicy policy);
284
285 static void qmlInsertModuleRegistration(const QString &uri, void (*registerFunction)());
286 static void qmlRemoveModuleRegistration(const QString &uri);
287
288 static bool qmlRegisterModuleTypes(const QString &uri);
289
290 static bool isValueType(QMetaType type);
291 static QQmlValueType *valueType(QMetaType metaType);
292 static const QMetaObject *metaObjectForValueType(QMetaType type);
293
294 static QQmlPropertyCache::ConstPtr findPropertyCacheInCompositeTypes(QMetaType t);
295 static void registerInternalCompositeType(
296 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
297 static void unregisterInternalCompositeType(
298 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
299 static void deepClearCompositeType(
300 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &oldUnit);
301 static int countInternalCompositeTypeSelfReferences(
302 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
303 static QQmlRefPointer<QV4::CompiledData::CompilationUnit> obtainCompilationUnit(
304 QMetaType type);
305 static QQmlRefPointer<QV4::CompiledData::CompilationUnit> obtainCompilationUnit(
306 const QUrl &url);
307};
308
309Q_DECLARE_TYPEINFO(QQmlMetaType, Q_RELOCATABLE_TYPE);
310
311// used in QQmlListMetaType to tag the metatpye
312inline const QMetaObject *dynamicQmlListMarker(const QtPrivate::QMetaTypeInterface *) {
313 return nullptr;
314};
315
316inline const QMetaObject *dynamicQmlMetaObject(const QtPrivate::QMetaTypeInterface *iface) {
317 return QQmlMetaType::metaObjectForType(QMetaType(iface)).metaObject();
318};
319
320// metatype interface for composite QML types
322{
324QT_WARNING_PUSH
325QT_WARNING_DISABLE_CLANG("-Wdangling-field") // for .name
328 /*.revision=*/ QMetaTypeInterface::CurrentRevision,
329 /*.alignment=*/ alignof(QObject *),
330 /*.size=*/ sizeof(QObject *),
331 /*.flags=*/ QtPrivate::QMetaTypeForType<QObject *>::flags(),
332 /*.typeId=*/ 0,
333 /*.metaObjectFn=*/ &dynamicQmlMetaObject,
334 /*.name=*/ name.constData(),
335 /*.defaultCtr=*/ [](const QMetaTypeInterface *, void *addr) {
336 *static_cast<QObject **>(addr) = nullptr;
337 },
338 /*.copyCtr=*/ [](const QMetaTypeInterface *, void *addr, const void *other) {
339 *static_cast<QObject **>(addr) = *static_cast<QObject *const *>(other);
340 },
341 /*.moveCtr=*/ [](const QMetaTypeInterface *, void *addr, void *other) {
342 *static_cast<QObject **>(addr) = *static_cast<QObject **>(other);
343 },
344 /*.dtor=*/ [](const QMetaTypeInterface *, void *) {},
345 /*.equals*/ nullptr,
346 /*.lessThan*/ nullptr,
347 /*.debugStream=*/ nullptr,
348 /*.dataStreamOut=*/ nullptr,
349 /*.dataStreamIn=*/ nullptr,
350 /*.legacyRegisterOp=*/ nullptr
351 }
352 , name(std::move(name)) { }
354};
355
356// metatype for qml list types
358{
360 // if this interface is for list<type>; valueType stores the interface for type
362QT_WARNING_PUSH
363QT_WARNING_DISABLE_CLANG("-Wdangling-field") // for .name
366 /*.revision=*/ QMetaTypeInterface::CurrentRevision,
367 /*.alignment=*/ alignof(QQmlListProperty<QObject>),
368 /*.size=*/ sizeof(QQmlListProperty<QObject>),
370 /*.typeId=*/ 0,
371 /*.metaObjectFn=*/ &dynamicQmlListMarker,
372 /*.name=*/ name.constData(),
373 /*.defaultCtr=*/ [](const QMetaTypeInterface *, void *addr) {
374 new (addr) QQmlListProperty<QObject> ();
375 },
376 /*.copyCtr=*/ [](const QMetaTypeInterface *, void *addr, const void *other) {
378 *static_cast<const QQmlListProperty<QObject> *>(other));
379 },
380 /*.moveCtr=*/ [](const QMetaTypeInterface *, void *addr, void *other) {
382 std::move(*static_cast<QQmlListProperty<QObject> *>(other)));
383 },
384 /*.dtor=*/ [](const QMetaTypeInterface *, void *addr) {
386 },
387 /*.equals*/ nullptr,
388 /*.lessThan*/ nullptr,
389 /*.debugStream=*/ nullptr,
390 /*.dataStreamOut=*/ nullptr,
391 /*.dataStreamIn=*/ nullptr,
392 /*.legacyRegisterOp=*/ nullptr
393 }
396};
397
398QT_END_NAMESPACE
399
400#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:342
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