2012-11-14 11 views
6

sto avvolgendo una libreria C che contiene una struct:SWIG Python - avvolgendo una funzione che prevede un doppio puntatore ad una struct

struct SCIP 
{ 
//... 
} 

e una funzione che crea una tale struct:

void SCIPcreate(SCIP** s) 

SWIG genera una classe python SCIP e una funzione SCIPcreate(*args) da quella.

Quando provo a chiamare SCIPcreate() in python, ovviamente si aspetta un parametro di tipo SCIP**, come dovrei creare una cosa del genere?

Oppure dovrei provare ad estendere la classe SCIP con un costruttore che chiama automaticamente SCIPcreate()? Se è così, come potrei fare a riguardo?

risposta

5

Dato il file di intestazione:

struct SCIP {}; 

void SCIPcreate(struct SCIP **s) { 
    *s = malloc(sizeof **s); 
} 

Possiamo avvolgere questa funzione utilizzando:

%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" 

che è due typemaps, uno per creare un puntatore temporanea locale da utilizzare come ingresso per la funzione e un'altra per copiare il valore del puntatore dopo la chiamata nel ritorno.

In alternativa a questo si potrebbe anche usare %inline per impostare un sovraccarico:

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