| |
1 /* |
| |
2 * libtu/objlist.c |
| |
3 * |
| |
4 * Copyright (c) Tuomo Valkonen 1999-2004. |
| |
5 * |
| |
6 * You may distribute and modify this library under the terms of either |
| |
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later. |
| |
8 */ |
| |
9 |
| |
10 #include "obj.h" |
| |
11 #include "types.h" |
| |
12 #include "objlist.h" |
| |
13 #include "dlist.h" |
| |
14 #include "misc.h" |
| |
15 |
| |
16 |
| |
17 static ObjList *iter_next=NULL; |
| |
18 |
| |
19 |
| |
20 static void free_node(ObjList **objlist, ObjList *node) |
| |
21 { |
| |
22 UNLINK_ITEM(*objlist, node, next, prev); |
| |
23 free(node); |
| |
24 } |
| |
25 |
| |
26 |
| |
27 void watch_handler(Watch *watch, Obj *obj) |
| |
28 { |
| |
29 ObjList *node=(ObjList*)watch; |
| |
30 ObjList **list=node->list; |
| |
31 |
| |
32 if(iter_next==node) |
| |
33 iter_next=node->next; |
| |
34 |
| |
35 free_node(list, node); |
| |
36 } |
| |
37 |
| |
38 |
| |
39 |
| |
40 bool objlist_insert(ObjList **objlist, Obj *obj) |
| |
41 { |
| |
42 ObjList *node; |
| |
43 |
| |
44 if(obj==NULL) |
| |
45 return FALSE; |
| |
46 |
| |
47 node=ALLOC(ObjList); |
| |
48 |
| |
49 if(node==NULL) |
| |
50 return FALSE; |
| |
51 |
| |
52 watch_init(&(node->watch)); |
| |
53 watch_setup(&(node->watch), obj, watch_handler); |
| |
54 node->list=objlist; |
| |
55 |
| |
56 LINK_ITEM_FIRST(*objlist, node, next, prev); |
| |
57 |
| |
58 return TRUE; |
| |
59 } |
| |
60 |
| |
61 |
| |
62 void objlist_remove(ObjList **objlist, Obj *obj) |
| |
63 { |
| |
64 ObjList *node=*objlist; |
| |
65 |
| |
66 while(node!=NULL){ |
| |
67 if(node->watch.obj==obj){ |
| |
68 watch_reset(&(node->watch)); |
| |
69 free_node(objlist, node); |
| |
70 return; |
| |
71 } |
| |
72 node=node->next; |
| |
73 } |
| |
74 } |
| |
75 |
| |
76 |
| |
77 void objlist_clear(ObjList **objlist) |
| |
78 { |
| |
79 while(*objlist!=NULL){ |
| |
80 watch_reset(&((*objlist)->watch)); |
| |
81 free_node(objlist, *objlist); |
| |
82 } |
| |
83 } |
| |
84 |
| |
85 |
| |
86 /* Warning: not thread-safe */ |
| |
87 |
| |
88 |
| |
89 Obj *objlist_init_iter(ObjList *objlist) |
| |
90 { |
| |
91 if(objlist==NULL){ |
| |
92 iter_next=NULL; |
| |
93 return NULL; |
| |
94 } |
| |
95 |
| |
96 iter_next=objlist->next; |
| |
97 return objlist->watch.obj; |
| |
98 } |
| |
99 |
| |
100 |
| |
101 Obj *objlist_iter() |
| |
102 { |
| |
103 ObjList *ret; |
| |
104 |
| |
105 if(iter_next==NULL) |
| |
106 return NULL; |
| |
107 |
| |
108 ret=iter_next; |
| |
109 iter_next=iter_next->next; |
| |
110 |
| |
111 return ret->watch.obj; |
| |
112 } |