diff options
Diffstat (limited to 'tools/qmltc/qmltccodewriter.cpp')
-rw-r--r-- | tools/qmltc/qmltccodewriter.cpp | 99 |
1 files changed, 85 insertions, 14 deletions
diff --git a/tools/qmltc/qmltccodewriter.cpp b/tools/qmltc/qmltccodewriter.cpp index 8010f1066c..134de0f98e 100644 --- a/tools/qmltc/qmltccodewriter.cpp +++ b/tools/qmltc/qmltccodewriter.cpp @@ -42,14 +42,14 @@ static QString getFunctionCategory(const QmltcMethod &method) { QString category = getFunctionCategory(static_cast<const QmltcMethodBase &>(method)); switch (method.type) { - case QQmlJSMetaMethod::Signal: + case QQmlJSMetaMethodType::Signal: category = u"Q_SIGNALS"_s; break; - case QQmlJSMetaMethod::Slot: + case QQmlJSMetaMethodType::Slot: category += u" Q_SLOTS"_s; break; - case QQmlJSMetaMethod::Method: - case QQmlJSMetaMethod::StaticMethod: + case QQmlJSMetaMethodType::Method: + case QQmlJSMetaMethodType::StaticMethod: break; } return category; @@ -118,6 +118,7 @@ void QmltcCodeWriter::writeGlobalHeader(QmltcOutputWrapper &code, const QString code.rawAppendToHeader(u"#include <QtCore/qproperty.h>"); code.rawAppendToHeader(u"#include <QtCore/qobject.h>"); code.rawAppendToHeader(u"#include <QtCore/qcoreapplication.h>"); + code.rawAppendToHeader(u"#include <QtCore/qxpfunctional.h>"); code.rawAppendToHeader(u"#include <QtQml/qqmlengine.h>"); code.rawAppendToHeader(u"#include <QtCore/qurl.h>"); // used in engine execution code.rawAppendToHeader(u"#include <QtQml/qqml.h>"); // used for attached properties @@ -160,6 +161,64 @@ void QmltcCodeWriter::writeGlobalHeader(QmltcOutputWrapper &code, const QString } } +void QmltcCodeWriter::write(QmltcOutputWrapper &code, + const QmltcPropertyInitializer &propertyInitializer, + const QmltcType &wrappedType) +{ + code.rawAppendToHeader(u"class " + propertyInitializer.name + u" {"); + + { + { + [[maybe_unused]] QmltcOutputWrapper::HeaderIndentationScope headerIndent(&code); + + code.rawAppendToHeader(u"friend class " + wrappedType.cppType + u";"); + } + + code.rawAppendToHeader(u"public:"_s); + + [[maybe_unused]] QmltcOutputWrapper::MemberNameScope typeScope(&code, propertyInitializer.name); + { + [[maybe_unused]] QmltcOutputWrapper::HeaderIndentationScope headerIndent(&code); + + write(code, propertyInitializer.constructor); + code.rawAppendToHeader(u""); // blank line + + for (const auto &propertySetter : propertyInitializer.propertySetters) { + write(code, propertySetter); + } + } + + code.rawAppendToHeader(u""); // blank line + code.rawAppendToHeader(u"private:"_s); + + { + [[maybe_unused]] QmltcOutputWrapper::HeaderIndentationScope headerIndent(&code); + + write(code, propertyInitializer.component); + write(code, propertyInitializer.initializedCache); + } + } + + code.rawAppendToHeader(u"};"_s); + code.rawAppendToHeader(u""); // blank line +} + +void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcRequiredPropertiesBundle &requiredPropertiesBundle) +{ + code.rawAppendToHeader(u"struct " + requiredPropertiesBundle.name + u" {"); + + { + [[maybe_unused]] QmltcOutputWrapper::HeaderIndentationScope headerIndent(&code); + + for (const auto &member : requiredPropertiesBundle.members) { + write(code, member); + } + } + + code.rawAppendToHeader(u"};"_s); + code.rawAppendToHeader(u""); // blank line +} + void QmltcCodeWriter::writeGlobalFooter(QmltcOutputWrapper &code, const QString &sourcePath, const QString &outNamespace) { @@ -187,12 +246,14 @@ static void writeToFile(const QString &path, const QByteArray &data) QFileInfo fi(path); if (fi.exists() && fi.size() == data.size()) { QFile oldFile(path); - oldFile.open(QIODevice::ReadOnly); - if (oldFile.readAll() == data) - return; + if (oldFile.open(QIODevice::ReadOnly)) { + if (oldFile.readAll() == data) + return; + } } QFile file(path); - file.open(QIODevice::WriteOnly); + if (!file.open(QIODevice::WriteOnly)) + qFatal("Could not open file %s", qPrintable(path)); file.write(data); } @@ -209,7 +270,7 @@ void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcProgram &progra code.rawAppendToHeader(u"class " + type.cppType + u";"); // write all the types and their content for (const QmltcType &type : std::as_const(program.compiledTypes)) - write(code, type); + write(code, type, program.exportMacro); // add typeCount definitions. after all types have been written down (so // they are now complete types as per C++). practically, this only concerns @@ -251,10 +312,14 @@ static void dumpFunctions(QmltcOutputWrapper &code, const QList<QmltcMethod> &fu } } -void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcType &type) +void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcType &type, + const QString &exportMacro) { const auto constructClassString = [&]() { - QString str = u"class " + type.cppType; + QString str = u"class "_s; + if (!exportMacro.isEmpty()) + str.append(exportMacro).append(u" "_s); + str.append(type.cppType); QStringList nonEmptyBaseClasses; nonEmptyBaseClasses.reserve(type.baseClasses.size()); std::copy_if(type.baseClasses.cbegin(), type.baseClasses.cend(), @@ -287,6 +352,12 @@ void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcType &type) code.rawAppendToHeader(u"/* External C++ API */"); code.rawAppendToHeader(u"public:", -1); + if (!type.propertyInitializer.name.isEmpty()) + write(code, type.propertyInitializer, type); + + if (type.requiredPropertiesBundle) + write(code, *type.requiredPropertiesBundle); + // NB: when non-document root, the externalCtor won't be public - but we // really don't care about the output format of such types if (!type.ignoreInit && type.externalCtor.access == QQmlJSMetaMethod::Public) { @@ -340,7 +411,7 @@ void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcType &type) // children for (const auto &child : std::as_const(type.children)) - QmltcCodeWriter::write(code, child); + QmltcCodeWriter::write(code, child, exportMacro); // (non-visible) functions dumpFunctions(code, type.functions, std::not_fn(isUserVisibleFunction)); @@ -392,14 +463,14 @@ void QmltcCodeWriter::write(QmltcOutputWrapper &code, const QmltcMethod &method) { const auto [hSignature, cppSignature] = functionSignatures(method); // Note: augment return type with preambles in declaration - code.rawAppendToHeader((method.type == QQmlJSMetaMethod::StaticMethod + code.rawAppendToHeader((method.type == QQmlJSMetaMethodType::StaticMethod ? u"static " + functionReturnType(method) : functionReturnType(method)) + u" " + hSignature + u";"); // do not generate method implementation if it is a signal const auto methodType = method.type; - if (methodType != QQmlJSMetaMethod::Signal) { + if (methodType != QQmlJSMetaMethodType::Signal) { code.rawAppendToCpp(u""_s); // blank line if (method.comments.size() > 0) { code.rawAppendToCpp(u"/*! \\internal"_s); |