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

Provided by KDAB

Privacy Policy
Learn Advanced QML with KDAB
Find out more

source code of qtdeclarative/src/qml/qml/qqmlmetatype_p.h