-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_list.h
124 lines (115 loc) · 5.93 KB
/
event_list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// Created by xuan on 15-6-13.
//
#ifndef RASPI_RPC_EVENT_LIST_H
#define RASPI_RPC_EVENT_LIST_H
#include <malloc.h>
#define EVENT_LIST_FUNCTION_MODIFIERS_DEFAULT
#ifndef EVENT_LIST_FUNCTION_MODIFIERS
#define EVENT_LIST_FUNCTION_MODIFIERS EVENT_LIST_FUNCTION_MODIFIERS_DEFAULT
#endif
#define EVENT_LIST_FUNCTION_ATTRIBUTES_DEFAULT
#ifndef EVENT_LIST_FUNCTION_ATTRIBUTES
#define EVENT_LIST_FUNCTION_ATTRIBUTES EVENT_LIST_FUNCTION_ATTRIBUTES_DEFAULT
#endif
#define EVENT_LIST_PROTO(el_name, callback_t) \
struct event_list_##el_name##_s; \
typedef callback_t event_list_##el_name##_cb; \
typedef struct event_list_node_##el_name##_s { \
struct event_list_##el_name##_s *el; \
struct event_list_node_##el_name##_s *prev, *next; \
void *data; \
event_list_##el_name##_cb callback; \
} event_list_node_##el_name##_t; \
typedef struct event_list_##el_name##_s { \
const char *name;\
event_list_node_##el_name##_t *first; \
event_list_node_##el_name##_t *last; \
} event_list_##el_name##_t; \
EVENT_LIST_FUNCTION_MODIFIERS \
event_list_node_##el_name##_t * \
EVENT_LIST_FUNCTION_ATTRIBUTES \
el_##el_name##_attach( \
event_list_##el_name##_t *list, \
event_list_##el_name##_cb callback, \
void *data); \
EVENT_LIST_FUNCTION_MODIFIERS \
void EVENT_LIST_FUNCTION_ATTRIBUTES \
el_##el_name##_detach( \
event_list_node_##el_name##_t *list);
#define EVENT_LIST_DEFINE(el_name) \
EVENT_LIST_FUNCTION_MODIFIERS \
event_list_node_##el_name##_t * \
EVENT_LIST_FUNCTION_ATTRIBUTES \
el_##el_name##_attach( \
event_list_##el_name##_t *list, \
event_list_##el_name##_cb callback, \
void *data) { \
event_list_node_##el_name##_t *node = \
(event_list_node_##el_name##_t *) malloc( \
sizeof(event_list_node_##el_name##_t)); \
if (!node) \
return NULL; \
node->el = list; \
node->data = data; \
node->callback = callback; \
node->next = NULL; \
\
node->prev = list->last; \
if (!list->first) \
list->first = node; \
if (list->last) \
list->last->next = node; \
list->last = node; \
\
return node; \
} \
EVENT_LIST_FUNCTION_MODIFIERS \
void EVENT_LIST_FUNCTION_ATTRIBUTES \
el_##el_name##_detach( \
event_list_node_##el_name##_t *node) { \
if (!node) \
return; \
if (!node->prev) \
node->el->first = node->next; \
else \
node->prev->next = node->next; \
if (!node->next) \
node->el->last = node->prev; \
else \
node->next->prev = node->prev; \
\
free(node); \
} \
EVENT_LIST_FUNCTION_MODIFIERS \
void el_##el_name##_clear \
EVENT_LIST_FUNCTION_ATTRIBUTES( \
event_list_##el_name##_t *list) { \
while (list->first) { \
el_##el_name##_detach(list->first); \
} \
}
#define EVENT_LIST_FIRE(el, ...) { \
typeof((el)->first) node = (el)->first, next; \
if (node) { \
do { \
next = node->next; \
if (node->callback) \
node->callback(node->data, __VA_ARGS__);\
node = next; \
} while(next); \
} \
}
#define EVENT_LIST_CALL(el) { \
typeof((el)->first) node = (el)->first, next; \
if (node) { \
do { \
next = node->next; \
if (node->callback) \
node->callback(node->data); \
node = next; \
} while(next); \
} \
}
#define EVENT_LIST_INIT(name) {(name), NULL, NULL}
#endif //RASPI_RPC_EVENT_LIST_H