You’ll need custom typemaps for char**& and int**& (if int**& can mean different things in different functions then it’s a bit more challenging, let us know if you need help with that.)

 

Unfortunately I don’t know about SWIG-to-Python, as I use C#/Java, but here are a couple of tips:

-          If you want to run some custom C code after calling the wrapped function, use the argout typemap. For example, the typemap

%typemap(argout) int& NEG   %{ $input = -$input; %}

is a typemap that applies to an int& argument named NEG. It negates the integer after calling the wrapped function.

-          It may be helpful to map “int**&” to a custom Python type. You’ll need several typemaps to accomplish that, but I couldn’t tell you what to do since I only know how to use the C#/Java typemaps. An easier approach is to write a bunch of global C functions to manipulate “int**&” values (e.g. to extract data, free the data, etc.) but it would be clunkier to work with on the Python side.

 

(PS William, sorry for top-posting but it seems completely impossible to bottom-post in MS Outlook)

 

From: CFK [mailto:cfkaran2@gmail.com]
Sent: Tuesday, October 09, 2012 1:04 PM
To: swig-user@lists.sourceforge.net
Subject: [Swig-user] How to wrap C++ method int foo(char**& inout1, int**& inout2) for Python???

 

I'm very new to SWIG, so if this answer is obvious, please forgive me. 

I'm trying to use SWIG to wrap a class that is similar to the following (I'm under NDA, and can't show the actual listing, so this is here to protect the guilty).

namespace A
{
    class B: public C
    {
        public:
            // constructors, destructors, etc.

            // My current headache
            int foo(char**& inout1, int**& inout2);
    };
}

There are a number of methods in this class that are similar to this, some of which are quite a bit more involved.  Each pointer is to a variable length array whose last element is NULL to indicate that there are no more pointers left.  The char array is just an array of NULL terminated C-strings.  Each array in the int array is length-prefixed; that is, the first element of the array is the number of elements in the rest of the array.  The problem is that this is both an input and an output; data is calloc()ed and passed in, then the method pulls out the pieces of interest, and operates on it.  It then decides if it is going to realloc() the array of pointers to make it larger, or calloc() a new array and copy over the pointers of interest from the old array.  It may also decide to mess with the C-strings/integer arrays, free()ing them, modifying them, etc.  Once the method returns, the caller owns all of the allocated memory; normal usage is to do what you want with it, and then either reuse what you need, or go through a deallocation loop that free()s everything.

My problem is that I have to find a way of wrapping this so that Python can use it sanely, and I'm not allowed to modify the header.  I looked at http://www.swig.org/Doc2.0/SWIGDocumentation.html#Library_nn11 for %newobject, but since that works on the return value, I wasn't sure if it is appropriate for this.  I also looked at the example at http://www.swig.org/Doc2.0/Python.html#Python_nn59 but I'm not sure if or how that applies here.

Any help would be appreciated!  I'm at my wits end, and like everyone, I'm on a tight deadline.

Thanks,
Cem Karan