]>
Commit | Line | Data |
---|---|---|
2f28d2ff AL |
1 | /* |
2 | * QEMU Object Model | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
9bbc853b | 13 | #include "qemu/osdep.h" |
13d4ff07 | 14 | #include "hw/qdev-core.h" |
da34e65c | 15 | #include "qapi/error.h" |
14cccb61 | 16 | #include "qom/object.h" |
a31bdae5 | 17 | #include "qom/object_interfaces.h" |
f348b6d1 | 18 | #include "qemu/cutils.h" |
7b1b5d19 | 19 | #include "qapi/visitor.h" |
b2cd7dee PB |
20 | #include "qapi/string-input-visitor.h" |
21 | #include "qapi/string-output-visitor.h" | |
0e76ed0a | 22 | #include "qapi/qobject-input-visitor.h" |
eb815e24 | 23 | #include "qapi/qapi-builtin-visit.h" |
7b1b5d19 | 24 | #include "qapi/qmp/qerror.h" |
0e76ed0a | 25 | #include "qapi/qmp/qjson.h" |
fa131d94 | 26 | #include "trace.h" |
2f28d2ff | 27 | |
7b7b7d18 PB |
28 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
29 | * of the QOM core on QObject? */ | |
14cccb61 | 30 | #include "qom/qom-qobject.h" |
7b1b5d19 | 31 | #include "qapi/qmp/qbool.h" |
15280c36 | 32 | #include "qapi/qmp/qnum.h" |
7b1b5d19 | 33 | #include "qapi/qmp/qstring.h" |
e02bdf1c | 34 | #include "qemu/error-report.h" |
7b7b7d18 | 35 | |
2f28d2ff AL |
36 | #define MAX_INTERFACES 32 |
37 | ||
38 | typedef struct InterfaceImpl InterfaceImpl; | |
39 | typedef struct TypeImpl TypeImpl; | |
40 | ||
41 | struct InterfaceImpl | |
42 | { | |
33e95c63 | 43 | const char *typename; |
2f28d2ff AL |
44 | }; |
45 | ||
46 | struct TypeImpl | |
47 | { | |
48 | const char *name; | |
49 | ||
50 | size_t class_size; | |
51 | ||
52 | size_t instance_size; | |
53 | ||
54 | void (*class_init)(ObjectClass *klass, void *data); | |
3b50e311 | 55 | void (*class_base_init)(ObjectClass *klass, void *data); |
2f28d2ff AL |
56 | |
57 | void *class_data; | |
58 | ||
59 | void (*instance_init)(Object *obj); | |
8231c2dd | 60 | void (*instance_post_init)(Object *obj); |
2f28d2ff AL |
61 | void (*instance_finalize)(Object *obj); |
62 | ||
63 | bool abstract; | |
64 | ||
65 | const char *parent; | |
66 | TypeImpl *parent_type; | |
67 | ||
68 | ObjectClass *class; | |
69 | ||
70 | int num_interfaces; | |
71 | InterfaceImpl interfaces[MAX_INTERFACES]; | |
72 | }; | |
73 | ||
9970bd88 PB |
74 | static Type type_interface; |
75 | ||
2f28d2ff AL |
76 | static GHashTable *type_table_get(void) |
77 | { | |
78 | static GHashTable *type_table; | |
79 | ||
80 | if (type_table == NULL) { | |
81 | type_table = g_hash_table_new(g_str_hash, g_str_equal); | |
82 | } | |
83 | ||
84 | return type_table; | |
85 | } | |
86 | ||
f54c19ca HP |
87 | static bool enumerating_types; |
88 | ||
2f28d2ff AL |
89 | static void type_table_add(TypeImpl *ti) |
90 | { | |
f54c19ca | 91 | assert(!enumerating_types); |
2f28d2ff AL |
92 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); |
93 | } | |
94 | ||
95 | static TypeImpl *type_table_lookup(const char *name) | |
96 | { | |
97 | return g_hash_table_lookup(type_table_get(), name); | |
98 | } | |
99 | ||
b061dc41 | 100 | static TypeImpl *type_new(const TypeInfo *info) |
2f28d2ff AL |
101 | { |
102 | TypeImpl *ti = g_malloc0(sizeof(*ti)); | |
33e95c63 | 103 | int i; |
2f28d2ff AL |
104 | |
105 | g_assert(info->name != NULL); | |
106 | ||
73093354 AL |
107 | if (type_table_lookup(info->name) != NULL) { |
108 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); | |
109 | abort(); | |
110 | } | |
111 | ||
2f28d2ff AL |
112 | ti->name = g_strdup(info->name); |
113 | ti->parent = g_strdup(info->parent); | |
114 | ||
115 | ti->class_size = info->class_size; | |
116 | ti->instance_size = info->instance_size; | |
117 | ||
118 | ti->class_init = info->class_init; | |
3b50e311 | 119 | ti->class_base_init = info->class_base_init; |
2f28d2ff AL |
120 | ti->class_data = info->class_data; |
121 | ||
122 | ti->instance_init = info->instance_init; | |
8231c2dd | 123 | ti->instance_post_init = info->instance_post_init; |
2f28d2ff AL |
124 | ti->instance_finalize = info->instance_finalize; |
125 | ||
126 | ti->abstract = info->abstract; | |
127 | ||
33e95c63 AL |
128 | for (i = 0; info->interfaces && info->interfaces[i].type; i++) { |
129 | ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); | |
2f28d2ff | 130 | } |
33e95c63 | 131 | ti->num_interfaces = i; |
2f28d2ff | 132 | |
b061dc41 PB |
133 | return ti; |
134 | } | |
2f28d2ff | 135 | |
b061dc41 PB |
136 | static TypeImpl *type_register_internal(const TypeInfo *info) |
137 | { | |
138 | TypeImpl *ti; | |
139 | ti = type_new(info); | |
140 | ||
141 | type_table_add(ti); | |
2f28d2ff AL |
142 | return ti; |
143 | } | |
144 | ||
049cb3cf PB |
145 | TypeImpl *type_register(const TypeInfo *info) |
146 | { | |
147 | assert(info->parent); | |
148 | return type_register_internal(info); | |
149 | } | |
150 | ||
2f28d2ff AL |
151 | TypeImpl *type_register_static(const TypeInfo *info) |
152 | { | |
153 | return type_register(info); | |
154 | } | |
155 | ||
aa04c9d2 IM |
156 | void type_register_static_array(const TypeInfo *infos, int nr_infos) |
157 | { | |
158 | int i; | |
159 | ||
160 | for (i = 0; i < nr_infos; i++) { | |
161 | type_register_static(&infos[i]); | |
162 | } | |
163 | } | |
164 | ||
2f28d2ff AL |
165 | static TypeImpl *type_get_by_name(const char *name) |
166 | { | |
167 | if (name == NULL) { | |
168 | return NULL; | |
169 | } | |
170 | ||
171 | return type_table_lookup(name); | |
172 | } | |
173 | ||
174 | static TypeImpl *type_get_parent(TypeImpl *type) | |
175 | { | |
176 | if (!type->parent_type && type->parent) { | |
177 | type->parent_type = type_get_by_name(type->parent); | |
89d337fd PMD |
178 | if (!type->parent_type) { |
179 | fprintf(stderr, "Type '%s' is missing its parent '%s'\n", | |
180 | type->name, type->parent); | |
181 | abort(); | |
182 | } | |
2f28d2ff AL |
183 | } |
184 | ||
185 | return type->parent_type; | |
186 | } | |
187 | ||
188 | static bool type_has_parent(TypeImpl *type) | |
189 | { | |
190 | return (type->parent != NULL); | |
191 | } | |
192 | ||
193 | static size_t type_class_get_size(TypeImpl *ti) | |
194 | { | |
195 | if (ti->class_size) { | |
196 | return ti->class_size; | |
197 | } | |
198 | ||
199 | if (type_has_parent(ti)) { | |
200 | return type_class_get_size(type_get_parent(ti)); | |
201 | } | |
202 | ||
203 | return sizeof(ObjectClass); | |
204 | } | |
205 | ||
aca59af6 IM |
206 | static size_t type_object_get_size(TypeImpl *ti) |
207 | { | |
208 | if (ti->instance_size) { | |
209 | return ti->instance_size; | |
210 | } | |
211 | ||
212 | if (type_has_parent(ti)) { | |
213 | return type_object_get_size(type_get_parent(ti)); | |
214 | } | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
3f97b53a BR |
219 | size_t object_type_get_instance_size(const char *typename) |
220 | { | |
221 | TypeImpl *type = type_get_by_name(typename); | |
222 | ||
223 | g_assert(type != NULL); | |
224 | return type_object_get_size(type); | |
225 | } | |
226 | ||
33e95c63 | 227 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
2f28d2ff | 228 | { |
33e95c63 AL |
229 | assert(target_type); |
230 | ||
b30d8054 | 231 | /* Check if target_type is a direct ancestor of type */ |
33e95c63 AL |
232 | while (type) { |
233 | if (type == target_type) { | |
234 | return true; | |
235 | } | |
2f28d2ff | 236 | |
33e95c63 AL |
237 | type = type_get_parent(type); |
238 | } | |
239 | ||
240 | return false; | |
241 | } | |
242 | ||
243 | static void type_initialize(TypeImpl *ti); | |
244 | ||
b061dc41 PB |
245 | static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, |
246 | TypeImpl *parent_type) | |
33e95c63 AL |
247 | { |
248 | InterfaceClass *new_iface; | |
249 | TypeInfo info = { }; | |
250 | TypeImpl *iface_impl; | |
251 | ||
b061dc41 PB |
252 | info.parent = parent_type->name; |
253 | info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); | |
33e95c63 AL |
254 | info.abstract = true; |
255 | ||
b061dc41 PB |
256 | iface_impl = type_new(&info); |
257 | iface_impl->parent_type = parent_type; | |
33e95c63 AL |
258 | type_initialize(iface_impl); |
259 | g_free((char *)info.name); | |
260 | ||
261 | new_iface = (InterfaceClass *)iface_impl->class; | |
262 | new_iface->concrete_class = ti->class; | |
b061dc41 | 263 | new_iface->interface_type = interface_type; |
33e95c63 | 264 | |
3d91293e | 265 | ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface); |
2f28d2ff AL |
266 | } |
267 | ||
16bf7f52 DB |
268 | static void object_property_free(gpointer data) |
269 | { | |
270 | ObjectProperty *prop = data; | |
271 | ||
0e76ed0a MAL |
272 | if (prop->defval) { |
273 | qobject_unref(prop->defval); | |
274 | prop->defval = NULL; | |
275 | } | |
16bf7f52 DB |
276 | g_free(prop->name); |
277 | g_free(prop->type); | |
278 | g_free(prop->description); | |
279 | g_free(prop); | |
280 | } | |
281 | ||
ac451033 | 282 | static void type_initialize(TypeImpl *ti) |
2f28d2ff | 283 | { |
745549c8 | 284 | TypeImpl *parent; |
2f28d2ff AL |
285 | |
286 | if (ti->class) { | |
287 | return; | |
288 | } | |
289 | ||
290 | ti->class_size = type_class_get_size(ti); | |
aca59af6 | 291 | ti->instance_size = type_object_get_size(ti); |
1c6d75d5 EH |
292 | /* Any type with zero instance_size is implicitly abstract. |
293 | * This means interface types are all abstract. | |
294 | */ | |
295 | if (ti->instance_size == 0) { | |
296 | ti->abstract = true; | |
297 | } | |
422ca143 MAL |
298 | if (type_is_ancestor(ti, type_interface)) { |
299 | assert(ti->instance_size == 0); | |
300 | assert(ti->abstract); | |
301 | assert(!ti->instance_init); | |
302 | assert(!ti->instance_post_init); | |
303 | assert(!ti->instance_finalize); | |
304 | assert(!ti->num_interfaces); | |
305 | } | |
2f28d2ff | 306 | ti->class = g_malloc0(ti->class_size); |
2f28d2ff | 307 | |
745549c8 PB |
308 | parent = type_get_parent(ti); |
309 | if (parent) { | |
ac451033 | 310 | type_initialize(parent); |
33e95c63 AL |
311 | GSList *e; |
312 | int i; | |
2f28d2ff | 313 | |
719a3077 | 314 | g_assert(parent->class_size <= ti->class_size); |
d5e633fc | 315 | g_assert(parent->instance_size <= ti->instance_size); |
745549c8 | 316 | memcpy(ti->class, parent->class, parent->class_size); |
3e407de4 | 317 | ti->class->interfaces = NULL; |
33e95c63 AL |
318 | |
319 | for (e = parent->class->interfaces; e; e = e->next) { | |
b061dc41 PB |
320 | InterfaceClass *iface = e->data; |
321 | ObjectClass *klass = OBJECT_CLASS(iface); | |
322 | ||
323 | type_initialize_interface(ti, iface->interface_type, klass->type); | |
33e95c63 AL |
324 | } |
325 | ||
326 | for (i = 0; i < ti->num_interfaces; i++) { | |
327 | TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); | |
a9ee3a9e PMD |
328 | if (!t) { |
329 | error_report("missing interface '%s' for object '%s'", | |
330 | ti->interfaces[i].typename, parent->name); | |
331 | abort(); | |
332 | } | |
33e95c63 AL |
333 | for (e = ti->class->interfaces; e; e = e->next) { |
334 | TypeImpl *target_type = OBJECT_CLASS(e->data)->type; | |
335 | ||
336 | if (type_is_ancestor(target_type, t)) { | |
337 | break; | |
338 | } | |
339 | } | |
340 | ||
341 | if (e) { | |
342 | continue; | |
343 | } | |
344 | ||
b061dc41 | 345 | type_initialize_interface(ti, t, t); |
33e95c63 | 346 | } |
745549c8 | 347 | } |
2f28d2ff | 348 | |
e5a0cc5e MY |
349 | ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, |
350 | object_property_free); | |
351 | ||
745549c8 | 352 | ti->class->type = ti; |
3b50e311 | 353 | |
745549c8 PB |
354 | while (parent) { |
355 | if (parent->class_base_init) { | |
356 | parent->class_base_init(ti->class, ti->class_data); | |
3b50e311 | 357 | } |
745549c8 | 358 | parent = type_get_parent(parent); |
2f28d2ff AL |
359 | } |
360 | ||
2f28d2ff AL |
361 | if (ti->class_init) { |
362 | ti->class_init(ti->class, ti->class_data); | |
363 | } | |
2f28d2ff AL |
364 | } |
365 | ||
366 | static void object_init_with_type(Object *obj, TypeImpl *ti) | |
367 | { | |
2f28d2ff AL |
368 | if (type_has_parent(ti)) { |
369 | object_init_with_type(obj, type_get_parent(ti)); | |
370 | } | |
371 | ||
2f28d2ff AL |
372 | if (ti->instance_init) { |
373 | ti->instance_init(obj); | |
374 | } | |
375 | } | |
376 | ||
8231c2dd EH |
377 | static void object_post_init_with_type(Object *obj, TypeImpl *ti) |
378 | { | |
379 | if (ti->instance_post_init) { | |
380 | ti->instance_post_init(obj); | |
381 | } | |
382 | ||
383 | if (type_has_parent(ti)) { | |
384 | object_post_init_with_type(obj, type_get_parent(ti)); | |
385 | } | |
386 | } | |
387 | ||
6fd5bef1 MA |
388 | bool object_apply_global_props(Object *obj, const GPtrArray *props, |
389 | Error **errp) | |
ea9ce893 | 390 | { |
ea9ce893 MAL |
391 | int i; |
392 | ||
393 | if (!props) { | |
6fd5bef1 | 394 | return true; |
ea9ce893 MAL |
395 | } |
396 | ||
397 | for (i = 0; i < props->len; i++) { | |
398 | GlobalProperty *p = g_ptr_array_index(props, i); | |
d769f0df | 399 | Error *err = NULL; |
ea9ce893 MAL |
400 | |
401 | if (object_dynamic_cast(obj, p->driver) == NULL) { | |
402 | continue; | |
403 | } | |
92fd453c DDAG |
404 | if (p->optional && !object_property_find(obj, p->property, NULL)) { |
405 | continue; | |
406 | } | |
ea9ce893 | 407 | p->used = true; |
778a2dc5 | 408 | if (!object_property_parse(obj, p->property, p->value, &err)) { |
ea9ce893 MAL |
409 | error_prepend(&err, "can't apply global %s.%s=%s: ", |
410 | p->driver, p->property, p->value); | |
50545b2c MAL |
411 | /* |
412 | * If errp != NULL, propagate error and return. | |
413 | * If errp == NULL, report a warning, but keep going | |
414 | * with the remaining globals. | |
415 | */ | |
416 | if (errp) { | |
417 | error_propagate(errp, err); | |
6fd5bef1 | 418 | return false; |
50545b2c MAL |
419 | } else { |
420 | warn_report_err(err); | |
421 | } | |
ea9ce893 MAL |
422 | } |
423 | } | |
6fd5bef1 MA |
424 | |
425 | return true; | |
ea9ce893 MAL |
426 | } |
427 | ||
617902af MA |
428 | /* |
429 | * Global property defaults | |
430 | * Slot 0: accelerator's global property defaults | |
431 | * Slot 1: machine's global property defaults | |
1fff3c20 | 432 | * Slot 2: global properties from legacy command line option |
617902af MA |
433 | * Each is a GPtrArray of of GlobalProperty. |
434 | * Applied in order, later entries override earlier ones. | |
435 | */ | |
1fff3c20 PB |
436 | static GPtrArray *object_compat_props[3]; |
437 | ||
438 | /* | |
439 | * Retrieve @GPtrArray for global property defined with options | |
440 | * other than "-global". These are generally used for syntactic | |
441 | * sugar and legacy command line options. | |
442 | */ | |
443 | void object_register_sugar_prop(const char *driver, const char *prop, const char *value) | |
444 | { | |
445 | GlobalProperty *g; | |
446 | if (!object_compat_props[2]) { | |
447 | object_compat_props[2] = g_ptr_array_new(); | |
448 | } | |
449 | g = g_new0(GlobalProperty, 1); | |
450 | g->driver = g_strdup(driver); | |
451 | g->property = g_strdup(prop); | |
452 | g->value = g_strdup(value); | |
453 | g_ptr_array_add(object_compat_props[2], g); | |
454 | } | |
617902af MA |
455 | |
456 | /* | |
457 | * Set machine's global property defaults to @compat_props. | |
458 | * May be called at most once. | |
459 | */ | |
460 | void object_set_machine_compat_props(GPtrArray *compat_props) | |
461 | { | |
462 | assert(!object_compat_props[1]); | |
463 | object_compat_props[1] = compat_props; | |
464 | } | |
465 | ||
466 | /* | |
467 | * Set accelerator's global property defaults to @compat_props. | |
468 | * May be called at most once. | |
469 | */ | |
470 | void object_set_accelerator_compat_props(GPtrArray *compat_props) | |
471 | { | |
472 | assert(!object_compat_props[0]); | |
473 | object_compat_props[0] = compat_props; | |
474 | } | |
475 | ||
476 | void object_apply_compat_props(Object *obj) | |
477 | { | |
478 | int i; | |
479 | ||
480 | for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) { | |
481 | object_apply_global_props(obj, object_compat_props[i], | |
1fff3c20 | 482 | i == 2 ? &error_fatal : &error_abort); |
617902af MA |
483 | } |
484 | } | |
485 | ||
2a1be4b3 MAL |
486 | static void object_class_property_init_all(Object *obj) |
487 | { | |
488 | ObjectPropertyIterator iter; | |
489 | ObjectProperty *prop; | |
490 | ||
491 | object_class_property_iter_init(&iter, object_get_class(obj)); | |
492 | while ((prop = object_property_iter_next(&iter))) { | |
493 | if (prop->init) { | |
494 | prop->init(obj, prop); | |
495 | } | |
496 | } | |
497 | } | |
498 | ||
e27a9595 | 499 | static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type) |
2f28d2ff | 500 | { |
ac451033 | 501 | type_initialize(type); |
aca59af6 | 502 | |
719a3077 | 503 | g_assert(type->instance_size >= sizeof(Object)); |
2f28d2ff | 504 | g_assert(type->abstract == false); |
719a3077 | 505 | g_assert(size >= type->instance_size); |
2f28d2ff AL |
506 | |
507 | memset(obj, 0, type->instance_size); | |
508 | obj->class = type->class; | |
764b6312 | 509 | object_ref(obj); |
2a1be4b3 | 510 | object_class_property_init_all(obj); |
b604a854 PF |
511 | obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal, |
512 | NULL, object_property_free); | |
2f28d2ff | 513 | object_init_with_type(obj, type); |
8231c2dd | 514 | object_post_init_with_type(obj, type); |
2f28d2ff AL |
515 | } |
516 | ||
213f0c4f | 517 | void object_initialize(void *data, size_t size, const char *typename) |
2f28d2ff AL |
518 | { |
519 | TypeImpl *type = type_get_by_name(typename); | |
520 | ||
e02bdf1c PMD |
521 | if (!type) { |
522 | error_report("missing object type '%s'", typename); | |
523 | abort(); | |
524 | } | |
525 | ||
5b9237f6 | 526 | object_initialize_with_type(data, size, type); |
2f28d2ff AL |
527 | } |
528 | ||
6fd5bef1 MA |
529 | bool object_initialize_child_with_props(Object *parentobj, |
530 | const char *propname, | |
531 | void *childobj, size_t size, | |
532 | const char *type, | |
533 | Error **errp, ...) | |
0210b39d TH |
534 | { |
535 | va_list vargs; | |
6fd5bef1 | 536 | bool ok; |
0210b39d TH |
537 | |
538 | va_start(vargs, errp); | |
6fd5bef1 MA |
539 | ok = object_initialize_child_with_propsv(parentobj, propname, |
540 | childobj, size, type, errp, | |
541 | vargs); | |
0210b39d | 542 | va_end(vargs); |
6fd5bef1 | 543 | return ok; |
0210b39d TH |
544 | } |
545 | ||
6fd5bef1 MA |
546 | bool object_initialize_child_with_propsv(Object *parentobj, |
547 | const char *propname, | |
548 | void *childobj, size_t size, | |
549 | const char *type, | |
550 | Error **errp, va_list vargs) | |
0210b39d TH |
551 | { |
552 | Error *local_err = NULL; | |
6fd5bef1 | 553 | bool ok = false; |
0210b39d | 554 | Object *obj; |
3650b2de | 555 | UserCreatable *uc; |
0210b39d TH |
556 | |
557 | object_initialize(childobj, size, type); | |
558 | obj = OBJECT(childobj); | |
559 | ||
1c94a351 | 560 | if (object_set_propv(obj, errp, vargs) < 0) { |
0210b39d TH |
561 | goto out; |
562 | } | |
563 | ||
d2623129 | 564 | object_property_add_child(parentobj, propname, obj); |
0210b39d | 565 | |
3650b2de MAL |
566 | uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); |
567 | if (uc) { | |
778a2dc5 | 568 | if (!user_creatable_complete(uc, &local_err)) { |
0210b39d TH |
569 | object_unparent(obj); |
570 | goto out; | |
571 | } | |
572 | } | |
573 | ||
6fd5bef1 MA |
574 | ok = true; |
575 | ||
975ac455 | 576 | out: |
0210b39d | 577 | /* |
975ac455 MA |
578 | * We want @obj's reference to be 1 on success, 0 on failure. |
579 | * On success, it's 2: one taken by object_initialize(), and one | |
580 | * by object_property_add_child(). | |
581 | * On failure in object_initialize() or earlier, it's 1. | |
582 | * On failure afterwards, it's also 1: object_unparent() releases | |
583 | * the reference taken by object_property_add_child(). | |
0210b39d TH |
584 | */ |
585 | object_unref(obj); | |
586 | ||
975ac455 | 587 | error_propagate(errp, local_err); |
6fd5bef1 | 588 | return ok; |
0210b39d TH |
589 | } |
590 | ||
9fc7fc4d MA |
591 | void object_initialize_child_internal(Object *parent, |
592 | const char *propname, | |
593 | void *child, size_t size, | |
594 | const char *type) | |
595 | { | |
596 | object_initialize_child_with_props(parent, propname, child, size, type, | |
597 | &error_abort, NULL); | |
598 | } | |
599 | ||
5d9d3f47 AF |
600 | static inline bool object_property_is_child(ObjectProperty *prop) |
601 | { | |
602 | return strstart(prop->type, "child<", NULL); | |
603 | } | |
604 | ||
57c9fafe AL |
605 | static void object_property_del_all(Object *obj) |
606 | { | |
9859facc | 607 | g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL); |
b604a854 | 608 | ObjectProperty *prop; |
9859facc | 609 | ObjectPropertyIterator iter; |
b604a854 PF |
610 | bool released; |
611 | ||
612 | do { | |
613 | released = false; | |
9859facc MAL |
614 | object_property_iter_init(&iter, obj); |
615 | while ((prop = object_property_iter_next(&iter)) != NULL) { | |
616 | if (g_hash_table_add(done, prop)) { | |
617 | if (prop->release) { | |
618 | prop->release(obj, prop->name, prop->opaque); | |
619 | released = true; | |
620 | break; | |
621 | } | |
b604a854 | 622 | } |
57c9fafe | 623 | } |
b604a854 | 624 | } while (released); |
57c9fafe | 625 | |
b604a854 | 626 | g_hash_table_unref(obj->properties); |
57c9fafe AL |
627 | } |
628 | ||
f73a32a5 | 629 | static void object_property_del_child(Object *obj, Object *child) |
57c9fafe AL |
630 | { |
631 | ObjectProperty *prop; | |
b604a854 PF |
632 | GHashTableIter iter; |
633 | gpointer key, value; | |
57c9fafe | 634 | |
b604a854 PF |
635 | g_hash_table_iter_init(&iter, obj->properties); |
636 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
637 | prop = value; | |
638 | if (object_property_is_child(prop) && prop->opaque == child) { | |
639 | if (prop->release) { | |
640 | prop->release(obj, prop->name, prop->opaque); | |
641 | prop->release = NULL; | |
642 | } | |
643 | break; | |
644 | } | |
645 | } | |
646 | g_hash_table_iter_init(&iter, obj->properties); | |
647 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
648 | prop = value; | |
5d9d3f47 | 649 | if (object_property_is_child(prop) && prop->opaque == child) { |
b604a854 | 650 | g_hash_table_iter_remove(&iter); |
6c1fdcf9 | 651 | break; |
57c9fafe AL |
652 | } |
653 | } | |
654 | } | |
655 | ||
656 | void object_unparent(Object *obj) | |
657 | { | |
e998fa8d | 658 | if (obj->parent) { |
f73a32a5 | 659 | object_property_del_child(obj->parent, obj); |
e998fa8d | 660 | } |
57c9fafe AL |
661 | } |
662 | ||
2f28d2ff AL |
663 | static void object_deinit(Object *obj, TypeImpl *type) |
664 | { | |
665 | if (type->instance_finalize) { | |
666 | type->instance_finalize(obj); | |
667 | } | |
668 | ||
2f28d2ff AL |
669 | if (type_has_parent(type)) { |
670 | object_deinit(obj, type_get_parent(type)); | |
671 | } | |
672 | } | |
673 | ||
339c2708 | 674 | static void object_finalize(void *data) |
2f28d2ff AL |
675 | { |
676 | Object *obj = data; | |
677 | TypeImpl *ti = obj->class->type; | |
678 | ||
57c9fafe | 679 | object_property_del_all(obj); |
76a6e1cc | 680 | object_deinit(obj, ti); |
db85b575 | 681 | |
719a3077 | 682 | g_assert(obj->ref == 0); |
fde9bf44 PB |
683 | if (obj->free) { |
684 | obj->free(obj); | |
685 | } | |
2f28d2ff AL |
686 | } |
687 | ||
63f7b10b | 688 | static Object *object_new_with_type(Type type) |
2f28d2ff AL |
689 | { |
690 | Object *obj; | |
691 | ||
692 | g_assert(type != NULL); | |
ac451033 | 693 | type_initialize(type); |
2f28d2ff AL |
694 | |
695 | obj = g_malloc(type->instance_size); | |
5b9237f6 | 696 | object_initialize_with_type(obj, type->instance_size, type); |
fde9bf44 | 697 | obj->free = g_free; |
2f28d2ff AL |
698 | |
699 | return obj; | |
700 | } | |
701 | ||
3c75e12e PB |
702 | Object *object_new_with_class(ObjectClass *klass) |
703 | { | |
704 | return object_new_with_type(klass->type); | |
705 | } | |
706 | ||
2f28d2ff AL |
707 | Object *object_new(const char *typename) |
708 | { | |
709 | TypeImpl *ti = type_get_by_name(typename); | |
710 | ||
711 | return object_new_with_type(ti); | |
712 | } | |
713 | ||
a31bdae5 DB |
714 | |
715 | Object *object_new_with_props(const char *typename, | |
716 | Object *parent, | |
717 | const char *id, | |
718 | Error **errp, | |
719 | ...) | |
720 | { | |
721 | va_list vargs; | |
722 | Object *obj; | |
723 | ||
724 | va_start(vargs, errp); | |
725 | obj = object_new_with_propv(typename, parent, id, errp, vargs); | |
726 | va_end(vargs); | |
727 | ||
728 | return obj; | |
729 | } | |
730 | ||
731 | ||
732 | Object *object_new_with_propv(const char *typename, | |
733 | Object *parent, | |
734 | const char *id, | |
735 | Error **errp, | |
736 | va_list vargs) | |
737 | { | |
738 | Object *obj; | |
739 | ObjectClass *klass; | |
740 | Error *local_err = NULL; | |
3650b2de | 741 | UserCreatable *uc; |
a31bdae5 DB |
742 | |
743 | klass = object_class_by_name(typename); | |
744 | if (!klass) { | |
745 | error_setg(errp, "invalid object type: %s", typename); | |
746 | return NULL; | |
747 | } | |
748 | ||
749 | if (object_class_is_abstract(klass)) { | |
750 | error_setg(errp, "object type '%s' is abstract", typename); | |
751 | return NULL; | |
752 | } | |
66e1155a | 753 | obj = object_new_with_type(klass->type); |
a31bdae5 | 754 | |
1c94a351 | 755 | if (object_set_propv(obj, errp, vargs) < 0) { |
a31bdae5 DB |
756 | goto error; |
757 | } | |
758 | ||
6134d752 | 759 | if (id != NULL) { |
d2623129 | 760 | object_property_add_child(parent, id, obj); |
a31bdae5 DB |
761 | } |
762 | ||
3650b2de MAL |
763 | uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); |
764 | if (uc) { | |
778a2dc5 | 765 | if (!user_creatable_complete(uc, &local_err)) { |
6134d752 DB |
766 | if (id != NULL) { |
767 | object_unparent(obj); | |
768 | } | |
a31bdae5 DB |
769 | goto error; |
770 | } | |
771 | } | |
772 | ||
688ffbb4 | 773 | object_unref(obj); |
a31bdae5 DB |
774 | return obj; |
775 | ||
776 | error: | |
621ff94d | 777 | error_propagate(errp, local_err); |
a31bdae5 DB |
778 | object_unref(obj); |
779 | return NULL; | |
780 | } | |
781 | ||
782 | ||
783 | int object_set_props(Object *obj, | |
784 | Error **errp, | |
785 | ...) | |
786 | { | |
787 | va_list vargs; | |
788 | int ret; | |
789 | ||
790 | va_start(vargs, errp); | |
791 | ret = object_set_propv(obj, errp, vargs); | |
792 | va_end(vargs); | |
793 | ||
794 | return ret; | |
795 | } | |
796 | ||
797 | ||
798 | int object_set_propv(Object *obj, | |
799 | Error **errp, | |
800 | va_list vargs) | |
801 | { | |
802 | const char *propname; | |
803 | Error *local_err = NULL; | |
804 | ||
805 | propname = va_arg(vargs, char *); | |
806 | while (propname != NULL) { | |
807 | const char *value = va_arg(vargs, char *); | |
808 | ||
809 | g_assert(value != NULL); | |
778a2dc5 | 810 | if (!object_property_parse(obj, propname, value, &local_err)) { |
a31bdae5 DB |
811 | error_propagate(errp, local_err); |
812 | return -1; | |
813 | } | |
814 | propname = va_arg(vargs, char *); | |
815 | } | |
816 | ||
817 | return 0; | |
818 | } | |
819 | ||
820 | ||
2f28d2ff AL |
821 | Object *object_dynamic_cast(Object *obj, const char *typename) |
822 | { | |
b7f43fe4 | 823 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
acc4af3f PB |
824 | return obj; |
825 | } | |
826 | ||
2f28d2ff AL |
827 | return NULL; |
828 | } | |
829 | ||
be17f18b PB |
830 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
831 | const char *file, int line, const char *func) | |
2f28d2ff | 832 | { |
fa131d94 PB |
833 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
834 | typename, file, line, func); | |
835 | ||
3556c233 | 836 | #ifdef CONFIG_QOM_CAST_DEBUG |
03587328 AL |
837 | int i; |
838 | Object *inst; | |
839 | ||
95916abc | 840 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd | 841 | if (atomic_read(&obj->class->object_cast_cache[i]) == typename) { |
03587328 AL |
842 | goto out; |
843 | } | |
844 | } | |
845 | ||
846 | inst = object_dynamic_cast(obj, typename); | |
2f28d2ff | 847 | |
b7f43fe4 | 848 | if (!inst && obj) { |
be17f18b PB |
849 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
850 | file, line, func, obj, typename); | |
2f28d2ff AL |
851 | abort(); |
852 | } | |
853 | ||
3556c233 | 854 | assert(obj == inst); |
03587328 | 855 | |
95916abc | 856 | if (obj && obj == inst) { |
03587328 | 857 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd AB |
858 | atomic_set(&obj->class->object_cast_cache[i - 1], |
859 | atomic_read(&obj->class->object_cast_cache[i])); | |
03587328 | 860 | } |
b6b3ccfd | 861 | atomic_set(&obj->class->object_cast_cache[i - 1], typename); |
03587328 AL |
862 | } |
863 | ||
864 | out: | |
3556c233 PB |
865 | #endif |
866 | return obj; | |
2f28d2ff AL |
867 | } |
868 | ||
869 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
870 | const char *typename) | |
871 | { | |
33e95c63 | 872 | ObjectClass *ret = NULL; |
bf0fda34 PB |
873 | TypeImpl *target_type; |
874 | TypeImpl *type; | |
2f28d2ff | 875 | |
bf0fda34 PB |
876 | if (!class) { |
877 | return NULL; | |
878 | } | |
879 | ||
793c96b5 | 880 | /* A simple fast path that can trigger a lot for leaf classes. */ |
bf0fda34 | 881 | type = class->type; |
793c96b5 PB |
882 | if (type->name == typename) { |
883 | return class; | |
884 | } | |
885 | ||
bf0fda34 | 886 | target_type = type_get_by_name(typename); |
9ab880b3 AG |
887 | if (!target_type) { |
888 | /* target class type unknown, so fail the cast */ | |
889 | return NULL; | |
890 | } | |
891 | ||
00e2ceae PC |
892 | if (type->class->interfaces && |
893 | type_is_ancestor(target_type, type_interface)) { | |
33e95c63 AL |
894 | int found = 0; |
895 | GSList *i; | |
2f28d2ff | 896 | |
33e95c63 AL |
897 | for (i = class->interfaces; i; i = i->next) { |
898 | ObjectClass *target_class = i->data; | |
899 | ||
900 | if (type_is_ancestor(target_class->type, target_type)) { | |
901 | ret = target_class; | |
902 | found++; | |
903 | } | |
904 | } | |
905 | ||
906 | /* The match was ambiguous, don't allow a cast */ | |
907 | if (found > 1) { | |
908 | ret = NULL; | |
909 | } | |
910 | } else if (type_is_ancestor(type, target_type)) { | |
911 | ret = class; | |
2f28d2ff AL |
912 | } |
913 | ||
33e95c63 | 914 | return ret; |
2f28d2ff AL |
915 | } |
916 | ||
917 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
be17f18b PB |
918 | const char *typename, |
919 | const char *file, int line, | |
920 | const char *func) | |
2f28d2ff | 921 | { |
fa131d94 PB |
922 | ObjectClass *ret; |
923 | ||
924 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", | |
925 | typename, file, line, func); | |
2f28d2ff | 926 | |
03587328 AL |
927 | #ifdef CONFIG_QOM_CAST_DEBUG |
928 | int i; | |
929 | ||
9d6a3d58 | 930 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd | 931 | if (atomic_read(&class->class_cast_cache[i]) == typename) { |
03587328 AL |
932 | ret = class; |
933 | goto out; | |
934 | } | |
935 | } | |
936 | #else | |
9d6a3d58 | 937 | if (!class || !class->interfaces) { |
3556c233 PB |
938 | return class; |
939 | } | |
940 | #endif | |
941 | ||
fa131d94 | 942 | ret = object_class_dynamic_cast(class, typename); |
bf0fda34 | 943 | if (!ret && class) { |
be17f18b PB |
944 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
945 | file, line, func, class, typename); | |
2f28d2ff AL |
946 | abort(); |
947 | } | |
948 | ||
03587328 | 949 | #ifdef CONFIG_QOM_CAST_DEBUG |
9d6a3d58 | 950 | if (class && ret == class) { |
03587328 | 951 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd AB |
952 | atomic_set(&class->class_cast_cache[i - 1], |
953 | atomic_read(&class->class_cast_cache[i])); | |
03587328 | 954 | } |
b6b3ccfd | 955 | atomic_set(&class->class_cast_cache[i - 1], typename); |
03587328 AL |
956 | } |
957 | out: | |
958 | #endif | |
2f28d2ff AL |
959 | return ret; |
960 | } | |
961 | ||
8f5d58ef | 962 | const char *object_get_typename(const Object *obj) |
2f28d2ff AL |
963 | { |
964 | return obj->class->type->name; | |
965 | } | |
966 | ||
967 | ObjectClass *object_get_class(Object *obj) | |
968 | { | |
969 | return obj->class; | |
970 | } | |
971 | ||
17862378 AF |
972 | bool object_class_is_abstract(ObjectClass *klass) |
973 | { | |
974 | return klass->type->abstract; | |
975 | } | |
976 | ||
2f28d2ff AL |
977 | const char *object_class_get_name(ObjectClass *klass) |
978 | { | |
979 | return klass->type->name; | |
980 | } | |
981 | ||
982 | ObjectClass *object_class_by_name(const char *typename) | |
983 | { | |
984 | TypeImpl *type = type_get_by_name(typename); | |
985 | ||
986 | if (!type) { | |
987 | return NULL; | |
988 | } | |
989 | ||
ac451033 | 990 | type_initialize(type); |
2f28d2ff AL |
991 | |
992 | return type->class; | |
993 | } | |
994 | ||
0f8198f1 GH |
995 | ObjectClass *module_object_class_by_name(const char *typename) |
996 | { | |
997 | ObjectClass *oc; | |
998 | ||
999 | oc = object_class_by_name(typename); | |
1000 | #ifdef CONFIG_MODULES | |
1001 | if (!oc) { | |
1002 | module_load_qom_one(typename); | |
1003 | oc = object_class_by_name(typename); | |
1004 | } | |
1005 | #endif | |
1006 | return oc; | |
1007 | } | |
1008 | ||
e7cce67f PB |
1009 | ObjectClass *object_class_get_parent(ObjectClass *class) |
1010 | { | |
1011 | TypeImpl *type = type_get_parent(class->type); | |
1012 | ||
1013 | if (!type) { | |
1014 | return NULL; | |
1015 | } | |
1016 | ||
1017 | type_initialize(type); | |
1018 | ||
1019 | return type->class; | |
1020 | } | |
1021 | ||
2f28d2ff AL |
1022 | typedef struct OCFData |
1023 | { | |
1024 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
1025 | const char *implements_type; |
1026 | bool include_abstract; | |
2f28d2ff AL |
1027 | void *opaque; |
1028 | } OCFData; | |
1029 | ||
1030 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
1031 | gpointer opaque) | |
1032 | { | |
1033 | OCFData *data = opaque; | |
1034 | TypeImpl *type = value; | |
93c511a1 | 1035 | ObjectClass *k; |
2f28d2ff | 1036 | |
ac451033 | 1037 | type_initialize(type); |
93c511a1 | 1038 | k = type->class; |
2f28d2ff | 1039 | |
93c511a1 AL |
1040 | if (!data->include_abstract && type->abstract) { |
1041 | return; | |
1042 | } | |
1043 | ||
1044 | if (data->implements_type && | |
1045 | !object_class_dynamic_cast(k, data->implements_type)) { | |
1046 | return; | |
1047 | } | |
1048 | ||
1049 | data->fn(k, data->opaque); | |
2f28d2ff AL |
1050 | } |
1051 | ||
1052 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 1053 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
1054 | void *opaque) |
1055 | { | |
93c511a1 | 1056 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff | 1057 | |
f54c19ca | 1058 | enumerating_types = true; |
2f28d2ff | 1059 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
f54c19ca | 1060 | enumerating_types = false; |
2f28d2ff | 1061 | } |
57c9fafe | 1062 | |
d714b8de PC |
1063 | static int do_object_child_foreach(Object *obj, |
1064 | int (*fn)(Object *child, void *opaque), | |
1065 | void *opaque, bool recurse) | |
32efc535 | 1066 | { |
b604a854 PF |
1067 | GHashTableIter iter; |
1068 | ObjectProperty *prop; | |
32efc535 PB |
1069 | int ret = 0; |
1070 | ||
b604a854 PF |
1071 | g_hash_table_iter_init(&iter, obj->properties); |
1072 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
32efc535 | 1073 | if (object_property_is_child(prop)) { |
d714b8de PC |
1074 | Object *child = prop->opaque; |
1075 | ||
1076 | ret = fn(child, opaque); | |
32efc535 PB |
1077 | if (ret != 0) { |
1078 | break; | |
1079 | } | |
d714b8de | 1080 | if (recurse) { |
4a39181d CLG |
1081 | ret = do_object_child_foreach(child, fn, opaque, true); |
1082 | if (ret != 0) { | |
1083 | break; | |
1084 | } | |
d714b8de | 1085 | } |
32efc535 PB |
1086 | } |
1087 | } | |
1088 | return ret; | |
1089 | } | |
1090 | ||
d714b8de PC |
1091 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
1092 | void *opaque) | |
1093 | { | |
1094 | return do_object_child_foreach(obj, fn, opaque, false); | |
1095 | } | |
1096 | ||
1097 | int object_child_foreach_recursive(Object *obj, | |
1098 | int (*fn)(Object *child, void *opaque), | |
1099 | void *opaque) | |
1100 | { | |
1101 | return do_object_child_foreach(obj, fn, opaque, true); | |
1102 | } | |
1103 | ||
418ba9e5 AF |
1104 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
1105 | { | |
1106 | GSList **list = opaque; | |
1107 | ||
1108 | *list = g_slist_prepend(*list, klass); | |
1109 | } | |
1110 | ||
1111 | GSList *object_class_get_list(const char *implements_type, | |
1112 | bool include_abstract) | |
1113 | { | |
1114 | GSList *list = NULL; | |
1115 | ||
1116 | object_class_foreach(object_class_get_list_tramp, | |
1117 | implements_type, include_abstract, &list); | |
1118 | return list; | |
1119 | } | |
1120 | ||
47c66009 PB |
1121 | static gint object_class_cmp(gconstpointer a, gconstpointer b) |
1122 | { | |
1123 | return strcasecmp(object_class_get_name((ObjectClass *)a), | |
1124 | object_class_get_name((ObjectClass *)b)); | |
1125 | } | |
1126 | ||
1127 | GSList *object_class_get_list_sorted(const char *implements_type, | |
1128 | bool include_abstract) | |
1129 | { | |
1130 | return g_slist_sort(object_class_get_list(implements_type, include_abstract), | |
1131 | object_class_cmp); | |
1132 | } | |
1133 | ||
b77ade9b | 1134 | Object *object_ref(Object *obj) |
57c9fafe | 1135 | { |
8ffad850 | 1136 | if (!obj) { |
b77ade9b | 1137 | return NULL; |
8ffad850 | 1138 | } |
8438a135 | 1139 | atomic_inc(&obj->ref); |
b77ade9b | 1140 | return obj; |
57c9fafe AL |
1141 | } |
1142 | ||
1143 | void object_unref(Object *obj) | |
1144 | { | |
8ffad850 PC |
1145 | if (!obj) { |
1146 | return; | |
1147 | } | |
719a3077 | 1148 | g_assert(obj->ref > 0); |
57c9fafe AL |
1149 | |
1150 | /* parent always holds a reference to its children */ | |
f08c03f3 | 1151 | if (atomic_fetch_dec(&obj->ref) == 1) { |
57c9fafe AL |
1152 | object_finalize(obj); |
1153 | } | |
1154 | } | |
1155 | ||
d2623129 MA |
1156 | static ObjectProperty * |
1157 | object_property_try_add(Object *obj, const char *name, const char *type, | |
1158 | ObjectPropertyAccessor *get, | |
1159 | ObjectPropertyAccessor *set, | |
1160 | ObjectPropertyRelease *release, | |
1161 | void *opaque, Error **errp) | |
57c9fafe | 1162 | { |
54852b03 | 1163 | ObjectProperty *prop; |
33965904 PC |
1164 | size_t name_len = strlen(name); |
1165 | ||
1166 | if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { | |
1167 | int i; | |
1168 | ObjectProperty *ret; | |
1169 | char *name_no_array = g_strdup(name); | |
1170 | ||
1171 | name_no_array[name_len - 3] = '\0'; | |
1172 | for (i = 0; ; ++i) { | |
1173 | char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); | |
1174 | ||
d2623129 MA |
1175 | ret = object_property_try_add(obj, full_name, type, get, set, |
1176 | release, opaque, NULL); | |
33965904 PC |
1177 | g_free(full_name); |
1178 | if (ret) { | |
1179 | break; | |
1180 | } | |
1181 | } | |
1182 | g_free(name_no_array); | |
1183 | return ret; | |
1184 | } | |
54852b03 | 1185 | |
16bf7f52 | 1186 | if (object_property_find(obj, name, NULL) != NULL) { |
d55e937d GK |
1187 | error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')", |
1188 | name, object_get_typename(obj)); | |
b604a854 | 1189 | return NULL; |
54852b03 PM |
1190 | } |
1191 | ||
1192 | prop = g_malloc0(sizeof(*prop)); | |
57c9fafe AL |
1193 | |
1194 | prop->name = g_strdup(name); | |
1195 | prop->type = g_strdup(type); | |
1196 | ||
1197 | prop->get = get; | |
1198 | prop->set = set; | |
1199 | prop->release = release; | |
1200 | prop->opaque = opaque; | |
1201 | ||
b604a854 | 1202 | g_hash_table_insert(obj->properties, prop->name, prop); |
64607d08 | 1203 | return prop; |
57c9fafe AL |
1204 | } |
1205 | ||
d2623129 MA |
1206 | ObjectProperty * |
1207 | object_property_add(Object *obj, const char *name, const char *type, | |
1208 | ObjectPropertyAccessor *get, | |
1209 | ObjectPropertyAccessor *set, | |
1210 | ObjectPropertyRelease *release, | |
1211 | void *opaque) | |
1212 | { | |
1213 | return object_property_try_add(obj, name, type, get, set, release, | |
1214 | opaque, &error_abort); | |
1215 | } | |
1216 | ||
16bf7f52 DB |
1217 | ObjectProperty * |
1218 | object_class_property_add(ObjectClass *klass, | |
1219 | const char *name, | |
1220 | const char *type, | |
1221 | ObjectPropertyAccessor *get, | |
1222 | ObjectPropertyAccessor *set, | |
1223 | ObjectPropertyRelease *release, | |
d2623129 | 1224 | void *opaque) |
16bf7f52 DB |
1225 | { |
1226 | ObjectProperty *prop; | |
1227 | ||
d2623129 | 1228 | assert(!object_class_property_find(klass, name, NULL)); |
16bf7f52 DB |
1229 | |
1230 | prop = g_malloc0(sizeof(*prop)); | |
1231 | ||
1232 | prop->name = g_strdup(name); | |
1233 | prop->type = g_strdup(type); | |
1234 | ||
1235 | prop->get = get; | |
1236 | prop->set = set; | |
1237 | prop->release = release; | |
1238 | prop->opaque = opaque; | |
1239 | ||
ba806ffb | 1240 | g_hash_table_insert(klass->properties, prop->name, prop); |
16bf7f52 DB |
1241 | |
1242 | return prop; | |
1243 | } | |
1244 | ||
89bfe000 PB |
1245 | ObjectProperty *object_property_find(Object *obj, const char *name, |
1246 | Error **errp) | |
57c9fafe AL |
1247 | { |
1248 | ObjectProperty *prop; | |
16bf7f52 DB |
1249 | ObjectClass *klass = object_get_class(obj); |
1250 | ||
1251 | prop = object_class_property_find(klass, name, NULL); | |
1252 | if (prop) { | |
1253 | return prop; | |
1254 | } | |
57c9fafe | 1255 | |
b604a854 PF |
1256 | prop = g_hash_table_lookup(obj->properties, name); |
1257 | if (prop) { | |
1258 | return prop; | |
57c9fafe AL |
1259 | } |
1260 | ||
f231b88d | 1261 | error_setg(errp, "Property '.%s' not found", name); |
57c9fafe AL |
1262 | return NULL; |
1263 | } | |
1264 | ||
7746abd8 DB |
1265 | void object_property_iter_init(ObjectPropertyIterator *iter, |
1266 | Object *obj) | |
a00c9482 | 1267 | { |
7746abd8 DB |
1268 | g_hash_table_iter_init(&iter->iter, obj->properties); |
1269 | iter->nextclass = object_get_class(obj); | |
a00c9482 DB |
1270 | } |
1271 | ||
1272 | ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter) | |
1273 | { | |
b604a854 | 1274 | gpointer key, val; |
16bf7f52 DB |
1275 | while (!g_hash_table_iter_next(&iter->iter, &key, &val)) { |
1276 | if (!iter->nextclass) { | |
1277 | return NULL; | |
1278 | } | |
1279 | g_hash_table_iter_init(&iter->iter, iter->nextclass->properties); | |
1280 | iter->nextclass = object_class_get_parent(iter->nextclass); | |
a00c9482 | 1281 | } |
b604a854 | 1282 | return val; |
a00c9482 DB |
1283 | } |
1284 | ||
961c47bb AK |
1285 | void object_class_property_iter_init(ObjectPropertyIterator *iter, |
1286 | ObjectClass *klass) | |
1287 | { | |
1288 | g_hash_table_iter_init(&iter->iter, klass->properties); | |
684546d8 | 1289 | iter->nextclass = object_class_get_parent(klass); |
961c47bb AK |
1290 | } |
1291 | ||
16bf7f52 DB |
1292 | ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, |
1293 | Error **errp) | |
1294 | { | |
1295 | ObjectProperty *prop; | |
1296 | ObjectClass *parent_klass; | |
1297 | ||
1298 | parent_klass = object_class_get_parent(klass); | |
1299 | if (parent_klass) { | |
1300 | prop = object_class_property_find(parent_klass, name, NULL); | |
1301 | if (prop) { | |
1302 | return prop; | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | prop = g_hash_table_lookup(klass->properties, name); | |
1307 | if (!prop) { | |
1308 | error_setg(errp, "Property '.%s' not found", name); | |
1309 | } | |
1310 | return prop; | |
1311 | } | |
1312 | ||
df4fe0b2 | 1313 | void object_property_del(Object *obj, const char *name) |
57c9fafe | 1314 | { |
b604a854 PF |
1315 | ObjectProperty *prop = g_hash_table_lookup(obj->properties, name); |
1316 | ||
0866aca1 AL |
1317 | if (prop->release) { |
1318 | prop->release(obj, name, prop->opaque); | |
1319 | } | |
b604a854 | 1320 | g_hash_table_remove(obj->properties, name); |
57c9fafe AL |
1321 | } |
1322 | ||
6fd5bef1 | 1323 | bool object_property_get(Object *obj, const char *name, Visitor *v, |
57c9fafe AL |
1324 | Error **errp) |
1325 | { | |
6fd5bef1 | 1326 | Error *err = NULL; |
89bfe000 | 1327 | ObjectProperty *prop = object_property_find(obj, name, errp); |
6fd5bef1 | 1328 | |
57c9fafe | 1329 | if (prop == NULL) { |
6fd5bef1 | 1330 | return false; |
57c9fafe AL |
1331 | } |
1332 | ||
1333 | if (!prop->get) { | |
c6bd8c70 | 1334 | error_setg(errp, QERR_PERMISSION_DENIED); |
6fd5bef1 | 1335 | return false; |
57c9fafe | 1336 | } |
6fd5bef1 MA |
1337 | prop->get(obj, v, name, prop->opaque, &err); |
1338 | error_propagate(errp, err); | |
1339 | return !err; | |
57c9fafe AL |
1340 | } |
1341 | ||
6fd5bef1 | 1342 | bool object_property_set(Object *obj, const char *name, Visitor *v, |
57c9fafe AL |
1343 | Error **errp) |
1344 | { | |
6fd5bef1 | 1345 | Error *err = NULL; |
89bfe000 | 1346 | ObjectProperty *prop = object_property_find(obj, name, errp); |
6fd5bef1 | 1347 | |
57c9fafe | 1348 | if (prop == NULL) { |
6fd5bef1 | 1349 | return false; |
57c9fafe AL |
1350 | } |
1351 | ||
1352 | if (!prop->set) { | |
c6bd8c70 | 1353 | error_setg(errp, QERR_PERMISSION_DENIED); |
6fd5bef1 | 1354 | return false; |
57c9fafe | 1355 | } |
6fd5bef1 MA |
1356 | prop->set(obj, v, name, prop->opaque, &err); |
1357 | error_propagate(errp, err); | |
1358 | return !err; | |
57c9fafe AL |
1359 | } |
1360 | ||
6fd5bef1 | 1361 | bool object_property_set_str(Object *obj, const char *name, |
5325cc34 | 1362 | const char *value, Error **errp) |
7b7b7d18 PB |
1363 | { |
1364 | QString *qstr = qstring_from_str(value); | |
6fd5bef1 | 1365 | bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp); |
7b7b7d18 | 1366 | |
cb3e7f08 | 1367 | qobject_unref(qstr); |
6fd5bef1 | 1368 | return ok; |
7b7b7d18 PB |
1369 | } |
1370 | ||
1371 | char *object_property_get_str(Object *obj, const char *name, | |
1372 | Error **errp) | |
1373 | { | |
1374 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
7b7b7d18 PB |
1375 | char *retval; |
1376 | ||
1377 | if (!ret) { | |
1378 | return NULL; | |
1379 | } | |
aafb21a0 PX |
1380 | |
1381 | retval = g_strdup(qobject_get_try_str(ret)); | |
1382 | if (!retval) { | |
c6bd8c70 | 1383 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
7b7b7d18 PB |
1384 | } |
1385 | ||
cb3e7f08 | 1386 | qobject_unref(ret); |
7b7b7d18 PB |
1387 | return retval; |
1388 | } | |
1389 | ||
6fd5bef1 | 1390 | bool object_property_set_link(Object *obj, const char *name, |
5325cc34 | 1391 | Object *value, Error **errp) |
1d9c5a12 | 1392 | { |
6fd5bef1 MA |
1393 | g_autofree char *path = NULL; |
1394 | ||
d3c49316 | 1395 | if (value) { |
6fd5bef1 | 1396 | path = object_get_canonical_path(value); |
d3c49316 | 1397 | } |
6fd5bef1 | 1398 | return object_property_set_str(obj, name, path ?: "", errp); |
1d9c5a12 PB |
1399 | } |
1400 | ||
1401 | Object *object_property_get_link(Object *obj, const char *name, | |
1402 | Error **errp) | |
1403 | { | |
1404 | char *str = object_property_get_str(obj, name, errp); | |
1405 | Object *target = NULL; | |
1406 | ||
1407 | if (str && *str) { | |
1408 | target = object_resolve_path(str, NULL); | |
1409 | if (!target) { | |
75158ebb MA |
1410 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1411 | "Device '%s' not found", str); | |
1d9c5a12 PB |
1412 | } |
1413 | } | |
1414 | ||
1415 | g_free(str); | |
1416 | return target; | |
1417 | } | |
1418 | ||
6fd5bef1 | 1419 | bool object_property_set_bool(Object *obj, const char *name, |
5325cc34 | 1420 | bool value, Error **errp) |
7b7b7d18 | 1421 | { |
fc48ffc3 | 1422 | QBool *qbool = qbool_from_bool(value); |
6fd5bef1 | 1423 | bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp); |
7b7b7d18 | 1424 | |
cb3e7f08 | 1425 | qobject_unref(qbool); |
6fd5bef1 | 1426 | return ok; |
7b7b7d18 PB |
1427 | } |
1428 | ||
1429 | bool object_property_get_bool(Object *obj, const char *name, | |
1430 | Error **errp) | |
1431 | { | |
1432 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1433 | QBool *qbool; | |
1434 | bool retval; | |
1435 | ||
1436 | if (!ret) { | |
1437 | return false; | |
1438 | } | |
7dc847eb | 1439 | qbool = qobject_to(QBool, ret); |
7b7b7d18 | 1440 | if (!qbool) { |
c6bd8c70 | 1441 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
7b7b7d18 PB |
1442 | retval = false; |
1443 | } else { | |
fc48ffc3 | 1444 | retval = qbool_get_bool(qbool); |
7b7b7d18 PB |
1445 | } |
1446 | ||
cb3e7f08 | 1447 | qobject_unref(ret); |
7b7b7d18 PB |
1448 | return retval; |
1449 | } | |
1450 | ||
6fd5bef1 | 1451 | bool object_property_set_int(Object *obj, const char *name, |
5325cc34 | 1452 | int64_t value, Error **errp) |
7b7b7d18 | 1453 | { |
01b2ffce | 1454 | QNum *qnum = qnum_from_int(value); |
6fd5bef1 | 1455 | bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp); |
7b7b7d18 | 1456 | |
cb3e7f08 | 1457 | qobject_unref(qnum); |
6fd5bef1 | 1458 | return ok; |
7b7b7d18 PB |
1459 | } |
1460 | ||
1461 | int64_t object_property_get_int(Object *obj, const char *name, | |
1462 | Error **errp) | |
1463 | { | |
1464 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
01b2ffce | 1465 | QNum *qnum; |
7b7b7d18 PB |
1466 | int64_t retval; |
1467 | ||
1468 | if (!ret) { | |
1469 | return -1; | |
1470 | } | |
01b2ffce | 1471 | |
7dc847eb | 1472 | qnum = qobject_to(QNum, ret); |
01b2ffce | 1473 | if (!qnum || !qnum_get_try_int(qnum, &retval)) { |
c6bd8c70 | 1474 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
7b7b7d18 | 1475 | retval = -1; |
7b7b7d18 PB |
1476 | } |
1477 | ||
cb3e7f08 | 1478 | qobject_unref(ret); |
7b7b7d18 | 1479 | return retval; |
b2cd7dee PB |
1480 | } |
1481 | ||
0e76ed0a MAL |
1482 | static void object_property_init_defval(Object *obj, ObjectProperty *prop) |
1483 | { | |
1484 | Visitor *v = qobject_input_visitor_new(prop->defval); | |
1485 | ||
1486 | assert(prop->set != NULL); | |
1487 | prop->set(obj, v, prop->name, prop->opaque, &error_abort); | |
1488 | ||
1489 | visit_free(v); | |
1490 | } | |
1491 | ||
1492 | static void object_property_set_default(ObjectProperty *prop, QObject *defval) | |
1493 | { | |
1494 | assert(!prop->defval); | |
1495 | assert(!prop->init); | |
1496 | ||
1497 | prop->defval = defval; | |
1498 | prop->init = object_property_init_defval; | |
1499 | } | |
1500 | ||
1501 | void object_property_set_default_bool(ObjectProperty *prop, bool value) | |
1502 | { | |
1503 | object_property_set_default(prop, QOBJECT(qbool_from_bool(value))); | |
1504 | } | |
1505 | ||
1506 | void object_property_set_default_str(ObjectProperty *prop, const char *value) | |
1507 | { | |
1508 | object_property_set_default(prop, QOBJECT(qstring_from_str(value))); | |
1509 | } | |
1510 | ||
1511 | void object_property_set_default_int(ObjectProperty *prop, int64_t value) | |
1512 | { | |
1513 | object_property_set_default(prop, QOBJECT(qnum_from_int(value))); | |
1514 | } | |
1515 | ||
1516 | void object_property_set_default_uint(ObjectProperty *prop, uint64_t value) | |
1517 | { | |
1518 | object_property_set_default(prop, QOBJECT(qnum_from_uint(value))); | |
1519 | } | |
1520 | ||
6fd5bef1 | 1521 | bool object_property_set_uint(Object *obj, const char *name, |
5325cc34 | 1522 | uint64_t value, Error **errp) |
3152779c MAL |
1523 | { |
1524 | QNum *qnum = qnum_from_uint(value); | |
6fd5bef1 | 1525 | bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp); |
3152779c | 1526 | |
cb3e7f08 | 1527 | qobject_unref(qnum); |
6fd5bef1 | 1528 | return ok; |
3152779c MAL |
1529 | } |
1530 | ||
1531 | uint64_t object_property_get_uint(Object *obj, const char *name, | |
1532 | Error **errp) | |
1533 | { | |
1534 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1535 | QNum *qnum; | |
1536 | uint64_t retval; | |
1537 | ||
1538 | if (!ret) { | |
1539 | return 0; | |
1540 | } | |
7dc847eb | 1541 | qnum = qobject_to(QNum, ret); |
3152779c MAL |
1542 | if (!qnum || !qnum_get_try_uint(qnum, &retval)) { |
1543 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint"); | |
1544 | retval = 0; | |
1545 | } | |
1546 | ||
cb3e7f08 | 1547 | qobject_unref(ret); |
3152779c MAL |
1548 | return retval; |
1549 | } | |
1550 | ||
a8e3fbed | 1551 | typedef struct EnumProperty { |
f7abe0ec | 1552 | const QEnumLookup *lookup; |
a8e3fbed DB |
1553 | int (*get)(Object *, Error **); |
1554 | void (*set)(Object *, int, Error **); | |
1555 | } EnumProperty; | |
1556 | ||
1f21772d | 1557 | int object_property_get_enum(Object *obj, const char *name, |
a3590dac | 1558 | const char *typename, Error **errp) |
1f21772d | 1559 | { |
976620ac | 1560 | char *str; |
1f21772d | 1561 | int ret; |
a3590dac DB |
1562 | ObjectProperty *prop = object_property_find(obj, name, errp); |
1563 | EnumProperty *enumprop; | |
1564 | ||
1565 | if (prop == NULL) { | |
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | if (!g_str_equal(prop->type, typename)) { | |
1570 | error_setg(errp, "Property %s on %s is not '%s' enum type", | |
1571 | name, object_class_get_name( | |
1572 | object_get_class(obj)), typename); | |
1573 | return 0; | |
1574 | } | |
1575 | ||
1576 | enumprop = prop->opaque; | |
1f21772d | 1577 | |
b555f89f MA |
1578 | str = object_property_get_str(obj, name, errp); |
1579 | if (!str) { | |
4715d42e MA |
1580 | return 0; |
1581 | } | |
976620ac | 1582 | |
ea097dff | 1583 | ret = qapi_enum_parse(enumprop->lookup, str, -1, errp); |
976620ac | 1584 | g_free(str); |
1f21772d HT |
1585 | |
1586 | return ret; | |
1587 | } | |
1588 | ||
6fd5bef1 | 1589 | bool object_property_parse(Object *obj, const char *name, |
5325cc34 | 1590 | const char *string, Error **errp) |
b2cd7dee | 1591 | { |
7a0525c7 | 1592 | Visitor *v = string_input_visitor_new(string); |
6fd5bef1 MA |
1593 | bool ok = object_property_set(obj, name, v, errp); |
1594 | ||
7a0525c7 | 1595 | visit_free(v); |
6fd5bef1 | 1596 | return ok; |
b2cd7dee PB |
1597 | } |
1598 | ||
0b7593e0 | 1599 | char *object_property_print(Object *obj, const char *name, bool human, |
b2cd7dee PB |
1600 | Error **errp) |
1601 | { | |
3b098d56 | 1602 | Visitor *v; |
3a53009f GA |
1603 | char *string = NULL; |
1604 | Error *local_err = NULL; | |
b2cd7dee | 1605 | |
3b098d56 | 1606 | v = string_output_visitor_new(human, &string); |
778a2dc5 | 1607 | if (!object_property_get(obj, name, v, &local_err)) { |
3a53009f GA |
1608 | error_propagate(errp, local_err); |
1609 | goto out; | |
1610 | } | |
1611 | ||
3b098d56 | 1612 | visit_complete(v, &string); |
3a53009f GA |
1613 | |
1614 | out: | |
3b098d56 | 1615 | visit_free(v); |
b2cd7dee | 1616 | return string; |
7b7b7d18 PB |
1617 | } |
1618 | ||
57c9fafe AL |
1619 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
1620 | { | |
89bfe000 | 1621 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1622 | if (prop == NULL) { |
57c9fafe AL |
1623 | return NULL; |
1624 | } | |
1625 | ||
1626 | return prop->type; | |
1627 | } | |
1628 | ||
1629 | Object *object_get_root(void) | |
1630 | { | |
8b45d447 | 1631 | static Object *root; |
57c9fafe | 1632 | |
8b45d447 AL |
1633 | if (!root) { |
1634 | root = object_new("container"); | |
57c9fafe AL |
1635 | } |
1636 | ||
8b45d447 | 1637 | return root; |
57c9fafe AL |
1638 | } |
1639 | ||
bc2256c4 DB |
1640 | Object *object_get_objects_root(void) |
1641 | { | |
1642 | return container_get(object_get_root(), "/objects"); | |
1643 | } | |
1644 | ||
7c47c4ea PX |
1645 | Object *object_get_internal_root(void) |
1646 | { | |
1647 | static Object *internal_root; | |
1648 | ||
1649 | if (!internal_root) { | |
1650 | internal_root = object_new("container"); | |
1651 | } | |
1652 | ||
1653 | return internal_root; | |
1654 | } | |
1655 | ||
d7bce999 EB |
1656 | static void object_get_child_property(Object *obj, Visitor *v, |
1657 | const char *name, void *opaque, | |
1658 | Error **errp) | |
57c9fafe AL |
1659 | { |
1660 | Object *child = opaque; | |
ddfb0baa | 1661 | char *path; |
57c9fafe AL |
1662 | |
1663 | path = object_get_canonical_path(child); | |
51e72bc1 | 1664 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1665 | g_free(path); |
1666 | } | |
1667 | ||
ddfb0baa MA |
1668 | static Object *object_resolve_child_property(Object *parent, void *opaque, |
1669 | const char *part) | |
64607d08 PB |
1670 | { |
1671 | return opaque; | |
1672 | } | |
1673 | ||
db85b575 AL |
1674 | static void object_finalize_child_property(Object *obj, const char *name, |
1675 | void *opaque) | |
1676 | { | |
1677 | Object *child = opaque; | |
1678 | ||
bffc687d PB |
1679 | if (child->class->unparent) { |
1680 | (child->class->unparent)(child); | |
1681 | } | |
1682 | child->parent = NULL; | |
db85b575 AL |
1683 | object_unref(child); |
1684 | } | |
1685 | ||
70251887 MA |
1686 | ObjectProperty * |
1687 | object_property_add_child(Object *obj, const char *name, | |
d2623129 | 1688 | Object *child) |
57c9fafe | 1689 | { |
70251887 | 1690 | g_autofree char *type = NULL; |
64607d08 | 1691 | ObjectProperty *op; |
57c9fafe | 1692 | |
d2623129 | 1693 | assert(!child->parent); |
8faa2f85 | 1694 | |
688ffbb4 | 1695 | type = g_strdup_printf("child<%s>", object_get_typename(child)); |
57c9fafe | 1696 | |
64607d08 | 1697 | op = object_property_add(obj, name, type, object_get_child_property, NULL, |
d2623129 | 1698 | object_finalize_child_property, child); |
64607d08 | 1699 | op->resolve = object_resolve_child_property; |
57c9fafe | 1700 | object_ref(child); |
57c9fafe | 1701 | child->parent = obj; |
70251887 | 1702 | return op; |
57c9fafe AL |
1703 | } |
1704 | ||
8f5d58ef | 1705 | void object_property_allow_set_link(const Object *obj, const char *name, |
39f72ef9 SH |
1706 | Object *val, Error **errp) |
1707 | { | |
1708 | /* Allow the link to be set, always */ | |
1709 | } | |
1710 | ||
9561fda8 | 1711 | typedef struct { |
9941d37b MAL |
1712 | union { |
1713 | Object **targetp; | |
1714 | Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */ | |
840ecdfb | 1715 | ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */ |
9941d37b | 1716 | }; |
8f5d58ef | 1717 | void (*check)(const Object *, const char *, Object *, Error **); |
9561fda8 SH |
1718 | ObjectPropertyLinkFlags flags; |
1719 | } LinkProperty; | |
1720 | ||
9941d37b MAL |
1721 | static Object ** |
1722 | object_link_get_targetp(Object *obj, LinkProperty *lprop) | |
1723 | { | |
1724 | if (lprop->flags & OBJ_PROP_LINK_DIRECT) { | |
1725 | return &lprop->target; | |
840ecdfb MAL |
1726 | } else if (lprop->flags & OBJ_PROP_LINK_CLASS) { |
1727 | return (void *)obj + lprop->offset; | |
9941d37b MAL |
1728 | } else { |
1729 | return lprop->targetp; | |
1730 | } | |
1731 | } | |
1732 | ||
d7bce999 EB |
1733 | static void object_get_link_property(Object *obj, Visitor *v, |
1734 | const char *name, void *opaque, | |
1735 | Error **errp) | |
57c9fafe | 1736 | { |
9561fda8 | 1737 | LinkProperty *lprop = opaque; |
9941d37b | 1738 | Object **targetp = object_link_get_targetp(obj, lprop); |
ddfb0baa | 1739 | char *path; |
57c9fafe | 1740 | |
36854207 MAL |
1741 | if (*targetp) { |
1742 | path = object_get_canonical_path(*targetp); | |
51e72bc1 | 1743 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1744 | g_free(path); |
1745 | } else { | |
ddfb0baa | 1746 | path = (char *)""; |
51e72bc1 | 1747 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1748 | } |
1749 | } | |
1750 | ||
f5ec6704 SH |
1751 | /* |
1752 | * object_resolve_link: | |
1753 | * | |
1754 | * Lookup an object and ensure its type matches the link property type. This | |
1755 | * is similar to object_resolve_path() except type verification against the | |
1756 | * link property is performed. | |
1757 | * | |
1758 | * Returns: The matched object or NULL on path lookup failures. | |
1759 | */ | |
1760 | static Object *object_resolve_link(Object *obj, const char *name, | |
1761 | const char *path, Error **errp) | |
1762 | { | |
1763 | const char *type; | |
ddfb0baa | 1764 | char *target_type; |
f5ec6704 SH |
1765 | bool ambiguous = false; |
1766 | Object *target; | |
1767 | ||
1768 | /* Go from link<FOO> to FOO. */ | |
1769 | type = object_property_get_type(obj, name, NULL); | |
1770 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
1771 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
1772 | ||
1773 | if (ambiguous) { | |
455b0fde EB |
1774 | error_setg(errp, "Path '%s' does not uniquely identify an object", |
1775 | path); | |
f5ec6704 SH |
1776 | } else if (!target) { |
1777 | target = object_resolve_path(path, &ambiguous); | |
1778 | if (target || ambiguous) { | |
c6bd8c70 | 1779 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); |
f5ec6704 | 1780 | } else { |
75158ebb MA |
1781 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1782 | "Device '%s' not found", path); | |
f5ec6704 SH |
1783 | } |
1784 | target = NULL; | |
1785 | } | |
1786 | g_free(target_type); | |
1787 | ||
1788 | return target; | |
1789 | } | |
1790 | ||
d7bce999 EB |
1791 | static void object_set_link_property(Object *obj, Visitor *v, |
1792 | const char *name, void *opaque, | |
1793 | Error **errp) | |
57c9fafe | 1794 | { |
c6aed983 | 1795 | Error *local_err = NULL; |
9561fda8 | 1796 | LinkProperty *prop = opaque; |
9941d37b | 1797 | Object **targetp = object_link_get_targetp(obj, prop); |
36854207 | 1798 | Object *old_target = *targetp; |
1c94a351 | 1799 | Object *new_target; |
c6aed983 | 1800 | char *path = NULL; |
57c9fafe | 1801 | |
1c94a351 MA |
1802 | if (!visit_type_str(v, name, &path, errp)) { |
1803 | return; | |
1804 | } | |
57c9fafe | 1805 | |
1c94a351 MA |
1806 | if (*path) { |
1807 | new_target = object_resolve_link(obj, name, path, errp); | |
1808 | if (!new_target) { | |
1809 | g_free(path); | |
1810 | return; | |
1811 | } | |
1812 | } else { | |
1813 | new_target = NULL; | |
57c9fafe AL |
1814 | } |
1815 | ||
1816 | g_free(path); | |
f0cdc966 | 1817 | |
39f72ef9 SH |
1818 | prop->check(obj, name, new_target, &local_err); |
1819 | if (local_err) { | |
1820 | error_propagate(errp, local_err); | |
1821 | return; | |
1822 | } | |
1823 | ||
36854207 | 1824 | *targetp = new_target; |
8770bafd | 1825 | if (prop->flags & OBJ_PROP_LINK_STRONG) { |
265b578c MAL |
1826 | object_ref(new_target); |
1827 | object_unref(old_target); | |
1828 | } | |
57c9fafe AL |
1829 | } |
1830 | ||
ddfb0baa MA |
1831 | static Object *object_resolve_link_property(Object *parent, void *opaque, |
1832 | const char *part) | |
64607d08 PB |
1833 | { |
1834 | LinkProperty *lprop = opaque; | |
1835 | ||
9941d37b | 1836 | return *object_link_get_targetp(parent, lprop); |
64607d08 PB |
1837 | } |
1838 | ||
9561fda8 SH |
1839 | static void object_release_link_property(Object *obj, const char *name, |
1840 | void *opaque) | |
1841 | { | |
1842 | LinkProperty *prop = opaque; | |
9941d37b | 1843 | Object **targetp = object_link_get_targetp(obj, prop); |
9561fda8 | 1844 | |
9941d37b MAL |
1845 | if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) { |
1846 | object_unref(*targetp); | |
9561fda8 | 1847 | } |
840ecdfb MAL |
1848 | if (!(prop->flags & OBJ_PROP_LINK_CLASS)) { |
1849 | g_free(prop); | |
1850 | } | |
9561fda8 SH |
1851 | } |
1852 | ||
70251887 MA |
1853 | static ObjectProperty * |
1854 | object_add_link_prop(Object *obj, const char *name, | |
1855 | const char *type, void *ptr, | |
1856 | void (*check)(const Object *, const char *, | |
1857 | Object *, Error **), | |
d2623129 | 1858 | ObjectPropertyLinkFlags flags) |
57c9fafe | 1859 | { |
9561fda8 | 1860 | LinkProperty *prop = g_malloc(sizeof(*prop)); |
70251887 | 1861 | g_autofree char *full_type = NULL; |
64607d08 | 1862 | ObjectProperty *op; |
57c9fafe | 1863 | |
4a8d5798 MAL |
1864 | if (flags & OBJ_PROP_LINK_DIRECT) { |
1865 | prop->target = ptr; | |
1866 | } else { | |
1867 | prop->targetp = ptr; | |
1868 | } | |
39f72ef9 | 1869 | prop->check = check; |
9561fda8 SH |
1870 | prop->flags = flags; |
1871 | ||
57c9fafe AL |
1872 | full_type = g_strdup_printf("link<%s>", type); |
1873 | ||
64607d08 PB |
1874 | op = object_property_add(obj, name, full_type, |
1875 | object_get_link_property, | |
1876 | check ? object_set_link_property : NULL, | |
1877 | object_release_link_property, | |
d2623129 | 1878 | prop); |
64607d08 | 1879 | op->resolve = object_resolve_link_property; |
70251887 | 1880 | return op; |
57c9fafe AL |
1881 | } |
1882 | ||
70251887 MA |
1883 | ObjectProperty * |
1884 | object_property_add_link(Object *obj, const char *name, | |
1885 | const char *type, Object **targetp, | |
1886 | void (*check)(const Object *, const char *, | |
1887 | Object *, Error **), | |
d2623129 | 1888 | ObjectPropertyLinkFlags flags) |
4a8d5798 | 1889 | { |
d2623129 | 1890 | return object_add_link_prop(obj, name, type, targetp, check, flags); |
4a8d5798 MAL |
1891 | } |
1892 | ||
840ecdfb MAL |
1893 | ObjectProperty * |
1894 | object_class_property_add_link(ObjectClass *oc, | |
1895 | const char *name, | |
1896 | const char *type, ptrdiff_t offset, | |
1897 | void (*check)(const Object *obj, const char *name, | |
1898 | Object *val, Error **errp), | |
d2623129 | 1899 | ObjectPropertyLinkFlags flags) |
840ecdfb | 1900 | { |
840ecdfb | 1901 | LinkProperty *prop = g_new0(LinkProperty, 1); |
ddfb0baa | 1902 | char *full_type; |
840ecdfb MAL |
1903 | ObjectProperty *op; |
1904 | ||
1905 | prop->offset = offset; | |
1906 | prop->check = check; | |
1907 | prop->flags = flags | OBJ_PROP_LINK_CLASS; | |
1908 | ||
1909 | full_type = g_strdup_printf("link<%s>", type); | |
1910 | ||
1911 | op = object_class_property_add(oc, name, full_type, | |
1912 | object_get_link_property, | |
1913 | check ? object_set_link_property : NULL, | |
1914 | object_release_link_property, | |
d2623129 | 1915 | prop); |
840ecdfb MAL |
1916 | |
1917 | op->resolve = object_resolve_link_property; | |
1918 | ||
840ecdfb MAL |
1919 | g_free(full_type); |
1920 | return op; | |
1921 | } | |
1922 | ||
70251887 MA |
1923 | ObjectProperty * |
1924 | object_property_add_const_link(Object *obj, const char *name, | |
d2623129 | 1925 | Object *target) |
fb9e7e33 | 1926 | { |
70251887 MA |
1927 | return object_add_link_prop(obj, name, |
1928 | object_get_typename(target), target, | |
d2623129 | 1929 | NULL, OBJ_PROP_LINK_DIRECT); |
fb9e7e33 PB |
1930 | } |
1931 | ||
e8512dfa | 1932 | char *object_get_canonical_path_component(const Object *obj) |
11f590b1 SH |
1933 | { |
1934 | ObjectProperty *prop = NULL; | |
b604a854 | 1935 | GHashTableIter iter; |
11f590b1 | 1936 | |
770dec26 PB |
1937 | if (obj->parent == NULL) { |
1938 | return NULL; | |
1939 | } | |
11f590b1 | 1940 | |
b604a854 PF |
1941 | g_hash_table_iter_init(&iter, obj->parent->properties); |
1942 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
11f590b1 SH |
1943 | if (!object_property_is_child(prop)) { |
1944 | continue; | |
1945 | } | |
1946 | ||
1947 | if (prop->opaque == obj) { | |
1948 | return g_strdup(prop->name); | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | /* obj had a parent but was not a child, should never happen */ | |
1953 | g_assert_not_reached(); | |
1954 | return NULL; | |
1955 | } | |
1956 | ||
e8512dfa | 1957 | char *object_get_canonical_path(const Object *obj) |
57c9fafe AL |
1958 | { |
1959 | Object *root = object_get_root(); | |
11f590b1 | 1960 | char *newpath, *path = NULL; |
57c9fafe | 1961 | |
e40077fd PB |
1962 | if (obj == root) { |
1963 | return g_strdup("/"); | |
1964 | } | |
1965 | ||
1966 | do { | |
11f590b1 | 1967 | char *component = object_get_canonical_path_component(obj); |
57c9fafe | 1968 | |
e40077fd PB |
1969 | if (!component) { |
1970 | /* A canonical path must be complete, so discard what was | |
1971 | * collected so far. | |
1972 | */ | |
11f590b1 | 1973 | g_free(path); |
e40077fd | 1974 | return NULL; |
57c9fafe AL |
1975 | } |
1976 | ||
e40077fd PB |
1977 | newpath = g_strdup_printf("/%s%s", component, path ? path : ""); |
1978 | g_free(path); | |
1979 | g_free(component); | |
1980 | path = newpath; | |
57c9fafe | 1981 | obj = obj->parent; |
e40077fd | 1982 | } while (obj != root); |
57c9fafe | 1983 | |
e40077fd | 1984 | return path; |
57c9fafe AL |
1985 | } |
1986 | ||
ddfb0baa | 1987 | Object *object_resolve_path_component(Object *parent, const char *part) |
a612b2a6 | 1988 | { |
89bfe000 | 1989 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
a612b2a6 PB |
1990 | if (prop == NULL) { |
1991 | return NULL; | |
1992 | } | |
1993 | ||
64607d08 PB |
1994 | if (prop->resolve) { |
1995 | return prop->resolve(parent, prop->opaque, part); | |
a612b2a6 PB |
1996 | } else { |
1997 | return NULL; | |
1998 | } | |
1999 | } | |
2000 | ||
57c9fafe | 2001 | static Object *object_resolve_abs_path(Object *parent, |
ad195c8f MY |
2002 | char **parts, |
2003 | const char *typename) | |
57c9fafe | 2004 | { |
57c9fafe AL |
2005 | Object *child; |
2006 | ||
ad195c8f | 2007 | if (*parts == NULL) { |
02fe2db6 | 2008 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
2009 | } |
2010 | ||
ad195c8f MY |
2011 | if (strcmp(*parts, "") == 0) { |
2012 | return object_resolve_abs_path(parent, parts + 1, typename); | |
57c9fafe AL |
2013 | } |
2014 | ||
ad195c8f | 2015 | child = object_resolve_path_component(parent, *parts); |
57c9fafe AL |
2016 | if (!child) { |
2017 | return NULL; | |
2018 | } | |
2019 | ||
ad195c8f | 2020 | return object_resolve_abs_path(child, parts + 1, typename); |
57c9fafe AL |
2021 | } |
2022 | ||
2023 | static Object *object_resolve_partial_path(Object *parent, | |
ddfb0baa MA |
2024 | char **parts, |
2025 | const char *typename, | |
2026 | bool *ambiguous) | |
57c9fafe AL |
2027 | { |
2028 | Object *obj; | |
b604a854 | 2029 | GHashTableIter iter; |
57c9fafe AL |
2030 | ObjectProperty *prop; |
2031 | ||
ad195c8f | 2032 | obj = object_resolve_abs_path(parent, parts, typename); |
57c9fafe | 2033 | |
b604a854 PF |
2034 | g_hash_table_iter_init(&iter, parent->properties); |
2035 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
57c9fafe AL |
2036 | Object *found; |
2037 | ||
5d9d3f47 | 2038 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
2039 | continue; |
2040 | } | |
2041 | ||
02fe2db6 PB |
2042 | found = object_resolve_partial_path(prop->opaque, parts, |
2043 | typename, ambiguous); | |
57c9fafe AL |
2044 | if (found) { |
2045 | if (obj) { | |
ebcc479e | 2046 | *ambiguous = true; |
57c9fafe AL |
2047 | return NULL; |
2048 | } | |
2049 | obj = found; | |
2050 | } | |
2051 | ||
ebcc479e | 2052 | if (*ambiguous) { |
57c9fafe AL |
2053 | return NULL; |
2054 | } | |
2055 | } | |
2056 | ||
2057 | return obj; | |
2058 | } | |
2059 | ||
02fe2db6 | 2060 | Object *object_resolve_path_type(const char *path, const char *typename, |
ebcc479e | 2061 | bool *ambiguousp) |
57c9fafe | 2062 | { |
57c9fafe | 2063 | Object *obj; |
ddfb0baa | 2064 | char **parts; |
57c9fafe AL |
2065 | |
2066 | parts = g_strsplit(path, "/", 0); | |
2e1103f6 | 2067 | assert(parts); |
57c9fafe | 2068 | |
2e1103f6 | 2069 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
ebcc479e | 2070 | bool ambiguous = false; |
02fe2db6 | 2071 | obj = object_resolve_partial_path(object_get_root(), parts, |
ebcc479e EH |
2072 | typename, &ambiguous); |
2073 | if (ambiguousp) { | |
2074 | *ambiguousp = ambiguous; | |
2075 | } | |
57c9fafe | 2076 | } else { |
ad195c8f | 2077 | obj = object_resolve_abs_path(object_get_root(), parts + 1, typename); |
57c9fafe AL |
2078 | } |
2079 | ||
2080 | g_strfreev(parts); | |
2081 | ||
2082 | return obj; | |
2083 | } | |
2084 | ||
02fe2db6 PB |
2085 | Object *object_resolve_path(const char *path, bool *ambiguous) |
2086 | { | |
2087 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
2088 | } | |
2089 | ||
57c9fafe AL |
2090 | typedef struct StringProperty |
2091 | { | |
2092 | char *(*get)(Object *, Error **); | |
2093 | void (*set)(Object *, const char *, Error **); | |
2094 | } StringProperty; | |
2095 | ||
d7bce999 EB |
2096 | static void property_get_str(Object *obj, Visitor *v, const char *name, |
2097 | void *opaque, Error **errp) | |
57c9fafe AL |
2098 | { |
2099 | StringProperty *prop = opaque; | |
2100 | char *value; | |
e1c8237d | 2101 | Error *err = NULL; |
57c9fafe | 2102 | |
e1c8237d MA |
2103 | value = prop->get(obj, &err); |
2104 | if (err) { | |
2105 | error_propagate(errp, err); | |
2106 | return; | |
57c9fafe | 2107 | } |
e1c8237d | 2108 | |
51e72bc1 | 2109 | visit_type_str(v, name, &value, errp); |
e1c8237d | 2110 | g_free(value); |
57c9fafe AL |
2111 | } |
2112 | ||
d7bce999 EB |
2113 | static void property_set_str(Object *obj, Visitor *v, const char *name, |
2114 | void *opaque, Error **errp) | |
57c9fafe AL |
2115 | { |
2116 | StringProperty *prop = opaque; | |
2117 | char *value; | |
2118 | Error *local_err = NULL; | |
2119 | ||
62a35aaa | 2120 | if (!visit_type_str(v, name, &value, &local_err)) { |
57c9fafe AL |
2121 | error_propagate(errp, local_err); |
2122 | return; | |
2123 | } | |
2124 | ||
2125 | prop->set(obj, value, errp); | |
2126 | g_free(value); | |
2127 | } | |
2128 | ||
7b7b7d18 PB |
2129 | static void property_release_str(Object *obj, const char *name, |
2130 | void *opaque) | |
57c9fafe AL |
2131 | { |
2132 | StringProperty *prop = opaque; | |
2133 | g_free(prop); | |
2134 | } | |
2135 | ||
70251887 MA |
2136 | ObjectProperty * |
2137 | object_property_add_str(Object *obj, const char *name, | |
2138 | char *(*get)(Object *, Error **), | |
d2623129 | 2139 | void (*set)(Object *, const char *, Error **)) |
57c9fafe AL |
2140 | { |
2141 | StringProperty *prop = g_malloc0(sizeof(*prop)); | |
2142 | ||
2143 | prop->get = get; | |
2144 | prop->set = set; | |
2145 | ||
d2623129 MA |
2146 | return object_property_add(obj, name, "string", |
2147 | get ? property_get_str : NULL, | |
2148 | set ? property_set_str : NULL, | |
2149 | property_release_str, | |
2150 | prop); | |
57c9fafe | 2151 | } |
745549c8 | 2152 | |
a3a16211 MAL |
2153 | ObjectProperty * |
2154 | object_class_property_add_str(ObjectClass *klass, const char *name, | |
16bf7f52 DB |
2155 | char *(*get)(Object *, Error **), |
2156 | void (*set)(Object *, const char *, | |
d2623129 | 2157 | Error **)) |
16bf7f52 | 2158 | { |
16bf7f52 DB |
2159 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
2160 | ||
2161 | prop->get = get; | |
2162 | prop->set = set; | |
2163 | ||
d2623129 MA |
2164 | return object_class_property_add(klass, name, "string", |
2165 | get ? property_get_str : NULL, | |
2166 | set ? property_set_str : NULL, | |
2167 | NULL, | |
2168 | prop); | |
16bf7f52 DB |
2169 | } |
2170 | ||
0e558843 AL |
2171 | typedef struct BoolProperty |
2172 | { | |
2173 | bool (*get)(Object *, Error **); | |
2174 | void (*set)(Object *, bool, Error **); | |
2175 | } BoolProperty; | |
2176 | ||
d7bce999 EB |
2177 | static void property_get_bool(Object *obj, Visitor *v, const char *name, |
2178 | void *opaque, Error **errp) | |
0e558843 AL |
2179 | { |
2180 | BoolProperty *prop = opaque; | |
2181 | bool value; | |
4715d42e MA |
2182 | Error *err = NULL; |
2183 | ||
2184 | value = prop->get(obj, &err); | |
2185 | if (err) { | |
2186 | error_propagate(errp, err); | |
2187 | return; | |
2188 | } | |
0e558843 | 2189 | |
51e72bc1 | 2190 | visit_type_bool(v, name, &value, errp); |
0e558843 AL |
2191 | } |
2192 | ||
d7bce999 EB |
2193 | static void property_set_bool(Object *obj, Visitor *v, const char *name, |
2194 | void *opaque, Error **errp) | |
0e558843 AL |
2195 | { |
2196 | BoolProperty *prop = opaque; | |
2197 | bool value; | |
2198 | Error *local_err = NULL; | |
2199 | ||
62a35aaa | 2200 | if (!visit_type_bool(v, name, &value, &local_err)) { |
0e558843 AL |
2201 | error_propagate(errp, local_err); |
2202 | return; | |
2203 | } | |
2204 | ||
2205 | prop->set(obj, value, errp); | |
2206 | } | |
2207 | ||
2208 | static void property_release_bool(Object *obj, const char *name, | |
2209 | void *opaque) | |
2210 | { | |
2211 | BoolProperty *prop = opaque; | |
2212 | g_free(prop); | |
2213 | } | |
2214 | ||
70251887 MA |
2215 | ObjectProperty * |
2216 | object_property_add_bool(Object *obj, const char *name, | |
2217 | bool (*get)(Object *, Error **), | |
d2623129 | 2218 | void (*set)(Object *, bool, Error **)) |
0e558843 AL |
2219 | { |
2220 | BoolProperty *prop = g_malloc0(sizeof(*prop)); | |
2221 | ||
2222 | prop->get = get; | |
2223 | prop->set = set; | |
2224 | ||
d2623129 MA |
2225 | return object_property_add(obj, name, "bool", |
2226 | get ? property_get_bool : NULL, | |
2227 | set ? property_set_bool : NULL, | |
2228 | property_release_bool, | |
2229 | prop); | |
0e558843 AL |
2230 | } |
2231 | ||
a3a16211 MAL |
2232 | ObjectProperty * |
2233 | object_class_property_add_bool(ObjectClass *klass, const char *name, | |
16bf7f52 | 2234 | bool (*get)(Object *, Error **), |
d2623129 | 2235 | void (*set)(Object *, bool, Error **)) |
16bf7f52 | 2236 | { |
16bf7f52 DB |
2237 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
2238 | ||
2239 | prop->get = get; | |
2240 | prop->set = set; | |
2241 | ||
d2623129 MA |
2242 | return object_class_property_add(klass, name, "bool", |
2243 | get ? property_get_bool : NULL, | |
2244 | set ? property_set_bool : NULL, | |
2245 | NULL, | |
2246 | prop); | |
16bf7f52 DB |
2247 | } |
2248 | ||
d7bce999 EB |
2249 | static void property_get_enum(Object *obj, Visitor *v, const char *name, |
2250 | void *opaque, Error **errp) | |
a8e3fbed DB |
2251 | { |
2252 | EnumProperty *prop = opaque; | |
2253 | int value; | |
4715d42e MA |
2254 | Error *err = NULL; |
2255 | ||
2256 | value = prop->get(obj, &err); | |
2257 | if (err) { | |
2258 | error_propagate(errp, err); | |
2259 | return; | |
2260 | } | |
a8e3fbed | 2261 | |
f7abe0ec | 2262 | visit_type_enum(v, name, &value, prop->lookup, errp); |
a8e3fbed DB |
2263 | } |
2264 | ||
d7bce999 EB |
2265 | static void property_set_enum(Object *obj, Visitor *v, const char *name, |
2266 | void *opaque, Error **errp) | |
a8e3fbed DB |
2267 | { |
2268 | EnumProperty *prop = opaque; | |
2269 | int value; | |
4715d42e | 2270 | Error *err = NULL; |
a8e3fbed | 2271 | |
62a35aaa | 2272 | if (!visit_type_enum(v, name, &value, prop->lookup, &err)) { |
4715d42e MA |
2273 | error_propagate(errp, err); |
2274 | return; | |
2275 | } | |
a8e3fbed DB |
2276 | prop->set(obj, value, errp); |
2277 | } | |
2278 | ||
2279 | static void property_release_enum(Object *obj, const char *name, | |
2280 | void *opaque) | |
2281 | { | |
2282 | EnumProperty *prop = opaque; | |
2283 | g_free(prop); | |
2284 | } | |
2285 | ||
70251887 MA |
2286 | ObjectProperty * |
2287 | object_property_add_enum(Object *obj, const char *name, | |
2288 | const char *typename, | |
2289 | const QEnumLookup *lookup, | |
2290 | int (*get)(Object *, Error **), | |
d2623129 | 2291 | void (*set)(Object *, int, Error **)) |
a8e3fbed | 2292 | { |
a8e3fbed DB |
2293 | EnumProperty *prop = g_malloc(sizeof(*prop)); |
2294 | ||
f7abe0ec | 2295 | prop->lookup = lookup; |
a8e3fbed DB |
2296 | prop->get = get; |
2297 | prop->set = set; | |
2298 | ||
d2623129 MA |
2299 | return object_property_add(obj, name, typename, |
2300 | get ? property_get_enum : NULL, | |
2301 | set ? property_set_enum : NULL, | |
2302 | property_release_enum, | |
2303 | prop); | |
a8e3fbed DB |
2304 | } |
2305 | ||
a3a16211 MAL |
2306 | ObjectProperty * |
2307 | object_class_property_add_enum(ObjectClass *klass, const char *name, | |
16bf7f52 | 2308 | const char *typename, |
f7abe0ec | 2309 | const QEnumLookup *lookup, |
16bf7f52 | 2310 | int (*get)(Object *, Error **), |
d2623129 | 2311 | void (*set)(Object *, int, Error **)) |
16bf7f52 | 2312 | { |
16bf7f52 DB |
2313 | EnumProperty *prop = g_malloc(sizeof(*prop)); |
2314 | ||
f7abe0ec | 2315 | prop->lookup = lookup; |
16bf7f52 DB |
2316 | prop->get = get; |
2317 | prop->set = set; | |
2318 | ||
d2623129 MA |
2319 | return object_class_property_add(klass, name, typename, |
2320 | get ? property_get_enum : NULL, | |
2321 | set ? property_set_enum : NULL, | |
2322 | NULL, | |
2323 | prop); | |
16bf7f52 DB |
2324 | } |
2325 | ||
8e099d14 DG |
2326 | typedef struct TMProperty { |
2327 | void (*get)(Object *, struct tm *, Error **); | |
2328 | } TMProperty; | |
2329 | ||
d7bce999 EB |
2330 | static void property_get_tm(Object *obj, Visitor *v, const char *name, |
2331 | void *opaque, Error **errp) | |
8e099d14 DG |
2332 | { |
2333 | TMProperty *prop = opaque; | |
2334 | Error *err = NULL; | |
2335 | struct tm value; | |
2336 | ||
2337 | prop->get(obj, &value, &err); | |
2338 | if (err) { | |
2339 | goto out; | |
2340 | } | |
2341 | ||
62a35aaa | 2342 | if (!visit_start_struct(v, name, NULL, 0, &err)) { |
8e099d14 DG |
2343 | goto out; |
2344 | } | |
62a35aaa | 2345 | if (!visit_type_int32(v, "tm_year", &value.tm_year, &err)) { |
8e099d14 DG |
2346 | goto out_end; |
2347 | } | |
62a35aaa | 2348 | if (!visit_type_int32(v, "tm_mon", &value.tm_mon, &err)) { |
8e099d14 DG |
2349 | goto out_end; |
2350 | } | |
62a35aaa | 2351 | if (!visit_type_int32(v, "tm_mday", &value.tm_mday, &err)) { |
8e099d14 DG |
2352 | goto out_end; |
2353 | } | |
62a35aaa | 2354 | if (!visit_type_int32(v, "tm_hour", &value.tm_hour, &err)) { |
8e099d14 DG |
2355 | goto out_end; |
2356 | } | |
62a35aaa | 2357 | if (!visit_type_int32(v, "tm_min", &value.tm_min, &err)) { |
8e099d14 DG |
2358 | goto out_end; |
2359 | } | |
62a35aaa | 2360 | if (!visit_type_int32(v, "tm_sec", &value.tm_sec, &err)) { |
8e099d14 DG |
2361 | goto out_end; |
2362 | } | |
15c2f669 | 2363 | visit_check_struct(v, &err); |
8e099d14 | 2364 | out_end: |
1158bb2a | 2365 | visit_end_struct(v, NULL); |
8e099d14 DG |
2366 | out: |
2367 | error_propagate(errp, err); | |
2368 | ||
2369 | } | |
2370 | ||
2371 | static void property_release_tm(Object *obj, const char *name, | |
2372 | void *opaque) | |
2373 | { | |
2374 | TMProperty *prop = opaque; | |
2375 | g_free(prop); | |
2376 | } | |
2377 | ||
70251887 MA |
2378 | ObjectProperty * |
2379 | object_property_add_tm(Object *obj, const char *name, | |
d2623129 | 2380 | void (*get)(Object *, struct tm *, Error **)) |
8e099d14 | 2381 | { |
8e099d14 DG |
2382 | TMProperty *prop = g_malloc0(sizeof(*prop)); |
2383 | ||
2384 | prop->get = get; | |
2385 | ||
d2623129 MA |
2386 | return object_property_add(obj, name, "struct tm", |
2387 | get ? property_get_tm : NULL, NULL, | |
2388 | property_release_tm, | |
2389 | prop); | |
8e099d14 DG |
2390 | } |
2391 | ||
a3a16211 MAL |
2392 | ObjectProperty * |
2393 | object_class_property_add_tm(ObjectClass *klass, const char *name, | |
d2623129 | 2394 | void (*get)(Object *, struct tm *, Error **)) |
16bf7f52 | 2395 | { |
16bf7f52 DB |
2396 | TMProperty *prop = g_malloc0(sizeof(*prop)); |
2397 | ||
2398 | prop->get = get; | |
2399 | ||
d2623129 MA |
2400 | return object_class_property_add(klass, name, "struct tm", |
2401 | get ? property_get_tm : NULL, | |
2402 | NULL, NULL, prop); | |
16bf7f52 DB |
2403 | } |
2404 | ||
90c69fb9 | 2405 | static char *object_get_type(Object *obj, Error **errp) |
2f262e06 PB |
2406 | { |
2407 | return g_strdup(object_get_typename(obj)); | |
2408 | } | |
2409 | ||
d7bce999 EB |
2410 | static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name, |
2411 | void *opaque, Error **errp) | |
e732ea63 MT |
2412 | { |
2413 | uint8_t value = *(uint8_t *)opaque; | |
51e72bc1 | 2414 | visit_type_uint8(v, name, &value, errp); |
e732ea63 MT |
2415 | } |
2416 | ||
836e1b38 FF |
2417 | static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name, |
2418 | void *opaque, Error **errp) | |
2419 | { | |
2420 | uint8_t *field = opaque; | |
2421 | uint8_t value; | |
2422 | Error *local_err = NULL; | |
2423 | ||
62a35aaa | 2424 | if (!visit_type_uint8(v, name, &value, &local_err)) { |
836e1b38 FF |
2425 | error_propagate(errp, local_err); |
2426 | return; | |
2427 | } | |
2428 | ||
2429 | *field = value; | |
2430 | } | |
2431 | ||
d7bce999 EB |
2432 | static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name, |
2433 | void *opaque, Error **errp) | |
e732ea63 MT |
2434 | { |
2435 | uint16_t value = *(uint16_t *)opaque; | |
51e72bc1 | 2436 | visit_type_uint16(v, name, &value, errp); |
e732ea63 MT |
2437 | } |
2438 | ||
836e1b38 FF |
2439 | static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name, |
2440 | void *opaque, Error **errp) | |
2441 | { | |
2442 | uint16_t *field = opaque; | |
2443 | uint16_t value; | |
2444 | Error *local_err = NULL; | |
2445 | ||
62a35aaa | 2446 | if (!visit_type_uint16(v, name, &value, &local_err)) { |
836e1b38 FF |
2447 | error_propagate(errp, local_err); |
2448 | return; | |
2449 | } | |
2450 | ||
2451 | *field = value; | |
2452 | } | |
2453 | ||
d7bce999 EB |
2454 | static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name, |
2455 | void *opaque, Error **errp) | |
e732ea63 MT |
2456 | { |
2457 | uint32_t value = *(uint32_t *)opaque; | |
51e72bc1 | 2458 | visit_type_uint32(v, name, &value, errp); |
e732ea63 MT |
2459 | } |
2460 | ||
836e1b38 FF |
2461 | static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name, |
2462 | void *opaque, Error **errp) | |
2463 | { | |
2464 | uint32_t *field = opaque; | |
2465 | uint32_t value; | |
2466 | Error *local_err = NULL; | |
2467 | ||
62a35aaa | 2468 | if (!visit_type_uint32(v, name, &value, &local_err)) { |
836e1b38 FF |
2469 | error_propagate(errp, local_err); |
2470 | return; | |
2471 | } | |
2472 | ||
2473 | *field = value; | |
2474 | } | |
2475 | ||
d7bce999 EB |
2476 | static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name, |
2477 | void *opaque, Error **errp) | |
e732ea63 MT |
2478 | { |
2479 | uint64_t value = *(uint64_t *)opaque; | |
51e72bc1 | 2480 | visit_type_uint64(v, name, &value, errp); |
e732ea63 MT |
2481 | } |
2482 | ||
836e1b38 FF |
2483 | static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name, |
2484 | void *opaque, Error **errp) | |
2485 | { | |
2486 | uint64_t *field = opaque; | |
2487 | uint64_t value; | |
2488 | Error *local_err = NULL; | |
2489 | ||
62a35aaa | 2490 | if (!visit_type_uint64(v, name, &value, &local_err)) { |
836e1b38 FF |
2491 | error_propagate(errp, local_err); |
2492 | return; | |
2493 | } | |
2494 | ||
2495 | *field = value; | |
2496 | } | |
2497 | ||
70251887 MA |
2498 | ObjectProperty * |
2499 | object_property_add_uint8_ptr(Object *obj, const char *name, | |
2500 | const uint8_t *v, | |
d2623129 | 2501 | ObjectPropertyFlags flags) |
e732ea63 | 2502 | { |
836e1b38 FF |
2503 | ObjectPropertyAccessor *getter = NULL; |
2504 | ObjectPropertyAccessor *setter = NULL; | |
2505 | ||
2506 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2507 | getter = property_get_uint8_ptr; | |
2508 | } | |
2509 | ||
2510 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2511 | setter = property_set_uint8_ptr; | |
2512 | } | |
2513 | ||
70251887 | 2514 | return object_property_add(obj, name, "uint8", |
d2623129 | 2515 | getter, setter, NULL, (void *)v); |
e732ea63 MT |
2516 | } |
2517 | ||
a3a16211 MAL |
2518 | ObjectProperty * |
2519 | object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name, | |
836e1b38 | 2520 | const uint8_t *v, |
d2623129 | 2521 | ObjectPropertyFlags flags) |
16bf7f52 | 2522 | { |
836e1b38 FF |
2523 | ObjectPropertyAccessor *getter = NULL; |
2524 | ObjectPropertyAccessor *setter = NULL; | |
2525 | ||
2526 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2527 | getter = property_get_uint8_ptr; | |
2528 | } | |
2529 | ||
2530 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2531 | setter = property_set_uint8_ptr; | |
2532 | } | |
2533 | ||
a3a16211 | 2534 | return object_class_property_add(klass, name, "uint8", |
d2623129 | 2535 | getter, setter, NULL, (void *)v); |
16bf7f52 DB |
2536 | } |
2537 | ||
70251887 MA |
2538 | ObjectProperty * |
2539 | object_property_add_uint16_ptr(Object *obj, const char *name, | |
2540 | const uint16_t *v, | |
d2623129 | 2541 | ObjectPropertyFlags flags) |
e732ea63 | 2542 | { |
836e1b38 FF |
2543 | ObjectPropertyAccessor *getter = NULL; |
2544 | ObjectPropertyAccessor *setter = NULL; | |
2545 | ||
2546 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2547 | getter = property_get_uint16_ptr; | |
2548 | } | |
2549 | ||
2550 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2551 | setter = property_set_uint16_ptr; | |
2552 | } | |
2553 | ||
70251887 | 2554 | return object_property_add(obj, name, "uint16", |
d2623129 | 2555 | getter, setter, NULL, (void *)v); |
e732ea63 MT |
2556 | } |
2557 | ||
a3a16211 MAL |
2558 | ObjectProperty * |
2559 | object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name, | |
836e1b38 | 2560 | const uint16_t *v, |
d2623129 | 2561 | ObjectPropertyFlags flags) |
16bf7f52 | 2562 | { |
836e1b38 FF |
2563 | ObjectPropertyAccessor *getter = NULL; |
2564 | ObjectPropertyAccessor *setter = NULL; | |
2565 | ||
2566 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2567 | getter = property_get_uint16_ptr; | |
2568 | } | |
2569 | ||
2570 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2571 | setter = property_set_uint16_ptr; | |
2572 | } | |
2573 | ||
a3a16211 | 2574 | return object_class_property_add(klass, name, "uint16", |
d2623129 | 2575 | getter, setter, NULL, (void *)v); |
16bf7f52 DB |
2576 | } |
2577 | ||
70251887 MA |
2578 | ObjectProperty * |
2579 | object_property_add_uint32_ptr(Object *obj, const char *name, | |
2580 | const uint32_t *v, | |
d2623129 | 2581 | ObjectPropertyFlags flags) |
e732ea63 | 2582 | { |
836e1b38 FF |
2583 | ObjectPropertyAccessor *getter = NULL; |
2584 | ObjectPropertyAccessor *setter = NULL; | |
2585 | ||
2586 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2587 | getter = property_get_uint32_ptr; | |
2588 | } | |
2589 | ||
2590 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2591 | setter = property_set_uint32_ptr; | |
2592 | } | |
2593 | ||
70251887 | 2594 | return object_property_add(obj, name, "uint32", |
d2623129 | 2595 | getter, setter, NULL, (void *)v); |
e732ea63 MT |
2596 | } |
2597 | ||
a3a16211 MAL |
2598 | ObjectProperty * |
2599 | object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name, | |
836e1b38 | 2600 | const uint32_t *v, |
d2623129 | 2601 | ObjectPropertyFlags flags) |
16bf7f52 | 2602 | { |
836e1b38 FF |
2603 | ObjectPropertyAccessor *getter = NULL; |
2604 | ObjectPropertyAccessor *setter = NULL; | |
2605 | ||
2606 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2607 | getter = property_get_uint32_ptr; | |
2608 | } | |
2609 | ||
2610 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2611 | setter = property_set_uint32_ptr; | |
2612 | } | |
2613 | ||
a3a16211 | 2614 | return object_class_property_add(klass, name, "uint32", |
d2623129 | 2615 | getter, setter, NULL, (void *)v); |
16bf7f52 DB |
2616 | } |
2617 | ||
70251887 MA |
2618 | ObjectProperty * |
2619 | object_property_add_uint64_ptr(Object *obj, const char *name, | |
2620 | const uint64_t *v, | |
d2623129 | 2621 | ObjectPropertyFlags flags) |
e732ea63 | 2622 | { |
836e1b38 FF |
2623 | ObjectPropertyAccessor *getter = NULL; |
2624 | ObjectPropertyAccessor *setter = NULL; | |
2625 | ||
2626 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2627 | getter = property_get_uint64_ptr; | |
2628 | } | |
2629 | ||
2630 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2631 | setter = property_set_uint64_ptr; | |
2632 | } | |
2633 | ||
70251887 | 2634 | return object_property_add(obj, name, "uint64", |
d2623129 | 2635 | getter, setter, NULL, (void *)v); |
e732ea63 MT |
2636 | } |
2637 | ||
a3a16211 MAL |
2638 | ObjectProperty * |
2639 | object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name, | |
836e1b38 | 2640 | const uint64_t *v, |
d2623129 | 2641 | ObjectPropertyFlags flags) |
16bf7f52 | 2642 | { |
836e1b38 FF |
2643 | ObjectPropertyAccessor *getter = NULL; |
2644 | ObjectPropertyAccessor *setter = NULL; | |
2645 | ||
2646 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2647 | getter = property_get_uint64_ptr; | |
2648 | } | |
2649 | ||
2650 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2651 | setter = property_set_uint64_ptr; | |
2652 | } | |
2653 | ||
a3a16211 | 2654 | return object_class_property_add(klass, name, "uint64", |
d2623129 | 2655 | getter, setter, NULL, (void *)v); |
16bf7f52 DB |
2656 | } |
2657 | ||
ef7c7ff6 SH |
2658 | typedef struct { |
2659 | Object *target_obj; | |
1590d266 | 2660 | char *target_name; |
ef7c7ff6 SH |
2661 | } AliasProperty; |
2662 | ||
d7bce999 EB |
2663 | static void property_get_alias(Object *obj, Visitor *v, const char *name, |
2664 | void *opaque, Error **errp) | |
ef7c7ff6 SH |
2665 | { |
2666 | AliasProperty *prop = opaque; | |
2667 | ||
5325cc34 | 2668 | object_property_get(prop->target_obj, prop->target_name, v, errp); |
ef7c7ff6 SH |
2669 | } |
2670 | ||
d7bce999 EB |
2671 | static void property_set_alias(Object *obj, Visitor *v, const char *name, |
2672 | void *opaque, Error **errp) | |
ef7c7ff6 SH |
2673 | { |
2674 | AliasProperty *prop = opaque; | |
2675 | ||
5325cc34 | 2676 | object_property_set(prop->target_obj, prop->target_name, v, errp); |
ef7c7ff6 SH |
2677 | } |
2678 | ||
64607d08 | 2679 | static Object *property_resolve_alias(Object *obj, void *opaque, |
ddfb0baa | 2680 | const char *part) |
64607d08 PB |
2681 | { |
2682 | AliasProperty *prop = opaque; | |
2683 | ||
2684 | return object_resolve_path_component(prop->target_obj, prop->target_name); | |
2685 | } | |
2686 | ||
ef7c7ff6 SH |
2687 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
2688 | { | |
2689 | AliasProperty *prop = opaque; | |
2690 | ||
1590d266 | 2691 | g_free(prop->target_name); |
ef7c7ff6 SH |
2692 | g_free(prop); |
2693 | } | |
2694 | ||
70251887 MA |
2695 | ObjectProperty * |
2696 | object_property_add_alias(Object *obj, const char *name, | |
d2623129 | 2697 | Object *target_obj, const char *target_name) |
ef7c7ff6 SH |
2698 | { |
2699 | AliasProperty *prop; | |
64607d08 | 2700 | ObjectProperty *op; |
ef7c7ff6 | 2701 | ObjectProperty *target_prop; |
70251887 | 2702 | g_autofree char *prop_type = NULL; |
ef7c7ff6 | 2703 | |
d2623129 MA |
2704 | target_prop = object_property_find(target_obj, target_name, |
2705 | &error_abort); | |
ef7c7ff6 | 2706 | |
d190698e PB |
2707 | if (object_property_is_child(target_prop)) { |
2708 | prop_type = g_strdup_printf("link%s", | |
2709 | target_prop->type + strlen("child")); | |
2710 | } else { | |
2711 | prop_type = g_strdup(target_prop->type); | |
2712 | } | |
2713 | ||
ef7c7ff6 SH |
2714 | prop = g_malloc(sizeof(*prop)); |
2715 | prop->target_obj = target_obj; | |
1590d266 | 2716 | prop->target_name = g_strdup(target_name); |
ef7c7ff6 | 2717 | |
d190698e | 2718 | op = object_property_add(obj, name, prop_type, |
64607d08 PB |
2719 | property_get_alias, |
2720 | property_set_alias, | |
2721 | property_release_alias, | |
d2623129 | 2722 | prop); |
64607d08 | 2723 | op->resolve = property_resolve_alias; |
0e76ed0a MAL |
2724 | if (target_prop->defval) { |
2725 | op->defval = qobject_ref(target_prop->defval); | |
2726 | } | |
d190698e | 2727 | |
a18bb417 | 2728 | object_property_set_description(obj, op->name, |
7eecec7d | 2729 | target_prop->description); |
70251887 | 2730 | return op; |
ef7c7ff6 SH |
2731 | } |
2732 | ||
80742642 | 2733 | void object_property_set_description(Object *obj, const char *name, |
7eecec7d | 2734 | const char *description) |
80742642 GA |
2735 | { |
2736 | ObjectProperty *op; | |
2737 | ||
7eecec7d | 2738 | op = object_property_find(obj, name, &error_abort); |
80742642 GA |
2739 | g_free(op->description); |
2740 | op->description = g_strdup(description); | |
2741 | } | |
2742 | ||
16bf7f52 DB |
2743 | void object_class_property_set_description(ObjectClass *klass, |
2744 | const char *name, | |
7eecec7d | 2745 | const char *description) |
16bf7f52 DB |
2746 | { |
2747 | ObjectProperty *op; | |
2748 | ||
2749 | op = g_hash_table_lookup(klass->properties, name); | |
16bf7f52 DB |
2750 | g_free(op->description); |
2751 | op->description = g_strdup(description); | |
2752 | } | |
2753 | ||
7439a036 | 2754 | static void object_class_init(ObjectClass *klass, void *data) |
2f262e06 | 2755 | { |
90c69fb9 | 2756 | object_class_property_add_str(klass, "type", object_get_type, |
d2623129 | 2757 | NULL); |
2f262e06 PB |
2758 | } |
2759 | ||
745549c8 PB |
2760 | static void register_types(void) |
2761 | { | |
2762 | static TypeInfo interface_info = { | |
2763 | .name = TYPE_INTERFACE, | |
33e95c63 | 2764 | .class_size = sizeof(InterfaceClass), |
745549c8 PB |
2765 | .abstract = true, |
2766 | }; | |
2767 | ||
2768 | static TypeInfo object_info = { | |
2769 | .name = TYPE_OBJECT, | |
2770 | .instance_size = sizeof(Object), | |
7439a036 | 2771 | .class_init = object_class_init, |
745549c8 PB |
2772 | .abstract = true, |
2773 | }; | |
2774 | ||
049cb3cf PB |
2775 | type_interface = type_register_internal(&interface_info); |
2776 | type_register_internal(&object_info); | |
745549c8 PB |
2777 | } |
2778 | ||
2779 | type_init(register_types) |