From c761068f334185bdd95401ee0392dff0d72d764b Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 19 May 2020 11:02:31 +0200 Subject: shiboken: Refactor generation of sequence access methods Rename writeStdListWrapperMethods() to writeDefaultSequenceMethods() since that is is more close to its purpose. In the function, get the base container type. Use std::advance instead of a loop to position the iterator. This is specialized for random access iterators to perform an addition and thus more efficient. Use const_iterator in __getitem__ to prevent Qt containers from detaching. Task-number: PYSIDE-904 Change-Id: I4735f39193c4f4efa856440ecddbc48b3a5071ae Reviewed-by: Christian Tismer --- .../shiboken2/generator/shiboken2/cppgenerator.cpp | 19 ++++++++++++------- sources/shiboken2/generator/shiboken2/cppgenerator.h | 2 +- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp index cf9d4508f..52892663e 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.cpp +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.cpp @@ -328,6 +328,7 @@ void CppGenerator::generateClass(QTextStream &s, const GeneratorContext &classCo s << "#include \n#include \n"; if (metaClass->generateExceptionHandling()) s << "#include \n"; + s << "#include \n"; // For containers if (wrapperDiagnostics()) s << "#include \n#include \n"; @@ -4134,7 +4135,7 @@ void CppGenerator::writeSequenceMethods(QTextStream &s, } if (!injectedCode) - writeStdListWrapperMethods(s, context); + writeDefaultSequenceMethods(s, context); } void CppGenerator::writeTypeAsSequenceDefinition(QTextStream &s, const AbstractMetaClass *metaClass) @@ -6062,7 +6063,7 @@ void CppGenerator::writeHashFunction(QTextStream &s, const GeneratorContext &con s<< "}\n\n"; } -void CppGenerator::writeStdListWrapperMethods(QTextStream &s, const GeneratorContext &context) +void CppGenerator::writeDefaultSequenceMethods(QTextStream &s, const GeneratorContext &context) { const AbstractMetaClass *metaClass = context.metaClass(); ErrorCode errorCode(0); @@ -6080,8 +6081,10 @@ void CppGenerator::writeStdListWrapperMethods(QTextStream &s, const GeneratorCon writeCppSelfDefinition(s, context); writeIndexError(s, QLatin1String("index out of bounds")); - s << INDENT << metaClass->qualifiedCppName() << "::iterator _item = " << CPP_SELF_VAR << "->begin();\n"; - s << INDENT << "for (Py_ssize_t pos = 0; pos < _i; pos++) _item++;\n"; + QString value; + s << INDENT << metaClass->qualifiedCppName() << "::const_iterator _item = " + << CPP_SELF_VAR << "->begin();\n" + << INDENT << "std::advance(_item, _i);\n"; const AbstractMetaTypeList instantiations = metaClass->templateBaseClassInstantiations(); if (instantiations.isEmpty()) { @@ -6115,9 +6118,11 @@ void CppGenerator::writeStdListWrapperMethods(QTextStream &s, const GeneratorCon s << INDENT << "}\n"; writeArgumentConversion(s, itemType, QLatin1String("cppValue"), QLatin1String("pyArg"), metaClass); - s << INDENT << metaClass->qualifiedCppName() << "::iterator _item = " << CPP_SELF_VAR << "->begin();\n"; - s << INDENT << "for (Py_ssize_t pos = 0; pos < _i; pos++) _item++;\n"; - s << INDENT << "*_item = cppValue;\n"; + s << INDENT << metaClass->qualifiedCppName() << "::iterator _item = " + << CPP_SELF_VAR << "->begin();\n" + << INDENT << "std::advance(_item, _i);\n" + << INDENT << "*_item = cppValue;\n"; + s << INDENT << "return {};\n"; s << "}\n"; } diff --git a/sources/shiboken2/generator/shiboken2/cppgenerator.h b/sources/shiboken2/generator/shiboken2/cppgenerator.h index 5fe67be21..7308f7d18 100644 --- a/sources/shiboken2/generator/shiboken2/cppgenerator.h +++ b/sources/shiboken2/generator/shiboken2/cppgenerator.h @@ -344,7 +344,7 @@ private: void writeHashFunction(QTextStream &s, const GeneratorContext &context); /// Write default implementations for sequence protocol - void writeStdListWrapperMethods(QTextStream &s, const GeneratorContext &context); + void writeDefaultSequenceMethods(QTextStream &s, const GeneratorContext &context); /// Helper function for writeStdListWrapperMethods. void writeIndexError(QTextStream &s, const QString &errorMsg); -- cgit v1.2.3