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