summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/typesystemvariables.rst20
-rw-r--r--shibokengenerator.cpp20
-rw-r--r--tests/samplebinding/typesystem_sample.xml72
3 files changed, 69 insertions, 43 deletions
diff --git a/doc/typesystemvariables.rst b/doc/typesystemvariables.rst
index 9966cfc5..1a6a36f9 100644
--- a/doc/typesystemvariables.rst
+++ b/doc/typesystemvariables.rst
@@ -15,11 +15,11 @@ Variables
=========
-.. _return_argument:
+.. _cpp_return_argument:
**%0**
- Replaced by the name of the return variable of the Python method/function wrapper.
+ Replaced by the C++ return variable of the Python method/function wrapper.
.. _arg_number:
@@ -120,6 +120,14 @@ Variables
value ``123``.
+.. _converttocpp:
+
+**%CONVERTTOCPP[CPPTYPE]**
+
+ Replaced by a |project| conversion call that converts a Python variable
+ to a C++ variable of the type indicated by ``CPPTYPE``.
+
+
.. _converttopython:
**%CONVERTTOPYTHON[CPPTYPE]**
@@ -143,6 +151,14 @@ Variables
Replaced by the name of a function or method.
+
+.. _py_return_argument:
+
+**%PYARG_0**
+
+ Replaced by the name of the Python return variable of the Python method/function wrapper.
+
+
.. _pyarg:
**%PYARG_#**
diff --git a/shibokengenerator.cpp b/shibokengenerator.cpp
index feb14b96..2094749f 100644
--- a/shibokengenerator.cpp
+++ b/shibokengenerator.cpp
@@ -848,6 +848,7 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
const AbstractMetaClass* context)
{
static QRegExp toPythonRegex("%CONVERTTOPYTHON\\[([^\\[]*)\\]");
+ static QRegExp toCppRegex("%CONVERTTOCPP\\[([^\\[]*)\\]");
static QRegExp pyArgsRegex("%PYARG_(\\d+)");
// detect is we should use pyargs instead of args as variable name for python arguments
@@ -884,8 +885,12 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
// replace "toPython "converters
code.replace(toPythonRegex, "Shiboken::Converter<\\1 >::toPython");
+ // replace "toCpp "converters
+ code.replace(toCppRegex, "Shiboken::Converter<\\1 >::toCpp");
+
if (func) {
// replace %PYARG_# variables
+ code.replace("%PYARG_0", retvalVariableName());
if (snip.language == TypeSystem::TargetLangCode) {
if (numArgs > 1) {
code.replace(pyArgsRegex, "pyargs[\\1-1]");
@@ -920,10 +925,15 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
}
// replace template variable for return variable name
- if (func->isConstructor())
+ if (func->isConstructor()) {
+ code.replace("%0.", QString("%1->").arg("cptr"));
code.replace("%0", "cptr");
- else
- code.replace("%0", retvalVariableName());
+ } else if (func->type()) {
+ QString pyRetVal = cpythonWrapperCPtr(func->type(), retvalVariableName());
+ if (func->type()->typeEntry()->isValue() || func->type()->typeEntry()->isObject())
+ code.replace("%0.", QString("%1->").arg(pyRetVal));
+ code.replace("%0", pyRetVal);
+ }
// replace template variable for self Python object
QString pySelf;
@@ -953,7 +963,7 @@ void ShibokenGenerator::writeCodeSnips(QTextStream& s,
}
}
- // replace template variables for individual arguments
+ // replace template variables %# for individual arguments
int removed = 0;
for (int i = 0; i < func->arguments().size(); i++) {
const AbstractMetaArgument* arg = func->arguments().at(i);
@@ -1099,7 +1109,7 @@ bool ShibokenGenerator::injectedCodeCallsPythonOverride(const AbstractMetaFuncti
bool ShibokenGenerator::injectedCodeHasReturnValueAttribution(const AbstractMetaFunction* func)
{
- static QRegExp retValAttributionRegexCheck("%0\\s*=[^=]\\s*.+");
+ static QRegExp retValAttributionRegexCheck("%PYARG_0\\s*=[^=]\\s*.+");
CodeSnipList snips = func->injectedCodeSnips(CodeSnip::Any, TypeSystem::TargetLangCode);
foreach (CodeSnip snip, snips) {
if (retValAttributionRegexCheck.indexIn(snip.code()) != -1)
diff --git a/tests/samplebinding/typesystem_sample.xml b/tests/samplebinding/typesystem_sample.xml
index c3c5786b..6ae08c2b 100644
--- a/tests/samplebinding/typesystem_sample.xml
+++ b/tests/samplebinding/typesystem_sample.xml
@@ -82,7 +82,7 @@
<inject-code class="native" position="beginning">
static void reparent_layout_items(PyObject* parent, PyObject* layout)
{
- const ObjectTypeList&amp; objChildren = SbkObjectTypeLayout_cptr(layout)->objects();
+ const ObjectTypeList&amp; objChildren = %CONVERTTOCPP[ObjectTypeLayout*](layout)->objects();
ObjectTypeList::const_iterator it = objChildren.begin();
for (; it != objChildren.end(); ++it) {
if ((*it)->isLayoutType()) {
@@ -123,9 +123,9 @@
</modify-function>
<modify-function signature="children()const">
<inject-code class="target" position="end">
- Py_ssize_t max = PyList_GET_SIZE(%0);
+ Py_ssize_t max = PyList_GET_SIZE(%PYARG_0);
for (int i = 0; i &lt; max; ++i) {
- Shiboken::setParent(%PYSELF, PyList_GET_ITEM(%0, i));
+ Shiboken::setParent(%PYSELF, PyList_GET_ITEM(%PYARG_0, i));
}
</inject-code>
</modify-function>
@@ -171,7 +171,7 @@
</modify-function>
<modify-function signature="modifiedProtectedSum(int, int)">
<inject-code class="target" position="beginning">
- %0 = %CONVERTTOPYTHON[%RETURN_TYPE](%CPPSELF.%TYPE::%FUNCTION_NAME(%1, %2) * 10);
+ %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%CPPSELF.%TYPE::%FUNCTION_NAME(%1, %2) * 10);
</inject-code>
</modify-function>
<modify-function signature="dataTypeName(void*) const" remove="all"/>
@@ -182,7 +182,7 @@
</modify-function>
<add-function signature="dataTypeName(PyObject*)const" return-type="const char*">
<inject-code class="target" position="beginning">
- %0 = %CONVERTTOPYTHON[%RETURN_TYPE](%CPPSELF.%FUNCTION_NAME(%PYARG_1));
+ %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](%CPPSELF.%FUNCTION_NAME(%PYARG_1));
</inject-code>
</add-function>
</value-type>
@@ -221,45 +221,45 @@
<template name="boolptr_at_end_fix_beginning">
bool __ok__;
- %0 = %CONVERTTOPYTHON[%RETURN_TYPE](
+ %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](
%CPPSELF.%TYPE::%FUNCTION_NAME(%ARGUMENT_NAMES, &amp;__ok__)
);
</template>
<template name="boolptr_at_start_fix_beginning">
bool __ok__;
- %0 = %CONVERTTOPYTHON[%RETURN_TYPE](
+ %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](
%CPPSELF.%TYPE::%FUNCTION_NAME(&amp;__ok__, %ARGUMENT_NAMES)
);
</template>
<template name="boolptr_fix_end">
PyObject* _tuple_ = PyTuple_New(2);
- PyTuple_SET_ITEM(_tuple_, 0, %0);
+ PyTuple_SET_ITEM(_tuple_, 0, %PYARG_0);
PyObject* _item_ = %CONVERTTOPYTHON[bool](__ok__);
PyTuple_SET_ITEM(_tuple_, 1, _item_);
- %0 = _tuple_;
+ %PYARG_0 = _tuple_;
</template>
<template name="return_4_arguments_as_tuple">
- %0 = PyTuple_New(4);
- PyTuple_SET_ITEM(%0, 0, %CONVERTTOPYTHON[%ARG1_TYPE](%1));
- PyTuple_SET_ITEM(%0, 1, %CONVERTTOPYTHON[%ARG2_TYPE](%2));
- PyTuple_SET_ITEM(%0, 2, %CONVERTTOPYTHON[%ARG3_TYPE](%3));
- PyTuple_SET_ITEM(%0, 3, %CONVERTTOPYTHON[%ARG4_TYPE](%4));
+ %PYARG_0 = PyTuple_New(4);
+ PyTuple_SET_ITEM(%PYARG_0, 0, %CONVERTTOPYTHON[%ARG1_TYPE](%1));
+ PyTuple_SET_ITEM(%PYARG_0, 1, %CONVERTTOPYTHON[%ARG2_TYPE](%2));
+ PyTuple_SET_ITEM(%PYARG_0, 2, %CONVERTTOPYTHON[%ARG3_TYPE](%3));
+ PyTuple_SET_ITEM(%PYARG_0, 3, %CONVERTTOPYTHON[%ARG4_TYPE](%4));
</template>
<template name="return_5_arguments_as_tuple">
- %0 = PyTuple_New(5);
- PyTuple_SET_ITEM(%0, 0, %CONVERTTOPYTHON[%ARG1_TYPE](%1));
- PyTuple_SET_ITEM(%0, 1, %CONVERTTOPYTHON[%ARG2_TYPE](%2));
- PyTuple_SET_ITEM(%0, 2, %CONVERTTOPYTHON[%ARG3_TYPE](%3));
- PyTuple_SET_ITEM(%0, 3, %CONVERTTOPYTHON[%ARG4_TYPE](%4));
- PyTuple_SET_ITEM(%0, 4, %CONVERTTOPYTHON[%ARG5_TYPE](%5));
+ %PYARG_0 = PyTuple_New(5);
+ PyTuple_SET_ITEM(%PYARG_0, 0, %CONVERTTOPYTHON[%ARG1_TYPE](%1));
+ PyTuple_SET_ITEM(%PYARG_0, 1, %CONVERTTOPYTHON[%ARG2_TYPE](%2));
+ PyTuple_SET_ITEM(%PYARG_0, 2, %CONVERTTOPYTHON[%ARG3_TYPE](%3));
+ PyTuple_SET_ITEM(%PYARG_0, 3, %CONVERTTOPYTHON[%ARG4_TYPE](%4));
+ PyTuple_SET_ITEM(%PYARG_0, 4, %CONVERTTOPYTHON[%ARG5_TYPE](%5));
</template>
<template name="return_none">
- %0 = Py_None;
+ %PYARG_0 = Py_None;
Py_INCREF(Py_None);
</template>
@@ -270,7 +270,7 @@
<remove-argument/>
</modify-argument>
<inject-code class="target" position="beginning">
- %0 = %CONVERTTOPYTHON[%RETURN_TYPE](
+ %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](
%CPPSELF.%TYPE::%FUNCTION_NAME(%1, true, %3, %4)
);
</inject-code>
@@ -526,7 +526,7 @@
<remove-argument/>
</modify-argument>
<inject-code class="target" position="beginning">
- %0 = %CONVERTTOPYTHON[%RETURN_TYPE](
+ %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](
%CPPSELF.%TYPE::%FUNCTION_NAME(%1, %1+%3, %3)
);
</inject-code>
@@ -544,7 +544,7 @@
</modify-function>
<modify-function signature="name()">
<inject-code class="native" position="end">
- SbkStr_cptr(%0)->prepend(Str("Pimped"));
+ %0.prepend(Str("Pimped"));
</inject-code>
</modify-function>
<modify-function signature="callMe()">
@@ -574,10 +574,10 @@
</add-function>
<modify-function signature="pointer() const">
<inject-code class="target" position="beginning">
- %0 = (PyObject*)%CPPSELF.%FUNCTION_NAME();
- if (!%0)
- %0 = Py_None;
- Py_XINCREF(%0);
+ %PYARG_0 = (PyObject*)%CPPSELF.%FUNCTION_NAME();
+ if (!%PYARG_0)
+ %PYARG_0 = Py_None;
+ Py_XINCREF(%PYARG_0);
</inject-code>
</modify-function>
</value-type>
@@ -609,7 +609,7 @@
</inject-code>
<inject-code class="target" position="end">
PyObject* tmp = PyString_FromString("end");
- PyString_Concat(&amp;%0, tmp);
+ PyString_Concat(&amp;%PYARG_0, tmp);
Py_DECREF(tmp);
</inject-code>
</modify-function>
@@ -618,7 +618,7 @@
<modify-function signature="simpleMethod2()">
<inject-code class="target" position="end">
PyObject* tmp = PyString_FromString("end");
- PyString_Concat(&amp;%0, tmp);
+ PyString_Concat(&amp;%PYARG_0, tmp);
Py_DECREF(tmp);
</inject-code>
</modify-function>
@@ -639,7 +639,7 @@
return 0;
}
%RETURN_TYPE foo = %CPPSELF.%FUNCTION_NAME(argc, argv);
- %0 = %CONVERTTOPYTHON[%RETURN_TYPE](foo);
+ %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](foo);
for (int i = 0; i &lt; argc; ++i)
delete[] argv[i];
@@ -663,7 +663,7 @@
return 0;
}
%RETURN_TYPE foo = %CPPSELF.%FUNCTION_NAME(argc, argv);
- %0 = %CONVERTTOPYTHON[%RETURN_TYPE](foo);
+ %PYARG_0 = %CONVERTTOPYTHON[%RETURN_TYPE](foo);
for (int i = 0; i &lt; argc; ++i)
delete[] argv[i];
@@ -689,7 +689,7 @@
int x2 = ((int) (%CPPSELF.x() * 100)) - (x1 * 100);
int y1 = (int) %CPPSELF.y();
int y2 = ((int) (%CPPSELF.y() * 100)) - (y1 * 100);
- %0 = PyString_FromFormat("Point(%d.%d, %d.%d)", x1, x2, y1, y2);
+ %PYARG_0 = PyString_FromFormat("Point(%d.%d, %d.%d)", x1, x2, y1, y2);
</inject-code>
</add-function>
<add-function signature="__repr__" return-type="PyObject*">
@@ -698,7 +698,7 @@
int x2 = ((int) (%CPPSELF.x() * 10)) - (x1 * 10);
int y1 = (int) %CPPSELF.y();
int y2 = ((int) (%CPPSELF.y() * 10)) - (y1 * 10);
- %0 = PyString_FromFormat("&lt;Point object at %p: (%d.%d, %d.%d)&gt;", %CPPSELF, x1, x2, y1, y2);
+ %PYARG_0 = PyString_FromFormat("&lt;Point object at %p: (%d.%d, %d.%d)&gt;", %CPPSELF, x1, x2, y1, y2);
</inject-code>
</add-function>
</value-type>
@@ -731,7 +731,7 @@
<value-type name="Str">
<add-function signature="__str__" return-type="PyObject*">
<inject-code class="target" position="beginning">
- %0 = PyString_FromString(%CPPSELF.cstring());
+ %PYARG_0 = PyString_FromString(%CPPSELF.cstring());
</inject-code>
</add-function>
<add-function signature="__len__" >
@@ -814,7 +814,7 @@
<value-type name="Echo">
<add-function signature="echo(const char *)" return-type="PyObject*">
<inject-code class="target" position="beginning">
- %0 = PyString_FromString(%1);
+ %PYARG_0 = PyString_FromString(%1);
</inject-code>
</add-function>
</value-type>