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