Pet*_*erE 6 python swig pointers
我正在包装一个包含结构的C库:
struct SCIP
{
//...
}
Run Code Online (Sandbox Code Playgroud)
以及创建这样一个结构的函数:
void SCIPcreate(SCIP** s)
Run Code Online (Sandbox Code Playgroud)
SWIG生成一个python类SCIP
和一个函数SCIPcreate(*args)
.
当我现在尝试调用SCIPcreate()
python时,它显然需要一个类型的参数,SCIP**
我应该如何创建这样的东西?
或者我应该尝试SCIP
使用SCIPcreate()
自动调用的构造函数扩展类?如果是这样,我该怎么办呢?
鉴于头文件:
struct SCIP {};
void SCIPcreate(struct SCIP **s) {
*s = malloc(sizeof **s);
}
Run Code Online (Sandbox Code Playgroud)
我们可以使用以下方法包装此函
%module test
%{
#include "test.h"
%}
%typemap(in,numinputs=0) struct SCIP **s (struct SCIP *temp) {
$1 = &temp;
}
%typemap(argout) struct SCIP **s {
%set_output(SWIG_NewPointerObj(SWIG_as_voidptr(*$1), $*1_descriptor, SWIG_POINTER_OWN));
}
%include "test.h"
Run Code Online (Sandbox Code Playgroud)
这是两个类型映射,一个用于创建本地临时指针,用作函数的输入,另一个用于在调用返回后复制指针的值.
作为替代方案,您还可以使用%inline
设置重载:
%newobject SCIPcreate;
%inline %{
struct SCIP *SCIPcreate() {
struct SICP *temp;
SCIPcreate(&temp);
return temp;
}
%}
Run Code Online (Sandbox Code Playgroud)