I've been able to use the following typemaps when using a std::list in output or input of a function in C++. These typemaps allow me to use the list object as a standard list in Python.
However, I haven't been able to figure out what typemap to use for a std::list which is a public member of my C++ class.
MyWork.h
class MyWork
{
public:
// Functions
void myFunc1(std::list<MyClass> my_list); // OK
std::list<MyClass> myFunc2(); // OK
// Properties
std::list<MyClass> MyList; // ????
};
SWIG Typemaps
%typemap(out) std::list<MyClass>
{
PyObject* outList = PyList_New(0);
int error;
std::list<MyClass>::iterator it;
for ( it=$1.begin() ; it != $1.end(); it++ )
{
PyObject* pyMyClass = SWIG_NewPointerObj(new MyClass(*it), SWIGTYPE_p_MyClass, SWIG_POINTER_OWN );
error = PyList_Append(outList, pyMyClass);
Py_DECREF(pyMyClass);
if (error) SWIG_fail;
}
$result = outList;
}
%typemap(in) std::list<MyClass>
{
//$input is the PyObject
//$1 is the parameter
if (PyList_Check($input))
{
std::list<MyClass> listTemp;
for(int i = 0; i<PyList_Size($input); i++)
{
PyObject* pyListItem = PyList_GetItem($input, i);
MyClass* arg2 = (MyClass*) 0 ;
int res1 = 0;
void *argp1;
res1 = SWIG_ConvertPtr(pyListItem, &argp1, SWIGTYPE_p_MyClass, 0 | 0);
if (!SWIG_IsOK(res1))
{
PyErr_SetString(PyExc_TypeError,"List must only contain MyClassobjects");
return NULL;
}
if (!argp1)
{
PyErr_SetString(PyExc_TypeError,"Invalid null reference for object MyClass");
return NULL;
}
arg2 = reinterpret_cast< MyClass* >(argp1);
listTemp.push_back(*arg2);
}
$1 = listTemp;
}
else
{
PyErr_SetString(PyExc_TypeError,"Wrong argument type, list expected");
return NULL;
}
}