문제

I have a c++ class written and I am using SWIG to make a Python version of my class. I would like to overload the constructor so that it can take in Python lists. For example:

>>> import example
>>> a = example.Array([1,2,3,4])

I was attempting to use the typemap feature in swig, but the scope of typemap does not include code in extend

Here is a similar example to what I have...

%typemap(in) double[]
{
    if (!PyList_Check($input))
        return NULL;
    int size = PyList_Size($input);
    int i = 0;
    $1 = (double *) malloc((size+1)*sizeof(double));

    for (i = 0; i < size; i++)
    {
            PyObject *o = PyList_GetItem($input,i);
            if (PyNumber_Check(o))
                    $1[i] = PyFloat_AsDouble(o);
            else
            {
                    PyErr_SetString(PyExc_TypeError,"list must contain numbers");
                    free($1);
                    return NULL;
            }
    }

    $1[i] = 0;
} 

%include "Array.h"    

%extend Array 
{
   Array(double lst[])
   {
        Array *a = new Array();

        ...
        /* do stuff with lst[] */
        ...

        return a;
   }
 }

I know the typemap is working correctly (I wrote a small test function that just prints out elements in the double[]).

I attempted putting the typemap inside the extend clause, but that did not solve the problem.

Maybe there is another way to use Python Lists inside of the extend, but I could not find any examples.

Thanks for the help in advance.

도움이 되었습니까?

해결책

You're really close: instead of a double lst[], extend with std::list<double>:

%include "std_list.i" // or std_vector.i

%include "Array.h"    

%extend Array 
{
   Array(const std::list<double>& numbers) {
        Array* arr = new Array;
        ...put numbers list items in "arr", then
        return a; // interpreter will take ownership
   }
}

SWIG should automatically convert the Python list to the std::list.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top