audio: don't use 'Yoda conditions'
[qemu/ar7.git] / qom / object.c
blob0e8267bc2acc9d4ecb31344cbfb649a405d866f2
1 /*
2 * QEMU Object Model
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
13 #include "qom/object.h"
14 #include "qemu-common.h"
15 #include "qapi/visitor.h"
16 #include "qapi-visit.h"
17 #include "qapi/string-input-visitor.h"
18 #include "qapi/string-output-visitor.h"
19 #include "qapi/qmp/qerror.h"
20 #include "trace.h"
22 /* TODO: replace QObject with a simpler visitor to avoid a dependency
23 * of the QOM core on QObject? */
24 #include "qom/qom-qobject.h"
25 #include "qapi/qmp/qobject.h"
26 #include "qapi/qmp/qbool.h"
27 #include "qapi/qmp/qint.h"
28 #include "qapi/qmp/qstring.h"
30 #define MAX_INTERFACES 32
32 typedef struct InterfaceImpl InterfaceImpl;
33 typedef struct TypeImpl TypeImpl;
35 struct InterfaceImpl
37 const char *typename;
40 struct TypeImpl
42 const char *name;
44 size_t class_size;
46 size_t instance_size;
48 void (*class_init)(ObjectClass *klass, void *data);
49 void (*class_base_init)(ObjectClass *klass, void *data);
50 void (*class_finalize)(ObjectClass *klass, void *data);
52 void *class_data;
54 void (*instance_init)(Object *obj);
55 void (*instance_post_init)(Object *obj);
56 void (*instance_finalize)(Object *obj);
58 bool abstract;
60 const char *parent;
61 TypeImpl *parent_type;
63 ObjectClass *class;
65 int num_interfaces;
66 InterfaceImpl interfaces[MAX_INTERFACES];
69 static Type type_interface;
71 static GHashTable *type_table_get(void)
73 static GHashTable *type_table;
75 if (type_table == NULL) {
76 type_table = g_hash_table_new(g_str_hash, g_str_equal);
79 return type_table;
82 static bool enumerating_types;
84 static void type_table_add(TypeImpl *ti)
86 assert(!enumerating_types);
87 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
90 static TypeImpl *type_table_lookup(const char *name)
92 return g_hash_table_lookup(type_table_get(), name);
95 static TypeImpl *type_new(const TypeInfo *info)
97 TypeImpl *ti = g_malloc0(sizeof(*ti));
98 int i;
100 g_assert(info->name != NULL);
102 if (type_table_lookup(info->name) != NULL) {
103 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
104 abort();
107 ti->name = g_strdup(info->name);
108 ti->parent = g_strdup(info->parent);
110 ti->class_size = info->class_size;
111 ti->instance_size = info->instance_size;
113 ti->class_init = info->class_init;
114 ti->class_base_init = info->class_base_init;
115 ti->class_finalize = info->class_finalize;
116 ti->class_data = info->class_data;
118 ti->instance_init = info->instance_init;
119 ti->instance_post_init = info->instance_post_init;
120 ti->instance_finalize = info->instance_finalize;
122 ti->abstract = info->abstract;
124 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
125 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
127 ti->num_interfaces = i;
129 return ti;
132 static TypeImpl *type_register_internal(const TypeInfo *info)
134 TypeImpl *ti;
135 ti = type_new(info);
137 type_table_add(ti);
138 return ti;
141 TypeImpl *type_register(const TypeInfo *info)
143 assert(info->parent);
144 return type_register_internal(info);
147 TypeImpl *type_register_static(const TypeInfo *info)
149 return type_register(info);
152 static TypeImpl *type_get_by_name(const char *name)
154 if (name == NULL) {
155 return NULL;
158 return type_table_lookup(name);
161 static TypeImpl *type_get_parent(TypeImpl *type)
163 if (!type->parent_type && type->parent) {
164 type->parent_type = type_get_by_name(type->parent);
165 g_assert(type->parent_type != NULL);
168 return type->parent_type;
171 static bool type_has_parent(TypeImpl *type)
173 return (type->parent != NULL);
176 static size_t type_class_get_size(TypeImpl *ti)
178 if (ti->class_size) {
179 return ti->class_size;
182 if (type_has_parent(ti)) {
183 return type_class_get_size(type_get_parent(ti));
186 return sizeof(ObjectClass);
189 static size_t type_object_get_size(TypeImpl *ti)
191 if (ti->instance_size) {
192 return ti->instance_size;
195 if (type_has_parent(ti)) {
196 return type_object_get_size(type_get_parent(ti));
199 return 0;
202 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
204 assert(target_type);
206 /* Check if typename is a direct ancestor of type */
207 while (type) {
208 if (type == target_type) {
209 return true;
212 type = type_get_parent(type);
215 return false;
218 static void type_initialize(TypeImpl *ti);
220 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
221 TypeImpl *parent_type)
223 InterfaceClass *new_iface;
224 TypeInfo info = { };
225 TypeImpl *iface_impl;
227 info.parent = parent_type->name;
228 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
229 info.abstract = true;
231 iface_impl = type_new(&info);
232 iface_impl->parent_type = parent_type;
233 type_initialize(iface_impl);
234 g_free((char *)info.name);
236 new_iface = (InterfaceClass *)iface_impl->class;
237 new_iface->concrete_class = ti->class;
238 new_iface->interface_type = interface_type;
240 ti->class->interfaces = g_slist_append(ti->class->interfaces,
241 iface_impl->class);
244 static void type_initialize(TypeImpl *ti)
246 TypeImpl *parent;
248 if (ti->class) {
249 return;
252 ti->class_size = type_class_get_size(ti);
253 ti->instance_size = type_object_get_size(ti);
255 ti->class = g_malloc0(ti->class_size);
257 parent = type_get_parent(ti);
258 if (parent) {
259 type_initialize(parent);
260 GSList *e;
261 int i;
263 g_assert(parent->class_size <= ti->class_size);
264 memcpy(ti->class, parent->class, parent->class_size);
265 ti->class->interfaces = NULL;
267 for (e = parent->class->interfaces; e; e = e->next) {
268 InterfaceClass *iface = e->data;
269 ObjectClass *klass = OBJECT_CLASS(iface);
271 type_initialize_interface(ti, iface->interface_type, klass->type);
274 for (i = 0; i < ti->num_interfaces; i++) {
275 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
276 for (e = ti->class->interfaces; e; e = e->next) {
277 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
279 if (type_is_ancestor(target_type, t)) {
280 break;
284 if (e) {
285 continue;
288 type_initialize_interface(ti, t, t);
292 ti->class->type = ti;
294 while (parent) {
295 if (parent->class_base_init) {
296 parent->class_base_init(ti->class, ti->class_data);
298 parent = type_get_parent(parent);
301 if (ti->class_init) {
302 ti->class_init(ti->class, ti->class_data);
306 static void object_init_with_type(Object *obj, TypeImpl *ti)
308 if (type_has_parent(ti)) {
309 object_init_with_type(obj, type_get_parent(ti));
312 if (ti->instance_init) {
313 ti->instance_init(obj);
317 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
319 if (ti->instance_post_init) {
320 ti->instance_post_init(obj);
323 if (type_has_parent(ti)) {
324 object_post_init_with_type(obj, type_get_parent(ti));
328 void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
330 Object *obj = data;
332 g_assert(type != NULL);
333 type_initialize(type);
335 g_assert(type->instance_size >= sizeof(Object));
336 g_assert(type->abstract == false);
337 g_assert(size >= type->instance_size);
339 memset(obj, 0, type->instance_size);
340 obj->class = type->class;
341 object_ref(obj);
342 QTAILQ_INIT(&obj->properties);
343 object_init_with_type(obj, type);
344 object_post_init_with_type(obj, type);
347 void object_initialize(void *data, size_t size, const char *typename)
349 TypeImpl *type = type_get_by_name(typename);
351 object_initialize_with_type(data, size, type);
354 static inline bool object_property_is_child(ObjectProperty *prop)
356 return strstart(prop->type, "child<", NULL);
359 static void object_property_del_all(Object *obj)
361 while (!QTAILQ_EMPTY(&obj->properties)) {
362 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
364 QTAILQ_REMOVE(&obj->properties, prop, node);
366 if (prop->release) {
367 prop->release(obj, prop->name, prop->opaque);
370 g_free(prop->name);
371 g_free(prop->type);
372 g_free(prop);
376 static void object_property_del_child(Object *obj, Object *child, Error **errp)
378 ObjectProperty *prop;
380 QTAILQ_FOREACH(prop, &obj->properties, node) {
381 if (object_property_is_child(prop) && prop->opaque == child) {
382 object_property_del(obj, prop->name, errp);
383 break;
388 void object_unparent(Object *obj)
390 if (!obj->parent) {
391 return;
394 object_ref(obj);
395 if (obj->class->unparent) {
396 (obj->class->unparent)(obj);
398 if (obj->parent) {
399 object_property_del_child(obj->parent, obj, NULL);
400 obj->parent = NULL;
402 object_unref(obj);
405 static void object_deinit(Object *obj, TypeImpl *type)
407 if (type->instance_finalize) {
408 type->instance_finalize(obj);
411 if (type_has_parent(type)) {
412 object_deinit(obj, type_get_parent(type));
416 static void object_finalize(void *data)
418 Object *obj = data;
419 TypeImpl *ti = obj->class->type;
421 object_deinit(obj, ti);
422 object_property_del_all(obj);
424 g_assert(obj->ref == 0);
425 if (obj->free) {
426 obj->free(obj);
430 Object *object_new_with_type(Type type)
432 Object *obj;
434 g_assert(type != NULL);
435 type_initialize(type);
437 obj = g_malloc(type->instance_size);
438 object_initialize_with_type(obj, type->instance_size, type);
439 obj->free = g_free;
441 return obj;
444 Object *object_new(const char *typename)
446 TypeImpl *ti = type_get_by_name(typename);
448 return object_new_with_type(ti);
451 Object *object_dynamic_cast(Object *obj, const char *typename)
453 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
454 return obj;
457 return NULL;
460 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
461 const char *file, int line, const char *func)
463 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
464 typename, file, line, func);
466 #ifdef CONFIG_QOM_CAST_DEBUG
467 int i;
468 Object *inst;
470 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
471 if (obj->class->object_cast_cache[i] == typename) {
472 goto out;
476 inst = object_dynamic_cast(obj, typename);
478 if (!inst && obj) {
479 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
480 file, line, func, obj, typename);
481 abort();
484 assert(obj == inst);
486 if (obj && obj == inst) {
487 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
488 obj->class->object_cast_cache[i - 1] =
489 obj->class->object_cast_cache[i];
491 obj->class->object_cast_cache[i - 1] = typename;
494 out:
495 #endif
496 return obj;
499 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
500 const char *typename)
502 ObjectClass *ret = NULL;
503 TypeImpl *target_type;
504 TypeImpl *type;
506 if (!class) {
507 return NULL;
510 /* A simple fast path that can trigger a lot for leaf classes. */
511 type = class->type;
512 if (type->name == typename) {
513 return class;
516 target_type = type_get_by_name(typename);
517 if (!target_type) {
518 /* target class type unknown, so fail the cast */
519 return NULL;
522 if (type->class->interfaces &&
523 type_is_ancestor(target_type, type_interface)) {
524 int found = 0;
525 GSList *i;
527 for (i = class->interfaces; i; i = i->next) {
528 ObjectClass *target_class = i->data;
530 if (type_is_ancestor(target_class->type, target_type)) {
531 ret = target_class;
532 found++;
536 /* The match was ambiguous, don't allow a cast */
537 if (found > 1) {
538 ret = NULL;
540 } else if (type_is_ancestor(type, target_type)) {
541 ret = class;
544 return ret;
547 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
548 const char *typename,
549 const char *file, int line,
550 const char *func)
552 ObjectClass *ret;
554 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
555 typename, file, line, func);
557 #ifdef CONFIG_QOM_CAST_DEBUG
558 int i;
560 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
561 if (class->class_cast_cache[i] == typename) {
562 ret = class;
563 goto out;
566 #else
567 if (!class || !class->interfaces) {
568 return class;
570 #endif
572 ret = object_class_dynamic_cast(class, typename);
573 if (!ret && class) {
574 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
575 file, line, func, class, typename);
576 abort();
579 #ifdef CONFIG_QOM_CAST_DEBUG
580 if (class && ret == class) {
581 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
582 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
584 class->class_cast_cache[i - 1] = typename;
586 out:
587 #endif
588 return ret;
591 const char *object_get_typename(Object *obj)
593 return obj->class->type->name;
596 ObjectClass *object_get_class(Object *obj)
598 return obj->class;
601 bool object_class_is_abstract(ObjectClass *klass)
603 return klass->type->abstract;
606 const char *object_class_get_name(ObjectClass *klass)
608 return klass->type->name;
611 ObjectClass *object_class_by_name(const char *typename)
613 TypeImpl *type = type_get_by_name(typename);
615 if (!type) {
616 return NULL;
619 type_initialize(type);
621 return type->class;
624 ObjectClass *object_class_get_parent(ObjectClass *class)
626 TypeImpl *type = type_get_parent(class->type);
628 if (!type) {
629 return NULL;
632 type_initialize(type);
634 return type->class;
637 typedef struct OCFData
639 void (*fn)(ObjectClass *klass, void *opaque);
640 const char *implements_type;
641 bool include_abstract;
642 void *opaque;
643 } OCFData;
645 static void object_class_foreach_tramp(gpointer key, gpointer value,
646 gpointer opaque)
648 OCFData *data = opaque;
649 TypeImpl *type = value;
650 ObjectClass *k;
652 type_initialize(type);
653 k = type->class;
655 if (!data->include_abstract && type->abstract) {
656 return;
659 if (data->implements_type &&
660 !object_class_dynamic_cast(k, data->implements_type)) {
661 return;
664 data->fn(k, data->opaque);
667 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
668 const char *implements_type, bool include_abstract,
669 void *opaque)
671 OCFData data = { fn, implements_type, include_abstract, opaque };
673 enumerating_types = true;
674 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
675 enumerating_types = false;
678 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
679 void *opaque)
681 ObjectProperty *prop;
682 int ret = 0;
684 QTAILQ_FOREACH(prop, &obj->properties, node) {
685 if (object_property_is_child(prop)) {
686 ret = fn(prop->opaque, opaque);
687 if (ret != 0) {
688 break;
692 return ret;
695 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
697 GSList **list = opaque;
699 *list = g_slist_prepend(*list, klass);
702 GSList *object_class_get_list(const char *implements_type,
703 bool include_abstract)
705 GSList *list = NULL;
707 object_class_foreach(object_class_get_list_tramp,
708 implements_type, include_abstract, &list);
709 return list;
712 void object_ref(Object *obj)
714 if (!obj) {
715 return;
717 atomic_inc(&obj->ref);
720 void object_unref(Object *obj)
722 if (!obj) {
723 return;
725 g_assert(obj->ref > 0);
727 /* parent always holds a reference to its children */
728 if (atomic_fetch_dec(&obj->ref) == 1) {
729 object_finalize(obj);
733 ObjectProperty *
734 object_property_add(Object *obj, const char *name, const char *type,
735 ObjectPropertyAccessor *get,
736 ObjectPropertyAccessor *set,
737 ObjectPropertyRelease *release,
738 void *opaque, Error **errp)
740 ObjectProperty *prop;
742 QTAILQ_FOREACH(prop, &obj->properties, node) {
743 if (strcmp(prop->name, name) == 0) {
744 error_setg(errp, "attempt to add duplicate property '%s'"
745 " to object (type '%s')", name,
746 object_get_typename(obj));
747 return NULL;
751 prop = g_malloc0(sizeof(*prop));
753 prop->name = g_strdup(name);
754 prop->type = g_strdup(type);
756 prop->get = get;
757 prop->set = set;
758 prop->release = release;
759 prop->opaque = opaque;
761 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
762 return prop;
765 ObjectProperty *object_property_find(Object *obj, const char *name,
766 Error **errp)
768 ObjectProperty *prop;
770 QTAILQ_FOREACH(prop, &obj->properties, node) {
771 if (strcmp(prop->name, name) == 0) {
772 return prop;
776 error_setg(errp, "Property '.%s' not found", name);
777 return NULL;
780 void object_property_del(Object *obj, const char *name, Error **errp)
782 ObjectProperty *prop = object_property_find(obj, name, errp);
783 if (prop == NULL) {
784 return;
787 if (prop->release) {
788 prop->release(obj, name, prop->opaque);
791 QTAILQ_REMOVE(&obj->properties, prop, node);
793 g_free(prop->name);
794 g_free(prop->type);
795 g_free(prop);
798 void object_property_get(Object *obj, Visitor *v, const char *name,
799 Error **errp)
801 ObjectProperty *prop = object_property_find(obj, name, errp);
802 if (prop == NULL) {
803 return;
806 if (!prop->get) {
807 error_set(errp, QERR_PERMISSION_DENIED);
808 } else {
809 prop->get(obj, v, prop->opaque, name, errp);
813 void object_property_set(Object *obj, Visitor *v, const char *name,
814 Error **errp)
816 ObjectProperty *prop = object_property_find(obj, name, errp);
817 if (prop == NULL) {
818 return;
821 if (!prop->set) {
822 error_set(errp, QERR_PERMISSION_DENIED);
823 } else {
824 prop->set(obj, v, prop->opaque, name, errp);
828 void object_property_set_str(Object *obj, const char *value,
829 const char *name, Error **errp)
831 QString *qstr = qstring_from_str(value);
832 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
834 QDECREF(qstr);
837 char *object_property_get_str(Object *obj, const char *name,
838 Error **errp)
840 QObject *ret = object_property_get_qobject(obj, name, errp);
841 QString *qstring;
842 char *retval;
844 if (!ret) {
845 return NULL;
847 qstring = qobject_to_qstring(ret);
848 if (!qstring) {
849 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
850 retval = NULL;
851 } else {
852 retval = g_strdup(qstring_get_str(qstring));
855 QDECREF(qstring);
856 return retval;
859 void object_property_set_link(Object *obj, Object *value,
860 const char *name, Error **errp)
862 gchar *path = object_get_canonical_path(value);
863 object_property_set_str(obj, path, name, errp);
864 g_free(path);
867 Object *object_property_get_link(Object *obj, const char *name,
868 Error **errp)
870 char *str = object_property_get_str(obj, name, errp);
871 Object *target = NULL;
873 if (str && *str) {
874 target = object_resolve_path(str, NULL);
875 if (!target) {
876 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
880 g_free(str);
881 return target;
884 void object_property_set_bool(Object *obj, bool value,
885 const char *name, Error **errp)
887 QBool *qbool = qbool_from_int(value);
888 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
890 QDECREF(qbool);
893 bool object_property_get_bool(Object *obj, const char *name,
894 Error **errp)
896 QObject *ret = object_property_get_qobject(obj, name, errp);
897 QBool *qbool;
898 bool retval;
900 if (!ret) {
901 return false;
903 qbool = qobject_to_qbool(ret);
904 if (!qbool) {
905 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
906 retval = false;
907 } else {
908 retval = qbool_get_int(qbool);
911 QDECREF(qbool);
912 return retval;
915 void object_property_set_int(Object *obj, int64_t value,
916 const char *name, Error **errp)
918 QInt *qint = qint_from_int(value);
919 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
921 QDECREF(qint);
924 int64_t object_property_get_int(Object *obj, const char *name,
925 Error **errp)
927 QObject *ret = object_property_get_qobject(obj, name, errp);
928 QInt *qint;
929 int64_t retval;
931 if (!ret) {
932 return -1;
934 qint = qobject_to_qint(ret);
935 if (!qint) {
936 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
937 retval = -1;
938 } else {
939 retval = qint_get_int(qint);
942 QDECREF(qint);
943 return retval;
946 int object_property_get_enum(Object *obj, const char *name,
947 const char *strings[], Error **errp)
949 StringOutputVisitor *sov;
950 StringInputVisitor *siv;
951 int ret;
953 sov = string_output_visitor_new(false);
954 object_property_get(obj, string_output_get_visitor(sov), name, errp);
955 siv = string_input_visitor_new(string_output_get_string(sov));
956 string_output_visitor_cleanup(sov);
957 visit_type_enum(string_input_get_visitor(siv),
958 &ret, strings, NULL, name, errp);
959 string_input_visitor_cleanup(siv);
961 return ret;
964 void object_property_get_uint16List(Object *obj, const char *name,
965 uint16List **list, Error **errp)
967 StringOutputVisitor *ov;
968 StringInputVisitor *iv;
970 ov = string_output_visitor_new(false);
971 object_property_get(obj, string_output_get_visitor(ov),
972 name, errp);
973 iv = string_input_visitor_new(string_output_get_string(ov));
974 visit_type_uint16List(string_input_get_visitor(iv),
975 list, NULL, errp);
976 string_output_visitor_cleanup(ov);
977 string_input_visitor_cleanup(iv);
980 void object_property_parse(Object *obj, const char *string,
981 const char *name, Error **errp)
983 StringInputVisitor *mi;
984 mi = string_input_visitor_new(string);
985 object_property_set(obj, string_input_get_visitor(mi), name, errp);
987 string_input_visitor_cleanup(mi);
990 char *object_property_print(Object *obj, const char *name, bool human,
991 Error **errp)
993 StringOutputVisitor *mo;
994 char *string;
996 mo = string_output_visitor_new(human);
997 object_property_get(obj, string_output_get_visitor(mo), name, errp);
998 string = string_output_get_string(mo);
999 string_output_visitor_cleanup(mo);
1000 return string;
1003 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1005 ObjectProperty *prop = object_property_find(obj, name, errp);
1006 if (prop == NULL) {
1007 return NULL;
1010 return prop->type;
1013 Object *object_get_root(void)
1015 static Object *root;
1017 if (!root) {
1018 root = object_new("container");
1021 return root;
1024 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
1025 const char *name, Error **errp)
1027 Object *child = opaque;
1028 gchar *path;
1030 path = object_get_canonical_path(child);
1031 visit_type_str(v, &path, name, errp);
1032 g_free(path);
1035 static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1037 return opaque;
1040 static void object_finalize_child_property(Object *obj, const char *name,
1041 void *opaque)
1043 Object *child = opaque;
1045 object_unref(child);
1048 void object_property_add_child(Object *obj, const char *name,
1049 Object *child, Error **errp)
1051 Error *local_err = NULL;
1052 gchar *type;
1053 ObjectProperty *op;
1055 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1057 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1058 object_finalize_child_property, child, &local_err);
1059 if (local_err) {
1060 error_propagate(errp, local_err);
1061 goto out;
1064 op->resolve = object_resolve_child_property;
1065 object_ref(child);
1066 g_assert(child->parent == NULL);
1067 child->parent = obj;
1069 out:
1070 g_free(type);
1073 void object_property_allow_set_link(Object *obj, const char *name,
1074 Object *val, Error **errp)
1076 /* Allow the link to be set, always */
1079 typedef struct {
1080 Object **child;
1081 void (*check)(Object *, const char *, Object *, Error **);
1082 ObjectPropertyLinkFlags flags;
1083 } LinkProperty;
1085 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1086 const char *name, Error **errp)
1088 LinkProperty *lprop = opaque;
1089 Object **child = lprop->child;
1090 gchar *path;
1092 if (*child) {
1093 path = object_get_canonical_path(*child);
1094 visit_type_str(v, &path, name, errp);
1095 g_free(path);
1096 } else {
1097 path = (gchar *)"";
1098 visit_type_str(v, &path, name, errp);
1103 * object_resolve_link:
1105 * Lookup an object and ensure its type matches the link property type. This
1106 * is similar to object_resolve_path() except type verification against the
1107 * link property is performed.
1109 * Returns: The matched object or NULL on path lookup failures.
1111 static Object *object_resolve_link(Object *obj, const char *name,
1112 const char *path, Error **errp)
1114 const char *type;
1115 gchar *target_type;
1116 bool ambiguous = false;
1117 Object *target;
1119 /* Go from link<FOO> to FOO. */
1120 type = object_property_get_type(obj, name, NULL);
1121 target_type = g_strndup(&type[5], strlen(type) - 6);
1122 target = object_resolve_path_type(path, target_type, &ambiguous);
1124 if (ambiguous) {
1125 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
1126 "Path '%s' does not uniquely identify an object", path);
1127 } else if (!target) {
1128 target = object_resolve_path(path, &ambiguous);
1129 if (target || ambiguous) {
1130 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1131 } else {
1132 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1134 target = NULL;
1136 g_free(target_type);
1138 return target;
1141 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1142 const char *name, Error **errp)
1144 Error *local_err = NULL;
1145 LinkProperty *prop = opaque;
1146 Object **child = prop->child;
1147 Object *old_target = *child;
1148 Object *new_target = NULL;
1149 char *path = NULL;
1151 visit_type_str(v, &path, name, &local_err);
1153 if (!local_err && strcmp(path, "") != 0) {
1154 new_target = object_resolve_link(obj, name, path, &local_err);
1157 g_free(path);
1158 if (local_err) {
1159 error_propagate(errp, local_err);
1160 return;
1163 prop->check(obj, name, new_target, &local_err);
1164 if (local_err) {
1165 error_propagate(errp, local_err);
1166 return;
1169 object_ref(new_target);
1170 *child = new_target;
1171 object_unref(old_target);
1174 static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1176 LinkProperty *lprop = opaque;
1178 return *lprop->child;
1181 static void object_release_link_property(Object *obj, const char *name,
1182 void *opaque)
1184 LinkProperty *prop = opaque;
1186 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1187 object_unref(*prop->child);
1189 g_free(prop);
1192 void object_property_add_link(Object *obj, const char *name,
1193 const char *type, Object **child,
1194 void (*check)(Object *, const char *,
1195 Object *, Error **),
1196 ObjectPropertyLinkFlags flags,
1197 Error **errp)
1199 Error *local_err = NULL;
1200 LinkProperty *prop = g_malloc(sizeof(*prop));
1201 gchar *full_type;
1202 ObjectProperty *op;
1204 prop->child = child;
1205 prop->check = check;
1206 prop->flags = flags;
1208 full_type = g_strdup_printf("link<%s>", type);
1210 op = object_property_add(obj, name, full_type,
1211 object_get_link_property,
1212 check ? object_set_link_property : NULL,
1213 object_release_link_property,
1214 prop,
1215 &local_err);
1216 if (local_err) {
1217 error_propagate(errp, local_err);
1218 g_free(prop);
1219 goto out;
1222 op->resolve = object_resolve_link_property;
1224 out:
1225 g_free(full_type);
1228 gchar *object_get_canonical_path_component(Object *obj)
1230 ObjectProperty *prop = NULL;
1232 g_assert(obj);
1233 g_assert(obj->parent != NULL);
1235 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1236 if (!object_property_is_child(prop)) {
1237 continue;
1240 if (prop->opaque == obj) {
1241 return g_strdup(prop->name);
1245 /* obj had a parent but was not a child, should never happen */
1246 g_assert_not_reached();
1247 return NULL;
1250 gchar *object_get_canonical_path(Object *obj)
1252 Object *root = object_get_root();
1253 char *newpath, *path = NULL;
1255 while (obj != root) {
1256 char *component = object_get_canonical_path_component(obj);
1258 if (path) {
1259 newpath = g_strdup_printf("%s/%s", component, path);
1260 g_free(component);
1261 g_free(path);
1262 path = newpath;
1263 } else {
1264 path = component;
1267 obj = obj->parent;
1270 newpath = g_strdup_printf("/%s", path ? path : "");
1271 g_free(path);
1273 return newpath;
1276 Object *object_resolve_path_component(Object *parent, const gchar *part)
1278 ObjectProperty *prop = object_property_find(parent, part, NULL);
1279 if (prop == NULL) {
1280 return NULL;
1283 if (prop->resolve) {
1284 return prop->resolve(parent, prop->opaque, part);
1285 } else {
1286 return NULL;
1290 static Object *object_resolve_abs_path(Object *parent,
1291 gchar **parts,
1292 const char *typename,
1293 int index)
1295 Object *child;
1297 if (parts[index] == NULL) {
1298 return object_dynamic_cast(parent, typename);
1301 if (strcmp(parts[index], "") == 0) {
1302 return object_resolve_abs_path(parent, parts, typename, index + 1);
1305 child = object_resolve_path_component(parent, parts[index]);
1306 if (!child) {
1307 return NULL;
1310 return object_resolve_abs_path(child, parts, typename, index + 1);
1313 static Object *object_resolve_partial_path(Object *parent,
1314 gchar **parts,
1315 const char *typename,
1316 bool *ambiguous)
1318 Object *obj;
1319 ObjectProperty *prop;
1321 obj = object_resolve_abs_path(parent, parts, typename, 0);
1323 QTAILQ_FOREACH(prop, &parent->properties, node) {
1324 Object *found;
1326 if (!object_property_is_child(prop)) {
1327 continue;
1330 found = object_resolve_partial_path(prop->opaque, parts,
1331 typename, ambiguous);
1332 if (found) {
1333 if (obj) {
1334 if (ambiguous) {
1335 *ambiguous = true;
1337 return NULL;
1339 obj = found;
1342 if (ambiguous && *ambiguous) {
1343 return NULL;
1347 return obj;
1350 Object *object_resolve_path_type(const char *path, const char *typename,
1351 bool *ambiguous)
1353 Object *obj;
1354 gchar **parts;
1356 parts = g_strsplit(path, "/", 0);
1357 assert(parts);
1359 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
1360 if (ambiguous) {
1361 *ambiguous = false;
1363 obj = object_resolve_partial_path(object_get_root(), parts,
1364 typename, ambiguous);
1365 } else {
1366 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1369 g_strfreev(parts);
1371 return obj;
1374 Object *object_resolve_path(const char *path, bool *ambiguous)
1376 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1379 typedef struct StringProperty
1381 char *(*get)(Object *, Error **);
1382 void (*set)(Object *, const char *, Error **);
1383 } StringProperty;
1385 static void property_get_str(Object *obj, Visitor *v, void *opaque,
1386 const char *name, Error **errp)
1388 StringProperty *prop = opaque;
1389 char *value;
1391 value = prop->get(obj, errp);
1392 if (value) {
1393 visit_type_str(v, &value, name, errp);
1394 g_free(value);
1398 static void property_set_str(Object *obj, Visitor *v, void *opaque,
1399 const char *name, Error **errp)
1401 StringProperty *prop = opaque;
1402 char *value;
1403 Error *local_err = NULL;
1405 visit_type_str(v, &value, name, &local_err);
1406 if (local_err) {
1407 error_propagate(errp, local_err);
1408 return;
1411 prop->set(obj, value, errp);
1412 g_free(value);
1415 static void property_release_str(Object *obj, const char *name,
1416 void *opaque)
1418 StringProperty *prop = opaque;
1419 g_free(prop);
1422 void object_property_add_str(Object *obj, const char *name,
1423 char *(*get)(Object *, Error **),
1424 void (*set)(Object *, const char *, Error **),
1425 Error **errp)
1427 Error *local_err = NULL;
1428 StringProperty *prop = g_malloc0(sizeof(*prop));
1430 prop->get = get;
1431 prop->set = set;
1433 object_property_add(obj, name, "string",
1434 get ? property_get_str : NULL,
1435 set ? property_set_str : NULL,
1436 property_release_str,
1437 prop, &local_err);
1438 if (local_err) {
1439 error_propagate(errp, local_err);
1440 g_free(prop);
1444 typedef struct BoolProperty
1446 bool (*get)(Object *, Error **);
1447 void (*set)(Object *, bool, Error **);
1448 } BoolProperty;
1450 static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1451 const char *name, Error **errp)
1453 BoolProperty *prop = opaque;
1454 bool value;
1456 value = prop->get(obj, errp);
1457 visit_type_bool(v, &value, name, errp);
1460 static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1461 const char *name, Error **errp)
1463 BoolProperty *prop = opaque;
1464 bool value;
1465 Error *local_err = NULL;
1467 visit_type_bool(v, &value, name, &local_err);
1468 if (local_err) {
1469 error_propagate(errp, local_err);
1470 return;
1473 prop->set(obj, value, errp);
1476 static void property_release_bool(Object *obj, const char *name,
1477 void *opaque)
1479 BoolProperty *prop = opaque;
1480 g_free(prop);
1483 void object_property_add_bool(Object *obj, const char *name,
1484 bool (*get)(Object *, Error **),
1485 void (*set)(Object *, bool, Error **),
1486 Error **errp)
1488 Error *local_err = NULL;
1489 BoolProperty *prop = g_malloc0(sizeof(*prop));
1491 prop->get = get;
1492 prop->set = set;
1494 object_property_add(obj, name, "bool",
1495 get ? property_get_bool : NULL,
1496 set ? property_set_bool : NULL,
1497 property_release_bool,
1498 prop, &local_err);
1499 if (local_err) {
1500 error_propagate(errp, local_err);
1501 g_free(prop);
1505 static char *qdev_get_type(Object *obj, Error **errp)
1507 return g_strdup(object_get_typename(obj));
1510 static void property_get_uint8_ptr(Object *obj, Visitor *v,
1511 void *opaque, const char *name,
1512 Error **errp)
1514 uint8_t value = *(uint8_t *)opaque;
1515 visit_type_uint8(v, &value, name, errp);
1518 static void property_get_uint16_ptr(Object *obj, Visitor *v,
1519 void *opaque, const char *name,
1520 Error **errp)
1522 uint16_t value = *(uint16_t *)opaque;
1523 visit_type_uint16(v, &value, name, errp);
1526 static void property_get_uint32_ptr(Object *obj, Visitor *v,
1527 void *opaque, const char *name,
1528 Error **errp)
1530 uint32_t value = *(uint32_t *)opaque;
1531 visit_type_uint32(v, &value, name, errp);
1534 static void property_get_uint64_ptr(Object *obj, Visitor *v,
1535 void *opaque, const char *name,
1536 Error **errp)
1538 uint64_t value = *(uint64_t *)opaque;
1539 visit_type_uint64(v, &value, name, errp);
1542 void object_property_add_uint8_ptr(Object *obj, const char *name,
1543 const uint8_t *v, Error **errp)
1545 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1546 NULL, NULL, (void *)v, errp);
1549 void object_property_add_uint16_ptr(Object *obj, const char *name,
1550 const uint16_t *v, Error **errp)
1552 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1553 NULL, NULL, (void *)v, errp);
1556 void object_property_add_uint32_ptr(Object *obj, const char *name,
1557 const uint32_t *v, Error **errp)
1559 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1560 NULL, NULL, (void *)v, errp);
1563 void object_property_add_uint64_ptr(Object *obj, const char *name,
1564 const uint64_t *v, Error **errp)
1566 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1567 NULL, NULL, (void *)v, errp);
1570 typedef struct {
1571 Object *target_obj;
1572 const char *target_name;
1573 } AliasProperty;
1575 static void property_get_alias(Object *obj, struct Visitor *v, void *opaque,
1576 const char *name, Error **errp)
1578 AliasProperty *prop = opaque;
1580 object_property_get(prop->target_obj, v, prop->target_name, errp);
1583 static void property_set_alias(Object *obj, struct Visitor *v, void *opaque,
1584 const char *name, Error **errp)
1586 AliasProperty *prop = opaque;
1588 object_property_set(prop->target_obj, v, prop->target_name, errp);
1591 static Object *property_resolve_alias(Object *obj, void *opaque,
1592 const gchar *part)
1594 AliasProperty *prop = opaque;
1596 return object_resolve_path_component(prop->target_obj, prop->target_name);
1599 static void property_release_alias(Object *obj, const char *name, void *opaque)
1601 AliasProperty *prop = opaque;
1603 g_free(prop);
1606 void object_property_add_alias(Object *obj, const char *name,
1607 Object *target_obj, const char *target_name,
1608 Error **errp)
1610 AliasProperty *prop;
1611 ObjectProperty *op;
1612 ObjectProperty *target_prop;
1613 gchar *prop_type;
1615 target_prop = object_property_find(target_obj, target_name, errp);
1616 if (!target_prop) {
1617 return;
1620 if (object_property_is_child(target_prop)) {
1621 prop_type = g_strdup_printf("link%s",
1622 target_prop->type + strlen("child"));
1623 } else {
1624 prop_type = g_strdup(target_prop->type);
1627 prop = g_malloc(sizeof(*prop));
1628 prop->target_obj = target_obj;
1629 prop->target_name = target_name;
1631 op = object_property_add(obj, name, prop_type,
1632 property_get_alias,
1633 property_set_alias,
1634 property_release_alias,
1635 prop, errp);
1636 op->resolve = property_resolve_alias;
1638 g_free(prop_type);
1641 static void object_instance_init(Object *obj)
1643 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1646 static void register_types(void)
1648 static TypeInfo interface_info = {
1649 .name = TYPE_INTERFACE,
1650 .class_size = sizeof(InterfaceClass),
1651 .abstract = true,
1654 static TypeInfo object_info = {
1655 .name = TYPE_OBJECT,
1656 .instance_size = sizeof(Object),
1657 .instance_init = object_instance_init,
1658 .abstract = true,
1661 type_interface = type_register_internal(&interface_info);
1662 type_register_internal(&object_info);
1665 type_init(register_types)