Hi all..

 

I hope you guys can help me out, cause I think I’m loosing my mind…

I’m tryint to import one of the simplest C++ function calls into python, using swig…

 

Ok.. the software library is big and mean, and I hardly know anything about it yet.. but here is my problem..

 

In the C++ library has a simple function:

ERRORCODE       GetModelState(const char *model, int &depthunit, BOOL & have_weight);

 

Where the const char * is just a filename.

 

Now I’m trying to make this function available in python… In the pre-existing software is a large .iprep file that has lots of type maps etc… but it also references/includes list.i

%{

# include <invutils/List.h>

%}

 

ERRORCODE       GetUniqueLayerNames(const CString &inputmodel, CString1D *OUTPUT);

ERRORCODE       GetDataLabel(CString1D * OUTPUT, const char *dirname, int depthunit);

ERRORCODE       GetLogtypeFromWell(Int1D * OUTPUT, const CString1D * welllabel, int depthunit);

ERRORCODE       GetLogtypeFromComponent(Int1D * OUTPUT, const char *dirname);

 

The functions in here are in the same source file as my GetModelState.. So I tried just adding

 

ERRORCODE       GetModelState(const char *model, int &depthunit, BOOL &have_weight);

 

In python I’ve imported the proper module (jason) and the function is now know within python.

 

The python call

 

Er=GetModelState(filename,depthunitOUT,have_weightOUT)

 

Results in an error that depthunitOUT and have_weightOUT are not “globally defined”

 

So I assume it needs some predefined variables..

The following call:

 

depthunitOUT = 0

have_weigth = False

Er=GetModelState(filename,depthunitOUT,have_weightOUT)

 

Gives the error

TypeError: Expected a pointer

 

Ok.. 1 day later.. reading the swig documentation I think I have to replace the variable names with the a keyword telling SWIG that these are output variables:

 

List.i becomes:

ERRORCODE       GetModelState(const char *model, int &OUTPUT, BOOL &OUTPUT);

 

Recompile & Execute:

 

TypeError: GetModelState() takes exactly 1 argument (3 given)

 

HUH?

 

I think maybe swig is smarter then me and specifying a function argument as OUTPUT is enough for it to work with reference arguments…

So List.i becomes:

ERRORCODE       GetModelState(const char *model, int OUTPUT, BOOL OUTPUT);

 

Recompile & Execute:

 

HA! No error message!.. but neither does it pass out the proper values.. (I’ve checked with gdb and the C++GetModelState IS correctly executed and the correct values ARE retreived, but they are not passed back to python)

 

So now I’m at a loss..

I reread the documentation and think maybe I need to create some typemaps..

 

Writing typemaps like:

%typemap(in, numinputs=0) int &depthunit (int temp) {

  $1 = &temp;

}

%typemap(argout, fragment="t_output_helper") int &depthunit {

  $result = t_output_helper($result, PyInt_FromLong((long)($1)));

}

 

%typemap(in, numinputs=0) BOOL &have_weight (BOOL temp) {

  $1 = &temp;

}

 

%typemap(argout, fragment="t_output_helper") BOOL &have_weight {

  $result = t_output_helper($result, PyInt_FromLong((long)($1)));

}

 

And all permutations of int & depthunit – int & OUTPUT – int OUTPUT

All result in the corresponding behaviour as described above…

 

Well the only positive result from all this, is that I’m now familiar with the way typemaps affect the SWIG generated .C files.. but how to use reference variables in function calls is still unknown to me..

 

 

Any and every help is much appreciated

 

Bjorn