icons
[lttv.git] / ltt / branches / poly / lttv / hook.c
CommitLineData
3d218f2a 1#include <lttv/hook.h>
eccb5352 2
3
4typedef struct _lttv_hook_closure {
5 lttv_hook hook;
6 void *hook_data;
7} lttv_hook_closure;
8
9
10inline lttv_hooks *lttv_hooks_new() {
11 return g_array_new(FALSE, FALSE, sizeof(lttv_hook_closure));
12}
13
14/* MD: delete elements of the array also, but don't free pointed addresses
15 * (functions).
16 */
17inline void lttv_hooks_destroy(lttv_hooks *h) {
18 g_array_free(h, TRUE);
19}
20
21inline void lttv_hooks_add(lttv_hooks *h, lttv_hook f, void *hook_data) {
22 lttv_hook_closure c;
23
24 /* if h is null, do nothing, or popup a warning message */
25 if(h == NULL)return;
26
27 c.hook = f;
28 c.hook_data = hook_data;
29 g_array_append_val(h,c);
30}
31
32
fcdf0ec2 33int lttv_hooks_call(lttv_hooks *h, void *call_data)
eccb5352 34{
35 int i;
36 lttv_hook_closure * c;
37
38 for(i = 0 ; i < h->len ; i++) {
39 c = ((lttv_hook_closure *)h->data) + i;
40 if(c != NULL)
41 c->hook(c->hook_data,call_data);
42 else
43 g_critical("NULL hook called\n");
44 }
45}
46
47
48/* Sometimes different hooks need to be called based on the case. The
49 case is represented by an unsigned integer id and may represent different
50 event types, for instance. */
51
52inline lttv_hooks_by_id *lttv_hooks_by_id_new() {
53 return g_ptr_array_new();
54}
55
56
57inline void lttv_hooks_by_id_destroy(lttv_hooks_by_id *h) {
58 /* MD: delete elements of the array also */
59 g_ptr_array_free(h, TRUE);
60}
61
62
63void lttv_hooks_by_id_add(lttv_hooks_by_id *h, lttv_hook f, void *hook_data,
64 unsigned int id)
65{
66 if(h->len < id) g_ptr_array_set_size(h, id);
67 if(h->pdata[id] == NULL) h->pdata[id] = lttv_hooks_new();
68 lttv_hooks_add(h->pdata[id], f, hook_data);
69}
70
71void lttv_hooks_by_id_call(lttv_hooks_by_id *h, void *call_data, unsigned int id)
72{
73 if(h->len <= id || h->pdata[id] == NULL) return;
74 lttv_hooks_call(h->pdata[id],call_data);
75}
76
This page took 0.02446 seconds and 4 git commands to generate.