aboutsummaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-07-28 04:31:35 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:17:05 -0300
commit454efd2e01c06832bedc89a93e139dd6f011a296 (patch)
tree6d1f63d70a979510e4608e8feae731421b7ae03b /generator
parent767e21c15eb9a400fb43eda35fa3aea01330e8b6 (diff)
All Python to C++ conversions were moved to CppGenerator::writePythonToCppTypeConversion().
Diffstat (limited to 'generator')
-rw-r--r--generator/cppgenerator.cpp90
-rw-r--r--generator/cppgenerator.h7
2 files changed, 55 insertions, 42 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp
index 20d99f257..663f0f674 100644
--- a/generator/cppgenerator.cpp
+++ b/generator/cppgenerator.cpp
@@ -862,24 +862,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, const AbstractMetaFu
CodeSnipList convRule = getReturnConversionRule(TypeSystem::NativeCode, func, "", CPP_RETURN_VAR);
writeCodeSnips(s, convRule, CodeSnip::Any, TypeSystem::NativeCode, func);
} else if (!injectedCodeHasReturnValueAttribution(func, TypeSystem::NativeCode)) {
- QString conversion;
- QTextStream c(&conversion);
- writeToCppConversion(c, func->type(), func->implementingClass(), PYTHON_RETURN_VAR);
- QString typeName;
- if (avoidProtectedHack() && func->type()->isEnum()) {
- const AbstractMetaEnum* metaEnum = findAbstractMetaEnum(func->type());
- bool isProtectedEnum = metaEnum && metaEnum->isProtected();
- if (isProtectedEnum) {
- if (metaEnum->enclosingClass())
- typeName = QString("%1::").arg(metaEnum->enclosingClass()->qualifiedCppName());
- typeName += metaEnum->name();
- conversion.prepend(typeName+'(');
- conversion.append(')');
- }
- }
- if (typeName.isEmpty())
- typeName = translateTypeForWrapperMethod(func->type(), func->implementingClass());
- s << INDENT << typeName << " " CPP_RETURN_VAR " = " << conversion << ';' << endl;
+ writePythonToCppTypeConversion(s, func->type(), PYTHON_RETURN_VAR, CPP_RETURN_VAR, func->implementingClass());
}
}
}
@@ -915,8 +898,22 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, const AbstractMetaFu
writeCodeSnips(s, snips, CodeSnip::End, TypeSystem::NativeCode, func, lastArg);
}
- if (type)
- s << INDENT << "return " CPP_RETURN_VAR ";" << endl;
+ if (type) {
+ s << INDENT << "return ";
+ // TODO-IMPROVEMENTS: try to put this on writePythonToCppTypeConversion.
+ if (avoidProtectedHack() && type->isEnum()) {
+ const AbstractMetaEnum* metaEnum = findAbstractMetaEnum(type);
+ bool isProtectedEnum = metaEnum && metaEnum->isProtected();
+ if (isProtectedEnum) {
+ QString typeCast = "(";
+ if (metaEnum->enclosingClass())
+ typeCast += QString("::%1").arg(metaEnum->enclosingClass()->qualifiedCppName());
+ typeCast += QString("::%1)").arg(metaEnum->name());
+ s << typeCast;
+ }
+ }
+ s << CPP_RETURN_VAR ";" << endl;
+ }
s << '}' << endl << endl;
}
@@ -1571,37 +1568,46 @@ void CppGenerator::writeArgumentConversion(QTextStream& s,
const AbstractMetaClass* context,
const QString& defaultValue)
{
- const TypeEntry* type = argType->typeEntry();
-
- if (type->isCustom() || type->isVarargs())
+ if (argType->typeEntry()->isCustom() || argType->typeEntry()->isVarargs())
return;
+ if (isWrapperType(argType))
+ writeInvalidPyObjectCheck(s, pyArgName);
+ writePythonToCppTypeConversion(s, argType, pyArgName, argName, context, defaultValue);
+}
- QString typeName;
- QString baseTypeName = type->name();
+void CppGenerator::writePythonToCppTypeConversion(QTextStream& s,
+ const AbstractMetaType* type,
+ const QString& pyIn,
+ const QString& cppOut,
+ const AbstractMetaClass* context,
+ const QString& defaultValue)
+{
+ if (type->typeEntry()->isCustom() || type->typeEntry()->isVarargs())
+ return;
- // exclude const on Objects
- Options flags = getConverterOptions(argType);
- typeName = translateTypeForWrapperMethod(argType, context, flags).trimmed();
+ QString conversion;
+ QTextStream c(&conversion);
+ writeToCppConversion(c, type, context, pyIn);
- if (isWrapperType(type))
- writeInvalidPyObjectCheck(s, pyArgName);
+ QString typeName;
+ QString cppOutAux = QString("%1_tmp").arg(cppOut);
// Value type that has default value.
- if (argType->isValue() && !defaultValue.isEmpty())
- s << INDENT << baseTypeName << ' ' << argName << "_tmp = " << defaultValue << ';' << endl;
+ if (type->isValue() && !defaultValue.isEmpty())
+ s << INDENT << type->typeEntry()->name() << ' ' << cppOutAux << " = " << defaultValue << ';' << endl;
+
+ if (typeName.isEmpty()) {
+ // exclude const on Objects
+ Options flags = getConverterOptions(type);
+ typeName = translateTypeForWrapperMethod(type, context, flags).trimmed();
+ }
- s << INDENT << typeName << ' ' << argName << " = ";
- if (!defaultValue.isEmpty())
- s << pyArgName << " ? ";
- s << "Shiboken::Converter< " << typeName << " >::toCpp(" << pyArgName << ')';
if (!defaultValue.isEmpty()) {
- s << " : ";
- if (argType->isValue())
- s << argName << "_tmp";
- else
- s << defaultValue;
+ conversion.prepend(QString("%1 ? ").arg(pyIn));
+ conversion.append(QString(" : %1").arg(type->isValue() ? cppOutAux : defaultValue));
}
- s << ';' << endl;
+
+ s << INDENT << typeName << " " << cppOut << " = " << conversion << ';' << endl;
}
void CppGenerator::writeNoneReturn(QTextStream& s, const AbstractMetaFunction* func, bool thereIsReturnValue)
diff --git a/generator/cppgenerator.h b/generator/cppgenerator.h
index 700ea4ff3..dd02cb6ad 100644
--- a/generator/cppgenerator.h
+++ b/generator/cppgenerator.h
@@ -94,6 +94,13 @@ private:
writeArgumentConversion(s, arg->type(), argName, pyArgName, context, defaultValue);
}
+ void writePythonToCppTypeConversion(QTextStream& s,
+ const AbstractMetaType* type,
+ const QString& pyIn,
+ const QString& cppOut,
+ const AbstractMetaClass* context = 0,
+ const QString& defaultValue = QString());
+
/**
* Set the Python method wrapper return value variable to Py_None if
* there are return types different from void in any of the other overloads