aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-08-08 19:15:54 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:17:09 -0300
commitf02e7bd726a5560542e7c078b925f289a9d6a75d (patch)
tree4a9ab69f582474090658aa2a0f95dead1f7bb8b3
parent05e7ecede5304ee56a805c7b5a10d3df11cf8952 (diff)
Method buildAbstractMetaTypeFromString now uses a cache for the types it builds.
-rw-r--r--generator/cppgenerator.cpp31
-rw-r--r--generator/cppgenerator.h2
-rw-r--r--generator/shibokengenerator.cpp40
-rw-r--r--generator/shibokengenerator.h10
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<AbstractMetaType> metaType_autoptr;
QString customCheck;
if (!customType.isEmpty()) {
+ AbstractMetaType* metaType;
customCheck = guessCPythonCheckFunction(customType, &metaType);
- if (metaType) {
- metaType_autoptr = std::auto_ptr<AbstractMetaType>(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<const AbstractMetaType> argType_autoptr;
- if (newType)
- argType_autoptr = std::auto_ptr<const AbstractMetaType>(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<const AbstractMetaType> argType_autoptr;
- if (newType)
- argType_autoptr = std::auto_ptr<const AbstractMetaType>(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<AbstractMetaType> type_autoptr;
QString customCheck;
if (metaType->typeEntry()->isCustom()) {
+ AbstractMetaType* type;
customCheck = guessCPythonCheckFunction(metaType->typeEntry()->name(), &type);
- if (type) {
- type_autoptr = std::auto_ptr<AbstractMetaType>(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<AbstractMetaType> metaType_autoptr;
QString customCheck;
if (type->isCustom()) {
+ AbstractMetaType* metaType;
customCheck = guessCPythonCheckFunction(type->name(), &metaType);
- if (metaType) {
- metaType_autoptr = std::auto_ptr<AbstractMetaType>(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<const AbstractMetaType> 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<AbstractMetaType> type_autoptr;
QString customCheck;
if (metaType->typeEntry()->isCustom()) {
+ AbstractMetaType* type;
customCheck = guessCPythonCheckFunction(metaType->typeEntry()->name(), &type);
- if (type) {
- type_autoptr = std::auto_ptr<AbstractMetaType>(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<QString, AbstractMetaType*> AbstractMetaTypeCache;
+ AbstractMetaTypeCache m_metaTypeFromStringCache;
+};
#endif // SHIBOKENGENERATOR_H
-