Merge remote-tracking branch 'remotes/cleber/tags/python-next-pull-request' into...
[qemu/ar7.git] / qom / object.c
blob1812f792247d3b95359b26a6a2fe96742970149f
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/osdep.h"
14 #include "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/qapi-builtin-visit.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qmp/qjson.h"
26 #include "trace.h"
28 /* TODO: replace QObject with a simpler visitor to avoid a dependency
29 * of the QOM core on QObject? */
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qemu/error-report.h"
36 #define MAX_INTERFACES 32
38 typedef struct InterfaceImpl InterfaceImpl;
39 typedef struct TypeImpl TypeImpl;
41 struct InterfaceImpl
43 const char *typename;
46 struct TypeImpl
48 const char *name;
50 size_t class_size;
52 size_t instance_size;
54 void (*class_init)(ObjectClass *klass, void *data);
55 void (*class_base_init)(ObjectClass *klass, void *data);
57 void *class_data;
59 void (*instance_init)(Object *obj);
60 void (*instance_post_init)(Object *obj);
61 void (*instance_finalize)(Object *obj);
63 bool abstract;
65 const char *parent;
66 TypeImpl *parent_type;
68 ObjectClass *class;
70 int num_interfaces;
71 InterfaceImpl interfaces[MAX_INTERFACES];
74 static Type type_interface;
76 static GHashTable *type_table_get(void)
78 static GHashTable *type_table;
80 if (type_table == NULL) {
81 type_table = g_hash_table_new(g_str_hash, g_str_equal);
84 return type_table;
87 static bool enumerating_types;
89 static void type_table_add(TypeImpl *ti)
91 assert(!enumerating_types);
92 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
95 static TypeImpl *type_table_lookup(const char *name)
97 return g_hash_table_lookup(type_table_get(), name);
100 static TypeImpl *type_new(const TypeInfo *info)
102 TypeImpl *ti = g_malloc0(sizeof(*ti));
103 int i;
105 g_assert(info->name != NULL);
107 if (type_table_lookup(info->name) != NULL) {
108 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
109 abort();
112 ti->name = g_strdup(info->name);
113 ti->parent = g_strdup(info->parent);
115 ti->class_size = info->class_size;
116 ti->instance_size = info->instance_size;
118 ti->class_init = info->class_init;
119 ti->class_base_init = info->class_base_init;
120 ti->class_data = info->class_data;
122 ti->instance_init = info->instance_init;
123 ti->instance_post_init = info->instance_post_init;
124 ti->instance_finalize = info->instance_finalize;
126 ti->abstract = info->abstract;
128 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
129 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
131 ti->num_interfaces = i;
133 return ti;
136 static TypeImpl *type_register_internal(const TypeInfo *info)
138 TypeImpl *ti;
139 ti = type_new(info);
141 type_table_add(ti);
142 return ti;
145 TypeImpl *type_register(const TypeInfo *info)
147 assert(info->parent);
148 return type_register_internal(info);
151 TypeImpl *type_register_static(const TypeInfo *info)
153 return type_register(info);
156 void type_register_static_array(const TypeInfo *infos, int nr_infos)
158 int i;
160 for (i = 0; i < nr_infos; i++) {
161 type_register_static(&infos[i]);
165 static TypeImpl *type_get_by_name(const char *name)
167 if (name == NULL) {
168 return NULL;
171 return type_table_lookup(name);
174 static TypeImpl *type_get_parent(TypeImpl *type)
176 if (!type->parent_type && type->parent) {
177 type->parent_type = type_get_by_name(type->parent);
178 if (!type->parent_type) {
179 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
180 type->name, type->parent);
181 abort();
185 return type->parent_type;
188 static bool type_has_parent(TypeImpl *type)
190 return (type->parent != NULL);
193 static size_t type_class_get_size(TypeImpl *ti)
195 if (ti->class_size) {
196 return ti->class_size;
199 if (type_has_parent(ti)) {
200 return type_class_get_size(type_get_parent(ti));
203 return sizeof(ObjectClass);
206 static size_t type_object_get_size(TypeImpl *ti)
208 if (ti->instance_size) {
209 return ti->instance_size;
212 if (type_has_parent(ti)) {
213 return type_object_get_size(type_get_parent(ti));
216 return 0;
219 size_t object_type_get_instance_size(const char *typename)
221 TypeImpl *type = type_get_by_name(typename);
223 g_assert(type != NULL);
224 return type_object_get_size(type);
227 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
229 assert(target_type);
231 /* Check if target_type is a direct ancestor of type */
232 while (type) {
233 if (type == target_type) {
234 return true;
237 type = type_get_parent(type);
240 return false;
243 static void type_initialize(TypeImpl *ti);
245 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
246 TypeImpl *parent_type)
248 InterfaceClass *new_iface;
249 TypeInfo info = { };
250 TypeImpl *iface_impl;
252 info.parent = parent_type->name;
253 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
254 info.abstract = true;
256 iface_impl = type_new(&info);
257 iface_impl->parent_type = parent_type;
258 type_initialize(iface_impl);
259 g_free((char *)info.name);
261 new_iface = (InterfaceClass *)iface_impl->class;
262 new_iface->concrete_class = ti->class;
263 new_iface->interface_type = interface_type;
265 ti->class->interfaces = g_slist_append(ti->class->interfaces,
266 iface_impl->class);
269 static void object_property_free(gpointer data)
271 ObjectProperty *prop = data;
273 if (prop->defval) {
274 qobject_unref(prop->defval);
275 prop->defval = NULL;
277 g_free(prop->name);
278 g_free(prop->type);
279 g_free(prop->description);
280 g_free(prop);
283 static void type_initialize(TypeImpl *ti)
285 TypeImpl *parent;
287 if (ti->class) {
288 return;
291 ti->class_size = type_class_get_size(ti);
292 ti->instance_size = type_object_get_size(ti);
293 /* Any type with zero instance_size is implicitly abstract.
294 * This means interface types are all abstract.
296 if (ti->instance_size == 0) {
297 ti->abstract = true;
299 if (type_is_ancestor(ti, type_interface)) {
300 assert(ti->instance_size == 0);
301 assert(ti->abstract);
302 assert(!ti->instance_init);
303 assert(!ti->instance_post_init);
304 assert(!ti->instance_finalize);
305 assert(!ti->num_interfaces);
307 ti->class = g_malloc0(ti->class_size);
309 parent = type_get_parent(ti);
310 if (parent) {
311 type_initialize(parent);
312 GSList *e;
313 int i;
315 g_assert(parent->class_size <= ti->class_size);
316 g_assert(parent->instance_size <= ti->instance_size);
317 memcpy(ti->class, parent->class, parent->class_size);
318 ti->class->interfaces = NULL;
319 ti->class->properties = g_hash_table_new_full(
320 g_str_hash, g_str_equal, NULL, object_property_free);
322 for (e = parent->class->interfaces; e; e = e->next) {
323 InterfaceClass *iface = e->data;
324 ObjectClass *klass = OBJECT_CLASS(iface);
326 type_initialize_interface(ti, iface->interface_type, klass->type);
329 for (i = 0; i < ti->num_interfaces; i++) {
330 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
331 if (!t) {
332 error_report("missing interface '%s' for object '%s'",
333 ti->interfaces[i].typename, parent->name);
334 abort();
336 for (e = ti->class->interfaces; e; e = e->next) {
337 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
339 if (type_is_ancestor(target_type, t)) {
340 break;
344 if (e) {
345 continue;
348 type_initialize_interface(ti, t, t);
350 } else {
351 ti->class->properties = g_hash_table_new_full(
352 g_str_hash, g_str_equal, NULL, object_property_free);
355 ti->class->type = ti;
357 while (parent) {
358 if (parent->class_base_init) {
359 parent->class_base_init(ti->class, ti->class_data);
361 parent = type_get_parent(parent);
364 if (ti->class_init) {
365 ti->class_init(ti->class, ti->class_data);
369 static void object_init_with_type(Object *obj, TypeImpl *ti)
371 if (type_has_parent(ti)) {
372 object_init_with_type(obj, type_get_parent(ti));
375 if (ti->instance_init) {
376 ti->instance_init(obj);
380 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
382 if (ti->instance_post_init) {
383 ti->instance_post_init(obj);
386 if (type_has_parent(ti)) {
387 object_post_init_with_type(obj, type_get_parent(ti));
391 void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp)
393 int i;
395 if (!props) {
396 return;
399 for (i = 0; i < props->len; i++) {
400 GlobalProperty *p = g_ptr_array_index(props, i);
401 Error *err = NULL;
403 if (object_dynamic_cast(obj, p->driver) == NULL) {
404 continue;
406 if (p->optional && !object_property_find(obj, p->property, NULL)) {
407 continue;
409 p->used = true;
410 object_property_parse(obj, p->value, p->property, &err);
411 if (err != NULL) {
412 error_prepend(&err, "can't apply global %s.%s=%s: ",
413 p->driver, p->property, p->value);
415 * If errp != NULL, propagate error and return.
416 * If errp == NULL, report a warning, but keep going
417 * with the remaining globals.
419 if (errp) {
420 error_propagate(errp, err);
421 return;
422 } else {
423 warn_report_err(err);
430 * Global property defaults
431 * Slot 0: accelerator's global property defaults
432 * Slot 1: machine's global property defaults
433 * Slot 2: global properties from legacy command line option
434 * Each is a GPtrArray of of GlobalProperty.
435 * Applied in order, later entries override earlier ones.
437 static GPtrArray *object_compat_props[3];
440 * Retrieve @GPtrArray for global property defined with options
441 * other than "-global". These are generally used for syntactic
442 * sugar and legacy command line options.
444 void object_register_sugar_prop(const char *driver, const char *prop, const char *value)
446 GlobalProperty *g;
447 if (!object_compat_props[2]) {
448 object_compat_props[2] = g_ptr_array_new();
450 g = g_new0(GlobalProperty, 1);
451 g->driver = g_strdup(driver);
452 g->property = g_strdup(prop);
453 g->value = g_strdup(value);
454 g_ptr_array_add(object_compat_props[2], g);
458 * Set machine's global property defaults to @compat_props.
459 * May be called at most once.
461 void object_set_machine_compat_props(GPtrArray *compat_props)
463 assert(!object_compat_props[1]);
464 object_compat_props[1] = compat_props;
468 * Set accelerator's global property defaults to @compat_props.
469 * May be called at most once.
471 void object_set_accelerator_compat_props(GPtrArray *compat_props)
473 assert(!object_compat_props[0]);
474 object_compat_props[0] = compat_props;
477 void object_apply_compat_props(Object *obj)
479 int i;
481 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
482 object_apply_global_props(obj, object_compat_props[i],
483 i == 2 ? &error_fatal : &error_abort);
487 static void object_class_property_init_all(Object *obj)
489 ObjectPropertyIterator iter;
490 ObjectProperty *prop;
492 object_class_property_iter_init(&iter, object_get_class(obj));
493 while ((prop = object_property_iter_next(&iter))) {
494 if (prop->init) {
495 prop->init(obj, prop);
500 static void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
502 Object *obj = data;
504 type_initialize(type);
506 g_assert(type->instance_size >= sizeof(Object));
507 g_assert(type->abstract == false);
508 g_assert(size >= type->instance_size);
510 memset(obj, 0, type->instance_size);
511 obj->class = type->class;
512 object_ref(obj);
513 object_class_property_init_all(obj);
514 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
515 NULL, object_property_free);
516 object_init_with_type(obj, type);
517 object_post_init_with_type(obj, type);
520 void object_initialize(void *data, size_t size, const char *typename)
522 TypeImpl *type = type_get_by_name(typename);
524 if (!type) {
525 error_report("missing object type '%s'", typename);
526 abort();
529 object_initialize_with_type(data, size, type);
532 void object_initialize_child(Object *parentobj, const char *propname,
533 void *childobj, size_t size, const char *type,
534 Error **errp, ...)
536 va_list vargs;
538 va_start(vargs, errp);
539 object_initialize_childv(parentobj, propname, childobj, size, type, errp,
540 vargs);
541 va_end(vargs);
544 void object_initialize_childv(Object *parentobj, const char *propname,
545 void *childobj, size_t size, const char *type,
546 Error **errp, va_list vargs)
548 Error *local_err = NULL;
549 Object *obj;
550 UserCreatable *uc;
552 object_initialize(childobj, size, type);
553 obj = OBJECT(childobj);
555 object_set_propv(obj, &local_err, vargs);
556 if (local_err) {
557 goto out;
560 object_property_add_child(parentobj, propname, obj, &local_err);
561 if (local_err) {
562 goto out;
565 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
566 if (uc) {
567 user_creatable_complete(uc, &local_err);
568 if (local_err) {
569 object_unparent(obj);
570 goto out;
575 * Since object_property_add_child added a reference to the child object,
576 * we can drop the reference added by object_initialize(), so the child
577 * property will own the only reference to the object.
579 object_unref(obj);
581 out:
582 if (local_err) {
583 error_propagate(errp, local_err);
584 object_unref(obj);
588 static inline bool object_property_is_child(ObjectProperty *prop)
590 return strstart(prop->type, "child<", NULL);
593 static void object_property_del_all(Object *obj)
595 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
596 ObjectProperty *prop;
597 ObjectPropertyIterator iter;
598 bool released;
600 do {
601 released = false;
602 object_property_iter_init(&iter, obj);
603 while ((prop = object_property_iter_next(&iter)) != NULL) {
604 if (g_hash_table_add(done, prop)) {
605 if (prop->release) {
606 prop->release(obj, prop->name, prop->opaque);
607 released = true;
608 break;
612 } while (released);
614 g_hash_table_unref(obj->properties);
617 static void object_property_del_child(Object *obj, Object *child, Error **errp)
619 ObjectProperty *prop;
620 GHashTableIter iter;
621 gpointer key, value;
623 g_hash_table_iter_init(&iter, obj->properties);
624 while (g_hash_table_iter_next(&iter, &key, &value)) {
625 prop = value;
626 if (object_property_is_child(prop) && prop->opaque == child) {
627 if (prop->release) {
628 prop->release(obj, prop->name, prop->opaque);
629 prop->release = NULL;
631 break;
634 g_hash_table_iter_init(&iter, obj->properties);
635 while (g_hash_table_iter_next(&iter, &key, &value)) {
636 prop = value;
637 if (object_property_is_child(prop) && prop->opaque == child) {
638 g_hash_table_iter_remove(&iter);
639 break;
644 void object_unparent(Object *obj)
646 if (obj->parent) {
647 object_property_del_child(obj->parent, obj, NULL);
651 static void object_deinit(Object *obj, TypeImpl *type)
653 if (type->instance_finalize) {
654 type->instance_finalize(obj);
657 if (type_has_parent(type)) {
658 object_deinit(obj, type_get_parent(type));
662 static void object_finalize(void *data)
664 Object *obj = data;
665 TypeImpl *ti = obj->class->type;
667 object_property_del_all(obj);
668 object_deinit(obj, ti);
670 g_assert(obj->ref == 0);
671 if (obj->free) {
672 obj->free(obj);
676 static Object *object_new_with_type(Type type)
678 Object *obj;
680 g_assert(type != NULL);
681 type_initialize(type);
683 obj = g_malloc(type->instance_size);
684 object_initialize_with_type(obj, type->instance_size, type);
685 obj->free = g_free;
687 return obj;
690 Object *object_new_with_class(ObjectClass *klass)
692 return object_new_with_type(klass->type);
695 Object *object_new(const char *typename)
697 TypeImpl *ti = type_get_by_name(typename);
699 return object_new_with_type(ti);
703 Object *object_new_with_props(const char *typename,
704 Object *parent,
705 const char *id,
706 Error **errp,
707 ...)
709 va_list vargs;
710 Object *obj;
712 va_start(vargs, errp);
713 obj = object_new_with_propv(typename, parent, id, errp, vargs);
714 va_end(vargs);
716 return obj;
720 Object *object_new_with_propv(const char *typename,
721 Object *parent,
722 const char *id,
723 Error **errp,
724 va_list vargs)
726 Object *obj;
727 ObjectClass *klass;
728 Error *local_err = NULL;
729 UserCreatable *uc;
731 klass = object_class_by_name(typename);
732 if (!klass) {
733 error_setg(errp, "invalid object type: %s", typename);
734 return NULL;
737 if (object_class_is_abstract(klass)) {
738 error_setg(errp, "object type '%s' is abstract", typename);
739 return NULL;
741 obj = object_new_with_type(klass->type);
743 if (object_set_propv(obj, &local_err, vargs) < 0) {
744 goto error;
747 if (id != NULL) {
748 object_property_add_child(parent, id, obj, &local_err);
749 if (local_err) {
750 goto error;
754 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
755 if (uc) {
756 user_creatable_complete(uc, &local_err);
757 if (local_err) {
758 if (id != NULL) {
759 object_unparent(obj);
761 goto error;
765 object_unref(OBJECT(obj));
766 return obj;
768 error:
769 error_propagate(errp, local_err);
770 object_unref(obj);
771 return NULL;
775 int object_set_props(Object *obj,
776 Error **errp,
777 ...)
779 va_list vargs;
780 int ret;
782 va_start(vargs, errp);
783 ret = object_set_propv(obj, errp, vargs);
784 va_end(vargs);
786 return ret;
790 int object_set_propv(Object *obj,
791 Error **errp,
792 va_list vargs)
794 const char *propname;
795 Error *local_err = NULL;
797 propname = va_arg(vargs, char *);
798 while (propname != NULL) {
799 const char *value = va_arg(vargs, char *);
801 g_assert(value != NULL);
802 object_property_parse(obj, value, propname, &local_err);
803 if (local_err) {
804 error_propagate(errp, local_err);
805 return -1;
807 propname = va_arg(vargs, char *);
810 return 0;
814 Object *object_dynamic_cast(Object *obj, const char *typename)
816 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
817 return obj;
820 return NULL;
823 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
824 const char *file, int line, const char *func)
826 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
827 typename, file, line, func);
829 #ifdef CONFIG_QOM_CAST_DEBUG
830 int i;
831 Object *inst;
833 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
834 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
835 goto out;
839 inst = object_dynamic_cast(obj, typename);
841 if (!inst && obj) {
842 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
843 file, line, func, obj, typename);
844 abort();
847 assert(obj == inst);
849 if (obj && obj == inst) {
850 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
851 atomic_set(&obj->class->object_cast_cache[i - 1],
852 atomic_read(&obj->class->object_cast_cache[i]));
854 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
857 out:
858 #endif
859 return obj;
862 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
863 const char *typename)
865 ObjectClass *ret = NULL;
866 TypeImpl *target_type;
867 TypeImpl *type;
869 if (!class) {
870 return NULL;
873 /* A simple fast path that can trigger a lot for leaf classes. */
874 type = class->type;
875 if (type->name == typename) {
876 return class;
879 target_type = type_get_by_name(typename);
880 if (!target_type) {
881 /* target class type unknown, so fail the cast */
882 return NULL;
885 if (type->class->interfaces &&
886 type_is_ancestor(target_type, type_interface)) {
887 int found = 0;
888 GSList *i;
890 for (i = class->interfaces; i; i = i->next) {
891 ObjectClass *target_class = i->data;
893 if (type_is_ancestor(target_class->type, target_type)) {
894 ret = target_class;
895 found++;
899 /* The match was ambiguous, don't allow a cast */
900 if (found > 1) {
901 ret = NULL;
903 } else if (type_is_ancestor(type, target_type)) {
904 ret = class;
907 return ret;
910 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
911 const char *typename,
912 const char *file, int line,
913 const char *func)
915 ObjectClass *ret;
917 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
918 typename, file, line, func);
920 #ifdef CONFIG_QOM_CAST_DEBUG
921 int i;
923 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
924 if (atomic_read(&class->class_cast_cache[i]) == typename) {
925 ret = class;
926 goto out;
929 #else
930 if (!class || !class->interfaces) {
931 return class;
933 #endif
935 ret = object_class_dynamic_cast(class, typename);
936 if (!ret && class) {
937 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
938 file, line, func, class, typename);
939 abort();
942 #ifdef CONFIG_QOM_CAST_DEBUG
943 if (class && ret == class) {
944 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
945 atomic_set(&class->class_cast_cache[i - 1],
946 atomic_read(&class->class_cast_cache[i]));
948 atomic_set(&class->class_cast_cache[i - 1], typename);
950 out:
951 #endif
952 return ret;
955 const char *object_get_typename(const Object *obj)
957 return obj->class->type->name;
960 ObjectClass *object_get_class(Object *obj)
962 return obj->class;
965 bool object_class_is_abstract(ObjectClass *klass)
967 return klass->type->abstract;
970 const char *object_class_get_name(ObjectClass *klass)
972 return klass->type->name;
975 ObjectClass *object_class_by_name(const char *typename)
977 TypeImpl *type = type_get_by_name(typename);
979 if (!type) {
980 return NULL;
983 type_initialize(type);
985 return type->class;
988 ObjectClass *object_class_get_parent(ObjectClass *class)
990 TypeImpl *type = type_get_parent(class->type);
992 if (!type) {
993 return NULL;
996 type_initialize(type);
998 return type->class;
1001 typedef struct OCFData
1003 void (*fn)(ObjectClass *klass, void *opaque);
1004 const char *implements_type;
1005 bool include_abstract;
1006 void *opaque;
1007 } OCFData;
1009 static void object_class_foreach_tramp(gpointer key, gpointer value,
1010 gpointer opaque)
1012 OCFData *data = opaque;
1013 TypeImpl *type = value;
1014 ObjectClass *k;
1016 type_initialize(type);
1017 k = type->class;
1019 if (!data->include_abstract && type->abstract) {
1020 return;
1023 if (data->implements_type &&
1024 !object_class_dynamic_cast(k, data->implements_type)) {
1025 return;
1028 data->fn(k, data->opaque);
1031 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1032 const char *implements_type, bool include_abstract,
1033 void *opaque)
1035 OCFData data = { fn, implements_type, include_abstract, opaque };
1037 enumerating_types = true;
1038 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1039 enumerating_types = false;
1042 static int do_object_child_foreach(Object *obj,
1043 int (*fn)(Object *child, void *opaque),
1044 void *opaque, bool recurse)
1046 GHashTableIter iter;
1047 ObjectProperty *prop;
1048 int ret = 0;
1050 g_hash_table_iter_init(&iter, obj->properties);
1051 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1052 if (object_property_is_child(prop)) {
1053 Object *child = prop->opaque;
1055 ret = fn(child, opaque);
1056 if (ret != 0) {
1057 break;
1059 if (recurse) {
1060 do_object_child_foreach(child, fn, opaque, true);
1064 return ret;
1067 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1068 void *opaque)
1070 return do_object_child_foreach(obj, fn, opaque, false);
1073 int object_child_foreach_recursive(Object *obj,
1074 int (*fn)(Object *child, void *opaque),
1075 void *opaque)
1077 return do_object_child_foreach(obj, fn, opaque, true);
1080 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1082 GSList **list = opaque;
1084 *list = g_slist_prepend(*list, klass);
1087 GSList *object_class_get_list(const char *implements_type,
1088 bool include_abstract)
1090 GSList *list = NULL;
1092 object_class_foreach(object_class_get_list_tramp,
1093 implements_type, include_abstract, &list);
1094 return list;
1097 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1099 return strcasecmp(object_class_get_name((ObjectClass *)a),
1100 object_class_get_name((ObjectClass *)b));
1103 GSList *object_class_get_list_sorted(const char *implements_type,
1104 bool include_abstract)
1106 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1107 object_class_cmp);
1110 Object *object_ref(Object *obj)
1112 if (!obj) {
1113 return NULL;
1115 atomic_inc(&obj->ref);
1116 return obj;
1119 void object_unref(Object *obj)
1121 if (!obj) {
1122 return;
1124 g_assert(obj->ref > 0);
1126 /* parent always holds a reference to its children */
1127 if (atomic_fetch_dec(&obj->ref) == 1) {
1128 object_finalize(obj);
1132 ObjectProperty *
1133 object_property_add(Object *obj, const char *name, const char *type,
1134 ObjectPropertyAccessor *get,
1135 ObjectPropertyAccessor *set,
1136 ObjectPropertyRelease *release,
1137 void *opaque, Error **errp)
1139 ObjectProperty *prop;
1140 size_t name_len = strlen(name);
1142 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1143 int i;
1144 ObjectProperty *ret;
1145 char *name_no_array = g_strdup(name);
1147 name_no_array[name_len - 3] = '\0';
1148 for (i = 0; ; ++i) {
1149 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1151 ret = object_property_add(obj, full_name, type, get, set,
1152 release, opaque, NULL);
1153 g_free(full_name);
1154 if (ret) {
1155 break;
1158 g_free(name_no_array);
1159 return ret;
1162 if (object_property_find(obj, name, NULL) != NULL) {
1163 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1164 name, object_get_typename(obj));
1165 return NULL;
1168 prop = g_malloc0(sizeof(*prop));
1170 prop->name = g_strdup(name);
1171 prop->type = g_strdup(type);
1173 prop->get = get;
1174 prop->set = set;
1175 prop->release = release;
1176 prop->opaque = opaque;
1178 g_hash_table_insert(obj->properties, prop->name, prop);
1179 return prop;
1182 ObjectProperty *
1183 object_class_property_add(ObjectClass *klass,
1184 const char *name,
1185 const char *type,
1186 ObjectPropertyAccessor *get,
1187 ObjectPropertyAccessor *set,
1188 ObjectPropertyRelease *release,
1189 void *opaque,
1190 Error **errp)
1192 ObjectProperty *prop;
1194 if (object_class_property_find(klass, name, NULL) != NULL) {
1195 error_setg(errp, "attempt to add duplicate property '%s' to class (type '%s')",
1196 name, object_class_get_name(klass));
1197 return NULL;
1200 prop = g_malloc0(sizeof(*prop));
1202 prop->name = g_strdup(name);
1203 prop->type = g_strdup(type);
1205 prop->get = get;
1206 prop->set = set;
1207 prop->release = release;
1208 prop->opaque = opaque;
1210 g_hash_table_insert(klass->properties, prop->name, prop);
1212 return prop;
1215 ObjectProperty *object_property_find(Object *obj, const char *name,
1216 Error **errp)
1218 ObjectProperty *prop;
1219 ObjectClass *klass = object_get_class(obj);
1221 prop = object_class_property_find(klass, name, NULL);
1222 if (prop) {
1223 return prop;
1226 prop = g_hash_table_lookup(obj->properties, name);
1227 if (prop) {
1228 return prop;
1231 error_setg(errp, "Property '.%s' not found", name);
1232 return NULL;
1235 void object_property_iter_init(ObjectPropertyIterator *iter,
1236 Object *obj)
1238 g_hash_table_iter_init(&iter->iter, obj->properties);
1239 iter->nextclass = object_get_class(obj);
1242 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1244 gpointer key, val;
1245 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1246 if (!iter->nextclass) {
1247 return NULL;
1249 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1250 iter->nextclass = object_class_get_parent(iter->nextclass);
1252 return val;
1255 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1256 ObjectClass *klass)
1258 g_hash_table_iter_init(&iter->iter, klass->properties);
1259 iter->nextclass = object_class_get_parent(klass);
1262 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1263 Error **errp)
1265 ObjectProperty *prop;
1266 ObjectClass *parent_klass;
1268 parent_klass = object_class_get_parent(klass);
1269 if (parent_klass) {
1270 prop = object_class_property_find(parent_klass, name, NULL);
1271 if (prop) {
1272 return prop;
1276 prop = g_hash_table_lookup(klass->properties, name);
1277 if (!prop) {
1278 error_setg(errp, "Property '.%s' not found", name);
1280 return prop;
1283 void object_property_del(Object *obj, const char *name, Error **errp)
1285 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1287 if (!prop) {
1288 error_setg(errp, "Property '.%s' not found", name);
1289 return;
1292 if (prop->release) {
1293 prop->release(obj, name, prop->opaque);
1295 g_hash_table_remove(obj->properties, name);
1298 void object_property_get(Object *obj, Visitor *v, const char *name,
1299 Error **errp)
1301 ObjectProperty *prop = object_property_find(obj, name, errp);
1302 if (prop == NULL) {
1303 return;
1306 if (!prop->get) {
1307 error_setg(errp, QERR_PERMISSION_DENIED);
1308 } else {
1309 prop->get(obj, v, name, prop->opaque, errp);
1313 void object_property_set(Object *obj, Visitor *v, const char *name,
1314 Error **errp)
1316 ObjectProperty *prop = object_property_find(obj, name, errp);
1317 if (prop == NULL) {
1318 return;
1321 if (!prop->set) {
1322 error_setg(errp, QERR_PERMISSION_DENIED);
1323 } else {
1324 prop->set(obj, v, name, prop->opaque, errp);
1328 void object_property_set_str(Object *obj, const char *value,
1329 const char *name, Error **errp)
1331 QString *qstr = qstring_from_str(value);
1332 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1334 qobject_unref(qstr);
1337 char *object_property_get_str(Object *obj, const char *name,
1338 Error **errp)
1340 QObject *ret = object_property_get_qobject(obj, name, errp);
1341 char *retval;
1343 if (!ret) {
1344 return NULL;
1347 retval = g_strdup(qobject_get_try_str(ret));
1348 if (!retval) {
1349 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1352 qobject_unref(ret);
1353 return retval;
1356 void object_property_set_link(Object *obj, Object *value,
1357 const char *name, Error **errp)
1359 if (value) {
1360 gchar *path = object_get_canonical_path(value);
1361 object_property_set_str(obj, path, name, errp);
1362 g_free(path);
1363 } else {
1364 object_property_set_str(obj, "", name, errp);
1368 Object *object_property_get_link(Object *obj, const char *name,
1369 Error **errp)
1371 char *str = object_property_get_str(obj, name, errp);
1372 Object *target = NULL;
1374 if (str && *str) {
1375 target = object_resolve_path(str, NULL);
1376 if (!target) {
1377 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1378 "Device '%s' not found", str);
1382 g_free(str);
1383 return target;
1386 void object_property_set_bool(Object *obj, bool value,
1387 const char *name, Error **errp)
1389 QBool *qbool = qbool_from_bool(value);
1390 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1392 qobject_unref(qbool);
1395 bool object_property_get_bool(Object *obj, const char *name,
1396 Error **errp)
1398 QObject *ret = object_property_get_qobject(obj, name, errp);
1399 QBool *qbool;
1400 bool retval;
1402 if (!ret) {
1403 return false;
1405 qbool = qobject_to(QBool, ret);
1406 if (!qbool) {
1407 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1408 retval = false;
1409 } else {
1410 retval = qbool_get_bool(qbool);
1413 qobject_unref(ret);
1414 return retval;
1417 void object_property_set_int(Object *obj, int64_t value,
1418 const char *name, Error **errp)
1420 QNum *qnum = qnum_from_int(value);
1421 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1423 qobject_unref(qnum);
1426 int64_t object_property_get_int(Object *obj, const char *name,
1427 Error **errp)
1429 QObject *ret = object_property_get_qobject(obj, name, errp);
1430 QNum *qnum;
1431 int64_t retval;
1433 if (!ret) {
1434 return -1;
1437 qnum = qobject_to(QNum, ret);
1438 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1439 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1440 retval = -1;
1443 qobject_unref(ret);
1444 return retval;
1447 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1449 Visitor *v = qobject_input_visitor_new(prop->defval);
1451 assert(prop->set != NULL);
1452 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1454 visit_free(v);
1457 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1459 assert(!prop->defval);
1460 assert(!prop->init);
1462 prop->defval = defval;
1463 prop->init = object_property_init_defval;
1466 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1468 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1471 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1473 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1476 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1478 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1481 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1483 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1486 void object_property_set_uint(Object *obj, uint64_t value,
1487 const char *name, Error **errp)
1489 QNum *qnum = qnum_from_uint(value);
1491 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1492 qobject_unref(qnum);
1495 uint64_t object_property_get_uint(Object *obj, const char *name,
1496 Error **errp)
1498 QObject *ret = object_property_get_qobject(obj, name, errp);
1499 QNum *qnum;
1500 uint64_t retval;
1502 if (!ret) {
1503 return 0;
1505 qnum = qobject_to(QNum, ret);
1506 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1507 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1508 retval = 0;
1511 qobject_unref(ret);
1512 return retval;
1515 typedef struct EnumProperty {
1516 const QEnumLookup *lookup;
1517 int (*get)(Object *, Error **);
1518 void (*set)(Object *, int, Error **);
1519 } EnumProperty;
1521 int object_property_get_enum(Object *obj, const char *name,
1522 const char *typename, Error **errp)
1524 Error *err = NULL;
1525 Visitor *v;
1526 char *str;
1527 int ret;
1528 ObjectProperty *prop = object_property_find(obj, name, errp);
1529 EnumProperty *enumprop;
1531 if (prop == NULL) {
1532 return 0;
1535 if (!g_str_equal(prop->type, typename)) {
1536 error_setg(errp, "Property %s on %s is not '%s' enum type",
1537 name, object_class_get_name(
1538 object_get_class(obj)), typename);
1539 return 0;
1542 enumprop = prop->opaque;
1544 v = string_output_visitor_new(false, &str);
1545 object_property_get(obj, v, name, &err);
1546 if (err) {
1547 error_propagate(errp, err);
1548 visit_free(v);
1549 return 0;
1551 visit_complete(v, &str);
1552 visit_free(v);
1553 v = string_input_visitor_new(str);
1554 visit_type_enum(v, name, &ret, enumprop->lookup, errp);
1556 g_free(str);
1557 visit_free(v);
1559 return ret;
1562 void object_property_get_uint16List(Object *obj, const char *name,
1563 uint16List **list, Error **errp)
1565 Error *err = NULL;
1566 Visitor *v;
1567 char *str;
1569 v = string_output_visitor_new(false, &str);
1570 object_property_get(obj, v, name, &err);
1571 if (err) {
1572 error_propagate(errp, err);
1573 goto out;
1575 visit_complete(v, &str);
1576 visit_free(v);
1577 v = string_input_visitor_new(str);
1578 visit_type_uint16List(v, NULL, list, errp);
1580 g_free(str);
1581 out:
1582 visit_free(v);
1585 void object_property_parse(Object *obj, const char *string,
1586 const char *name, Error **errp)
1588 Visitor *v = string_input_visitor_new(string);
1589 object_property_set(obj, v, name, errp);
1590 visit_free(v);
1593 char *object_property_print(Object *obj, const char *name, bool human,
1594 Error **errp)
1596 Visitor *v;
1597 char *string = NULL;
1598 Error *local_err = NULL;
1600 v = string_output_visitor_new(human, &string);
1601 object_property_get(obj, v, name, &local_err);
1602 if (local_err) {
1603 error_propagate(errp, local_err);
1604 goto out;
1607 visit_complete(v, &string);
1609 out:
1610 visit_free(v);
1611 return string;
1614 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1616 ObjectProperty *prop = object_property_find(obj, name, errp);
1617 if (prop == NULL) {
1618 return NULL;
1621 return prop->type;
1624 Object *object_get_root(void)
1626 static Object *root;
1628 if (!root) {
1629 root = object_new("container");
1632 return root;
1635 Object *object_get_objects_root(void)
1637 return container_get(object_get_root(), "/objects");
1640 Object *object_get_internal_root(void)
1642 static Object *internal_root;
1644 if (!internal_root) {
1645 internal_root = object_new("container");
1648 return internal_root;
1651 static void object_get_child_property(Object *obj, Visitor *v,
1652 const char *name, void *opaque,
1653 Error **errp)
1655 Object *child = opaque;
1656 gchar *path;
1658 path = object_get_canonical_path(child);
1659 visit_type_str(v, name, &path, errp);
1660 g_free(path);
1663 static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1665 return opaque;
1668 static void object_finalize_child_property(Object *obj, const char *name,
1669 void *opaque)
1671 Object *child = opaque;
1673 if (child->class->unparent) {
1674 (child->class->unparent)(child);
1676 child->parent = NULL;
1677 object_unref(child);
1680 void object_property_add_child(Object *obj, const char *name,
1681 Object *child, Error **errp)
1683 Error *local_err = NULL;
1684 gchar *type;
1685 ObjectProperty *op;
1687 if (child->parent != NULL) {
1688 error_setg(errp, "child object is already parented");
1689 return;
1692 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1694 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1695 object_finalize_child_property, child, &local_err);
1696 if (local_err) {
1697 error_propagate(errp, local_err);
1698 goto out;
1701 op->resolve = object_resolve_child_property;
1702 object_ref(child);
1703 child->parent = obj;
1705 out:
1706 g_free(type);
1709 void object_property_allow_set_link(const Object *obj, const char *name,
1710 Object *val, Error **errp)
1712 /* Allow the link to be set, always */
1715 typedef struct {
1716 union {
1717 Object **targetp;
1718 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1719 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1721 void (*check)(const Object *, const char *, Object *, Error **);
1722 ObjectPropertyLinkFlags flags;
1723 } LinkProperty;
1725 static Object **
1726 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1728 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1729 return &lprop->target;
1730 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1731 return (void *)obj + lprop->offset;
1732 } else {
1733 return lprop->targetp;
1737 static void object_get_link_property(Object *obj, Visitor *v,
1738 const char *name, void *opaque,
1739 Error **errp)
1741 LinkProperty *lprop = opaque;
1742 Object **targetp = object_link_get_targetp(obj, lprop);
1743 gchar *path;
1745 if (*targetp) {
1746 path = object_get_canonical_path(*targetp);
1747 visit_type_str(v, name, &path, errp);
1748 g_free(path);
1749 } else {
1750 path = (gchar *)"";
1751 visit_type_str(v, name, &path, errp);
1756 * object_resolve_link:
1758 * Lookup an object and ensure its type matches the link property type. This
1759 * is similar to object_resolve_path() except type verification against the
1760 * link property is performed.
1762 * Returns: The matched object or NULL on path lookup failures.
1764 static Object *object_resolve_link(Object *obj, const char *name,
1765 const char *path, Error **errp)
1767 const char *type;
1768 gchar *target_type;
1769 bool ambiguous = false;
1770 Object *target;
1772 /* Go from link<FOO> to FOO. */
1773 type = object_property_get_type(obj, name, NULL);
1774 target_type = g_strndup(&type[5], strlen(type) - 6);
1775 target = object_resolve_path_type(path, target_type, &ambiguous);
1777 if (ambiguous) {
1778 error_setg(errp, "Path '%s' does not uniquely identify an object",
1779 path);
1780 } else if (!target) {
1781 target = object_resolve_path(path, &ambiguous);
1782 if (target || ambiguous) {
1783 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1784 } else {
1785 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1786 "Device '%s' not found", path);
1788 target = NULL;
1790 g_free(target_type);
1792 return target;
1795 static void object_set_link_property(Object *obj, Visitor *v,
1796 const char *name, void *opaque,
1797 Error **errp)
1799 Error *local_err = NULL;
1800 LinkProperty *prop = opaque;
1801 Object **targetp = object_link_get_targetp(obj, prop);
1802 Object *old_target = *targetp;
1803 Object *new_target = NULL;
1804 char *path = NULL;
1806 visit_type_str(v, name, &path, &local_err);
1808 if (!local_err && strcmp(path, "") != 0) {
1809 new_target = object_resolve_link(obj, name, path, &local_err);
1812 g_free(path);
1813 if (local_err) {
1814 error_propagate(errp, local_err);
1815 return;
1818 prop->check(obj, name, new_target, &local_err);
1819 if (local_err) {
1820 error_propagate(errp, local_err);
1821 return;
1824 *targetp = new_target;
1825 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1826 object_ref(new_target);
1827 object_unref(old_target);
1831 static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1833 LinkProperty *lprop = opaque;
1835 return *object_link_get_targetp(parent, lprop);
1838 static void object_release_link_property(Object *obj, const char *name,
1839 void *opaque)
1841 LinkProperty *prop = opaque;
1842 Object **targetp = object_link_get_targetp(obj, prop);
1844 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1845 object_unref(*targetp);
1847 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1848 g_free(prop);
1852 static void object_add_link_prop(Object *obj, const char *name,
1853 const char *type, void *ptr,
1854 void (*check)(const Object *, const char *,
1855 Object *, Error **),
1856 ObjectPropertyLinkFlags flags,
1857 Error **errp)
1859 Error *local_err = NULL;
1860 LinkProperty *prop = g_malloc(sizeof(*prop));
1861 gchar *full_type;
1862 ObjectProperty *op;
1864 if (flags & OBJ_PROP_LINK_DIRECT) {
1865 prop->target = ptr;
1866 } else {
1867 prop->targetp = ptr;
1869 prop->check = check;
1870 prop->flags = flags;
1872 full_type = g_strdup_printf("link<%s>", type);
1874 op = object_property_add(obj, name, full_type,
1875 object_get_link_property,
1876 check ? object_set_link_property : NULL,
1877 object_release_link_property,
1878 prop,
1879 &local_err);
1880 if (local_err) {
1881 error_propagate(errp, local_err);
1882 g_free(prop);
1883 goto out;
1886 op->resolve = object_resolve_link_property;
1888 out:
1889 g_free(full_type);
1892 void object_property_add_link(Object *obj, const char *name,
1893 const char *type, Object **targetp,
1894 void (*check)(const Object *, const char *,
1895 Object *, Error **),
1896 ObjectPropertyLinkFlags flags,
1897 Error **errp)
1899 object_add_link_prop(obj, name, type, targetp, check, flags, errp);
1902 ObjectProperty *
1903 object_class_property_add_link(ObjectClass *oc,
1904 const char *name,
1905 const char *type, ptrdiff_t offset,
1906 void (*check)(const Object *obj, const char *name,
1907 Object *val, Error **errp),
1908 ObjectPropertyLinkFlags flags,
1909 Error **errp)
1911 Error *local_err = NULL;
1912 LinkProperty *prop = g_new0(LinkProperty, 1);
1913 gchar *full_type;
1914 ObjectProperty *op;
1916 prop->offset = offset;
1917 prop->check = check;
1918 prop->flags = flags | OBJ_PROP_LINK_CLASS;
1920 full_type = g_strdup_printf("link<%s>", type);
1922 op = object_class_property_add(oc, name, full_type,
1923 object_get_link_property,
1924 check ? object_set_link_property : NULL,
1925 object_release_link_property,
1926 prop,
1927 &local_err);
1928 if (local_err) {
1929 error_propagate(errp, local_err);
1930 g_free(prop);
1931 goto out;
1934 op->resolve = object_resolve_link_property;
1936 out:
1937 g_free(full_type);
1938 return op;
1941 void object_property_add_const_link(Object *obj, const char *name,
1942 Object *target, Error **errp)
1944 object_add_link_prop(obj, name, object_get_typename(target), target,
1945 NULL, OBJ_PROP_LINK_DIRECT, errp);
1948 gchar *object_get_canonical_path_component(Object *obj)
1950 ObjectProperty *prop = NULL;
1951 GHashTableIter iter;
1953 if (obj->parent == NULL) {
1954 return NULL;
1957 g_hash_table_iter_init(&iter, obj->parent->properties);
1958 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1959 if (!object_property_is_child(prop)) {
1960 continue;
1963 if (prop->opaque == obj) {
1964 return g_strdup(prop->name);
1968 /* obj had a parent but was not a child, should never happen */
1969 g_assert_not_reached();
1970 return NULL;
1973 gchar *object_get_canonical_path(Object *obj)
1975 Object *root = object_get_root();
1976 char *newpath, *path = NULL;
1978 if (obj == root) {
1979 return g_strdup("/");
1982 do {
1983 char *component = object_get_canonical_path_component(obj);
1985 if (!component) {
1986 /* A canonical path must be complete, so discard what was
1987 * collected so far.
1989 g_free(path);
1990 return NULL;
1993 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1994 g_free(path);
1995 g_free(component);
1996 path = newpath;
1997 obj = obj->parent;
1998 } while (obj != root);
2000 return path;
2003 Object *object_resolve_path_component(Object *parent, const gchar *part)
2005 ObjectProperty *prop = object_property_find(parent, part, NULL);
2006 if (prop == NULL) {
2007 return NULL;
2010 if (prop->resolve) {
2011 return prop->resolve(parent, prop->opaque, part);
2012 } else {
2013 return NULL;
2017 static Object *object_resolve_abs_path(Object *parent,
2018 gchar **parts,
2019 const char *typename,
2020 int index)
2022 Object *child;
2024 if (parts[index] == NULL) {
2025 return object_dynamic_cast(parent, typename);
2028 if (strcmp(parts[index], "") == 0) {
2029 return object_resolve_abs_path(parent, parts, typename, index + 1);
2032 child = object_resolve_path_component(parent, parts[index]);
2033 if (!child) {
2034 return NULL;
2037 return object_resolve_abs_path(child, parts, typename, index + 1);
2040 static Object *object_resolve_partial_path(Object *parent,
2041 gchar **parts,
2042 const char *typename,
2043 bool *ambiguous)
2045 Object *obj;
2046 GHashTableIter iter;
2047 ObjectProperty *prop;
2049 obj = object_resolve_abs_path(parent, parts, typename, 0);
2051 g_hash_table_iter_init(&iter, parent->properties);
2052 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2053 Object *found;
2055 if (!object_property_is_child(prop)) {
2056 continue;
2059 found = object_resolve_partial_path(prop->opaque, parts,
2060 typename, ambiguous);
2061 if (found) {
2062 if (obj) {
2063 *ambiguous = true;
2064 return NULL;
2066 obj = found;
2069 if (*ambiguous) {
2070 return NULL;
2074 return obj;
2077 Object *object_resolve_path_type(const char *path, const char *typename,
2078 bool *ambiguousp)
2080 Object *obj;
2081 gchar **parts;
2083 parts = g_strsplit(path, "/", 0);
2084 assert(parts);
2086 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2087 bool ambiguous = false;
2088 obj = object_resolve_partial_path(object_get_root(), parts,
2089 typename, &ambiguous);
2090 if (ambiguousp) {
2091 *ambiguousp = ambiguous;
2093 } else {
2094 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
2097 g_strfreev(parts);
2099 return obj;
2102 Object *object_resolve_path(const char *path, bool *ambiguous)
2104 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2107 typedef struct StringProperty
2109 char *(*get)(Object *, Error **);
2110 void (*set)(Object *, const char *, Error **);
2111 } StringProperty;
2113 static void property_get_str(Object *obj, Visitor *v, const char *name,
2114 void *opaque, Error **errp)
2116 StringProperty *prop = opaque;
2117 char *value;
2118 Error *err = NULL;
2120 value = prop->get(obj, &err);
2121 if (err) {
2122 error_propagate(errp, err);
2123 return;
2126 visit_type_str(v, name, &value, errp);
2127 g_free(value);
2130 static void property_set_str(Object *obj, Visitor *v, const char *name,
2131 void *opaque, Error **errp)
2133 StringProperty *prop = opaque;
2134 char *value;
2135 Error *local_err = NULL;
2137 visit_type_str(v, name, &value, &local_err);
2138 if (local_err) {
2139 error_propagate(errp, local_err);
2140 return;
2143 prop->set(obj, value, errp);
2144 g_free(value);
2147 static void property_release_str(Object *obj, const char *name,
2148 void *opaque)
2150 StringProperty *prop = opaque;
2151 g_free(prop);
2154 void object_property_add_str(Object *obj, const char *name,
2155 char *(*get)(Object *, Error **),
2156 void (*set)(Object *, const char *, Error **),
2157 Error **errp)
2159 Error *local_err = NULL;
2160 StringProperty *prop = g_malloc0(sizeof(*prop));
2162 prop->get = get;
2163 prop->set = set;
2165 object_property_add(obj, name, "string",
2166 get ? property_get_str : NULL,
2167 set ? property_set_str : NULL,
2168 property_release_str,
2169 prop, &local_err);
2170 if (local_err) {
2171 error_propagate(errp, local_err);
2172 g_free(prop);
2176 ObjectProperty *
2177 object_class_property_add_str(ObjectClass *klass, const char *name,
2178 char *(*get)(Object *, Error **),
2179 void (*set)(Object *, const char *,
2180 Error **),
2181 Error **errp)
2183 Error *local_err = NULL;
2184 StringProperty *prop = g_malloc0(sizeof(*prop));
2185 ObjectProperty *rv;
2187 prop->get = get;
2188 prop->set = set;
2190 rv = object_class_property_add(klass, name, "string",
2191 get ? property_get_str : NULL,
2192 set ? property_set_str : NULL,
2193 NULL,
2194 prop, &local_err);
2195 if (local_err) {
2196 error_propagate(errp, local_err);
2197 g_free(prop);
2200 return rv;
2203 typedef struct BoolProperty
2205 bool (*get)(Object *, Error **);
2206 void (*set)(Object *, bool, Error **);
2207 } BoolProperty;
2209 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2210 void *opaque, Error **errp)
2212 BoolProperty *prop = opaque;
2213 bool value;
2214 Error *err = NULL;
2216 value = prop->get(obj, &err);
2217 if (err) {
2218 error_propagate(errp, err);
2219 return;
2222 visit_type_bool(v, name, &value, errp);
2225 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2226 void *opaque, Error **errp)
2228 BoolProperty *prop = opaque;
2229 bool value;
2230 Error *local_err = NULL;
2232 visit_type_bool(v, name, &value, &local_err);
2233 if (local_err) {
2234 error_propagate(errp, local_err);
2235 return;
2238 prop->set(obj, value, errp);
2241 static void property_release_bool(Object *obj, const char *name,
2242 void *opaque)
2244 BoolProperty *prop = opaque;
2245 g_free(prop);
2248 void object_property_add_bool(Object *obj, const char *name,
2249 bool (*get)(Object *, Error **),
2250 void (*set)(Object *, bool, Error **),
2251 Error **errp)
2253 Error *local_err = NULL;
2254 BoolProperty *prop = g_malloc0(sizeof(*prop));
2256 prop->get = get;
2257 prop->set = set;
2259 object_property_add(obj, name, "bool",
2260 get ? property_get_bool : NULL,
2261 set ? property_set_bool : NULL,
2262 property_release_bool,
2263 prop, &local_err);
2264 if (local_err) {
2265 error_propagate(errp, local_err);
2266 g_free(prop);
2270 ObjectProperty *
2271 object_class_property_add_bool(ObjectClass *klass, const char *name,
2272 bool (*get)(Object *, Error **),
2273 void (*set)(Object *, bool, Error **),
2274 Error **errp)
2276 Error *local_err = NULL;
2277 BoolProperty *prop = g_malloc0(sizeof(*prop));
2278 ObjectProperty *rv;
2280 prop->get = get;
2281 prop->set = set;
2283 rv = object_class_property_add(klass, name, "bool",
2284 get ? property_get_bool : NULL,
2285 set ? property_set_bool : NULL,
2286 NULL,
2287 prop, &local_err);
2288 if (local_err) {
2289 error_propagate(errp, local_err);
2290 g_free(prop);
2293 return rv;
2296 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2297 void *opaque, Error **errp)
2299 EnumProperty *prop = opaque;
2300 int value;
2301 Error *err = NULL;
2303 value = prop->get(obj, &err);
2304 if (err) {
2305 error_propagate(errp, err);
2306 return;
2309 visit_type_enum(v, name, &value, prop->lookup, errp);
2312 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2313 void *opaque, Error **errp)
2315 EnumProperty *prop = opaque;
2316 int value;
2317 Error *err = NULL;
2319 visit_type_enum(v, name, &value, prop->lookup, &err);
2320 if (err) {
2321 error_propagate(errp, err);
2322 return;
2324 prop->set(obj, value, errp);
2327 static void property_release_enum(Object *obj, const char *name,
2328 void *opaque)
2330 EnumProperty *prop = opaque;
2331 g_free(prop);
2334 void object_property_add_enum(Object *obj, const char *name,
2335 const char *typename,
2336 const QEnumLookup *lookup,
2337 int (*get)(Object *, Error **),
2338 void (*set)(Object *, int, Error **),
2339 Error **errp)
2341 Error *local_err = NULL;
2342 EnumProperty *prop = g_malloc(sizeof(*prop));
2344 prop->lookup = lookup;
2345 prop->get = get;
2346 prop->set = set;
2348 object_property_add(obj, name, typename,
2349 get ? property_get_enum : NULL,
2350 set ? property_set_enum : NULL,
2351 property_release_enum,
2352 prop, &local_err);
2353 if (local_err) {
2354 error_propagate(errp, local_err);
2355 g_free(prop);
2359 ObjectProperty *
2360 object_class_property_add_enum(ObjectClass *klass, const char *name,
2361 const char *typename,
2362 const QEnumLookup *lookup,
2363 int (*get)(Object *, Error **),
2364 void (*set)(Object *, int, Error **),
2365 Error **errp)
2367 Error *local_err = NULL;
2368 EnumProperty *prop = g_malloc(sizeof(*prop));
2369 ObjectProperty *rv;
2371 prop->lookup = lookup;
2372 prop->get = get;
2373 prop->set = set;
2375 rv = object_class_property_add(klass, name, typename,
2376 get ? property_get_enum : NULL,
2377 set ? property_set_enum : NULL,
2378 NULL,
2379 prop, &local_err);
2380 if (local_err) {
2381 error_propagate(errp, local_err);
2382 g_free(prop);
2385 return rv;
2388 typedef struct TMProperty {
2389 void (*get)(Object *, struct tm *, Error **);
2390 } TMProperty;
2392 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2393 void *opaque, Error **errp)
2395 TMProperty *prop = opaque;
2396 Error *err = NULL;
2397 struct tm value;
2399 prop->get(obj, &value, &err);
2400 if (err) {
2401 goto out;
2404 visit_start_struct(v, name, NULL, 0, &err);
2405 if (err) {
2406 goto out;
2408 visit_type_int32(v, "tm_year", &value.tm_year, &err);
2409 if (err) {
2410 goto out_end;
2412 visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
2413 if (err) {
2414 goto out_end;
2416 visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
2417 if (err) {
2418 goto out_end;
2420 visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
2421 if (err) {
2422 goto out_end;
2424 visit_type_int32(v, "tm_min", &value.tm_min, &err);
2425 if (err) {
2426 goto out_end;
2428 visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
2429 if (err) {
2430 goto out_end;
2432 visit_check_struct(v, &err);
2433 out_end:
2434 visit_end_struct(v, NULL);
2435 out:
2436 error_propagate(errp, err);
2440 static void property_release_tm(Object *obj, const char *name,
2441 void *opaque)
2443 TMProperty *prop = opaque;
2444 g_free(prop);
2447 void object_property_add_tm(Object *obj, const char *name,
2448 void (*get)(Object *, struct tm *, Error **),
2449 Error **errp)
2451 Error *local_err = NULL;
2452 TMProperty *prop = g_malloc0(sizeof(*prop));
2454 prop->get = get;
2456 object_property_add(obj, name, "struct tm",
2457 get ? property_get_tm : NULL, NULL,
2458 property_release_tm,
2459 prop, &local_err);
2460 if (local_err) {
2461 error_propagate(errp, local_err);
2462 g_free(prop);
2466 ObjectProperty *
2467 object_class_property_add_tm(ObjectClass *klass, const char *name,
2468 void (*get)(Object *, struct tm *, Error **),
2469 Error **errp)
2471 Error *local_err = NULL;
2472 TMProperty *prop = g_malloc0(sizeof(*prop));
2473 ObjectProperty *rv;
2475 prop->get = get;
2477 rv = object_class_property_add(klass, name, "struct tm",
2478 get ? property_get_tm : NULL, NULL,
2479 NULL,
2480 prop, &local_err);
2481 if (local_err) {
2482 error_propagate(errp, local_err);
2483 g_free(prop);
2486 return rv;
2489 static char *qdev_get_type(Object *obj, Error **errp)
2491 return g_strdup(object_get_typename(obj));
2494 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2495 void *opaque, Error **errp)
2497 uint8_t value = *(uint8_t *)opaque;
2498 visit_type_uint8(v, name, &value, errp);
2501 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2502 void *opaque, Error **errp)
2504 uint8_t *field = opaque;
2505 uint8_t value;
2506 Error *local_err = NULL;
2508 visit_type_uint8(v, name, &value, &local_err);
2509 if (local_err) {
2510 error_propagate(errp, local_err);
2511 return;
2514 *field = value;
2517 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2518 void *opaque, Error **errp)
2520 uint16_t value = *(uint16_t *)opaque;
2521 visit_type_uint16(v, name, &value, errp);
2524 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2525 void *opaque, Error **errp)
2527 uint16_t *field = opaque;
2528 uint16_t value;
2529 Error *local_err = NULL;
2531 visit_type_uint16(v, name, &value, &local_err);
2532 if (local_err) {
2533 error_propagate(errp, local_err);
2534 return;
2537 *field = value;
2540 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2541 void *opaque, Error **errp)
2543 uint32_t value = *(uint32_t *)opaque;
2544 visit_type_uint32(v, name, &value, errp);
2547 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2548 void *opaque, Error **errp)
2550 uint32_t *field = opaque;
2551 uint32_t value;
2552 Error *local_err = NULL;
2554 visit_type_uint32(v, name, &value, &local_err);
2555 if (local_err) {
2556 error_propagate(errp, local_err);
2557 return;
2560 *field = value;
2563 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2564 void *opaque, Error **errp)
2566 uint64_t value = *(uint64_t *)opaque;
2567 visit_type_uint64(v, name, &value, errp);
2570 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2571 void *opaque, Error **errp)
2573 uint64_t *field = opaque;
2574 uint64_t value;
2575 Error *local_err = NULL;
2577 visit_type_uint64(v, name, &value, &local_err);
2578 if (local_err) {
2579 error_propagate(errp, local_err);
2580 return;
2583 *field = value;
2586 void object_property_add_uint8_ptr(Object *obj, const char *name,
2587 const uint8_t *v,
2588 ObjectPropertyFlags flags,
2589 Error **errp)
2591 ObjectPropertyAccessor *getter = NULL;
2592 ObjectPropertyAccessor *setter = NULL;
2594 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2595 getter = property_get_uint8_ptr;
2598 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2599 setter = property_set_uint8_ptr;
2602 object_property_add(obj, name, "uint8",
2603 getter, setter, NULL, (void *)v, errp);
2606 ObjectProperty *
2607 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2608 const uint8_t *v,
2609 ObjectPropertyFlags flags,
2610 Error **errp)
2612 ObjectPropertyAccessor *getter = NULL;
2613 ObjectPropertyAccessor *setter = NULL;
2615 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2616 getter = property_get_uint8_ptr;
2619 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2620 setter = property_set_uint8_ptr;
2623 return object_class_property_add(klass, name, "uint8",
2624 getter, setter, NULL, (void *)v, errp);
2627 void object_property_add_uint16_ptr(Object *obj, const char *name,
2628 const uint16_t *v,
2629 ObjectPropertyFlags flags,
2630 Error **errp)
2632 ObjectPropertyAccessor *getter = NULL;
2633 ObjectPropertyAccessor *setter = NULL;
2635 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2636 getter = property_get_uint16_ptr;
2639 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2640 setter = property_set_uint16_ptr;
2643 object_property_add(obj, name, "uint16",
2644 getter, setter, NULL, (void *)v, errp);
2647 ObjectProperty *
2648 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2649 const uint16_t *v,
2650 ObjectPropertyFlags flags,
2651 Error **errp)
2653 ObjectPropertyAccessor *getter = NULL;
2654 ObjectPropertyAccessor *setter = NULL;
2656 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2657 getter = property_get_uint16_ptr;
2660 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2661 setter = property_set_uint16_ptr;
2664 return object_class_property_add(klass, name, "uint16",
2665 getter, setter, NULL, (void *)v, errp);
2668 void object_property_add_uint32_ptr(Object *obj, const char *name,
2669 const uint32_t *v,
2670 ObjectPropertyFlags flags,
2671 Error **errp)
2673 ObjectPropertyAccessor *getter = NULL;
2674 ObjectPropertyAccessor *setter = NULL;
2676 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2677 getter = property_get_uint32_ptr;
2680 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2681 setter = property_set_uint32_ptr;
2684 object_property_add(obj, name, "uint32",
2685 getter, setter, NULL, (void *)v, errp);
2688 ObjectProperty *
2689 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2690 const uint32_t *v,
2691 ObjectPropertyFlags flags,
2692 Error **errp)
2694 ObjectPropertyAccessor *getter = NULL;
2695 ObjectPropertyAccessor *setter = NULL;
2697 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2698 getter = property_get_uint32_ptr;
2701 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2702 setter = property_set_uint32_ptr;
2705 return object_class_property_add(klass, name, "uint32",
2706 getter, setter, NULL, (void *)v, errp);
2709 void object_property_add_uint64_ptr(Object *obj, const char *name,
2710 const uint64_t *v,
2711 ObjectPropertyFlags flags,
2712 Error **errp)
2714 ObjectPropertyAccessor *getter = NULL;
2715 ObjectPropertyAccessor *setter = NULL;
2717 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2718 getter = property_get_uint64_ptr;
2721 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2722 setter = property_set_uint64_ptr;
2725 object_property_add(obj, name, "uint64",
2726 getter, setter, NULL, (void *)v, errp);
2729 ObjectProperty *
2730 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2731 const uint64_t *v,
2732 ObjectPropertyFlags flags,
2733 Error **errp)
2735 ObjectPropertyAccessor *getter = NULL;
2736 ObjectPropertyAccessor *setter = NULL;
2738 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2739 getter = property_get_uint64_ptr;
2742 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2743 setter = property_set_uint64_ptr;
2746 return object_class_property_add(klass, name, "uint64",
2747 getter, setter, NULL, (void *)v, errp);
2750 typedef struct {
2751 Object *target_obj;
2752 char *target_name;
2753 } AliasProperty;
2755 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2756 void *opaque, Error **errp)
2758 AliasProperty *prop = opaque;
2760 object_property_get(prop->target_obj, v, prop->target_name, errp);
2763 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2764 void *opaque, Error **errp)
2766 AliasProperty *prop = opaque;
2768 object_property_set(prop->target_obj, v, prop->target_name, errp);
2771 static Object *property_resolve_alias(Object *obj, void *opaque,
2772 const gchar *part)
2774 AliasProperty *prop = opaque;
2776 return object_resolve_path_component(prop->target_obj, prop->target_name);
2779 static void property_release_alias(Object *obj, const char *name, void *opaque)
2781 AliasProperty *prop = opaque;
2783 g_free(prop->target_name);
2784 g_free(prop);
2787 void object_property_add_alias(Object *obj, const char *name,
2788 Object *target_obj, const char *target_name,
2789 Error **errp)
2791 AliasProperty *prop;
2792 ObjectProperty *op;
2793 ObjectProperty *target_prop;
2794 gchar *prop_type;
2795 Error *local_err = NULL;
2797 target_prop = object_property_find(target_obj, target_name, errp);
2798 if (!target_prop) {
2799 return;
2802 if (object_property_is_child(target_prop)) {
2803 prop_type = g_strdup_printf("link%s",
2804 target_prop->type + strlen("child"));
2805 } else {
2806 prop_type = g_strdup(target_prop->type);
2809 prop = g_malloc(sizeof(*prop));
2810 prop->target_obj = target_obj;
2811 prop->target_name = g_strdup(target_name);
2813 op = object_property_add(obj, name, prop_type,
2814 property_get_alias,
2815 property_set_alias,
2816 property_release_alias,
2817 prop, &local_err);
2818 if (local_err) {
2819 error_propagate(errp, local_err);
2820 g_free(prop);
2821 goto out;
2823 op->resolve = property_resolve_alias;
2824 if (target_prop->defval) {
2825 op->defval = qobject_ref(target_prop->defval);
2828 object_property_set_description(obj, op->name,
2829 target_prop->description,
2830 &error_abort);
2832 out:
2833 g_free(prop_type);
2836 void object_property_set_description(Object *obj, const char *name,
2837 const char *description, Error **errp)
2839 ObjectProperty *op;
2841 op = object_property_find(obj, name, errp);
2842 if (!op) {
2843 return;
2846 g_free(op->description);
2847 op->description = g_strdup(description);
2850 void object_class_property_set_description(ObjectClass *klass,
2851 const char *name,
2852 const char *description,
2853 Error **errp)
2855 ObjectProperty *op;
2857 op = g_hash_table_lookup(klass->properties, name);
2858 if (!op) {
2859 error_setg(errp, "Property '.%s' not found", name);
2860 return;
2863 g_free(op->description);
2864 op->description = g_strdup(description);
2867 static void object_class_init(ObjectClass *klass, void *data)
2869 object_class_property_add_str(klass, "type", qdev_get_type,
2870 NULL, &error_abort);
2873 static void register_types(void)
2875 static TypeInfo interface_info = {
2876 .name = TYPE_INTERFACE,
2877 .class_size = sizeof(InterfaceClass),
2878 .abstract = true,
2881 static TypeInfo object_info = {
2882 .name = TYPE_OBJECT,
2883 .instance_size = sizeof(Object),
2884 .class_init = object_class_init,
2885 .abstract = true,
2888 type_interface = type_register_internal(&interface_info);
2889 type_register_internal(&object_info);
2892 type_init(register_types)