]> git.proxmox.com Git - qemu.git/blame - qom/object.c
qom: preserve object while unparenting it
[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
14cccb61 13#include "qom/object.h"
2f28d2ff 14#include "qemu-common.h"
7b1b5d19 15#include "qapi/visitor.h"
b2cd7dee
PB
16#include "qapi/string-input-visitor.h"
17#include "qapi/string-output-visitor.h"
7b1b5d19 18#include "qapi/qmp/qerror.h"
2f28d2ff 19
7b7b7d18
PB
20/* TODO: replace QObject with a simpler visitor to avoid a dependency
21 * of the QOM core on QObject? */
14cccb61 22#include "qom/qom-qobject.h"
7b1b5d19
PB
23#include "qapi/qmp/qobject.h"
24#include "qapi/qmp/qbool.h"
25#include "qapi/qmp/qint.h"
26#include "qapi/qmp/qstring.h"
7b7b7d18 27
2f28d2ff
AL
28#define MAX_INTERFACES 32
29
30typedef struct InterfaceImpl InterfaceImpl;
31typedef struct TypeImpl TypeImpl;
32
33struct InterfaceImpl
34{
33e95c63 35 const char *typename;
2f28d2ff
AL
36};
37
38struct TypeImpl
39{
40 const char *name;
41
42 size_t class_size;
43
44 size_t instance_size;
45
46 void (*class_init)(ObjectClass *klass, void *data);
3b50e311 47 void (*class_base_init)(ObjectClass *klass, void *data);
2f28d2ff
AL
48 void (*class_finalize)(ObjectClass *klass, void *data);
49
50 void *class_data;
51
52 void (*instance_init)(Object *obj);
53 void (*instance_finalize)(Object *obj);
54
55 bool abstract;
56
57 const char *parent;
58 TypeImpl *parent_type;
59
60 ObjectClass *class;
61
62 int num_interfaces;
63 InterfaceImpl interfaces[MAX_INTERFACES];
64};
65
9970bd88
PB
66static Type type_interface;
67
2f28d2ff
AL
68static GHashTable *type_table_get(void)
69{
70 static GHashTable *type_table;
71
72 if (type_table == NULL) {
73 type_table = g_hash_table_new(g_str_hash, g_str_equal);
74 }
75
76 return type_table;
77}
78
79static void type_table_add(TypeImpl *ti)
80{
81 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
82}
83
84static TypeImpl *type_table_lookup(const char *name)
85{
86 return g_hash_table_lookup(type_table_get(), name);
87}
88
049cb3cf 89static TypeImpl *type_register_internal(const TypeInfo *info)
2f28d2ff
AL
90{
91 TypeImpl *ti = g_malloc0(sizeof(*ti));
33e95c63 92 int i;
2f28d2ff
AL
93
94 g_assert(info->name != NULL);
95
73093354
AL
96 if (type_table_lookup(info->name) != NULL) {
97 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
98 abort();
99 }
100
2f28d2ff
AL
101 ti->name = g_strdup(info->name);
102 ti->parent = g_strdup(info->parent);
103
104 ti->class_size = info->class_size;
105 ti->instance_size = info->instance_size;
106
107 ti->class_init = info->class_init;
3b50e311 108 ti->class_base_init = info->class_base_init;
2f28d2ff
AL
109 ti->class_finalize = info->class_finalize;
110 ti->class_data = info->class_data;
111
112 ti->instance_init = info->instance_init;
113 ti->instance_finalize = info->instance_finalize;
114
115 ti->abstract = info->abstract;
116
33e95c63
AL
117 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
118 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
2f28d2ff 119 }
33e95c63 120 ti->num_interfaces = i;
2f28d2ff
AL
121
122 type_table_add(ti);
123
124 return ti;
125}
126
049cb3cf
PB
127TypeImpl *type_register(const TypeInfo *info)
128{
129 assert(info->parent);
130 return type_register_internal(info);
131}
132
2f28d2ff
AL
133TypeImpl *type_register_static(const TypeInfo *info)
134{
135 return type_register(info);
136}
137
138static TypeImpl *type_get_by_name(const char *name)
139{
140 if (name == NULL) {
141 return NULL;
142 }
143
144 return type_table_lookup(name);
145}
146
147static TypeImpl *type_get_parent(TypeImpl *type)
148{
149 if (!type->parent_type && type->parent) {
150 type->parent_type = type_get_by_name(type->parent);
151 g_assert(type->parent_type != NULL);
152 }
153
154 return type->parent_type;
155}
156
157static bool type_has_parent(TypeImpl *type)
158{
159 return (type->parent != NULL);
160}
161
162static size_t type_class_get_size(TypeImpl *ti)
163{
164 if (ti->class_size) {
165 return ti->class_size;
166 }
167
168 if (type_has_parent(ti)) {
169 return type_class_get_size(type_get_parent(ti));
170 }
171
172 return sizeof(ObjectClass);
173}
174
aca59af6
IM
175static size_t type_object_get_size(TypeImpl *ti)
176{
177 if (ti->instance_size) {
178 return ti->instance_size;
179 }
180
181 if (type_has_parent(ti)) {
182 return type_object_get_size(type_get_parent(ti));
183 }
184
185 return 0;
186}
187
33e95c63 188static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
2f28d2ff 189{
33e95c63
AL
190 assert(target_type);
191
192 /* Check if typename is a direct ancestor of type */
193 while (type) {
194 if (type == target_type) {
195 return true;
196 }
2f28d2ff 197
33e95c63
AL
198 type = type_get_parent(type);
199 }
200
201 return false;
202}
203
204static void type_initialize(TypeImpl *ti);
205
206static void type_initialize_interface(TypeImpl *ti, const char *parent)
207{
208 InterfaceClass *new_iface;
209 TypeInfo info = { };
210 TypeImpl *iface_impl;
211
212 info.parent = parent;
213 info.name = g_strdup_printf("%s::%s", ti->name, info.parent);
214 info.abstract = true;
215
216 iface_impl = type_register(&info);
217 type_initialize(iface_impl);
218 g_free((char *)info.name);
219
220 new_iface = (InterfaceClass *)iface_impl->class;
221 new_iface->concrete_class = ti->class;
222
223 ti->class->interfaces = g_slist_append(ti->class->interfaces,
224 iface_impl->class);
2f28d2ff
AL
225}
226
ac451033 227static void type_initialize(TypeImpl *ti)
2f28d2ff 228{
745549c8 229 TypeImpl *parent;
2f28d2ff
AL
230
231 if (ti->class) {
232 return;
233 }
234
235 ti->class_size = type_class_get_size(ti);
aca59af6 236 ti->instance_size = type_object_get_size(ti);
2f28d2ff
AL
237
238 ti->class = g_malloc0(ti->class_size);
2f28d2ff 239
745549c8
PB
240 parent = type_get_parent(ti);
241 if (parent) {
ac451033 242 type_initialize(parent);
33e95c63
AL
243 GSList *e;
244 int i;
2f28d2ff 245
2f28d2ff 246 g_assert(parent->class_size <= ti->class_size);
745549c8 247 memcpy(ti->class, parent->class, parent->class_size);
33e95c63
AL
248
249 for (e = parent->class->interfaces; e; e = e->next) {
250 ObjectClass *iface = e->data;
251 type_initialize_interface(ti, object_class_get_name(iface));
252 }
253
254 for (i = 0; i < ti->num_interfaces; i++) {
255 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
256 for (e = ti->class->interfaces; e; e = e->next) {
257 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
258
259 if (type_is_ancestor(target_type, t)) {
260 break;
261 }
262 }
263
264 if (e) {
265 continue;
266 }
267
268 type_initialize_interface(ti, ti->interfaces[i].typename);
269 }
745549c8 270 }
2f28d2ff 271
745549c8 272 ti->class->type = ti;
3b50e311 273
745549c8
PB
274 while (parent) {
275 if (parent->class_base_init) {
276 parent->class_base_init(ti->class, ti->class_data);
3b50e311 277 }
745549c8 278 parent = type_get_parent(parent);
2f28d2ff
AL
279 }
280
2f28d2ff
AL
281 if (ti->class_init) {
282 ti->class_init(ti->class, ti->class_data);
283 }
2f28d2ff 284
2f28d2ff 285
2f28d2ff
AL
286}
287
288static void object_init_with_type(Object *obj, TypeImpl *ti)
289{
2f28d2ff
AL
290 if (type_has_parent(ti)) {
291 object_init_with_type(obj, type_get_parent(ti));
292 }
293
2f28d2ff
AL
294 if (ti->instance_init) {
295 ti->instance_init(obj);
296 }
297}
298
299void object_initialize_with_type(void *data, TypeImpl *type)
300{
301 Object *obj = data;
302
303 g_assert(type != NULL);
ac451033 304 type_initialize(type);
aca59af6
IM
305
306 g_assert(type->instance_size >= sizeof(Object));
2f28d2ff
AL
307 g_assert(type->abstract == false);
308
309 memset(obj, 0, type->instance_size);
310 obj->class = type->class;
764b6312 311 object_ref(obj);
57c9fafe 312 QTAILQ_INIT(&obj->properties);
2f28d2ff
AL
313 object_init_with_type(obj, type);
314}
315
316void object_initialize(void *data, const char *typename)
317{
318 TypeImpl *type = type_get_by_name(typename);
319
320 object_initialize_with_type(data, type);
321}
322
5d9d3f47
AF
323static inline bool object_property_is_child(ObjectProperty *prop)
324{
325 return strstart(prop->type, "child<", NULL);
326}
327
328static inline bool object_property_is_link(ObjectProperty *prop)
329{
330 return strstart(prop->type, "link<", NULL);
331}
332
57c9fafe
AL
333static void object_property_del_all(Object *obj)
334{
335 while (!QTAILQ_EMPTY(&obj->properties)) {
336 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
337
338 QTAILQ_REMOVE(&obj->properties, prop, node);
339
340 if (prop->release) {
341 prop->release(obj, prop->name, prop->opaque);
342 }
343
344 g_free(prop->name);
345 g_free(prop->type);
346 g_free(prop);
347 }
348}
349
350static void object_property_del_child(Object *obj, Object *child, Error **errp)
351{
352 ObjectProperty *prop;
353
354 QTAILQ_FOREACH(prop, &obj->properties, node) {
5d9d3f47 355 if (object_property_is_child(prop) && prop->opaque == child) {
57c9fafe 356 object_property_del(obj, prop->name, errp);
6c1fdcf9 357 break;
57c9fafe
AL
358 }
359 }
360}
361
362void object_unparent(Object *obj)
363{
52e636cd 364 object_ref(obj);
57c9fafe
AL
365 if (obj->parent) {
366 object_property_del_child(obj->parent, obj, NULL);
367 }
667d22d1
PB
368 if (obj->class->unparent) {
369 (obj->class->unparent)(obj);
370 }
52e636cd 371 object_unref(obj);
57c9fafe
AL
372}
373
2f28d2ff
AL
374static void object_deinit(Object *obj, TypeImpl *type)
375{
376 if (type->instance_finalize) {
377 type->instance_finalize(obj);
378 }
379
2f28d2ff
AL
380 if (type_has_parent(type)) {
381 object_deinit(obj, type_get_parent(type));
382 }
383}
384
339c2708 385static void object_finalize(void *data)
2f28d2ff
AL
386{
387 Object *obj = data;
388 TypeImpl *ti = obj->class->type;
389
390 object_deinit(obj, ti);
57c9fafe 391 object_property_del_all(obj);
db85b575
AL
392
393 g_assert(obj->ref == 0);
fde9bf44
PB
394 if (obj->free) {
395 obj->free(obj);
396 }
2f28d2ff
AL
397}
398
399Object *object_new_with_type(Type type)
400{
401 Object *obj;
402
403 g_assert(type != NULL);
ac451033 404 type_initialize(type);
2f28d2ff
AL
405
406 obj = g_malloc(type->instance_size);
407 object_initialize_with_type(obj, type);
fde9bf44 408 obj->free = g_free;
2f28d2ff
AL
409
410 return obj;
411}
412
413Object *object_new(const char *typename)
414{
415 TypeImpl *ti = type_get_by_name(typename);
416
417 return object_new_with_type(ti);
418}
419
420void object_delete(Object *obj)
421{
da5a44e8
PB
422 object_unparent(obj);
423 g_assert(obj->ref == 1);
db85b575 424 object_unref(obj);
2f28d2ff
AL
425}
426
2f28d2ff
AL
427Object *object_dynamic_cast(Object *obj, const char *typename)
428{
b7f43fe4 429 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
acc4af3f
PB
430 return obj;
431 }
432
2f28d2ff
AL
433 return NULL;
434}
435
2f28d2ff
AL
436Object *object_dynamic_cast_assert(Object *obj, const char *typename)
437{
438 Object *inst;
439
440 inst = object_dynamic_cast(obj, typename);
441
b7f43fe4 442 if (!inst && obj) {
2f28d2ff
AL
443 fprintf(stderr, "Object %p is not an instance of type %s\n",
444 obj, typename);
445 abort();
446 }
447
448 return inst;
449}
450
451ObjectClass *object_class_dynamic_cast(ObjectClass *class,
452 const char *typename)
453{
454 TypeImpl *target_type = type_get_by_name(typename);
455 TypeImpl *type = class->type;
33e95c63 456 ObjectClass *ret = NULL;
2f28d2ff 457
33e95c63
AL
458 if (type->num_interfaces && type_is_ancestor(target_type, type_interface)) {
459 int found = 0;
460 GSList *i;
2f28d2ff 461
33e95c63
AL
462 for (i = class->interfaces; i; i = i->next) {
463 ObjectClass *target_class = i->data;
464
465 if (type_is_ancestor(target_class->type, target_type)) {
466 ret = target_class;
467 found++;
468 }
469 }
470
471 /* The match was ambiguous, don't allow a cast */
472 if (found > 1) {
473 ret = NULL;
474 }
475 } else if (type_is_ancestor(type, target_type)) {
476 ret = class;
2f28d2ff
AL
477 }
478
33e95c63 479 return ret;
2f28d2ff
AL
480}
481
482ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
483 const char *typename)
484{
485 ObjectClass *ret = object_class_dynamic_cast(class, typename);
486
487 if (!ret) {
488 fprintf(stderr, "Object %p is not an instance of type %s\n",
489 class, typename);
490 abort();
491 }
492
493 return ret;
494}
495
496const char *object_get_typename(Object *obj)
497{
498 return obj->class->type->name;
499}
500
501ObjectClass *object_get_class(Object *obj)
502{
503 return obj->class;
504}
505
17862378
AF
506bool object_class_is_abstract(ObjectClass *klass)
507{
508 return klass->type->abstract;
509}
510
2f28d2ff
AL
511const char *object_class_get_name(ObjectClass *klass)
512{
513 return klass->type->name;
514}
515
516ObjectClass *object_class_by_name(const char *typename)
517{
518 TypeImpl *type = type_get_by_name(typename);
519
520 if (!type) {
521 return NULL;
522 }
523
ac451033 524 type_initialize(type);
2f28d2ff
AL
525
526 return type->class;
527}
528
e7cce67f
PB
529ObjectClass *object_class_get_parent(ObjectClass *class)
530{
531 TypeImpl *type = type_get_parent(class->type);
532
533 if (!type) {
534 return NULL;
535 }
536
537 type_initialize(type);
538
539 return type->class;
540}
541
2f28d2ff
AL
542typedef struct OCFData
543{
544 void (*fn)(ObjectClass *klass, void *opaque);
93c511a1
AL
545 const char *implements_type;
546 bool include_abstract;
2f28d2ff
AL
547 void *opaque;
548} OCFData;
549
550static void object_class_foreach_tramp(gpointer key, gpointer value,
551 gpointer opaque)
552{
553 OCFData *data = opaque;
554 TypeImpl *type = value;
93c511a1 555 ObjectClass *k;
2f28d2ff 556
ac451033 557 type_initialize(type);
93c511a1 558 k = type->class;
2f28d2ff 559
93c511a1
AL
560 if (!data->include_abstract && type->abstract) {
561 return;
562 }
563
564 if (data->implements_type &&
565 !object_class_dynamic_cast(k, data->implements_type)) {
566 return;
567 }
568
569 data->fn(k, data->opaque);
2f28d2ff
AL
570}
571
572void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 573 const char *implements_type, bool include_abstract,
2f28d2ff
AL
574 void *opaque)
575{
93c511a1 576 OCFData data = { fn, implements_type, include_abstract, opaque };
2f28d2ff
AL
577
578 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
579}
57c9fafe 580
32efc535
PB
581int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
582 void *opaque)
583{
584 ObjectProperty *prop;
585 int ret = 0;
586
587 QTAILQ_FOREACH(prop, &obj->properties, node) {
588 if (object_property_is_child(prop)) {
589 ret = fn(prop->opaque, opaque);
590 if (ret != 0) {
591 break;
592 }
593 }
594 }
595 return ret;
596}
597
418ba9e5
AF
598static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
599{
600 GSList **list = opaque;
601
602 *list = g_slist_prepend(*list, klass);
603}
604
605GSList *object_class_get_list(const char *implements_type,
606 bool include_abstract)
607{
608 GSList *list = NULL;
609
610 object_class_foreach(object_class_get_list_tramp,
611 implements_type, include_abstract, &list);
612 return list;
613}
614
57c9fafe
AL
615void object_ref(Object *obj)
616{
617 obj->ref++;
618}
619
620void object_unref(Object *obj)
621{
622 g_assert(obj->ref > 0);
623 obj->ref--;
624
625 /* parent always holds a reference to its children */
626 if (obj->ref == 0) {
627 object_finalize(obj);
628 }
629}
630
631void object_property_add(Object *obj, const char *name, const char *type,
632 ObjectPropertyAccessor *get,
633 ObjectPropertyAccessor *set,
634 ObjectPropertyRelease *release,
635 void *opaque, Error **errp)
636{
637 ObjectProperty *prop = g_malloc0(sizeof(*prop));
638
639 prop->name = g_strdup(name);
640 prop->type = g_strdup(type);
641
642 prop->get = get;
643 prop->set = set;
644 prop->release = release;
645 prop->opaque = opaque;
646
647 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
648}
649
89bfe000
PB
650ObjectProperty *object_property_find(Object *obj, const char *name,
651 Error **errp)
57c9fafe
AL
652{
653 ObjectProperty *prop;
654
655 QTAILQ_FOREACH(prop, &obj->properties, node) {
656 if (strcmp(prop->name, name) == 0) {
657 return prop;
658 }
659 }
660
89bfe000 661 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
57c9fafe
AL
662 return NULL;
663}
664
665void object_property_del(Object *obj, const char *name, Error **errp)
666{
89bfe000 667 ObjectProperty *prop = object_property_find(obj, name, errp);
0866aca1 668 if (prop == NULL) {
0866aca1
AL
669 return;
670 }
57c9fafe 671
0866aca1
AL
672 if (prop->release) {
673 prop->release(obj, name, prop->opaque);
674 }
675
676 QTAILQ_REMOVE(&obj->properties, prop, node);
57c9fafe
AL
677
678 g_free(prop->name);
679 g_free(prop->type);
680 g_free(prop);
681}
682
683void object_property_get(Object *obj, Visitor *v, const char *name,
684 Error **errp)
685{
89bfe000 686 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 687 if (prop == NULL) {
57c9fafe
AL
688 return;
689 }
690
691 if (!prop->get) {
692 error_set(errp, QERR_PERMISSION_DENIED);
693 } else {
694 prop->get(obj, v, prop->opaque, name, errp);
695 }
696}
697
698void object_property_set(Object *obj, Visitor *v, const char *name,
699 Error **errp)
700{
89bfe000 701 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 702 if (prop == NULL) {
57c9fafe
AL
703 return;
704 }
705
706 if (!prop->set) {
707 error_set(errp, QERR_PERMISSION_DENIED);
708 } else {
709 prop->set(obj, v, prop->opaque, name, errp);
710 }
711}
712
7b7b7d18
PB
713void object_property_set_str(Object *obj, const char *value,
714 const char *name, Error **errp)
715{
716 QString *qstr = qstring_from_str(value);
717 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
718
719 QDECREF(qstr);
720}
721
722char *object_property_get_str(Object *obj, const char *name,
723 Error **errp)
724{
725 QObject *ret = object_property_get_qobject(obj, name, errp);
726 QString *qstring;
727 char *retval;
728
729 if (!ret) {
730 return NULL;
731 }
732 qstring = qobject_to_qstring(ret);
733 if (!qstring) {
734 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
735 retval = NULL;
736 } else {
737 retval = g_strdup(qstring_get_str(qstring));
738 }
739
740 QDECREF(qstring);
741 return retval;
742}
743
1d9c5a12
PB
744void object_property_set_link(Object *obj, Object *value,
745 const char *name, Error **errp)
746{
747 object_property_set_str(obj, object_get_canonical_path(value),
748 name, errp);
749}
750
751Object *object_property_get_link(Object *obj, const char *name,
752 Error **errp)
753{
754 char *str = object_property_get_str(obj, name, errp);
755 Object *target = NULL;
756
757 if (str && *str) {
758 target = object_resolve_path(str, NULL);
759 if (!target) {
760 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
761 }
762 }
763
764 g_free(str);
765 return target;
766}
767
7b7b7d18
PB
768void object_property_set_bool(Object *obj, bool value,
769 const char *name, Error **errp)
770{
771 QBool *qbool = qbool_from_int(value);
772 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
773
774 QDECREF(qbool);
775}
776
777bool object_property_get_bool(Object *obj, const char *name,
778 Error **errp)
779{
780 QObject *ret = object_property_get_qobject(obj, name, errp);
781 QBool *qbool;
782 bool retval;
783
784 if (!ret) {
785 return false;
786 }
787 qbool = qobject_to_qbool(ret);
788 if (!qbool) {
789 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
790 retval = false;
791 } else {
792 retval = qbool_get_int(qbool);
793 }
794
795 QDECREF(qbool);
796 return retval;
797}
798
799void object_property_set_int(Object *obj, int64_t value,
800 const char *name, Error **errp)
801{
802 QInt *qint = qint_from_int(value);
803 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
804
805 QDECREF(qint);
806}
807
808int64_t object_property_get_int(Object *obj, const char *name,
809 Error **errp)
810{
811 QObject *ret = object_property_get_qobject(obj, name, errp);
812 QInt *qint;
813 int64_t retval;
814
815 if (!ret) {
816 return -1;
817 }
818 qint = qobject_to_qint(ret);
819 if (!qint) {
820 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
821 retval = -1;
822 } else {
823 retval = qint_get_int(qint);
824 }
825
826 QDECREF(qint);
827 return retval;
b2cd7dee
PB
828}
829
830void object_property_parse(Object *obj, const char *string,
831 const char *name, Error **errp)
832{
833 StringInputVisitor *mi;
834 mi = string_input_visitor_new(string);
835 object_property_set(obj, string_input_get_visitor(mi), name, errp);
836
837 string_input_visitor_cleanup(mi);
838}
839
840char *object_property_print(Object *obj, const char *name,
841 Error **errp)
842{
843 StringOutputVisitor *mo;
844 char *string;
845
846 mo = string_output_visitor_new();
8185bfc1 847 object_property_get(obj, string_output_get_visitor(mo), name, errp);
b2cd7dee
PB
848 string = string_output_get_string(mo);
849 string_output_visitor_cleanup(mo);
850 return string;
7b7b7d18
PB
851}
852
57c9fafe
AL
853const char *object_property_get_type(Object *obj, const char *name, Error **errp)
854{
89bfe000 855 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 856 if (prop == NULL) {
57c9fafe
AL
857 return NULL;
858 }
859
860 return prop->type;
861}
862
863Object *object_get_root(void)
864{
8b45d447 865 static Object *root;
57c9fafe 866
8b45d447
AL
867 if (!root) {
868 root = object_new("container");
57c9fafe
AL
869 }
870
8b45d447 871 return root;
57c9fafe
AL
872}
873
874static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
875 const char *name, Error **errp)
876{
877 Object *child = opaque;
878 gchar *path;
879
880 path = object_get_canonical_path(child);
881 visit_type_str(v, &path, name, errp);
882 g_free(path);
883}
884
db85b575
AL
885static void object_finalize_child_property(Object *obj, const char *name,
886 void *opaque)
887{
888 Object *child = opaque;
889
890 object_unref(child);
891}
892
57c9fafe
AL
893void object_property_add_child(Object *obj, const char *name,
894 Object *child, Error **errp)
895{
896 gchar *type;
897
898 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
899
900 object_property_add(obj, name, type, object_get_child_property,
db85b575 901 NULL, object_finalize_child_property, child, errp);
57c9fafe
AL
902
903 object_ref(child);
904 g_assert(child->parent == NULL);
905 child->parent = obj;
906
907 g_free(type);
908}
909
910static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
911 const char *name, Error **errp)
912{
913 Object **child = opaque;
914 gchar *path;
915
916 if (*child) {
917 path = object_get_canonical_path(*child);
918 visit_type_str(v, &path, name, errp);
919 g_free(path);
920 } else {
921 path = (gchar *)"";
922 visit_type_str(v, &path, name, errp);
923 }
924}
925
926static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
927 const char *name, Error **errp)
928{
929 Object **child = opaque;
f0cdc966 930 Object *old_target;
57c9fafe
AL
931 bool ambiguous = false;
932 const char *type;
933 char *path;
11e35bfd 934 gchar *target_type;
57c9fafe
AL
935
936 type = object_property_get_type(obj, name, NULL);
937
938 visit_type_str(v, &path, name, errp);
939
f0cdc966
AB
940 old_target = *child;
941 *child = NULL;
57c9fafe
AL
942
943 if (strcmp(path, "") != 0) {
944 Object *target;
945
11e35bfd
PB
946 /* Go from link<FOO> to FOO. */
947 target_type = g_strndup(&type[5], strlen(type) - 6);
948 target = object_resolve_path_type(path, target_type, &ambiguous);
57c9fafe 949
11e35bfd
PB
950 if (ambiguous) {
951 error_set(errp, QERR_AMBIGUOUS_PATH, path);
952 } else if (target) {
953 object_ref(target);
954 *child = target;
57c9fafe 955 } else {
11e35bfd
PB
956 target = object_resolve_path(path, &ambiguous);
957 if (target || ambiguous) {
958 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
959 } else {
960 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
961 }
57c9fafe 962 }
11e35bfd 963 g_free(target_type);
57c9fafe
AL
964 }
965
966 g_free(path);
f0cdc966
AB
967
968 if (old_target != NULL) {
969 object_unref(old_target);
970 }
57c9fafe
AL
971}
972
973void object_property_add_link(Object *obj, const char *name,
974 const char *type, Object **child,
975 Error **errp)
976{
977 gchar *full_type;
978
979 full_type = g_strdup_printf("link<%s>", type);
980
981 object_property_add(obj, name, full_type,
982 object_get_link_property,
983 object_set_link_property,
984 NULL, child, errp);
985
986 g_free(full_type);
987}
988
989gchar *object_get_canonical_path(Object *obj)
990{
991 Object *root = object_get_root();
992 char *newpath = NULL, *path = NULL;
993
994 while (obj != root) {
995 ObjectProperty *prop = NULL;
996
997 g_assert(obj->parent != NULL);
998
999 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
5d9d3f47 1000 if (!object_property_is_child(prop)) {
57c9fafe
AL
1001 continue;
1002 }
1003
1004 if (prop->opaque == obj) {
1005 if (path) {
1006 newpath = g_strdup_printf("%s/%s", prop->name, path);
1007 g_free(path);
1008 path = newpath;
1009 } else {
1010 path = g_strdup(prop->name);
1011 }
1012 break;
1013 }
1014 }
1015
1016 g_assert(prop != NULL);
1017
1018 obj = obj->parent;
1019 }
1020
1021 newpath = g_strdup_printf("/%s", path);
1022 g_free(path);
1023
1024 return newpath;
1025}
1026
3e84b483 1027Object *object_resolve_path_component(Object *parent, const gchar *part)
a612b2a6 1028{
89bfe000 1029 ObjectProperty *prop = object_property_find(parent, part, NULL);
a612b2a6
PB
1030 if (prop == NULL) {
1031 return NULL;
1032 }
1033
5d9d3f47 1034 if (object_property_is_link(prop)) {
a612b2a6 1035 return *(Object **)prop->opaque;
5d9d3f47 1036 } else if (object_property_is_child(prop)) {
a612b2a6
PB
1037 return prop->opaque;
1038 } else {
1039 return NULL;
1040 }
1041}
1042
57c9fafe
AL
1043static Object *object_resolve_abs_path(Object *parent,
1044 gchar **parts,
02fe2db6 1045 const char *typename,
57c9fafe
AL
1046 int index)
1047{
57c9fafe
AL
1048 Object *child;
1049
1050 if (parts[index] == NULL) {
02fe2db6 1051 return object_dynamic_cast(parent, typename);
57c9fafe
AL
1052 }
1053
1054 if (strcmp(parts[index], "") == 0) {
02fe2db6 1055 return object_resolve_abs_path(parent, parts, typename, index + 1);
57c9fafe
AL
1056 }
1057
a612b2a6 1058 child = object_resolve_path_component(parent, parts[index]);
57c9fafe
AL
1059 if (!child) {
1060 return NULL;
1061 }
1062
02fe2db6 1063 return object_resolve_abs_path(child, parts, typename, index + 1);
57c9fafe
AL
1064}
1065
1066static Object *object_resolve_partial_path(Object *parent,
1067 gchar **parts,
02fe2db6 1068 const char *typename,
57c9fafe
AL
1069 bool *ambiguous)
1070{
1071 Object *obj;
1072 ObjectProperty *prop;
1073
02fe2db6 1074 obj = object_resolve_abs_path(parent, parts, typename, 0);
57c9fafe
AL
1075
1076 QTAILQ_FOREACH(prop, &parent->properties, node) {
1077 Object *found;
1078
5d9d3f47 1079 if (!object_property_is_child(prop)) {
57c9fafe
AL
1080 continue;
1081 }
1082
02fe2db6
PB
1083 found = object_resolve_partial_path(prop->opaque, parts,
1084 typename, ambiguous);
57c9fafe
AL
1085 if (found) {
1086 if (obj) {
1087 if (ambiguous) {
1088 *ambiguous = true;
1089 }
1090 return NULL;
1091 }
1092 obj = found;
1093 }
1094
1095 if (ambiguous && *ambiguous) {
1096 return NULL;
1097 }
1098 }
1099
1100 return obj;
1101}
1102
02fe2db6
PB
1103Object *object_resolve_path_type(const char *path, const char *typename,
1104 bool *ambiguous)
57c9fafe
AL
1105{
1106 bool partial_path = true;
1107 Object *obj;
1108 gchar **parts;
1109
1110 parts = g_strsplit(path, "/", 0);
1111 if (parts == NULL || parts[0] == NULL) {
1112 g_strfreev(parts);
1113 return object_get_root();
1114 }
1115
1116 if (strcmp(parts[0], "") == 0) {
1117 partial_path = false;
1118 }
1119
1120 if (partial_path) {
1121 if (ambiguous) {
1122 *ambiguous = false;
1123 }
02fe2db6
PB
1124 obj = object_resolve_partial_path(object_get_root(), parts,
1125 typename, ambiguous);
57c9fafe 1126 } else {
02fe2db6 1127 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
57c9fafe
AL
1128 }
1129
1130 g_strfreev(parts);
1131
1132 return obj;
1133}
1134
02fe2db6
PB
1135Object *object_resolve_path(const char *path, bool *ambiguous)
1136{
1137 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1138}
1139
57c9fafe
AL
1140typedef struct StringProperty
1141{
1142 char *(*get)(Object *, Error **);
1143 void (*set)(Object *, const char *, Error **);
1144} StringProperty;
1145
7b7b7d18
PB
1146static void property_get_str(Object *obj, Visitor *v, void *opaque,
1147 const char *name, Error **errp)
57c9fafe
AL
1148{
1149 StringProperty *prop = opaque;
1150 char *value;
1151
1152 value = prop->get(obj, errp);
1153 if (value) {
1154 visit_type_str(v, &value, name, errp);
1155 g_free(value);
1156 }
1157}
1158
7b7b7d18
PB
1159static void property_set_str(Object *obj, Visitor *v, void *opaque,
1160 const char *name, Error **errp)
57c9fafe
AL
1161{
1162 StringProperty *prop = opaque;
1163 char *value;
1164 Error *local_err = NULL;
1165
1166 visit_type_str(v, &value, name, &local_err);
1167 if (local_err) {
1168 error_propagate(errp, local_err);
1169 return;
1170 }
1171
1172 prop->set(obj, value, errp);
1173 g_free(value);
1174}
1175
7b7b7d18
PB
1176static void property_release_str(Object *obj, const char *name,
1177 void *opaque)
57c9fafe
AL
1178{
1179 StringProperty *prop = opaque;
1180 g_free(prop);
1181}
1182
1183void object_property_add_str(Object *obj, const char *name,
1184 char *(*get)(Object *, Error **),
1185 void (*set)(Object *, const char *, Error **),
1186 Error **errp)
1187{
1188 StringProperty *prop = g_malloc0(sizeof(*prop));
1189
1190 prop->get = get;
1191 prop->set = set;
1192
1193 object_property_add(obj, name, "string",
7b7b7d18
PB
1194 get ? property_get_str : NULL,
1195 set ? property_set_str : NULL,
1196 property_release_str,
57c9fafe
AL
1197 prop, errp);
1198}
745549c8 1199
0e558843
AL
1200typedef struct BoolProperty
1201{
1202 bool (*get)(Object *, Error **);
1203 void (*set)(Object *, bool, Error **);
1204} BoolProperty;
1205
1206static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1207 const char *name, Error **errp)
1208{
1209 BoolProperty *prop = opaque;
1210 bool value;
1211
1212 value = prop->get(obj, errp);
1213 visit_type_bool(v, &value, name, errp);
1214}
1215
1216static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1217 const char *name, Error **errp)
1218{
1219 BoolProperty *prop = opaque;
1220 bool value;
1221 Error *local_err = NULL;
1222
1223 visit_type_bool(v, &value, name, &local_err);
1224 if (local_err) {
1225 error_propagate(errp, local_err);
1226 return;
1227 }
1228
1229 prop->set(obj, value, errp);
1230}
1231
1232static void property_release_bool(Object *obj, const char *name,
1233 void *opaque)
1234{
1235 BoolProperty *prop = opaque;
1236 g_free(prop);
1237}
1238
1239void object_property_add_bool(Object *obj, const char *name,
1240 bool (*get)(Object *, Error **),
1241 void (*set)(Object *, bool, Error **),
1242 Error **errp)
1243{
1244 BoolProperty *prop = g_malloc0(sizeof(*prop));
1245
1246 prop->get = get;
1247 prop->set = set;
1248
1249 object_property_add(obj, name, "bool",
1250 get ? property_get_bool : NULL,
1251 set ? property_set_bool : NULL,
1252 property_release_bool,
1253 prop, errp);
1254}
1255
2f262e06
PB
1256static char *qdev_get_type(Object *obj, Error **errp)
1257{
1258 return g_strdup(object_get_typename(obj));
1259}
1260
1261static void object_instance_init(Object *obj)
1262{
1263 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1264}
1265
745549c8
PB
1266static void register_types(void)
1267{
1268 static TypeInfo interface_info = {
1269 .name = TYPE_INTERFACE,
33e95c63 1270 .class_size = sizeof(InterfaceClass),
745549c8
PB
1271 .abstract = true,
1272 };
1273
1274 static TypeInfo object_info = {
1275 .name = TYPE_OBJECT,
1276 .instance_size = sizeof(Object),
2f262e06 1277 .instance_init = object_instance_init,
745549c8
PB
1278 .abstract = true,
1279 };
1280
049cb3cf
PB
1281 type_interface = type_register_internal(&interface_info);
1282 type_register_internal(&object_info);
745549c8
PB
1283}
1284
1285type_init(register_types)