summaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorLauro Neto <lauro.neto@openbossa.org>2010-07-19 17:13:51 -0300
committerrenatofilho <renato.filho@openbossa.org>2010-10-11 19:22:08 -0300
commit4478a25b66729490e27460a7fa59ae9bceadd1fd (patch)
tree00727eeb2e673818f0b5c103c338a9d76f6007b3 /generator
parent3ef179b87fb133253181b733e3d1605103632856 (diff)
downloadshiboken-4478a25b66729490e27460a7fa59ae9bceadd1fd.tar.gz
shiboken-4478a25b66729490e27460a7fa59ae9bceadd1fd.tar.xz
shiboken-4478a25b66729490e27460a7fa59ae9bceadd1fd.zip
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 b890df64..1ad7a2d1 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 55591c59..b356ca87 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);