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