Python SWIG:将C ++返回参数转换为返回值,并将原始C ++类型转换为Python类型 [英] Python SWIG: convert C++ return parameter to return value, and convert raw C++ type to Python type

查看:203
本文介绍了Python SWIG:将C ++返回参数转换为返回值,并将原始C ++类型转换为Python类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为C ++库修改现有的SWIG Python接口,以添加Python包装器以实现更多功能,我将非常感谢SWIG经验丰富的人员提供的帮助。

I'm trying to modify an existing SWIG Python interface for a C++ library, to add Python wrappers for more functions, and I would greatly appreciate some help from someone experienced with SWIG.

具体来说,我正在使用一个具有以下签名的函数:

Specifically I'm working on a function with a signature like this:

void execute(int x, double y, ResultType& result1, ResultType& result2);

此函数接受两个空的ResultType对象,并将其填充为输出参数。在Python中,这必须转换为仅包含 x y 的函数,然后返回<$的元组c $ c> result1 和 result2

This function accepts two empty ResultType objects and fills them in as a output parameters. In Python, this has to translate to a function that takes only x and y, then returns a tuple of result1 and result2.

ResultType是一个容器类型,它是

ResultType is a container type that is used widely throughout the library.

从研究中,我认为我需要为result1和result2添加一个类型映射 in,这将吞噬参数并将其保存到临时变量中。我还发现该引用由SWIG转换为指针,因此& temp 而不是 temp 。这是我的类型映射输入:

From research, I think I understand that I need to add a typemap "in" for result1 and result2, which swallows the arguments and saves them to temporary variables. I also found that the reference is converted to a pointer by SWIG, hence &temp instead of temp. Here is my typemap "in":

typemap(in, numinputs=0) ResultType& result1 (ResultType temp) {
    $1 = &temp;
}

typemap(in, numinputs=0) ResultType& result2 (ResultType temp) {
    $1 = &temp;
}



typemap(argout)



接下来,我添加了一个类型映射 argout,将值附加到返回元组:

typemap(argout)

Next, I added a typemap "argout" that appends the values to a return tuple:

%typemap(argout) ResultType& result1 {
    $result = SWIG_Python_AppendOutput($result, temp$argnum);
}

%typemap(argout) ResultType& result2 {
    $result = SWIG_Python_AppendOutput($result, temp$argnum);
}

但是,这显然行不通,因为 temp $ argnum 的原始C ++类型为 ResultType ,而我需要有一个 PyObject * 以便附加到元组。 ResultType已经具有可用的SWIG包装器。因此,在Python中,我可以调用 ResultType()来构造它的实例,而不会出现问题。假设到目前为止我处在正确的轨道上,如何将原始C ++ ResultType 对象转换为 PyObject * 属于SWIG生成的 ResultType 包装器? (对不起,如果有太多详细信息,我正在尝试避免出现 XY问题)

However, this obviously won't work, because temp$argnum will be of the raw C++ type ResultType, whereas I need to have a PyObject * in order to append to a tuple. ResultType already has a working SWIG wrapper. So, in Python I can call ResultType() to construct an instance of it without a problem. Assuming that I am on the right track so far, how do I convert the raw C++ ResultType object to a PyObject * belonging to the SWIG-generated wrapper for ResultType? (Sorry if too much detail, I'm trying to avoid the "XY Problem")

推荐答案

就像$ 1是参考对于输入类型映射中的Python输入对象,$ 1是对argout类型映射中C ++输出变量的引用。使用此功能,您可以为该数据生成一个Python对象,并将其附加到结果中。

Just like $1 is a reference to the Python input object in the input typemap, $1 is a reference to the C++ output variable in the argout typemap. Using this, you can generate a Python object for that data and append it to the result.

以下是Windows的一个功能示例:

Here's a functional example for Windows:

test.h

#ifdef EXPORT
#define API __declspec(dllexport)
#else
#define API __declspec(dllimport)
#endif

struct ResultType
{
    int x;
    double y;
};

API void execute(int x, double y, ResultType& result1, ResultType& result2);

test.cpp

#define EXPORT
#include "test.h"

API void execute(int x, double y, ResultType& result1, ResultType& result2)
{
    result1.x = 2 * x;
    result1.y = 2 * y;
    result2.x = 3 * x;
    result2.y = 3 * y;
}

test.i

%module test

%{
#include "test.h"
%}

%include <windows.i>

%typemap(in,numinputs=0) ResultType& %{
    // Create a persistent object to hold the result;
    $1 = new ResultType;
%}

%typemap(argout) ResultType& (PyObject* tmp) %{
    // Store the persistent object in a PyObject* that will be destroyed
    // when it goes out of scope.
    tmp = SWIG_NewPointerObj($1, $1_descriptor, SWIG_POINTER_OWN);
    $result = SWIG_Python_AppendOutput($result, tmp);
%}

%include "test.h"

输出

>>> import test
>>> r = test.execute(2,3)
>>> r[0].x
4
>>> r[0].y
6.0
>>> r[1].x
6
>>> r[1].y
9.0

这篇关于Python SWIG:将C ++返回参数转换为返回值,并将原始C ++类型转换为Python类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆