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