Refine the interactions between the hooks provided by the different modules.
[lttv.git] / ltt / branches / poly / lttv / hook.c
1 #include <lttv/hook.h>
2
3
4 typedef struct _LttvHookClosure {
5 lttv_hook hook;
6 void *hook_data;
7 } LttvHookClosure;
8
9
10 LttvHooks *lttv_hooks_new()
11 {
12 return g_array_new(FALSE, FALSE, sizeof(LttvHookClosure));
13 }
14
15
16 void lttv_hooks_destroy(LttvHooks *h)
17 {
18 g_array_free(h, TRUE);
19 }
20
21
22 void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data)
23 {
24 LttvHookClosure c;
25
26 if(h == NULL)g_error("Null hook added");
27
28 c.hook = f;
29 c.hook_data = hook_data;
30 g_array_append_val(h,c);
31 }
32
33
34 void lttv_hooks_add_list(LttvHooks *h, LttvHooks *list)
35 {
36 guint i;
37
38 for(i = 0 ; i < list->len; i++) {
39 g_array_append_val(h,g_array_index(list, LttvHookClosure, i));
40 }
41 }
42
43
44 void *lttv_hooks_remove(LttvHooks *h, LttvHook f)
45 {
46 unsigned i;
47
48 void *hook_data;
49
50 LttvHookClosure *c;
51
52 for(i = 0 ; i < h->len ; i++) {
53 c = &g_array_index(h, LttvHookClosure, i);
54 if(c->hook == f) {
55 hook_data = c->hook_data;
56 lttv_hooks_remove_by_position(h, i);
57 return hook_data;
58 }
59 }
60 return NULL;
61 }
62
63
64 void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data)
65 {
66 unsigned i;
67
68 LttvHookClosure *c;
69
70 for(i = 0 ; i < h->len ; i++) {
71 c = &g_array_index(h, LttvHookClosure, i);
72 if(c->hook == f && c->hook_data == hook_data) {
73 lttv_hooks_remove_by_position(h, i);
74 return;
75 }
76 }
77 }
78
79
80 void lttv_hooks_remove_list(LttvHooks *h, LttvHooks *list)
81 {
82 guint i, j;
83
84 LttvHookClosure *c, *c_list;
85
86 for(i = 0, j = 0 ; i < h->len && j < list->len ;) {
87 c = &g_array_index(h, LttvHookClosure, i);
88 c_list = &g_array_index(list, LttvHookClosure, j);
89 if(c->hook == c_list->hook && c->hook_data == c_list->hook_data) {
90 lttv_hooks_remove_by_position(h, i);
91 j++;
92 }
93 else i++;
94 }
95
96 /* Normally the hooks in h are ordered as in list. If this is not the case,
97 try harder here. */
98
99 if(j < list->len) {
100 for(; j < list->len ; j++) {
101 c_list = &g_array_index(list, LttvHookClosure, j);
102 lttv_hooks_remove_data(h, c_list->hook, c_list->hook_data);
103 }
104 }
105 }
106
107
108 unsigned lttv_hooks_number(LttvHooks *h)
109 {
110 return h->len;
111 }
112
113
114 void lttv_hooks_get(LttvHooks *h, unsigned i, LttvHook *f, void **hook_data)
115 {
116 LttvHookClosure *c;
117
118 c = &g_array_index(h, LttvHookClosure, i);
119 *f = c->hook;
120 *hook_data = c->hook_data;
121 }
122
123
124 void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i);
125 {
126 g_array_remove_index(h, i)
127 }
128
129
130 gboolean lttv_hooks_call(LttvHooks *h, void *call_data)
131 {
132 gboolean ret = FALSE;
133
134 LttvHookClosure *c;
135
136 if(h != NULL) {
137 for(i = 0 ; i < h->len ; i++) {
138 c = &g_array_index(h, LttvHookClosure, i);
139 ret = ret || c->hook(c->hook_data,call_data);
140 }
141 }
142 return ret;
143 }
144
145
146 gboolean lttv_hooks_call_check(LttvHooks *h, void *call_data)
147 {
148 LttvHookClosure *c;
149
150 for(i = 0 ; i < h->len ; i++) {
151 c = &g_array_index(h, LttvHookClosure, i);
152 if(c->hook(c->hook_data,call_data)) return TRUE;
153 }
154 return FALSE;
155 }
156
157
158 LttvHooksById *lttv_hooks_by_id_new()
159 {
160 return g_ptr_array_new();
161 }
162
163
164 void lttv_hooks_by_id_destroy(LttvHooksById *h)
165 {
166 guint i;
167
168 for(i = 0 ; i < h->len ; i++) {
169 if(h->pdata[i] != NULL) lttv_hooks_destroy((LttvHooks *)(h->pdata[i]));
170 }
171 g_ptr_array_free(h, TRUE);
172 }
173
174
175 LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id);
176 {
177 if(h->len <= id) g_ptr_array_set_size(h, id + 1);
178 if(h->pdata[id] == NULL) h->pdata[id] = lttv_hooks_new();
179 return h->pdata[id];
180 }
181
182
183 unsigned lttv_hooks_by_id_max_id(LttvHooksById *h)
184 {
185 return h->len;
186 }
187
188
189 LttvHooks *lttv_hooks_by_id_get(LttvHooksById *h, unsigned id)
190 {
191 if(id < h->len) return h->pdata[id];
192 return NULL;
193 }
194
195
196 void lttv_hooks_by_id_remove(LttvHooksById *h, unsigned id)
197 {
198 if(id < h->len && h->pdata[id] != NULL) {
199 lttv_hooks_destroy((LttvHooks *)h->pdata[id]);
200 h->pdata[id] = NULL;
201 }
202 }
203
This page took 0.035798 seconds and 4 git commands to generate.