-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproperty.c
354 lines (314 loc) · 8.84 KB
/
property.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* Copyright (c) 2011, 2013 Øyvind Kolås
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Authors: Øyvind Kolås <[email protected]>
*/
/* should probably a b-tree instead of a list to improve performance */
#include "oi.h"
#include "stdlib.h"
@generateheader
@trait Property
{
var props;
} PACKED;
typedef struct
{
const char *name; /* quark it */
var value;
} PropertyEntry;
static void prop_destroy! (PropertyEntry *entry, void *user_data);
static void init ()
{
this->props = @list:new ();
this->props@list:set_destroy ((void*) prop_destroy, NULL);
}
static void destroy ()
{
this->props@var:finalize();
}
static void
each_wrapper! (void *item, void *user_data)
{
void **args = user_data;
PropertyEntry *entry = item;
void (*cb)(const char *key, void *item, void *user_data) = args[0];
cb (entry->name, "foo", args[1]);
}
/*
void each (void (*cb)(const char *key, void *item, void *user_data),
void *user_data)
*/
void each (void *cb, void *user_data)
{
void *args[] = {cb, user_data};
if (this)
this->props@list:each(each_wrapper, args);
}
static void
add_to_list! (void *item, void *value, void *user_data)
{
const char *entry = item;
var list = user_data;
list@list:append((void*)entry);
}
var list ()
{
var ret = var_new (LIST, NULL);
self@property:each(add_to_list, ret);
//ret@list:set_destroy(oi_free, NULL);
return ret;
}
static void prop_destroy! (PropertyEntry *entry, void *user_data)
{
if (entry->name)
oi_strfree ((void*)entry->name);
ref_dec (entry->value);
oi_free (sizeof (PropertyEntry), entry);
}
static int match_name! (void *property_entry, const char *name)
{
PropertyEntry *entry = property_entry;
return (entry && entry->name && name && !strcmp (entry->name, name));
}
static PropertyEntry *get_entry_read (const char *name)
{
int no;
PropertyEntry *entry = NULL;
no = this->props@list:find_custom ((void*)match_name, (void*)name);
if (no >= 0)
entry = this->props@list:get (no);
else
{
#if 0
/* reading the property creates it! */
entry = oi_malloc (sizeof (PropertyEntry));
entry->name = name@oi:strdup ();
this->props@list:append (entry);
entry->type = OI_PTYPE_INT;
entry->value.as_int = 0;
#endif
}
return entry;
}
static PropertyEntry *get_entry_write (const char *name)
{
int no;
PropertyEntry *entry;
self@mutex:lock ();
no = this->props@list:find_custom((void*)match_name, (void*)name);
if (no >= 0)
{
entry = this->props@list:get(no);
}
else
{
entry = oi_malloc (sizeof (PropertyEntry));
entry->name = name@oi:strdup();
entry->value = var_new(VALUE, NULL);
this->props@list:append (entry);
} /* XXX: oicc hack*/ ;
self@mutex:unlock ();
return entry;
}
void set_float (const char *name, float value)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry ||
entry->value@value:type() != OI_PTYPE_FLOAT ||
(entry->value@value:type() == OI_PTYPE_FLOAT &&
entry->value@value:get_float() != value))
{
entry = self@property:get_entry_write (name);
self@"pre-notify"((void*)name);
entry->value@value:set_float (value);
self@"notify"((void*)name);
}
}
void set_double (const char *name, double value)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry ||
entry->value@value:type() != OI_PTYPE_DOUBLE ||
(entry->value@value:type() == OI_PTYPE_DOUBLE &&
entry->value@value:get_double () != value))
{
entry = self@property:get_entry_write (name);
self@"pre-notify"((void*)name);
entry->value@value:set_double (value);
self@"notify"((void*)name);
}
}
void set_int (const char *name, int value)
{
PropertyEntry *entry = self@property:get_entry_read (name);
PropertyType type = 0;
if (entry)
type = entry->value@value:type();
if (!entry ||
type != OI_PTYPE_INT ||
(type == OI_PTYPE_INT &&
entry->value@value:get_int() != value))
{
self@"pre-notify"((void*)name);
entry = self@property:get_entry_write (name);
entry->value@value:set_int (value);
self@"notify"((void*)name);
}
}
void set_string (const char *name, const char *value)
{
PropertyEntry *entry = self@property:get_entry_read (name);
PropertyType type = 0;
if (entry)
type = entry->value@value:type();
if (!entry ||
type != OI_PTYPE_STRING ||
(type == OI_PTYPE_STRING && strcmp(entry->value@value:get_string(), value)))
{
self@"pre-notify"((void*)name);
entry = self@property:get_entry_write (name);
entry->value@value:set_string (value);
self@"notify"((void*)name);
}
}
void set_oi (const char *name, var value)
{
PropertyEntry *entry = self@property:get_entry_read (name);
PropertyType type = 0;
if (entry)
type = entry->value@value:type();
if (!entry ||
type != OI_PTYPE_OI ||
(type == OI_PTYPE_OI && entry->value@value:get_oi()!= value))
{
self@"pre-notify"((void*)name);
entry = self@property:get_entry_write (name);
entry->value@value:set_oi (value);
self@"notify"((void*)name);
}
}
void set_pointer (const char *name, void *value)
{
PropertyEntry *entry = self@property:get_entry_read (name);
PropertyType type = 0;
if (entry)
type = entry->value@value:type();
if (!entry ||
type != OI_PTYPE_POINTER ||
(type == OI_PTYPE_POINTER && entry->value@value:get_pointer()!= value))
{
self@"pre-notify"((void*)name);
entry = self@property:get_entry_write (name);
entry->value@value:set_pointer (value);
self@"notify"((void*)name);
}
}
float get_float (const char *name)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry)
return 0.0;
return (entry->value@value:get_float());
}
float get_double (const char *name)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry)
return 0.0;
return (entry->value@value:get_double ());
}
int get_int (const char *name)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry)
return 0;
return (entry->value@value:get_int());
}
const char *get_string (const char *name)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry)
return "";
return (entry->value@value:get_string());
}
var get_oi (const char *name)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry)
return NULL;
return (entry->value@value:get_oi());
}
void *get_pointer (const char *name)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry)
return NULL;
return (entry->value@value:get_pointer());
}
PropertyType type (const char *name)
{
PropertyEntry *entry = self@property:get_entry_read (name);
if (!entry)
return 0;
return (entry->value@value:type());
}
int is_string (const char *name)
{
return (self@property:type(name) == OI_PTYPE_STRING);
}
void dup (var clone)
{
var proplist = self@property:list();
int i ;
for (i = 0; i < proplist@list:get_size(); i++)
{
const char *name = proplist@list:get(i);
PropertyType type = self@property:type(name);
switch (type)
{
case OI_PTYPE_FLOAT:
property_set_float (clone, name,
property_get_float (self, name));
break;
case OI_PTYPE_DOUBLE:
property_set_double (clone, name,
property_get_double (self, name));
break;
case OI_PTYPE_INT:
property_set_int (clone, name,
property_get_int (self, name));
break;
case OI_PTYPE_STRING:
property_set_string (clone, name,
property_get_string (self, name));
break;
case OI_PTYPE_OI:
{
var oi = property_get_oi (self, name);
property_set_oi (clone, name, oi);
oi@ref:dec ();
}
break;
case OI_PTYPE_POINTER:
property_set_pointer (clone, name,
property_get_pointer (self, name));
break;
default:
fprintf (stderr, "cloning of property '%s' failed due to type %i %i\n",
name, type, OI_PTYPE_OI);
}
}
proplist@ref:dec ();
}
@end