]>
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 | 551 | { |
6fd5bef1 | 552 | bool ok = false; |
0210b39d | 553 | Object *obj; |
3650b2de | 554 | UserCreatable *uc; |
0210b39d TH |
555 | |
556 | object_initialize(childobj, size, type); | |
557 | obj = OBJECT(childobj); | |
558 | ||
b783f54d | 559 | if (!object_set_propv(obj, errp, vargs)) { |
0210b39d TH |
560 | goto out; |
561 | } | |
562 | ||
d2623129 | 563 | object_property_add_child(parentobj, propname, obj); |
0210b39d | 564 | |
3650b2de MAL |
565 | uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); |
566 | if (uc) { | |
992861fb | 567 | if (!user_creatable_complete(uc, errp)) { |
0210b39d TH |
568 | object_unparent(obj); |
569 | goto out; | |
570 | } | |
571 | } | |
572 | ||
6fd5bef1 MA |
573 | ok = true; |
574 | ||
975ac455 | 575 | out: |
0210b39d | 576 | /* |
975ac455 MA |
577 | * We want @obj's reference to be 1 on success, 0 on failure. |
578 | * On success, it's 2: one taken by object_initialize(), and one | |
579 | * by object_property_add_child(). | |
580 | * On failure in object_initialize() or earlier, it's 1. | |
581 | * On failure afterwards, it's also 1: object_unparent() releases | |
582 | * the reference taken by object_property_add_child(). | |
0210b39d TH |
583 | */ |
584 | object_unref(obj); | |
6fd5bef1 | 585 | return ok; |
0210b39d TH |
586 | } |
587 | ||
9fc7fc4d MA |
588 | void object_initialize_child_internal(Object *parent, |
589 | const char *propname, | |
590 | void *child, size_t size, | |
591 | const char *type) | |
592 | { | |
593 | object_initialize_child_with_props(parent, propname, child, size, type, | |
594 | &error_abort, NULL); | |
595 | } | |
596 | ||
5d9d3f47 AF |
597 | static inline bool object_property_is_child(ObjectProperty *prop) |
598 | { | |
599 | return strstart(prop->type, "child<", NULL); | |
600 | } | |
601 | ||
57c9fafe AL |
602 | static void object_property_del_all(Object *obj) |
603 | { | |
9859facc | 604 | g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL); |
b604a854 | 605 | ObjectProperty *prop; |
9859facc | 606 | ObjectPropertyIterator iter; |
b604a854 PF |
607 | bool released; |
608 | ||
609 | do { | |
610 | released = false; | |
9859facc MAL |
611 | object_property_iter_init(&iter, obj); |
612 | while ((prop = object_property_iter_next(&iter)) != NULL) { | |
613 | if (g_hash_table_add(done, prop)) { | |
614 | if (prop->release) { | |
615 | prop->release(obj, prop->name, prop->opaque); | |
616 | released = true; | |
617 | break; | |
618 | } | |
b604a854 | 619 | } |
57c9fafe | 620 | } |
b604a854 | 621 | } while (released); |
57c9fafe | 622 | |
b604a854 | 623 | g_hash_table_unref(obj->properties); |
57c9fafe AL |
624 | } |
625 | ||
f73a32a5 | 626 | static void object_property_del_child(Object *obj, Object *child) |
57c9fafe AL |
627 | { |
628 | ObjectProperty *prop; | |
b604a854 PF |
629 | GHashTableIter iter; |
630 | gpointer key, value; | |
57c9fafe | 631 | |
b604a854 PF |
632 | g_hash_table_iter_init(&iter, obj->properties); |
633 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
634 | prop = value; | |
635 | if (object_property_is_child(prop) && prop->opaque == child) { | |
636 | if (prop->release) { | |
637 | prop->release(obj, prop->name, prop->opaque); | |
638 | prop->release = NULL; | |
639 | } | |
640 | break; | |
641 | } | |
642 | } | |
643 | g_hash_table_iter_init(&iter, obj->properties); | |
644 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
645 | prop = value; | |
5d9d3f47 | 646 | if (object_property_is_child(prop) && prop->opaque == child) { |
b604a854 | 647 | g_hash_table_iter_remove(&iter); |
6c1fdcf9 | 648 | break; |
57c9fafe AL |
649 | } |
650 | } | |
651 | } | |
652 | ||
653 | void object_unparent(Object *obj) | |
654 | { | |
e998fa8d | 655 | if (obj->parent) { |
f73a32a5 | 656 | object_property_del_child(obj->parent, obj); |
e998fa8d | 657 | } |
57c9fafe AL |
658 | } |
659 | ||
2f28d2ff AL |
660 | static void object_deinit(Object *obj, TypeImpl *type) |
661 | { | |
662 | if (type->instance_finalize) { | |
663 | type->instance_finalize(obj); | |
664 | } | |
665 | ||
2f28d2ff AL |
666 | if (type_has_parent(type)) { |
667 | object_deinit(obj, type_get_parent(type)); | |
668 | } | |
669 | } | |
670 | ||
339c2708 | 671 | static void object_finalize(void *data) |
2f28d2ff AL |
672 | { |
673 | Object *obj = data; | |
674 | TypeImpl *ti = obj->class->type; | |
675 | ||
57c9fafe | 676 | object_property_del_all(obj); |
76a6e1cc | 677 | object_deinit(obj, ti); |
db85b575 | 678 | |
719a3077 | 679 | g_assert(obj->ref == 0); |
fde9bf44 PB |
680 | if (obj->free) { |
681 | obj->free(obj); | |
682 | } | |
2f28d2ff AL |
683 | } |
684 | ||
63f7b10b | 685 | static Object *object_new_with_type(Type type) |
2f28d2ff AL |
686 | { |
687 | Object *obj; | |
688 | ||
689 | g_assert(type != NULL); | |
ac451033 | 690 | type_initialize(type); |
2f28d2ff AL |
691 | |
692 | obj = g_malloc(type->instance_size); | |
5b9237f6 | 693 | object_initialize_with_type(obj, type->instance_size, type); |
fde9bf44 | 694 | obj->free = g_free; |
2f28d2ff AL |
695 | |
696 | return obj; | |
697 | } | |
698 | ||
3c75e12e PB |
699 | Object *object_new_with_class(ObjectClass *klass) |
700 | { | |
701 | return object_new_with_type(klass->type); | |
702 | } | |
703 | ||
2f28d2ff AL |
704 | Object *object_new(const char *typename) |
705 | { | |
706 | TypeImpl *ti = type_get_by_name(typename); | |
707 | ||
708 | return object_new_with_type(ti); | |
709 | } | |
710 | ||
a31bdae5 DB |
711 | |
712 | Object *object_new_with_props(const char *typename, | |
713 | Object *parent, | |
714 | const char *id, | |
715 | Error **errp, | |
716 | ...) | |
717 | { | |
718 | va_list vargs; | |
719 | Object *obj; | |
720 | ||
721 | va_start(vargs, errp); | |
722 | obj = object_new_with_propv(typename, parent, id, errp, vargs); | |
723 | va_end(vargs); | |
724 | ||
725 | return obj; | |
726 | } | |
727 | ||
728 | ||
729 | Object *object_new_with_propv(const char *typename, | |
730 | Object *parent, | |
731 | const char *id, | |
732 | Error **errp, | |
733 | va_list vargs) | |
734 | { | |
735 | Object *obj; | |
736 | ObjectClass *klass; | |
3650b2de | 737 | UserCreatable *uc; |
a31bdae5 DB |
738 | |
739 | klass = object_class_by_name(typename); | |
740 | if (!klass) { | |
741 | error_setg(errp, "invalid object type: %s", typename); | |
742 | return NULL; | |
743 | } | |
744 | ||
745 | if (object_class_is_abstract(klass)) { | |
746 | error_setg(errp, "object type '%s' is abstract", typename); | |
747 | return NULL; | |
748 | } | |
66e1155a | 749 | obj = object_new_with_type(klass->type); |
a31bdae5 | 750 | |
b783f54d | 751 | if (!object_set_propv(obj, errp, vargs)) { |
a31bdae5 DB |
752 | goto error; |
753 | } | |
754 | ||
6134d752 | 755 | if (id != NULL) { |
d2623129 | 756 | object_property_add_child(parent, id, obj); |
a31bdae5 DB |
757 | } |
758 | ||
3650b2de MAL |
759 | uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE); |
760 | if (uc) { | |
992861fb | 761 | if (!user_creatable_complete(uc, errp)) { |
6134d752 DB |
762 | if (id != NULL) { |
763 | object_unparent(obj); | |
764 | } | |
a31bdae5 DB |
765 | goto error; |
766 | } | |
767 | } | |
768 | ||
688ffbb4 | 769 | object_unref(obj); |
a31bdae5 DB |
770 | return obj; |
771 | ||
772 | error: | |
a31bdae5 DB |
773 | object_unref(obj); |
774 | return NULL; | |
775 | } | |
776 | ||
777 | ||
b783f54d | 778 | bool object_set_props(Object *obj, |
a31bdae5 DB |
779 | Error **errp, |
780 | ...) | |
781 | { | |
782 | va_list vargs; | |
b783f54d | 783 | bool ret; |
a31bdae5 DB |
784 | |
785 | va_start(vargs, errp); | |
786 | ret = object_set_propv(obj, errp, vargs); | |
787 | va_end(vargs); | |
788 | ||
789 | return ret; | |
790 | } | |
791 | ||
792 | ||
b783f54d | 793 | bool object_set_propv(Object *obj, |
a31bdae5 DB |
794 | Error **errp, |
795 | va_list vargs) | |
796 | { | |
797 | const char *propname; | |
a31bdae5 DB |
798 | |
799 | propname = va_arg(vargs, char *); | |
800 | while (propname != NULL) { | |
801 | const char *value = va_arg(vargs, char *); | |
802 | ||
803 | g_assert(value != NULL); | |
668f62ec | 804 | if (!object_property_parse(obj, propname, value, errp)) { |
b783f54d | 805 | return false; |
a31bdae5 DB |
806 | } |
807 | propname = va_arg(vargs, char *); | |
808 | } | |
809 | ||
b783f54d | 810 | return true; |
a31bdae5 DB |
811 | } |
812 | ||
813 | ||
2f28d2ff AL |
814 | Object *object_dynamic_cast(Object *obj, const char *typename) |
815 | { | |
b7f43fe4 | 816 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
acc4af3f PB |
817 | return obj; |
818 | } | |
819 | ||
2f28d2ff AL |
820 | return NULL; |
821 | } | |
822 | ||
be17f18b PB |
823 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
824 | const char *file, int line, const char *func) | |
2f28d2ff | 825 | { |
fa131d94 PB |
826 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
827 | typename, file, line, func); | |
828 | ||
3556c233 | 829 | #ifdef CONFIG_QOM_CAST_DEBUG |
03587328 AL |
830 | int i; |
831 | Object *inst; | |
832 | ||
95916abc | 833 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd | 834 | if (atomic_read(&obj->class->object_cast_cache[i]) == typename) { |
03587328 AL |
835 | goto out; |
836 | } | |
837 | } | |
838 | ||
839 | inst = object_dynamic_cast(obj, typename); | |
2f28d2ff | 840 | |
b7f43fe4 | 841 | if (!inst && obj) { |
be17f18b PB |
842 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
843 | file, line, func, obj, typename); | |
2f28d2ff AL |
844 | abort(); |
845 | } | |
846 | ||
3556c233 | 847 | assert(obj == inst); |
03587328 | 848 | |
95916abc | 849 | if (obj && obj == inst) { |
03587328 | 850 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd AB |
851 | atomic_set(&obj->class->object_cast_cache[i - 1], |
852 | atomic_read(&obj->class->object_cast_cache[i])); | |
03587328 | 853 | } |
b6b3ccfd | 854 | atomic_set(&obj->class->object_cast_cache[i - 1], typename); |
03587328 AL |
855 | } |
856 | ||
857 | out: | |
3556c233 PB |
858 | #endif |
859 | return obj; | |
2f28d2ff AL |
860 | } |
861 | ||
862 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
863 | const char *typename) | |
864 | { | |
33e95c63 | 865 | ObjectClass *ret = NULL; |
bf0fda34 PB |
866 | TypeImpl *target_type; |
867 | TypeImpl *type; | |
2f28d2ff | 868 | |
bf0fda34 PB |
869 | if (!class) { |
870 | return NULL; | |
871 | } | |
872 | ||
793c96b5 | 873 | /* A simple fast path that can trigger a lot for leaf classes. */ |
bf0fda34 | 874 | type = class->type; |
793c96b5 PB |
875 | if (type->name == typename) { |
876 | return class; | |
877 | } | |
878 | ||
bf0fda34 | 879 | target_type = type_get_by_name(typename); |
9ab880b3 AG |
880 | if (!target_type) { |
881 | /* target class type unknown, so fail the cast */ | |
882 | return NULL; | |
883 | } | |
884 | ||
00e2ceae PC |
885 | if (type->class->interfaces && |
886 | type_is_ancestor(target_type, type_interface)) { | |
33e95c63 AL |
887 | int found = 0; |
888 | GSList *i; | |
2f28d2ff | 889 | |
33e95c63 AL |
890 | for (i = class->interfaces; i; i = i->next) { |
891 | ObjectClass *target_class = i->data; | |
892 | ||
893 | if (type_is_ancestor(target_class->type, target_type)) { | |
894 | ret = target_class; | |
895 | found++; | |
896 | } | |
897 | } | |
898 | ||
899 | /* The match was ambiguous, don't allow a cast */ | |
900 | if (found > 1) { | |
901 | ret = NULL; | |
902 | } | |
903 | } else if (type_is_ancestor(type, target_type)) { | |
904 | ret = class; | |
2f28d2ff AL |
905 | } |
906 | ||
33e95c63 | 907 | return ret; |
2f28d2ff AL |
908 | } |
909 | ||
910 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
be17f18b PB |
911 | const char *typename, |
912 | const char *file, int line, | |
913 | const char *func) | |
2f28d2ff | 914 | { |
fa131d94 PB |
915 | ObjectClass *ret; |
916 | ||
917 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", | |
918 | typename, file, line, func); | |
2f28d2ff | 919 | |
03587328 AL |
920 | #ifdef CONFIG_QOM_CAST_DEBUG |
921 | int i; | |
922 | ||
9d6a3d58 | 923 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd | 924 | if (atomic_read(&class->class_cast_cache[i]) == typename) { |
03587328 AL |
925 | ret = class; |
926 | goto out; | |
927 | } | |
928 | } | |
929 | #else | |
9d6a3d58 | 930 | if (!class || !class->interfaces) { |
3556c233 PB |
931 | return class; |
932 | } | |
933 | #endif | |
934 | ||
fa131d94 | 935 | ret = object_class_dynamic_cast(class, typename); |
bf0fda34 | 936 | if (!ret && class) { |
be17f18b PB |
937 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
938 | file, line, func, class, typename); | |
2f28d2ff AL |
939 | abort(); |
940 | } | |
941 | ||
03587328 | 942 | #ifdef CONFIG_QOM_CAST_DEBUG |
9d6a3d58 | 943 | if (class && ret == class) { |
03587328 | 944 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
b6b3ccfd AB |
945 | atomic_set(&class->class_cast_cache[i - 1], |
946 | atomic_read(&class->class_cast_cache[i])); | |
03587328 | 947 | } |
b6b3ccfd | 948 | atomic_set(&class->class_cast_cache[i - 1], typename); |
03587328 AL |
949 | } |
950 | out: | |
951 | #endif | |
2f28d2ff AL |
952 | return ret; |
953 | } | |
954 | ||
8f5d58ef | 955 | const char *object_get_typename(const Object *obj) |
2f28d2ff AL |
956 | { |
957 | return obj->class->type->name; | |
958 | } | |
959 | ||
960 | ObjectClass *object_get_class(Object *obj) | |
961 | { | |
962 | return obj->class; | |
963 | } | |
964 | ||
17862378 AF |
965 | bool object_class_is_abstract(ObjectClass *klass) |
966 | { | |
967 | return klass->type->abstract; | |
968 | } | |
969 | ||
2f28d2ff AL |
970 | const char *object_class_get_name(ObjectClass *klass) |
971 | { | |
972 | return klass->type->name; | |
973 | } | |
974 | ||
975 | ObjectClass *object_class_by_name(const char *typename) | |
976 | { | |
977 | TypeImpl *type = type_get_by_name(typename); | |
978 | ||
979 | if (!type) { | |
980 | return NULL; | |
981 | } | |
982 | ||
ac451033 | 983 | type_initialize(type); |
2f28d2ff AL |
984 | |
985 | return type->class; | |
986 | } | |
987 | ||
0f8198f1 GH |
988 | ObjectClass *module_object_class_by_name(const char *typename) |
989 | { | |
990 | ObjectClass *oc; | |
991 | ||
992 | oc = object_class_by_name(typename); | |
993 | #ifdef CONFIG_MODULES | |
994 | if (!oc) { | |
995 | module_load_qom_one(typename); | |
996 | oc = object_class_by_name(typename); | |
997 | } | |
998 | #endif | |
999 | return oc; | |
1000 | } | |
1001 | ||
e7cce67f PB |
1002 | ObjectClass *object_class_get_parent(ObjectClass *class) |
1003 | { | |
1004 | TypeImpl *type = type_get_parent(class->type); | |
1005 | ||
1006 | if (!type) { | |
1007 | return NULL; | |
1008 | } | |
1009 | ||
1010 | type_initialize(type); | |
1011 | ||
1012 | return type->class; | |
1013 | } | |
1014 | ||
2f28d2ff AL |
1015 | typedef struct OCFData |
1016 | { | |
1017 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
1018 | const char *implements_type; |
1019 | bool include_abstract; | |
2f28d2ff AL |
1020 | void *opaque; |
1021 | } OCFData; | |
1022 | ||
1023 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
1024 | gpointer opaque) | |
1025 | { | |
1026 | OCFData *data = opaque; | |
1027 | TypeImpl *type = value; | |
93c511a1 | 1028 | ObjectClass *k; |
2f28d2ff | 1029 | |
ac451033 | 1030 | type_initialize(type); |
93c511a1 | 1031 | k = type->class; |
2f28d2ff | 1032 | |
93c511a1 AL |
1033 | if (!data->include_abstract && type->abstract) { |
1034 | return; | |
1035 | } | |
1036 | ||
1037 | if (data->implements_type && | |
1038 | !object_class_dynamic_cast(k, data->implements_type)) { | |
1039 | return; | |
1040 | } | |
1041 | ||
1042 | data->fn(k, data->opaque); | |
2f28d2ff AL |
1043 | } |
1044 | ||
1045 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 1046 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
1047 | void *opaque) |
1048 | { | |
93c511a1 | 1049 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff | 1050 | |
f54c19ca | 1051 | enumerating_types = true; |
2f28d2ff | 1052 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
f54c19ca | 1053 | enumerating_types = false; |
2f28d2ff | 1054 | } |
57c9fafe | 1055 | |
d714b8de PC |
1056 | static int do_object_child_foreach(Object *obj, |
1057 | int (*fn)(Object *child, void *opaque), | |
1058 | void *opaque, bool recurse) | |
32efc535 | 1059 | { |
b604a854 PF |
1060 | GHashTableIter iter; |
1061 | ObjectProperty *prop; | |
32efc535 PB |
1062 | int ret = 0; |
1063 | ||
b604a854 PF |
1064 | g_hash_table_iter_init(&iter, obj->properties); |
1065 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
32efc535 | 1066 | if (object_property_is_child(prop)) { |
d714b8de PC |
1067 | Object *child = prop->opaque; |
1068 | ||
1069 | ret = fn(child, opaque); | |
32efc535 PB |
1070 | if (ret != 0) { |
1071 | break; | |
1072 | } | |
d714b8de | 1073 | if (recurse) { |
4a39181d CLG |
1074 | ret = do_object_child_foreach(child, fn, opaque, true); |
1075 | if (ret != 0) { | |
1076 | break; | |
1077 | } | |
d714b8de | 1078 | } |
32efc535 PB |
1079 | } |
1080 | } | |
1081 | return ret; | |
1082 | } | |
1083 | ||
d714b8de PC |
1084 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
1085 | void *opaque) | |
1086 | { | |
1087 | return do_object_child_foreach(obj, fn, opaque, false); | |
1088 | } | |
1089 | ||
1090 | int object_child_foreach_recursive(Object *obj, | |
1091 | int (*fn)(Object *child, void *opaque), | |
1092 | void *opaque) | |
1093 | { | |
1094 | return do_object_child_foreach(obj, fn, opaque, true); | |
1095 | } | |
1096 | ||
418ba9e5 AF |
1097 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
1098 | { | |
1099 | GSList **list = opaque; | |
1100 | ||
1101 | *list = g_slist_prepend(*list, klass); | |
1102 | } | |
1103 | ||
1104 | GSList *object_class_get_list(const char *implements_type, | |
1105 | bool include_abstract) | |
1106 | { | |
1107 | GSList *list = NULL; | |
1108 | ||
1109 | object_class_foreach(object_class_get_list_tramp, | |
1110 | implements_type, include_abstract, &list); | |
1111 | return list; | |
1112 | } | |
1113 | ||
47c66009 PB |
1114 | static gint object_class_cmp(gconstpointer a, gconstpointer b) |
1115 | { | |
1116 | return strcasecmp(object_class_get_name((ObjectClass *)a), | |
1117 | object_class_get_name((ObjectClass *)b)); | |
1118 | } | |
1119 | ||
1120 | GSList *object_class_get_list_sorted(const char *implements_type, | |
1121 | bool include_abstract) | |
1122 | { | |
1123 | return g_slist_sort(object_class_get_list(implements_type, include_abstract), | |
1124 | object_class_cmp); | |
1125 | } | |
1126 | ||
b77ade9b | 1127 | Object *object_ref(Object *obj) |
57c9fafe | 1128 | { |
8ffad850 | 1129 | if (!obj) { |
b77ade9b | 1130 | return NULL; |
8ffad850 | 1131 | } |
8438a135 | 1132 | atomic_inc(&obj->ref); |
b77ade9b | 1133 | return obj; |
57c9fafe AL |
1134 | } |
1135 | ||
1136 | void object_unref(Object *obj) | |
1137 | { | |
8ffad850 PC |
1138 | if (!obj) { |
1139 | return; | |
1140 | } | |
719a3077 | 1141 | g_assert(obj->ref > 0); |
57c9fafe AL |
1142 | |
1143 | /* parent always holds a reference to its children */ | |
f08c03f3 | 1144 | if (atomic_fetch_dec(&obj->ref) == 1) { |
57c9fafe AL |
1145 | object_finalize(obj); |
1146 | } | |
1147 | } | |
1148 | ||
db57fef1 | 1149 | ObjectProperty * |
d2623129 MA |
1150 | object_property_try_add(Object *obj, const char *name, const char *type, |
1151 | ObjectPropertyAccessor *get, | |
1152 | ObjectPropertyAccessor *set, | |
1153 | ObjectPropertyRelease *release, | |
1154 | void *opaque, Error **errp) | |
57c9fafe | 1155 | { |
54852b03 | 1156 | ObjectProperty *prop; |
33965904 PC |
1157 | size_t name_len = strlen(name); |
1158 | ||
1159 | if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { | |
1160 | int i; | |
1161 | ObjectProperty *ret; | |
1162 | char *name_no_array = g_strdup(name); | |
1163 | ||
1164 | name_no_array[name_len - 3] = '\0'; | |
1165 | for (i = 0; ; ++i) { | |
1166 | char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); | |
1167 | ||
d2623129 MA |
1168 | ret = object_property_try_add(obj, full_name, type, get, set, |
1169 | release, opaque, NULL); | |
33965904 PC |
1170 | g_free(full_name); |
1171 | if (ret) { | |
1172 | break; | |
1173 | } | |
1174 | } | |
1175 | g_free(name_no_array); | |
1176 | return ret; | |
1177 | } | |
54852b03 | 1178 | |
16bf7f52 | 1179 | if (object_property_find(obj, name, NULL) != NULL) { |
d55e937d GK |
1180 | error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')", |
1181 | name, object_get_typename(obj)); | |
b604a854 | 1182 | return NULL; |
54852b03 PM |
1183 | } |
1184 | ||
1185 | prop = g_malloc0(sizeof(*prop)); | |
57c9fafe AL |
1186 | |
1187 | prop->name = g_strdup(name); | |
1188 | prop->type = g_strdup(type); | |
1189 | ||
1190 | prop->get = get; | |
1191 | prop->set = set; | |
1192 | prop->release = release; | |
1193 | prop->opaque = opaque; | |
1194 | ||
b604a854 | 1195 | g_hash_table_insert(obj->properties, prop->name, prop); |
64607d08 | 1196 | return prop; |
57c9fafe AL |
1197 | } |
1198 | ||
d2623129 MA |
1199 | ObjectProperty * |
1200 | object_property_add(Object *obj, const char *name, const char *type, | |
1201 | ObjectPropertyAccessor *get, | |
1202 | ObjectPropertyAccessor *set, | |
1203 | ObjectPropertyRelease *release, | |
1204 | void *opaque) | |
1205 | { | |
1206 | return object_property_try_add(obj, name, type, get, set, release, | |
1207 | opaque, &error_abort); | |
1208 | } | |
1209 | ||
16bf7f52 DB |
1210 | ObjectProperty * |
1211 | object_class_property_add(ObjectClass *klass, | |
1212 | const char *name, | |
1213 | const char *type, | |
1214 | ObjectPropertyAccessor *get, | |
1215 | ObjectPropertyAccessor *set, | |
1216 | ObjectPropertyRelease *release, | |
d2623129 | 1217 | void *opaque) |
16bf7f52 DB |
1218 | { |
1219 | ObjectProperty *prop; | |
1220 | ||
d2623129 | 1221 | assert(!object_class_property_find(klass, name, NULL)); |
16bf7f52 DB |
1222 | |
1223 | prop = g_malloc0(sizeof(*prop)); | |
1224 | ||
1225 | prop->name = g_strdup(name); | |
1226 | prop->type = g_strdup(type); | |
1227 | ||
1228 | prop->get = get; | |
1229 | prop->set = set; | |
1230 | prop->release = release; | |
1231 | prop->opaque = opaque; | |
1232 | ||
ba806ffb | 1233 | g_hash_table_insert(klass->properties, prop->name, prop); |
16bf7f52 DB |
1234 | |
1235 | return prop; | |
1236 | } | |
1237 | ||
89bfe000 PB |
1238 | ObjectProperty *object_property_find(Object *obj, const char *name, |
1239 | Error **errp) | |
57c9fafe AL |
1240 | { |
1241 | ObjectProperty *prop; | |
16bf7f52 DB |
1242 | ObjectClass *klass = object_get_class(obj); |
1243 | ||
1244 | prop = object_class_property_find(klass, name, NULL); | |
1245 | if (prop) { | |
1246 | return prop; | |
1247 | } | |
57c9fafe | 1248 | |
b604a854 PF |
1249 | prop = g_hash_table_lookup(obj->properties, name); |
1250 | if (prop) { | |
1251 | return prop; | |
57c9fafe AL |
1252 | } |
1253 | ||
f231b88d | 1254 | error_setg(errp, "Property '.%s' not found", name); |
57c9fafe AL |
1255 | return NULL; |
1256 | } | |
1257 | ||
7746abd8 DB |
1258 | void object_property_iter_init(ObjectPropertyIterator *iter, |
1259 | Object *obj) | |
a00c9482 | 1260 | { |
7746abd8 DB |
1261 | g_hash_table_iter_init(&iter->iter, obj->properties); |
1262 | iter->nextclass = object_get_class(obj); | |
a00c9482 DB |
1263 | } |
1264 | ||
1265 | ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter) | |
1266 | { | |
b604a854 | 1267 | gpointer key, val; |
16bf7f52 DB |
1268 | while (!g_hash_table_iter_next(&iter->iter, &key, &val)) { |
1269 | if (!iter->nextclass) { | |
1270 | return NULL; | |
1271 | } | |
1272 | g_hash_table_iter_init(&iter->iter, iter->nextclass->properties); | |
1273 | iter->nextclass = object_class_get_parent(iter->nextclass); | |
a00c9482 | 1274 | } |
b604a854 | 1275 | return val; |
a00c9482 DB |
1276 | } |
1277 | ||
961c47bb AK |
1278 | void object_class_property_iter_init(ObjectPropertyIterator *iter, |
1279 | ObjectClass *klass) | |
1280 | { | |
1281 | g_hash_table_iter_init(&iter->iter, klass->properties); | |
684546d8 | 1282 | iter->nextclass = object_class_get_parent(klass); |
961c47bb AK |
1283 | } |
1284 | ||
16bf7f52 DB |
1285 | ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, |
1286 | Error **errp) | |
1287 | { | |
1288 | ObjectProperty *prop; | |
1289 | ObjectClass *parent_klass; | |
1290 | ||
1291 | parent_klass = object_class_get_parent(klass); | |
1292 | if (parent_klass) { | |
1293 | prop = object_class_property_find(parent_klass, name, NULL); | |
1294 | if (prop) { | |
1295 | return prop; | |
1296 | } | |
1297 | } | |
1298 | ||
1299 | prop = g_hash_table_lookup(klass->properties, name); | |
1300 | if (!prop) { | |
1301 | error_setg(errp, "Property '.%s' not found", name); | |
1302 | } | |
1303 | return prop; | |
1304 | } | |
1305 | ||
df4fe0b2 | 1306 | void object_property_del(Object *obj, const char *name) |
57c9fafe | 1307 | { |
b604a854 PF |
1308 | ObjectProperty *prop = g_hash_table_lookup(obj->properties, name); |
1309 | ||
0866aca1 AL |
1310 | if (prop->release) { |
1311 | prop->release(obj, name, prop->opaque); | |
1312 | } | |
b604a854 | 1313 | g_hash_table_remove(obj->properties, name); |
57c9fafe AL |
1314 | } |
1315 | ||
6fd5bef1 | 1316 | bool object_property_get(Object *obj, const char *name, Visitor *v, |
57c9fafe AL |
1317 | Error **errp) |
1318 | { | |
6fd5bef1 | 1319 | Error *err = NULL; |
89bfe000 | 1320 | ObjectProperty *prop = object_property_find(obj, name, errp); |
6fd5bef1 | 1321 | |
57c9fafe | 1322 | if (prop == NULL) { |
6fd5bef1 | 1323 | return false; |
57c9fafe AL |
1324 | } |
1325 | ||
1326 | if (!prop->get) { | |
c6bd8c70 | 1327 | error_setg(errp, QERR_PERMISSION_DENIED); |
6fd5bef1 | 1328 | return false; |
57c9fafe | 1329 | } |
6fd5bef1 MA |
1330 | prop->get(obj, v, name, prop->opaque, &err); |
1331 | error_propagate(errp, err); | |
1332 | return !err; | |
57c9fafe AL |
1333 | } |
1334 | ||
6fd5bef1 | 1335 | bool object_property_set(Object *obj, const char *name, Visitor *v, |
57c9fafe AL |
1336 | Error **errp) |
1337 | { | |
6fd5bef1 | 1338 | Error *err = NULL; |
89bfe000 | 1339 | ObjectProperty *prop = object_property_find(obj, name, errp); |
6fd5bef1 | 1340 | |
57c9fafe | 1341 | if (prop == NULL) { |
6fd5bef1 | 1342 | return false; |
57c9fafe AL |
1343 | } |
1344 | ||
1345 | if (!prop->set) { | |
c6bd8c70 | 1346 | error_setg(errp, QERR_PERMISSION_DENIED); |
6fd5bef1 | 1347 | return false; |
57c9fafe | 1348 | } |
6fd5bef1 MA |
1349 | prop->set(obj, v, name, prop->opaque, &err); |
1350 | error_propagate(errp, err); | |
1351 | return !err; | |
57c9fafe AL |
1352 | } |
1353 | ||
6fd5bef1 | 1354 | bool object_property_set_str(Object *obj, const char *name, |
5325cc34 | 1355 | const char *value, Error **errp) |
7b7b7d18 PB |
1356 | { |
1357 | QString *qstr = qstring_from_str(value); | |
6fd5bef1 | 1358 | bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp); |
7b7b7d18 | 1359 | |
cb3e7f08 | 1360 | qobject_unref(qstr); |
6fd5bef1 | 1361 | return ok; |
7b7b7d18 PB |
1362 | } |
1363 | ||
1364 | char *object_property_get_str(Object *obj, const char *name, | |
1365 | Error **errp) | |
1366 | { | |
1367 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
7b7b7d18 PB |
1368 | char *retval; |
1369 | ||
1370 | if (!ret) { | |
1371 | return NULL; | |
1372 | } | |
aafb21a0 PX |
1373 | |
1374 | retval = g_strdup(qobject_get_try_str(ret)); | |
1375 | if (!retval) { | |
c6bd8c70 | 1376 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
7b7b7d18 PB |
1377 | } |
1378 | ||
cb3e7f08 | 1379 | qobject_unref(ret); |
7b7b7d18 PB |
1380 | return retval; |
1381 | } | |
1382 | ||
6fd5bef1 | 1383 | bool object_property_set_link(Object *obj, const char *name, |
5325cc34 | 1384 | Object *value, Error **errp) |
1d9c5a12 | 1385 | { |
6fd5bef1 MA |
1386 | g_autofree char *path = NULL; |
1387 | ||
d3c49316 | 1388 | if (value) { |
6fd5bef1 | 1389 | path = object_get_canonical_path(value); |
d3c49316 | 1390 | } |
6fd5bef1 | 1391 | return object_property_set_str(obj, name, path ?: "", errp); |
1d9c5a12 PB |
1392 | } |
1393 | ||
1394 | Object *object_property_get_link(Object *obj, const char *name, | |
1395 | Error **errp) | |
1396 | { | |
1397 | char *str = object_property_get_str(obj, name, errp); | |
1398 | Object *target = NULL; | |
1399 | ||
1400 | if (str && *str) { | |
1401 | target = object_resolve_path(str, NULL); | |
1402 | if (!target) { | |
75158ebb MA |
1403 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1404 | "Device '%s' not found", str); | |
1d9c5a12 PB |
1405 | } |
1406 | } | |
1407 | ||
1408 | g_free(str); | |
1409 | return target; | |
1410 | } | |
1411 | ||
6fd5bef1 | 1412 | bool object_property_set_bool(Object *obj, const char *name, |
5325cc34 | 1413 | bool value, Error **errp) |
7b7b7d18 | 1414 | { |
fc48ffc3 | 1415 | QBool *qbool = qbool_from_bool(value); |
6fd5bef1 | 1416 | bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp); |
7b7b7d18 | 1417 | |
cb3e7f08 | 1418 | qobject_unref(qbool); |
6fd5bef1 | 1419 | return ok; |
7b7b7d18 PB |
1420 | } |
1421 | ||
1422 | bool object_property_get_bool(Object *obj, const char *name, | |
1423 | Error **errp) | |
1424 | { | |
1425 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1426 | QBool *qbool; | |
1427 | bool retval; | |
1428 | ||
1429 | if (!ret) { | |
1430 | return false; | |
1431 | } | |
7dc847eb | 1432 | qbool = qobject_to(QBool, ret); |
7b7b7d18 | 1433 | if (!qbool) { |
c6bd8c70 | 1434 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
7b7b7d18 PB |
1435 | retval = false; |
1436 | } else { | |
fc48ffc3 | 1437 | retval = qbool_get_bool(qbool); |
7b7b7d18 PB |
1438 | } |
1439 | ||
cb3e7f08 | 1440 | qobject_unref(ret); |
7b7b7d18 PB |
1441 | return retval; |
1442 | } | |
1443 | ||
6fd5bef1 | 1444 | bool object_property_set_int(Object *obj, const char *name, |
5325cc34 | 1445 | int64_t value, Error **errp) |
7b7b7d18 | 1446 | { |
01b2ffce | 1447 | QNum *qnum = qnum_from_int(value); |
6fd5bef1 | 1448 | bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp); |
7b7b7d18 | 1449 | |
cb3e7f08 | 1450 | qobject_unref(qnum); |
6fd5bef1 | 1451 | return ok; |
7b7b7d18 PB |
1452 | } |
1453 | ||
1454 | int64_t object_property_get_int(Object *obj, const char *name, | |
1455 | Error **errp) | |
1456 | { | |
1457 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
01b2ffce | 1458 | QNum *qnum; |
7b7b7d18 PB |
1459 | int64_t retval; |
1460 | ||
1461 | if (!ret) { | |
1462 | return -1; | |
1463 | } | |
01b2ffce | 1464 | |
7dc847eb | 1465 | qnum = qobject_to(QNum, ret); |
01b2ffce | 1466 | if (!qnum || !qnum_get_try_int(qnum, &retval)) { |
c6bd8c70 | 1467 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
7b7b7d18 | 1468 | retval = -1; |
7b7b7d18 PB |
1469 | } |
1470 | ||
cb3e7f08 | 1471 | qobject_unref(ret); |
7b7b7d18 | 1472 | return retval; |
b2cd7dee PB |
1473 | } |
1474 | ||
0e76ed0a MAL |
1475 | static void object_property_init_defval(Object *obj, ObjectProperty *prop) |
1476 | { | |
1477 | Visitor *v = qobject_input_visitor_new(prop->defval); | |
1478 | ||
1479 | assert(prop->set != NULL); | |
1480 | prop->set(obj, v, prop->name, prop->opaque, &error_abort); | |
1481 | ||
1482 | visit_free(v); | |
1483 | } | |
1484 | ||
1485 | static void object_property_set_default(ObjectProperty *prop, QObject *defval) | |
1486 | { | |
1487 | assert(!prop->defval); | |
1488 | assert(!prop->init); | |
1489 | ||
1490 | prop->defval = defval; | |
1491 | prop->init = object_property_init_defval; | |
1492 | } | |
1493 | ||
1494 | void object_property_set_default_bool(ObjectProperty *prop, bool value) | |
1495 | { | |
1496 | object_property_set_default(prop, QOBJECT(qbool_from_bool(value))); | |
1497 | } | |
1498 | ||
1499 | void object_property_set_default_str(ObjectProperty *prop, const char *value) | |
1500 | { | |
1501 | object_property_set_default(prop, QOBJECT(qstring_from_str(value))); | |
1502 | } | |
1503 | ||
1504 | void object_property_set_default_int(ObjectProperty *prop, int64_t value) | |
1505 | { | |
1506 | object_property_set_default(prop, QOBJECT(qnum_from_int(value))); | |
1507 | } | |
1508 | ||
1509 | void object_property_set_default_uint(ObjectProperty *prop, uint64_t value) | |
1510 | { | |
1511 | object_property_set_default(prop, QOBJECT(qnum_from_uint(value))); | |
1512 | } | |
1513 | ||
6fd5bef1 | 1514 | bool object_property_set_uint(Object *obj, const char *name, |
5325cc34 | 1515 | uint64_t value, Error **errp) |
3152779c MAL |
1516 | { |
1517 | QNum *qnum = qnum_from_uint(value); | |
6fd5bef1 | 1518 | bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp); |
3152779c | 1519 | |
cb3e7f08 | 1520 | qobject_unref(qnum); |
6fd5bef1 | 1521 | return ok; |
3152779c MAL |
1522 | } |
1523 | ||
1524 | uint64_t object_property_get_uint(Object *obj, const char *name, | |
1525 | Error **errp) | |
1526 | { | |
1527 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1528 | QNum *qnum; | |
1529 | uint64_t retval; | |
1530 | ||
1531 | if (!ret) { | |
1532 | return 0; | |
1533 | } | |
7dc847eb | 1534 | qnum = qobject_to(QNum, ret); |
3152779c MAL |
1535 | if (!qnum || !qnum_get_try_uint(qnum, &retval)) { |
1536 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint"); | |
1537 | retval = 0; | |
1538 | } | |
1539 | ||
cb3e7f08 | 1540 | qobject_unref(ret); |
3152779c MAL |
1541 | return retval; |
1542 | } | |
1543 | ||
a8e3fbed | 1544 | typedef struct EnumProperty { |
f7abe0ec | 1545 | const QEnumLookup *lookup; |
a8e3fbed DB |
1546 | int (*get)(Object *, Error **); |
1547 | void (*set)(Object *, int, Error **); | |
1548 | } EnumProperty; | |
1549 | ||
1f21772d | 1550 | int object_property_get_enum(Object *obj, const char *name, |
a3590dac | 1551 | const char *typename, Error **errp) |
1f21772d | 1552 | { |
976620ac | 1553 | char *str; |
1f21772d | 1554 | int ret; |
a3590dac DB |
1555 | ObjectProperty *prop = object_property_find(obj, name, errp); |
1556 | EnumProperty *enumprop; | |
1557 | ||
1558 | if (prop == NULL) { | |
1559 | return 0; | |
1560 | } | |
1561 | ||
1562 | if (!g_str_equal(prop->type, typename)) { | |
1563 | error_setg(errp, "Property %s on %s is not '%s' enum type", | |
1564 | name, object_class_get_name( | |
1565 | object_get_class(obj)), typename); | |
1566 | return 0; | |
1567 | } | |
1568 | ||
1569 | enumprop = prop->opaque; | |
1f21772d | 1570 | |
b555f89f MA |
1571 | str = object_property_get_str(obj, name, errp); |
1572 | if (!str) { | |
4715d42e MA |
1573 | return 0; |
1574 | } | |
976620ac | 1575 | |
ea097dff | 1576 | ret = qapi_enum_parse(enumprop->lookup, str, -1, errp); |
976620ac | 1577 | g_free(str); |
1f21772d HT |
1578 | |
1579 | return ret; | |
1580 | } | |
1581 | ||
6fd5bef1 | 1582 | bool object_property_parse(Object *obj, const char *name, |
5325cc34 | 1583 | const char *string, Error **errp) |
b2cd7dee | 1584 | { |
7a0525c7 | 1585 | Visitor *v = string_input_visitor_new(string); |
6fd5bef1 MA |
1586 | bool ok = object_property_set(obj, name, v, errp); |
1587 | ||
7a0525c7 | 1588 | visit_free(v); |
6fd5bef1 | 1589 | return ok; |
b2cd7dee PB |
1590 | } |
1591 | ||
0b7593e0 | 1592 | char *object_property_print(Object *obj, const char *name, bool human, |
b2cd7dee PB |
1593 | Error **errp) |
1594 | { | |
3b098d56 | 1595 | Visitor *v; |
3a53009f | 1596 | char *string = NULL; |
b2cd7dee | 1597 | |
3b098d56 | 1598 | v = string_output_visitor_new(human, &string); |
af175e85 | 1599 | if (!object_property_get(obj, name, v, errp)) { |
3a53009f GA |
1600 | goto out; |
1601 | } | |
1602 | ||
3b098d56 | 1603 | visit_complete(v, &string); |
3a53009f GA |
1604 | |
1605 | out: | |
3b098d56 | 1606 | visit_free(v); |
b2cd7dee | 1607 | return string; |
7b7b7d18 PB |
1608 | } |
1609 | ||
57c9fafe AL |
1610 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
1611 | { | |
89bfe000 | 1612 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1613 | if (prop == NULL) { |
57c9fafe AL |
1614 | return NULL; |
1615 | } | |
1616 | ||
1617 | return prop->type; | |
1618 | } | |
1619 | ||
1620 | Object *object_get_root(void) | |
1621 | { | |
8b45d447 | 1622 | static Object *root; |
57c9fafe | 1623 | |
8b45d447 AL |
1624 | if (!root) { |
1625 | root = object_new("container"); | |
57c9fafe AL |
1626 | } |
1627 | ||
8b45d447 | 1628 | return root; |
57c9fafe AL |
1629 | } |
1630 | ||
bc2256c4 DB |
1631 | Object *object_get_objects_root(void) |
1632 | { | |
1633 | return container_get(object_get_root(), "/objects"); | |
1634 | } | |
1635 | ||
7c47c4ea PX |
1636 | Object *object_get_internal_root(void) |
1637 | { | |
1638 | static Object *internal_root; | |
1639 | ||
1640 | if (!internal_root) { | |
1641 | internal_root = object_new("container"); | |
1642 | } | |
1643 | ||
1644 | return internal_root; | |
1645 | } | |
1646 | ||
d7bce999 EB |
1647 | static void object_get_child_property(Object *obj, Visitor *v, |
1648 | const char *name, void *opaque, | |
1649 | Error **errp) | |
57c9fafe AL |
1650 | { |
1651 | Object *child = opaque; | |
ddfb0baa | 1652 | char *path; |
57c9fafe AL |
1653 | |
1654 | path = object_get_canonical_path(child); | |
51e72bc1 | 1655 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1656 | g_free(path); |
1657 | } | |
1658 | ||
ddfb0baa MA |
1659 | static Object *object_resolve_child_property(Object *parent, void *opaque, |
1660 | const char *part) | |
64607d08 PB |
1661 | { |
1662 | return opaque; | |
1663 | } | |
1664 | ||
db85b575 AL |
1665 | static void object_finalize_child_property(Object *obj, const char *name, |
1666 | void *opaque) | |
1667 | { | |
1668 | Object *child = opaque; | |
1669 | ||
bffc687d PB |
1670 | if (child->class->unparent) { |
1671 | (child->class->unparent)(child); | |
1672 | } | |
1673 | child->parent = NULL; | |
db85b575 AL |
1674 | object_unref(child); |
1675 | } | |
1676 | ||
70251887 | 1677 | ObjectProperty * |
db57fef1 EA |
1678 | object_property_try_add_child(Object *obj, const char *name, |
1679 | Object *child, Error **errp) | |
57c9fafe | 1680 | { |
70251887 | 1681 | g_autofree char *type = NULL; |
64607d08 | 1682 | ObjectProperty *op; |
57c9fafe | 1683 | |
d2623129 | 1684 | assert(!child->parent); |
8faa2f85 | 1685 | |
688ffbb4 | 1686 | type = g_strdup_printf("child<%s>", object_get_typename(child)); |
57c9fafe | 1687 | |
db57fef1 EA |
1688 | op = object_property_try_add(obj, name, type, object_get_child_property, |
1689 | NULL, object_finalize_child_property, | |
1690 | child, errp); | |
1691 | if (!op) { | |
1692 | return NULL; | |
1693 | } | |
64607d08 | 1694 | op->resolve = object_resolve_child_property; |
57c9fafe | 1695 | object_ref(child); |
57c9fafe | 1696 | child->parent = obj; |
70251887 | 1697 | return op; |
57c9fafe AL |
1698 | } |
1699 | ||
db57fef1 EA |
1700 | ObjectProperty * |
1701 | object_property_add_child(Object *obj, const char *name, | |
1702 | Object *child) | |
1703 | { | |
1704 | return object_property_try_add_child(obj, name, child, &error_abort); | |
1705 | } | |
1706 | ||
8f5d58ef | 1707 | void object_property_allow_set_link(const Object *obj, const char *name, |
39f72ef9 SH |
1708 | Object *val, Error **errp) |
1709 | { | |
1710 | /* Allow the link to be set, always */ | |
1711 | } | |
1712 | ||
9561fda8 | 1713 | typedef struct { |
9941d37b MAL |
1714 | union { |
1715 | Object **targetp; | |
1716 | Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */ | |
840ecdfb | 1717 | ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */ |
9941d37b | 1718 | }; |
8f5d58ef | 1719 | void (*check)(const Object *, const char *, Object *, Error **); |
9561fda8 SH |
1720 | ObjectPropertyLinkFlags flags; |
1721 | } LinkProperty; | |
1722 | ||
9941d37b MAL |
1723 | static Object ** |
1724 | object_link_get_targetp(Object *obj, LinkProperty *lprop) | |
1725 | { | |
1726 | if (lprop->flags & OBJ_PROP_LINK_DIRECT) { | |
1727 | return &lprop->target; | |
840ecdfb MAL |
1728 | } else if (lprop->flags & OBJ_PROP_LINK_CLASS) { |
1729 | return (void *)obj + lprop->offset; | |
9941d37b MAL |
1730 | } else { |
1731 | return lprop->targetp; | |
1732 | } | |
1733 | } | |
1734 | ||
d7bce999 EB |
1735 | static void object_get_link_property(Object *obj, Visitor *v, |
1736 | const char *name, void *opaque, | |
1737 | Error **errp) | |
57c9fafe | 1738 | { |
9561fda8 | 1739 | LinkProperty *lprop = opaque; |
9941d37b | 1740 | Object **targetp = object_link_get_targetp(obj, lprop); |
ddfb0baa | 1741 | char *path; |
57c9fafe | 1742 | |
36854207 MAL |
1743 | if (*targetp) { |
1744 | path = object_get_canonical_path(*targetp); | |
51e72bc1 | 1745 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1746 | g_free(path); |
1747 | } else { | |
ddfb0baa | 1748 | path = (char *)""; |
51e72bc1 | 1749 | visit_type_str(v, name, &path, errp); |
57c9fafe AL |
1750 | } |
1751 | } | |
1752 | ||
f5ec6704 SH |
1753 | /* |
1754 | * object_resolve_link: | |
1755 | * | |
1756 | * Lookup an object and ensure its type matches the link property type. This | |
1757 | * is similar to object_resolve_path() except type verification against the | |
1758 | * link property is performed. | |
1759 | * | |
1760 | * Returns: The matched object or NULL on path lookup failures. | |
1761 | */ | |
1762 | static Object *object_resolve_link(Object *obj, const char *name, | |
1763 | const char *path, Error **errp) | |
1764 | { | |
1765 | const char *type; | |
ddfb0baa | 1766 | char *target_type; |
f5ec6704 SH |
1767 | bool ambiguous = false; |
1768 | Object *target; | |
1769 | ||
1770 | /* Go from link<FOO> to FOO. */ | |
1771 | type = object_property_get_type(obj, name, NULL); | |
1772 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
1773 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
1774 | ||
1775 | if (ambiguous) { | |
455b0fde EB |
1776 | error_setg(errp, "Path '%s' does not uniquely identify an object", |
1777 | path); | |
f5ec6704 SH |
1778 | } else if (!target) { |
1779 | target = object_resolve_path(path, &ambiguous); | |
1780 | if (target || ambiguous) { | |
c6bd8c70 | 1781 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); |
f5ec6704 | 1782 | } else { |
75158ebb MA |
1783 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1784 | "Device '%s' not found", path); | |
f5ec6704 SH |
1785 | } |
1786 | target = NULL; | |
1787 | } | |
1788 | g_free(target_type); | |
1789 | ||
1790 | return target; | |
1791 | } | |
1792 | ||
d7bce999 EB |
1793 | static void object_set_link_property(Object *obj, Visitor *v, |
1794 | const char *name, void *opaque, | |
1795 | Error **errp) | |
57c9fafe | 1796 | { |
c6aed983 | 1797 | Error *local_err = NULL; |
9561fda8 | 1798 | LinkProperty *prop = opaque; |
9941d37b | 1799 | Object **targetp = object_link_get_targetp(obj, prop); |
36854207 | 1800 | Object *old_target = *targetp; |
1c94a351 | 1801 | Object *new_target; |
c6aed983 | 1802 | char *path = NULL; |
57c9fafe | 1803 | |
1c94a351 MA |
1804 | if (!visit_type_str(v, name, &path, errp)) { |
1805 | return; | |
1806 | } | |
57c9fafe | 1807 | |
1c94a351 MA |
1808 | if (*path) { |
1809 | new_target = object_resolve_link(obj, name, path, errp); | |
1810 | if (!new_target) { | |
1811 | g_free(path); | |
1812 | return; | |
1813 | } | |
1814 | } else { | |
1815 | new_target = NULL; | |
57c9fafe AL |
1816 | } |
1817 | ||
1818 | g_free(path); | |
f0cdc966 | 1819 | |
39f72ef9 SH |
1820 | prop->check(obj, name, new_target, &local_err); |
1821 | if (local_err) { | |
1822 | error_propagate(errp, local_err); | |
1823 | return; | |
1824 | } | |
1825 | ||
36854207 | 1826 | *targetp = new_target; |
8770bafd | 1827 | if (prop->flags & OBJ_PROP_LINK_STRONG) { |
265b578c MAL |
1828 | object_ref(new_target); |
1829 | object_unref(old_target); | |
1830 | } | |
57c9fafe AL |
1831 | } |
1832 | ||
ddfb0baa MA |
1833 | static Object *object_resolve_link_property(Object *parent, void *opaque, |
1834 | const char *part) | |
64607d08 PB |
1835 | { |
1836 | LinkProperty *lprop = opaque; | |
1837 | ||
9941d37b | 1838 | return *object_link_get_targetp(parent, lprop); |
64607d08 PB |
1839 | } |
1840 | ||
9561fda8 SH |
1841 | static void object_release_link_property(Object *obj, const char *name, |
1842 | void *opaque) | |
1843 | { | |
1844 | LinkProperty *prop = opaque; | |
9941d37b | 1845 | Object **targetp = object_link_get_targetp(obj, prop); |
9561fda8 | 1846 | |
9941d37b MAL |
1847 | if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) { |
1848 | object_unref(*targetp); | |
9561fda8 | 1849 | } |
840ecdfb MAL |
1850 | if (!(prop->flags & OBJ_PROP_LINK_CLASS)) { |
1851 | g_free(prop); | |
1852 | } | |
9561fda8 SH |
1853 | } |
1854 | ||
70251887 MA |
1855 | static ObjectProperty * |
1856 | object_add_link_prop(Object *obj, const char *name, | |
1857 | const char *type, void *ptr, | |
1858 | void (*check)(const Object *, const char *, | |
1859 | Object *, Error **), | |
d2623129 | 1860 | ObjectPropertyLinkFlags flags) |
57c9fafe | 1861 | { |
9561fda8 | 1862 | LinkProperty *prop = g_malloc(sizeof(*prop)); |
70251887 | 1863 | g_autofree char *full_type = NULL; |
64607d08 | 1864 | ObjectProperty *op; |
57c9fafe | 1865 | |
4a8d5798 MAL |
1866 | if (flags & OBJ_PROP_LINK_DIRECT) { |
1867 | prop->target = ptr; | |
1868 | } else { | |
1869 | prop->targetp = ptr; | |
1870 | } | |
39f72ef9 | 1871 | prop->check = check; |
9561fda8 SH |
1872 | prop->flags = flags; |
1873 | ||
57c9fafe AL |
1874 | full_type = g_strdup_printf("link<%s>", type); |
1875 | ||
64607d08 PB |
1876 | op = object_property_add(obj, name, full_type, |
1877 | object_get_link_property, | |
1878 | check ? object_set_link_property : NULL, | |
1879 | object_release_link_property, | |
d2623129 | 1880 | prop); |
64607d08 | 1881 | op->resolve = object_resolve_link_property; |
70251887 | 1882 | return op; |
57c9fafe AL |
1883 | } |
1884 | ||
70251887 MA |
1885 | ObjectProperty * |
1886 | object_property_add_link(Object *obj, const char *name, | |
1887 | const char *type, Object **targetp, | |
1888 | void (*check)(const Object *, const char *, | |
1889 | Object *, Error **), | |
d2623129 | 1890 | ObjectPropertyLinkFlags flags) |
4a8d5798 | 1891 | { |
d2623129 | 1892 | return object_add_link_prop(obj, name, type, targetp, check, flags); |
4a8d5798 MAL |
1893 | } |
1894 | ||
840ecdfb MAL |
1895 | ObjectProperty * |
1896 | object_class_property_add_link(ObjectClass *oc, | |
1897 | const char *name, | |
1898 | const char *type, ptrdiff_t offset, | |
1899 | void (*check)(const Object *obj, const char *name, | |
1900 | Object *val, Error **errp), | |
d2623129 | 1901 | ObjectPropertyLinkFlags flags) |
840ecdfb | 1902 | { |
840ecdfb | 1903 | LinkProperty *prop = g_new0(LinkProperty, 1); |
ddfb0baa | 1904 | char *full_type; |
840ecdfb MAL |
1905 | ObjectProperty *op; |
1906 | ||
1907 | prop->offset = offset; | |
1908 | prop->check = check; | |
1909 | prop->flags = flags | OBJ_PROP_LINK_CLASS; | |
1910 | ||
1911 | full_type = g_strdup_printf("link<%s>", type); | |
1912 | ||
1913 | op = object_class_property_add(oc, name, full_type, | |
1914 | object_get_link_property, | |
1915 | check ? object_set_link_property : NULL, | |
1916 | object_release_link_property, | |
d2623129 | 1917 | prop); |
840ecdfb MAL |
1918 | |
1919 | op->resolve = object_resolve_link_property; | |
1920 | ||
840ecdfb MAL |
1921 | g_free(full_type); |
1922 | return op; | |
1923 | } | |
1924 | ||
70251887 MA |
1925 | ObjectProperty * |
1926 | object_property_add_const_link(Object *obj, const char *name, | |
d2623129 | 1927 | Object *target) |
fb9e7e33 | 1928 | { |
70251887 MA |
1929 | return object_add_link_prop(obj, name, |
1930 | object_get_typename(target), target, | |
d2623129 | 1931 | NULL, OBJ_PROP_LINK_DIRECT); |
fb9e7e33 PB |
1932 | } |
1933 | ||
7a309cc9 | 1934 | const char *object_get_canonical_path_component(const Object *obj) |
11f590b1 SH |
1935 | { |
1936 | ObjectProperty *prop = NULL; | |
b604a854 | 1937 | GHashTableIter iter; |
11f590b1 | 1938 | |
770dec26 PB |
1939 | if (obj->parent == NULL) { |
1940 | return NULL; | |
1941 | } | |
11f590b1 | 1942 | |
b604a854 PF |
1943 | g_hash_table_iter_init(&iter, obj->parent->properties); |
1944 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
11f590b1 SH |
1945 | if (!object_property_is_child(prop)) { |
1946 | continue; | |
1947 | } | |
1948 | ||
1949 | if (prop->opaque == obj) { | |
7a309cc9 | 1950 | return prop->name; |
11f590b1 SH |
1951 | } |
1952 | } | |
1953 | ||
1954 | /* obj had a parent but was not a child, should never happen */ | |
1955 | g_assert_not_reached(); | |
1956 | return NULL; | |
1957 | } | |
1958 | ||
e8512dfa | 1959 | char *object_get_canonical_path(const Object *obj) |
57c9fafe AL |
1960 | { |
1961 | Object *root = object_get_root(); | |
11f590b1 | 1962 | char *newpath, *path = NULL; |
57c9fafe | 1963 | |
e40077fd PB |
1964 | if (obj == root) { |
1965 | return g_strdup("/"); | |
1966 | } | |
1967 | ||
1968 | do { | |
7a309cc9 | 1969 | const char *component = object_get_canonical_path_component(obj); |
57c9fafe | 1970 | |
e40077fd PB |
1971 | if (!component) { |
1972 | /* A canonical path must be complete, so discard what was | |
1973 | * collected so far. | |
1974 | */ | |
11f590b1 | 1975 | g_free(path); |
e40077fd | 1976 | return NULL; |
57c9fafe AL |
1977 | } |
1978 | ||
e40077fd PB |
1979 | newpath = g_strdup_printf("/%s%s", component, path ? path : ""); |
1980 | g_free(path); | |
e40077fd | 1981 | path = newpath; |
57c9fafe | 1982 | obj = obj->parent; |
e40077fd | 1983 | } while (obj != root); |
57c9fafe | 1984 | |
e40077fd | 1985 | return path; |
57c9fafe AL |
1986 | } |
1987 | ||
ddfb0baa | 1988 | Object *object_resolve_path_component(Object *parent, const char *part) |
a612b2a6 | 1989 | { |
89bfe000 | 1990 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
a612b2a6 PB |
1991 | if (prop == NULL) { |
1992 | return NULL; | |
1993 | } | |
1994 | ||
64607d08 PB |
1995 | if (prop->resolve) { |
1996 | return prop->resolve(parent, prop->opaque, part); | |
a612b2a6 PB |
1997 | } else { |
1998 | return NULL; | |
1999 | } | |
2000 | } | |
2001 | ||
57c9fafe | 2002 | static Object *object_resolve_abs_path(Object *parent, |
ad195c8f MY |
2003 | char **parts, |
2004 | const char *typename) | |
57c9fafe | 2005 | { |
57c9fafe AL |
2006 | Object *child; |
2007 | ||
ad195c8f | 2008 | if (*parts == NULL) { |
02fe2db6 | 2009 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
2010 | } |
2011 | ||
ad195c8f MY |
2012 | if (strcmp(*parts, "") == 0) { |
2013 | return object_resolve_abs_path(parent, parts + 1, typename); | |
57c9fafe AL |
2014 | } |
2015 | ||
ad195c8f | 2016 | child = object_resolve_path_component(parent, *parts); |
57c9fafe AL |
2017 | if (!child) { |
2018 | return NULL; | |
2019 | } | |
2020 | ||
ad195c8f | 2021 | return object_resolve_abs_path(child, parts + 1, typename); |
57c9fafe AL |
2022 | } |
2023 | ||
2024 | static Object *object_resolve_partial_path(Object *parent, | |
ddfb0baa MA |
2025 | char **parts, |
2026 | const char *typename, | |
2027 | bool *ambiguous) | |
57c9fafe AL |
2028 | { |
2029 | Object *obj; | |
b604a854 | 2030 | GHashTableIter iter; |
57c9fafe AL |
2031 | ObjectProperty *prop; |
2032 | ||
ad195c8f | 2033 | obj = object_resolve_abs_path(parent, parts, typename); |
57c9fafe | 2034 | |
b604a854 PF |
2035 | g_hash_table_iter_init(&iter, parent->properties); |
2036 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
57c9fafe AL |
2037 | Object *found; |
2038 | ||
5d9d3f47 | 2039 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
2040 | continue; |
2041 | } | |
2042 | ||
02fe2db6 PB |
2043 | found = object_resolve_partial_path(prop->opaque, parts, |
2044 | typename, ambiguous); | |
57c9fafe AL |
2045 | if (found) { |
2046 | if (obj) { | |
ebcc479e | 2047 | *ambiguous = true; |
57c9fafe AL |
2048 | return NULL; |
2049 | } | |
2050 | obj = found; | |
2051 | } | |
2052 | ||
ebcc479e | 2053 | if (*ambiguous) { |
57c9fafe AL |
2054 | return NULL; |
2055 | } | |
2056 | } | |
2057 | ||
2058 | return obj; | |
2059 | } | |
2060 | ||
02fe2db6 | 2061 | Object *object_resolve_path_type(const char *path, const char *typename, |
ebcc479e | 2062 | bool *ambiguousp) |
57c9fafe | 2063 | { |
57c9fafe | 2064 | Object *obj; |
ddfb0baa | 2065 | char **parts; |
57c9fafe AL |
2066 | |
2067 | parts = g_strsplit(path, "/", 0); | |
2e1103f6 | 2068 | assert(parts); |
57c9fafe | 2069 | |
2e1103f6 | 2070 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
ebcc479e | 2071 | bool ambiguous = false; |
02fe2db6 | 2072 | obj = object_resolve_partial_path(object_get_root(), parts, |
ebcc479e EH |
2073 | typename, &ambiguous); |
2074 | if (ambiguousp) { | |
2075 | *ambiguousp = ambiguous; | |
2076 | } | |
57c9fafe | 2077 | } else { |
ad195c8f | 2078 | obj = object_resolve_abs_path(object_get_root(), parts + 1, typename); |
57c9fafe AL |
2079 | } |
2080 | ||
2081 | g_strfreev(parts); | |
2082 | ||
2083 | return obj; | |
2084 | } | |
2085 | ||
02fe2db6 PB |
2086 | Object *object_resolve_path(const char *path, bool *ambiguous) |
2087 | { | |
2088 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
2089 | } | |
2090 | ||
57c9fafe AL |
2091 | typedef struct StringProperty |
2092 | { | |
2093 | char *(*get)(Object *, Error **); | |
2094 | void (*set)(Object *, const char *, Error **); | |
2095 | } StringProperty; | |
2096 | ||
d7bce999 EB |
2097 | static void property_get_str(Object *obj, Visitor *v, const char *name, |
2098 | void *opaque, Error **errp) | |
57c9fafe AL |
2099 | { |
2100 | StringProperty *prop = opaque; | |
2101 | char *value; | |
e1c8237d | 2102 | Error *err = NULL; |
57c9fafe | 2103 | |
e1c8237d MA |
2104 | value = prop->get(obj, &err); |
2105 | if (err) { | |
2106 | error_propagate(errp, err); | |
2107 | return; | |
57c9fafe | 2108 | } |
e1c8237d | 2109 | |
51e72bc1 | 2110 | visit_type_str(v, name, &value, errp); |
e1c8237d | 2111 | g_free(value); |
57c9fafe AL |
2112 | } |
2113 | ||
d7bce999 EB |
2114 | static void property_set_str(Object *obj, Visitor *v, const char *name, |
2115 | void *opaque, Error **errp) | |
57c9fafe AL |
2116 | { |
2117 | StringProperty *prop = opaque; | |
2118 | char *value; | |
57c9fafe | 2119 | |
668f62ec | 2120 | if (!visit_type_str(v, name, &value, errp)) { |
57c9fafe AL |
2121 | return; |
2122 | } | |
2123 | ||
2124 | prop->set(obj, value, errp); | |
2125 | g_free(value); | |
2126 | } | |
2127 | ||
7b7b7d18 PB |
2128 | static void property_release_str(Object *obj, const char *name, |
2129 | void *opaque) | |
57c9fafe AL |
2130 | { |
2131 | StringProperty *prop = opaque; | |
2132 | g_free(prop); | |
2133 | } | |
2134 | ||
70251887 MA |
2135 | ObjectProperty * |
2136 | object_property_add_str(Object *obj, const char *name, | |
2137 | char *(*get)(Object *, Error **), | |
d2623129 | 2138 | void (*set)(Object *, const char *, Error **)) |
57c9fafe AL |
2139 | { |
2140 | StringProperty *prop = g_malloc0(sizeof(*prop)); | |
2141 | ||
2142 | prop->get = get; | |
2143 | prop->set = set; | |
2144 | ||
d2623129 MA |
2145 | return object_property_add(obj, name, "string", |
2146 | get ? property_get_str : NULL, | |
2147 | set ? property_set_str : NULL, | |
2148 | property_release_str, | |
2149 | prop); | |
57c9fafe | 2150 | } |
745549c8 | 2151 | |
a3a16211 MAL |
2152 | ObjectProperty * |
2153 | object_class_property_add_str(ObjectClass *klass, const char *name, | |
16bf7f52 DB |
2154 | char *(*get)(Object *, Error **), |
2155 | void (*set)(Object *, const char *, | |
d2623129 | 2156 | Error **)) |
16bf7f52 | 2157 | { |
16bf7f52 DB |
2158 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
2159 | ||
2160 | prop->get = get; | |
2161 | prop->set = set; | |
2162 | ||
d2623129 MA |
2163 | return object_class_property_add(klass, name, "string", |
2164 | get ? property_get_str : NULL, | |
2165 | set ? property_set_str : NULL, | |
2166 | NULL, | |
2167 | prop); | |
16bf7f52 DB |
2168 | } |
2169 | ||
0e558843 AL |
2170 | typedef struct BoolProperty |
2171 | { | |
2172 | bool (*get)(Object *, Error **); | |
2173 | void (*set)(Object *, bool, Error **); | |
2174 | } BoolProperty; | |
2175 | ||
d7bce999 EB |
2176 | static void property_get_bool(Object *obj, Visitor *v, const char *name, |
2177 | void *opaque, Error **errp) | |
0e558843 AL |
2178 | { |
2179 | BoolProperty *prop = opaque; | |
2180 | bool value; | |
4715d42e MA |
2181 | Error *err = NULL; |
2182 | ||
2183 | value = prop->get(obj, &err); | |
2184 | if (err) { | |
2185 | error_propagate(errp, err); | |
2186 | return; | |
2187 | } | |
0e558843 | 2188 | |
51e72bc1 | 2189 | visit_type_bool(v, name, &value, errp); |
0e558843 AL |
2190 | } |
2191 | ||
d7bce999 EB |
2192 | static void property_set_bool(Object *obj, Visitor *v, const char *name, |
2193 | void *opaque, Error **errp) | |
0e558843 AL |
2194 | { |
2195 | BoolProperty *prop = opaque; | |
2196 | bool value; | |
0e558843 | 2197 | |
668f62ec | 2198 | if (!visit_type_bool(v, name, &value, errp)) { |
0e558843 AL |
2199 | return; |
2200 | } | |
2201 | ||
2202 | prop->set(obj, value, errp); | |
2203 | } | |
2204 | ||
2205 | static void property_release_bool(Object *obj, const char *name, | |
2206 | void *opaque) | |
2207 | { | |
2208 | BoolProperty *prop = opaque; | |
2209 | g_free(prop); | |
2210 | } | |
2211 | ||
70251887 MA |
2212 | ObjectProperty * |
2213 | object_property_add_bool(Object *obj, const char *name, | |
2214 | bool (*get)(Object *, Error **), | |
d2623129 | 2215 | void (*set)(Object *, bool, Error **)) |
0e558843 AL |
2216 | { |
2217 | BoolProperty *prop = g_malloc0(sizeof(*prop)); | |
2218 | ||
2219 | prop->get = get; | |
2220 | prop->set = set; | |
2221 | ||
d2623129 MA |
2222 | return object_property_add(obj, name, "bool", |
2223 | get ? property_get_bool : NULL, | |
2224 | set ? property_set_bool : NULL, | |
2225 | property_release_bool, | |
2226 | prop); | |
0e558843 AL |
2227 | } |
2228 | ||
a3a16211 MAL |
2229 | ObjectProperty * |
2230 | object_class_property_add_bool(ObjectClass *klass, const char *name, | |
16bf7f52 | 2231 | bool (*get)(Object *, Error **), |
d2623129 | 2232 | void (*set)(Object *, bool, Error **)) |
16bf7f52 | 2233 | { |
16bf7f52 DB |
2234 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
2235 | ||
2236 | prop->get = get; | |
2237 | prop->set = set; | |
2238 | ||
d2623129 MA |
2239 | return object_class_property_add(klass, name, "bool", |
2240 | get ? property_get_bool : NULL, | |
2241 | set ? property_set_bool : NULL, | |
2242 | NULL, | |
2243 | prop); | |
16bf7f52 DB |
2244 | } |
2245 | ||
d7bce999 EB |
2246 | static void property_get_enum(Object *obj, Visitor *v, const char *name, |
2247 | void *opaque, Error **errp) | |
a8e3fbed DB |
2248 | { |
2249 | EnumProperty *prop = opaque; | |
2250 | int value; | |
4715d42e MA |
2251 | Error *err = NULL; |
2252 | ||
2253 | value = prop->get(obj, &err); | |
2254 | if (err) { | |
2255 | error_propagate(errp, err); | |
2256 | return; | |
2257 | } | |
a8e3fbed | 2258 | |
f7abe0ec | 2259 | visit_type_enum(v, name, &value, prop->lookup, errp); |
a8e3fbed DB |
2260 | } |
2261 | ||
d7bce999 EB |
2262 | static void property_set_enum(Object *obj, Visitor *v, const char *name, |
2263 | void *opaque, Error **errp) | |
a8e3fbed DB |
2264 | { |
2265 | EnumProperty *prop = opaque; | |
2266 | int value; | |
2267 | ||
668f62ec | 2268 | if (!visit_type_enum(v, name, &value, prop->lookup, errp)) { |
4715d42e MA |
2269 | return; |
2270 | } | |
a8e3fbed DB |
2271 | prop->set(obj, value, errp); |
2272 | } | |
2273 | ||
2274 | static void property_release_enum(Object *obj, const char *name, | |
2275 | void *opaque) | |
2276 | { | |
2277 | EnumProperty *prop = opaque; | |
2278 | g_free(prop); | |
2279 | } | |
2280 | ||
70251887 MA |
2281 | ObjectProperty * |
2282 | object_property_add_enum(Object *obj, const char *name, | |
2283 | const char *typename, | |
2284 | const QEnumLookup *lookup, | |
2285 | int (*get)(Object *, Error **), | |
d2623129 | 2286 | void (*set)(Object *, int, Error **)) |
a8e3fbed | 2287 | { |
a8e3fbed DB |
2288 | EnumProperty *prop = g_malloc(sizeof(*prop)); |
2289 | ||
f7abe0ec | 2290 | prop->lookup = lookup; |
a8e3fbed DB |
2291 | prop->get = get; |
2292 | prop->set = set; | |
2293 | ||
d2623129 MA |
2294 | return object_property_add(obj, name, typename, |
2295 | get ? property_get_enum : NULL, | |
2296 | set ? property_set_enum : NULL, | |
2297 | property_release_enum, | |
2298 | prop); | |
a8e3fbed DB |
2299 | } |
2300 | ||
a3a16211 MAL |
2301 | ObjectProperty * |
2302 | object_class_property_add_enum(ObjectClass *klass, const char *name, | |
16bf7f52 | 2303 | const char *typename, |
f7abe0ec | 2304 | const QEnumLookup *lookup, |
16bf7f52 | 2305 | int (*get)(Object *, Error **), |
d2623129 | 2306 | void (*set)(Object *, int, Error **)) |
16bf7f52 | 2307 | { |
16bf7f52 DB |
2308 | EnumProperty *prop = g_malloc(sizeof(*prop)); |
2309 | ||
f7abe0ec | 2310 | prop->lookup = lookup; |
16bf7f52 DB |
2311 | prop->get = get; |
2312 | prop->set = set; | |
2313 | ||
d2623129 MA |
2314 | return object_class_property_add(klass, name, typename, |
2315 | get ? property_get_enum : NULL, | |
2316 | set ? property_set_enum : NULL, | |
2317 | NULL, | |
2318 | prop); | |
16bf7f52 DB |
2319 | } |
2320 | ||
8e099d14 DG |
2321 | typedef struct TMProperty { |
2322 | void (*get)(Object *, struct tm *, Error **); | |
2323 | } TMProperty; | |
2324 | ||
d7bce999 EB |
2325 | static void property_get_tm(Object *obj, Visitor *v, const char *name, |
2326 | void *opaque, Error **errp) | |
8e099d14 DG |
2327 | { |
2328 | TMProperty *prop = opaque; | |
2329 | Error *err = NULL; | |
2330 | struct tm value; | |
2331 | ||
2332 | prop->get(obj, &value, &err); | |
2333 | if (err) { | |
992861fb MA |
2334 | error_propagate(errp, err); |
2335 | return; | |
8e099d14 DG |
2336 | } |
2337 | ||
992861fb MA |
2338 | if (!visit_start_struct(v, name, NULL, 0, errp)) { |
2339 | return; | |
8e099d14 | 2340 | } |
992861fb | 2341 | if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) { |
8e099d14 DG |
2342 | goto out_end; |
2343 | } | |
992861fb | 2344 | if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) { |
8e099d14 DG |
2345 | goto out_end; |
2346 | } | |
992861fb | 2347 | if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) { |
8e099d14 DG |
2348 | goto out_end; |
2349 | } | |
992861fb | 2350 | if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) { |
8e099d14 DG |
2351 | goto out_end; |
2352 | } | |
992861fb | 2353 | if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) { |
8e099d14 DG |
2354 | goto out_end; |
2355 | } | |
992861fb | 2356 | if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) { |
8e099d14 DG |
2357 | goto out_end; |
2358 | } | |
992861fb | 2359 | visit_check_struct(v, errp); |
8e099d14 | 2360 | out_end: |
1158bb2a | 2361 | visit_end_struct(v, NULL); |
8e099d14 DG |
2362 | } |
2363 | ||
2364 | static void property_release_tm(Object *obj, const char *name, | |
2365 | void *opaque) | |
2366 | { | |
2367 | TMProperty *prop = opaque; | |
2368 | g_free(prop); | |
2369 | } | |
2370 | ||
70251887 MA |
2371 | ObjectProperty * |
2372 | object_property_add_tm(Object *obj, const char *name, | |
d2623129 | 2373 | void (*get)(Object *, struct tm *, Error **)) |
8e099d14 | 2374 | { |
8e099d14 DG |
2375 | TMProperty *prop = g_malloc0(sizeof(*prop)); |
2376 | ||
2377 | prop->get = get; | |
2378 | ||
d2623129 MA |
2379 | return object_property_add(obj, name, "struct tm", |
2380 | get ? property_get_tm : NULL, NULL, | |
2381 | property_release_tm, | |
2382 | prop); | |
8e099d14 DG |
2383 | } |
2384 | ||
a3a16211 MAL |
2385 | ObjectProperty * |
2386 | object_class_property_add_tm(ObjectClass *klass, const char *name, | |
d2623129 | 2387 | void (*get)(Object *, struct tm *, Error **)) |
16bf7f52 | 2388 | { |
16bf7f52 DB |
2389 | TMProperty *prop = g_malloc0(sizeof(*prop)); |
2390 | ||
2391 | prop->get = get; | |
2392 | ||
d2623129 MA |
2393 | return object_class_property_add(klass, name, "struct tm", |
2394 | get ? property_get_tm : NULL, | |
2395 | NULL, NULL, prop); | |
16bf7f52 DB |
2396 | } |
2397 | ||
90c69fb9 | 2398 | static char *object_get_type(Object *obj, Error **errp) |
2f262e06 PB |
2399 | { |
2400 | return g_strdup(object_get_typename(obj)); | |
2401 | } | |
2402 | ||
d7bce999 EB |
2403 | static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name, |
2404 | void *opaque, Error **errp) | |
e732ea63 MT |
2405 | { |
2406 | uint8_t value = *(uint8_t *)opaque; | |
51e72bc1 | 2407 | visit_type_uint8(v, name, &value, errp); |
e732ea63 MT |
2408 | } |
2409 | ||
836e1b38 FF |
2410 | static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name, |
2411 | void *opaque, Error **errp) | |
2412 | { | |
2413 | uint8_t *field = opaque; | |
2414 | uint8_t value; | |
836e1b38 | 2415 | |
668f62ec | 2416 | if (!visit_type_uint8(v, name, &value, errp)) { |
836e1b38 FF |
2417 | return; |
2418 | } | |
2419 | ||
2420 | *field = value; | |
2421 | } | |
2422 | ||
d7bce999 EB |
2423 | static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name, |
2424 | void *opaque, Error **errp) | |
e732ea63 MT |
2425 | { |
2426 | uint16_t value = *(uint16_t *)opaque; | |
51e72bc1 | 2427 | visit_type_uint16(v, name, &value, errp); |
e732ea63 MT |
2428 | } |
2429 | ||
836e1b38 FF |
2430 | static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name, |
2431 | void *opaque, Error **errp) | |
2432 | { | |
2433 | uint16_t *field = opaque; | |
2434 | uint16_t value; | |
836e1b38 | 2435 | |
668f62ec | 2436 | if (!visit_type_uint16(v, name, &value, errp)) { |
836e1b38 FF |
2437 | return; |
2438 | } | |
2439 | ||
2440 | *field = value; | |
2441 | } | |
2442 | ||
d7bce999 EB |
2443 | static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name, |
2444 | void *opaque, Error **errp) | |
e732ea63 MT |
2445 | { |
2446 | uint32_t value = *(uint32_t *)opaque; | |
51e72bc1 | 2447 | visit_type_uint32(v, name, &value, errp); |
e732ea63 MT |
2448 | } |
2449 | ||
836e1b38 FF |
2450 | static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name, |
2451 | void *opaque, Error **errp) | |
2452 | { | |
2453 | uint32_t *field = opaque; | |
2454 | uint32_t value; | |
836e1b38 | 2455 | |
668f62ec | 2456 | if (!visit_type_uint32(v, name, &value, errp)) { |
836e1b38 FF |
2457 | return; |
2458 | } | |
2459 | ||
2460 | *field = value; | |
2461 | } | |
2462 | ||
d7bce999 EB |
2463 | static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name, |
2464 | void *opaque, Error **errp) | |
e732ea63 MT |
2465 | { |
2466 | uint64_t value = *(uint64_t *)opaque; | |
51e72bc1 | 2467 | visit_type_uint64(v, name, &value, errp); |
e732ea63 MT |
2468 | } |
2469 | ||
836e1b38 FF |
2470 | static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name, |
2471 | void *opaque, Error **errp) | |
2472 | { | |
2473 | uint64_t *field = opaque; | |
2474 | uint64_t value; | |
836e1b38 | 2475 | |
668f62ec | 2476 | if (!visit_type_uint64(v, name, &value, errp)) { |
836e1b38 FF |
2477 | return; |
2478 | } | |
2479 | ||
2480 | *field = value; | |
2481 | } | |
2482 | ||
70251887 MA |
2483 | ObjectProperty * |
2484 | object_property_add_uint8_ptr(Object *obj, const char *name, | |
2485 | const uint8_t *v, | |
d2623129 | 2486 | ObjectPropertyFlags flags) |
e732ea63 | 2487 | { |
836e1b38 FF |
2488 | ObjectPropertyAccessor *getter = NULL; |
2489 | ObjectPropertyAccessor *setter = NULL; | |
2490 | ||
2491 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2492 | getter = property_get_uint8_ptr; | |
2493 | } | |
2494 | ||
2495 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2496 | setter = property_set_uint8_ptr; | |
2497 | } | |
2498 | ||
70251887 | 2499 | return object_property_add(obj, name, "uint8", |
d2623129 | 2500 | getter, setter, NULL, (void *)v); |
e732ea63 MT |
2501 | } |
2502 | ||
a3a16211 MAL |
2503 | ObjectProperty * |
2504 | object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name, | |
836e1b38 | 2505 | const uint8_t *v, |
d2623129 | 2506 | ObjectPropertyFlags flags) |
16bf7f52 | 2507 | { |
836e1b38 FF |
2508 | ObjectPropertyAccessor *getter = NULL; |
2509 | ObjectPropertyAccessor *setter = NULL; | |
2510 | ||
2511 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2512 | getter = property_get_uint8_ptr; | |
2513 | } | |
2514 | ||
2515 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2516 | setter = property_set_uint8_ptr; | |
2517 | } | |
2518 | ||
a3a16211 | 2519 | return object_class_property_add(klass, name, "uint8", |
d2623129 | 2520 | getter, setter, NULL, (void *)v); |
16bf7f52 DB |
2521 | } |
2522 | ||
70251887 MA |
2523 | ObjectProperty * |
2524 | object_property_add_uint16_ptr(Object *obj, const char *name, | |
2525 | const uint16_t *v, | |
d2623129 | 2526 | ObjectPropertyFlags flags) |
e732ea63 | 2527 | { |
836e1b38 FF |
2528 | ObjectPropertyAccessor *getter = NULL; |
2529 | ObjectPropertyAccessor *setter = NULL; | |
2530 | ||
2531 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2532 | getter = property_get_uint16_ptr; | |
2533 | } | |
2534 | ||
2535 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2536 | setter = property_set_uint16_ptr; | |
2537 | } | |
2538 | ||
70251887 | 2539 | return object_property_add(obj, name, "uint16", |
d2623129 | 2540 | getter, setter, NULL, (void *)v); |
e732ea63 MT |
2541 | } |
2542 | ||
a3a16211 MAL |
2543 | ObjectProperty * |
2544 | object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name, | |
836e1b38 | 2545 | const uint16_t *v, |
d2623129 | 2546 | ObjectPropertyFlags flags) |
16bf7f52 | 2547 | { |
836e1b38 FF |
2548 | ObjectPropertyAccessor *getter = NULL; |
2549 | ObjectPropertyAccessor *setter = NULL; | |
2550 | ||
2551 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2552 | getter = property_get_uint16_ptr; | |
2553 | } | |
2554 | ||
2555 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2556 | setter = property_set_uint16_ptr; | |
2557 | } | |
2558 | ||
a3a16211 | 2559 | return object_class_property_add(klass, name, "uint16", |
d2623129 | 2560 | getter, setter, NULL, (void *)v); |
16bf7f52 DB |
2561 | } |
2562 | ||
70251887 MA |
2563 | ObjectProperty * |
2564 | object_property_add_uint32_ptr(Object *obj, const char *name, | |
2565 | const uint32_t *v, | |
d2623129 | 2566 | ObjectPropertyFlags flags) |
e732ea63 | 2567 | { |
836e1b38 FF |
2568 | ObjectPropertyAccessor *getter = NULL; |
2569 | ObjectPropertyAccessor *setter = NULL; | |
2570 | ||
2571 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2572 | getter = property_get_uint32_ptr; | |
2573 | } | |
2574 | ||
2575 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2576 | setter = property_set_uint32_ptr; | |
2577 | } | |
2578 | ||
70251887 | 2579 | return object_property_add(obj, name, "uint32", |
d2623129 | 2580 | getter, setter, NULL, (void *)v); |
e732ea63 MT |
2581 | } |
2582 | ||
a3a16211 MAL |
2583 | ObjectProperty * |
2584 | object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name, | |
836e1b38 | 2585 | const uint32_t *v, |
d2623129 | 2586 | ObjectPropertyFlags flags) |
16bf7f52 | 2587 | { |
836e1b38 FF |
2588 | ObjectPropertyAccessor *getter = NULL; |
2589 | ObjectPropertyAccessor *setter = NULL; | |
2590 | ||
2591 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2592 | getter = property_get_uint32_ptr; | |
2593 | } | |
2594 | ||
2595 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2596 | setter = property_set_uint32_ptr; | |
2597 | } | |
2598 | ||
a3a16211 | 2599 | return object_class_property_add(klass, name, "uint32", |
d2623129 | 2600 | getter, setter, NULL, (void *)v); |
16bf7f52 DB |
2601 | } |
2602 | ||
70251887 MA |
2603 | ObjectProperty * |
2604 | object_property_add_uint64_ptr(Object *obj, const char *name, | |
2605 | const uint64_t *v, | |
d2623129 | 2606 | ObjectPropertyFlags flags) |
e732ea63 | 2607 | { |
836e1b38 FF |
2608 | ObjectPropertyAccessor *getter = NULL; |
2609 | ObjectPropertyAccessor *setter = NULL; | |
2610 | ||
2611 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2612 | getter = property_get_uint64_ptr; | |
2613 | } | |
2614 | ||
2615 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2616 | setter = property_set_uint64_ptr; | |
2617 | } | |
2618 | ||
70251887 | 2619 | return object_property_add(obj, name, "uint64", |
d2623129 | 2620 | getter, setter, NULL, (void *)v); |
e732ea63 MT |
2621 | } |
2622 | ||
a3a16211 MAL |
2623 | ObjectProperty * |
2624 | object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name, | |
836e1b38 | 2625 | const uint64_t *v, |
d2623129 | 2626 | ObjectPropertyFlags flags) |
16bf7f52 | 2627 | { |
836e1b38 FF |
2628 | ObjectPropertyAccessor *getter = NULL; |
2629 | ObjectPropertyAccessor *setter = NULL; | |
2630 | ||
2631 | if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) { | |
2632 | getter = property_get_uint64_ptr; | |
2633 | } | |
2634 | ||
2635 | if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) { | |
2636 | setter = property_set_uint64_ptr; | |
2637 | } | |
2638 | ||
a3a16211 | 2639 | return object_class_property_add(klass, name, "uint64", |
d2623129 | 2640 | getter, setter, NULL, (void *)v); |
16bf7f52 DB |
2641 | } |
2642 | ||
ef7c7ff6 SH |
2643 | typedef struct { |
2644 | Object *target_obj; | |
1590d266 | 2645 | char *target_name; |
ef7c7ff6 SH |
2646 | } AliasProperty; |
2647 | ||
d7bce999 EB |
2648 | static void property_get_alias(Object *obj, Visitor *v, const char *name, |
2649 | void *opaque, Error **errp) | |
ef7c7ff6 SH |
2650 | { |
2651 | AliasProperty *prop = opaque; | |
2652 | ||
5325cc34 | 2653 | object_property_get(prop->target_obj, prop->target_name, v, errp); |
ef7c7ff6 SH |
2654 | } |
2655 | ||
d7bce999 EB |
2656 | static void property_set_alias(Object *obj, Visitor *v, const char *name, |
2657 | void *opaque, Error **errp) | |
ef7c7ff6 SH |
2658 | { |
2659 | AliasProperty *prop = opaque; | |
2660 | ||
5325cc34 | 2661 | object_property_set(prop->target_obj, prop->target_name, v, errp); |
ef7c7ff6 SH |
2662 | } |
2663 | ||
64607d08 | 2664 | static Object *property_resolve_alias(Object *obj, void *opaque, |
ddfb0baa | 2665 | const char *part) |
64607d08 PB |
2666 | { |
2667 | AliasProperty *prop = opaque; | |
2668 | ||
2669 | return object_resolve_path_component(prop->target_obj, prop->target_name); | |
2670 | } | |
2671 | ||
ef7c7ff6 SH |
2672 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
2673 | { | |
2674 | AliasProperty *prop = opaque; | |
2675 | ||
1590d266 | 2676 | g_free(prop->target_name); |
ef7c7ff6 SH |
2677 | g_free(prop); |
2678 | } | |
2679 | ||
70251887 MA |
2680 | ObjectProperty * |
2681 | object_property_add_alias(Object *obj, const char *name, | |
d2623129 | 2682 | Object *target_obj, const char *target_name) |
ef7c7ff6 SH |
2683 | { |
2684 | AliasProperty *prop; | |
64607d08 | 2685 | ObjectProperty *op; |
ef7c7ff6 | 2686 | ObjectProperty *target_prop; |
70251887 | 2687 | g_autofree char *prop_type = NULL; |
ef7c7ff6 | 2688 | |
d2623129 MA |
2689 | target_prop = object_property_find(target_obj, target_name, |
2690 | &error_abort); | |
ef7c7ff6 | 2691 | |
d190698e PB |
2692 | if (object_property_is_child(target_prop)) { |
2693 | prop_type = g_strdup_printf("link%s", | |
2694 | target_prop->type + strlen("child")); | |
2695 | } else { | |
2696 | prop_type = g_strdup(target_prop->type); | |
2697 | } | |
2698 | ||
ef7c7ff6 SH |
2699 | prop = g_malloc(sizeof(*prop)); |
2700 | prop->target_obj = target_obj; | |
1590d266 | 2701 | prop->target_name = g_strdup(target_name); |
ef7c7ff6 | 2702 | |
d190698e | 2703 | op = object_property_add(obj, name, prop_type, |
64607d08 PB |
2704 | property_get_alias, |
2705 | property_set_alias, | |
2706 | property_release_alias, | |
d2623129 | 2707 | prop); |
64607d08 | 2708 | op->resolve = property_resolve_alias; |
0e76ed0a MAL |
2709 | if (target_prop->defval) { |
2710 | op->defval = qobject_ref(target_prop->defval); | |
2711 | } | |
d190698e | 2712 | |
a18bb417 | 2713 | object_property_set_description(obj, op->name, |
7eecec7d | 2714 | target_prop->description); |
70251887 | 2715 | return op; |
ef7c7ff6 SH |
2716 | } |
2717 | ||
80742642 | 2718 | void object_property_set_description(Object *obj, const char *name, |
7eecec7d | 2719 | const char *description) |
80742642 GA |
2720 | { |
2721 | ObjectProperty *op; | |
2722 | ||
7eecec7d | 2723 | op = object_property_find(obj, name, &error_abort); |
80742642 GA |
2724 | g_free(op->description); |
2725 | op->description = g_strdup(description); | |
2726 | } | |
2727 | ||
16bf7f52 DB |
2728 | void object_class_property_set_description(ObjectClass *klass, |
2729 | const char *name, | |
7eecec7d | 2730 | const char *description) |
16bf7f52 DB |
2731 | { |
2732 | ObjectProperty *op; | |
2733 | ||
2734 | op = g_hash_table_lookup(klass->properties, name); | |
16bf7f52 DB |
2735 | g_free(op->description); |
2736 | op->description = g_strdup(description); | |
2737 | } | |
2738 | ||
7439a036 | 2739 | static void object_class_init(ObjectClass *klass, void *data) |
2f262e06 | 2740 | { |
90c69fb9 | 2741 | object_class_property_add_str(klass, "type", object_get_type, |
d2623129 | 2742 | NULL); |
2f262e06 PB |
2743 | } |
2744 | ||
745549c8 PB |
2745 | static void register_types(void) |
2746 | { | |
2747 | static TypeInfo interface_info = { | |
2748 | .name = TYPE_INTERFACE, | |
33e95c63 | 2749 | .class_size = sizeof(InterfaceClass), |
745549c8 PB |
2750 | .abstract = true, |
2751 | }; | |
2752 | ||
2753 | static TypeInfo object_info = { | |
2754 | .name = TYPE_OBJECT, | |
2755 | .instance_size = sizeof(Object), | |
7439a036 | 2756 | .class_init = object_class_init, |
745549c8 PB |
2757 | .abstract = true, |
2758 | }; | |
2759 | ||
049cb3cf PB |
2760 | type_interface = type_register_internal(&interface_info); |
2761 | type_register_internal(&object_info); | |
745549c8 PB |
2762 | } |
2763 | ||
2764 | type_init(register_types) |