实现了同一事件的发生支持多次回调函数的注册,这样可以在不同的程序文件中,对同一事件的发生进行不同的处理,以满足应用的需求。
该库使用GCC编译,亲测OK。test文件夹内附有测试例程,如果在其它编译器下,编译出现问题,或不能正常运行,请根据所使用的编译器,优化代码。
如果有需要优化的地方,或因为需要支持其它编译器而改变了代码,请新建分支,并提pull request,一起为开源做出贡献,谢谢!
typedef enum { EVENT_0, EVENT_1, EVENT_2, EVENT_3, EVENT_4, EVENT_5, EVENT_6, EVENT_7, EVENT_8, EVENT_9, EVENT_MAX /* the max number of the event to be registered */ } event_t; 根据不同的应用自行定义。
typedef int (* callback_t)(void * arg); 定义callback_t函数指针数据类型,指向(输入参数为void * arg,返回值为int类型)的函数。
typedef struct { callback_t cb[REGISTER_NUM_MAX]; /* save the callback functions that have been registered */ unsigned int count; /* save the number of the callback functions that have been registered */ } callback_s; 成员cb保存同一事件每一个被注册的回调函数的入口地址。
成员count保存同一事件被注册的回调函数的数目。
#define REGISTER_NUM_MAX 10 /* the max number that is allowed to be registered of every single event */ 指明每个事件最多被允许注册的回调函数的最大的数目。
event : 事件 cb_fn : 传递待注册的回调函数入口 无 event : 事件 无 event : 事件 cb_fn : 传递待删除的回调函数入口 无