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