diff options
author | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-19 11:02:31 +0200 |
---|---|---|
committer | Friedemann Kleint <Friedemann.Kleint@qt.io> | 2020-05-19 16:55:29 +0200 |
commit | c761068f334185bdd95401ee0392dff0d72d764b (patch) | |
tree | 34434b3fbd56ddc35dc96c7028d39e4c1a395f53 /sources/shiboken2/generator | |
parent | 62692a6f0df7ce14e0cb8808545ab80f04d417cf (diff) |
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 <tismer@stackless.com>
Diffstat (limited to 'sources/shiboken2/generator')
-rw-r--r-- | sources/shiboken2/generator/shiboken2/cppgenerator.cpp | 19 | ||||
-rw-r--r-- | 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 <algorithm>\n#include <set>\n"; if (metaClass->generateExceptionHandling()) s << "#include <exception>\n"; + s << "#include <iterator>\n"; // For containers if (wrapperDiagnostics()) s << "#include <helper.h>\n#include <iostream>\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); |