I'm using cython to wrap my C++ class:
Foo.h
class Foo
{
private:
std::map<int,int > myMap;
std::vector<int> myVector;
public:
// An example of internal structures initialization
Foo()
{
for (int i=0; i<10; i++)
{
myVector.push_back(i);
myMap[i]=i*i;
}
}
std::map<int,int> getMyMap()
{
return myMap;
}
std::vector<int> getMyVector()
{
return myVector;
}
}
I'm wondering if there is some way to get the std::map as python dict and the std::vector as python list without explicitly creating (and thus wasting memory) copies of such structures.
A temptative implementation for the std::vector is the following:
cdef extern from "Foo.h":
cdef cppclass Foo:
Foo()
vector[int] getMyVector()
cdef class pyFoo:
cdef Foo *thisptr # hold a C++ instance which we're wrapping
def __cinit__(self):
self.thisptr = new Foo()
def getMyVector(self):
cdef vector[int] aa
cdef int N
b= []
aa = self.thisptr.getMyVector()
N=aa.size()
for i in range(N):
b.append(aa[i])
return b;
But this clearly must store two structures containing the same data. I'm wondering if there is a way to map from c++ to list with cython or should I use boost::python?