aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2019-01-04 11:12:29 +0100
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2019-01-07 08:16:05 +0000
commit4786ceb39b513b34cfe83874ae590866d19be2db (patch)
tree7acb2cf4718c15b0d0f02b187ee8535c4c4ba2ab /sources/shiboken2
parent60ce66780a82af1314fae9d82ace519815e7a3a1 (diff)
shiboken: Refactor code generating the assignment of cppSelf
The code was needlessly convoluted and generated ugly C-style casts for wrapper classes. Factor out a function to write the assignment. Change-Id: I2bc4b258f254d36a1a22f68336c285e64df0aca1 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Diffstat (limited to 'sources/shiboken2')
-rw-r--r--sources/shiboken2/generator/shiboken2/cppgenerator.cpp49
-rw-r--r--sources/shiboken2/generator/shiboken2/cppgenerator.h3
2 files changed, 31 insertions, 21 deletions
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
index 76f902c07..99bfae9f0 100644
--- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
+++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp
@@ -1987,6 +1987,26 @@ void CppGenerator::writeArgumentsInitializer(QTextStream& s, OverloadData& overl
s << endl;
}
+void CppGenerator::writeCppSelfAssigment(QTextStream &s, const GeneratorContext &context,
+ const QString &className, bool cppSelfAsReference,
+ 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())
+ s << cpythonWrapperCPtr(context.metaClass(), pythonSelfVar);
+ else
+ s << cpythonWrapperCPtr(context.preciseType(), pythonSelfVar);
+ if (useWrapperClass)
+ s << ')';
+}
+
void CppGenerator::writeCppSelfDefinition(QTextStream &s,
GeneratorContext &context,
bool hasStaticOverload,
@@ -2003,26 +2023,9 @@ void CppGenerator::writeCppSelfDefinition(QTextStream &s,
className = context.preciseType()->cppSignature();
}
- QString cppSelfAttribution;
- QString pythonSelfVar = QLatin1String("self");
- QString cpythonWrapperCPtrResult;
- if (!context.forSmartPointer())
- cpythonWrapperCPtrResult = cpythonWrapperCPtr(metaClass, pythonSelfVar);
- else
- cpythonWrapperCPtrResult = cpythonWrapperCPtr(context.preciseType(), pythonSelfVar);
-
- if (cppSelfAsReference) {
- QString cast = useWrapperClass ? QString::fromLatin1("(%1*)").arg(className) : QString();
- cppSelfAttribution = QString::fromLatin1("%1& %2 = *(%3%4)")
- .arg(className, QLatin1String(CPP_SELF_VAR), cast,
- cpythonWrapperCPtrResult);
- } else {
- s << INDENT << className << "* " << CPP_SELF_VAR << " = 0;" << endl;
+ if (!cppSelfAsReference) {
+ s << INDENT << className << "* " << CPP_SELF_VAR << " = nullptr;" << endl;
writeUnusedVariableCast(s, QLatin1String(CPP_SELF_VAR));
- cppSelfAttribution = QString::fromLatin1("%1 = %2%3")
- .arg(QLatin1String(CPP_SELF_VAR),
- (useWrapperClass ? QString::fromLatin1("(%1*)").arg(className) : QString()),
- cpythonWrapperCPtrResult);
}
// Checks if the underlying C++ object is valid.
@@ -2031,14 +2034,18 @@ void CppGenerator::writeCppSelfDefinition(QTextStream &s,
{
Indentation indent(INDENT);
writeInvalidPyObjectCheck(s, QLatin1String("self"));
- s << INDENT << cppSelfAttribution << ';' << endl;
+ s << INDENT;
+ writeCppSelfAssigment(s, context, className, cppSelfAsReference, useWrapperClass);
+ s << ';' << endl;
}
s << INDENT << '}' << endl;
return;
}
writeInvalidPyObjectCheck(s, QLatin1String("self"));
- s << INDENT << cppSelfAttribution << ';' << endl;
+ s << INDENT;
+ writeCppSelfAssigment(s, context, className, cppSelfAsReference, useWrapperClass);
+ s << ';' << endl;
}
void CppGenerator::writeCppSelfDefinition(QTextStream &s,
diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.h b/sources/shiboken2/generator/shiboken2/cppgenerator.h
index 55a1c265d..3035fad34 100644
--- a/sources/shiboken2/generator/shiboken2/cppgenerator.h
+++ b/sources/shiboken2/generator/shiboken2/cppgenerator.h
@@ -73,6 +73,9 @@ 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 writeCppSelfDefinition(QTextStream &s,
const AbstractMetaFunction *func,
GeneratorContext &context,