I'm trying to get list from C++ code in my Python code using Cython.
Here my header (ex.h):
namespace c
{
class C {
int id;
public:
C(int id) {
this->id = id;
}
int getId();
};
typedef std::vector<C> CList;
}
and my source file (ex.cpp):
namespace c
{
int C::getId() {
return this->id;
}
CList getlist(int t) {
CList list;
for (int i = 0; i < t; ++i) {
C c(i);
list.push_back(c);
}
return list;
}
}
So in my Python code I want to iterate through CList like this:
import exapi
for i in exapi.take_list():
print i
# OR
print i.get_id()
In .pyx file (exapi.pyx) I'm trying to create Cython extension to handle C++ object:
cdef extern from "ex.h" namespace "c":
cdef cppclass C:
C(int) except +
int getId()
cdef cppclass CList:
pass
cdef CList getlist(int)
cdef class PY_C:
pass
cdef class PY_CList:
pass
def take_list():
return getlist(10) # I know this is wrong
What forwarding methods and function should I add to PY_C and PY_Clist to make my python code working?
And I read Using C++ in Cython, but there new objects creates in Cython code, but in my case I need to use objects created in C++ side.