aboutsummaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorLauro Neto <lauro.neto@openbossa.org>2010-07-19 17:13:51 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:07:21 -0300
commit4b80e2ef898194ed9d7c2989dd4d9fe82453aeb1 (patch)
treeafbf4694b4d99d4f74cd3fffdcb889109549529d /generator
parent39aac44e2ff5dce3c525b211dc7186ddf01754cb (diff)
Initial copy function generator code
Reviewer: Renato Araújo <renato.filho@openbossa.org> Luciano Wolf <luciano.wolf@openbossa.org>
Diffstat (limited to 'generator')
-rw-r--r--generator/cppgenerator.cpp49
-rw-r--r--generator/cppgenerator.h2
2 files changed, 50 insertions, 1 deletions
diff --git a/generator/cppgenerator.cpp b/generator/cppgenerator.cpp
index b890df64b..1ad7a2d16 100644
--- a/generator/cppgenerator.cpp
+++ b/generator/cppgenerator.cpp
@@ -320,12 +320,18 @@ void CppGenerator::generateClass(QTextStream &s, const AbstractMetaClass *metaCl
QString className = cpythonTypeName(metaClass).replace(QRegExp("_Type$"), "");
+ if (metaClass->typeEntry()->isValue())
+ writeCopyFunction(s, metaClass);
+
// Write single method definitions
s << singleMethodDefinitions;
// Write methods definition
s << "static PyMethodDef " << className << "_methods[] = {" << endl;
- s << methodsDefinitions << INDENT << "{0} // Sentinel" << endl;
+ s << methodsDefinitions << endl;
+ if (metaClass->typeEntry()->isValue())
+ s << INDENT << "{\"__copy__\", (PyCFunction)" << className << "___copy__" << ", METH_NOARGS}," << endl;
+ s << INDENT << "{0} // Sentinel" << endl;
s << "};" << endl << endl;
// Write tp_getattro function
@@ -2655,6 +2661,47 @@ void CppGenerator::writeTypeAsNumberDefinition(QTextStream& s, const AbstractMet
s << "};" << endl << endl;
}
+void CppGenerator::writeCopyFunction(QTextStream& s, const AbstractMetaClass *metaClass)
+{
+ QString className = cpythonTypeName(metaClass).replace(QRegExp("_Type$"), "");
+
+ Indentation indent(INDENT);
+
+ s << "static PyObject *" << className << "___copy__(PyObject *self)" << endl;
+ s << "{" << endl;
+ s << INDENT << metaClass->qualifiedCppName() << "* " CPP_SELF_VAR " = 0;" << endl;
+ s << INDENT << "if (Shiboken::cppObjectIsInvalid(self))" << endl;
+ {
+ Indentation indent(INDENT);
+ s << INDENT << "return 0;" << endl;
+ }
+
+ s << INDENT << "cppSelf = Shiboken::Converter<" << metaClass->qualifiedCppName() << "*>::toCpp(self);" << endl;
+ s << INDENT << "PyObject* " PYTHON_RETURN_VAR " = 0;" << endl;
+
+ s << INDENT << metaClass->qualifiedCppName() << "* copy = new " << metaClass->qualifiedCppName();
+ s << "(*cppSelf);" << endl;
+ s << INDENT << PYTHON_RETURN_VAR " = Shiboken::Converter<" << metaClass->qualifiedCppName();
+ s << "*>::toPython(copy);" << endl;
+
+ s << INDENT << "SbkBaseWrapper_setOwnership(" PYTHON_RETURN_VAR ", true);" << endl;
+
+ s << endl;
+
+ s << INDENT << "if (PyErr_Occurred() || !" PYTHON_RETURN_VAR ") {" << endl;
+ {
+ Indentation indent(INDENT);
+ s << INDENT << "Py_XDECREF(" PYTHON_RETURN_VAR ");" << endl;
+ s << INDENT << "return 0;" << endl;
+ }
+
+ s << INDENT << "}" << endl;
+
+ s << INDENT << "return " PYTHON_RETURN_VAR ";" << endl;
+ s << "}" << endl;
+ s << endl;
+}
+
void CppGenerator::writeGetterFunction(QTextStream& s, const AbstractMetaField* metaField)
{
s << "static PyObject* " << cpythonGetterFunctionName(metaField) << "(PyObject* self, void*)" << endl;
diff --git a/generator/cppgenerator.h b/generator/cppgenerator.h
index 55591c598..b356ca873 100644
--- a/generator/cppgenerator.h
+++ b/generator/cppgenerator.h
@@ -137,6 +137,8 @@ private:
void writeTypeAsSequenceDefinition(QTextStream& s, const AbstractMetaClass* metaClass);
void writeTypeAsNumberDefinition(QTextStream& s, const AbstractMetaClass* metaClass);
+ void writeCopyFunction(QTextStream& s, const AbstractMetaClass *metaClass);
+
void writeGetterFunction(QTextStream& s, const AbstractMetaField* metaField);
void writeSetterFunction(QTextStream& s, const AbstractMetaField* metaField);