Changed automake files to reflect the new header files.
[lttv.git] / ltt / branches / poly / lttv / hook.c
CommitLineData
3d218f2a 1#include <lttv/hook.h>
eccb5352 2
3
dc877563 4typedef struct _LttvHookClosure {
eccb5352 5 lttv_hook hook;
6 void *hook_data;
dc877563 7} LttvHookClosure;
eccb5352 8
9
dc877563 10LttvHooks *lttv_hooks_new()
11{
12 return g_array_new(FALSE, FALSE, sizeof(LttvHookClosure));
eccb5352 13}
14
dc877563 15
16void lttv_hooks_destroy(LttvHooks *h)
17{
eccb5352 18 g_array_free(h, TRUE);
19}
20
eccb5352 21
dc877563 22void lttv_hooks_add(LttvHooks *h, LttvHook f, void *hook_data)
23{
24 LttvHookClosure c;
25
26 if(h == NULL)g_error("Null hook added");
eccb5352 27
28 c.hook = f;
29 c.hook_data = hook_data;
30 g_array_append_val(h,c);
31}
32
33
dc877563 34void 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
44void *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
64void lttv_hooks_remove_data(LttvHooks *h, LttvHook f, void *hook_data)
eccb5352 65{
dc877563 66 unsigned i;
67
68 LttvHookClosure *c;
eccb5352 69
70 for(i = 0 ; i < h->len ; i++) {
dc877563 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
80void 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 }
eccb5352 104 }
105}
106
107
dc877563 108unsigned lttv_hooks_number(LttvHooks *h)
109{
110 return h->len;
111}
eccb5352 112
dc877563 113
114void 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
124void lttv_hooks_remove_by_position(LttvHooks *h, unsigned i);
125{
126 g_array_remove_index(h, i)
127}
128
129
130gboolean 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
146gboolean 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
158LttvHooksById *lttv_hooks_by_id_new()
159{
eccb5352 160 return g_ptr_array_new();
161}
162
163
dc877563 164void 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 }
eccb5352 171 g_ptr_array_free(h, TRUE);
172}
173
174
dc877563 175LttvHooks *lttv_hooks_by_id_find(LttvHooksById *h, unsigned id);
eccb5352 176{
dc877563 177 if(h->len <= id) g_ptr_array_set_size(h, id + 1);
eccb5352 178 if(h->pdata[id] == NULL) h->pdata[id] = lttv_hooks_new();
dc877563 179 return h->pdata[id];
eccb5352 180}
181
dc877563 182
183unsigned lttv_hooks_by_id_max_id(LttvHooksById *h)
eccb5352 184{
dc877563 185 return h->len;
186}
187
188
189LttvHooks *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
196void 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 }
eccb5352 202}
203
This page took 0.032141 seconds and 4 git commands to generate.