zf_event.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. *****************************************************************************
  3. * @file zf_event.h
  4. * @author Zorb
  5. * @version V1.0.0
  6. * @date 2018-06-28
  7. * @brief 事件和事件处理器的头文件
  8. *****************************************************************************
  9. * @history
  10. *
  11. * 1. Date:2018-06-28
  12. * Author:Zorb
  13. * Modification:建立文件
  14. *
  15. *****************************************************************************
  16. */
  17. #ifndef __ZF_EVENT_H__
  18. #define __ZF_EVENT_H__
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #include "stdbool.h"
  23. #include "stdint.h"
  24. #include "zf_list.h"
  25. #define EVENT_HIGHEST_PRIORITY 0 /* 事件最高优先级(用户不可用) */
  26. #define EVENT_LOWEST_PRIORITY 32 /* 事件最低优先级(用户不可用) */
  27. /* 推送事件 */
  28. #define EVENT_POST(handler_, event_) handler_->Add(handler_, event_)
  29. /* 事件 */
  30. typedef struct _Event Event;
  31. /* 事件处理器 */
  32. typedef struct _EventHandler EventHandler;
  33. /* 事件处理程序类型 */
  34. typedef void (*IEventProcess)(List *pArgList);
  35. /* 事件结构 */
  36. struct _Event
  37. {
  38. uint8_t Priority; /* 优先级 */
  39. IEventProcess EventProcess; /* 事件程序 */
  40. List *pArgList; /* 事件程序的参数指针 */
  41. /* 增加程序参数(深拷贝,按先后顺序入队列) */
  42. bool (*AddArg)(Event * const pEvent, void *pArg, uint32_t size);
  43. /* 销毁事件 */
  44. bool (*Dispose)(Event * const pEvent);
  45. };
  46. /* 事件处理器结构 */
  47. struct _EventHandler
  48. {
  49. List *pEventList; /* 事件列表 */
  50. bool IsRunning; /* 是否正在运行:默认开 */
  51. /* 获取事件数 */
  52. uint32_t (*GetEventCount)(EventHandler * const pEventHandler);
  53. /* 增加事件(按优先级排序) */
  54. bool (*Add)(EventHandler * const pEventHandler, Event *pEvent);
  55. /* 删除事件(释放空间) */
  56. bool (*Delete)(EventHandler * const pEventHandler, Event *pEvent);
  57. /* 清空事件列表(释放空间) */
  58. bool (*Clear)(EventHandler * const pEventHandler);
  59. /* 销毁事件处理器(释放空间) */
  60. bool (*Dispose)(EventHandler * const pEventHandler);
  61. /* 执行事件(按列表位置) */
  62. void (*Execute)(struct _EventHandler * const pEventHandler);
  63. };
  64. /* 创建事件 */
  65. bool Event_create(Event **ppEvent);
  66. /* 增加事件参数(深拷贝,按先后顺序入队列) */
  67. bool Event_addArg(Event * const pEvent, void *pArg, uint32_t size);
  68. /* 销毁事件 */
  69. bool Event_Dispose(Event * const pEvent);
  70. /* 创建事件处理器 */
  71. bool EventHandler_create(EventHandler **ppEventHandler);
  72. /* 获取事件数 */
  73. uint32_t EventHandler_getEventCount(EventHandler * const pEventHandler);
  74. /* 增加事件(按优先级排序) */
  75. bool EventHandler_add(EventHandler * const pEventHandler, Event *pEvent);
  76. /* 删除事件(释放空间) */
  77. bool EventHandler_delete(EventHandler * const pEventHandler, Event *pEvent);
  78. /* 清空事件列表(释放空间) */
  79. bool EventHandler_clear(EventHandler * const pEventHandler);
  80. /* 销毁事件处理器(释放空间) */
  81. bool EventHandler_dispose(EventHandler * const pEventHandler);
  82. /* 执行事件(按列表位置) */
  83. void EventHandler_execute(EventHandler * const pEventHandler);
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif /* __ZF_EVENT_H__ */
  88. /******************************** END OF FILE ********************************/