我能够从原生到Cython获得正常的回调。
但是,我如何设置成员函数在cython中进行回调。
我的sampleCallback.h
文件是:
namespace mango {
class sampleCalls {
public:
typedef void (*Callback)(char *name);
sampleCalls(Callback method, char *name);
void execute();
private:
Callback _method;
char *_key;
}; }
我的sampleCallback.cpp
文件包含简单的实现:
namespace mango {
sampleCalls::sampleCalls(Callback method, char *name) {
_method = method;
_key = name;
};
void sampleCalls::execute()
{
return _method(_key);
};
}
现在我写了一个非常简单的cython包装器callback.pyx
:
ctypedef void (*Callback)(char *name)
cdef extern from "sampleCallback.h" namespace "mango" :
cdef cppclass sampleCalls:
sampleCalls(Callback method, char *name)
void execute()
cdef void callbackCY(char* name):
print "I AM ", name
cdef class pyCallback:
cdef sampleCalls* sc
def __init__(self):
self.sc = new sampleCalls(callbackCY, "BATMAN")
def execute(self):
self.sc.execute()
当我运行一个简单的python代码时,这构建并正常工作:
import callback
cb = callback.pyCallback()
cb.execute()
这很好用,输出:: 我是蝙蝠侠
但这不是我想要的。 我希望回调函数成为cython定义类的一部分, 所以我实现了这样的类:
cdef class pyCallback:
cdef sampleCalls* sc
def __init__(self):
self.sc = new sampleCalls(cb, "BATMAN")
def execute(self):
self.sc.execute()
cdef void cb(self, char* name):
print name, " WINS"
这当然不起作用,因为回调原型不匹配。
什么是正确的方式?
我也尝试过std :: bind这样的东西::
cdef place1 "std::placeholder1"
cdef bind_py "std::bind"
cdef class pyCallback:
cdef sampleCalls* sc
def __init__(self):
self.sc = new sampleCalls(bind_py(self.cb, self, place1), "BATMAN")
这也不起作用。 错误::
Error compiling Cython file:
------------------------------------------------------------
...
cdef class pyCallback:
cdef sampleCalls* sc
def __init__(self):
self.sc = new sampleCalls(bind_py(self.cb, self, place1), "BATMAN")
^
------------------------------------------------------------
callback.pyx:19:40: Cannot convert 'void (pyCallback, char *)' to Python object
Error compiling Cython file:
------------------------------------------------------------
...
cdef class pyCallback:
cdef sampleCalls* sc
def __init__(self):
self.sc = new sampleCalls(bind_py(self.cb, self, place1), "BATMAN")
^
------------------------------------------------------------
callback.pyx:19:35: Cannot convert Python object to 'Callback'
我怎样才能完成这项工作?请帮忙。