首先,我想说我没有修改甚至查看 c 源代码的选项,因此任何涉及修改 c 文件的操作都无济于事。
在 VP.h 中:
typedef enum VPEvent {
...
EVENT_OBJECT_CLICK,
...
}
...
typedef void *VPInstance;
typedef void(*VPEventHandler)(VPInstance);
...
VPSDK_API VPInstance vp_create(void);
...
VPSDK_API int vp_event_set(VPInstance instance, VPEvent eventname, VPEventHandler event);
...
在 VP.pyx 中:
cdef extern from "VP.h":
...
cdef enum VPEvent:
...
VP_EVENT_OBJECT_CLICK,
...
...
ctypedef void *VPInstance
ctypedef void(*VPEventHandler)(VPInstance)
...
VPInstance vp_create()
...
int vp_event_set(VPInstance instance, VPEvent eventname, VPEventHandler event)
...
...
EVENT_OBJECT_CLICK = VP_EVENT_OBJECT_CLICK
...
cdef class create:
cdef VPInstance instance
def __init__(self):
self.instance = vp_create()
...
def event_set(self, eventname, event):
return vp_event_set(self.instance, eventname, event)
我想在 Python 中拥有什么:
import VP
...
def click(bot):
bot.say("Someone clicked something!")
...
bot = VP.create()
bot.event_set(VP.EVENT_OBJECT_CLICK, click)
这就是你在 c 中的做法:
#include <VP.h>
void click(VPInstance instance) {
vp_say(instance, "Someone clicked something!");
}
int main(int argc, char ** argv) {
...
VPInstance instance;
instance = vp_create();
...
vp_event_set(instance, VP_EVENT_OBJECT_CLICK, click)
}
但是问题是在编译 VP.pyx 时我得到
无法将 Python 对象转换为“VPEventHandler”
同样,默认情况下,回调被赋予一个 VPInstance 指针,但我想将此值抽象到一个类中。