1 | // Copyright (C) 2022 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 | #include "qqmltcobjectcreationhelper_p.h" |
5 | |
6 | #include <private/qqmlmetatype_p.h> |
7 | #include <private/qmetaobjectbuilder_p.h> |
8 | #include <private/qobject_p.h> |
9 | #include <private/qqmltype_p_p.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | void qmltcCreateDynamicMetaObject(QObject *object, const QmltcTypeData &data) |
14 | { |
15 | // TODO: when/if qmltc-compiled types would be registered via |
16 | // qmltyperegistrar, instead of creating a dummy QQmlTypePrivate, fetch the |
17 | // good QQmlType via QQmlMetaType::qmlType(). to do it correctly, one needs, |
18 | // along with the meta object, module name and revision. all that should be |
19 | // available ahead-of-time to qmltc. |
20 | auto qmlTypePrivate = new QQmlTypePrivate(data.regType); |
21 | |
22 | // tie qmlTypePrivate destruction to objects's destruction. the type's |
23 | // content is not needed once the associated object is deleted |
24 | QObject::connect(sender: object, signal: &QObject::destroyed, |
25 | slot: [qmlTypePrivate](QObject *) { qmlTypePrivate->release(); }); |
26 | |
27 | // initialize QQmlType::QQmlCppTypeData |
28 | Q_ASSERT(data.regType == QQmlType::CppType); |
29 | qmlTypePrivate->extraData.cd->allocationSize = data.allocationSize; |
30 | qmlTypePrivate->extraData.cd->newFunc = nullptr; |
31 | qmlTypePrivate->extraData.cd->userdata = nullptr; |
32 | qmlTypePrivate->extraData.cd->noCreationReason = |
33 | QStringLiteral("Qmltc-compiled type is not creatable via QQmlType"); |
34 | qmlTypePrivate->extraData.cd->createValueTypeFunc = nullptr; |
35 | qmlTypePrivate->extraData.cd->parserStatusCast = -1; |
36 | qmlTypePrivate->extraData.cd->extFunc = nullptr; |
37 | qmlTypePrivate->extraData.cd->extMetaObject = nullptr; |
38 | qmlTypePrivate->extraData.cd->customParser = nullptr; |
39 | qmlTypePrivate->extraData.cd->attachedPropertiesFunc = nullptr; |
40 | qmlTypePrivate->extraData.cd->attachedPropertiesType = nullptr; |
41 | qmlTypePrivate->extraData.cd->propertyValueSourceCast = -1; |
42 | qmlTypePrivate->extraData.cd->propertyValueInterceptorCast = -1; |
43 | qmlTypePrivate->extraData.cd->finalizerCast = -1; |
44 | qmlTypePrivate->extraData.cd->registerEnumClassesUnscoped = false; |
45 | qmlTypePrivate->extraData.cd->registerEnumsFromRelatedTypes = false; |
46 | |
47 | qmlTypePrivate->baseMetaObject = data.metaObject; |
48 | qmlTypePrivate->init(); |
49 | |
50 | QQmlType qmlType(qmlTypePrivate); |
51 | Q_ASSERT(qmlType.isValid()); |
52 | |
53 | QObjectPrivate *op = QObjectPrivate::get(o: object); |
54 | // ### inefficient - rather, call this function only once for the leaf type |
55 | if (op->metaObject) { |
56 | delete op->metaObject; |
57 | op->metaObject = nullptr; |
58 | } |
59 | |
60 | qmlType.createProxy(instance: object); |
61 | } |
62 | |
63 | QT_END_NAMESPACE |
64 |