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