diff options
author | Renato Araujo Oliveira Filho <renato.filho@openbossa.org> | 2009-08-27 16:46:56 -0300 |
---|---|---|
committer | Renato Araujo Oliveira Filho <renato.filho@openbossa.org> | 2009-08-27 18:43:08 -0300 |
commit | 6958a91921f3b2edb12679cf451289083d84ce7d (patch) | |
tree | 13228e42f0ba72344418785426bd8d107e0bf260 /generators | |
parent | 5a193b528142b15d2af726d0f2ab49f9aca258c3 (diff) |
Implemneted QAbstrctField as Python properties.
This implementation avoid Karmic python and boost conflict for readonly fields.
Reviewed by Lauro Neto <lauro.neto@openbossa.org>
Diffstat (limited to 'generators')
-rw-r--r-- | generators/boostpython/cppgenerator.cpp | 89 | ||||
-rw-r--r-- | generators/boostpython/cppgenerator.h | 6 |
2 files changed, 87 insertions, 8 deletions
diff --git a/generators/boostpython/cppgenerator.cpp b/generators/boostpython/cppgenerator.cpp index 1b51b4561..6103daaf1 100644 --- a/generators/boostpython/cppgenerator.cpp +++ b/generators/boostpython/cppgenerator.cpp @@ -594,6 +594,13 @@ void CppGenerator::writePrelude(QTextStream& s, const AbstractMetaClass* cppClas } } + //Fields + foreach (AbstractMetaField *field, cppClass->fields()) { + if (field->isPublic()) { + writeFieldAccess(s, cppClass, field); + } + } + //inject code native end writeCodeSnips(s, cppClass->typeEntry()->codeSnips(), CodeSnip::End, TypeSystem::NativeCode); @@ -846,6 +853,72 @@ AbstractMetaFunction* CppGenerator::findMainConstructor(const AbstractMetaClass* return 0; } +void CppGenerator::writeGetterFieldFunction(QTextStream &s, const AbstractMetaClass *cppClass, const AbstractMetaField *field) +{ + s << "static "; + + bool pointer = false; + if (field->type()->isQObject() || field->type()->isObject()) + pointer = true; + + if (pointer) + s << "python::object"; + else + s << field->type()->cppSignature(); + + s << " getter_" << cppClass->name() << "_" << field->name() << "("; + + if (!field->isStatic()) + s << cppClass->qualifiedCppName() << " &self"; + + s << ")" << endl << "{" << endl + << INDENT << "return "; + + if (pointer) + s << "python::object(PySide::ptr("; + + if (!field->isStatic()) + s << "self."; + else + s << field->enclosingClass()->typeEntry()->qualifiedCppName() << "::"; + + s << field->name(); + + if (pointer) + s << "))"; + + s << ";" << endl << "}" << endl; +} + +void CppGenerator::writeSetterFieldFunction(QTextStream &s, const AbstractMetaClass *cppClass, const AbstractMetaField *field) +{ + s << "static void setter_" << cppClass->name() << "_" << field->name() << "("; + + if (!field->isStatic()) + s << cppClass->qualifiedCppName() << " &self, "; + + s << field->type()->cppSignature() << " _value)" << endl << "{" << endl + << INDENT; + + if (!field->isStatic()) + s << "self."; + else + s << field->enclosingClass()->typeEntry()->qualifiedCppName() << "::"; + + s << field->name() << " = _value;" << endl << "}" << endl; +} + + +void CppGenerator::writeFieldAccess(QTextStream &s, const AbstractMetaClass *cppClass, const AbstractMetaField *field) +{ + Indentation indent(INDENT); + + writeGetterFieldFunction(s, cppClass, field); + if (!field->type()->isConstant()) + writeSetterFieldFunction(s, cppClass, field); +} + + void CppGenerator::writeHashFunction(QTextStream& s, const AbstractMetaClass* cppClass) { QString argType; @@ -943,15 +1016,15 @@ void CppGenerator::writeBoostDeclaration(QTextStream& s, const AbstractMetaClass QString strAccess; if (field->isPublic()) { - if (field->type()->isConstant()) - strAccess = "def_readonly"; - else - strAccess = "def_readwrite"; - s << INDENT << "python_cls." - << strAccess - << "(\"" << field->name() << "\", &" - << field->enclosingClass()->typeEntry()->qualifiedCppName() << "::" << field->name() << ");" << endl; + s << INDENT << "python_cls.add_property(" + << "\"" << field->name() << "\"" + << ", getter_" << cppClass->name() << "_" << field->name(); + if (!field->type()->isConstant()) + s << ", setter_" << cppClass->name() << "_" << field->name(); + + s << ");" << endl; + } } diff --git a/generators/boostpython/cppgenerator.h b/generators/boostpython/cppgenerator.h index 5fcc6f38f..7e4795d0a 100644 --- a/generators/boostpython/cppgenerator.h +++ b/generators/boostpython/cppgenerator.h @@ -79,6 +79,11 @@ private: QString operatorFunctionName(const AbstractMetaFunction *func); QString getOperatorArgumentTypeName(const AbstractMetaFunction *func, int argumentIndex); + // Field access related + void writeSetterFieldFunction(QTextStream &s, const AbstractMetaClass *cppClass, const AbstractMetaField *field); + void writeGetterFieldFunction(QTextStream &s, const AbstractMetaClass *cppClass, const AbstractMetaField *field); + void writeFieldAccess(QTextStream &s, const AbstractMetaClass *cppClass, const AbstractMetaField *field); + // call policy related QString verifyDefaultReturnPolicy(const AbstractMetaFunction *func, const QString &callPolicy); QString getFunctionCallPolicy(const AbstractMetaFunction *func); @@ -86,6 +91,7 @@ private: // enum related void writeEnums(QTextStream &s, const AbstractMetaClass *cppClass, bool useNamespace); void writeEnum(QTextStream &s, const AbstractMetaEnum *cppEnum, const QString &nameSpace); + // write implicitly conversions void writeImplicitlyConversion(QTextStream &s, const AbstractMetaClass *cppClass); void writeVirtualDefaultFunction(QTextStream &s, const AbstractMetaFunction *arg2); |