diff options
author | Renato Araujo Oliveira Filho <renato.filho@openbossa.org> | 2011-01-13 15:04:03 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:12:53 -0300 |
commit | 3b747443bde5752277aa9791adbfc1d32868689b (patch) | |
tree | 46331deab4d066a8588504a0a4c9867a74615200 | |
parent | 78e3c256ff6a154ce8068b3dd7bc551394d73422 (diff) |
Used c++ pointer during the c++ class attribute get function.
Now all function get for class attribute return the internal memory of
the c++ attribute.
Reviewer: Hugo Parente <hugo.lima@openbossa.org>
Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r-- | generator/cppgenerator.cpp | 34 | ||||
-rw-r--r-- | libshiboken/bindingmanager.cpp | 2 | ||||
-rw-r--r-- | tests/samplebinding/class_fields_test.py | 9 |
3 files changed, 36 insertions, 9 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp index 01f8a6d47..dcee8fd77 100644 --- a/generator/cppgenerator.cpp +++ b/generator/cppgenerator.cpp @@ -2711,18 +2711,40 @@ void CppGenerator::writeGetterFunction(QTextStream& s, const AbstractMetaField* { s << "static PyObject* " << cpythonGetterFunctionName(metaField) << "(PyObject* self, void*)" << endl; s << '{' << endl; - s << INDENT << "return "; + s << INDENT << "PyObject* val = "; QString cppField; + AbstractMetaType *metaType = metaField->type(); + // Force use of pointer to return internal variable memory + bool useReference = (!metaType->isConstant() && + !metaType->isEnum() && + !metaType->isPrimitive() && + metaType->indirections() == 0); + #ifdef AVOID_PROTECTED_HACK if (metaField->isProtected()) - cppField = QString("((%1*)%2)->%3()").arg(wrapperName(metaField->enclosingClass())).arg(cpythonWrapperCPtr(metaField->enclosingClass(), "self")).arg(protectedFieldGetterName(metaField)); + cppField = QString("(%1(%2*)%3)->%4()") + .arg(useReference ? '&' : ' ') + .arg(wrapperName(metaField->enclosingClass())) + .arg(cpythonWrapperCPtr(metaField->enclosingClass(), "self")) + .arg(protectedFieldGetterName(metaField)); else #endif - cppField= QString("%1->%2").arg(cpythonWrapperCPtr(metaField->enclosingClass(), "self")).arg(metaField->name()); - writeToPythonConversion(s, metaField->type(), metaField->enclosingClass(), cppField); - s << ';' << endl; - s << '}' << endl; + cppField= QString("%1%2->%3") + .arg(useReference ? '&' : ' ') + .arg(cpythonWrapperCPtr(metaField->enclosingClass(), "self")) + .arg(metaField->name()); + + if (useReference) { + s << INDENT << "Shiboken::createWrapper(" << cppField << ");" << endl; + s << INDENT << "Shiboken::Object::releaseOwnership(val);" << endl; + } else { + writeToPythonConversion(s, metaField->type(), metaField->enclosingClass(), cppField); + s << ';' << endl; + } + + s << INDENT << "return val;" << endl + << endl << '}' << endl; } void CppGenerator::writeSetterFunction(QTextStream& s, const AbstractMetaField* metaField) diff --git a/libshiboken/bindingmanager.cpp b/libshiboken/bindingmanager.cpp index 3aa4c88e5..ba9f77eb3 100644 --- a/libshiboken/bindingmanager.cpp +++ b/libshiboken/bindingmanager.cpp @@ -128,8 +128,6 @@ void BindingManager::BindingManagerPrivate::assignWrapper(SbkObject* wrapper, co WrapperMap::iterator iter = wrapperMapper.find(cptr); if (iter == wrapperMapper.end()) wrapperMapper.insert(std::make_pair(cptr, wrapper)); - else - iter->second = wrapper; } BindingManager::BindingManager() diff --git a/tests/samplebinding/class_fields_test.py b/tests/samplebinding/class_fields_test.py index f0791e1d2..c01a6bf07 100644 --- a/tests/samplebinding/class_fields_test.py +++ b/tests/samplebinding/class_fields_test.py @@ -64,7 +64,14 @@ class TestAccessingCppFields(unittest.TestCase): new_value = Point(-10, 537) d.valueTypeField = new_value self.assertEqual(d.valueTypeField, new_value) - self.assert_(not d.valueTypeField == old_value) + + #object modify + d.valueTypeField.setX(10) + d.valueTypeField.setY(20) + self.assertEqual(d.valueTypeField.x(), 10) + self.assertEqual(d.valueTypeField.y(), 20) + + # attribution with invalid type self.assertRaises(TypeError, lambda : setattr(d, 'valueTypeField', 123)) |