| 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, context: object, |
| 25 | slot: [qmlTypePrivate](QObject *) { qmlTypePrivate->release(); }, |
| 26 | type: Qt::DirectConnection); |
| 27 | |
| 28 | // initialize QQmlType::QQmlCppTypeData |
| 29 | Q_ASSERT(data.regType == QQmlType::CppType); |
| 30 | qmlTypePrivate->extraData.cppTypeData->allocationSize = data.allocationSize; |
| 31 | qmlTypePrivate->extraData.cppTypeData->newFunc = nullptr; |
| 32 | qmlTypePrivate->extraData.cppTypeData->userdata = nullptr; |
| 33 | qmlTypePrivate->extraData.cppTypeData->noCreationReason = |
| 34 | QStringLiteral("Qmltc-compiled type is not creatable via QQmlType" ); |
| 35 | qmlTypePrivate->extraData.cppTypeData->createValueTypeFunc = nullptr; |
| 36 | qmlTypePrivate->extraData.cppTypeData->parserStatusCast = -1; |
| 37 | qmlTypePrivate->extraData.cppTypeData->extFunc = nullptr; |
| 38 | qmlTypePrivate->extraData.cppTypeData->extMetaObject = nullptr; |
| 39 | qmlTypePrivate->extraData.cppTypeData->customParser = nullptr; |
| 40 | qmlTypePrivate->extraData.cppTypeData->attachedPropertiesFunc = nullptr; |
| 41 | qmlTypePrivate->extraData.cppTypeData->attachedPropertiesType = nullptr; |
| 42 | qmlTypePrivate->extraData.cppTypeData->propertyValueSourceCast = -1; |
| 43 | qmlTypePrivate->extraData.cppTypeData->propertyValueInterceptorCast = -1; |
| 44 | qmlTypePrivate->extraData.cppTypeData->finalizerCast = -1; |
| 45 | qmlTypePrivate->extraData.cppTypeData->registerEnumClassesUnscoped = false; |
| 46 | qmlTypePrivate->extraData.cppTypeData->registerEnumsFromRelatedTypes = false; |
| 47 | |
| 48 | qmlTypePrivate->baseMetaObject = data.metaObject; |
| 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 | |