aboutsummaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-07-18 16:22:21 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:17:03 -0300
commit3016b0de40d93de2ca3eb690c1ae96529643f26a (patch)
tree4d60ed064f3b454ad12020ab906552f228609ae3 /generator
parent742fc4d4b723425a2d8ee1bea12116521d528c96 (diff)
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 <hugo.lima@openbossa.org> Reviewed by Lauro Moura <lauro.neto@openbossa.org> Reviewed by Luciano Wolf <luciano.wolf@openbossa.org>
Diffstat (limited to 'generator')
-rw-r--r--generator/cppgenerator.cpp18
-rw-r--r--generator/shibokengenerator.cpp20
-rw-r--r--generator/shibokengenerator.h13
3 files changed, 39 insertions, 12 deletions
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 <typeresolver.h>" << endl;
s << "#include <typeinfo>" << endl;
- if (usePySideExtensions()) {
- if (metaClass->isQObject()) {
- s << "#include <signalmanager.h>" << endl;
- s << "#include <pysidemetafunction.h>" << endl;
- }
+ if (usePySideExtensions() && metaClass->isQObject()) {
+ s << "#include <signalmanager.h>" << endl;
+ s << "#include <pysidemetafunction.h>" << 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);