Using SWIG, dereference a C array pointer to a TCL variable

423 views Asked by At

The scenario. I use a software (A) that allow me to use a TCL script (B). Since I have to do many operations In B, I build a C code (C) to use via SWIG and do the math. Now, I'm running A in parallel, and I'm able to use some built-in TCL-functions of A to send and receive some variable of B via MPI. The problem is that one of this variables is really a C array.

I know that SWIG interchanges only the memory address between the C and TCL, but to send this to another computers is useless. So I think that I have to dereference the array into a TCL variable.

Could someone give me an idea of how to do this?

PS: I don't know too much about SWIG.

1

There are 1 answers

0
prussian_metal On BEST ANSWER

typemaps are the way to go here. What they essentially allow you to do is to provide SWIG with code snippets that are added to the top and bottom of your wrappers. In other words, you can control how you parse arguments and how you clean up once your operation is complete.

From your explanation it is hard for me to tell what exactly it is you are trying to achieve. Maybe some pseudo-code would make things clearer. I am a user of SWIG with Python and know nothing about TCL, so I will try to explain how I'd do this with my setup.

Lets say this is the function being wrapped:

double[] foo_c (double bar[], int a_size);

You want the return value of the function to be "dereferenced". So you want the function to return not-a-pointer. In python, lets say I want the function to return a Python list.

typemap(out) double[] foo_c //signature of function whose behavior is to be modified
{
    $result = PyList_New (a_size); //$result is what SWIG will return
    for (int i=0; i<a_size; ++i) {
        PyObject *o = PyFloat_FromDouble ($1[i]); 
        //$1 denotes return variable before SWIG wrapping goodness 
        PyList_SetItem ($result,j,o);
    }
}

As you can see, most of the code is really in C/C++ so it doesn't matter that I took a Python example. You just need to have a look at the TCL/C API to figure out which functionality you will be needing for your purposes.

Hope this helps!