class 类
class XEventWarp:public XEvent { public: PyObject *self; XEventWarp(PyObject* self_):self(self_){ Py_INCREF(self); } XEventWarp( PyObject* self_, const XEvent& copy ) : XEvent(copy), self(self_) { Py_INCREF(self); } ~XEventWarp() {Py_DECREF(self); } }; BOOST_PYTHON_MODULE(_core) { class_<XEvent, auto_ptr<XEventWarp> >("XEvent") .def("Skip",&XEvent::Skip) .def("Stop",&XEvent::Stop) .def("__cmp__",&XEvent::Cmp) .add_property("wKeyState",&XEvent::GetWKeyState) .add_property("cpt",&XEvent::GetClientPos) .add_property("spt",&XEvent::GetScreenPos) .add_property("hWnd",&XEvent::GetHandler) .add_property("tick",&XEvent::GetTime) .add_property("lParam",&XEvent::GetLParam) .add_property("wParam",&XEvent::GetWParam) ; implicitly_convertible<auto_ptr<XEventWarp>, auto_ptr<XEvent>>(); }
由于是虚函数 所以需要写一个包装类
class HWndObjectWapper:public HWndObject,public wrapper<HWndObject> { public: HWndObjectWapper(){} HWndObjectWapper(std::string className,std::string sbClassName) :HWndObject(className,sbClassName){} VOID OnEvent(XEvent* evt) { if(override function=this->get_override("OnEvent")) { //object obj(evt); //function(obj); reference_existing_object::apply<XEvent*>::type converter; PyObject* obj = converter(evt); object real_obj = object( handle<>( obj ) ); function(real_obj); } else { this->HWndObject::OnEvent(evt); } }; VOID OnFinalMessage() { if(override function=this->get_override("OnFinalMessage")) { function(); } else { this->HWndObject::OnFinalMessage(); } } VOID DefaultOnFinalMessage(){ HWndObject::OnFinalMessage();}; VOID DefaultOnEvent(XEvent* evt){ HWndObject::OnEvent(evt);}; };其中有两种调用方式,如果需要 传递到py里面的object和C++ object绑定
则
reference_existing_object::apply<XEvent*>::type converter;
PyObject* obj = converter(evt);
object real_obj = object( handle<>( obj ) );
function(real_obj);
如果 不需要 关联 则
object obj(evt);
function(obj);
直接调用即可