1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQml module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQMLMETATYPE_P_H
41#define QQMLMETATYPE_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <private/qtqmlglobal_p.h>
55#include <private/qqmltype_p.h>
56#include <private/qqmlproxymetaobject_p.h>
57#include <private/qqmldirparser_p.h>
58
59QT_BEGIN_NAMESPACE
60
61class QQmlTypeModule;
62class QRecursiveMutex;
63class QQmlError;
64class QQmlValueType;
65
66namespace QV4 { class ExecutableCompilationUnit; }
67
68struct CompositeMetaTypeIds
69{
70private:
71 int *refCount = nullptr;
72 void deref();
73 void ref()
74 {
75 Q_ASSERT(refCount);
76 ++*refCount;
77 }
78public:
79 CompositeMetaTypeIds() = default;
80 CompositeMetaTypeIds(QMetaType id, QMetaType listId) : id(id), listId(listId) {}
81 CompositeMetaTypeIds(const CompositeMetaTypeIds &other)
82 : refCount(other.refCount), id(other.id), listId(other.listId)
83 {
84 if (refCount)
85 ref();
86 }
87 CompositeMetaTypeIds(CompositeMetaTypeIds &&other)
88 : refCount(other.refCount), id(other.id), listId(other.listId)
89 {
90 other.refCount = nullptr;
91 }
92 CompositeMetaTypeIds &operator=(const CompositeMetaTypeIds &other)
93 {
94 if (refCount)
95 deref();
96 refCount = other.refCount;
97 id = other.id;
98 listId = other.listId;
99 if (refCount)
100 ref();
101 return *this;
102 }
103 CompositeMetaTypeIds &operator=(CompositeMetaTypeIds &&other)
104 {
105 if (refCount)
106 deref();
107 refCount = other.refCount;
108 id = other.id;
109 listId = other.listId;
110 other.refCount = nullptr;
111 return *this;
112 }
113 ~CompositeMetaTypeIds();
114 static CompositeMetaTypeIds fromCompositeName(const QByteArray &name);
115public:
116 QMetaType id;
117 QMetaType listId;
118 bool isValid() const { return id.isValid() && listId.isValid(); }
119};
120
121class Q_QML_PRIVATE_EXPORT QQmlMetaType
122{
123 friend struct CompositeMetaTypeIds;
124 static CompositeMetaTypeIds registerInternalCompositeType(const QByteArray &className);
125 static void unregisterInternalCompositeType(const CompositeMetaTypeIds &typeIds);
126
127public:
128 enum class RegistrationResult {
129 Success,
130 Failure,
131 NoRegistrationFunction
132 };
133
134 static QQmlType registerType(const QQmlPrivate::RegisterType &type);
135 static QQmlType registerInterface(const QQmlPrivate::RegisterInterface &type);
136 static QQmlType registerSingletonType(const QQmlPrivate::RegisterSingletonType &type);
137 static QQmlType registerCompositeSingletonType(const QQmlPrivate::RegisterCompositeSingletonType &type);
138 static QQmlType registerCompositeType(const QQmlPrivate::RegisterCompositeType &type);
139 static RegistrationResult registerPluginTypes(QObject *instance, const QString &basePath,
140 const QString &uri, const QString &typeNamespace,
141 QTypeRevision version, QList<QQmlError> *errors);
142 static QQmlType typeForUrl(const QString &urlString, const QHashedStringRef& typeName,
143 bool isCompositeSingleton, QList<QQmlError> *errors,
144 QTypeRevision version = QTypeRevision());
145
146 static void unregisterType(int type);
147
148 static void registerModule(const char *uri, QTypeRevision version);
149 static bool protectModule(const QString &uri, QTypeRevision version,
150 bool weakProtectAllVersions = false);
151
152 static void registerModuleImport(const QString &uri, QTypeRevision version,
153 const QQmlDirParser::Import &import);
154 static void unregisterModuleImport(const QString &uri, QTypeRevision version,
155 const QQmlDirParser::Import &import);
156 static QList<QQmlDirParser::Import> moduleImports(const QString &uri, QTypeRevision version);
157
158 static int typeId(const char *uri, QTypeRevision version, const char *qmlName);
159
160 static void registerUndeletableType(const QQmlType &dtype);
161
162 static QList<QString> qmlTypeNames();
163 static QList<QQmlType> qmlTypes();
164 static QList<QQmlType> qmlSingletonTypes();
165 static QList<QQmlType> qmlAllTypes();
166
167 enum class TypeIdCategory {
168 MetaType,
169 QmlType
170 };
171
172 static QQmlType qmlType(const QString &qualifiedName, QTypeRevision version);
173 static QQmlType qmlType(const QHashedStringRef &name, const QHashedStringRef &module, QTypeRevision version);
174 static QQmlType qmlType(const QMetaObject *);
175 static QQmlType qmlType(const QMetaObject *metaObject, const QHashedStringRef &module, QTypeRevision version);
176 static QQmlType qmlType(int typeId, TypeIdCategory category = TypeIdCategory::MetaType);
177 static QQmlType qmlType(const QUrl &unNormalizedUrl, bool includeNonFileImports = false);
178
179 static QQmlPropertyCache *propertyCache(const QMetaObject *metaObject,
180 QTypeRevision version = QTypeRevision(), bool doRef = false);
181 static QQmlPropertyCache *propertyCache(const QQmlType &type, QTypeRevision version);
182
183 static void freeUnusedTypesAndCaches();
184
185 static QMetaProperty defaultProperty(const QMetaObject *);
186 static QMetaProperty defaultProperty(QObject *);
187 static QMetaMethod defaultMethod(const QMetaObject *);
188 static QMetaMethod defaultMethod(QObject *);
189
190 static QObject *toQObject(const QVariant &, bool *ok = nullptr);
191
192 static QMetaType listType(QMetaType type);
193 static QQmlAttachedPropertiesFunc attachedPropertiesFunc(QQmlEnginePrivate *,
194 const QMetaObject *);
195 static bool isInterface(int);
196 static const char *interfaceIId(int);
197 static bool isList(QMetaType type);
198
199 static QTypeRevision latestModuleVersion(const QString &uri);
200 static bool isStronglyLockedModule(const QString &uri, QTypeRevision version);
201 static QTypeRevision matchingModuleVersion(const QString &module, QTypeRevision version);
202 static QQmlTypeModule *typeModule(const QString &uri, QTypeRevision version);
203
204 static QList<QQmlPrivate::AutoParentFunction> parentFunctions();
205
206 enum class CachedUnitLookupError {
207 NoError,
208 NoUnitFound,
209 VersionMismatch
210 };
211
212 static const QQmlPrivate::CachedQmlUnit *findCachedCompilationUnit(const QUrl &uri, CachedUnitLookupError *status);
213
214 // used by tst_qqmlcachegen.cpp
215 static void prependCachedUnitLookupFunction(QQmlPrivate::QmlUnitCacheLookupFunction handler);
216 static void removeCachedUnitLookupFunction(QQmlPrivate::QmlUnitCacheLookupFunction handler);
217
218 static QRecursiveMutex *typeRegistrationLock();
219
220 static QString prettyTypeName(const QObject *object);
221
222 template <typename QQmlTypeContainer>
223 static void removeQQmlTypePrivate(QQmlTypeContainer &container,
224 const QQmlTypePrivate *reference)
225 {
226 for (typename QQmlTypeContainer::iterator it = container.begin(); it != container.end();) {
227 if (*it == reference)
228 it = container.erase(it);
229 else
230 ++it;
231 }
232 }
233
234 static int registerAutoParentFunction(const QQmlPrivate::RegisterAutoParent &autoparent);
235 static void unregisterAutoParentFunction(const QQmlPrivate::AutoParentFunction &function);
236
237 static QQmlType registerSequentialContainer(
238 const QQmlPrivate::RegisterSequentialContainer &sequenceRegistration);
239 static void unregisterSequentialContainer(int id);
240
241 static int registerUnitCacheHook(const QQmlPrivate::RegisterQmlUnitCacheHook &hookRegistration);
242 static void clearTypeRegistrations();
243
244 static QList<QQmlProxyMetaObject::ProxyData> proxyData(const QMetaObject *mo,
245 const QMetaObject *baseMetaObject,
246 QMetaObject *lastMetaObject);
247
248 static void clone(QMetaObjectBuilder &builder, const QMetaObject *mo,
249 const QMetaObject *ignoreStart, const QMetaObject *ignoreEnd);
250
251 static void qmlInsertModuleRegistration(const QString &uri, void (*registerFunction)());
252 static void qmlRemoveModuleRegistration(const QString &uri);
253
254 static bool qmlRegisterModuleTypes(const QString &uri);
255
256 static bool isValueType(QMetaType type);
257 static QQmlValueType *valueType(QMetaType metaType);
258 static const QMetaObject *metaObjectForValueType(QMetaType type);
259};
260
261Q_DECLARE_TYPEINFO(QQmlMetaType, Q_RELOCATABLE_TYPE);
262
263// used in QQmlListMetaType to tag the metatpye
264inline const QMetaObject *dynamicQmlListMarker(const QtPrivate::QMetaTypeInterface *) {
265 return nullptr;
266};
267
268// metatype interface for composite QML types
269struct QQmlMetaTypeInterface : QtPrivate::QMetaTypeInterface
270{
271 const QByteArray name;
272 template <typename T>
273 QQmlMetaTypeInterface(const QByteArray &name, T *)
274 : QMetaTypeInterface {
275 /*.revision=*/ 0,
276 /*.alignment=*/ alignof(T),
277 /*.size=*/ sizeof(T),
278 /*.flags=*/ QtPrivate::QMetaTypeTypeFlags<T>::Flags,
279 /*.typeId=*/ 0,
280 /*.metaObject=*/ nullptr,//QtPrivate::MetaObjectForType<T>::value(),
281 /*.name=*/ name.constData(),
282 /*.defaultCtr=*/ [](const QMetaTypeInterface *, void *addr) { new (addr) T(); },
283 /*.copyCtr=*/ [](const QMetaTypeInterface *, void *addr, const void *other) {
284 new (addr) T(*reinterpret_cast<const T *>(other));
285 },
286 /*.moveCtr=*/ [](const QMetaTypeInterface *, void *addr, void *other) {
287 new (addr) T(std::move(*reinterpret_cast<T *>(other)));
288 },
289 /*.dtor=*/ [](const QMetaTypeInterface *, void *addr) {
290 reinterpret_cast<T *>(addr)->~T();
291 },
292 /*.equals*/ nullptr,
293 /*.lessThan*/ nullptr,
294 /*.debugStream=*/ nullptr,
295 /*.dataStreamOut=*/ nullptr,
296 /*.dataStreamIn=*/ nullptr,
297 /*.legacyRegisterOp=*/ nullptr
298 }
299 , name(name) { }
300};
301
302// metatype for qml list types
303struct QQmlListMetaTypeInterface : QtPrivate::QMetaTypeInterface
304{
305 const QByteArray name;
306 // if this interface is for list<type>; valueType stores the interface for type
307 const QtPrivate::QMetaTypeInterface *valueType;
308 template<typename T>
309 QQmlListMetaTypeInterface(const QByteArray &name, T *, const QtPrivate::QMetaTypeInterface * valueType)
310 : QMetaTypeInterface {
311 /*.revision=*/ 0,
312 /*.alignment=*/ alignof(T),
313 /*.size=*/ sizeof(T),
314 /*.flags=*/ QtPrivate::QMetaTypeTypeFlags<T>::Flags,
315 /*.typeId=*/ 0,
316 /*.metaObjectFn=*/ &dynamicQmlListMarker,
317 /*.name=*/ name.constData(),
318 /*.defaultCtr=*/ [](const QMetaTypeInterface *, void *addr) { new (addr) T(); },
319 /*.copyCtr=*/ [](const QMetaTypeInterface *, void *addr, const void *other) {
320 new (addr) T(*reinterpret_cast<const T *>(other));
321 },
322 /*.moveCtr=*/ [](const QMetaTypeInterface *, void *addr, void *other) {
323 new (addr) T(std::move(*reinterpret_cast<T *>(other)));
324 },
325 /*.dtor=*/ [](const QMetaTypeInterface *, void *addr) {
326 reinterpret_cast<T *>(addr)->~T();
327 },
328 /*.equals*/ nullptr,
329 /*.lessThan*/ nullptr,
330 /*.debugStream=*/ nullptr,
331 /*.dataStreamOut=*/ nullptr,
332 /*.dataStreamIn=*/ nullptr,
333 /*.legacyRegisterOp=*/ nullptr
334 }
335 , name(name), valueType(valueType) { }
336};
337
338QT_END_NAMESPACE
339
340#endif // QQMLMETATYPE_P_H
341
342

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