openpic: combine openpic and mpic reset functions
[qemu/agraf.git] / qom / object.c
blob0739aa29431accdaa8c92e860318eb56452d2260
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 "qemu/object.h"
14 #include "qemu-common.h"
15 #include "qapi/qapi-visit-core.h"
16 #include "qapi/string-input-visitor.h"
17 #include "qapi/string-output-visitor.h"
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"
27 #define MAX_INTERFACES 32
29 typedef struct InterfaceImpl InterfaceImpl;
30 typedef struct TypeImpl TypeImpl;
32 struct InterfaceImpl
34 const char *typename;
37 struct TypeImpl
39 const char *name;
41 size_t class_size;
43 size_t instance_size;
45 void (*class_init)(ObjectClass *klass, void *data);
46 void (*class_base_init)(ObjectClass *klass, void *data);
47 void (*class_finalize)(ObjectClass *klass, void *data);
49 void *class_data;
51 void (*instance_init)(Object *obj);
52 void (*instance_finalize)(Object *obj);
54 bool abstract;
56 const char *parent;
57 TypeImpl *parent_type;
59 ObjectClass *class;
61 int num_interfaces;
62 InterfaceImpl interfaces[MAX_INTERFACES];
65 static Type type_interface;
67 static GHashTable *type_table_get(void)
69 static GHashTable *type_table;
71 if (type_table == NULL) {
72 type_table = g_hash_table_new(g_str_hash, g_str_equal);
75 return type_table;
78 static void type_table_add(TypeImpl *ti)
80 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
83 static TypeImpl *type_table_lookup(const char *name)
85 return g_hash_table_lookup(type_table_get(), name);
88 static TypeImpl *type_register_internal(const TypeInfo *info)
90 TypeImpl *ti = g_malloc0(sizeof(*ti));
91 int i;
93 g_assert(info->name != NULL);
95 if (type_table_lookup(info->name) != NULL) {
96 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
97 abort();
100 ti->name = g_strdup(info->name);
101 ti->parent = g_strdup(info->parent);
103 ti->class_size = info->class_size;
104 ti->instance_size = info->instance_size;
106 ti->class_init = info->class_init;
107 ti->class_base_init = info->class_base_init;
108 ti->class_finalize = info->class_finalize;
109 ti->class_data = info->class_data;
111 ti->instance_init = info->instance_init;
112 ti->instance_finalize = info->instance_finalize;
114 ti->abstract = info->abstract;
116 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
117 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
119 ti->num_interfaces = i;
121 type_table_add(ti);
123 return ti;
126 TypeImpl *type_register(const TypeInfo *info)
128 assert(info->parent);
129 return type_register_internal(info);
132 TypeImpl *type_register_static(const TypeInfo *info)
134 return type_register(info);
137 static TypeImpl *type_get_by_name(const char *name)
139 if (name == NULL) {
140 return NULL;
143 return type_table_lookup(name);
146 static TypeImpl *type_get_parent(TypeImpl *type)
148 if (!type->parent_type && type->parent) {
149 type->parent_type = type_get_by_name(type->parent);
150 g_assert(type->parent_type != NULL);
153 return type->parent_type;
156 static bool type_has_parent(TypeImpl *type)
158 return (type->parent != NULL);
161 static size_t type_class_get_size(TypeImpl *ti)
163 if (ti->class_size) {
164 return ti->class_size;
167 if (type_has_parent(ti)) {
168 return type_class_get_size(type_get_parent(ti));
171 return sizeof(ObjectClass);
174 static size_t type_object_get_size(TypeImpl *ti)
176 if (ti->instance_size) {
177 return ti->instance_size;
180 if (type_has_parent(ti)) {
181 return type_object_get_size(type_get_parent(ti));
184 return 0;
187 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
189 assert(target_type);
191 /* Check if typename is a direct ancestor of type */
192 while (type) {
193 if (type == target_type) {
194 return true;
197 type = type_get_parent(type);
200 return false;
203 static void type_initialize(TypeImpl *ti);
205 static void type_initialize_interface(TypeImpl *ti, const char *parent)
207 InterfaceClass *new_iface;
208 TypeInfo info = { };
209 TypeImpl *iface_impl;
211 info.parent = parent;
212 info.name = g_strdup_printf("%s::%s", ti->name, info.parent);
213 info.abstract = true;
215 iface_impl = type_register(&info);
216 type_initialize(iface_impl);
217 g_free((char *)info.name);
219 new_iface = (InterfaceClass *)iface_impl->class;
220 new_iface->concrete_class = ti->class;
222 ti->class->interfaces = g_slist_append(ti->class->interfaces,
223 iface_impl->class);
226 static void type_initialize(TypeImpl *ti)
228 TypeImpl *parent;
230 if (ti->class) {
231 return;
234 ti->class_size = type_class_get_size(ti);
235 ti->instance_size = type_object_get_size(ti);
237 ti->class = g_malloc0(ti->class_size);
239 parent = type_get_parent(ti);
240 if (parent) {
241 type_initialize(parent);
242 GSList *e;
243 int i;
245 g_assert(parent->class_size <= ti->class_size);
246 memcpy(ti->class, parent->class, parent->class_size);
248 for (e = parent->class->interfaces; e; e = e->next) {
249 ObjectClass *iface = e->data;
250 type_initialize_interface(ti, object_class_get_name(iface));
253 for (i = 0; i < ti->num_interfaces; i++) {
254 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
255 for (e = ti->class->interfaces; e; e = e->next) {
256 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
258 if (type_is_ancestor(target_type, t)) {
259 break;
263 if (e) {
264 continue;
267 type_initialize_interface(ti, ti->interfaces[i].typename);
271 ti->class->type = ti;
273 while (parent) {
274 if (parent->class_base_init) {
275 parent->class_base_init(ti->class, ti->class_data);
277 parent = type_get_parent(parent);
280 if (ti->class_init) {
281 ti->class_init(ti->class, ti->class_data);
287 static void object_init_with_type(Object *obj, TypeImpl *ti)
289 if (type_has_parent(ti)) {
290 object_init_with_type(obj, type_get_parent(ti));
293 if (ti->instance_init) {
294 ti->instance_init(obj);
298 void object_initialize_with_type(void *data, TypeImpl *type)
300 Object *obj = data;
302 g_assert(type != NULL);
303 type_initialize(type);
305 g_assert(type->instance_size >= sizeof(Object));
306 g_assert(type->abstract == false);
308 memset(obj, 0, type->instance_size);
309 obj->class = type->class;
310 object_ref(obj);
311 QTAILQ_INIT(&obj->properties);
312 object_init_with_type(obj, type);
315 void object_initialize(void *data, const char *typename)
317 TypeImpl *type = type_get_by_name(typename);
319 object_initialize_with_type(data, type);
322 static inline bool object_property_is_child(ObjectProperty *prop)
324 return strstart(prop->type, "child<", NULL);
327 static inline bool object_property_is_link(ObjectProperty *prop)
329 return strstart(prop->type, "link<", NULL);
332 static void object_property_del_all(Object *obj)
334 while (!QTAILQ_EMPTY(&obj->properties)) {
335 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
337 QTAILQ_REMOVE(&obj->properties, prop, node);
339 if (prop->release) {
340 prop->release(obj, prop->name, prop->opaque);
343 g_free(prop->name);
344 g_free(prop->type);
345 g_free(prop);
349 static void object_property_del_child(Object *obj, Object *child, Error **errp)
351 ObjectProperty *prop;
353 QTAILQ_FOREACH(prop, &obj->properties, node) {
354 if (object_property_is_child(prop) && prop->opaque == child) {
355 object_property_del(obj, prop->name, errp);
356 break;
361 void object_unparent(Object *obj)
363 if (obj->parent) {
364 object_property_del_child(obj->parent, obj, NULL);
366 if (obj->class->unparent) {
367 (obj->class->unparent)(obj);
371 static void object_deinit(Object *obj, TypeImpl *type)
373 if (type->instance_finalize) {
374 type->instance_finalize(obj);
377 if (type_has_parent(type)) {
378 object_deinit(obj, type_get_parent(type));
382 static void object_finalize(void *data)
384 Object *obj = data;
385 TypeImpl *ti = obj->class->type;
387 object_deinit(obj, ti);
388 object_property_del_all(obj);
390 g_assert(obj->ref == 0);
391 if (obj->free) {
392 obj->free(obj);
396 Object *object_new_with_type(Type type)
398 Object *obj;
400 g_assert(type != NULL);
401 type_initialize(type);
403 obj = g_malloc(type->instance_size);
404 object_initialize_with_type(obj, type);
405 obj->free = g_free;
407 return obj;
410 Object *object_new(const char *typename)
412 TypeImpl *ti = type_get_by_name(typename);
414 return object_new_with_type(ti);
417 void object_delete(Object *obj)
419 object_unparent(obj);
420 g_assert(obj->ref == 1);
421 object_unref(obj);
424 Object *object_dynamic_cast(Object *obj, const char *typename)
426 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
427 return obj;
430 return NULL;
433 Object *object_dynamic_cast_assert(Object *obj, const char *typename)
435 Object *inst;
437 inst = object_dynamic_cast(obj, typename);
439 if (!inst && obj) {
440 fprintf(stderr, "Object %p is not an instance of type %s\n",
441 obj, typename);
442 abort();
445 return inst;
448 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
449 const char *typename)
451 TypeImpl *target_type = type_get_by_name(typename);
452 TypeImpl *type = class->type;
453 ObjectClass *ret = NULL;
455 if (type->num_interfaces && type_is_ancestor(target_type, type_interface)) {
456 int found = 0;
457 GSList *i;
459 for (i = class->interfaces; i; i = i->next) {
460 ObjectClass *target_class = i->data;
462 if (type_is_ancestor(target_class->type, target_type)) {
463 ret = target_class;
464 found++;
468 /* The match was ambiguous, don't allow a cast */
469 if (found > 1) {
470 ret = NULL;
472 } else if (type_is_ancestor(type, target_type)) {
473 ret = class;
476 return ret;
479 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
480 const char *typename)
482 ObjectClass *ret = object_class_dynamic_cast(class, typename);
484 if (!ret) {
485 fprintf(stderr, "Object %p is not an instance of type %s\n",
486 class, typename);
487 abort();
490 return ret;
493 const char *object_get_typename(Object *obj)
495 return obj->class->type->name;
498 ObjectClass *object_get_class(Object *obj)
500 return obj->class;
503 const char *object_class_get_name(ObjectClass *klass)
505 return klass->type->name;
508 ObjectClass *object_class_by_name(const char *typename)
510 TypeImpl *type = type_get_by_name(typename);
512 if (!type) {
513 return NULL;
516 type_initialize(type);
518 return type->class;
521 ObjectClass *object_class_get_parent(ObjectClass *class)
523 TypeImpl *type = type_get_parent(class->type);
525 if (!type) {
526 return NULL;
529 type_initialize(type);
531 return type->class;
534 typedef struct OCFData
536 void (*fn)(ObjectClass *klass, void *opaque);
537 const char *implements_type;
538 bool include_abstract;
539 void *opaque;
540 } OCFData;
542 static void object_class_foreach_tramp(gpointer key, gpointer value,
543 gpointer opaque)
545 OCFData *data = opaque;
546 TypeImpl *type = value;
547 ObjectClass *k;
549 type_initialize(type);
550 k = type->class;
552 if (!data->include_abstract && type->abstract) {
553 return;
556 if (data->implements_type &&
557 !object_class_dynamic_cast(k, data->implements_type)) {
558 return;
561 data->fn(k, data->opaque);
564 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
565 const char *implements_type, bool include_abstract,
566 void *opaque)
568 OCFData data = { fn, implements_type, include_abstract, opaque };
570 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
573 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
574 void *opaque)
576 ObjectProperty *prop;
577 int ret = 0;
579 QTAILQ_FOREACH(prop, &obj->properties, node) {
580 if (object_property_is_child(prop)) {
581 ret = fn(prop->opaque, opaque);
582 if (ret != 0) {
583 break;
587 return ret;
590 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
592 GSList **list = opaque;
594 *list = g_slist_prepend(*list, klass);
597 GSList *object_class_get_list(const char *implements_type,
598 bool include_abstract)
600 GSList *list = NULL;
602 object_class_foreach(object_class_get_list_tramp,
603 implements_type, include_abstract, &list);
604 return list;
607 void object_ref(Object *obj)
609 obj->ref++;
612 void object_unref(Object *obj)
614 g_assert(obj->ref > 0);
615 obj->ref--;
617 /* parent always holds a reference to its children */
618 if (obj->ref == 0) {
619 object_finalize(obj);
623 void object_property_add(Object *obj, const char *name, const char *type,
624 ObjectPropertyAccessor *get,
625 ObjectPropertyAccessor *set,
626 ObjectPropertyRelease *release,
627 void *opaque, Error **errp)
629 ObjectProperty *prop = g_malloc0(sizeof(*prop));
631 prop->name = g_strdup(name);
632 prop->type = g_strdup(type);
634 prop->get = get;
635 prop->set = set;
636 prop->release = release;
637 prop->opaque = opaque;
639 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
642 ObjectProperty *object_property_find(Object *obj, const char *name,
643 Error **errp)
645 ObjectProperty *prop;
647 QTAILQ_FOREACH(prop, &obj->properties, node) {
648 if (strcmp(prop->name, name) == 0) {
649 return prop;
653 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
654 return NULL;
657 void object_property_del(Object *obj, const char *name, Error **errp)
659 ObjectProperty *prop = object_property_find(obj, name, errp);
660 if (prop == NULL) {
661 return;
664 if (prop->release) {
665 prop->release(obj, name, prop->opaque);
668 QTAILQ_REMOVE(&obj->properties, prop, node);
670 g_free(prop->name);
671 g_free(prop->type);
672 g_free(prop);
675 void object_property_get(Object *obj, Visitor *v, const char *name,
676 Error **errp)
678 ObjectProperty *prop = object_property_find(obj, name, errp);
679 if (prop == NULL) {
680 return;
683 if (!prop->get) {
684 error_set(errp, QERR_PERMISSION_DENIED);
685 } else {
686 prop->get(obj, v, prop->opaque, name, errp);
690 void object_property_set(Object *obj, Visitor *v, const char *name,
691 Error **errp)
693 ObjectProperty *prop = object_property_find(obj, name, errp);
694 if (prop == NULL) {
695 return;
698 if (!prop->set) {
699 error_set(errp, QERR_PERMISSION_DENIED);
700 } else {
701 prop->set(obj, v, prop->opaque, name, errp);
705 void object_property_set_str(Object *obj, const char *value,
706 const char *name, Error **errp)
708 QString *qstr = qstring_from_str(value);
709 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
711 QDECREF(qstr);
714 char *object_property_get_str(Object *obj, const char *name,
715 Error **errp)
717 QObject *ret = object_property_get_qobject(obj, name, errp);
718 QString *qstring;
719 char *retval;
721 if (!ret) {
722 return NULL;
724 qstring = qobject_to_qstring(ret);
725 if (!qstring) {
726 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
727 retval = NULL;
728 } else {
729 retval = g_strdup(qstring_get_str(qstring));
732 QDECREF(qstring);
733 return retval;
736 void object_property_set_link(Object *obj, Object *value,
737 const char *name, Error **errp)
739 object_property_set_str(obj, object_get_canonical_path(value),
740 name, errp);
743 Object *object_property_get_link(Object *obj, const char *name,
744 Error **errp)
746 char *str = object_property_get_str(obj, name, errp);
747 Object *target = NULL;
749 if (str && *str) {
750 target = object_resolve_path(str, NULL);
751 if (!target) {
752 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
756 g_free(str);
757 return target;
760 void object_property_set_bool(Object *obj, bool value,
761 const char *name, Error **errp)
763 QBool *qbool = qbool_from_int(value);
764 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
766 QDECREF(qbool);
769 bool object_property_get_bool(Object *obj, const char *name,
770 Error **errp)
772 QObject *ret = object_property_get_qobject(obj, name, errp);
773 QBool *qbool;
774 bool retval;
776 if (!ret) {
777 return false;
779 qbool = qobject_to_qbool(ret);
780 if (!qbool) {
781 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
782 retval = false;
783 } else {
784 retval = qbool_get_int(qbool);
787 QDECREF(qbool);
788 return retval;
791 void object_property_set_int(Object *obj, int64_t value,
792 const char *name, Error **errp)
794 QInt *qint = qint_from_int(value);
795 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
797 QDECREF(qint);
800 int64_t object_property_get_int(Object *obj, const char *name,
801 Error **errp)
803 QObject *ret = object_property_get_qobject(obj, name, errp);
804 QInt *qint;
805 int64_t retval;
807 if (!ret) {
808 return -1;
810 qint = qobject_to_qint(ret);
811 if (!qint) {
812 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
813 retval = -1;
814 } else {
815 retval = qint_get_int(qint);
818 QDECREF(qint);
819 return retval;
822 void object_property_parse(Object *obj, const char *string,
823 const char *name, Error **errp)
825 StringInputVisitor *mi;
826 mi = string_input_visitor_new(string);
827 object_property_set(obj, string_input_get_visitor(mi), name, errp);
829 string_input_visitor_cleanup(mi);
832 char *object_property_print(Object *obj, const char *name,
833 Error **errp)
835 StringOutputVisitor *mo;
836 char *string;
838 mo = string_output_visitor_new();
839 object_property_get(obj, string_output_get_visitor(mo), name, errp);
840 string = string_output_get_string(mo);
841 string_output_visitor_cleanup(mo);
842 return string;
845 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
847 ObjectProperty *prop = object_property_find(obj, name, errp);
848 if (prop == NULL) {
849 return NULL;
852 return prop->type;
855 Object *object_get_root(void)
857 static Object *root;
859 if (!root) {
860 root = object_new("container");
863 return root;
866 static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
867 const char *name, Error **errp)
869 Object *child = opaque;
870 gchar *path;
872 path = object_get_canonical_path(child);
873 visit_type_str(v, &path, name, errp);
874 g_free(path);
877 static void object_finalize_child_property(Object *obj, const char *name,
878 void *opaque)
880 Object *child = opaque;
882 object_unref(child);
885 void object_property_add_child(Object *obj, const char *name,
886 Object *child, Error **errp)
888 gchar *type;
890 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
892 object_property_add(obj, name, type, object_get_child_property,
893 NULL, object_finalize_child_property, child, errp);
895 object_ref(child);
896 g_assert(child->parent == NULL);
897 child->parent = obj;
899 g_free(type);
902 static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
903 const char *name, Error **errp)
905 Object **child = opaque;
906 gchar *path;
908 if (*child) {
909 path = object_get_canonical_path(*child);
910 visit_type_str(v, &path, name, errp);
911 g_free(path);
912 } else {
913 path = (gchar *)"";
914 visit_type_str(v, &path, name, errp);
918 static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
919 const char *name, Error **errp)
921 Object **child = opaque;
922 Object *old_target;
923 bool ambiguous = false;
924 const char *type;
925 char *path;
926 gchar *target_type;
928 type = object_property_get_type(obj, name, NULL);
930 visit_type_str(v, &path, name, errp);
932 old_target = *child;
933 *child = NULL;
935 if (strcmp(path, "") != 0) {
936 Object *target;
938 /* Go from link<FOO> to FOO. */
939 target_type = g_strndup(&type[5], strlen(type) - 6);
940 target = object_resolve_path_type(path, target_type, &ambiguous);
942 if (ambiguous) {
943 error_set(errp, QERR_AMBIGUOUS_PATH, path);
944 } else if (target) {
945 object_ref(target);
946 *child = target;
947 } else {
948 target = object_resolve_path(path, &ambiguous);
949 if (target || ambiguous) {
950 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
951 } else {
952 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
955 g_free(target_type);
958 g_free(path);
960 if (old_target != NULL) {
961 object_unref(old_target);
965 void object_property_add_link(Object *obj, const char *name,
966 const char *type, Object **child,
967 Error **errp)
969 gchar *full_type;
971 full_type = g_strdup_printf("link<%s>", type);
973 object_property_add(obj, name, full_type,
974 object_get_link_property,
975 object_set_link_property,
976 NULL, child, errp);
978 g_free(full_type);
981 gchar *object_get_canonical_path(Object *obj)
983 Object *root = object_get_root();
984 char *newpath = NULL, *path = NULL;
986 while (obj != root) {
987 ObjectProperty *prop = NULL;
989 g_assert(obj->parent != NULL);
991 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
992 if (!object_property_is_child(prop)) {
993 continue;
996 if (prop->opaque == obj) {
997 if (path) {
998 newpath = g_strdup_printf("%s/%s", prop->name, path);
999 g_free(path);
1000 path = newpath;
1001 } else {
1002 path = g_strdup(prop->name);
1004 break;
1008 g_assert(prop != NULL);
1010 obj = obj->parent;
1013 newpath = g_strdup_printf("/%s", path);
1014 g_free(path);
1016 return newpath;
1019 Object *object_resolve_path_component(Object *parent, gchar *part)
1021 ObjectProperty *prop = object_property_find(parent, part, NULL);
1022 if (prop == NULL) {
1023 return NULL;
1026 if (object_property_is_link(prop)) {
1027 return *(Object **)prop->opaque;
1028 } else if (object_property_is_child(prop)) {
1029 return prop->opaque;
1030 } else {
1031 return NULL;
1035 static Object *object_resolve_abs_path(Object *parent,
1036 gchar **parts,
1037 const char *typename,
1038 int index)
1040 Object *child;
1042 if (parts[index] == NULL) {
1043 return object_dynamic_cast(parent, typename);
1046 if (strcmp(parts[index], "") == 0) {
1047 return object_resolve_abs_path(parent, parts, typename, index + 1);
1050 child = object_resolve_path_component(parent, parts[index]);
1051 if (!child) {
1052 return NULL;
1055 return object_resolve_abs_path(child, parts, typename, index + 1);
1058 static Object *object_resolve_partial_path(Object *parent,
1059 gchar **parts,
1060 const char *typename,
1061 bool *ambiguous)
1063 Object *obj;
1064 ObjectProperty *prop;
1066 obj = object_resolve_abs_path(parent, parts, typename, 0);
1068 QTAILQ_FOREACH(prop, &parent->properties, node) {
1069 Object *found;
1071 if (!object_property_is_child(prop)) {
1072 continue;
1075 found = object_resolve_partial_path(prop->opaque, parts,
1076 typename, ambiguous);
1077 if (found) {
1078 if (obj) {
1079 if (ambiguous) {
1080 *ambiguous = true;
1082 return NULL;
1084 obj = found;
1087 if (ambiguous && *ambiguous) {
1088 return NULL;
1092 return obj;
1095 Object *object_resolve_path_type(const char *path, const char *typename,
1096 bool *ambiguous)
1098 bool partial_path = true;
1099 Object *obj;
1100 gchar **parts;
1102 parts = g_strsplit(path, "/", 0);
1103 if (parts == NULL || parts[0] == NULL) {
1104 g_strfreev(parts);
1105 return object_get_root();
1108 if (strcmp(parts[0], "") == 0) {
1109 partial_path = false;
1112 if (partial_path) {
1113 if (ambiguous) {
1114 *ambiguous = false;
1116 obj = object_resolve_partial_path(object_get_root(), parts,
1117 typename, ambiguous);
1118 } else {
1119 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1122 g_strfreev(parts);
1124 return obj;
1127 Object *object_resolve_path(const char *path, bool *ambiguous)
1129 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1132 typedef struct StringProperty
1134 char *(*get)(Object *, Error **);
1135 void (*set)(Object *, const char *, Error **);
1136 } StringProperty;
1138 static void property_get_str(Object *obj, Visitor *v, void *opaque,
1139 const char *name, Error **errp)
1141 StringProperty *prop = opaque;
1142 char *value;
1144 value = prop->get(obj, errp);
1145 if (value) {
1146 visit_type_str(v, &value, name, errp);
1147 g_free(value);
1151 static void property_set_str(Object *obj, Visitor *v, void *opaque,
1152 const char *name, Error **errp)
1154 StringProperty *prop = opaque;
1155 char *value;
1156 Error *local_err = NULL;
1158 visit_type_str(v, &value, name, &local_err);
1159 if (local_err) {
1160 error_propagate(errp, local_err);
1161 return;
1164 prop->set(obj, value, errp);
1165 g_free(value);
1168 static void property_release_str(Object *obj, const char *name,
1169 void *opaque)
1171 StringProperty *prop = opaque;
1172 g_free(prop);
1175 void object_property_add_str(Object *obj, const char *name,
1176 char *(*get)(Object *, Error **),
1177 void (*set)(Object *, const char *, Error **),
1178 Error **errp)
1180 StringProperty *prop = g_malloc0(sizeof(*prop));
1182 prop->get = get;
1183 prop->set = set;
1185 object_property_add(obj, name, "string",
1186 get ? property_get_str : NULL,
1187 set ? property_set_str : NULL,
1188 property_release_str,
1189 prop, errp);
1192 typedef struct BoolProperty
1194 bool (*get)(Object *, Error **);
1195 void (*set)(Object *, bool, Error **);
1196 } BoolProperty;
1198 static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1199 const char *name, Error **errp)
1201 BoolProperty *prop = opaque;
1202 bool value;
1204 value = prop->get(obj, errp);
1205 visit_type_bool(v, &value, name, errp);
1208 static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1209 const char *name, Error **errp)
1211 BoolProperty *prop = opaque;
1212 bool value;
1213 Error *local_err = NULL;
1215 visit_type_bool(v, &value, name, &local_err);
1216 if (local_err) {
1217 error_propagate(errp, local_err);
1218 return;
1221 prop->set(obj, value, errp);
1224 static void property_release_bool(Object *obj, const char *name,
1225 void *opaque)
1227 BoolProperty *prop = opaque;
1228 g_free(prop);
1231 void object_property_add_bool(Object *obj, const char *name,
1232 bool (*get)(Object *, Error **),
1233 void (*set)(Object *, bool, Error **),
1234 Error **errp)
1236 BoolProperty *prop = g_malloc0(sizeof(*prop));
1238 prop->get = get;
1239 prop->set = set;
1241 object_property_add(obj, name, "bool",
1242 get ? property_get_bool : NULL,
1243 set ? property_set_bool : NULL,
1244 property_release_bool,
1245 prop, errp);
1248 static char *qdev_get_type(Object *obj, Error **errp)
1250 return g_strdup(object_get_typename(obj));
1253 static void object_instance_init(Object *obj)
1255 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1258 static void register_types(void)
1260 static TypeInfo interface_info = {
1261 .name = TYPE_INTERFACE,
1262 .class_size = sizeof(InterfaceClass),
1263 .abstract = true,
1266 static TypeInfo object_info = {
1267 .name = TYPE_OBJECT,
1268 .instance_size = sizeof(Object),
1269 .instance_init = object_instance_init,
1270 .abstract = true,
1273 type_interface = type_register_internal(&interface_info);
1274 type_register_internal(&object_info);
1277 type_init(register_types)