From f02e7bd726a5560542e7c078b925f289a9d6a75d Mon Sep 17 00:00:00 2001 From: Marcelo Lira Date: Mon, 8 Aug 2011 19:15:54 -0300 Subject: Method buildAbstractMetaTypeFromString now uses a cache for the types it builds. --- generator/cppgenerator.cpp | 31 +++++++------------------------ generator/cppgenerator.h | 2 +- generator/shibokengenerator.cpp | 40 ++++++++++++++++++++++------------------ generator/shibokengenerator.h | 10 ++++++---- 4 files changed, 36 insertions(+), 47 deletions(-) diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp index 4446ff2a7..f0c19e3ff 100644 --- a/generator/cppgenerator.cpp +++ b/generator/cppgenerator.cpp @@ -1471,15 +1471,12 @@ void CppGenerator::writeInvalidPyObjectCheck(QTextStream& s, const QString& pyOb void CppGenerator::writeTypeCheck(QTextStream& s, const AbstractMetaType* argType, QString argumentName, bool isNumber, QString customType, bool rejectNull) { - AbstractMetaType* metaType; - std::auto_ptr metaType_autoptr; QString customCheck; if (!customType.isEmpty()) { + AbstractMetaType* metaType; customCheck = guessCPythonCheckFunction(customType, &metaType); - if (metaType) { - metaType_autoptr = std::auto_ptr(metaType); + if (metaType) argType = metaType; - } } QString typeCheck; @@ -1532,10 +1529,8 @@ void CppGenerator::writeArgumentConversion(QTextStream& s, writePythonToCppTypeConversion(s, argType, pyArgName, argName, context, defaultValue); } -const AbstractMetaType* CppGenerator::getArgumentType(const AbstractMetaFunction* func, int argPos, bool* newType) +const AbstractMetaType* CppGenerator::getArgumentType(const AbstractMetaFunction* func, int argPos) { - *newType = false; - if (argPos < 0 || argPos > func->arguments().size()) { ReportHandler::warning(QString("Argument index for function '%1' out of range.").arg(func->signature())); return 0; @@ -1543,12 +1538,10 @@ const AbstractMetaType* CppGenerator::getArgumentType(const AbstractMetaFunction const AbstractMetaType* argType = 0; QString typeReplaced = func->typeReplaced(argPos); - if (typeReplaced.isEmpty()) { + if (typeReplaced.isEmpty()) argType = (argPos == 0) ? func->type() : func->arguments().at(argPos-1)->type(); - } else { + else argType = buildAbstractMetaTypeFromString(typeReplaced); - *newType = (bool)argType; - } if (!argType && !m_knownPythonTypes.contains(typeReplaced)) { ReportHandler::warning(QString("Unknown type '%1' used as argument type replacement "\ "in function '%2', the generated code may be broken.") @@ -1862,16 +1855,11 @@ void CppGenerator::writeSingleFunctionCall(QTextStream& s, const OverloadData& o if (!func->conversionRule(TypeSystem::NativeCode, argIdx + 1).isEmpty()) continue; - bool newType; - const AbstractMetaType* argType = getArgumentType(func, argIdx + 1, &newType); + const AbstractMetaType* argType = getArgumentType(func, argIdx + 1); if (!argType) continue; - std::auto_ptr argType_autoptr; - if (newType) - argType_autoptr = std::auto_ptr(argType); - int argPos = argIdx - removedArgs; QString argName = QString(CPP_ARG"%1").arg(argPos); QString pyArgName = usePyArgs ? QString(PYTHON_ARGS "[%1]").arg(argPos) : PYTHON_ARG; @@ -2996,16 +2984,11 @@ void CppGenerator::writeRichCompareFunction(QTextStream& s, const AbstractMetaCl if (func->isStatic()) continue; - bool newType; - const AbstractMetaType* argType = getArgumentType(func, 1, &newType); + const AbstractMetaType* argType = getArgumentType(func, 1); if (!argType) continue; - std::auto_ptr argType_autoptr; - if (newType) - argType_autoptr = std::auto_ptr(argType); - bool numberType = alternativeNumericTypes == 1 || ShibokenGenerator::isPyInt(argType); if (!first) { diff --git a/generator/cppgenerator.h b/generator/cppgenerator.h index f0548931e..b6ce72ce7 100644 --- a/generator/cppgenerator.h +++ b/generator/cppgenerator.h @@ -100,7 +100,7 @@ private: * \param newType It is set to true if the type returned is a new object that must be deallocated. * \return The type of the argument indicated by \p argPos. */ - const AbstractMetaType* getArgumentType(const AbstractMetaFunction* func, int argPos, bool* newType); + const AbstractMetaType* getArgumentType(const AbstractMetaFunction* func, int argPos); void writePythonToCppTypeConversion(QTextStream& s, const AbstractMetaType* type, diff --git a/generator/shibokengenerator.cpp b/generator/shibokengenerator.cpp index 9ec21a42f..e723b6f5e 100644 --- a/generator/shibokengenerator.cpp +++ b/generator/shibokengenerator.cpp @@ -75,6 +75,14 @@ ShibokenGenerator::ShibokenGenerator() : Generator() if (m_knownPythonTypes.isEmpty()) ShibokenGenerator::initKnownPythonTypes(); + + m_metaTypeFromStringCache = AbstractMetaTypeCache(); +} + +ShibokenGenerator::~ShibokenGenerator() +{ + foreach (AbstractMetaType* type, m_metaTypeFromStringCache.values()) + delete type; } void ShibokenGenerator::clearTpFuncs() @@ -884,15 +892,12 @@ bool ShibokenGenerator::visibilityModifiedToPrivate(const AbstractMetaFunction* QString ShibokenGenerator::cpythonCheckFunction(const AbstractMetaType* metaType, bool genericNumberType) { - AbstractMetaType* type; - std::auto_ptr type_autoptr; QString customCheck; if (metaType->typeEntry()->isCustom()) { + AbstractMetaType* type; customCheck = guessCPythonCheckFunction(metaType->typeEntry()->name(), &type); - if (type) { - type_autoptr = std::auto_ptr(type); + if (type) metaType = type; - } if (!customCheck.isEmpty()) return customCheck; } @@ -912,15 +917,12 @@ QString ShibokenGenerator::cpythonCheckFunction(const AbstractMetaType* metaType QString ShibokenGenerator::cpythonCheckFunction(const TypeEntry* type, bool genericNumberType) { - AbstractMetaType* metaType; - std::auto_ptr metaType_autoptr; QString customCheck; if (type->isCustom()) { + AbstractMetaType* metaType; customCheck = guessCPythonCheckFunction(type->name(), &metaType); - if (metaType) { - metaType_autoptr = std::auto_ptr(metaType); + if (metaType) return cpythonCheckFunction(metaType, genericNumberType); - } return customCheck; } @@ -957,7 +959,6 @@ QString ShibokenGenerator::guessCPythonIsConvertible(const QString& type) return "PyType_Check"; AbstractMetaType* metaType = buildAbstractMetaTypeFromString(type); - std::auto_ptr metaType_autoptr(metaType); if (metaType && !metaType->typeEntry()->isCustom()) return cpythonIsConvertibleFunction(metaType); @@ -982,15 +983,12 @@ QString ShibokenGenerator::cpythonIsConvertibleFunction(const TypeEntry* type, b QString ShibokenGenerator::cpythonIsConvertibleFunction(const AbstractMetaType* metaType, bool genericNumberType) { - AbstractMetaType* type; - std::auto_ptr type_autoptr; QString customCheck; if (metaType->typeEntry()->isCustom()) { + AbstractMetaType* type; customCheck = guessCPythonCheckFunction(metaType->typeEntry()->name(), &type); - if (type) { - type_autoptr = std::auto_ptr(type); + if (type) metaType = type; - } if (!customCheck.isEmpty()) return customCheck; } @@ -1712,9 +1710,14 @@ bool ShibokenGenerator::isCopyable(const AbstractMetaClass *metaClass) return false; } -AbstractMetaType* ShibokenGenerator::buildAbstractMetaTypeFromString(QString typeString) +AbstractMetaType* ShibokenGenerator::buildAbstractMetaTypeFromString(QString typeSignature) { - typeString = typeString.trimmed(); + typeSignature = typeSignature.trimmed(); + + if (m_metaTypeFromStringCache.contains(typeSignature)) + return m_metaTypeFromStringCache.value(typeSignature); + + QString typeString = typeSignature; bool isConst = typeString.startsWith("const "); if (isConst) typeString.remove(0, sizeof("const ") / sizeof(char) - 1); @@ -1740,6 +1743,7 @@ AbstractMetaType* ShibokenGenerator::buildAbstractMetaTypeFromString(QString typ metaType->setReference(isReference); metaType->setConstant(isConst); metaType->decideUsagePattern(); + m_metaTypeFromStringCache.insert(typeSignature, metaType); } return metaType; } diff --git a/generator/shibokengenerator.h b/generator/shibokengenerator.h index e103b3f42..e920f7f9f 100644 --- a/generator/shibokengenerator.h +++ b/generator/shibokengenerator.h @@ -54,6 +54,7 @@ class ShibokenGenerator : public Generator { public: ShibokenGenerator(); + virtual ~ShibokenGenerator(); QString translateTypeForWrapperMethod(const AbstractMetaType* cType, const AbstractMetaClass* context, Options opt = NoOption) const; @@ -386,10 +387,10 @@ public: /** * Builds an AbstractMetaType object from a QString. * Returns NULL if no type could be built from the string. - * \param typeString The string describing the type to be built. + * \param typeSignature The string describing the type to be built. * \return A new AbstractMetaType object that must be deleted by the caller, or a NULL pointer in case of failure. */ - AbstractMetaType* buildAbstractMetaTypeFromString(QString typeString); + AbstractMetaType* buildAbstractMetaTypeFromString(QString typeSignature); /** * Helper function to return the flags to be used by a meta type when @@ -445,8 +446,9 @@ private: bool m_verboseErrorMessagesDisabled; bool m_useIsNullAsNbNonZero; bool m_avoidProtectedHack; -}; + typedef QHash AbstractMetaTypeCache; + AbstractMetaTypeCache m_metaTypeFromStringCache; +}; #endif // SHIBOKENGENERATOR_H - -- cgit v1.2.3