#include "Callback.h" using namespace boost::python; NAMESPACE_UPP //Callback struct CallbackActionPy : public CallbackAction { object o; virtual void Execute() { call(o.ptr()); } CallbackActionPy(PyObject *callable) : o(handle<>(borrowed(callable))) {} }; struct Callback_to_python //FIXME translation of Callback internals to python { static PyObject* convert(const Callback& s) { return incref(object(s).ptr()); } }; struct Callback_from_python { Callback_from_python() { converter::registry::push_back(&convertible, &construct, type_id()); } static void* convertible(PyObject* po) { if(po == Py_None) return po; if(extract(po).check()) return po; if(PyFunction_Check(po)) return po; if(PyCallable_Check(po) && PyMethod_Self(po)) return po; return 0; } static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) { void* d = ((converter::rvalue_from_python_storage*)data)->storage.bytes; if(po == Py_None) { new(d) Callback(); } #if 1 //actually isnt used anywhere so far else if(extract(po).check()) { Callback& c = extract(po); new(d) Callback(c); } #endif else if(PyFunction_Check(po)) { new(d) Callback(new CallbackActionPy(po)); } else if(PyCallable_Check(po) && PyMethod_Self(po)) { new(d) Callback(new CallbackActionPy(po)); } else { throw_error_already_set(); } //FIXME dont know type, which to throw? data->convertible = d; } }; //Gate struct GateActionPy : public GateAction { object o; virtual bool Execute() { return call(o.ptr()); } GateActionPy(PyObject *callable) : o(handle<>(borrowed(callable))) {} }; struct Gate_from_python { Gate_from_python() { converter::registry::push_back(&convertible, &construct, type_id()); } static void* convertible(PyObject* po) { if(po == Py_None) return po; if(PyFunction_Check(po)) return po; if(PyCallable_Check(po) && PyMethod_Self(po)) return po; if(extract(po).check()) return po; return 0; } static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) { void* d = ((converter::rvalue_from_python_storage*)data)->storage.bytes; if(po == Py_None) { new(d) Gate(); } else if(PyFunction_Check(po)) { new(d) Gate(new GateActionPy(po)); } else if(PyCallable_Check(po) && PyMethod_Self(po)) { new(d) Gate(new GateActionPy(po)); } #if 1 //actually isnt used anywhere so far else if(extract(po).check()) { Gate& c = extract(po); new(d) Gate(c); } #endif else { throw_error_already_set(); } //FIXME dont know type, which to throw? data->convertible = d; } }; /// struct Callback1Action_int_Py : public Callback1Action { object o; virtual void Execute(int p1) { call(o.ptr(), "i", p1); } Callback1Action_int_Py(PyObject *callable) : o(handle<>(borrowed(callable))) {} }; struct Callback1_int_from_python { Callback1_int_from_python() { converter::registry::push_back(&convertible, &construct, type_id >()); } static void* convertible(PyObject* po) { if(po == Py_None) return po; if(PyFunction_Check(po)) return po; if(PyCallable_Check(po) && PyMethod_Self(po)) return po; if(extract&>(po).check()) return po; return 0; } static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) { void* d = ((converter::rvalue_from_python_storage >*)data)->storage.bytes; if(po == Py_None) { new(d) Callback1(); } else if(PyFunction_Check(po)) { new(d) Callback1(new Callback1Action_int_Py(po)); } else if(PyCallable_Check(po) && PyMethod_Self(po)) { new(d) Callback1(new Callback1Action_int_Py(po)); } #if 1 //actually isnt used anywhere so far else if(extract&>(po).check()) { Callback1& c = extract&>(po); new(d) Callback1(c); } #endif else { throw_error_already_set(); } //FIXME dont know type, which to throw? data->convertible = d; } }; //Gate1 struct Gate1Action_int_Py : public Gate1Action { object o; virtual bool Execute(int p1) { return call(o.ptr(), "i", p1); } Gate1Action_int_Py(PyObject *callable) : o(handle<>(borrowed(callable))) {} }; struct Gate1_int_from_python { Gate1_int_from_python() { converter::registry::push_back(&convertible, &construct, type_id >()); } static void* convertible(PyObject* po) { if(po == Py_None) return po; if(PyFunction_Check(po)) return po; if(PyCallable_Check(po) && PyMethod_Self(po)) return po; if(extract&>(po).check()) return po; return 0; } static void construct(PyObject* po, converter::rvalue_from_python_stage1_data* data) { void* d = ((converter::rvalue_from_python_storage >*)data)->storage.bytes; if(po == Py_None) { new(d) Gate1(); } else if(PyFunction_Check(po)) { new(d) Gate1(new Gate1Action_int_Py(po)); } else if(PyCallable_Check(po) && PyMethod_Self(po)) { new(d) Gate1(new Gate1Action_int_Py(po)); } #if 1 //actually isnt used anywhere so far else if(extract&>(po).check()) { Gate1& c = extract&>(po); new(d) Gate1(c); } #endif else { throw_error_already_set(); } //FIXME dont know type, which to throw? data->convertible = d; } }; void export_Callback() { ONCELOCK { //to_python_converter(); Callback_from_python(); class_("Callback", "Upp Callback") .def(init()) .def("clear", &Callback::Clear) .def("valid", &Callback::operator bool) .def("__call__", &Callback::Execute) ; class_("Gate", "Upp Gate") .def(init()) .def("clear", &Gate::Clear) .def("valid", &Gate::operator bool) .def("__call__", &Gate::Execute) ; class_ >("Callback1", "Upp Callback1") .def(init&>()) .def("clear", &Callback1::Clear) .def("valid", &Callback1::operator bool) .def("__call__", &Callback1::Execute) ; class_ >("Gate1", "Upp Gate1") .def(init&>()) .def("clear", &Gate1::Clear) .def("valid", &Gate1::operator bool) .def("__call__", &Gate1::Execute) ; } } END_UPP_NAMESPACE