2012-11-14 43 views
6

我包裹包含一個struct C庫函數:SWIG的Python - 包裝期望一個雙指針指向一個struct

struct SCIP 
{ 
//... 
} 

,並創建這樣一個結構的函數:

void SCIPcreate(SCIP** s) 

SWIG生成一個python類SCIP和一個函數SCIPcreate(*args)

當我現在嘗試在Python中調用SCIPcreate()時,顯然需要一個SCIP**類型的參數,我該如何創建這樣的事情?

或者我應該嘗試使用自動調用SCIPcreate()的構造函數來擴展SCIP類?如果是這樣,我會怎麼做呢?

回答

5

鑑於頭文件:

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" 

這是兩個typemaps,一個創建本地,暫時指針將被用作輸入到該函數和另一個將指針後的指針值複製到返回值中。

作爲替代這個你也可以使用%inline來設置過載:

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