aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-12-29 18:26:44 -0200
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:12:52 -0300
commit882c98f7a9b3a3fc030cee87235cf8b375cd6b54 (patch)
treebe9d5723a0f914bf96ac955c5d0a0356dbf1d051
parent2faf996129baf265c4b9ed1ff72a4cfb378d8306 (diff)
Add support for operator overload injection.
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org> Lauro Moura <lauro.neto@openbossa.org>
-rw-r--r--generator/cppgenerator.cpp40
-rw-r--r--generator/shibokengenerator.cpp5
-rw-r--r--tests/samplebinding/objecttypeoperators_test.py3
-rw-r--r--tests/samplebinding/typesystem_sample.xml8
4 files changed, 38 insertions, 18 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp
index c186ab607..e49922b51 100644
--- a/generator/cppgenerator.cpp
+++ b/generator/cppgenerator.cpp
@@ -2793,10 +2793,10 @@ void CppGenerator::writeRichCompareFunction(QTextStream& s, const AbstractMetaCl
{
QString baseName = cpythonBaseName(metaClass);
s << "static PyObject* ";
- s << baseName << "_richcompare(PyObject* self, PyObject* other, int op)" << endl;
+ s << baseName << "_richcompare(PyObject* self, PyObject* arg, int op)" << endl;
s << '{' << endl;
QList<AbstractMetaFunctionList> cmpOverloads = filterGroupedOperatorFunctions(metaClass, AbstractMetaClass::ComparisonOp);
- s << INDENT << "PyObject* result = 0;" << endl;
+ s << INDENT << "PyObject* " PYTHON_RETURN_VAR " = 0;" << endl;
s << INDENT << metaClass->qualifiedCppName() << "& " CPP_SELF_VAR " = *" << cpythonWrapperCPtr(metaClass) << ';' << endl;
s << endl;
@@ -2840,7 +2840,7 @@ void CppGenerator::writeRichCompareFunction(QTextStream& s, const AbstractMetaCl
s << INDENT;
}
- s << "if (" << cpythonIsConvertibleFunction(type, numberType) << "(other)) {" << endl;
+ s << "if (" << cpythonIsConvertibleFunction(type, numberType) << "(arg)) {" << endl;
{
Indentation indent(INDENT);
s << INDENT << "// " << func->signature() << endl;
@@ -2848,21 +2848,27 @@ void CppGenerator::writeRichCompareFunction(QTextStream& s, const AbstractMetaCl
s << translateTypeForWrapperMethod(type, 0, ExcludeReference | ExcludeConst);
if (type->isObject() || type->isQObject())
s << '&';
- s << " cppOther = ";
- writeToCppConversion(s, type, 0, "other", ExcludeReference | ExcludeConst);
+ s << " cppArg0 = ";
+ writeToCppConversion(s, type, 0, "arg", ExcludeReference | ExcludeConst);
s << ';' << endl;
- s << INDENT << "result = ";
- if (!func->type()) {
- s << "Py_None;" << endl;
- s << INDENT << "Py_INCREF(Py_None);" << endl;
- s << INDENT << CPP_SELF_VAR " " << op << " cppOther; // this op return void" << endl;
+ // If the function is user added, use the inject code
+ if (func->isUserAdded()) {
+ CodeSnipList snips = func->injectedCodeSnips();
+ writeCodeSnips(s, snips, CodeSnip::Any, TypeSystem::TargetLangCode, func, func->arguments().last());
} else {
- QByteArray self(CPP_SELF_VAR);
- if (func->isPointerOperator())
- self.prepend('&');
- writeToPythonConversion(s, func->type(), metaClass, self + ' ' + op + " cppOther");
- s << ';' << endl;
+ s << INDENT << PYTHON_RETURN_VAR " = ";
+ if (!func->type()) {
+ s << "Py_None;" << endl;
+ s << INDENT << "Py_INCREF(Py_None);" << endl;
+ s << INDENT << CPP_SELF_VAR " " << op << " cppArg0; // this op return void" << endl;
+ } else {
+ QByteArray self(CPP_SELF_VAR);
+ if (func->isPointerOperator())
+ self.prepend('&');
+ writeToPythonConversion(s, func->type(), metaClass, self + ' ' + op + " cppArg0");
+ s << ';' << endl;
+ }
}
}
s << INDENT << '}';
@@ -2881,10 +2887,10 @@ void CppGenerator::writeRichCompareFunction(QTextStream& s, const AbstractMetaCl
}
s << INDENT << '}' << endl << endl;
- s << INDENT << "if (result && !PyErr_Occurred())" << endl;
+ s << INDENT << "if (" PYTHON_RETURN_VAR " && !PyErr_Occurred())" << endl;
{
Indentation indent(INDENT);
- s << INDENT << "return result;" << endl;
+ s << INDENT << "return " PYTHON_RETURN_VAR ";" << endl;
}
s << INDENT << baseName << "_RichComparison_TypeError:" << endl;
s << INDENT << "PyErr_SetString(PyExc_NotImplementedError, \"operator not implemented.\");" << endl;
diff --git a/generator/shibokengenerator.cpp b/generator/shibokengenerator.cpp
index 2647e634b..ebf63ab8c 100644
--- a/generator/shibokengenerator.cpp
+++ b/generator/shibokengenerator.cpp
@@ -1218,6 +1218,11 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
} else {
cppSelf = "cppSelf";
}
+
+ // on comparison operator cppSelf is always a reference.
+ if (func->isComparisonOperator())
+ replacement = "%1.";
+
code.replace("%CPPSELF.", replacement.arg(cppSelf));
code.replace("%CPPSELF", cppSelf);
diff --git a/tests/samplebinding/objecttypeoperators_test.py b/tests/samplebinding/objecttypeoperators_test.py
index 148bc3591..71e5a5d40 100644
--- a/tests/samplebinding/objecttypeoperators_test.py
+++ b/tests/samplebinding/objecttypeoperators_test.py
@@ -47,6 +47,9 @@ class ObjectTypeOperatorsTest(unittest.TestCase):
self.assertEqual("a", a)
self.assertEqual(a, "a")
+ def testOperatorInjection(self):
+ a = ObjectTypeOperators("a")
+ self.assertNotEqual(a, "b")
if __name__ == '__main__':
unittest.main()
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index 576c54656..5a518c34a 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -1274,7 +1274,13 @@
<object-type name="HandleHolder" />
- <object-type name="ObjectTypeOperators" />
+ <object-type name="ObjectTypeOperators">
+ <add-function signature="operator!=(std::string)" return-type="bool">
+ <inject-code class="target">
+ %PYARG_0 = %CONVERTTOPYTHON[bool](%CPPSELF.key() != %1);
+ </inject-code>
+ </add-function>
+ </object-type>
<!-- type used in abstract method -->
<object-type name="HideType" generate="no" />