2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
34 struct callback_list {
38 struct callback_list *
39 callback_list_new(void)
41 struct callback_list *ret=g_new0(struct callback_list, 1);
47 callback_new_attr(void (*func)(void), enum attr_type type, int pcount, void **p)
52 ret=g_malloc0(sizeof(struct callback)+pcount*sizeof(void *));
56 for (i = 0 ; i < pcount ; i++) {
63 callback_new(void (*func)(void), int pcount, void **p)
65 return callback_new_attr(func, attr_none, pcount, p);
69 callback_destroy(struct callback *cb)
75 callback_set_arg(struct callback *cb, int arg, void *p)
77 if (arg < 0 || arg > cb->pcount)
83 callback_list_add(struct callback_list *l, struct callback *cb)
85 l->list=g_list_prepend(l->list, cb);
90 callback_list_add_new(struct callback_list *l, void (*func)(void), int pcount, void **p)
94 ret=callback_new(func, pcount, p);
95 callback_list_add(l, ret);
100 callback_list_remove(struct callback_list *l, struct callback *cb)
102 l->list=g_list_remove(l->list, cb);
106 callback_list_remove_destroy(struct callback_list *l, struct callback *cb)
108 callback_list_remove(l, cb);
113 callback_call(struct callback *cb, int pcount, void **p)
119 if (cb->pcount + pcount <= 8) {
120 dbg(1,"cb->pcount=%d\n", cb->pcount);
121 if (cb->pcount && cb->p)
122 dbg(1,"cb->p[0]=%p\n", cb->p[0]);
123 dbg(1,"pcount=%d\n", pcount);
125 dbg(1,"p[0]=%p\n", p[0]);
126 for (i = 0 ; i < cb->pcount ; i++)
128 for (i = 0 ; i < pcount ; i++)
129 pf[i+cb->pcount]=p[i];
130 switch (cb->pcount+pcount) {
132 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6],pf[7]);
135 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5],pf[6]);
138 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4],pf[5]);
141 cb->func(pf[0],pf[1],pf[2],pf[3],pf[4]);
144 cb->func(pf[0],pf[1],pf[2],pf[3]);
147 cb->func(pf[0],pf[1],pf[2]);
150 cb->func(pf[0],pf[1]);
160 g_warning("too many parameters for callback (%d+%d)\n", cb->pcount, pcount);
165 callback_list_call_attr(struct callback_list *l, enum attr_type type, int pcount, void **p)
173 if (type == attr_any || cb->type == attr_any || cb->type == type)
174 callback_call(cb, pcount, p);
175 cbi=g_list_next(cbi);
181 callback_list_call(struct callback_list *l, int pcount, void **p)
183 callback_list_call_attr(l, attr_any, pcount, p);
188 callback_list_destroy(struct callback_list *l)
194 cbi=g_list_next(cbi);
196 g_list_free(l->list);