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
29
30class QQmlTypeModule;
31class QRecursiveMutex;
32class QQmlError;
33class QQmlValueType;
34
35namespace QV4 {
36namespace CompiledData {
37struct CompilationUnit;
38}
39}
40
41class Q_QML_EXPORT QQmlMetaType
42{
43 friend class QQmlDesignerMetaObject;
44
45public:
46
47 enum class RegistrationResult {
48 Success,
49 Failure,
50 NoRegistrationFunction
51 };
52
53 static QUrl inlineComponentUrl(const QUrl &baseUrl, const QString &name)
54 {
55 QUrl icUrl = baseUrl;
56 icUrl.setFragment(name);
57 return icUrl;
58 }
59
60 static bool equalBaseUrls(const QUrl &aUrl, const QUrl &bUrl)
61 {
62 // Everything but fragment has to match
63 return aUrl.port() == bUrl.port()
64 && aUrl.scheme() == bUrl.scheme()
65 && aUrl.userName() == bUrl.userName()
66 && aUrl.password() == bUrl.password()
67 && aUrl.host() == bUrl.host()
68 && aUrl.path() == bUrl.path()
69 && aUrl.query() == bUrl.query();
70 }
71
72 static QUrl normalizedUrl(const QUrl &unNormalizedUrl)
73 {
74 QUrl normalized(unNormalizedUrl);
75 if (normalized.scheme() == QLatin1String("qrc"))
76 normalized.setHost(QString()); // map qrc:///a.qml to qrc:/a.qml
77 return normalized;
78 }
79
80 enum CompositeTypeLookupMode {
81 NonSingleton,
82 Singleton,
83 JavaScript,
84 };
85
86 static QQmlType findCompositeType(
87 const QUrl &url,
88 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit,
89 CompositeTypeLookupMode mode = NonSingleton);
90 static QQmlType findOrCreateFactualInlineComponentType(
91 const QUrl &url,
92 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
93 static QQmlType findOrCreateFactualInlineComponentType(
94 const QUrl &baseUrl, const QString &name,
95 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit)
96 {
97 return findOrCreateFactualInlineComponentType(inlineComponentUrl(baseUrl, name), compilationUnit);
98 }
99
100 static QQmlType registerType(const QQmlPrivate::RegisterType &type);
101 static QQmlType registerInterface(const QQmlPrivate::RegisterInterface &type);
102 static QQmlType registerSingletonType(
103 const QQmlPrivate::RegisterSingletonType &type,
104 const QQmlType::SingletonInstanceInfo::ConstPtr &siinfo);
105 static QQmlType registerCompositeSingletonType(
106 const QQmlPrivate::RegisterCompositeSingletonType &type,
107 const QQmlType::SingletonInstanceInfo::ConstPtr &siinfo);
108 static QQmlType registerCompositeType(const QQmlPrivate::RegisterCompositeType &type);
109 static RegistrationResult registerPluginTypes(QObject *instance, const QString &basePath,
110 const QString &uri, const QString &typeNamespace,
111 QTypeRevision version, QList<QQmlError> *errors);
112
113 static QQmlType typeForUrl(
114 const QUrl &url, const QHashedStringRef &typeName, CompositeTypeLookupMode mode,
115 QList<QQmlError> *errors, QTypeRevision version = QTypeRevision());
116
117 static QQmlType findOrCreateSpeculativeInlineComponentType(const QUrl &url);
118 static QQmlType findOrCreateSpeculativeInlineComponentType(const QQmlType &outerType, const QString &name)
119 {
120 return outerType.isComposite()
121 ? findOrCreateSpeculativeInlineComponentType(
122 inlineComponentUrl(outerType.sourceUrl(), name))
123 : QQmlType();
124 }
125
126 static void unregisterType(int type);
127
128 static void registerMetaObjectForType(const QMetaObject *metaobject, const QQmlTypePrivate *type);
129
130 static void registerModule(const char *uri, QTypeRevision version);
131 static bool protectModule(const QString &uri, QTypeRevision version,
132 bool weakProtectAllVersions = false);
133
134 static void registerModuleImport(const QString &uri, QTypeRevision version,
135 const QQmlDirParser::Import &import);
136 static void unregisterModuleImport(const QString &uri, QTypeRevision version,
137 const QQmlDirParser::Import &import);
138 static QList<QQmlDirParser::Import> moduleImports(const QString &uri, QTypeRevision version);
139
140 static int typeId(const char *uri, QTypeRevision version, const char *qmlName);
141
142 static void registerUndeletableType(const QQmlType &dtype);
143
144 static QList<QString> qmlTypeNames();
145 static QList<QQmlType> qmlTypes();
146 static QList<QQmlType> qmlSingletonTypes();
147 static QList<QQmlType> qmlAllTypes();
148
149 static QQmlType qmlType(const QString &qualifiedName, QTypeRevision version);
150 static QQmlType qmlType(const QHashedStringRef &name, const QHashedStringRef &module, QTypeRevision version);
151 static QQmlType qmlType(const QMetaObject *);
152 static QQmlType qmlType(const QMetaObject *metaObject, const QHashedStringRef &module, QTypeRevision version);
153 static QQmlType qmlTypeById(int qmlTypeId);
154 static QQmlType firstQmlTypeForAttachmentMetaObject(const QMetaObject *attachmentMetaObject);
155
156 static QQmlType qmlType(QMetaType metaType);
157 static QQmlType qmlListType(QMetaType metaType);
158
159 static QQmlType qmlType(const QUrl &unNormalizedUrl);
160
161 static QQmlPropertyCache::ConstPtr propertyCache(
162 QObject *object, QTypeRevision version = QTypeRevision());
163 static QQmlPropertyCache::ConstPtr propertyCache(
164 const QMetaObject *metaObject, QTypeRevision version = QTypeRevision());
165 static QQmlPropertyCache::ConstPtr propertyCache(
166 const QQmlType &type, QTypeRevision version);
167
168 // These methods may be called from the loader thread
169 static QQmlMetaObject rawMetaObjectForType(QMetaType metaType);
170 static QQmlMetaObject metaObjectForType(QMetaType metaType);
171 static QQmlPropertyCache::ConstPtr propertyCacheForType(QMetaType metaType);
172 static QQmlPropertyCache::ConstPtr rawPropertyCacheForType(QMetaType metaType);
173 static QQmlPropertyCache::ConstPtr rawPropertyCacheForType(
174 QMetaType metaType, QTypeRevision version);
175
176 static bool canConvert(QObject *o, QMetaType metaType);
177 static bool canConvert(const QQmlPropertyCache::ConstPtr &from, QMetaType metaType);
178
179 static void freeUnusedTypesAndCaches();
180
181 static QMetaProperty defaultProperty(const QMetaObject *);
182 static QMetaProperty defaultProperty(QObject *);
183 static QMetaMethod defaultMethod(const QMetaObject *);
184 static QMetaMethod defaultMethod(QObject *);
185
186 static QObject *toQObject(const QVariant &, bool *ok = nullptr);
187
188 static QMetaType listValueType(QMetaType type);
189 static QQmlAttachedPropertiesFunc attachedPropertiesFunc(
190 QQmlTypeLoader *typeLoader, const QMetaObject *);
191 static bool isInterface(QMetaType type);
192 static const char *interfaceIId(QMetaType type);
193 static bool isList(QMetaType type);
194
195 static QTypeRevision latestModuleVersion(const QString &uri);
196 static bool isStronglyLockedModule(const QString &uri, QTypeRevision version);
197 static QTypeRevision matchingModuleVersion(const QString &module, QTypeRevision version);
198 static QQmlTypeModule *typeModule(const QString &uri, QTypeRevision version);
199
200 static QList<QQmlPrivate::AutoParentFunction> parentFunctions();
201
202 enum class CachedUnitLookupError {
203 NoError,
204 NoUnitFound,
205 VersionMismatch,
206 NotFullyTyped
207 };
208
209 enum CacheMode { RejectAll, AcceptUntyped, RequireFullyTyped };
210 static const QQmlPrivate::CachedQmlUnit *findCachedCompilationUnit(
211 const QUrl &uri, CacheMode mode, CachedUnitLookupError *status);
212
213 // used by tst_qqmlcachegen.cpp
214 static void prependCachedUnitLookupFunction(QQmlPrivate::QmlUnitCacheLookupFunction handler);
215 static void removeCachedUnitLookupFunction(QQmlPrivate::QmlUnitCacheLookupFunction handler);
216
217 static QString prettyTypeName(const QObject *object);
218
219 template <typename QQmlTypeContainer>
220 static void removeQQmlTypePrivate(QQmlTypeContainer &container,
221 const QQmlTypePrivate *reference)
222 {
223 for (typename QQmlTypeContainer::iterator it = container.begin(); it != container.end();) {
224 if (*it == reference)
225 it = container.erase(it);
226 else
227 ++it;
228 }
229 }
230
231 template <typename InlineComponentContainer>
232 static void removeFromInlineComponents(
233 InlineComponentContainer &container, const QQmlTypePrivate *reference)
234 {
235 const QUrl referenceUrl = QQmlType(reference).sourceUrl();
236 for (auto it = container.begin(), end = container.end(); it != end;) {
237 if (equalBaseUrls(it.key(), referenceUrl))
238 it = container.erase(it);
239 else
240 ++it;
241 }
242 }
243
244 static void removeFromInlineComponents(
245 QSet<QUrl> &container, const QQmlTypePrivate *reference)
246 {
247 const QUrl referenceUrl = QQmlType(reference).sourceUrl();
248 for (auto it = container.begin(), end = container.end(); it != end;) {
249 if (equalBaseUrls(*it, referenceUrl))
250 it = container.erase(it);
251 else
252 ++it;
253 }
254 }
255
256 static void registerTypeAlias(int typeId, const QString &name);
257
258 static int registerAutoParentFunction(const QQmlPrivate::RegisterAutoParent &autoparent);
259 static void unregisterAutoParentFunction(const QQmlPrivate::AutoParentFunction &function);
260
261 static QQmlType registerSequentialContainer(
262 const QQmlPrivate::RegisterSequentialContainer &sequenceRegistration);
263 static void unregisterSequentialContainer(int id);
264
265 static int registerUnitCacheHook(const QQmlPrivate::RegisterQmlUnitCacheHook &hookRegistration);
266 static void clearTypeRegistrations();
267
268 static QList<QQmlProxyMetaObject::ProxyData> proxyData(const QMetaObject *mo,
269 const QMetaObject *baseMetaObject,
270 QMetaObject *lastMetaObject);
271
272 enum ClonePolicy {
273 CloneAll, // default
274 CloneEnumsOnly, // skip properties and methods
275 };
276 static void clone(QMetaObjectBuilder &builder, const QMetaObject *mo,
277 const QMetaObject *ignoreStart, const QMetaObject *ignoreEnd,
278 ClonePolicy policy);
279
280 static void qmlInsertModuleRegistration(const QString &uri, void (*registerFunction)());
281 static void qmlRemoveModuleRegistration(const QString &uri);
282
283 static bool qmlRegisterModuleTypes(const QString &uri);
284
285 static bool isValueType(QMetaType type);
286 static QQmlValueType *valueType(QMetaType metaType);
287 static const QMetaObject *metaObjectForValueType(QMetaType type);
288
289 static QQmlPropertyCache::ConstPtr findPropertyCacheInCompositeTypes(QMetaType t);
290 static void registerInternalCompositeType(
291 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
292 static void unregisterInternalCompositeType(
293 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
294 static int countInternalCompositeTypeSelfReferences(
295 const QQmlRefPointer<QV4::CompiledData::CompilationUnit> &compilationUnit);
296 static QQmlRefPointer<QV4::CompiledData::CompilationUnit> obtainCompilationUnit(
297 QMetaType type);
298 static QQmlRefPointer<QV4::CompiledData::CompilationUnit> obtainCompilationUnit(
299 const QUrl &url);
300};
301
302Q_DECLARE_TYPEINFO(QQmlMetaType, Q_RELOCATABLE_TYPE);
303
304// used in QQmlListMetaType to tag the metatpye
305inline const QMetaObject *dynamicQmlListMarker(const QtPrivate::QMetaTypeInterface *) {
306 return nullptr;
307};
308
309inline const QMetaObject *dynamicQmlMetaObject(const QtPrivate::QMetaTypeInterface *iface) {
310 return QQmlMetaType::metaObjectForType(QMetaType(iface)).metaObject();
311};
312
313// metatype interface for composite QML types
315{
317QT_WARNING_PUSH
318QT_WARNING_DISABLE_CLANG("-Wdangling-field") // for .name
321 /*.revision=*/ QMetaTypeInterface::CurrentRevision,
322 /*.alignment=*/ alignof(QObject *),
323 /*.size=*/ sizeof(QObject *),
324 /*.flags=*/ QtPrivate::QMetaTypeForType<QObject *>::flags(),
325 /*.typeId=*/ 0,
326 /*.metaObjectFn=*/ &dynamicQmlMetaObject,
327 /*.name=*/ name.constData(),
328 /*.defaultCtr=*/ [](const QMetaTypeInterface *, void *addr) {
329 *static_cast<QObject **>(addr) = nullptr;
330 },
331 /*.copyCtr=*/ [](const QMetaTypeInterface *, void *addr, const void *other) {
332 *static_cast<QObject **>(addr) = *static_cast<QObject *const *>(other);
333 },
334 /*.moveCtr=*/ [](const QMetaTypeInterface *, void *addr, void *other) {
335 *static_cast<QObject **>(addr) = *static_cast<QObject **>(other);
336 },
337 /*.dtor=*/ [](const QMetaTypeInterface *, void *) {},
338 /*.equals*/ nullptr,
339 /*.lessThan*/ nullptr,
340 /*.debugStream=*/ nullptr,
341 /*.dataStreamOut=*/ nullptr,
342 /*.dataStreamIn=*/ nullptr,
343 /*.legacyRegisterOp=*/ nullptr
344 }
345 , name(std::move(name)) { }
347};
348
349// metatype for qml list types
351{
353 // if this interface is for list<type>; valueType stores the interface for type
355QT_WARNING_PUSH
356QT_WARNING_DISABLE_CLANG("-Wdangling-field") // for .name
359 /*.revision=*/ QMetaTypeInterface::CurrentRevision,
360 /*.alignment=*/ alignof(QQmlListProperty<QObject>),
361 /*.size=*/ sizeof(QQmlListProperty<QObject>),
363 /*.typeId=*/ 0,
364 /*.metaObjectFn=*/ &dynamicQmlListMarker,
365 /*.name=*/ name.constData(),
366 /*.defaultCtr=*/ [](const QMetaTypeInterface *, void *addr) {
367 new (addr) QQmlListProperty<QObject> ();
368 },
369 /*.copyCtr=*/ [](const QMetaTypeInterface *, void *addr, const void *other) {
371 *static_cast<const QQmlListProperty<QObject> *>(other));
372 },
373 /*.moveCtr=*/ [](const QMetaTypeInterface *, void *addr, void *other) {
375 std::move(*static_cast<QQmlListProperty<QObject> *>(other)));
376 },
377 /*.dtor=*/ [](const QMetaTypeInterface *, void *addr) {
379 },
380 /*.equals*/ nullptr,
381 /*.lessThan*/ nullptr,
382 /*.debugStream=*/ nullptr,
383 /*.dataStreamOut=*/ nullptr,
384 /*.dataStreamIn=*/ nullptr,
385 /*.legacyRegisterOp=*/ nullptr
386 }
389};
390
391QT_END_NAMESPACE
392
393#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
const QMetaObject * dynamicQmlListMarker(const QtPrivate::QMetaTypeInterface *)
const QMetaObject * dynamicQmlMetaObject(const QtPrivate::QMetaTypeInterface *iface)
const QtPrivate::QMetaTypeInterface * valueType