diff options
author | Brett Stottlemyer <bstottle@ford.com> | 2021-08-18 23:49:31 -0400 |
---|---|---|
committer | Brett Stottlemyer <bstottle@ford.com> | 2021-08-22 22:59:21 -0400 |
commit | 8d496f7e2dbcfe1c91066ea33c206a171a8fe111 (patch) | |
tree | 93cdee000eeaed96dfb7333aa0a366ff0565b0d4 /tools/repc/repcodegenerator.cpp | |
parent | 1346c95a91652f6df526f9c9828848d6fb66e138 (diff) |
Refactor repcodegenerator
Two changes that affect a lot of lines
1) Make m_ast and m_stream member variables rather than passing them to
most functions.
2) Keep lines to less than 100 characters for readability
Pick-to: 6.2
Change-Id: I0c2bd2b549001fb4e9d36d17a2582b1f9bbb81f1
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'tools/repc/repcodegenerator.cpp')
-rw-r--r-- | tools/repc/repcodegenerator.cpp | 1219 |
1 files changed, 722 insertions, 497 deletions
diff --git a/tools/repc/repcodegenerator.cpp b/tools/repc/repcodegenerator.cpp index 56dd785..45acfc9 100644 --- a/tools/repc/repcodegenerator.cpp +++ b/tools/repc/repcodegenerator.cpp @@ -28,11 +28,8 @@ #include "repcodegenerator.h" -#include "repparser.h" - #include <QFileInfo> #include <QMetaType> -#include <QTextStream> #include <QCryptographicHash> #include <QRegularExpression> @@ -87,7 +84,8 @@ static bool hasScopedEnum(const ASTClass &classContext) return false; } -static QString fullyQualifiedTypeName(const ASTClass& classContext, const QString &className, const QString &typeName) +static QString fullyQualifiedName(const ASTClass& classContext, const QString &className, + const QString &typeName) { static const QRegularExpression re = QRegularExpression(QLatin1String("([^<>,\\s]+)")); QString copy = typeName; @@ -102,7 +100,9 @@ static QString fullyQualifiedTypeName(const ASTClass& classContext, const QStrin } // for enums we need to transform signal/slot arguments to include the class scope -static QList<ASTFunction> transformEnumParams(const ASTClass& classContext, const QList<ASTFunction> &methodList, const QString &typeName) { +static QList<ASTFunction> transformEnumParams(const ASTClass& classContext, + const QList<ASTFunction> &methodList, + const QString &typeName) { QList<ASTFunction> localList = methodList; for (ASTFunction &astFunction : localList) { for (ASTDeclaration &astParam : astFunction.params) { @@ -127,10 +127,9 @@ static bool isBuiltinType(const QString &type) return (metaType.id() < QMetaType::User); } -RepCodeGenerator::RepCodeGenerator(QIODevice *outputDevice) - : m_outputDevice(outputDevice) +RepCodeGenerator::RepCodeGenerator(QIODevice *outputDevice, const AST &ast) + : m_stream(outputDevice), m_ast(ast) { - Q_ASSERT(m_outputDevice); } static QByteArray enumSignature(const ASTEnum &e) @@ -153,7 +152,8 @@ static QByteArray typeData(const QString &type, const QHash<QString, QByteArray> return type.toLatin1(); } -static QByteArray functionsData(const QList<ASTFunction> &functions, const QHash<QString, QByteArray> &specialTypes) +static QByteArray functionsData(const QList<ASTFunction> &functions, + const QHash<QString, QByteArray> &specialTypes) { QByteArray ret; for (const ASTFunction &func : functions) { @@ -161,7 +161,8 @@ static QByteArray functionsData(const QList<ASTFunction> &functions, const QHash for (const ASTDeclaration ¶m : func.params) { ret += param.name.toLatin1(); ret += typeData(param.type, specialTypes); - ret += QByteArray(reinterpret_cast<const char *>(¶m.variableType), sizeof(param.variableType)); + ret += QByteArray(reinterpret_cast<const char *>(¶m.variableType), + sizeof(param.variableType)); } ret += typeData(func.returnType, specialTypes); } @@ -181,7 +182,8 @@ QByteArray RepCodeGenerator::classSignature(const ASTClass &ac) checksum.addData(ac.name.toLatin1()); // Checksum properties - QSet<int> classIndices{ ac.subClassPropertyIndices.begin(), ac.subClassPropertyIndices.end() }; + QSet<int> classIndices{ ac.subClassPropertyIndices.begin(), + ac.subClassPropertyIndices.end() }; int propertyIndex = -1; int modelIndex = 0; for (const ASTProperty &p : ac.properties) { @@ -216,38 +218,33 @@ QByteArray RepCodeGenerator::classSignature(const ASTClass &ac) return m_globalTypes[ac.name]; } -void RepCodeGenerator::generate(const AST &ast, Mode mode, QString fileName) +void RepCodeGenerator::generate(Mode mode, QString fileName) { - QTextStream stream(m_outputDevice); if (fileName.isEmpty()) - stream << "#pragma once" << Qt::endl << Qt::endl; + m_stream << "#pragma once" << Qt::endl << Qt::endl; else { fileName = QFileInfo(fileName).fileName(); fileName = fileName.toUpper(); fileName.replace(QLatin1Char('.'), QLatin1Char('_')); - stream << "#ifndef " << fileName << Qt::endl; - stream << "#define " << fileName << Qt::endl << Qt::endl; + m_stream << "#ifndef " << fileName << Qt::endl; + m_stream << "#define " << fileName << Qt::endl << Qt::endl; } - generateHeader(mode, stream, ast); - for (const ASTEnum &en : ast.enums) { - ASTFlag flag; - if (en.flagIndex >= 0) - flag = ast.flags.at(en.flagIndex); - generateEnumGadget(stream, en, flag, QStringLiteral("%1Enum").arg(en.name)); - } - for (const POD &pod : ast.pods) - generatePOD(stream, pod); + generateHeader(mode); + for (const ASTEnum &en : m_ast.enums) + generateEnumGadget(en, QStringLiteral("%1Enum").arg(en.name)); + for (const POD &pod : m_ast.pods) + generatePOD(pod); QSet<QString> metaTypes; - for (const POD &pod : ast.pods) { + for (const POD &pod : m_ast.pods) { metaTypes << pod.name; for (const PODAttribute &attribute : pod.attributes) metaTypes << attribute.type; } const QString metaTypeRegistrationCode = generateMetaTypeRegistration(metaTypes); - for (const ASTClass &astClass : ast.classes) { + for (const ASTClass &astClass : m_ast.classes) { QSet<QString> classMetaTypes; QSet<QString> pendingMetaTypes; for (const ASTEnum &en : astClass.enums) @@ -275,35 +272,37 @@ void RepCodeGenerator::generate(const AST &ast, Mode mode, QString fileName) + generateMetaTypeRegistrationForPending(pendingMetaTypes); if (mode == MERGED) { - generateClass(REPLICA, stream, astClass, replicaMetaTypeRegistrationCode); - generateClass(SOURCE, stream, astClass, classMetaTypeRegistrationCode); - generateClass(SIMPLE_SOURCE, stream, astClass, classMetaTypeRegistrationCode); - generateSourceAPI(stream, astClass); + generateClass(REPLICA, astClass, replicaMetaTypeRegistrationCode); + generateClass(SOURCE, astClass, classMetaTypeRegistrationCode); + generateClass(SIMPLE_SOURCE, astClass, classMetaTypeRegistrationCode); + generateSourceAPI(astClass); } else { - generateClass(mode, stream, astClass, mode == REPLICA ? replicaMetaTypeRegistrationCode : classMetaTypeRegistrationCode); + generateClass(mode, astClass, mode == REPLICA ? replicaMetaTypeRegistrationCode + : classMetaTypeRegistrationCode); if (mode == SOURCE) { - generateClass(SIMPLE_SOURCE, stream, astClass, classMetaTypeRegistrationCode); - generateSourceAPI(stream, astClass); + generateClass(SIMPLE_SOURCE, astClass, classMetaTypeRegistrationCode); + generateSourceAPI(astClass); } } } - stream << Qt::endl; + m_stream << Qt::endl; if (!fileName.isEmpty()) - stream << "#endif // " << fileName << Qt::endl; + m_stream << "#endif // " << fileName << Qt::endl; } -void RepCodeGenerator::generateHeader(Mode mode, QTextStream &out, const AST &ast) +void RepCodeGenerator::generateHeader(Mode mode) { - out << "// This is an autogenerated file.\n" - "// Do not edit this file, any changes made will be lost the next time it is generated.\n" - "\n" - "#include <QtCore/qobject.h>\n" - "#include <QtCore/qdatastream.h>\n" - "#include <QtCore/qvariant.h>\n" - "#include <QtCore/qmetatype.h>\n"; + m_stream << + "// This is an autogenerated file.\n" + "// Do not edit this file, any changes made will be lost the next time it is generated.\n" + "\n" + "#include <QtCore/qobject.h>\n" + "#include <QtCore/qdatastream.h>\n" + "#include <QtCore/qvariant.h>\n" + "#include <QtCore/qmetatype.h>\n"; bool hasModel = false; - for (auto c : ast.classes) + for (auto c : m_ast.classes) { if (c.modelMetadata.count() > 0) { @@ -312,39 +311,44 @@ void RepCodeGenerator::generateHeader(Mode mode, QTextStream &out, const AST &as } } if (hasModel) - out << "#include <QtCore/qabstractitemmodel.h>\n"; - out << "\n" - "#include <QtRemoteObjects/qremoteobjectnode.h>\n"; + m_stream << "#include <QtCore/qabstractitemmodel.h>\n"; + m_stream << "\n" + "#include <QtRemoteObjects/qremoteobjectnode.h>\n"; if (mode == MERGED) { - out << "#include <QtRemoteObjects/qremoteobjectpendingcall.h>\n"; - out << "#include <QtRemoteObjects/qremoteobjectreplica.h>\n"; - out << "#include <QtRemoteObjects/qremoteobjectsource.h>\n"; + m_stream << "#include <QtRemoteObjects/qremoteobjectpendingcall.h>\n"; + m_stream << "#include <QtRemoteObjects/qremoteobjectreplica.h>\n"; + m_stream << "#include <QtRemoteObjects/qremoteobjectsource.h>\n"; if (hasModel) - out << "#include <QtRemoteObjects/qremoteobjectabstractitemmodelreplica.h>\n"; + m_stream << "#include <QtRemoteObjects/qremoteobjectabstractitemmodelreplica.h>\n"; } else if (mode == REPLICA) { - out << "#include <QtRemoteObjects/qremoteobjectpendingcall.h>\n"; - out << "#include <QtRemoteObjects/qremoteobjectreplica.h>\n"; + m_stream << "#include <QtRemoteObjects/qremoteobjectpendingcall.h>\n"; + m_stream << "#include <QtRemoteObjects/qremoteobjectreplica.h>\n"; if (hasModel) - out << "#include <QtRemoteObjects/qremoteobjectabstractitemmodelreplica.h>\n"; + m_stream << "#include <QtRemoteObjects/qremoteobjectabstractitemmodelreplica.h>\n"; } else - out << "#include <QtRemoteObjects/qremoteobjectsource.h>\n"; - out << "\n"; + m_stream << "#include <QtRemoteObjects/qremoteobjectsource.h>\n"; + m_stream << "\n"; - out << ast.preprocessorDirectives.join(QLatin1Char('\n')); - out << "\n"; + m_stream << m_ast.preprocessorDirectives.join(QLatin1Char('\n')); + m_stream << "\n"; } -static QString formatTemplateStringArgTypeNameCapitalizedName(int numberOfTypeOccurrences, int numberOfNameOccurrences, - QString templateString, const POD &pod) +static QString formatTemplateStringArgTypeNameCapitalizedName(int numberOfTypeOccurrences, + int numberOfNameOccurrences, + QString templateString, + const POD &pod) { QString out; const int LengthOfPlaceholderText = 2; - Q_ASSERT(templateString.count(QRegularExpression(QStringLiteral("%\\d"))) == numberOfNameOccurrences + numberOfTypeOccurrences); + Q_ASSERT(templateString.count(QRegularExpression(QStringLiteral("%\\d"))) + == numberOfNameOccurrences + numberOfTypeOccurrences); const auto expectedOutSize = numberOfNameOccurrences * accumulatedSizeOfNames(pod.attributes) + numberOfTypeOccurrences * accumulatedSizeOfTypes(pod.attributes) - + pod.attributes.size() * (templateString.size() - (numberOfNameOccurrences + numberOfTypeOccurrences) * LengthOfPlaceholderText); + + pod.attributes.size() * (templateString.size() + - (numberOfNameOccurrences + numberOfTypeOccurrences) + * LengthOfPlaceholderText); out.reserve(expectedOutSize); for (const PODAttribute &a : pod.attributes) out += templateString.arg(a.type, a.name, cap(a.name)); @@ -353,7 +357,8 @@ static QString formatTemplateStringArgTypeNameCapitalizedName(int numberOfTypeOc QString RepCodeGenerator::formatQPropertyDeclarations(const POD &pod) { - return formatTemplateStringArgTypeNameCapitalizedName(1, 3, QStringLiteral(" Q_PROPERTY(%1 %2 READ %2 WRITE set%3)\n"), pod); + QString prop = QStringLiteral(" Q_PROPERTY(%1 %2 READ %2 WRITE set%3)\n"); + return formatTemplateStringArgTypeNameCapitalizedName(1, 3, prop, pod); } QString RepCodeGenerator::formatConstructors(const POD &pod) @@ -377,9 +382,9 @@ QString RepCodeGenerator::formatConstructors(const POD &pod) QString RepCodeGenerator::formatPropertyGettersAndSetters(const POD &pod) { - // MSVC doesn't like adjacent string literal concatenation within QStringLiteral, so keep it in one line: QString templateString - = QStringLiteral(" %1 %2() const { return m_%2; }\n void set%3(%1 %2) { if (%2 != m_%2) { m_%2 = %2; } }\n"); + = QString::fromLatin1(" %1 %2() const { return m_%2; }\n" + " void set%3(%1 %2) { if (%2 != m_%2) { m_%2 = %2; } }\n"); return formatTemplateStringArgTypeNameCapitalizedName(2, 8, qMove(templateString), pod); } @@ -422,12 +427,14 @@ QString RepCodeGenerator::formatDebugOperator(const POD &pod) QString RepCodeGenerator::formatMarshallingOperators(const POD &pod) { - return QLatin1String("inline QDataStream &operator<<(QDataStream &ds, const ") + pod.name + QLatin1String(" &obj) {\n" + return QLatin1String("inline QDataStream &operator<<(QDataStream &ds, const ") + pod.name + + QLatin1String(" &obj) {\n" " QtRemoteObjects::copyStoredProperties(&obj, ds);\n" " return ds;\n" "}\n" "\n" - "inline QDataStream &operator>>(QDataStream &ds, ") + pod.name + QLatin1String(" &obj) {\n" + "inline QDataStream &operator>>(QDataStream &ds, ") + pod.name + + QLatin1String(" &obj) {\n" " QtRemoteObjects::copyStoredProperties(ds, &obj);\n" " return ds;\n" "}\n") @@ -440,7 +447,8 @@ QString RepCodeGenerator::typeForMode(const ASTProperty &property, RepCodeGenera return property.type; if (property.type.startsWith(QStringLiteral("QAbstractItemModel"))) - return mode == REPLICA ? property.type + QStringLiteral("Replica*") : property.type + QStringLiteral("*"); + return mode == REPLICA ? property.type + QStringLiteral("Replica*") + : property.type + QStringLiteral("*"); switch (mode) { case REPLICA: return property.type + QStringLiteral("Replica*"); @@ -453,25 +461,27 @@ QString RepCodeGenerator::typeForMode(const ASTProperty &property, RepCodeGenera return QStringLiteral("InvalidPropertyName"); } -void RepCodeGenerator::generateSimpleSetter(QTextStream &out, const ASTProperty &property, bool generateOverride) +void RepCodeGenerator::generateSimpleSetter(const ASTProperty &property, bool generateOverride) { if (!generateOverride) - out << " virtual "; + m_stream << " virtual "; else - out << " "; - out << "void set" << cap(property.name) << "(" << typeForMode(property, SIMPLE_SOURCE) << " " << property.name << ")"; + m_stream << " "; + m_stream << "void set" << cap(property.name) << "(" << typeForMode(property, SIMPLE_SOURCE) + << " " << property.name << ")"; if (generateOverride) - out << " override"; - out << Qt::endl; - out << " {" << Qt::endl; - out << " if (" << property.name << " != m_" << property.name << ") {" << Qt::endl; - out << " m_" << property.name << " = " << property.name << ";" << Qt::endl; - out << " Q_EMIT " << property.name << "Changed(m_" << property.name << ");" << Qt::endl; - out << " }" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " override"; + m_stream << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " if (" << property.name << " != m_" << property.name << ") {" << Qt::endl; + m_stream << " m_" << property.name << " = " << property.name << ";" << Qt::endl; + m_stream << " Q_EMIT " << property.name << "Changed(m_" << property.name << ");" + << Qt::endl; + m_stream << " }" << Qt::endl; + m_stream << " }" << Qt::endl; } -void RepCodeGenerator::generatePOD(QTextStream &out, const POD &pod) +void RepCodeGenerator::generatePOD(const POD &pod) { QByteArray podData = pod.name.toLatin1(); QStringList equalityCheck; @@ -480,30 +490,30 @@ void RepCodeGenerator::generatePOD(QTextStream &out, const POD &pod) podData += attr.name.toLatin1() + typeData(attr.type, m_globalTypes); } m_globalTypes[pod.name] = podData; - out << "class " << pod.name << "\n" - "{\n" - " Q_GADGET\n" - << "\n" - << formatQPropertyDeclarations(pod) - << "public:\n" - << formatConstructors(pod) - << formatPropertyGettersAndSetters(pod) - << "private:\n" - << formatDataMembers(pod) - << "};\n" - << "\n" - << "inline bool operator==(const " << pod.name << " &left, const " << pod.name << " &right) Q_DECL_NOTHROW {\n" - << " return " << equalityCheck.join(QStringLiteral(" && ")) << ";\n" - << "}\n" - << "inline bool operator!=(const " << pod.name << " &left, const " << pod.name << " &right) Q_DECL_NOTHROW {\n" - << " return !(left == right);\n" - << "}\n" - << "\n" - << formatDebugOperator(pod) - << formatMarshallingOperators(pod) - << "\n" - "\n" - ; + m_stream << "class " << pod.name << "\n" + "{\n" + " Q_GADGET\n" + << "\n" + << formatQPropertyDeclarations(pod) + << "public:\n" + << formatConstructors(pod) + << formatPropertyGettersAndSetters(pod) + << "private:\n" + << formatDataMembers(pod) + << "};\n" + << "\n" + << "inline bool operator==(const " << pod.name << " &left, const " << pod.name << + " &right) Q_DECL_NOTHROW {\n" + << " return " << equalityCheck.join(QStringLiteral(" && ")) << ";\n" + << "}\n" + << "inline bool operator!=(const " << pod.name << " &left, const " << pod.name << + " &right) Q_DECL_NOTHROW {\n" + << " return !(left == right);\n" + << "}\n" + << "\n" + << formatDebugOperator(pod) + << formatMarshallingOperators(pod) + << "\n\n"; } QString getEnumType(const ASTEnum &en) @@ -525,49 +535,51 @@ QString getEnumType(const ASTEnum &en) } } -void RepCodeGenerator::generateDeclarationsForEnums(QTextStream &out, const QList<ASTEnum> &enums, bool generateQENUM) +void RepCodeGenerator::generateDeclarationsForEnums(const QList<ASTEnum> &enums, + bool generateQENUM) { if (!generateQENUM) { - out << " // You need to add this enum as well as Q_ENUM to your" << Qt::endl; - out << " // QObject class in order to use .rep enums over QtRO for" << Qt::endl; - out << " // non-repc generated QObjects." << Qt::endl; + m_stream << " // You need to add this enum as well as Q_ENUM to your" << Qt::endl; + m_stream << " // QObject class in order to use .rep enums over QtRO for" << Qt::endl; + m_stream << " // non-repc generated QObjects." << Qt::endl; } for (const ASTEnum &en : enums) { m_globalTypes[en.name] = enumSignature(en); - out << " enum " << (en.isScoped ? "class " : "") << en.name << (en.type.isEmpty() ? "" : " : ") << en.type << " {\n"; + m_stream << " enum " << (en.isScoped ? "class " : "") << en.name + << (en.type.isEmpty() ? "" : " : ") << en.type << " {\n"; for (const ASTEnumParam &p : en.params) - out << " " << p.name << " = " << p.value << ",\n"; + m_stream << " " << p.name << " = " << p.value << ",\n"; - out << " };\n"; + m_stream << " };\n"; if (generateQENUM) - out << " Q_ENUM(" << en.name << ")\n"; + m_stream << " Q_ENUM(" << en.name << ")\n"; } } -void RepCodeGenerator::generateEnumGadget(QTextStream &out, const ASTEnum &en, const ASTFlag &flag, const QString &className) +void RepCodeGenerator::generateEnumGadget(const ASTEnum &en, const QString &className) { - out << "class " << className << "\n" - "{\n" - " Q_GADGET\n"; + m_stream << "class " << className << "\n" + "{\n" + " Q_GADGET\n"; if (en.isScoped) - out << " Q_CLASSINFO(\"RegisterEnumClassesUnscoped\", \"false\")\n"; - out << " " << className << "();\n" - "\n" - "public:\n"; + m_stream << " Q_CLASSINFO(\"RegisterEnumClassesUnscoped\", \"false\")\n"; + m_stream << " " << className << "();\n" + "\n" + "public:\n"; auto enums = QList<ASTEnum>() << en; - generateDeclarationsForEnums(out, enums); - if (flag.isValid()) { - out << " Q_DECLARE_FLAGS(" << flag.name << ", " << flag._enum << ")\n"; - out << " Q_FLAG(" << flag.name << ")\n"; + generateDeclarationsForEnums(enums); + if (en.flagIndex >= 0) { + auto flag = m_ast.flags.at(en.flagIndex); + m_stream << " Q_DECLARE_FLAGS(" << flag.name << ", " << flag._enum << ")\n"; + m_stream << " Q_FLAG(" << flag.name << ")\n"; + m_stream << "};\n\n"; + m_stream << "Q_DECLARE_OPERATORS_FOR_FLAGS(" << className << "::" << flag.name << ")\n\n"; + } else { + m_stream << "};\n\n"; } - - out << "};\n\n"; - - if (flag.isValid()) - out << "Q_DECLARE_OPERATORS_FOR_FLAGS(" << className << "::" << flag.name << ")\n\n"; } QString RepCodeGenerator::generateMetaTypeRegistration(const QSet<QString> &metaTypes) @@ -591,9 +603,14 @@ QString RepCodeGenerator::generateMetaTypeRegistrationForPending(const QSet<QStr QString out; if (!metaTypes.isEmpty()) out += QLatin1String(" qRegisterMetaType<QRemoteObjectPendingCall>();\n"); - const QString qRegisterMetaType = QStringLiteral(" qRegisterMetaType<QRemoteObjectPendingReply<%1>>();\n"); - const QString qRegisterConverterConditional = QStringLiteral(" if (!QMetaType::hasRegisteredConverterFunction<QRemoteObjectPendingReply<%1>, QRemoteObjectPendingCall>())\n"); - const QString qRegisterConverter = QStringLiteral(" QMetaType::registerConverter<QRemoteObjectPendingReply<%1>, QRemoteObjectPendingCall>();\n"); + const QString qRegisterMetaType = + QStringLiteral(" qRegisterMetaType<QRemoteObjectPendingReply<%1>>();\n"); + const QString qRegisterConverterConditional = + QStringLiteral(" if (!QMetaType::hasRegisteredConverterFunction<" + "QRemoteObjectPendingReply<%1>, QRemoteObjectPendingCall>())\n"); + const QString qRegisterConverter = + QStringLiteral(" QMetaType::registerConverter<QRemoteObjectPendingReply<%1>" + ", QRemoteObjectPendingCall>();\n"); for (const QString &metaType : metaTypes) { out += qRegisterMetaType.arg(metaType); out += qRegisterConverterConditional.arg(metaType); @@ -602,23 +619,29 @@ QString RepCodeGenerator::generateMetaTypeRegistrationForPending(const QSet<QStr return out; } -void RepCodeGenerator::generateClass(Mode mode, QTextStream &out, const ASTClass &astClass, const QString &metaTypeRegistrationCode) +void RepCodeGenerator::generateClass(Mode mode, const ASTClass &astClass, + const QString &metaTypeRegistrationCode) { - const QString className = (astClass.name + (mode == REPLICA ? QStringLiteral("Replica") : mode == SOURCE ? QStringLiteral("Source") : QStringLiteral("SimpleSource"))); + const QString className = (astClass.name + (mode == REPLICA ? + QStringLiteral("Replica") : mode == SOURCE ? + QStringLiteral("Source") : QStringLiteral("SimpleSource"))); if (mode == REPLICA) - out << "class " << className << " : public QRemoteObjectReplica" << Qt::endl; + m_stream << "class " << className << " : public QRemoteObjectReplica" << Qt::endl; else if (mode == SIMPLE_SOURCE) - out << "class " << className << " : public " << astClass.name << "Source" << Qt::endl; + m_stream << "class " << className << " : public " << astClass.name << "Source" + << Qt::endl; else - out << "class " << className << " : public QObject" << Qt::endl; + m_stream << "class " << className << " : public QObject" << Qt::endl; - out << "{\n"; - out << " Q_OBJECT\n"; + m_stream << "{\n"; + m_stream << " Q_OBJECT\n"; if (hasScopedEnum(astClass)) // See https://bugreports.qt.io/browse/QTBUG-73360 - out << " Q_CLASSINFO(\"RegisterEnumClassesUnscoped\", \"false\")\n"; + m_stream << " Q_CLASSINFO(\"RegisterEnumClassesUnscoped\", \"false\")\n"; if (mode != SIMPLE_SOURCE) { - out << " Q_CLASSINFO(QCLASSINFO_REMOTEOBJECT_TYPE, \"" << astClass.name << "\")" << Qt::endl; - out << " Q_CLASSINFO(QCLASSINFO_REMOTEOBJECT_SIGNATURE, \"" << QLatin1String(classSignature(astClass)) << "\")" << Qt::endl; + m_stream << " Q_CLASSINFO(QCLASSINFO_REMOTEOBJECT_TYPE, \"" << astClass.name + << "\")" << Qt::endl; + m_stream << " Q_CLASSINFO(QCLASSINFO_REMOTEOBJECT_SIGNATURE, \"" + << QLatin1String(classSignature(astClass)) << "\")" << Qt::endl; for (int i = 0; i < astClass.modelMetadata.count(); i++) { const auto model = astClass.modelMetadata.at(i); const auto modelName = astClass.properties.at(model.propertyIndex).name; @@ -626,134 +649,161 @@ void RepCodeGenerator::generateClass(Mode mode, QTextStream &out, const ASTClass QStringList list; for (auto role : model.roles) list << role.name; - out << QString::fromLatin1(" Q_CLASSINFO(\"%1_ROLES\", \"%2\")").arg(modelName.toUpper(), list.join(QChar::fromLatin1('|'))) << Qt::endl; + m_stream << QString::fromLatin1(" Q_CLASSINFO(\"%1_ROLES\", \"%2\")") + .arg(modelName.toUpper(), list.join(QChar::fromLatin1('|'))) + << Qt::endl; } } //First output properties for (const ASTProperty &property : astClass.properties) { - out << " Q_PROPERTY(" << typeForMode(property, mode) << " " << property.name << " READ " << property.name; + m_stream << " Q_PROPERTY(" << typeForMode(property, mode) << " " << property.name + << " READ " << property.name; if (property.modifier == ASTProperty::Constant) { if (mode == REPLICA) // We still need to notify when we get the initial value - out << " NOTIFY " << property.name << "Changed"; + m_stream << " NOTIFY " << property.name << "Changed"; else - out << " CONSTANT"; + m_stream << " CONSTANT"; } else if (property.modifier == ASTProperty::ReadOnly) - out << " NOTIFY " << property.name << "Changed"; + m_stream << " NOTIFY " << property.name << "Changed"; else if (property.modifier == ASTProperty::ReadWrite) - out << " WRITE set" << cap(property.name) << " NOTIFY " << property.name << "Changed"; - else if (property.modifier == ASTProperty::ReadPush || property.modifier == ASTProperty::SourceOnlySetter) { + m_stream << " WRITE set" << cap(property.name) << " NOTIFY " << property.name + << "Changed"; + else if (property.modifier == ASTProperty::ReadPush || + property.modifier == ASTProperty::SourceOnlySetter) { if (mode == REPLICA) // The setter slot isn't known to the PROP - out << " NOTIFY " << property.name << "Changed"; + m_stream << " NOTIFY " << property.name << "Changed"; else // The Source can use the setter, since non-asynchronous - out << " WRITE set" << cap(property.name) << " NOTIFY " << property.name << "Changed"; + m_stream << " WRITE set" << cap(property.name) << " NOTIFY " + << property.name << "Changed"; } - out << ")" << Qt::endl; + m_stream << ")" << Qt::endl; } if (!astClass.enums.isEmpty()) { - out << "" << Qt::endl; - out << "public:" << Qt::endl; - generateDeclarationsForEnums(out, astClass.enums); + m_stream << "" << Qt::endl; + m_stream << "public:" << Qt::endl; + generateDeclarationsForEnums(astClass.enums); for (const auto &flag : astClass.flags) { - out << " Q_DECLARE_FLAGS(" << flag.name << ", " << flag._enum << ")\n"; - out << " Q_FLAG(" << flag.name << ")\n"; + m_stream << " Q_DECLARE_FLAGS(" << flag.name << ", " << flag._enum << ")\n"; + m_stream << " Q_FLAG(" << flag.name << ")\n"; } } } - out << "" << Qt::endl; - out << "public:" << Qt::endl; + m_stream << "" << Qt::endl; + m_stream << "public:" << Qt::endl; if (mode == REPLICA) { - out << " " << className << "() : QRemoteObjectReplica() { initialize(); }" << Qt::endl; - out << " static void registerMetatypes()" << Qt::endl; - out << " {" << Qt::endl; - out << " static bool initialized = false;" << Qt::endl; - out << " if (initialized)" << Qt::endl; - out << " return;" << Qt::endl; - out << " initialized = true;" << Qt::endl; + m_stream << " " << className << "() : QRemoteObjectReplica() { initialize(); }" + << Qt::endl; + m_stream << " static void registerMetatypes()" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " static bool initialized = false;" << Qt::endl; + m_stream << " if (initialized)" << Qt::endl; + m_stream << " return;" << Qt::endl; + m_stream << " initialized = true;" << Qt::endl; if (!metaTypeRegistrationCode.isEmpty()) - out << metaTypeRegistrationCode << Qt::endl; + m_stream << metaTypeRegistrationCode << Qt::endl; - out << " }" << Qt::endl; + m_stream << " }" << Qt::endl; if (astClass.hasPointerObjects()) { - out << " void setNode(QRemoteObjectNode *node) override" << Qt::endl; - out << " {" << Qt::endl; - out << " QRemoteObjectReplica::setNode(node);" << Qt::endl; + m_stream << " void setNode(QRemoteObjectNode *node) override" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " QRemoteObjectReplica::setNode(node);" << Qt::endl; for (int index = 0; index < astClass.properties.count(); ++index) { const ASTProperty &property = astClass.properties.at(index); if (!property.isPointer) continue; const QString acquireName = astClass.name + QLatin1String("::") + property.name; if (astClass.subClassPropertyIndices.contains(index)) - out << QString::fromLatin1(" setChild(%1, QVariant::fromValue(node->acquire<%2Replica>(QRemoteObjectStringLiterals::CLASS().arg(\"%3\"))));") - .arg(QString::number(index), property.type, acquireName) << Qt::endl; + m_stream << QString::fromLatin1(" setChild(%1, QVariant::fromValue(" + "node->acquire<%2Replica>(QRemoteObjectStringLiterals::CLASS()" + ".arg(\"%3\"))));") + .arg(QString::number(index), property.type, acquireName) + << Qt::endl; else - out << QString::fromLatin1(" setChild(%1, QVariant::fromValue(node->acquireModel(QRemoteObjectStringLiterals::MODEL().arg(\"%2\"))));") - .arg(QString::number(index), acquireName) << Qt::endl; - out << " Q_EMIT " << property.name << "Changed(" << property.name << "()" << ");" << Qt::endl; + m_stream << QString::fromLatin1(" setChild(%1, QVariant::fromValue(" + "node->acquireModel(QRemoteObjectStringLiterals::MODEL()" + ".arg(\"%2\"))));") + .arg(QString::number(index), acquireName) << Qt::endl; + m_stream << " Q_EMIT " << property.name << "Changed(" << property.name + << "()" << ");" << Qt::endl; } - out << " }" << Qt::endl; + m_stream << " }" << Qt::endl; } - out << "" << Qt::endl; - out << "private:" << Qt::endl; - out << " " << className << "(QRemoteObjectNode *node, const QString &name = QString())" << Qt::endl; - out << " : QRemoteObjectReplica(ConstructWithNode)" << Qt::endl; - out << " {" << Qt::endl; - out << " initializeNode(node, name);" << Qt::endl; + m_stream << "" << Qt::endl; + m_stream << "private:" << Qt::endl; + m_stream << " " << className + << "(QRemoteObjectNode *node, const QString &name = QString())" << Qt::endl; + m_stream << " : QRemoteObjectReplica(ConstructWithNode)" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " initializeNode(node, name);" << Qt::endl; for (int index = 0; index < astClass.properties.count(); ++index) { const ASTProperty &property = astClass.properties.at(index); if (!property.isPointer) continue; const QString acquireName = astClass.name + QLatin1String("::") + property.name; if (astClass.subClassPropertyIndices.contains(index)) - out << QString::fromLatin1(" setChild(%1, QVariant::fromValue(node->acquire<%2Replica>(QRemoteObjectStringLiterals::CLASS().arg(\"%3\"))));") - .arg(QString::number(index), property.type, acquireName) << Qt::endl; + m_stream << QString::fromLatin1(" setChild(%1, QVariant::fromValue(" + "node->acquire<%2Replica>(QRemoteObjectStringLiterals::CLASS()" + ".arg(\"%3\"))));") + .arg(QString::number(index), property.type, acquireName) << Qt::endl; else - out << QString::fromLatin1(" setChild(%1, QVariant::fromValue(node->acquireModel(QRemoteObjectStringLiterals::MODEL().arg(\"%2\"))));") - .arg(QString::number(index), acquireName) << Qt::endl; + m_stream << QString::fromLatin1(" setChild(%1, QVariant::fromValue(" + "node->acquireModel(QRemoteObjectStringLiterals::MODEL()" + ".arg(\"%2\"))));") + .arg(QString::number(index), acquireName) << Qt::endl; } - out << " }" << Qt::endl; + m_stream << " }" << Qt::endl; - out << "" << Qt::endl; + m_stream << "" << Qt::endl; - out << " void initialize() override" << Qt::endl; - out << " {" << Qt::endl; - out << " " << className << "::registerMetatypes();" << Qt::endl; - out << " QVariantList properties;" << Qt::endl; - out << " properties.reserve(" << astClass.properties.size() << ");" << Qt::endl; + m_stream << " void initialize() override" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " " << className << "::registerMetatypes();" << Qt::endl; + m_stream << " QVariantList properties;" << Qt::endl; + m_stream << " properties.reserve(" << astClass.properties.size() << ");" + << Qt::endl; for (const ASTProperty &property : astClass.properties) { if (property.isPointer) - out << " properties << QVariant::fromValue((" << typeForMode(property, mode) << ")" << property.defaultValue << ");" << Qt::endl; + m_stream << " properties << QVariant::fromValue((" + << typeForMode(property, mode) << ")" << property.defaultValue + << ");" << Qt::endl; else - out << " properties << QVariant::fromValue(" << typeForMode(property, mode) << "(" << property.defaultValue << "));" << Qt::endl; + m_stream << " properties << QVariant::fromValue(" + << typeForMode(property, mode) << "(" << property.defaultValue + << "));" << Qt::endl; } int nPersisted = 0; if (astClass.hasPersisted) { - out << " QVariantList stored = retrieveProperties(QStringLiteral(\"" << astClass.name << "\"), \"" << classSignature(astClass) << "\");" << Qt::endl; - out << " if (!stored.isEmpty()) {" << Qt::endl; + m_stream << " QVariantList stored = retrieveProperties(QStringLiteral(\"" + << astClass.name << "\"), \"" << classSignature(astClass) << "\");" + << Qt::endl; + m_stream << " if (!stored.isEmpty()) {" << Qt::endl; for (int i = 0; i < astClass.properties.size(); i++) { if (astClass.properties.at(i).persisted) { - out << " properties[" << i << "] = stored.at(" << nPersisted << ");" << Qt::endl; + m_stream << " properties[" << i << "] = stored.at(" << nPersisted + << ");" << Qt::endl; nPersisted++; } } - out << " }" << Qt::endl; + m_stream << " }" << Qt::endl; } - out << " setProperties(std::move(properties));" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " setProperties(std::move(properties));" << Qt::endl; + m_stream << " }" << Qt::endl; } else if (mode == SOURCE) { - out << " explicit " << className << "(QObject *parent = nullptr) : QObject(parent)" << Qt::endl; - out << " {" << Qt::endl; + m_stream << " explicit " << className + << "(QObject *parent = nullptr) : QObject(parent)" << Qt::endl; + m_stream << " {" << Qt::endl; if (!metaTypeRegistrationCode.isEmpty()) - out << metaTypeRegistrationCode << Qt::endl; - out << " }" << Qt::endl; + m_stream << metaTypeRegistrationCode << Qt::endl; + m_stream << " }" << Qt::endl; } else { QList<int> constIndices; for (int index = 0; index < astClass.properties.count(); ++index) { @@ -762,43 +812,51 @@ void RepCodeGenerator::generateClass(Mode mode, QTextStream &out, const ASTClass constIndices.append(index); } if (constIndices.isEmpty()) { - out << " explicit " << className << "(QObject *parent = nullptr) : " << astClass.name << "Source(parent)" << Qt::endl; + m_stream << " explicit " << className << "(QObject *parent = nullptr) : " + << astClass.name << "Source(parent)" << Qt::endl; } else { QStringList parameters; for (int index : constIndices) { const ASTProperty &property = astClass.properties.at(index); - parameters.append(QString::fromLatin1("%1 %2 = %3").arg(typeForMode(property, SOURCE), property.name, property.defaultValue)); + parameters.append(QString::fromLatin1("%1 %2 = %3") + .arg(typeForMode(property, SOURCE), property.name, + property.defaultValue)); } parameters.append(QStringLiteral("QObject *parent = nullptr")); - out << " explicit " << className << "(" << parameters.join(QStringLiteral(", ")) << ") : " << astClass.name << "Source(parent)" << Qt::endl; + m_stream << " explicit " << className << "(" + << parameters.join(QStringLiteral(", ")) << ") : " << astClass.name + << "Source(parent)" << Qt::endl; } for (const ASTProperty &property : astClass.properties) { if (property.modifier == ASTProperty::Constant) - out << " , m_" << property.name << "(" << property.name << ")" << Qt::endl; + m_stream << " , m_" << property.name << "(" << property.name << ")" + << Qt::endl; else - out << " , m_" << property.name << "(" << property.defaultValue << ")" << Qt::endl; + m_stream << " , m_" << property.name << "(" << property.defaultValue << ")" + << Qt::endl; } - out << " {" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " }" << Qt::endl; } - out << "" << Qt::endl; - out << "public:" << Qt::endl; + m_stream << "" << Qt::endl; + m_stream << "public:" << Qt::endl; if (mode == REPLICA && astClass.hasPersisted) { - out << " ~" << className << "() override {" << Qt::endl; - out << " QVariantList persisted;" << Qt::endl; + m_stream << " ~" << className << "() override {" << Qt::endl; + m_stream << " QVariantList persisted;" << Qt::endl; for (int i = 0; i < astClass.properties.size(); i++) { if (astClass.properties.at(i).persisted) { - out << " persisted << propAsVariant(" << i << ");" << Qt::endl; + m_stream << " persisted << propAsVariant(" << i << ");" << Qt::endl; } } - out << " persistProperties(QStringLiteral(\"" << astClass.name << "\"), \"" << classSignature(astClass) << "\", persisted);" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " persistProperties(QStringLiteral(\"" << astClass.name << "\"), \"" + << classSignature(astClass) << "\", persisted);" << Qt::endl; + m_stream << " }" << Qt::endl; } else { - out << " ~" << className << "() override = default;" << Qt::endl; + m_stream << " ~" << className << "() override = default;" << Qt::endl; } - out << "" << Qt::endl; + m_stream << "" << Qt::endl; //Next output getter/setter if (mode == REPLICA) { @@ -806,51 +864,62 @@ void RepCodeGenerator::generateClass(Mode mode, QTextStream &out, const ASTClass for (const ASTProperty &property : astClass.properties) { auto type = typeForMode(property, mode); if (type == QLatin1String("QVariant")) { - out << " " << type << " " << property.name << "() const" << Qt::endl; - out << " {" << Qt::endl; - out << " return propAsVariant(" << i << ");" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " " << type << " " << property.name << "() const" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " return propAsVariant(" << i << ");" << Qt::endl; + m_stream << " }" << Qt::endl; } else { - out << " " << type << " " << property.name << "() const" << Qt::endl; - out << " {" << Qt::endl; - out << " const QVariant variant = propAsVariant(" << i << ");" << Qt::endl; - out << " if (!variant.canConvert<" << type << ">()) {" << Qt::endl; - out << " qWarning() << \"QtRO cannot convert the property " << property.name << " to type " << type << "\";" << Qt::endl; - out << " }" << Qt::endl; - out << " return variant.value<" << type << " >();" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " " << type << " " << property.name << "() const" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " const QVariant variant = propAsVariant(" << i << ");" + << Qt::endl; + m_stream << " if (!variant.canConvert<" << type << ">()) {" << Qt::endl; + m_stream << " qWarning() << \"QtRO cannot convert the property " + << property.name << " to type " << type << "\";" << Qt::endl; + m_stream << " }" << Qt::endl; + m_stream << " return variant.value<" << type << " >();" << Qt::endl; + m_stream << " }" << Qt::endl; } i++; if (property.modifier == ASTProperty::ReadWrite) { - out << "" << Qt::endl; - out << " void set" << cap(property.name) << "(" << property.type << " " << property.name << ")" << Qt::endl; - out << " {" << Qt::endl; - out << " static int __repc_index = " << className << "::staticMetaObject.indexOfProperty(\"" << property.name << "\");" << Qt::endl; - out << " QVariantList __repc_args;" << Qt::endl; - out << " __repc_args << QVariant::fromValue(" << property.name << ");" << Qt::endl; - out << " send(QMetaObject::WriteProperty, __repc_index, __repc_args);" << Qt::endl; - out << " }" << Qt::endl; + m_stream << "" << Qt::endl; + m_stream << " void set" << cap(property.name) << "(" << property.type << " " + << property.name << ")" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " static int __repc_index = " << className + << "::staticMetaObject.indexOfProperty(\"" << property.name << "\");" + << Qt::endl; + m_stream << " QVariantList __repc_args;" << Qt::endl; + m_stream << " __repc_args << QVariant::fromValue(" << property.name << ");" + << Qt::endl; + m_stream << " send(QMetaObject::WriteProperty, __repc_index, __repc_args);" + << Qt::endl; + m_stream << " }" << Qt::endl; } - out << "" << Qt::endl; + m_stream << "" << Qt::endl; } } else if (mode == SOURCE) { for (const ASTProperty &property : astClass.properties) - out << " virtual " << typeForMode(property, mode) << " " << property.name << "() const = 0;" << Qt::endl; + m_stream << " virtual " << typeForMode(property, mode) << " " << property.name + << "() const = 0;" << Qt::endl; for (const ASTProperty &property : astClass.properties) { if (property.modifier == ASTProperty::ReadWrite || property.modifier == ASTProperty::ReadPush || property.modifier == ASTProperty::SourceOnlySetter) - out << " virtual void set" << cap(property.name) << "(" << typeForMode(property, mode) << " " << property.name << ") = 0;" << Qt::endl; + m_stream << " virtual void set" << cap(property.name) << "(" + << typeForMode(property, mode) << " " << property.name << ") = 0;" + << Qt::endl; } } else { for (const ASTProperty &property : astClass.properties) - out << " " << typeForMode(property, mode) << " " << property.name << "() const override { return m_" + m_stream << " " << typeForMode(property, mode) << " " << property.name + << "() const override { return m_" << property.name << "; }" << Qt::endl; for (const ASTProperty &property : astClass.properties) { if (property.modifier == ASTProperty::ReadWrite || property.modifier == ASTProperty::ReadPush || property.modifier == ASTProperty::SourceOnlySetter) { - generateSimpleSetter(out, property); + generateSimpleSetter(property); } } } @@ -858,23 +927,31 @@ void RepCodeGenerator::generateClass(Mode mode, QTextStream &out, const ASTClass if (mode != SIMPLE_SOURCE) { //Next output property signals if (!astClass.properties.isEmpty() || !astClass.signalsList.isEmpty()) { - out << "" << Qt::endl; - out << "Q_SIGNALS:" << Qt::endl; + m_stream << "" << Qt::endl; + m_stream << "Q_SIGNALS:" << Qt::endl; for (const ASTProperty &property : astClass.properties) { if (property.modifier != ASTProperty::Constant) - out << " void " << property.name << "Changed(" << fullyQualifiedTypeName(astClass, className, typeForMode(property, mode)) << " " << property.name << ");" << Qt::endl; + m_stream + << " void " << property.name << "Changed(" + << fullyQualifiedName(astClass, className, typeForMode(property, mode)) + << " " << property.name << ");" << Qt::endl; } - const QList<ASTFunction> signalsList = transformEnumParams(astClass, astClass.signalsList, className); + const auto signalsList = transformEnumParams(astClass, astClass.signalsList, + className); for (const ASTFunction &signal : signalsList) - out << " void " << signal.name << "(" << signal.paramsAsString() << ");" << Qt::endl; + m_stream << " void " << signal.name << "(" << signal.paramsAsString() << ");" + << Qt::endl; // CONSTANT source properties still need an onChanged signal on the Replica side to // update (once) when the value is initialized. Put these last, so they don't mess // up the signal index order for (const ASTProperty &property : astClass.properties) { if (mode == REPLICA && property.modifier == ASTProperty::Constant) - out << " void " << property.name << "Changed(" << fullyQualifiedTypeName(astClass, className, typeForMode(property, mode)) << " " << property.name << ");" << Qt::endl; + m_stream + << " void " << property.name << "Changed(" + << fullyQualifiedName(astClass, className, typeForMode(property, mode)) + << " " << property.name << ");" << Qt::endl; } } bool hasWriteSlots = false; @@ -885,55 +962,72 @@ void RepCodeGenerator::generateClass(Mode mode, QTextStream &out, const ASTClass } } if (hasWriteSlots || !astClass.slotsList.isEmpty()) { - out << "" << Qt::endl; - out << "public Q_SLOTS:" << Qt::endl; + m_stream << "" << Qt::endl; + m_stream << "public Q_SLOTS:" << Qt::endl; for (const ASTProperty &property : astClass.properties) { if (property.modifier == ASTProperty::ReadPush) { - const auto type = fullyQualifiedTypeName(astClass, className, property.type); + const auto type = fullyQualifiedName(astClass, className, property.type); if (mode != REPLICA) { - out << " virtual void push" << cap(property.name) << "(" << type << " " << property.name << ")" << Qt::endl; - out << " {" << Qt::endl; - out << " set" << cap(property.name) << "(" << property.name << ");" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " virtual void push" << cap(property.name) << "(" << type + << " " << property.name << ")" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " set" << cap(property.name) << "(" << property.name + << ");" << Qt::endl; + m_stream << " }" << Qt::endl; } else { - out << " void push" << cap(property.name) << "(" << type << " " << property.name << ")" << Qt::endl; - out << " {" << Qt::endl; - out << " static int __repc_index = " << className << "::staticMetaObject.indexOfSlot(\"push" << cap(property.name) << "(" << type << ")\");" << Qt::endl; - out << " QVariantList __repc_args;" << Qt::endl; - out << " __repc_args << QVariant::fromValue(" << property.name << ");" << Qt::endl; - out << " send(QMetaObject::InvokeMetaMethod, __repc_index, __repc_args);" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " void push" << cap(property.name) << "(" << type << " " + << property.name << ")" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " static int __repc_index = " << className + << "::staticMetaObject.indexOfSlot(\"push" << cap(property.name) + << "(" << type << ")\");" << Qt::endl; + m_stream << " QVariantList __repc_args;" << Qt::endl; + m_stream << " __repc_args << QVariant::fromValue(" << property.name + << ");" << Qt::endl; + m_stream << " send(QMetaObject::InvokeMetaMethod, __repc_index," + << " __repc_args);" << Qt::endl; + m_stream << " }" << Qt::endl; } } } - const QList<ASTFunction> slotsList = transformEnumParams(astClass, astClass.slotsList, className); + const auto slotsList = transformEnumParams(astClass, astClass.slotsList, className); for (const ASTFunction &slot : slotsList) { - const auto returnType = fullyQualifiedTypeName(astClass, className, slot.returnType); + const auto returnType = fullyQualifiedName(astClass, className, slot.returnType); if (mode != REPLICA) { - out << " virtual " << returnType << " " << slot.name << "(" << slot.paramsAsString() << ") = 0;" << Qt::endl; + m_stream << " virtual " << returnType << " " << slot.name << "(" + << slot.paramsAsString() << ") = 0;" << Qt::endl; } else { // TODO: Discuss whether it is a good idea to special-case for void here, const bool isVoid = slot.returnType == QStringLiteral("void"); if (isVoid) - out << " void " << slot.name << "(" << slot.paramsAsString() << ")" << Qt::endl; + m_stream << " void " << slot.name << "(" << slot.paramsAsString() + << ")" << Qt::endl; else - out << " QRemoteObjectPendingReply<" << returnType << "> " << slot.name << "(" << slot.paramsAsString()<< ")" << Qt::endl; - out << " {" << Qt::endl; - out << " static int __repc_index = " << className << "::staticMetaObject.indexOfSlot(\"" << slot.name << "(" << slot.paramsAsString(ASTFunction::Normalized) << ")\");" << Qt::endl; - out << " QVariantList __repc_args;" << Qt::endl; + m_stream << " QRemoteObjectPendingReply<" << returnType << "> " + << slot.name << "(" << slot.paramsAsString()<< ")" << Qt::endl; + m_stream << " {" << Qt::endl; + m_stream << " static int __repc_index = " << className + << "::staticMetaObject.indexOfSlot(\"" << slot.name << "(" + << slot.paramsAsString(ASTFunction::Normalized) << ")\");" + << Qt::endl; + m_stream << " QVariantList __repc_args;" << Qt::endl; const auto ¶mNames = slot.paramNames(); if (!paramNames.isEmpty()) { - out << " __repc_args" << Qt::endl; + m_stream << " __repc_args" << Qt::endl; for (const QString &name : paramNames) - out << " << " << "QVariant::fromValue(" << name << ")" << Qt::endl; - out << " ;" << Qt::endl; + m_stream << " << " << "QVariant::fromValue(" << name << ")" + << Qt::endl; + m_stream << " ;" << Qt::endl; } if (isVoid) - out << " send(QMetaObject::InvokeMetaMethod, __repc_index, __repc_args);" << Qt::endl; + m_stream << " send(QMetaObject::InvokeMetaMethod, __repc_index," + << " __repc_args);" << Qt::endl; else - out << " return QRemoteObjectPendingReply<" << returnType << ">(sendWithReply(QMetaObject::InvokeMetaMethod, __repc_index, __repc_args));" << Qt::endl; - out << " }" << Qt::endl; + m_stream << " return QRemoteObjectPendingReply<" << returnType + << ">(sendWithReply(QMetaObject::InvokeMetaMethod, __repc_index," + << " __repc_args));" << Qt::endl; + m_stream << " }" << Qt::endl; } } } @@ -943,107 +1037,138 @@ void RepCodeGenerator::generateClass(Mode mode, QTextStream &out, const ASTClass for (const ASTProperty &property : astClass.properties) { if (property.modifier == ASTProperty::ReadOnly) { if (addProtected) { - out << "" << Qt::endl; - out << "protected:" << Qt::endl; + m_stream << "" << Qt::endl; + m_stream << "protected:" << Qt::endl; addProtected = false; } - generateSimpleSetter(out, property, false); + generateSimpleSetter(property, false); } } } } - out << "" << Qt::endl; - out << "private:" << Qt::endl; + m_stream << "" << Qt::endl; + m_stream << "private:" << Qt::endl; //Next output data members if (mode == SIMPLE_SOURCE) { for (const ASTProperty &property : astClass.properties) - out << " " << typeForMode(property, SOURCE) << " " << "m_" << property.name << ";" << Qt::endl; + m_stream << " " << typeForMode(property, SOURCE) << " " << "m_" << property.name + << ";" << Qt::endl; } if (mode != SIMPLE_SOURCE) - out << " friend class QT_PREPEND_NAMESPACE(QRemoteObjectNode);" << Qt::endl; + m_stream << " friend class QT_PREPEND_NAMESPACE(QRemoteObjectNode);" << Qt::endl; - out << "};\n\n"; + m_stream << "};\n\n"; if (mode != SIMPLE_SOURCE) { for (const ASTFlag &flag : astClass.flags) - out << "Q_DECLARE_OPERATORS_FOR_FLAGS(" << className << "::" << flag.name << ")\n\n"; + m_stream << "Q_DECLARE_OPERATORS_FOR_FLAGS(" << className << "::" << flag.name + << ")\n\n"; } } -void RepCodeGenerator::generateSourceAPI(QTextStream &out, const ASTClass &astClass) +void RepCodeGenerator::generateSourceAPI(const ASTClass &astClass) { const QString className = astClass.name + QStringLiteral("SourceAPI"); - out << QStringLiteral("template <class ObjectType>") << Qt::endl; - out << QString::fromLatin1("struct %1 : public SourceApiMap").arg(className) << Qt::endl; - out << QStringLiteral("{") << Qt::endl; + m_stream << QStringLiteral("template <class ObjectType>") << Qt::endl; + m_stream << QString::fromLatin1("struct %1 : public SourceApiMap").arg(className) << Qt::endl; + m_stream << QStringLiteral("{") << Qt::endl; if (!astClass.enums.isEmpty()) { // Include enum definition in SourceAPI - generateDeclarationsForEnums(out, astClass.enums, false); + generateDeclarationsForEnums(astClass.enums, false); } for (const auto &flag : astClass.flags) - out << QLatin1String(" typedef QFlags<typename ObjectType::%1> %2;").arg(flag._enum, flag.name) << Qt::endl; - out << QString::fromLatin1(" %1(ObjectType *object, const QString &name = QLatin1String(\"%2\"))").arg(className, astClass.name) << Qt::endl; - out << QStringLiteral(" : SourceApiMap(), m_name(name)") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; + m_stream << QLatin1String(" typedef QFlags<typename ObjectType::%1> %2;") + .arg(flag._enum, flag.name) << Qt::endl; + m_stream << QString::fromLatin1(" %1(ObjectType *object, const QString &name = " + "QLatin1String(\"%2\"))").arg(className, astClass.name) + << Qt::endl; + m_stream << QStringLiteral(" : SourceApiMap(), m_name(name)") << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; if (!astClass.hasPointerObjects()) - out << QStringLiteral(" Q_UNUSED(object)") << Qt::endl; + m_stream << QStringLiteral(" Q_UNUSED(object)") << Qt::endl; const auto enumCount = astClass.enums.count(); const auto totalCount = enumCount + astClass.flags.count(); for (int i : astClass.subClassPropertyIndices) { const ASTProperty &child = astClass.properties.at(i); - out << QString::fromLatin1(" using %1_type_t = typename std::remove_pointer<decltype(object->%1())>::type;") - .arg(child.name) << Qt::endl; + m_stream << QString::fromLatin1(" using %1_type_t = typename std::remove_pointer<" + "decltype(object->%1())>::type;") + .arg(child.name) << Qt::endl; } - out << QString::fromLatin1(" m_enums[0] = %1;").arg(totalCount) << Qt::endl; + m_stream << QString::fromLatin1(" m_enums[0] = %1;").arg(totalCount) << Qt::endl; for (qsizetype i = 0; i < enumCount; ++i) { const auto enumerator = astClass.enums.at(i); - out << QString::fromLatin1(" m_enums[%1] = ObjectType::staticMetaObject.indexOfEnumerator(\"%2\");") - .arg(i+1).arg(enumerator.name) << Qt::endl; + m_stream << QString::fromLatin1(" m_enums[%1] = ObjectType::staticMetaObject." + "indexOfEnumerator(\"%2\");") + .arg(i+1).arg(enumerator.name) << Qt::endl; } for (qsizetype i = enumCount; i < totalCount; ++i) { const auto flag = astClass.flags.at(i - enumCount); - out << QString::fromLatin1(" m_enums[%1] = ObjectType::staticMetaObject.indexOfEnumerator(\"%2\");") - .arg(i+1).arg(flag.name) << Qt::endl; + m_stream << QString::fromLatin1(" m_enums[%1] = ObjectType::staticMetaObject." + "indexOfEnumerator(\"%2\");") + .arg(i+1).arg(flag.name) << Qt::endl; } const auto propCount = astClass.properties.count(); - out << QString::fromLatin1(" m_properties[0] = %1;").arg(propCount) << Qt::endl; + m_stream << QString::fromLatin1(" m_properties[0] = %1;").arg(propCount) << Qt::endl; QList<ASTProperty> onChangeProperties; QList<qsizetype> propertyChangeIndex; for (qsizetype i = 0; i < propCount; ++i) { const ASTProperty &prop = astClass.properties.at(i); - const QString propTypeName = fullyQualifiedTypeName(astClass, QStringLiteral("typename ObjectType"), typeForMode(prop, SOURCE)); - out << QString::fromLatin1(" m_properties[%1] = QtPrivate::qtro_property_index<ObjectType>(&ObjectType::%2, " - "static_cast<%3 (QObject::*)()>(nullptr),\"%2\");") - .arg(QString::number(i+1), prop.name, propTypeName) << Qt::endl; + const QString propTypeName = + fullyQualifiedName(astClass, QStringLiteral("typename ObjectType"), + typeForMode(prop, SOURCE)); + m_stream << QString::fromLatin1(" m_properties[%1] = " + "QtPrivate::qtro_property_index<ObjectType>(" + "&ObjectType::%2, static_cast<%3 (QObject::*)()>(nullptr)" + ",\"%2\");") + .arg(QString::number(i+1), prop.name, propTypeName) + << Qt::endl; if (prop.modifier == prop.ReadWrite) //Make sure we have a setter function - out << QStringLiteral(" QtPrivate::qtro_method_test<ObjectType>(&ObjectType::set%1, static_cast<void (QObject::*)(%2)>(nullptr));") - .arg(cap(prop.name), propTypeName) << Qt::endl; + m_stream << QStringLiteral(" QtPrivate::qtro_method_test<ObjectType>(" + "&ObjectType::set%1, static_cast<void (QObject::*)(%2)>" + "(nullptr));") + .arg(cap(prop.name), propTypeName) << Qt::endl; if (prop.modifier != prop.Constant) { //Make sure we have an onChange signal - out << QStringLiteral(" QtPrivate::qtro_method_test<ObjectType>(&ObjectType::%1Changed, static_cast<void (QObject::*)()>(nullptr));") - .arg(prop.name) << Qt::endl; + m_stream << QStringLiteral(" QtPrivate::qtro_method_test<ObjectType>(" + "&ObjectType::%1Changed, static_cast<void (QObject::*)()>(" + "nullptr));") + .arg(prop.name) << Qt::endl; onChangeProperties << prop; propertyChangeIndex << i + 1; //m_properties[0] is the count, so index is one higher } } const auto signalCount = astClass.signalsList.count(); const auto changedCount = onChangeProperties.size(); - out << QString::fromLatin1(" m_signals[0] = %1;").arg(signalCount+onChangeProperties.size()) << Qt::endl; + m_stream << QString::fromLatin1(" m_signals[0] = %1;") + .arg(signalCount+onChangeProperties.size()) << Qt::endl; for (qsizetype i = 0; i < changedCount; ++i) - out << QString::fromLatin1(" m_signals[%1] = QtPrivate::qtro_signal_index<ObjectType>(&ObjectType::%2Changed, " - "static_cast<void (QObject::*)(%3)>(nullptr),m_signalArgCount+%4,&m_signalArgTypes[%4]);") - .arg(QString::number(i+1), onChangeProperties.at(i).name, - fullyQualifiedTypeName(astClass, QStringLiteral("typename ObjectType"), typeForMode(onChangeProperties.at(i), SOURCE)), - QString::number(i)) << Qt::endl; - - QList<ASTFunction> signalsList = transformEnumParams(astClass, astClass.signalsList, QStringLiteral("typename ObjectType")); + m_stream + << QString::fromLatin1(" m_signals[%1] = QtPrivate::qtro_signal_index" + "<ObjectType>(&ObjectType::%2Changed, static_cast<void" + "(QObject::*)(%3)>(nullptr),m_signalArgCount+%4," + "&m_signalArgTypes[%4]);") + .arg(QString::number(i+1), onChangeProperties.at(i).name, + fullyQualifiedName(astClass, + QStringLiteral("typename ObjectType"), + typeForMode(onChangeProperties.at(i), + SOURCE)), + QString::number(i)) + << Qt::endl; + + QList<ASTFunction> signalsList = transformEnumParams(astClass, astClass.signalsList, + QStringLiteral("typename ObjectType")); for (qsizetype i = 0; i < signalCount; ++i) { const ASTFunction &sig = signalsList.at(i); - out << QString::fromLatin1(" m_signals[%1] = QtPrivate::qtro_signal_index<ObjectType>(&ObjectType::%2, " - "static_cast<void (QObject::*)(%3)>(nullptr),m_signalArgCount+%4,&m_signalArgTypes[%4]);") - .arg(QString::number(changedCount+i+1), sig.name, sig.paramsAsString(ASTFunction::Normalized), QString::number(changedCount+i)) << Qt::endl; + m_stream << QString::fromLatin1(" m_signals[%1] = QtPrivate::qtro_signal_index" + "<ObjectType>(&ObjectType::%2, static_cast<void " + "(QObject::*)(%3)>(nullptr),m_signalArgCount+%4," + "&m_signalArgTypes[%4]);") + .arg(QString::number(changedCount+i+1), sig.name, + sig.paramsAsString(ASTFunction::Normalized), + QString::number(changedCount+i)) + << Qt::endl; } const auto slotCount = astClass.slotsList.count(); QList<ASTProperty> pushProps; @@ -1053,149 +1178,208 @@ void RepCodeGenerator::generateSourceAPI(QTextStream &out, const ASTClass &astCl } const auto pushCount = pushProps.count(); const auto methodCount = slotCount + pushCount; - out << QString::fromLatin1(" m_methods[0] = %1;").arg(methodCount) << Qt::endl; + m_stream << QString::fromLatin1(" m_methods[0] = %1;").arg(methodCount) << Qt::endl; + const QString objType = QStringLiteral("typename ObjectType::"); for (qsizetype i = 0; i < pushCount; ++i) { const ASTProperty &prop = pushProps.at(i); - const QString propTypeName = fullyQualifiedTypeName(astClass, QStringLiteral("typename ObjectType"), prop.type); - out << QString::fromLatin1(" m_methods[%1] = QtPrivate::qtro_method_index<ObjectType>(&ObjectType::push%2, " - "static_cast<void (QObject::*)(%3)>(nullptr),\"push%2(%4)\",m_methodArgCount+%5,&m_methodArgTypes[%5]);") - .arg(QString::number(i+1), cap(prop.name), propTypeName, - QString(propTypeName).remove(QStringLiteral("typename ObjectType::")), // we don't want this in the string signature - QString::number(i)) << Qt::endl; + const QString propTypeName = fullyQualifiedName(astClass, + QStringLiteral("typename ObjectType"), + prop.type); + m_stream << + QString::fromLatin1(" m_methods[%1] = QtPrivate::qtro_method_index" + "<ObjectType>(&ObjectType::push%2, static_cast<void " + "(QObject::*)(%3)>(nullptr),\"push%2(%4)\"," + "m_methodArgCount+%5,&m_methodArgTypes[%5]);") + .arg(QString::number(i+1), cap(prop.name), propTypeName, + // we don't want "typename ObjectType::" in the signature + QString(propTypeName).remove(objType), + QString::number(i)) + << Qt::endl; } - QList<ASTFunction> slotsList = transformEnumParams(astClass, astClass.slotsList, QStringLiteral("typename ObjectType")); + QList<ASTFunction> slotsList = transformEnumParams(astClass, astClass.slotsList, + QStringLiteral("typename ObjectType")); for (qsizetype i = 0; i < slotCount; ++i) { const ASTFunction &slot = slotsList.at(i); const QString params = slot.paramsAsString(ASTFunction::Normalized); - out << QString::fromLatin1(" m_methods[%1] = QtPrivate::qtro_method_index<ObjectType>(&ObjectType::%2, " - "static_cast<void (QObject::*)(%3)>(nullptr),\"%2(%4)\",m_methodArgCount+%5,&m_methodArgTypes[%5]);") - .arg(QString::number(i+pushCount+1), slot.name, params, - QString(params).remove(QStringLiteral("typename ObjectType::")), // we don't want this in the string signature - QString::number(i+pushCount)) << Qt::endl; + m_stream << QString::fromLatin1(" m_methods[%1] = QtPrivate::qtro_method_index" + "<ObjectType>(&ObjectType::%2, static_cast<void " + "(QObject::*)(%3)>(nullptr),\"%2(%4)\"," + "m_methodArgCount+%5,&m_methodArgTypes[%5]);") + .arg(QString::number(i+pushCount+1), slot.name, params, + // we don't want "typename ObjectType::" in the signature + QString(params).remove(objType), + QString::number(i+pushCount)) + << Qt::endl; } for (const auto &model : astClass.modelMetadata) { const ASTProperty &property = astClass.properties.at(model.propertyIndex); - out << QString::fromLatin1(" m_models << ModelInfo({object->%1(),").arg(property.name) << Qt::endl; - out << QString::fromLatin1(" QStringLiteral(\"%1\"),").arg(property.name) << Qt::endl; + m_stream << QString::fromLatin1(" m_models << ModelInfo({object->%1(),") + .arg(property.name) << Qt::endl; + m_stream << QString::fromLatin1(" QStringLiteral(\"%1\"),") + .arg(property.name) << Qt::endl; QStringList list; if (!model.roles.isEmpty()) { for (auto role : model.roles) list << role.name; } - out << QString::fromLatin1(" QByteArrayLiteral(\"%1\")});").arg(list.join(QChar::fromLatin1('|'))) << Qt::endl; + m_stream << + QString::fromLatin1(" QByteArrayLiteral(\"%1\")});") + .arg(list.join(QChar::fromLatin1('|'))) << Qt::endl; } for (int i : astClass.subClassPropertyIndices) { const ASTProperty &child = astClass.properties.at(i); - out << QString::fromLatin1(" m_subclasses << new %2SourceAPI<%1_type_t>(object->%1(), QStringLiteral(\"%1\"));") - .arg(child.name, child.type) << Qt::endl; + m_stream << + QString::fromLatin1(" m_subclasses << new %2SourceAPI<%1_type_t>(object->%1()," + " QStringLiteral(\"%1\"));") + .arg(child.name, child.type) << Qt::endl; } - out << QStringLiteral(" }") << Qt::endl; - out << QStringLiteral("") << Qt::endl; - out << QString::fromLatin1(" QString name() const override { return m_name; }") << Qt::endl; - out << QString::fromLatin1(" QString typeName() const override { return QStringLiteral(\"%1\"); }").arg(astClass.name) << Qt::endl; - out << QStringLiteral(" int enumCount() const override { return m_enums[0]; }") << Qt::endl; - out << QStringLiteral(" int propertyCount() const override { return m_properties[0]; }") << Qt::endl; - out << QStringLiteral(" int signalCount() const override { return m_signals[0]; }") << Qt::endl; - out << QStringLiteral(" int methodCount() const override { return m_methods[0]; }") << Qt::endl; - out << QStringLiteral(" int sourceEnumIndex(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (index < 0 || index >= m_enums[0])") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" return m_enums[index+1];") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; - out << QStringLiteral(" int sourcePropertyIndex(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (index < 0 || index >= m_properties[0])") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" return m_properties[index+1];") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; - out << QStringLiteral(" int sourceSignalIndex(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (index < 0 || index >= m_signals[0])") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" return m_signals[index+1];") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; - out << QStringLiteral(" int sourceMethodIndex(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (index < 0 || index >= m_methods[0])") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" return m_methods[index+1];") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral("") << Qt::endl; + m_stream << QString::fromLatin1(" QString name() const override { return m_name; }") + << Qt::endl; + m_stream << QString::fromLatin1(" QString typeName() const override { " + "return QStringLiteral(\"%1\"); }") + .arg(astClass.name) << Qt::endl; + m_stream << QStringLiteral(" int enumCount() const override { return m_enums[0]; }") + << Qt::endl; + m_stream << + QStringLiteral(" int propertyCount() const override { return m_properties[0]; }") + << Qt::endl; + m_stream << QStringLiteral(" int signalCount() const override { return m_signals[0]; }") + << Qt::endl; + m_stream << QStringLiteral(" int methodCount() const override { return m_methods[0]; }") + << Qt::endl; + m_stream << QStringLiteral(" int sourceEnumIndex(int index) const override") << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (index < 0 || index >= m_enums[0])") << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" return m_enums[index+1];") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" int sourcePropertyIndex(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (index < 0 || index >= m_properties[0])") << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" return m_properties[index+1];") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" int sourceSignalIndex(int index) const override") << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (index < 0 || index >= m_signals[0])") << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" return m_signals[index+1];") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" int sourceMethodIndex(int index) const override") << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (index < 0 || index >= m_methods[0])") << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" return m_methods[index+1];") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; if (signalCount+changedCount > 0) { - out << QStringLiteral(" int signalParameterCount(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (index < 0 || index >= m_signals[0])") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" return m_signalArgCount[index];") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; - out << QStringLiteral(" int signalParameterType(int sigIndex, int paramIndex) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (sigIndex < 0 || sigIndex >= m_signals[0] || paramIndex < 0 || paramIndex >= m_signalArgCount[sigIndex])") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" return m_signalArgTypes[sigIndex][paramIndex];") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" int signalParameterCount(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (index < 0 || index >= m_signals[0])") << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" return m_signalArgCount[index];") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" int signalParameterType(int sigIndex, int paramIndex) " + "const override") << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (sigIndex < 0 || sigIndex >= m_signals[0] || " + "paramIndex < 0 || paramIndex >= m_signalArgCount[sigIndex])") + << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" return m_signalArgTypes[sigIndex][paramIndex];") + << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; } else { - out << QStringLiteral(" int signalParameterCount(int index) const override { Q_UNUSED(index) return -1; }") << Qt::endl; - out << QStringLiteral(" int signalParameterType(int sigIndex, int paramIndex) const override") << Qt::endl; - out << QStringLiteral(" { Q_UNUSED(sigIndex) Q_UNUSED(paramIndex) return -1; }") << Qt::endl; + m_stream << QStringLiteral(" int signalParameterCount(int index) const override " + "{ Q_UNUSED(index) return -1; }") << Qt::endl; + m_stream << QStringLiteral(" int signalParameterType(int sigIndex, int paramIndex) " + "const override") << Qt::endl; + m_stream << QStringLiteral(" { Q_UNUSED(sigIndex) Q_UNUSED(paramIndex) return -1; }") + << Qt::endl; } if (methodCount > 0) { - out << QStringLiteral(" int methodParameterCount(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (index < 0 || index >= m_methods[0])") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" return m_methodArgCount[index];") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; - out << QStringLiteral(" int methodParameterType(int methodIndex, int paramIndex) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (methodIndex < 0 || methodIndex >= m_methods[0] || paramIndex < 0 || paramIndex >= m_methodArgCount[methodIndex])") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" return m_methodArgTypes[methodIndex][paramIndex];") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" int methodParameterCount(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (index < 0 || index >= m_methods[0])") << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" return m_methodArgCount[index];") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" int methodParameterType(int methodIndex, int paramIndex) " + "const override") << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << + QStringLiteral(" if (methodIndex < 0 || methodIndex >= m_methods[0] || " + "paramIndex < 0 || paramIndex >= m_methodArgCount[methodIndex])") + << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" return m_methodArgTypes[methodIndex][paramIndex];") + << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; } else { - out << QStringLiteral(" int methodParameterCount(int index) const override { Q_UNUSED(index) return -1; }") << Qt::endl; - out << QStringLiteral(" int methodParameterType(int methodIndex, int paramIndex) const override") << Qt::endl; - out << QStringLiteral(" { Q_UNUSED(methodIndex) Q_UNUSED(paramIndex) return -1; }") << Qt::endl; + m_stream << QStringLiteral(" int methodParameterCount(int index) const override { " + "Q_UNUSED(index) return -1; }") << Qt::endl; + m_stream << QStringLiteral(" int methodParameterType(int methodIndex, int paramIndex) " + "const override") << Qt::endl; + m_stream << + QStringLiteral(" { Q_UNUSED(methodIndex) Q_UNUSED(paramIndex) return -1; }") + << Qt::endl; } //propertyIndexFromSignal method - out << QStringLiteral(" int propertyIndexFromSignal(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" int propertyIndexFromSignal(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; if (!propertyChangeIndex.isEmpty()) { - out << QStringLiteral(" switch (index) {") << Qt::endl; + m_stream << QStringLiteral(" switch (index) {") << Qt::endl; for (int i = 0; i < propertyChangeIndex.size(); ++i) - out << QString::fromLatin1(" case %1: return m_properties[%2];").arg(i).arg(propertyChangeIndex.at(i)) << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QString::fromLatin1(" case %1: return m_properties[%2];") + .arg(i).arg(propertyChangeIndex.at(i)) << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; } else - out << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; //propertyRawIndexFromSignal method - out << QStringLiteral(" int propertyRawIndexFromSignal(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" int propertyRawIndexFromSignal(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; if (!propertyChangeIndex.isEmpty()) { - out << QStringLiteral(" switch (index) {") << Qt::endl; + m_stream << QStringLiteral(" switch (index) {") << Qt::endl; for (int i = 0; i < propertyChangeIndex.size(); ++i) - out << QString::fromLatin1(" case %1: return %2;").arg(i).arg(propertyChangeIndex.at(i)-1) << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QString::fromLatin1(" case %1: return %2;").arg(i) + .arg(propertyChangeIndex.at(i)-1) << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; } else - out << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; - out << QStringLiteral(" return -1;") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; + m_stream << QStringLiteral(" return -1;") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; //signalSignature method - out << QStringLiteral(" const QByteArray signalSignature(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" const QByteArray signalSignature(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; if (signalCount+changedCount > 0) { - out << QStringLiteral(" switch (index) {") << Qt::endl; + m_stream << QStringLiteral(" switch (index) {") << Qt::endl; for (int i = 0; i < changedCount; ++i) { const ASTProperty &prop = onChangeProperties.at(i); if (isClassEnum(astClass, prop.type)) - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2Changed($1)\").replace(\"$1\", QtPrivate::qtro_enum_signature<ObjectType>(\"%3\"));") - .arg(QString::number(i), prop.name, prop.type) << Qt::endl; + m_stream << + QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2" + "Changed($1)\").replace(\"$1\", " + "QtPrivate::qtro_enum_signature<ObjectType>(\"%3\"));") + .arg(QString::number(i), prop.name, prop.type) + << Qt::endl; else - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2Changed(%3)\");") - .arg(QString::number(i), prop.name, typeForMode(prop, SOURCE)) << Qt::endl; + m_stream << + QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2" + "Changed(%3)\");") + .arg(QString::number(i), prop.name, + typeForMode(prop, SOURCE)) + << Qt::endl; } for (int i = 0; i < signalCount; ++i) { @@ -1207,41 +1391,58 @@ void RepCodeGenerator::generateSourceAPI(QTextStream &out, const ASTClass &astCl for (int j = 0; j < paramsAsList.count(); j++) { auto const p = paramsAsList.at(j); if (isClassEnum(astClass, p)) { - paramsAsString.replace(paramsAsString.indexOf(p), p.size(), QStringLiteral("$%1").arg(enumCount)); - enumString.append(QString::fromLatin1(".replace(\"$%1\", QtPrivate::qtro_enum_signature<ObjectType>(\"%2\"))").arg(enumCount++).arg(paramsAsList.at(j))); + paramsAsString.replace(paramsAsString.indexOf(p), p.size(), + QStringLiteral("$%1").arg(enumCount)); + enumString.append(QString::fromLatin1(".replace(\"$%1\", QtPrivate::" + "qtro_enum_signature<ObjectType>" + "(\"%2\"))") + .arg(enumCount++) + .arg(paramsAsList.at(j))); } } - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2(%3)\")%4;") - .arg(QString::number(i+changedCount), sig.name, paramsAsString, enumString) << Qt::endl; + m_stream << + QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2(%3)\")%4;") + .arg(QString::number(i+changedCount), sig.name, + paramsAsString, enumString) << Qt::endl; } - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; } else - out << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; - out << QStringLiteral(" return QByteArrayLiteral(\"\");") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; + m_stream << QStringLiteral(" return QByteArrayLiteral(\"\");") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; //signalParameterNames method - out << QStringLiteral(" QByteArrayList signalParameterNames(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (index < 0 || index >= m_signals[0])") << Qt::endl; - out << QStringLiteral(" return QByteArrayList();") << Qt::endl; - out << QStringLiteral(" return ObjectType::staticMetaObject.method(m_signals[index + 1]).parameterNames();") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << + QStringLiteral(" QByteArrayList signalParameterNames(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (index < 0 || index >= m_signals[0])") << Qt::endl; + m_stream << QStringLiteral(" return QByteArrayList();") << Qt::endl; + m_stream << QStringLiteral(" return ObjectType::staticMetaObject.method(m_signals[" + "index + 1]).parameterNames();") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; //methodSignature method - out << QStringLiteral(" const QByteArray methodSignature(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" const QByteArray methodSignature(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; if (methodCount > 0) { - out << QStringLiteral(" switch (index) {") << Qt::endl; + m_stream << QStringLiteral(" switch (index) {") << Qt::endl; for (int i = 0; i < pushCount; ++i) { const ASTProperty &prop = pushProps.at(i); if (isClassEnum(astClass, prop.type)) - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"push%2($1)\").replace(\"$1\", QtPrivate::qtro_enum_signature<ObjectType>(\"%3\"));") - .arg(QString::number(i), prop.name, prop.type) << Qt::endl; + m_stream << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"push" + "%2($1)\").replace(\"$1\", QtPrivate::" + "qtro_enum_signature<ObjectType>(\"%3\"));") + .arg(QString::number(i), prop.name, prop.type) + << Qt::endl; else - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"push%2(%3)\");") - .arg(QString::number(i), cap(prop.name), prop.type) << Qt::endl; + m_stream << + QString::fromLatin1(" case %1: return QByteArrayLiteral(\"push" + "%2(%3)\");") + .arg(QString::number(i), cap(prop.name), prop.type) + << Qt::endl; } for (int i = 0; i < slotCount; ++i) { @@ -1253,80 +1454,104 @@ void RepCodeGenerator::generateSourceAPI(QTextStream &out, const ASTClass &astCl for (int j = 0; j < paramsAsList.count(); j++) { auto const p = paramsAsList.at(j); if (isClassEnum(astClass, p)) { - paramsAsString.replace(paramsAsString.indexOf(p), p.size(), QStringLiteral("$%1").arg(enumCount)); - enumString.append(QString::fromLatin1(".replace(\"$%1\", QtPrivate::qtro_enum_signature<ObjectType>(\"%2\"))").arg(enumCount++).arg(paramsAsList.at(j))); + paramsAsString.replace(paramsAsString.indexOf(p), p.size(), + QStringLiteral("$%1").arg(enumCount)); + enumString.append(QString::fromLatin1(".replace(\"$%1\", QtPrivate::" + "qtro_enum_signature<ObjectType>" + "(\"%2\"))") + .arg(enumCount++) + .arg(paramsAsList.at(j))); } } - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2(%3)\")%4;") - .arg(QString::number(i+pushCount), slot.name, paramsAsString, enumString) << Qt::endl; + m_stream << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2(%3)" + "\")%4;") + .arg(QString::number(i+pushCount), slot.name, + paramsAsString, enumString) << Qt::endl; } - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; } else - out << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; - out << QStringLiteral(" return QByteArrayLiteral(\"\");") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; + m_stream << QStringLiteral(" return QByteArrayLiteral(\"\");") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; //methodType method - out << QStringLiteral(" QMetaMethod::MethodType methodType(int) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" return QMetaMethod::Slot;") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" QMetaMethod::MethodType methodType(int) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" return QMetaMethod::Slot;") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; //methodParameterNames method - out << QStringLiteral(" QByteArrayList methodParameterNames(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; - out << QStringLiteral(" if (index < 0 || index >= m_methods[0])") << Qt::endl; - out << QStringLiteral(" return QByteArrayList();") << Qt::endl; - out << QStringLiteral(" return ObjectType::staticMetaObject.method(m_methods[index + 1]).parameterNames();") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << + QStringLiteral(" QByteArrayList methodParameterNames(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" if (index < 0 || index >= m_methods[0])") << Qt::endl; + m_stream << QStringLiteral(" return QByteArrayList();") << Qt::endl; + m_stream << QStringLiteral(" return ObjectType::staticMetaObject.method(m_methods[" + "index + 1]).parameterNames();") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; //typeName method - out << QStringLiteral(" const QByteArray typeName(int index) const override") << Qt::endl; - out << QStringLiteral(" {") << Qt::endl; + m_stream << QStringLiteral(" const QByteArray typeName(int index) const override") + << Qt::endl; + m_stream << QStringLiteral(" {") << Qt::endl; if (methodCount > 0) { - out << QStringLiteral(" switch (index) {") << Qt::endl; + m_stream << QStringLiteral(" switch (index) {") << Qt::endl; for (int i = 0; i < pushCount; ++i) { - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"void\");") - .arg(QString::number(i)) << Qt::endl; + m_stream << + QString::fromLatin1(" case %1: return QByteArrayLiteral(\"void\");") + .arg(QString::number(i)) << Qt::endl; } for (int i = 0; i < slotCount; ++i) { const ASTFunction &slot = astClass.slotsList.at(i); if (isClassEnum(astClass, slot.returnType)) - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"$1\").replace(\"$1\", QtPrivate::qtro_enum_signature<ObjectType>(\"%2\"));") - .arg(QString::number(i+pushCount), slot.returnType) << Qt::endl; + m_stream << + QString::fromLatin1(" case %1: return QByteArrayLiteral(\"$1\")" + ".replace(\"$1\", QtPrivate::qtro_enum_signature" + "<ObjectType>(\"%2\"));") + .arg(QString::number(i+pushCount), slot.returnType) + << Qt::endl; else - out << QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2\");") - .arg(QString::number(i+pushCount), slot.returnType) << Qt::endl; + m_stream << + QString::fromLatin1(" case %1: return QByteArrayLiteral(\"%2\");") + .arg(QString::number(i+pushCount), slot.returnType) << Qt::endl; } - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; } else - out << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; - out << QStringLiteral(" return QByteArrayLiteral(\"\");") << Qt::endl; - out << QStringLiteral(" }") << Qt::endl; + m_stream << QStringLiteral(" Q_UNUSED(index)") << Qt::endl; + m_stream << QStringLiteral(" return QByteArrayLiteral(\"\");") << Qt::endl; + m_stream << QStringLiteral(" }") << Qt::endl; //objectSignature method - out << QStringLiteral(" QByteArray objectSignature() const override { return QByteArray{\"") + m_stream << + QStringLiteral(" QByteArray objectSignature() const override { return QByteArray{\"") << QLatin1String(classSignature(astClass)) << QStringLiteral("\"}; }") << Qt::endl; - out << QStringLiteral("") << Qt::endl; - out << QString::fromLatin1(" int m_enums[%1];").arg(totalCount + 1) << Qt::endl; - out << QString::fromLatin1(" int m_properties[%1];").arg(propCount+1) << Qt::endl; - out << QString::fromLatin1(" int m_signals[%1];").arg(signalCount+changedCount+1) << Qt::endl; - out << QString::fromLatin1(" int m_methods[%1];").arg(methodCount+1) << Qt::endl; - out << QString::fromLatin1(" const QString m_name;") << Qt::endl; + m_stream << QStringLiteral("") << Qt::endl; + m_stream << QString::fromLatin1(" int m_enums[%1];").arg(totalCount + 1) << Qt::endl; + m_stream << QString::fromLatin1(" int m_properties[%1];").arg(propCount+1) << Qt::endl; + m_stream << QString::fromLatin1(" int m_signals[%1];").arg(signalCount+changedCount+1) + << Qt::endl; + m_stream << QString::fromLatin1(" int m_methods[%1];").arg(methodCount+1) << Qt::endl; + m_stream << QString::fromLatin1(" const QString m_name;") << Qt::endl; if (signalCount+changedCount > 0) { - out << QString::fromLatin1(" int m_signalArgCount[%1];").arg(signalCount+changedCount) << Qt::endl; - out << QString::fromLatin1(" const int* m_signalArgTypes[%1];").arg(signalCount+changedCount) << Qt::endl; + m_stream << QString::fromLatin1(" int m_signalArgCount[%1];") + .arg(signalCount+changedCount) << Qt::endl; + m_stream << QString::fromLatin1(" const int* m_signalArgTypes[%1];") + .arg(signalCount+changedCount) << Qt::endl; } if (methodCount > 0) { - out << QString::fromLatin1(" int m_methodArgCount[%1];").arg(methodCount) << Qt::endl; - out << QString::fromLatin1(" const int* m_methodArgTypes[%1];").arg(methodCount) << Qt::endl; + m_stream << QString::fromLatin1(" int m_methodArgCount[%1];").arg(methodCount) + << Qt::endl; + m_stream << QString::fromLatin1(" const int* m_methodArgTypes[%1];").arg(methodCount) + << Qt::endl; } - out << QStringLiteral("};") << Qt::endl; - out << "" << Qt::endl; + m_stream << QStringLiteral("};") << Qt::endl; + m_stream << "" << Qt::endl; } QT_END_NAMESPACE |