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