From 3016b0de40d93de2ca3eb690c1ae96529643f26a Mon Sep 17 00:00:00 2001 From: Marcelo Lira Date: Mon, 18 Jul 2011 16:22:21 -0300 Subject: Added convenience functions to improve code readability. Checking if a type is an Object Type is a very common task, followed by asking if a type is a pointer to a type that has a Python wrapper. These functions solve the problem: ShibokenGenerator::isObjectType(type) ShibokenGenerator::isPointerToWrapperType(type) I refactored the generator code to make use of those functions. Reviewed by Hugo Parente Reviewed by Lauro Moura Reviewed by Luciano Wolf --- generator/cppgenerator.cpp | 18 ++++++++---------- generator/shibokengenerator.cpp | 20 +++++++++++++++++++- generator/shibokengenerator.h | 13 ++++++++++++- 3 files changed, 39 insertions(+), 12 deletions(-) (limited to 'generator') diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp index 38724d369..11098ed7c 100644 --- a/generator/cppgenerator.cpp +++ b/generator/cppgenerator.cpp @@ -283,11 +283,9 @@ void CppGenerator::generateClass(QTextStream &s, const AbstractMetaClass *metaCl s << "#include " << endl; s << "#include " << endl; - if (usePySideExtensions()) { - if (metaClass->isQObject()) { - s << "#include " << endl; - s << "#include " << endl; - } + if (usePySideExtensions() && metaClass->isQObject()) { + s << "#include " << endl; + s << "#include " << endl; } // The multiple inheritance initialization function @@ -847,7 +845,7 @@ void CppGenerator::writeVirtualMethodNative(QTextStream &s, const AbstractMetaFu desiredType = '"' + func->typeReplaced(0) + '"'; } s << "(" PYTHON_RETURN_VAR ");" << endl; - if (func->type()->isQObject() || func->type()->isObject() || func->type()->isValuePointer()) + if (ShibokenGenerator::isPointerToWrapperType(func->type())) s << INDENT << "typeIsValid = typeIsValid || (" PYTHON_RETURN_VAR " == Py_None);" << endl; s << INDENT << "if (!typeIsValid) {" << endl; @@ -2706,7 +2704,7 @@ void CppGenerator::writeClassDefinition(QTextStream& s, const AbstractMetaClass* s << INDENT << "/*priv_data*/ 0" << endl; s << "};" << endl; QString suffix; - if (metaClass->typeEntry()->isObject() || metaClass->typeEntry()->isQObject()) + if (ShibokenGenerator::isObjectType(metaClass)) suffix = "*"; s << "} //extern" << endl; } @@ -3657,7 +3655,7 @@ void CppGenerator::writeClassRegister(QTextStream& s, const AbstractMetaClass* m // Set OriginalName QByteArray suffix; - if (metaClass->typeEntry()->isObject() || metaClass->typeEntry()->isQObject()) + if (ShibokenGenerator::isObjectType(metaClass)) suffix = "*"; s << INDENT << "Shiboken::ObjectType::setOriginalName(&" << pyTypeName << ", \"" << metaClass->qualifiedCppName() << suffix << "\");" << endl; @@ -4203,7 +4201,7 @@ bool CppGenerator::writeParentChildManagement(QTextStream& s, const AbstractMeta int childIndex = argIndex; if (ctorHeuristicEnabled && argIndex > 0 && numArgs) { AbstractMetaArgument* arg = func->arguments().at(argIndex-1); - if (arg->name() == "parent" && (arg->type()->isObject() || arg->type()->isQObject())) { + if (arg->name() == "parent" && ShibokenGenerator::isObjectType(arg->type())) { action = ArgumentOwner::Add; parentIndex = argIndex; childIndex = -1; @@ -4268,7 +4266,7 @@ void CppGenerator::writeReturnValueHeuristics(QTextStream& s, const AbstractMeta ArgumentOwner argOwner = getArgumentOwner(func, ArgumentOwner::ReturnIndex); if (argOwner.action == ArgumentOwner::Invalid || argOwner.index != ArgumentOwner::ThisIndex) { - if (type->isQObject() || type->isObject() || type->isValuePointer()) + if (ShibokenGenerator::isPointerToWrapperType(type)) s << INDENT << "Shiboken::Object::setParent(" << self << ", " PYTHON_RETURN_VAR ");" << endl; } } diff --git a/generator/shibokengenerator.cpp b/generator/shibokengenerator.cpp index a700b5a27..e3aa733cb 100644 --- a/generator/shibokengenerator.cpp +++ b/generator/shibokengenerator.cpp @@ -795,6 +795,24 @@ bool ShibokenGenerator::isPairContainer(const AbstractMetaType* type) && ((ContainerTypeEntry*)type->typeEntry())->type() == ContainerTypeEntry::PairContainer; } +bool ShibokenGenerator::isObjectType(const ComplexTypeEntry* type) +{ + return type->isObject() || type->isQObject(); +} +bool ShibokenGenerator::isObjectType(const AbstractMetaClass* metaClass) +{ + return ShibokenGenerator::isObjectType(metaClass->typeEntry()); +} +bool ShibokenGenerator::isObjectType(const AbstractMetaType* metaType) +{ + return metaType->isObject() || metaType->isQObject(); +} + +bool ShibokenGenerator::isPointerToWrapperType(const AbstractMetaType* type) +{ + return ShibokenGenerator::isObjectType(type) || type->isValuePointer(); +} + bool ShibokenGenerator::shouldDereferenceArgumentPointer(const AbstractMetaArgument* arg) { return shouldDereferenceAbstractMetaTypePointer(arg->type()); @@ -1225,7 +1243,7 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s, code.replace("%0.", QString("%1->").arg("cptr")); code.replace("%0", "cptr"); } else if (func->type()) { - QString returnValueOp = func->type()->isObject() || func->type()->isQObject() ? "%1->" : "%1."; + QString returnValueOp = ShibokenGenerator::isObjectType(func->type()) ? "%1->" : "%1."; if (func->type()->typeEntry()->isValue() || func->type()->typeEntry()->isObject()) code.replace("%0.", returnValueOp.arg(CPP_RETURN_VAR)); code.replace("%0", CPP_RETURN_VAR); diff --git a/generator/shibokengenerator.h b/generator/shibokengenerator.h index 526e5ac13..aafc40da0 100644 --- a/generator/shibokengenerator.h +++ b/generator/shibokengenerator.h @@ -241,6 +241,17 @@ public: static bool isCString(const AbstractMetaType* type); static bool isPairContainer(const AbstractMetaType* type); + /// Tells if the type or class is an Object (or QObject) Type. + static bool isObjectType(const ComplexTypeEntry* type); + static bool isObjectType(const AbstractMetaType* metaType); + static bool isObjectType(const AbstractMetaClass* metaClass); + + /** + * 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); + /// 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++. @@ -335,7 +346,7 @@ public: */ static Options getConverterOptions(const AbstractMetaType* metaType); - /** + /** * Helper function to find for argument default value */ static QString getDefaultValue(const AbstractMetaFunction* func, const AbstractMetaArgument* arg); -- cgit v1.2.3