I'm having a lot of troubles trying to wrap a c++ method that returns a constant reference to a vector of pairs to a Python list of tuples using %typemap(out)
.
I currently have something like this:
myclass.h:
#inlcude <vector>
using std::vector;
class MyClass {
private:
const vector<pair<int,int>> & _myvector;
public:
MyClass(const vector<pair<int,int>> & myvector );
const vector<pair<int,int>> & GetMyVector() const;
}
myclass.cpp:
#include "myclass.h"
MyClass::MyClass(const vector<pair<int,int>> & myvector): _myvector(myvector){};
const vector<pair<int,int>> & MyClass::GetMyVector() const {
return _myvector;
};
myclass.i: (checking ommited)
%module MyClass
%include "std_vector.i"
namespace std {
%template(vector_pair_int) vector<pair<int,int>>;
}
%typemap(in) (const vector_pair_int &){
$1 = new vector<pair<int,int>>;
int size = PyList_Size($input);
for (int i=0; i<size; i++){
PyObject *o = PyList_GetItem($input,i);
PyObject *o1 = PyList_GetItem(o,0);
PyObject *o2 = PyList_GetItem(o,1);
$1->push_back(make_pair(PyInt_AsLong(o1),PyInt_AsLong(o2)))
}
}
%typemap(out) (const vector_pair_int &) {
$result = PyList_New($1.size());
vector<pair<int,int>>:: const_iterator it;
int count=0;
for (it= $1.begin(); it!= $1.end(); ++it){
PyObject * tup = PyTuple_Pack(2, it->first,it->second);
PyList_SET_ITEM($result,count,tup);
count++;
}
}
Ok so the first thing that I dont quite understand is that the previous code for the typemap (out) doesnt compile because it tells me that $1 is a pointer to the container not a reference. When I change the use of $1 as a pointer, it compiles but it doesnt work.
Second, in the case that compiles, the typemap(in) works (the c++ container is correctly filled), but when I try to retrieve the container from python I get garbage. When I pass to the constructor something like MyClass([(1,2)]), and then I use GetMyVector() it returns a python list but of size 3 and with garbage on the tuples... what I'm doing wrong???