obj.c

Mon, 17 Jan 2005 22:02:09 +0100

author
tuomov
date
Mon, 17 Jan 2005 22:02:09 +0100
changeset 85
9f94b2e96e3b
parent 62
aae5facf9fc5
permissions
-rw-r--r--

trunk: changeset 1934
Fixed everything that requires locale stuff to check CF_NO_LOCALE.

60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
1 /*
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
2 * libtu/obj.c
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
3 *
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
4 * Copyright (c) Tuomo Valkonen 1999-2004.
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
5 *
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
6 * You may distribute and modify this library under the terms of either
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
7 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
8 */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
9
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
10 #include <string.h>
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
11
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
12 #include "types.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
13 #include "obj.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
14 #include "objp.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
15 #include "misc.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
16 #include "dlist.h"
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
17
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
18
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
19 ClassDescr CLASSDESCR(Obj)={"Obj", NULL, 0, NULL, NULL};
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
20
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
21
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
22 static void do_watches(Obj *obj, bool call);
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
23
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
24
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
25 /*{{{ Destroy */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
26
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
27
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
28 void destroy_obj(Obj *obj)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
29 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
30 ClassDescr *d;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
31
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
32 if(OBJ_IS_BEING_DESTROYED(obj))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
33 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
34
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
35 obj->flags|=OBJ_DEST;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
36
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
37 do_watches(obj, TRUE);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
38
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
39 d=obj->obj_type;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
40
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
41 while(d!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
42 if(d->destroy_fn!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
43 d->destroy_fn(obj);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
44 break;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
45 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
46 d=d->ancestor;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
47 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
48
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
49 do_watches(obj, FALSE);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
50
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
51 free(obj);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
52 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
53
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
54
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
55 /*}}}*/
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
56
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
57
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
58 /*{{{ is/cast */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
59
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
61 bool obj_is(const Obj *obj, const ClassDescr *descr)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
62 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
63 ClassDescr *d;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
64
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
65 if(obj==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
66 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
67
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
68 d=obj->obj_type;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
69
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
70 while(d!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
71 if(d==descr)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
72 return TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
73 d=d->ancestor;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
74 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
75 return FALSE;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
76 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
77
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
78
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
79 bool obj_is_str(const Obj *obj, const char *str)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
80 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
81 ClassDescr *d;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
82
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
83 if(obj==NULL || str==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
84 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
85
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
86 d=obj->obj_type;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
87
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
88 while(d!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
89 if(strcmp(d->name, str)==0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
90 return TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
91 d=d->ancestor;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
92 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
93 return FALSE;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
94 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
95
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
96
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
97 const void *obj_cast(const Obj *obj, const ClassDescr *descr)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
98 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
99 ClassDescr *d;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
100
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
101 if(obj==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
102 return NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
103
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
104 d=obj->obj_type;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
105
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
106 while(d!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
107 if(d==descr)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
108 return (void*)obj;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
109 d=d->ancestor;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
110 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
111 return NULL;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
112 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
113
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
114
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
115 /*}}}*/
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
116
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
117
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
118 /*{{{ Dynamic functions */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
119
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
120
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
121 /* This function is called when no handler is found.
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
122 */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
123 static void dummy_dyn()
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
124 {
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
125 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
126
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
127
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
128 static int comp_fun(const void *a, const void *b)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
129 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
130 void *af=(void*)((DynFunTab*)a)->func;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
131 void *bf=(void*)((DynFunTab*)b)->func;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
132
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
133 return (af<bf ? -1 : (af==bf ? 0 : 1));
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
134 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
135
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
136
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
137 DynFun *lookup_dynfun(const Obj *obj, DynFun *func,
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
138 bool *funnotfound)
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
139 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
140 ClassDescr *descr;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
141 DynFunTab *df;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
142 /*DynFunTab dummy={NULL, NULL};*/
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
143 /*dummy.func=func;*/
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
144
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
145 if(obj==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
146 return NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
147
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
148 descr=obj->obj_type;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
149
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
150 for(; descr!=&Obj_classdescr; descr=descr->ancestor){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
151 if(descr->funtab==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
152 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
153
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
154 if(descr->funtab_n==-1){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
155 /* Need to sort the table. */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
156 descr->funtab_n=0;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
157 df=descr->funtab;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
158 while(df->func!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
159 descr->funtab_n++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
160 df++;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
161 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
162
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
163 if(descr->funtab_n>0){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
164 qsort(descr->funtab, descr->funtab_n, sizeof(DynFunTab),
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
165 comp_fun);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
166 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
167 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
168
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
169 /*
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
170 if(descr->funtab_n==0)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
171 continue;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
172
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
173 df=(DynFunTab*)bsearch(&dummy, descr->funtab, descr->funtab_n,
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
174 sizeof(DynFunTab), comp_fun);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
175 if(df!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
176 *funnotfound=FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
177 return df->handler;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
178 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
179 */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
180
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
181 /* Using custom bsearch instead of the one in libc is probably
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
182 * faster as the overhead of calling a comparison function would
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
183 * be significant given that the comparisons are simple and
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
184 * funtab_n not that big.
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
185 */
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
186 {
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
187 int min=0, max=descr->funtab_n-1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
188 int ndx;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
189 df=descr->funtab;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
190 while(max>=min){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
191 ndx=(max+min)/2;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
192 if((void*)df[ndx].func==(void*)func){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
193 *funnotfound=FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
194 return df[ndx].handler;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
195 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
196 if((void*)df[ndx].func<(void*)func)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
197 min=ndx+1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
198 else
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
199 max=ndx-1;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
200 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
201 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
202 }
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
203
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
204 *funnotfound=TRUE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
205 return dummy_dyn;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
206 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
207
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
208
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
209 bool has_dynfun(const Obj *obj, DynFun *func)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
210 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
211 bool funnotfound;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
212 lookup_dynfun(obj, func, &funnotfound);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
213 return !funnotfound;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
214 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
215
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
216
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
217 /*}}}*/
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
218
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
219
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
220 /*{{{ Watches */
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
221
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
222
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
223 bool watch_setup(Watch *watch, Obj *obj, WatchHandler *handler)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
224 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
225 if(OBJ_IS_BEING_DESTROYED(obj))
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
226 return FALSE;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
227
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
228 watch_reset(watch);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
229
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
230 watch->handler=handler;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
231 LINK_ITEM(obj->obj_watches, watch, next, prev);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
232 watch->obj=obj;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
233
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
234 return TRUE;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
235 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
236
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
237 void do_watch_reset(Watch *watch, bool call)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
238 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
239 WatchHandler *handler=watch->handler;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
240 Obj *obj=watch->obj;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
241
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
242 watch->handler=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
243
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
244 if(obj==NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
245 return;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
246
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
247 UNLINK_ITEM(obj->obj_watches, watch, next, prev);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
248 watch->obj=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
249
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
250 if(call && handler!=NULL)
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
251 handler(watch, obj);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
252 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
253
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
254
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
255 void watch_reset(Watch *watch)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
256 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
257 do_watch_reset(watch, FALSE);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
258 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
259
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
260
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
261 bool watch_ok(Watch *watch)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
262 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
263 return watch->obj!=NULL;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
264 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
265
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
266
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
267 static void do_watches(Obj *obj, bool call)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
268 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
269 Watch *watch, *next;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
270
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
271 watch=obj->obj_watches;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
272
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
273 while(watch!=NULL){
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
274 next=watch->next;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
275 do_watch_reset(watch, call);
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
276 watch=next;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
277 }
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
278 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
279
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
280
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
281 void watch_call(Obj *obj)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
282 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
283 do_watches(obj, FALSE);
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
284 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
285
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
286
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
287 void watch_init(Watch *watch)
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
288 {
62
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
289 watch->obj=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
290 watch->next=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
291 watch->prev=NULL;
aae5facf9fc5 trunk: changeset 1318
tuomov
parents: 60
diff changeset
292 watch->handler=NULL;
60
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
293 }
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
294
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
295
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
296 /*}}}*/
a4033700e35c trunk: changeset 1313
tuomov
parents:
diff changeset
297

mercurial