我正在将一个用C++编写的库包装到Python API libwebqq中
有一种在boost函数中定义的类型.
typedef boost::function<void (std::string)> EventListener;
Run Code Online (Sandbox Code Playgroud)
Python级别可以定义"EventListener"变量回调.
C++级别中还有一个映射结构,它是类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 & …Run Code Online (Sandbox Code Playgroud)