aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2020-05-04 08:07:24 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2020-05-04 08:56:51 +0200
commit89074f1295b5fb7c0c620350e26527860ee7f1e0 (patch)
treed03ff98ba1f2cb3fbfa67043a74ce0f9af13b24c
parent538f897fb5829b670f42c8e45e291f85d969c5e8 (diff)
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 <cristian.maureira-fredes@qt.io>
-rw-r--r--sources/shiboken2/generator/shiboken2/cppgenerator.cpp53
-rw-r--r--sources/shiboken2/generator/shiboken2/cppgenerator.h5
2 files changed, 29 insertions, 29 deletions
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,