Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWIG Python—包装一个需要指向结构的双指针的函数_Python_Pointers_Swig - Fatal编程技术网

SWIG Python—包装一个需要指向结构的双指针的函数

SWIG Python—包装一个需要指向结构的双指针的函数,python,pointers,swig,Python,Pointers,Swig,我正在包装一个包含结构的C库: struct SCIP { //... } void SCIPcreate(SCIP** s) 以及创建这样一个结构的函数: struct SCIP { //... } void SCIPcreate(SCIP** s) SWIG生成一个python类SCIP和一个函数SCIPcreate(*args) 当我现在尝试在python中调用SCIPcreate()时,它显然需要一个类型为SCIP**的参数,我该如何创建这样的东西呢 或者我应该尝试使用自动调用S

我正在包装一个包含结构的C库:

struct SCIP
{
//...
}
void SCIPcreate(SCIP** s)
以及创建这样一个结构的函数:

struct SCIP
{
//...
}
void SCIPcreate(SCIP** s)
SWIG生成一个python类
SCIP
和一个函数
SCIPcreate(*args)

当我现在尝试在python中调用
SCIPcreate()
时,它显然需要一个类型为
SCIP**
的参数,我该如何创建这样的东西呢

或者我应该尝试使用自动调用
SCIPcreate()
的构造函数扩展
SCIP
类吗?如果是这样的话,我该怎么做呢?

给定头文件:

struct SCIP {};

void SCIPcreate(struct SCIP **s) {
  *s = malloc(sizeof **s);
}
我们可以使用以下方法包装此函数:

%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"
这是两个类型映射,一个用于创建本地临时指针,用作函数的输入,另一个用于在调用后将指针的值复制到返回中

除此之外,您还可以使用
%inline
设置重载:

%newobject SCIPcreate;
%inline %{
  struct SCIP *SCIPcreate() {
    struct SICP *temp;
    SCIPcreate(&temp);
    return temp;
  }
%}