summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2010-06-02 13:33:59 -0300
committerMarcelo Lira <marcelo.lira@openbossa.org>2010-06-03 08:46:38 -0300
commite2738233c5bffb1fd0ad0f44c1a930c366329faa (patch)
tree5c40c7a0b498297da24cb69d0cab971fd571271b
parent7bc6f1512b1febc026d81917f33b1ea90d23fe24 (diff)
downloadshiboken-e2738233c5bffb1fd0ad0f44c1a930c366329faa.tar.gz
shiboken-e2738233c5bffb1fd0ad0f44c1a930c366329faa.tar.xz
shiboken-e2738233c5bffb1fd0ad0f44c1a930c366329faa.zip
Added support for protected fields when not using the "protected hack".
Tests were also added.
-rw-r--r--cppgenerator.cpp36
-rw-r--r--headergenerator.cpp21
-rw-r--r--headergenerator.h1
-rw-r--r--shibokengenerator.cpp10
-rw-r--r--shibokengenerator.h2
-rw-r--r--tests/libsample/protected.h9
-rw-r--r--tests/samplebinding/CMakeLists.txt1
-rwxr-xr-xtests/samplebinding/protected_test.py13
-rw-r--r--tests/samplebinding/typesystem_sample.xml2
9 files changed, 85 insertions, 10 deletions
diff --git a/cppgenerator.cpp b/cppgenerator.cpp
index c2fc670f..53f86a4f 100644
--- a/cppgenerator.cpp
+++ b/cppgenerator.cpp
@@ -1085,10 +1085,10 @@ void CppGenerator::writeCppSelfDefinition(QTextStream& s, const AbstractMetaFunc
s << INDENT;
#ifdef AVOID_PROTECTED_HACK
- bool hasProtectedFunctions = func->ownerClass()->hasProtectedFunctions();
QString _wrapperName = wrapperName(func->ownerClass());
- s << (hasProtectedFunctions ? _wrapperName : func->ownerClass()->qualifiedCppName()) << "* " CPP_SELF_VAR " = ";
- s << (hasProtectedFunctions ? QString("(%1*)").arg(_wrapperName) : "");
+ bool hasProtectedMembers = func->ownerClass()->hasProtectedMembers();
+ s << (hasProtectedMembers ? _wrapperName : func->ownerClass()->qualifiedCppName()) << "* " CPP_SELF_VAR " = ";
+ s << (hasProtectedMembers ? QString("(%1*)").arg(_wrapperName) : "");
#else
s << func->ownerClass()->qualifiedCppName() << "* " CPP_SELF_VAR " = ";
#endif
@@ -2198,7 +2198,14 @@ void CppGenerator::writeGetterFunction(QTextStream& s, const AbstractMetaField*
s << "static PyObject* " << cpythonGetterFunctionName(metaField) << "(SbkBaseWrapper* self)" << endl;
s << '{' << endl;
s << INDENT << "return ";
- QString cppField= QString("%1->%2").arg(cpythonWrapperCPtr(metaField->enclosingClass(), "self")).arg(metaField->name());
+
+ QString cppField;
+#ifdef AVOID_PROTECTED_HACK
+ if (metaField->isProtected())
+ cppField = QString("((%1*)%2)->%3()").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;
@@ -2229,13 +2236,24 @@ void CppGenerator::writeSetterFunction(QTextStream& s, const AbstractMetaField*
}
s << INDENT << '}' << endl << endl;
- QString fieldStr = QString("%1->%2").arg(cpythonWrapperCPtr(metaField->enclosingClass(), "self")).arg(metaField->name());
-
-
- s << INDENT << fieldStr << " = ";
- writeToCppConversion(s, metaField->type(), metaField->enclosingClass(), "value");
+ s << INDENT;
+#ifdef AVOID_PROTECTED_HACK
+ if (metaField->isProtected()) {
+ QString fieldStr = QString("((%1*)%2)->%3").arg(wrapperName(metaField->enclosingClass())).arg(cpythonWrapperCPtr(metaField->enclosingClass(), "self")).arg(protectedFieldSetterName(metaField));
+ s << fieldStr << '(';
+ writeToCppConversion(s, metaField->type(), metaField->enclosingClass(), "value");
+ s << ')';
+ } else {
+#endif
+ QString fieldStr = QString("%1->%2").arg(cpythonWrapperCPtr(metaField->enclosingClass(), "self")).arg(metaField->name());
+ s << fieldStr << " = ";
+ writeToCppConversion(s, metaField->type(), metaField->enclosingClass(), "value");
+#ifdef AVOID_PROTECTED_HACK
+ }
+#endif
s << ';' << endl << endl;
+
bool pythonWrapperRefCounting = metaField->type()->typeEntry()->isObject()
|| metaField->type()->isValuePointer();
if (pythonWrapperRefCounting) {
diff --git a/headergenerator.cpp b/headergenerator.cpp
index f43fc2cd..d696b92c 100644
--- a/headergenerator.cpp
+++ b/headergenerator.cpp
@@ -45,6 +45,17 @@ void HeaderGenerator::writeCopyCtor(QTextStream& s, const AbstractMetaClass* met
s << INDENT << "}" << endl << endl;
}
+void HeaderGenerator::writeProtectedFieldAccessors(QTextStream& s, const AbstractMetaField* field) const
+{
+ QString fieldType = field->type()->cppSignature();
+ QString fieldName = field->enclosingClass()->qualifiedCppName() + "::" + field->name();
+
+ s << INDENT << "inline " << fieldType << ' ' << protectedFieldGetterName(field) << "()";
+ s << " { return " << fieldName << "; }" << endl;
+ s << INDENT << "inline void " << protectedFieldSetterName(field) << '(' << fieldType << " value)";
+ s << " { " << fieldName << " = value; }" << endl;
+}
+
void HeaderGenerator::generateClass(QTextStream& s, const AbstractMetaClass* metaClass)
{
ReportHandler::debugSparse("Generating header for " + metaClass->fullName());
@@ -90,6 +101,16 @@ void HeaderGenerator::generateClass(QTextStream& s, const AbstractMetaClass* met
writeFunction(s, func);
}
+#ifdef AVOID_PROTECTED_HACK
+ if (metaClass->hasProtectedFields()) {
+ foreach (AbstractMetaField* field, metaClass->fields()) {
+ if (!field->isProtected())
+ continue;
+ writeProtectedFieldAccessors(s, field);
+ }
+ }
+#endif
+
//destructor
#ifdef AVOID_PROTECTED_HACK
if (!metaClass->hasPrivateDestructor())
diff --git a/headergenerator.h b/headergenerator.h
index 0e7fe4eb..a8879d1d 100644
--- a/headergenerator.h
+++ b/headergenerator.h
@@ -38,6 +38,7 @@ protected:
private:
void writeCopyCtor(QTextStream &s, const AbstractMetaClass* metaClass) const;
+ void writeProtectedFieldAccessors(QTextStream& s, const AbstractMetaField* field) const;
void writeFunction(QTextStream& s, const AbstractMetaFunction* func) const;
void writePureVirtualEmptyImpl(QTextStream& , const AbstractMetaFunction* func) const;
void writeDefaultImplementation(QTextStream& s, const AbstractMetaFunction* func) const;
diff --git a/shibokengenerator.cpp b/shibokengenerator.cpp
index 16a216da..690490ce 100644
--- a/shibokengenerator.cpp
+++ b/shibokengenerator.cpp
@@ -217,6 +217,16 @@ QString ShibokenGenerator::protectedEnumSurrogateName(const AbstractMetaEnum* me
return metaEnum->fullName().replace(".", "_") + "_Surrogate";
}
+QString ShibokenGenerator::protectedFieldGetterName(const AbstractMetaField* field)
+{
+ return QString("protected_%1_getter").arg(field->name());
+}
+
+QString ShibokenGenerator::protectedFieldSetterName(const AbstractMetaField* field)
+{
+ return QString("protected_%1_setter").arg(field->name());
+}
+
QString ShibokenGenerator::cpythonFunctionName(const AbstractMetaFunction* func)
{
QString result;
diff --git a/shibokengenerator.h b/shibokengenerator.h
index bb1dcae5..b45de8cd 100644
--- a/shibokengenerator.h
+++ b/shibokengenerator.h
@@ -214,6 +214,8 @@ public:
static QString wrapperName(const AbstractMetaClass* metaClass);
static QString protectedEnumSurrogateName(const AbstractMetaEnum* metaEnum);
+ static QString protectedFieldGetterName(const AbstractMetaField* field);
+ static QString protectedFieldSetterName(const AbstractMetaField* field);
static QString pythonPrimitiveTypeName(const QString& cppTypeName);
static QString pythonPrimitiveTypeName(const PrimitiveTypeEntry* type);
diff --git a/tests/libsample/protected.h b/tests/libsample/protected.h
index d8d55a40..8ba4914f 100644
--- a/tests/libsample/protected.h
+++ b/tests/libsample/protected.h
@@ -126,5 +126,14 @@ protected:
virtual PublicEnum publicEnumMethod(PublicEnum in) { return in; }
};
+
+class LIBSAMPLE_API ProtectedProperty
+{
+public:
+ ProtectedProperty() : protectedProperty(0) {}
+protected:
+ int protectedProperty;
+};
+
#endif // PROTECTED_H
diff --git a/tests/samplebinding/CMakeLists.txt b/tests/samplebinding/CMakeLists.txt
index 8775802b..8848212d 100644
--- a/tests/samplebinding/CMakeLists.txt
+++ b/tests/samplebinding/CMakeLists.txt
@@ -52,6 +52,7 @@ ${CMAKE_CURRENT_BINARY_DIR}/sample/protectednonpolymorphic_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphic_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphicdaughter_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/protectedpolymorphicgranddaughter_wrapper.cpp
+${CMAKE_CURRENT_BINARY_DIR}/sample/protectedproperty_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/protectedvirtualdestructor_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/reference_wrapper.cpp
${CMAKE_CURRENT_BINARY_DIR}/sample/sample_module_wrapper.cpp
diff --git a/tests/samplebinding/protected_test.py b/tests/samplebinding/protected_test.py
index fa477c42..356df1f7 100755
--- a/tests/samplebinding/protected_test.py
+++ b/tests/samplebinding/protected_test.py
@@ -30,7 +30,7 @@ import unittest
from sample import ProtectedNonPolymorphic, ProtectedVirtualDestructor
from sample import ProtectedPolymorphic, ProtectedPolymorphicDaughter, ProtectedPolymorphicGrandDaughter
-from sample import ProtectedEnumClass
+from sample import ProtectedProperty, ProtectedEnumClass
from sample import PrivateDtor
from sample import Point
@@ -243,6 +243,17 @@ class ProtectedEnumTest(unittest.TestCase):
self.assertEqual(obj.callPublicEnumMethod(ProtectedEnumClass.PublicItem1), ProtectedEnumClass.PublicItem0)
+class ProtectedPropertyTest(unittest.TestCase):
+ '''Test cases for a class with a protected property (or field in C++).'''
+
+ def testProtectedProperty(self):
+ '''Writes and reads a protected property.'''
+ obj = ProtectedProperty()
+
+ obj.protectedProperty = 3
+ self.assertEqual(obj.protectedProperty, 3)
+
+
class PrivateDtorProtectedMethodTest(unittest.TestCase):
'''Test cases for classes with private destructors and protected methods.'''
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index e53486fc..a4e94107 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -284,6 +284,8 @@
<enum-type name="ProtectedEnumClass::ProtectedEnum" />
<enum-type name="ProtectedEnumClass::PublicEnum" />
+ <value-type name="ProtectedProperty" />
+
<template name="boolptr_at_end_fix_beginning">
bool __ok__;
%PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](