diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-07-18 18:04:19 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:17:03 -0300 |
commit | 3e899a5e657b0f3a6bc38e788fc89c76c8962f47 (patch) | |
tree | f984416d173d7735ce05fe5a1a97d923cc7012e4 /generator | |
parent | 3016b0de40d93de2ca3eb690c1ae96529643f26a (diff) |
More convenience functions added. More refactoring. Code more readable.
ShibokenGenerator::isPointer()
ShibokenGenerator::isWrapperType()
Reviewed by Hugo Parente <hugo.lima@openbossa.org>
Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
Diffstat (limited to 'generator')
-rw-r--r-- | generator/cppgenerator.cpp | 24 | ||||
-rw-r--r-- | generator/overloaddata.cpp | 2 | ||||
-rw-r--r-- | generator/shibokengenerator.cpp | 46 | ||||
-rw-r--r-- | generator/shibokengenerator.h | 12 |
4 files changed, 59 insertions, 25 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp index 11098ed7c..5a610b352 100644 --- a/generator/cppgenerator.cpp +++ b/generator/cppgenerator.cpp @@ -187,8 +187,7 @@ void CppGenerator::writeRegisterType(QTextStream& s, const AbstractMetaClass* me QString typeName = metaClass->qualifiedCppName(); QString reducedName = reduceTypeName(metaClass); - bool isObjectType = metaClass->typeEntry()->isObject(); - if (!isObjectType) { + if (!ShibokenGenerator::isObjectType(metaClass)) { s << INDENT << "Shiboken::TypeResolver::createValueTypeResolver< ::" << typeName << " >" << "(\"" << typeName << "\");\n"; if (!reducedName.isEmpty()) s << INDENT << "Shiboken::TypeResolver::createValueTypeResolver< ::" << typeName << " >" << "(\"" << reducedName << "\");\n"; @@ -197,7 +196,7 @@ void CppGenerator::writeRegisterType(QTextStream& s, const AbstractMetaClass* me s << INDENT << "Shiboken::TypeResolver::createObjectTypeResolver< ::" << typeName << " >" << "(\"" << typeName << "*\");\n"; if (!reducedName.isEmpty()) s << INDENT << "Shiboken::TypeResolver::createObjectTypeResolver< ::" << typeName << " >" << "(\"" << reducedName << "*\");\n"; - QString functionSufix = (isObjectType ? "Object" : "Value"); + QString functionSufix = (ShibokenGenerator::isObjectType(metaClass) ? "Object" : "Value"); s << INDENT << "Shiboken::TypeResolver::create" << functionSufix; s << "TypeResolver< ::" << typeName << " >" << "(typeid(::" << typeName << ").name());\n"; if (shouldGenerateCppWrapper(metaClass)) { @@ -1188,7 +1187,7 @@ void CppGenerator::writeMinimalConstructorCallArguments(QTextStream& s, const Ab Q_ASSERT(metaType); const TypeEntry* type = metaType->typeEntry(); - if (type->isObject() || metaType->isValuePointer()) { + if (ShibokenGenerator::isPointerToWrapperType(metaType)) { s << "0"; } else if (type->isPrimitive()) { const PrimitiveTypeEntry* primitiveTypeEntry = reinterpret_cast<const PrimitiveTypeEntry*>(type); @@ -1577,11 +1576,12 @@ void CppGenerator::writeErrorSection(QTextStream& s, OverloadData& overloadData) } if (!arg->defaultValueExpression().isEmpty()) { strArg += " = "; - if ((isCString(argType) || argType->isValuePointer() || argType->typeEntry()->isObject()) - && arg->defaultValueExpression() == "0") + if ((isCString(argType) || ShibokenGenerator::isPointerToWrapperType(argType)) + && arg->defaultValueExpression() == "0") { strArg += "None"; - else + } else { strArg += arg->defaultValueExpression().replace("::", ".").replace("\"", "\\\""); + } } args << strArg; } @@ -1648,13 +1648,12 @@ void CppGenerator::writeArgumentConversion(QTextStream& s, QString typeName; QString baseTypeName = type->name(); - bool isWrappedCppClass = type->isValue() || type->isObject(); // exclude const on Objects Options flags = getConverterOptions(argType); typeName = translateTypeForWrapperMethod(argType, context, flags).trimmed(); - if (isWrappedCppClass) + if (ShibokenGenerator::isWrapperType(type)) writeInvalidCppObjectCheck(s, pyArgName, 0); // Value type that has default value. @@ -3029,10 +3028,7 @@ void CppGenerator::writeSetterFunction(QTextStream& s, const AbstractMetaField* } s << ';' << endl << endl; - - bool pythonWrapperRefCounting = metaField->type()->typeEntry()->isObject() - || metaField->type()->isValuePointer(); - if (pythonWrapperRefCounting) { + if (ShibokenGenerator::isPointerToWrapperType(metaField->type())) { s << INDENT << "Shiboken::Object::keepReference(reinterpret_cast<SbkObject*>(self), \""; s << metaField->name() << "\", value);" << endl; //s << INDENT << "Py_XDECREF(oldvalue);" << endl; @@ -3737,7 +3733,7 @@ void CppGenerator::writeInitQtMetaTypeFunctionBody(QTextStream& s, const Abstrac if (!metaClass->isNamespace() && !metaClass->isAbstract()) { // Qt metatypes are registered only on their first use, so we do this now. bool canBeValue = false; - if (!metaClass->typeEntry()->isObject()) { + if (!ShibokenGenerator::isObjectType(metaClass)) { // check if there's a empty ctor foreach (AbstractMetaFunction* func, metaClass->functions()) { if (func->isConstructor() && !func->arguments().count()) { diff --git a/generator/overloaddata.cpp b/generator/overloaddata.cpp index 9dc353cee..57084558e 100644 --- a/generator/overloaddata.cpp +++ b/generator/overloaddata.cpp @@ -338,7 +338,7 @@ void OverloadData::sortNextOverloads() } else if (checkQVariant && targetTypeEntryName != "QVariant") { if (!graph.containsEdge(qvariantIndex, targetTypeId)) // Avoid cyclic dependency. graph.addEdge(targetTypeId, qvariantIndex); - } else if (checkQString && ov->argType()->indirections() > 0 + } else if (checkQString && ShibokenGenerator::isPointer(ov->argType()) && targetTypeEntryName != "QString" && targetTypeEntryName != "QByteArray" && (!checkPyObject || targetTypeId != pyobjectIndex)) { diff --git a/generator/shibokengenerator.cpp b/generator/shibokengenerator.cpp index e3aa733cb..8c80e7c2c 100644 --- a/generator/shibokengenerator.cpp +++ b/generator/shibokengenerator.cpp @@ -486,7 +486,7 @@ QString ShibokenGenerator::cpythonWrapperCPtr(const AbstractMetaType* metaType, QString ShibokenGenerator::cpythonWrapperCPtr(const TypeEntry* type, QString argName) { - if (type->isValue() || type->isObject()) + if (ShibokenGenerator::isWrapperType(type)) return baseConversionString( "::" + type->qualifiedCppName() + '*') + "toCpp(" + argName + ')'; return QString(); } @@ -519,7 +519,7 @@ void ShibokenGenerator::writeBaseConversion(QTextStream& s, const TypeEntry* typ typeName = protectedEnumSurrogateName(metaEnum); } else { typeName = type->qualifiedCppName().trimmed(); - if (type->isObject()) + if (ShibokenGenerator::isObjectType(type)) typeName.append('*'); } s << baseConversionString(typeName); @@ -633,7 +633,7 @@ QString ShibokenGenerator::cpythonBaseName(const AbstractMetaClass* metaClass) QString ShibokenGenerator::cpythonBaseName(const TypeEntry* type) { QString baseName; - if ((type->isObject() || type->isValue() || type->isNamespace())) { // && !type->isReference()) { + if (ShibokenGenerator::isWrapperType(type) || type->isNamespace()) { // && !type->isReference()) { baseName = "Sbk_" + type->name(); } else if (type->isPrimitive()) { const PrimitiveTypeEntry* ptype = (const PrimitiveTypeEntry*) type; @@ -795,6 +795,12 @@ bool ShibokenGenerator::isPairContainer(const AbstractMetaType* type) && ((ContainerTypeEntry*)type->typeEntry())->type() == ContainerTypeEntry::PairContainer; } +bool ShibokenGenerator::isObjectType(const TypeEntry* type) +{ + if (type->isComplex()) + return ShibokenGenerator::isObjectType((const ComplexTypeEntry*)type); + return type->isObject(); +} bool ShibokenGenerator::isObjectType(const ComplexTypeEntry* type) { return type->isObject() || type->isQObject(); @@ -808,6 +814,27 @@ bool ShibokenGenerator::isObjectType(const AbstractMetaType* metaType) return metaType->isObject() || metaType->isQObject(); } +bool ShibokenGenerator::isWrapperType(const TypeEntry* type) +{ + if (type->isComplex()) + return ShibokenGenerator::isWrapperType((const ComplexTypeEntry*)type); + return type->isObject() || type->isValue(); +} +bool ShibokenGenerator::isWrapperType(const ComplexTypeEntry* type) +{ + return ShibokenGenerator::isObjectType(type) || type->isValue(); +} +bool ShibokenGenerator::isWrapperType(const AbstractMetaType* metaType) +{ + return ShibokenGenerator::isObjectType(metaType) + || metaType->typeEntry()->isValue(); +} + +bool ShibokenGenerator::isPointer(const AbstractMetaType* type) +{ + return type->indirections() > 0; +} + bool ShibokenGenerator::isPointerToWrapperType(const AbstractMetaType* type) { return ShibokenGenerator::isObjectType(type) || type->isValuePointer(); @@ -820,10 +847,9 @@ bool ShibokenGenerator::shouldDereferenceArgumentPointer(const AbstractMetaArgum bool ShibokenGenerator::shouldDereferenceAbstractMetaTypePointer(const AbstractMetaType* metaType) { - const TypeEntry* type = metaType->typeEntry(); - bool isPointer = metaType->indirections() > 0; - bool isCppClass = type->isValue() || type->isObject(); - return isCppClass && !isPointer && (metaType->isValue() || metaType->isReference()); + return ShibokenGenerator::isWrapperType(metaType) + && !ShibokenGenerator::isPointer(metaType) + && (metaType->isValue() || metaType->isReference()); } bool ShibokenGenerator::visibilityModifiedToPrivate(const AbstractMetaFunction* func) @@ -1243,8 +1269,8 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s, code.replace("%0.", QString("%1->").arg("cptr")); code.replace("%0", "cptr"); } else if (func->type()) { - QString returnValueOp = ShibokenGenerator::isObjectType(func->type()) ? "%1->" : "%1."; - if (func->type()->typeEntry()->isValue() || func->type()->typeEntry()->isObject()) + QString returnValueOp = ShibokenGenerator::isPointerToWrapperType(func->type()) ? "%1->" : "%1."; + if (ShibokenGenerator::isWrapperType(func->type())) code.replace("%0.", returnValueOp.arg(CPP_RETURN_VAR)); code.replace("%0", CPP_RETURN_VAR); } @@ -1591,7 +1617,7 @@ QString ShibokenGenerator::extendedToCppFunctionName(const TypeEntry* targetType bool ShibokenGenerator::isCopyable(const AbstractMetaClass *metaClass) { - if (metaClass->isNamespace() || metaClass->typeEntry()->isObject()) + if (metaClass->isNamespace() || ShibokenGenerator::isObjectType(metaClass)) return false; else if (metaClass->typeEntry()->copyable() == ComplexTypeEntry::Unknown) return metaClass->hasCloneOperator(); diff --git a/generator/shibokengenerator.h b/generator/shibokengenerator.h index aafc40da0..0d6713537 100644 --- a/generator/shibokengenerator.h +++ b/generator/shibokengenerator.h @@ -242,16 +242,28 @@ public: static bool isPairContainer(const AbstractMetaType* type); /// Tells if the type or class is an Object (or QObject) Type. + static bool isObjectType(const TypeEntry* type); static bool isObjectType(const ComplexTypeEntry* type); static bool isObjectType(const AbstractMetaType* metaType); static bool isObjectType(const AbstractMetaClass* metaClass); /** + * Returns true if the type passed has a Python wrapper for it. + * Although namespace has a Python wrapper, it's not considered a type. + */ + static bool isWrapperType(const TypeEntry* type); + static bool isWrapperType(const ComplexTypeEntry* type); + static bool isWrapperType(const AbstractMetaType* metaType); + + /** * Checks if the type is an Object/QObject or pointer to Value Type. * In other words, tells if the type is "T*" and T has a Python wrapper. */ static bool isPointerToWrapperType(const AbstractMetaType* type); + /// Check if type is a pointer. + static bool isPointer(const AbstractMetaType* type); + /// Checks if an argument type should be dereferenced by the Python method wrapper before calling the C++ method. static bool shouldDereferenceArgumentPointer(const AbstractMetaArgument* arg); /// Checks if a meta type should be dereferenced by the Python method wrapper passing it to C++. |