[英]SWIG struct pointer as output parameter
I have a struct: 我有一个结构:
struct some_struct_s {
int arg1;
int arg2;
};
I have a C function: 我有一个C函数:
int func(some_struct_s *output);
Both are %included
into my SWIG file. 两者都
%included
在我的SWIG文件中。
I want some_struct_s *output
to be treated like an output parameter. 我希望将
some_struct_s *output
视为输出参数。 Python example: Python示例:
int_val, some_struct_output = func()
"Output parameters" is covered in the manual for POD-types (sec 10.1.3), but not for non-POD types. 手册中介绍了POD类型(第10.1.3节)的“输出参数”,但非POD类型则没有。
How do I tell SWIG I want some_struct_s *output
to be an output parameter? 如何告诉SWIG我希望
some_struct_s *output
为输出参数?
From the documentation : 从文档中 :
11.5.7 "argout" typemap
11.5.7“ argout”类型图
The "argout" typemap is used to return values from arguments.
“ argout”类型映射用于从参数返回值。 This is most commonly used to write wrappers for C/C++ functions that need to return multiple values.
这最常用于为需要返回多个值的C / C ++函数编写包装器。 The "argout" typemap is almost always combined with an "in" typemap---possibly to ignore the input value....
几乎总是将“ argout”类型映射与“ in”类型映射---可能会忽略输入值。
Here's a complete example for your code (no error checking for brevity): 这是您的代码的完整示例(为简洁起见,没有错误检查):
%module test
// Declare an input typemap that suppresses requiring any input and
// declare a temporary stack variable to hold the return data.
%typemap(in,numinputs=0) some_struct_s* (some_struct_s tmp) %{
$1 = &tmp;
%}
// Declare an output argument typemap. In this case, we'll use
// a tuple to hold the structure data (no error checking).
%typemap(argout) some_struct_s* (PyObject* o) %{
o = PyTuple_New(2);
PyTuple_SET_ITEM(o,0,PyLong_FromLong($1->arg1));
PyTuple_SET_ITEM(o,1,PyLong_FromLong($1->arg2));
$result = SWIG_Python_AppendOutput($result,o);
%}
// Instead of a header file, we'll just declare this code inline.
// This includes the code in the wrapper, as well as telling SWIG
// to create wrappers in the target language.
%inline %{
struct some_struct_s {
int arg1;
int arg2;
};
int func(some_struct_s *output)
{
output->arg1 = 1;
output->arg2 = 2;
return 0;
}
%}
Demo below. 下面的演示。 Note that the
int
return value of zero as well as the output parameter as a tuple are returned as a list. 请注意,
int
返回值零以及输出参数作为元组都作为列表返回。
>>> import test
>>> test.func()
[0, (1, 2)]
If you don't want typemaps, you can also inject code to create the object and return it to hide it from the user: 如果您不想使用类型映射,还可以注入代码来创建对象,然后将其返回以对用户隐藏:
%module test
%rename(_func) func; // Give the wrapper a different name
%inline %{
struct some_struct_s {
int arg1;
int arg2;
};
int func(struct some_struct_s *output)
{
output->arg1 = 1;
output->arg2 = 2;
return 0;
}
%}
// Declare your interface
%pythoncode %{
def func():
s = some_struct_s()
r = _func(s)
return r,s
%}
Demo: 演示:
>>> import test
>>> r,s=test.func()
>>> r
0
>>> s
<test.some_struct_s; proxy of <Swig Object of type 'some_struct_s *' at 0x000001511D70A880> >
>>> s.arg1
1
>>> s.arg2
2
You can make the typemap language agnostic if you carefully select SWIG macros: 如果仔细选择SWIG宏,则可以使类型映射语言不可知:
%module test
%typemap(in,numinputs=0) struct some_struct_s *output %{
$1 = malloc(sizeof(struct some_struct_s));
%}
%typemap(argout) struct some_struct_s* output {
%append_output(SWIG_NewPointerObj($1,$1_descriptor,1));
}
%inline %{
struct some_struct_s {
int arg1;
int arg2;
};
int func(struct some_struct_s *output)
{
output->arg1 = 1;
output->arg2 = 2;
return 0;
}
%}
Demo: 演示:
>>> import test
>>> r,s=test.func()
>>> r
0
>>> s
<test.some_struct_s; proxy of <Swig Object of type 'some_struct_s *' at 0x000001DD0425A700> >
>>> s.arg1
1
>>> s.arg2
2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.