我包装纸将其用C++编写到Python API libwebqqC++后端调用蟒级别定义回调与痛饮包装
有一个库是设置在升压功能定义的类型。
typedef boost::function<void (std::string)> EventListener;
Python级别可以定义“EventListener”变量回调。
还有一个C++级别的map结构,它是Adapter类中的event_map。 event_map的关键类型是一个QQEvent枚举类型,event_map的值类型是包装了EvenListener的类“Action”。
class Action
{
EventListener _callback;
public:
Action(){
n_actions++;
}
Action(const EventListener & cb){
setCallback(cb);
}
virtual void operator()(std::string data) {
_callback(data);
}
void setCallback(const EventListener & cb){
_callback = cb;
}
virtual ~Action(){ std::cout<<"Destruct Action"<<std::endl; n_actions --; }
static int n_actions;
};
class Adapter{
std::map<QQEvent, Action > event_map;
public:
Adapter();
~Adapter();
void trigger(const QQEvent &event, const std::string data);
void register_event_handler(QQEvent event, EventListener callback);
bool is_event_registered(const QQEvent & event);
void delete_event_handler(QQEvent event);
};
类中的“register_event_handler”Adapter是向相关事件注册回调函数的API。 如果发生事件,C++后端会调用它。但是我们需要在python级别实现回调。我裹着“callback.i”回调类型
问题是,当我致电register_event测试蟒蛇script,一个类型的错误总是发生:
Traceback (most recent call last):
File "testwrapper.py", line 44, in <module>
worker = Worker()
File "testwrapper.py", line 28, in __init__
a.setCallback(self.on_message)
File "/home/devil/linux/libwebqq/wrappers/python/libwebqqpython.py", line 95, in setCallback
def setCallback(self, *args): return _libwebqqpython.Action_setCallback(self, *args)
TypeError: in method 'Action_setCallback', argument 2 of type 'EventListener const &'
Destruct Action
请帮忙找出根源这种类型的错误的原因和解决这个问题。
http://stackoverflow.com/questions/12392703/what-is-the-cleanest-way-to-call-a-python-function-from-c-with-a-swig-wrapped – xmduhan 2016-05-11 10:33:08