From: Maciej D. <mac...@gm...> - 2008-06-26 14:20:59
|
2008/6/26 EntonH <en...@gm...>: > > hi there, > > I am trying to pass a list like ["Data1","Data2","Data3",0] from Python to > C++. > How do i have to describe the function in the interface file (*.i) when i > want to pass this list as an array to the C++ source? > > Greetings, > EntonH > -- You can pass it as a vector of strings: // example.h #include <string> #include <vector> void foo(std::vector<std::string> v); // example.i: %{ #include "example.h" %} %include stl.i %template(StringVector) std::vector<std::string>; %include "example.h" Or as a C strings array: // example.h void foo(char** list); // example.i %{ #include "example.h" %} %typemap(in) char** list { Py_ssize_t list_len = PyList_Size($input); Py_ssize_t i; $1 = (char**) malloc((list_len + 1) * sizeof(char*)); for (i = 0; i < list_len; ++i) { PyObject* obj = PyList_GetItem($input, i); $1[i] = PyString_AsString(obj); } $1[list_len] = 0; } %typemap(freearg) char** list { if ($1) free($1); } extern void foo(char** list); -- Maciek |