diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2011-08-05 13:55:56 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:17:08 -0300 |
commit | 50aef1dd4836b5019c60d31c54501ebcd37f72cd (patch) | |
tree | 3e9f41435c3ee0da5fdb5ee32b5b9900ad5a5259 /generator | |
parent | 08f29f0d8f456eb1f994b05c21fd04468c95329c (diff) |
Improved guessCPythonCheckFunction method to produce an AbstractMetaType for known types.
This is in opposition of simply returning a string with a custom type
check. The details are in the docstring in ShibokenGenerator header.
Also added a new modification test and refactored here and there in the
sample binding type system.
Diffstat (limited to 'generator')
-rw-r--r-- | generator/cppgenerator.cpp | 19 | ||||
-rw-r--r-- | generator/shibokengenerator.cpp | 51 | ||||
-rw-r--r-- | generator/shibokengenerator.h | 12 |
3 files changed, 65 insertions, 17 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp index 9e8007f6d..687a24499 100644 --- a/generator/cppgenerator.cpp +++ b/generator/cppgenerator.cpp @@ -1471,12 +1471,21 @@ void CppGenerator::writeInvalidPyObjectCheck(QTextStream& s, const QString& pyOb void CppGenerator::writeTypeCheck(QTextStream& s, const AbstractMetaType* argType, QString argumentName, bool isNumber, QString customType) { - if (!customType.isEmpty()) - s << guessCPythonCheckFunction(customType); - else if (argType->isEnum()) - s << cpythonIsConvertibleFunction(argType, false); + AbstractMetaType* metaType; + std::auto_ptr<AbstractMetaType> metaType_autoptr; + QString customCheck; + if (!customType.isEmpty()) { + customCheck = guessCPythonCheckFunction(customType, &metaType); + if (metaType) { + metaType_autoptr = std::auto_ptr<AbstractMetaType>(metaType); + argType = metaType; + } + } + + if (customCheck.isEmpty()) + s << cpythonIsConvertibleFunction(argType, argType->isEnum() ? false : isNumber); else - s << cpythonIsConvertibleFunction(argType, isNumber); + s << customCheck; s << '(' << argumentName << ')'; } diff --git a/generator/shibokengenerator.cpp b/generator/shibokengenerator.cpp index 3e5b8eabb..17cd31a91 100644 --- a/generator/shibokengenerator.cpp +++ b/generator/shibokengenerator.cpp @@ -860,8 +860,18 @@ bool ShibokenGenerator::visibilityModifiedToPrivate(const AbstractMetaFunction* QString ShibokenGenerator::cpythonCheckFunction(const AbstractMetaType* metaType, bool genericNumberType) { - if (metaType->typeEntry()->isCustom()) - return guessCPythonCheckFunction(metaType->typeEntry()->name()); + AbstractMetaType* type; + std::auto_ptr<AbstractMetaType> type_autoptr; + QString customCheck; + if (metaType->typeEntry()->isCustom()) { + customCheck = guessCPythonCheckFunction(metaType->typeEntry()->name(), &type); + if (type) { + type_autoptr = std::auto_ptr<AbstractMetaType>(type); + metaType = type; + } + if (!customCheck.isEmpty()) + return customCheck; + } QString baseName = cpythonBaseName(metaType); if (isNumber(baseName)) @@ -878,8 +888,17 @@ QString ShibokenGenerator::cpythonCheckFunction(const AbstractMetaType* metaType QString ShibokenGenerator::cpythonCheckFunction(const TypeEntry* type, bool genericNumberType) { - if (type->isCustom()) - return guessCPythonCheckFunction(type->name()); + AbstractMetaType* metaType; + std::auto_ptr<AbstractMetaType> metaType_autoptr; + QString customCheck; + if (type->isCustom()) { + customCheck = guessCPythonCheckFunction(type->name(), &metaType); + if (metaType) { + metaType_autoptr = std::auto_ptr<AbstractMetaType>(metaType); + return cpythonCheckFunction(metaType, genericNumberType); + } + return customCheck; + } QString baseName = cpythonBaseName(type); if (isNumber(baseName)) @@ -892,18 +911,18 @@ QString ShibokenGenerator::cpythonCheckFunction(const TypeEntry* type, bool gene return QString("%1checkType").arg(baseName); } -QString ShibokenGenerator::guessCPythonCheckFunction(const QString& type) +QString ShibokenGenerator::guessCPythonCheckFunction(const QString& type, AbstractMetaType** metaType) { + *metaType = 0; if (type == "PyTypeObject") return "PyType_Check"; if (type == "PyBuffer") return "Shiboken::Buffer::checkType"; - AbstractMetaType* metaType = buildAbstractMetaTypeFromString(type); - std::auto_ptr<const AbstractMetaType> metaType_autoptr(metaType); - if (metaType && !metaType->typeEntry()->isCustom()) - return cpythonCheckFunction(metaType); + *metaType = buildAbstractMetaTypeFromString(type); + if (*metaType && !(*metaType)->typeEntry()->isCustom()) + return QString(); return QString("%1_Check").arg(type); } @@ -939,8 +958,18 @@ QString ShibokenGenerator::cpythonIsConvertibleFunction(const TypeEntry* type, b QString ShibokenGenerator::cpythonIsConvertibleFunction(const AbstractMetaType* metaType, bool genericNumberType) { - if (metaType->typeEntry()->isCustom()) - return guessCPythonCheckFunction(metaType->typeEntry()->name()); + AbstractMetaType* type; + std::auto_ptr<AbstractMetaType> type_autoptr; + QString customCheck; + if (metaType->typeEntry()->isCustom()) { + customCheck = guessCPythonCheckFunction(metaType->typeEntry()->name(), &type); + if (type) { + type_autoptr = std::auto_ptr<AbstractMetaType>(type); + metaType = type; + } + if (!customCheck.isEmpty()) + return customCheck; + } QString baseName = cpythonBaseName(metaType); if (isNumber(baseName)) diff --git a/generator/shibokengenerator.h b/generator/shibokengenerator.h index 674bbeab2..729dc0bc0 100644 --- a/generator/shibokengenerator.h +++ b/generator/shibokengenerator.h @@ -296,7 +296,17 @@ public: QString cpythonTypeNameExt(const TypeEntry* type); QString cpythonCheckFunction(const TypeEntry* type, bool genericNumberType = false); QString cpythonCheckFunction(const AbstractMetaType* metaType, bool genericNumberType = false); - QString guessCPythonCheckFunction(const QString& type); + /** + * Receives the argument \p type and tries to find the appropriate AbstractMetaType for it + * or a custom type check. + * \param type A string representing the type to be discovered. + * \param metaType A pointer to an AbstractMetaType pointer, to where write a new meta type object + * if one is produced from the \p type string. This object must be deallocated by + * the caller. It will set the target variable to NULL, is \p type is a Python type. + * \return A custom check if \p type is a custom type, or an empty string if \p metaType + * receives an existing type object. + */ + QString guessCPythonCheckFunction(const QString& type, AbstractMetaType** metaType); QString cpythonIsConvertibleFunction(const TypeEntry* type, bool genericNumberType = false, bool checkExact = false); QString cpythonIsConvertibleFunction(const AbstractMetaType* metaType, bool genericNumberType = false); QString cpythonIsConvertibleFunction(const AbstractMetaArgument* metaArg, bool genericNumberType = false) |