Skip to content

Commit

Permalink
Used c++ pointer during the c++ class attribute get function.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Renato Araujo Oliveira Filho authored and hugopl committed Mar 8, 2012
1 parent 78e3c25 commit 3b74744
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
34 changes: 28 additions & 6 deletions generator/cppgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions libshiboken/bindingmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
9 changes: 8 additions & 1 deletion tests/samplebinding/class_fields_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,14 @@ def testAccessingValueTypeField(self):
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))
Expand Down

0 comments on commit 3b74744

Please sign in to comment.