aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-07-02 16:26:57 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2010-07-02 16:32:31 -0300
commit95b44627e2080c25a6fba858b18a68727c064d16 (patch)
tree3cfedabb14f77b04f9ef6292249ade06019c8c8c
parent7a726a6338e80bdfe9a4a926af29176012859f88 (diff)
Add support for static fields.
Reviewer: Marcelo Lira <marcelo.lira@openbossa.org> Luciano Wolf <luciano.wolf@openbossa.org>
-rw-r--r--cppgenerator.cpp22
-rw-r--r--tests/libsample/abstract.cpp2
-rw-r--r--tests/libsample/abstract.h1
-rw-r--r--tests/samplebinding/class_fields_test.py3
4 files changed, 27 insertions, 1 deletions
diff --git a/cppgenerator.cpp b/cppgenerator.cpp
index a9c9a8f6..1dd61e09 100644
--- a/cppgenerator.cpp
+++ b/cppgenerator.cpp
@@ -329,6 +329,8 @@ void CppGenerator::generateClass(QTextStream &s, const AbstractMetaClass *metaCl
if (shouldGenerateGetSetList(metaClass)) {
foreach (const AbstractMetaField* metaField, metaClass->fields()) {
+ if (metaField->isStatic())
+ continue;
writeGetterFunction(s, metaField);
if (!metaField->type()->isConstant())
writeSetterFunction(s, metaField);
@@ -338,6 +340,9 @@ void CppGenerator::generateClass(QTextStream &s, const AbstractMetaClass *metaCl
s << "// Getters and Setters for " << metaClass->name() << endl;
s << "static PyGetSetDef " << cpythonGettersSettersDefinitionName(metaClass) << "[] = {" << endl;
foreach (const AbstractMetaField* metaField, metaClass->fields()) {
+ if (metaField->isStatic())
+ continue;
+
bool hasSetter = !metaField->type()->isConstant();
s << INDENT << "{const_cast<char*>(\"" << metaField->name() << "\"), ";
s << cpythonGetterFunctionName(metaField);
@@ -2175,7 +2180,11 @@ bool CppGenerator::supportsSequenceProtocol(const AbstractMetaClass* metaClass)
bool CppGenerator::shouldGenerateGetSetList(const AbstractMetaClass* metaClass)
{
- return !metaClass->fields().isEmpty();
+ foreach (AbstractMetaField* f, metaClass->fields()) {
+ if (!f->isStatic())
+ return true;
+ }
+ return false;
}
bool CppGenerator::pythonFunctionWrapperUsesListOfArguments(const OverloadData& overloadData)
@@ -3207,6 +3216,17 @@ void CppGenerator::writeClassRegister(QTextStream& s, const AbstractMetaClass* m
if (metaClass->hasSignals())
writeSignalInitialization(s, metaClass);
+ // Write static fields
+ foreach (const AbstractMetaField* field, metaClass->fields()) {
+ if (!field->isStatic())
+ continue;
+ s << INDENT << "PyDict_SetItemString(" + cpythonTypeName(metaClass) + ".super.ht_type.tp_dict, \"";
+ s << field->name() << "\", ";
+ writeToPythonConversion(s, field->type(), metaClass, metaClass->qualifiedCppName() + "::" + field->name());
+ s << ");" << endl;
+ }
+ s << endl;
+
// class inject-code target/end
if (!metaClass->typeEntry()->codeSnips().isEmpty()) {
s << endl;
diff --git a/tests/libsample/abstract.cpp b/tests/libsample/abstract.cpp
index e81d2cb8..f1f26421 100644
--- a/tests/libsample/abstract.cpp
+++ b/tests/libsample/abstract.cpp
@@ -38,6 +38,8 @@
using namespace std;
+const int Abstract::staticPrimitiveField = 0;
+
Abstract::Abstract(int id) : m_id(id)
{
primitiveField = 123;
diff --git a/tests/libsample/abstract.h b/tests/libsample/abstract.h
index cdfbb30f..834a2f9b 100644
--- a/tests/libsample/abstract.h
+++ b/tests/libsample/abstract.h
@@ -54,6 +54,7 @@ public:
TpAbstract, TpDerived
};
+ static const int staticPrimitiveField;
int primitiveField;
Point valueTypeField;
ObjectType* objectTypeField;
diff --git a/tests/samplebinding/class_fields_test.py b/tests/samplebinding/class_fields_test.py
index 4578efb2..576e8d62 100644
--- a/tests/samplebinding/class_fields_test.py
+++ b/tests/samplebinding/class_fields_test.py
@@ -117,6 +117,9 @@ class TestAccessingCppFields(unittest.TestCase):
del d
self.assertEqual(getrefcount(o), refcount)
+ def testStaticField(self):
+ self.assertEqual(Derived.staticPrimitiveField, 0)
+
if __name__ == '__main__':
unittest.main()