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