From 89074f1295b5fb7c0c620350e26527860ee7f1e0 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Mon, 4 May 2020 08:07:24 +0200 Subject: shiboken: Streamline code generated for assignment of cppSelf For common case of non-static overloads, shiboken would generate 2 lines, one assigning a nullptr and a consecutive one assigning cppSelf. Refactor CppGenerator::writeCppSelfAssigment() to no longer do this and use auto. Change-Id: If32c4a7f480d4df735877ebb25c4584d64b49bf1 Reviewed-by: Cristian Maureira-Fredes --- .../shiboken2/generator/shiboken2/cppgenerator.cpp | 53 +++++++++++----------- .../shiboken2/generator/shiboken2/cppgenerator.h | 5 +- 2 files changed, 29 insertions(+), 29 deletions(-) (limited to 'sources/shiboken2/generator') diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index aef9fc33e..724b390e0 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -2058,16 +2058,10 @@ void CppGenerator::writeArgumentsInitializer(QTextStream &s, OverloadData &overl s << Qt::endl; } -void CppGenerator::writeCppSelfAssigment(QTextStream &s, const GeneratorContext &context, - const QString &className, bool cppSelfAsReference, - bool useWrapperClass) +void CppGenerator::writeCppSelfConversion(QTextStream &s, const GeneratorContext &context, + const QString &className, bool useWrapperClass) { static const QString pythonSelfVar = QLatin1String("self"); - if (cppSelfAsReference) - s << className << " &"; - s << CPP_SELF_VAR << " = "; - if (cppSelfAsReference) - s << " *"; if (useWrapperClass) s << "static_cast<" << className << " *>("; if (!context.forSmartPointer()) @@ -2083,6 +2077,8 @@ void CppGenerator::writeCppSelfDefinition(QTextStream &s, bool hasStaticOverload, bool cppSelfAsReference) { + Q_ASSERT(!(cppSelfAsReference && hasStaticOverload)); + const AbstractMetaClass *metaClass = context.metaClass(); bool useWrapperClass = avoidProtectedHack() && metaClass->hasProtectedMembers(); QString className; @@ -2094,29 +2090,34 @@ void CppGenerator::writeCppSelfDefinition(QTextStream &s, className = context.preciseType()->cppSignature(); } - if (!cppSelfAsReference) { - s << INDENT << className << " *" << CPP_SELF_VAR << " = nullptr;\n"; - writeUnusedVariableCast(s, QLatin1String(CPP_SELF_VAR)); + writeInvalidPyObjectCheck(s, QLatin1String("self")); + + if (cppSelfAsReference) { + s << INDENT << "auto &" << CPP_SELF_VAR << " = *"; + writeCppSelfConversion(s, context, className, useWrapperClass); + s << ";\n"; + return; } - // Checks if the underlying C++ object is valid. - if (hasStaticOverload && !cppSelfAsReference) { - s << INDENT << "if (self) {\n"; - { - Indentation indent(INDENT); - writeInvalidPyObjectCheck(s, QLatin1String("self")); - s << INDENT; - writeCppSelfAssigment(s, context, className, cppSelfAsReference, useWrapperClass); - s << ";\n"; - } - s << INDENT << "}\n"; + if (!hasStaticOverload) { + s << INDENT << "auto " << CPP_SELF_VAR << " = "; + writeCppSelfConversion(s, context, className, useWrapperClass); + s << ";\n"; + writeUnusedVariableCast(s, QLatin1String(CPP_SELF_VAR)); return; } - writeInvalidPyObjectCheck(s, QLatin1String("self")); - s << INDENT; - writeCppSelfAssigment(s, context, className, cppSelfAsReference, useWrapperClass); - s << ";\n"; + s << INDENT << className << " *" << CPP_SELF_VAR << " = nullptr;\n"; + writeUnusedVariableCast(s, QLatin1String(CPP_SELF_VAR)); + + // Checks if the underlying C++ object is valid. + s << INDENT << "if (self)\n"; + { + Indentation indent(INDENT); + s << INDENT << CPP_SELF_VAR << " = "; + writeCppSelfConversion(s, context, className, useWrapperClass); + s << ";\n"; + } } void CppGenerator::writeCppSelfDefinition(QTextStream &s, diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.h b/sources/shiboken2/generator/shiboken2/cppgenerator.h index 2f0219bfa..583066c7a 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.h +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.h @@ -81,9 +81,8 @@ private: void writeMethodWrapper(QTextStream &s, const AbstractMetaFunctionList &overloads, GeneratorContext &classContext); void writeArgumentsInitializer(QTextStream &s, OverloadData &overloadData); - void writeCppSelfAssigment(QTextStream &s, const GeneratorContext &context, - const QString &className, bool cppSelfAsReference, - bool useWrapperClass); + void writeCppSelfConversion(QTextStream &s, const GeneratorContext &context, + const QString &className, bool useWrapperClass); void writeCppSelfDefinition(QTextStream &s, const AbstractMetaFunction *func, GeneratorContext &context, -- cgit v1.2.3