i'm trying to extend functionality of my lib written in C++ with Python and Cython. I have class MyClass in C++ wich is essential for my lib. And i'm using a lot it's wrapper class PyMyClass in Python. So i want to use functions (with PyMyClass as argument) from C++. How i can accomplish this?
I imagine it something like this:
cdef public my_func(MyClass class):
*cast MyClass to PyMyClass*
other_py_func(PyMyClass)
myclass.h
namespace classes {
class MyClass
{
public:
explicit MyClass(int x, int y);
int sum();
private:
int a;
int b;
};
}
myclass.cpp
MyClass::MyClass(int x, int y)
{
a = x;
b = y;
}
MyClass::sum()
{
return a + b
}
pymyclass.pyx
cdef extern from "myclass.h" namespace "classes":
cdef cppclass MyClass:
MyClass(int x, int y) except +
int sum()
cdef public my_func(MyClass var):
print "It is python"
cdef class PyMyClass(object): cdef MyClass *c_class
def __cinit__(self, int x, int y):
self.c_class = new MyClass(x,y)
def __dealoc__(self):
del self.c_class.sum()
def sum(self):
return self.c_class.sum()
I understand that i can add get_x(), get_y() and set_x(int), set_y(int) to MyClass and just copy all the fields from MyClass to PyMyClass. But PyMyClass has already a pointer to MyClass. Is it possible just assign address of MyClass instance to PyMyClass field? I mean something like this:
cdef class PyMyClass(object):
cdef MyClass *c_class
def __cinit__(self, int x, int y):
self.c_class = MyClass(x,y)
def __cinit__(self, void * ptr):
self.c_class = (Antenna *)ptr
def __dealoc__(self):
del self.c_class.sum()
def sum(self):
return self.c_class.sum()
Then Cython function will look like cdef public my_func(MyClass *class): pyclass = PyMyClass(class) other_py_func(pyclass)