SWIG and Automated C/C++ Scripting Extensions

By David Beazley

Dr. Dobb's Journal February 1998

// Make 'double *vector' accept a list of values
%typemap(python,in) double *vector {
    /* Convert a Python List into an array of values */
    int i,sz;
    sz = PyList_Size($source);
    $target = (double *) malloc(sz*sizeof(double));
    for (i = 0; i < sz; i++) {
         $target[i] = (double) 
            PyFloat_AsDouble(PyList_GetItem($source,i));
    }
}
%typemap(python,freearg) double *vector {
    free($source);  /* clean up to avoid memory leaks */
}
void foo(double *vector);

Example 10: Sample SWIG typemap specification.

Back to Article


Copyright © 1998, Dr. Dobb's Journal