virtiofsd: Open vhost connection instead of mounting
[qemu/kevin.git] / qom / object.c
blob0d971ca8977203995194a83acf557ec6db8a08e5
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/qapi-builtin-visit.h"
23 #include "qapi/qmp/qerror.h"
24 #include "trace.h"
26 /* TODO: replace QObject with a simpler visitor to avoid a dependency
27 * of the QOM core on QObject? */
28 #include "qom/qom-qobject.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/qnum.h"
31 #include "qapi/qmp/qstring.h"
32 #include "qemu/error-report.h"
34 #define MAX_INTERFACES 32
36 typedef struct InterfaceImpl InterfaceImpl;
37 typedef struct TypeImpl TypeImpl;
39 struct InterfaceImpl
41 const char *typename;
44 struct TypeImpl
46 const char *name;
48 size_t class_size;
50 size_t instance_size;
52 void (*class_init)(ObjectClass *klass, void *data);
53 void (*class_base_init)(ObjectClass *klass, void *data);
55 void *class_data;
57 void (*instance_init)(Object *obj);
58 void (*instance_post_init)(Object *obj);
59 void (*instance_finalize)(Object *obj);
61 bool abstract;
63 const char *parent;
64 TypeImpl *parent_type;
66 ObjectClass *class;
68 int num_interfaces;
69 InterfaceImpl interfaces[MAX_INTERFACES];
72 static Type type_interface;
74 static GHashTable *type_table_get(void)
76 static GHashTable *type_table;
78 if (type_table == NULL) {
79 type_table = g_hash_table_new(g_str_hash, g_str_equal);
82 return type_table;
85 static bool enumerating_types;
87 static void type_table_add(TypeImpl *ti)
89 assert(!enumerating_types);
90 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
93 static TypeImpl *type_table_lookup(const char *name)
95 return g_hash_table_lookup(type_table_get(), name);
98 static TypeImpl *type_new(const TypeInfo *info)
100 TypeImpl *ti = g_malloc0(sizeof(*ti));
101 int i;
103 g_assert(info->name != NULL);
105 if (type_table_lookup(info->name) != NULL) {
106 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
107 abort();
110 ti->name = g_strdup(info->name);
111 ti->parent = g_strdup(info->parent);
113 ti->class_size = info->class_size;
114 ti->instance_size = info->instance_size;
116 ti->class_init = info->class_init;
117 ti->class_base_init = info->class_base_init;
118 ti->class_data = info->class_data;
120 ti->instance_init = info->instance_init;
121 ti->instance_post_init = info->instance_post_init;
122 ti->instance_finalize = info->instance_finalize;
124 ti->abstract = info->abstract;
126 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
127 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
129 ti->num_interfaces = i;
131 return ti;
134 static TypeImpl *type_register_internal(const TypeInfo *info)
136 TypeImpl *ti;
137 ti = type_new(info);
139 type_table_add(ti);
140 return ti;
143 TypeImpl *type_register(const TypeInfo *info)
145 assert(info->parent);
146 return type_register_internal(info);
149 TypeImpl *type_register_static(const TypeInfo *info)
151 return type_register(info);
154 void type_register_static_array(const TypeInfo *infos, int nr_infos)
156 int i;
158 for (i = 0; i < nr_infos; i++) {
159 type_register_static(&infos[i]);
163 static TypeImpl *type_get_by_name(const char *name)
165 if (name == NULL) {
166 return NULL;
169 return type_table_lookup(name);
172 static TypeImpl *type_get_parent(TypeImpl *type)
174 if (!type->parent_type && type->parent) {
175 type->parent_type = type_get_by_name(type->parent);
176 g_assert(type->parent_type != NULL);
179 return type->parent_type;
182 static bool type_has_parent(TypeImpl *type)
184 return (type->parent != NULL);
187 static size_t type_class_get_size(TypeImpl *ti)
189 if (ti->class_size) {
190 return ti->class_size;
193 if (type_has_parent(ti)) {
194 return type_class_get_size(type_get_parent(ti));
197 return sizeof(ObjectClass);
200 static size_t type_object_get_size(TypeImpl *ti)
202 if (ti->instance_size) {
203 return ti->instance_size;
206 if (type_has_parent(ti)) {
207 return type_object_get_size(type_get_parent(ti));
210 return 0;
213 size_t object_type_get_instance_size(const char *typename)
215 TypeImpl *type = type_get_by_name(typename);
217 g_assert(type != NULL);
218 return type_object_get_size(type);
221 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
223 assert(target_type);
225 /* Check if target_type is a direct ancestor of type */
226 while (type) {
227 if (type == target_type) {
228 return true;
231 type = type_get_parent(type);
234 return false;
237 static void type_initialize(TypeImpl *ti);
239 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
240 TypeImpl *parent_type)
242 InterfaceClass *new_iface;
243 TypeInfo info = { };
244 TypeImpl *iface_impl;
246 info.parent = parent_type->name;
247 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
248 info.abstract = true;
250 iface_impl = type_new(&info);
251 iface_impl->parent_type = parent_type;
252 type_initialize(iface_impl);
253 g_free((char *)info.name);
255 new_iface = (InterfaceClass *)iface_impl->class;
256 new_iface->concrete_class = ti->class;
257 new_iface->interface_type = interface_type;
259 ti->class->interfaces = g_slist_append(ti->class->interfaces,
260 iface_impl->class);
263 static void object_property_free(gpointer data)
265 ObjectProperty *prop = data;
267 g_free(prop->name);
268 g_free(prop->type);
269 g_free(prop->description);
270 g_free(prop);
273 static void type_initialize(TypeImpl *ti)
275 TypeImpl *parent;
277 if (ti->class) {
278 return;
281 ti->class_size = type_class_get_size(ti);
282 ti->instance_size = type_object_get_size(ti);
283 /* Any type with zero instance_size is implicitly abstract.
284 * This means interface types are all abstract.
286 if (ti->instance_size == 0) {
287 ti->abstract = true;
289 if (type_is_ancestor(ti, type_interface)) {
290 assert(ti->instance_size == 0);
291 assert(ti->abstract);
292 assert(!ti->instance_init);
293 assert(!ti->instance_post_init);
294 assert(!ti->instance_finalize);
295 assert(!ti->num_interfaces);
297 ti->class = g_malloc0(ti->class_size);
299 parent = type_get_parent(ti);
300 if (parent) {
301 type_initialize(parent);
302 GSList *e;
303 int i;
305 g_assert(parent->class_size <= ti->class_size);
306 memcpy(ti->class, parent->class, parent->class_size);
307 ti->class->interfaces = NULL;
308 ti->class->properties = g_hash_table_new_full(
309 g_str_hash, g_str_equal, g_free, object_property_free);
311 for (e = parent->class->interfaces; e; e = e->next) {
312 InterfaceClass *iface = e->data;
313 ObjectClass *klass = OBJECT_CLASS(iface);
315 type_initialize_interface(ti, iface->interface_type, klass->type);
318 for (i = 0; i < ti->num_interfaces; i++) {
319 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
320 for (e = ti->class->interfaces; e; e = e->next) {
321 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
323 if (type_is_ancestor(target_type, t)) {
324 break;
328 if (e) {
329 continue;
332 type_initialize_interface(ti, t, t);
334 } else {
335 ti->class->properties = g_hash_table_new_full(
336 g_str_hash, g_str_equal, g_free, object_property_free);
339 ti->class->type = ti;
341 while (parent) {
342 if (parent->class_base_init) {
343 parent->class_base_init(ti->class, ti->class_data);
345 parent = type_get_parent(parent);
348 if (ti->class_init) {
349 ti->class_init(ti->class, ti->class_data);
353 static void object_init_with_type(Object *obj, TypeImpl *ti)
355 if (type_has_parent(ti)) {
356 object_init_with_type(obj, type_get_parent(ti));
359 if (ti->instance_init) {
360 ti->instance_init(obj);
364 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
366 if (ti->instance_post_init) {
367 ti->instance_post_init(obj);
370 if (type_has_parent(ti)) {
371 object_post_init_with_type(obj, type_get_parent(ti));
375 void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp)
377 int i;
379 if (!props) {
380 return;
383 for (i = 0; i < props->len; i++) {
384 GlobalProperty *p = g_ptr_array_index(props, i);
385 Error *err = NULL;
387 if (object_dynamic_cast(obj, p->driver) == NULL) {
388 continue;
390 if (p->optional && !object_property_find(obj, p->property, NULL)) {
391 continue;
393 p->used = true;
394 object_property_parse(obj, p->value, p->property, &err);
395 if (err != NULL) {
396 error_prepend(&err, "can't apply global %s.%s=%s: ",
397 p->driver, p->property, p->value);
399 * If errp != NULL, propagate error and return.
400 * If errp == NULL, report a warning, but keep going
401 * with the remaining globals.
403 if (errp) {
404 error_propagate(errp, err);
405 return;
406 } else {
407 warn_report_err(err);
414 * Global property defaults
415 * Slot 0: accelerator's global property defaults
416 * Slot 1: machine's global property defaults
417 * Slot 2: global properties from legacy command line option
418 * Each is a GPtrArray of of GlobalProperty.
419 * Applied in order, later entries override earlier ones.
421 static GPtrArray *object_compat_props[3];
424 * Retrieve @GPtrArray for global property defined with options
425 * other than "-global". These are generally used for syntactic
426 * sugar and legacy command line options.
428 void object_register_sugar_prop(const char *driver, const char *prop, const char *value)
430 GlobalProperty *g;
431 if (!object_compat_props[2]) {
432 object_compat_props[2] = g_ptr_array_new();
434 g = g_new0(GlobalProperty, 1);
435 g->driver = g_strdup(driver);
436 g->property = g_strdup(prop);
437 g->value = g_strdup(value);
438 g_ptr_array_add(object_compat_props[2], g);
442 * Set machine's global property defaults to @compat_props.
443 * May be called at most once.
445 void object_set_machine_compat_props(GPtrArray *compat_props)
447 assert(!object_compat_props[1]);
448 object_compat_props[1] = compat_props;
452 * Set accelerator's global property defaults to @compat_props.
453 * May be called at most once.
455 void object_set_accelerator_compat_props(GPtrArray *compat_props)
457 assert(!object_compat_props[0]);
458 object_compat_props[0] = compat_props;
461 void object_apply_compat_props(Object *obj)
463 int i;
465 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
466 object_apply_global_props(obj, object_compat_props[i],
467 i == 2 ? &error_fatal : &error_abort);
471 static void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
473 Object *obj = data;
475 type_initialize(type);
477 g_assert(type->instance_size >= sizeof(Object));
478 g_assert(type->abstract == false);
479 g_assert(size >= type->instance_size);
481 memset(obj, 0, type->instance_size);
482 obj->class = type->class;
483 object_ref(obj);
484 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
485 NULL, object_property_free);
486 object_init_with_type(obj, type);
487 object_post_init_with_type(obj, type);
490 void object_initialize(void *data, size_t size, const char *typename)
492 TypeImpl *type = type_get_by_name(typename);
494 if (!type) {
495 error_report("missing object type '%s'", typename);
496 abort();
499 object_initialize_with_type(data, size, type);
502 void object_initialize_child(Object *parentobj, const char *propname,
503 void *childobj, size_t size, const char *type,
504 Error **errp, ...)
506 va_list vargs;
508 va_start(vargs, errp);
509 object_initialize_childv(parentobj, propname, childobj, size, type, errp,
510 vargs);
511 va_end(vargs);
514 void object_initialize_childv(Object *parentobj, const char *propname,
515 void *childobj, size_t size, const char *type,
516 Error **errp, va_list vargs)
518 Error *local_err = NULL;
519 Object *obj;
520 UserCreatable *uc;
522 object_initialize(childobj, size, type);
523 obj = OBJECT(childobj);
525 object_set_propv(obj, &local_err, vargs);
526 if (local_err) {
527 goto out;
530 object_property_add_child(parentobj, propname, obj, &local_err);
531 if (local_err) {
532 goto out;
535 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
536 if (uc) {
537 user_creatable_complete(uc, &local_err);
538 if (local_err) {
539 object_unparent(obj);
540 goto out;
545 * Since object_property_add_child added a reference to the child object,
546 * we can drop the reference added by object_initialize(), so the child
547 * property will own the only reference to the object.
549 object_unref(obj);
551 out:
552 if (local_err) {
553 error_propagate(errp, local_err);
554 object_unref(obj);
558 static inline bool object_property_is_child(ObjectProperty *prop)
560 return strstart(prop->type, "child<", NULL);
563 static void object_property_del_all(Object *obj)
565 ObjectProperty *prop;
566 GHashTableIter iter;
567 gpointer key, value;
568 bool released;
570 do {
571 released = false;
572 g_hash_table_iter_init(&iter, obj->properties);
573 while (g_hash_table_iter_next(&iter, &key, &value)) {
574 prop = value;
575 if (prop->release) {
576 prop->release(obj, prop->name, prop->opaque);
577 prop->release = NULL;
578 released = true;
579 break;
581 g_hash_table_iter_remove(&iter);
583 } while (released);
585 g_hash_table_unref(obj->properties);
588 static void object_property_del_child(Object *obj, Object *child, Error **errp)
590 ObjectProperty *prop;
591 GHashTableIter iter;
592 gpointer key, value;
594 g_hash_table_iter_init(&iter, obj->properties);
595 while (g_hash_table_iter_next(&iter, &key, &value)) {
596 prop = value;
597 if (object_property_is_child(prop) && prop->opaque == child) {
598 if (prop->release) {
599 prop->release(obj, prop->name, prop->opaque);
600 prop->release = NULL;
602 break;
605 g_hash_table_iter_init(&iter, obj->properties);
606 while (g_hash_table_iter_next(&iter, &key, &value)) {
607 prop = value;
608 if (object_property_is_child(prop) && prop->opaque == child) {
609 g_hash_table_iter_remove(&iter);
610 break;
615 void object_unparent(Object *obj)
617 if (obj->parent) {
618 object_property_del_child(obj->parent, obj, NULL);
622 static void object_deinit(Object *obj, TypeImpl *type)
624 if (type->instance_finalize) {
625 type->instance_finalize(obj);
628 if (type_has_parent(type)) {
629 object_deinit(obj, type_get_parent(type));
633 static void object_finalize(void *data)
635 Object *obj = data;
636 TypeImpl *ti = obj->class->type;
638 object_property_del_all(obj);
639 object_deinit(obj, ti);
641 g_assert(obj->ref == 0);
642 if (obj->free) {
643 obj->free(obj);
647 static Object *object_new_with_type(Type type)
649 Object *obj;
651 g_assert(type != NULL);
652 type_initialize(type);
654 obj = g_malloc(type->instance_size);
655 object_initialize_with_type(obj, type->instance_size, type);
656 obj->free = g_free;
658 return obj;
661 Object *object_new_with_class(ObjectClass *klass)
663 return object_new_with_type(klass->type);
666 Object *object_new(const char *typename)
668 TypeImpl *ti = type_get_by_name(typename);
670 return object_new_with_type(ti);
674 Object *object_new_with_props(const char *typename,
675 Object *parent,
676 const char *id,
677 Error **errp,
678 ...)
680 va_list vargs;
681 Object *obj;
683 va_start(vargs, errp);
684 obj = object_new_with_propv(typename, parent, id, errp, vargs);
685 va_end(vargs);
687 return obj;
691 Object *object_new_with_propv(const char *typename,
692 Object *parent,
693 const char *id,
694 Error **errp,
695 va_list vargs)
697 Object *obj;
698 ObjectClass *klass;
699 Error *local_err = NULL;
700 UserCreatable *uc;
702 klass = object_class_by_name(typename);
703 if (!klass) {
704 error_setg(errp, "invalid object type: %s", typename);
705 return NULL;
708 if (object_class_is_abstract(klass)) {
709 error_setg(errp, "object type '%s' is abstract", typename);
710 return NULL;
712 obj = object_new_with_type(klass->type);
714 if (object_set_propv(obj, &local_err, vargs) < 0) {
715 goto error;
718 if (id != NULL) {
719 object_property_add_child(parent, id, obj, &local_err);
720 if (local_err) {
721 goto error;
725 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
726 if (uc) {
727 user_creatable_complete(uc, &local_err);
728 if (local_err) {
729 if (id != NULL) {
730 object_unparent(obj);
732 goto error;
736 object_unref(OBJECT(obj));
737 return obj;
739 error:
740 error_propagate(errp, local_err);
741 object_unref(obj);
742 return NULL;
746 int object_set_props(Object *obj,
747 Error **errp,
748 ...)
750 va_list vargs;
751 int ret;
753 va_start(vargs, errp);
754 ret = object_set_propv(obj, errp, vargs);
755 va_end(vargs);
757 return ret;
761 int object_set_propv(Object *obj,
762 Error **errp,
763 va_list vargs)
765 const char *propname;
766 Error *local_err = NULL;
768 propname = va_arg(vargs, char *);
769 while (propname != NULL) {
770 const char *value = va_arg(vargs, char *);
772 g_assert(value != NULL);
773 object_property_parse(obj, value, propname, &local_err);
774 if (local_err) {
775 error_propagate(errp, local_err);
776 return -1;
778 propname = va_arg(vargs, char *);
781 return 0;
785 Object *object_dynamic_cast(Object *obj, const char *typename)
787 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
788 return obj;
791 return NULL;
794 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
795 const char *file, int line, const char *func)
797 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
798 typename, file, line, func);
800 #ifdef CONFIG_QOM_CAST_DEBUG
801 int i;
802 Object *inst;
804 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
805 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
806 goto out;
810 inst = object_dynamic_cast(obj, typename);
812 if (!inst && obj) {
813 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
814 file, line, func, obj, typename);
815 abort();
818 assert(obj == inst);
820 if (obj && obj == inst) {
821 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
822 atomic_set(&obj->class->object_cast_cache[i - 1],
823 atomic_read(&obj->class->object_cast_cache[i]));
825 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
828 out:
829 #endif
830 return obj;
833 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
834 const char *typename)
836 ObjectClass *ret = NULL;
837 TypeImpl *target_type;
838 TypeImpl *type;
840 if (!class) {
841 return NULL;
844 /* A simple fast path that can trigger a lot for leaf classes. */
845 type = class->type;
846 if (type->name == typename) {
847 return class;
850 target_type = type_get_by_name(typename);
851 if (!target_type) {
852 /* target class type unknown, so fail the cast */
853 return NULL;
856 if (type->class->interfaces &&
857 type_is_ancestor(target_type, type_interface)) {
858 int found = 0;
859 GSList *i;
861 for (i = class->interfaces; i; i = i->next) {
862 ObjectClass *target_class = i->data;
864 if (type_is_ancestor(target_class->type, target_type)) {
865 ret = target_class;
866 found++;
870 /* The match was ambiguous, don't allow a cast */
871 if (found > 1) {
872 ret = NULL;
874 } else if (type_is_ancestor(type, target_type)) {
875 ret = class;
878 return ret;
881 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
882 const char *typename,
883 const char *file, int line,
884 const char *func)
886 ObjectClass *ret;
888 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
889 typename, file, line, func);
891 #ifdef CONFIG_QOM_CAST_DEBUG
892 int i;
894 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
895 if (atomic_read(&class->class_cast_cache[i]) == typename) {
896 ret = class;
897 goto out;
900 #else
901 if (!class || !class->interfaces) {
902 return class;
904 #endif
906 ret = object_class_dynamic_cast(class, typename);
907 if (!ret && class) {
908 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
909 file, line, func, class, typename);
910 abort();
913 #ifdef CONFIG_QOM_CAST_DEBUG
914 if (class && ret == class) {
915 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
916 atomic_set(&class->class_cast_cache[i - 1],
917 atomic_read(&class->class_cast_cache[i]));
919 atomic_set(&class->class_cast_cache[i - 1], typename);
921 out:
922 #endif
923 return ret;
926 const char *object_get_typename(const Object *obj)
928 return obj->class->type->name;
931 ObjectClass *object_get_class(Object *obj)
933 return obj->class;
936 bool object_class_is_abstract(ObjectClass *klass)
938 return klass->type->abstract;
941 const char *object_class_get_name(ObjectClass *klass)
943 return klass->type->name;
946 ObjectClass *object_class_by_name(const char *typename)
948 TypeImpl *type = type_get_by_name(typename);
950 if (!type) {
951 return NULL;
954 type_initialize(type);
956 return type->class;
959 ObjectClass *object_class_get_parent(ObjectClass *class)
961 TypeImpl *type = type_get_parent(class->type);
963 if (!type) {
964 return NULL;
967 type_initialize(type);
969 return type->class;
972 typedef struct OCFData
974 void (*fn)(ObjectClass *klass, void *opaque);
975 const char *implements_type;
976 bool include_abstract;
977 void *opaque;
978 } OCFData;
980 static void object_class_foreach_tramp(gpointer key, gpointer value,
981 gpointer opaque)
983 OCFData *data = opaque;
984 TypeImpl *type = value;
985 ObjectClass *k;
987 type_initialize(type);
988 k = type->class;
990 if (!data->include_abstract && type->abstract) {
991 return;
994 if (data->implements_type &&
995 !object_class_dynamic_cast(k, data->implements_type)) {
996 return;
999 data->fn(k, data->opaque);
1002 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1003 const char *implements_type, bool include_abstract,
1004 void *opaque)
1006 OCFData data = { fn, implements_type, include_abstract, opaque };
1008 enumerating_types = true;
1009 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1010 enumerating_types = false;
1013 static int do_object_child_foreach(Object *obj,
1014 int (*fn)(Object *child, void *opaque),
1015 void *opaque, bool recurse)
1017 GHashTableIter iter;
1018 ObjectProperty *prop;
1019 int ret = 0;
1021 g_hash_table_iter_init(&iter, obj->properties);
1022 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1023 if (object_property_is_child(prop)) {
1024 Object *child = prop->opaque;
1026 ret = fn(child, opaque);
1027 if (ret != 0) {
1028 break;
1030 if (recurse) {
1031 do_object_child_foreach(child, fn, opaque, true);
1035 return ret;
1038 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1039 void *opaque)
1041 return do_object_child_foreach(obj, fn, opaque, false);
1044 int object_child_foreach_recursive(Object *obj,
1045 int (*fn)(Object *child, void *opaque),
1046 void *opaque)
1048 return do_object_child_foreach(obj, fn, opaque, true);
1051 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1053 GSList **list = opaque;
1055 *list = g_slist_prepend(*list, klass);
1058 GSList *object_class_get_list(const char *implements_type,
1059 bool include_abstract)
1061 GSList *list = NULL;
1063 object_class_foreach(object_class_get_list_tramp,
1064 implements_type, include_abstract, &list);
1065 return list;
1068 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1070 return strcasecmp(object_class_get_name((ObjectClass *)a),
1071 object_class_get_name((ObjectClass *)b));
1074 GSList *object_class_get_list_sorted(const char *implements_type,
1075 bool include_abstract)
1077 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1078 object_class_cmp);
1081 void object_ref(Object *obj)
1083 if (!obj) {
1084 return;
1086 atomic_inc(&obj->ref);
1089 void object_unref(Object *obj)
1091 if (!obj) {
1092 return;
1094 g_assert(obj->ref > 0);
1096 /* parent always holds a reference to its children */
1097 if (atomic_fetch_dec(&obj->ref) == 1) {
1098 object_finalize(obj);
1102 ObjectProperty *
1103 object_property_add(Object *obj, const char *name, const char *type,
1104 ObjectPropertyAccessor *get,
1105 ObjectPropertyAccessor *set,
1106 ObjectPropertyRelease *release,
1107 void *opaque, Error **errp)
1109 ObjectProperty *prop;
1110 size_t name_len = strlen(name);
1112 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1113 int i;
1114 ObjectProperty *ret;
1115 char *name_no_array = g_strdup(name);
1117 name_no_array[name_len - 3] = '\0';
1118 for (i = 0; ; ++i) {
1119 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1121 ret = object_property_add(obj, full_name, type, get, set,
1122 release, opaque, NULL);
1123 g_free(full_name);
1124 if (ret) {
1125 break;
1128 g_free(name_no_array);
1129 return ret;
1132 if (object_property_find(obj, name, NULL) != NULL) {
1133 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1134 name, object_get_typename(obj));
1135 return NULL;
1138 prop = g_malloc0(sizeof(*prop));
1140 prop->name = g_strdup(name);
1141 prop->type = g_strdup(type);
1143 prop->get = get;
1144 prop->set = set;
1145 prop->release = release;
1146 prop->opaque = opaque;
1148 g_hash_table_insert(obj->properties, prop->name, prop);
1149 return prop;
1152 ObjectProperty *
1153 object_class_property_add(ObjectClass *klass,
1154 const char *name,
1155 const char *type,
1156 ObjectPropertyAccessor *get,
1157 ObjectPropertyAccessor *set,
1158 ObjectPropertyRelease *release,
1159 void *opaque,
1160 Error **errp)
1162 ObjectProperty *prop;
1164 if (object_class_property_find(klass, name, NULL) != NULL) {
1165 error_setg(errp, "attempt to add duplicate property '%s' to class (type '%s')",
1166 name, object_class_get_name(klass));
1167 return NULL;
1170 prop = g_malloc0(sizeof(*prop));
1172 prop->name = g_strdup(name);
1173 prop->type = g_strdup(type);
1175 prop->get = get;
1176 prop->set = set;
1177 prop->release = release;
1178 prop->opaque = opaque;
1180 g_hash_table_insert(klass->properties, g_strdup(name), prop);
1182 return prop;
1185 ObjectProperty *object_property_find(Object *obj, const char *name,
1186 Error **errp)
1188 ObjectProperty *prop;
1189 ObjectClass *klass = object_get_class(obj);
1191 prop = object_class_property_find(klass, name, NULL);
1192 if (prop) {
1193 return prop;
1196 prop = g_hash_table_lookup(obj->properties, name);
1197 if (prop) {
1198 return prop;
1201 error_setg(errp, "Property '.%s' not found", name);
1202 return NULL;
1205 void object_property_iter_init(ObjectPropertyIterator *iter,
1206 Object *obj)
1208 g_hash_table_iter_init(&iter->iter, obj->properties);
1209 iter->nextclass = object_get_class(obj);
1212 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1214 gpointer key, val;
1215 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1216 if (!iter->nextclass) {
1217 return NULL;
1219 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1220 iter->nextclass = object_class_get_parent(iter->nextclass);
1222 return val;
1225 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1226 ObjectClass *klass)
1228 g_hash_table_iter_init(&iter->iter, klass->properties);
1229 iter->nextclass = object_class_get_parent(klass);
1232 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1233 Error **errp)
1235 ObjectProperty *prop;
1236 ObjectClass *parent_klass;
1238 parent_klass = object_class_get_parent(klass);
1239 if (parent_klass) {
1240 prop = object_class_property_find(parent_klass, name, NULL);
1241 if (prop) {
1242 return prop;
1246 prop = g_hash_table_lookup(klass->properties, name);
1247 if (!prop) {
1248 error_setg(errp, "Property '.%s' not found", name);
1250 return prop;
1253 void object_property_del(Object *obj, const char *name, Error **errp)
1255 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1257 if (!prop) {
1258 error_setg(errp, "Property '.%s' not found", name);
1259 return;
1262 if (prop->release) {
1263 prop->release(obj, name, prop->opaque);
1265 g_hash_table_remove(obj->properties, name);
1268 void object_property_get(Object *obj, Visitor *v, const char *name,
1269 Error **errp)
1271 ObjectProperty *prop = object_property_find(obj, name, errp);
1272 if (prop == NULL) {
1273 return;
1276 if (!prop->get) {
1277 error_setg(errp, QERR_PERMISSION_DENIED);
1278 } else {
1279 prop->get(obj, v, name, prop->opaque, errp);
1283 void object_property_set(Object *obj, Visitor *v, const char *name,
1284 Error **errp)
1286 ObjectProperty *prop = object_property_find(obj, name, errp);
1287 if (prop == NULL) {
1288 return;
1291 if (!prop->set) {
1292 error_setg(errp, QERR_PERMISSION_DENIED);
1293 } else {
1294 prop->set(obj, v, name, prop->opaque, errp);
1298 void object_property_set_str(Object *obj, const char *value,
1299 const char *name, Error **errp)
1301 QString *qstr = qstring_from_str(value);
1302 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1304 qobject_unref(qstr);
1307 char *object_property_get_str(Object *obj, const char *name,
1308 Error **errp)
1310 QObject *ret = object_property_get_qobject(obj, name, errp);
1311 char *retval;
1313 if (!ret) {
1314 return NULL;
1317 retval = g_strdup(qobject_get_try_str(ret));
1318 if (!retval) {
1319 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1322 qobject_unref(ret);
1323 return retval;
1326 void object_property_set_link(Object *obj, Object *value,
1327 const char *name, Error **errp)
1329 if (value) {
1330 gchar *path = object_get_canonical_path(value);
1331 object_property_set_str(obj, path, name, errp);
1332 g_free(path);
1333 } else {
1334 object_property_set_str(obj, "", name, errp);
1338 Object *object_property_get_link(Object *obj, const char *name,
1339 Error **errp)
1341 char *str = object_property_get_str(obj, name, errp);
1342 Object *target = NULL;
1344 if (str && *str) {
1345 target = object_resolve_path(str, NULL);
1346 if (!target) {
1347 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1348 "Device '%s' not found", str);
1352 g_free(str);
1353 return target;
1356 void object_property_set_bool(Object *obj, bool value,
1357 const char *name, Error **errp)
1359 QBool *qbool = qbool_from_bool(value);
1360 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1362 qobject_unref(qbool);
1365 bool object_property_get_bool(Object *obj, const char *name,
1366 Error **errp)
1368 QObject *ret = object_property_get_qobject(obj, name, errp);
1369 QBool *qbool;
1370 bool retval;
1372 if (!ret) {
1373 return false;
1375 qbool = qobject_to(QBool, ret);
1376 if (!qbool) {
1377 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1378 retval = false;
1379 } else {
1380 retval = qbool_get_bool(qbool);
1383 qobject_unref(ret);
1384 return retval;
1387 void object_property_set_int(Object *obj, int64_t value,
1388 const char *name, Error **errp)
1390 QNum *qnum = qnum_from_int(value);
1391 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1393 qobject_unref(qnum);
1396 int64_t object_property_get_int(Object *obj, const char *name,
1397 Error **errp)
1399 QObject *ret = object_property_get_qobject(obj, name, errp);
1400 QNum *qnum;
1401 int64_t retval;
1403 if (!ret) {
1404 return -1;
1407 qnum = qobject_to(QNum, ret);
1408 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1409 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1410 retval = -1;
1413 qobject_unref(ret);
1414 return retval;
1417 void object_property_set_uint(Object *obj, uint64_t value,
1418 const char *name, Error **errp)
1420 QNum *qnum = qnum_from_uint(value);
1422 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1423 qobject_unref(qnum);
1426 uint64_t object_property_get_uint(Object *obj, const char *name,
1427 Error **errp)
1429 QObject *ret = object_property_get_qobject(obj, name, errp);
1430 QNum *qnum;
1431 uint64_t retval;
1433 if (!ret) {
1434 return 0;
1436 qnum = qobject_to(QNum, ret);
1437 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1438 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1439 retval = 0;
1442 qobject_unref(ret);
1443 return retval;
1446 typedef struct EnumProperty {
1447 const QEnumLookup *lookup;
1448 int (*get)(Object *, Error **);
1449 void (*set)(Object *, int, Error **);
1450 } EnumProperty;
1452 int object_property_get_enum(Object *obj, const char *name,
1453 const char *typename, Error **errp)
1455 Error *err = NULL;
1456 Visitor *v;
1457 char *str;
1458 int ret;
1459 ObjectProperty *prop = object_property_find(obj, name, errp);
1460 EnumProperty *enumprop;
1462 if (prop == NULL) {
1463 return 0;
1466 if (!g_str_equal(prop->type, typename)) {
1467 error_setg(errp, "Property %s on %s is not '%s' enum type",
1468 name, object_class_get_name(
1469 object_get_class(obj)), typename);
1470 return 0;
1473 enumprop = prop->opaque;
1475 v = string_output_visitor_new(false, &str);
1476 object_property_get(obj, v, name, &err);
1477 if (err) {
1478 error_propagate(errp, err);
1479 visit_free(v);
1480 return 0;
1482 visit_complete(v, &str);
1483 visit_free(v);
1484 v = string_input_visitor_new(str);
1485 visit_type_enum(v, name, &ret, enumprop->lookup, errp);
1487 g_free(str);
1488 visit_free(v);
1490 return ret;
1493 void object_property_get_uint16List(Object *obj, const char *name,
1494 uint16List **list, Error **errp)
1496 Error *err = NULL;
1497 Visitor *v;
1498 char *str;
1500 v = string_output_visitor_new(false, &str);
1501 object_property_get(obj, v, name, &err);
1502 if (err) {
1503 error_propagate(errp, err);
1504 goto out;
1506 visit_complete(v, &str);
1507 visit_free(v);
1508 v = string_input_visitor_new(str);
1509 visit_type_uint16List(v, NULL, list, errp);
1511 g_free(str);
1512 out:
1513 visit_free(v);
1516 void object_property_parse(Object *obj, const char *string,
1517 const char *name, Error **errp)
1519 Visitor *v = string_input_visitor_new(string);
1520 object_property_set(obj, v, name, errp);
1521 visit_free(v);
1524 char *object_property_print(Object *obj, const char *name, bool human,
1525 Error **errp)
1527 Visitor *v;
1528 char *string = NULL;
1529 Error *local_err = NULL;
1531 v = string_output_visitor_new(human, &string);
1532 object_property_get(obj, v, name, &local_err);
1533 if (local_err) {
1534 error_propagate(errp, local_err);
1535 goto out;
1538 visit_complete(v, &string);
1540 out:
1541 visit_free(v);
1542 return string;
1545 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1547 ObjectProperty *prop = object_property_find(obj, name, errp);
1548 if (prop == NULL) {
1549 return NULL;
1552 return prop->type;
1555 Object *object_get_root(void)
1557 static Object *root;
1559 if (!root) {
1560 root = object_new("container");
1563 return root;
1566 Object *object_get_objects_root(void)
1568 return container_get(object_get_root(), "/objects");
1571 Object *object_get_internal_root(void)
1573 static Object *internal_root;
1575 if (!internal_root) {
1576 internal_root = object_new("container");
1579 return internal_root;
1582 static void object_get_child_property(Object *obj, Visitor *v,
1583 const char *name, void *opaque,
1584 Error **errp)
1586 Object *child = opaque;
1587 gchar *path;
1589 path = object_get_canonical_path(child);
1590 visit_type_str(v, name, &path, errp);
1591 g_free(path);
1594 static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1596 return opaque;
1599 static void object_finalize_child_property(Object *obj, const char *name,
1600 void *opaque)
1602 Object *child = opaque;
1604 if (child->class->unparent) {
1605 (child->class->unparent)(child);
1607 child->parent = NULL;
1608 object_unref(child);
1611 void object_property_add_child(Object *obj, const char *name,
1612 Object *child, Error **errp)
1614 Error *local_err = NULL;
1615 gchar *type;
1616 ObjectProperty *op;
1618 if (child->parent != NULL) {
1619 error_setg(errp, "child object is already parented");
1620 return;
1623 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1625 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1626 object_finalize_child_property, child, &local_err);
1627 if (local_err) {
1628 error_propagate(errp, local_err);
1629 goto out;
1632 op->resolve = object_resolve_child_property;
1633 object_ref(child);
1634 child->parent = obj;
1636 out:
1637 g_free(type);
1640 void object_property_allow_set_link(const Object *obj, const char *name,
1641 Object *val, Error **errp)
1643 /* Allow the link to be set, always */
1646 typedef struct {
1647 Object **child;
1648 void (*check)(const Object *, const char *, Object *, Error **);
1649 ObjectPropertyLinkFlags flags;
1650 } LinkProperty;
1652 static void object_get_link_property(Object *obj, Visitor *v,
1653 const char *name, void *opaque,
1654 Error **errp)
1656 LinkProperty *lprop = opaque;
1657 Object **child = lprop->child;
1658 gchar *path;
1660 if (*child) {
1661 path = object_get_canonical_path(*child);
1662 visit_type_str(v, name, &path, errp);
1663 g_free(path);
1664 } else {
1665 path = (gchar *)"";
1666 visit_type_str(v, name, &path, errp);
1671 * object_resolve_link:
1673 * Lookup an object and ensure its type matches the link property type. This
1674 * is similar to object_resolve_path() except type verification against the
1675 * link property is performed.
1677 * Returns: The matched object or NULL on path lookup failures.
1679 static Object *object_resolve_link(Object *obj, const char *name,
1680 const char *path, Error **errp)
1682 const char *type;
1683 gchar *target_type;
1684 bool ambiguous = false;
1685 Object *target;
1687 /* Go from link<FOO> to FOO. */
1688 type = object_property_get_type(obj, name, NULL);
1689 target_type = g_strndup(&type[5], strlen(type) - 6);
1690 target = object_resolve_path_type(path, target_type, &ambiguous);
1692 if (ambiguous) {
1693 error_setg(errp, "Path '%s' does not uniquely identify an object",
1694 path);
1695 } else if (!target) {
1696 target = object_resolve_path(path, &ambiguous);
1697 if (target || ambiguous) {
1698 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1699 } else {
1700 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1701 "Device '%s' not found", path);
1703 target = NULL;
1705 g_free(target_type);
1707 return target;
1710 static void object_set_link_property(Object *obj, Visitor *v,
1711 const char *name, void *opaque,
1712 Error **errp)
1714 Error *local_err = NULL;
1715 LinkProperty *prop = opaque;
1716 Object **child = prop->child;
1717 Object *old_target = *child;
1718 Object *new_target = NULL;
1719 char *path = NULL;
1721 visit_type_str(v, name, &path, &local_err);
1723 if (!local_err && strcmp(path, "") != 0) {
1724 new_target = object_resolve_link(obj, name, path, &local_err);
1727 g_free(path);
1728 if (local_err) {
1729 error_propagate(errp, local_err);
1730 return;
1733 prop->check(obj, name, new_target, &local_err);
1734 if (local_err) {
1735 error_propagate(errp, local_err);
1736 return;
1739 *child = new_target;
1740 if (prop->flags == OBJ_PROP_LINK_STRONG) {
1741 object_ref(new_target);
1742 object_unref(old_target);
1746 static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1748 LinkProperty *lprop = opaque;
1750 return *lprop->child;
1753 static void object_release_link_property(Object *obj, const char *name,
1754 void *opaque)
1756 LinkProperty *prop = opaque;
1758 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *prop->child) {
1759 object_unref(*prop->child);
1761 g_free(prop);
1764 void object_property_add_link(Object *obj, const char *name,
1765 const char *type, Object **child,
1766 void (*check)(const Object *, const char *,
1767 Object *, Error **),
1768 ObjectPropertyLinkFlags flags,
1769 Error **errp)
1771 Error *local_err = NULL;
1772 LinkProperty *prop = g_malloc(sizeof(*prop));
1773 gchar *full_type;
1774 ObjectProperty *op;
1776 prop->child = child;
1777 prop->check = check;
1778 prop->flags = flags;
1780 full_type = g_strdup_printf("link<%s>", type);
1782 op = object_property_add(obj, name, full_type,
1783 object_get_link_property,
1784 check ? object_set_link_property : NULL,
1785 object_release_link_property,
1786 prop,
1787 &local_err);
1788 if (local_err) {
1789 error_propagate(errp, local_err);
1790 g_free(prop);
1791 goto out;
1794 op->resolve = object_resolve_link_property;
1796 out:
1797 g_free(full_type);
1800 void object_property_add_const_link(Object *obj, const char *name,
1801 Object *target, Error **errp)
1803 char *link_type;
1804 ObjectProperty *op;
1806 link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1807 op = object_property_add(obj, name, link_type,
1808 object_get_child_property, NULL,
1809 NULL, target, errp);
1810 if (op != NULL) {
1811 op->resolve = object_resolve_child_property;
1813 g_free(link_type);
1816 gchar *object_get_canonical_path_component(Object *obj)
1818 ObjectProperty *prop = NULL;
1819 GHashTableIter iter;
1821 if (obj->parent == NULL) {
1822 return NULL;
1825 g_hash_table_iter_init(&iter, obj->parent->properties);
1826 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1827 if (!object_property_is_child(prop)) {
1828 continue;
1831 if (prop->opaque == obj) {
1832 return g_strdup(prop->name);
1836 /* obj had a parent but was not a child, should never happen */
1837 g_assert_not_reached();
1838 return NULL;
1841 gchar *object_get_canonical_path(Object *obj)
1843 Object *root = object_get_root();
1844 char *newpath, *path = NULL;
1846 if (obj == root) {
1847 return g_strdup("/");
1850 do {
1851 char *component = object_get_canonical_path_component(obj);
1853 if (!component) {
1854 /* A canonical path must be complete, so discard what was
1855 * collected so far.
1857 g_free(path);
1858 return NULL;
1861 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1862 g_free(path);
1863 g_free(component);
1864 path = newpath;
1865 obj = obj->parent;
1866 } while (obj != root);
1868 return path;
1871 Object *object_resolve_path_component(Object *parent, const gchar *part)
1873 ObjectProperty *prop = object_property_find(parent, part, NULL);
1874 if (prop == NULL) {
1875 return NULL;
1878 if (prop->resolve) {
1879 return prop->resolve(parent, prop->opaque, part);
1880 } else {
1881 return NULL;
1885 static Object *object_resolve_abs_path(Object *parent,
1886 gchar **parts,
1887 const char *typename,
1888 int index)
1890 Object *child;
1892 if (parts[index] == NULL) {
1893 return object_dynamic_cast(parent, typename);
1896 if (strcmp(parts[index], "") == 0) {
1897 return object_resolve_abs_path(parent, parts, typename, index + 1);
1900 child = object_resolve_path_component(parent, parts[index]);
1901 if (!child) {
1902 return NULL;
1905 return object_resolve_abs_path(child, parts, typename, index + 1);
1908 static Object *object_resolve_partial_path(Object *parent,
1909 gchar **parts,
1910 const char *typename,
1911 bool *ambiguous)
1913 Object *obj;
1914 GHashTableIter iter;
1915 ObjectProperty *prop;
1917 obj = object_resolve_abs_path(parent, parts, typename, 0);
1919 g_hash_table_iter_init(&iter, parent->properties);
1920 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1921 Object *found;
1923 if (!object_property_is_child(prop)) {
1924 continue;
1927 found = object_resolve_partial_path(prop->opaque, parts,
1928 typename, ambiguous);
1929 if (found) {
1930 if (obj) {
1931 *ambiguous = true;
1932 return NULL;
1934 obj = found;
1937 if (*ambiguous) {
1938 return NULL;
1942 return obj;
1945 Object *object_resolve_path_type(const char *path, const char *typename,
1946 bool *ambiguousp)
1948 Object *obj;
1949 gchar **parts;
1951 parts = g_strsplit(path, "/", 0);
1952 assert(parts);
1954 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
1955 bool ambiguous = false;
1956 obj = object_resolve_partial_path(object_get_root(), parts,
1957 typename, &ambiguous);
1958 if (ambiguousp) {
1959 *ambiguousp = ambiguous;
1961 } else {
1962 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1965 g_strfreev(parts);
1967 return obj;
1970 Object *object_resolve_path(const char *path, bool *ambiguous)
1972 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1975 typedef struct StringProperty
1977 char *(*get)(Object *, Error **);
1978 void (*set)(Object *, const char *, Error **);
1979 } StringProperty;
1981 static void property_get_str(Object *obj, Visitor *v, const char *name,
1982 void *opaque, Error **errp)
1984 StringProperty *prop = opaque;
1985 char *value;
1986 Error *err = NULL;
1988 value = prop->get(obj, &err);
1989 if (err) {
1990 error_propagate(errp, err);
1991 return;
1994 visit_type_str(v, name, &value, errp);
1995 g_free(value);
1998 static void property_set_str(Object *obj, Visitor *v, const char *name,
1999 void *opaque, Error **errp)
2001 StringProperty *prop = opaque;
2002 char *value;
2003 Error *local_err = NULL;
2005 visit_type_str(v, name, &value, &local_err);
2006 if (local_err) {
2007 error_propagate(errp, local_err);
2008 return;
2011 prop->set(obj, value, errp);
2012 g_free(value);
2015 static void property_release_str(Object *obj, const char *name,
2016 void *opaque)
2018 StringProperty *prop = opaque;
2019 g_free(prop);
2022 void object_property_add_str(Object *obj, const char *name,
2023 char *(*get)(Object *, Error **),
2024 void (*set)(Object *, const char *, Error **),
2025 Error **errp)
2027 Error *local_err = NULL;
2028 StringProperty *prop = g_malloc0(sizeof(*prop));
2030 prop->get = get;
2031 prop->set = set;
2033 object_property_add(obj, name, "string",
2034 get ? property_get_str : NULL,
2035 set ? property_set_str : NULL,
2036 property_release_str,
2037 prop, &local_err);
2038 if (local_err) {
2039 error_propagate(errp, local_err);
2040 g_free(prop);
2044 void object_class_property_add_str(ObjectClass *klass, const char *name,
2045 char *(*get)(Object *, Error **),
2046 void (*set)(Object *, const char *,
2047 Error **),
2048 Error **errp)
2050 Error *local_err = NULL;
2051 StringProperty *prop = g_malloc0(sizeof(*prop));
2053 prop->get = get;
2054 prop->set = set;
2056 object_class_property_add(klass, name, "string",
2057 get ? property_get_str : NULL,
2058 set ? property_set_str : NULL,
2059 property_release_str,
2060 prop, &local_err);
2061 if (local_err) {
2062 error_propagate(errp, local_err);
2063 g_free(prop);
2067 typedef struct BoolProperty
2069 bool (*get)(Object *, Error **);
2070 void (*set)(Object *, bool, Error **);
2071 } BoolProperty;
2073 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2074 void *opaque, Error **errp)
2076 BoolProperty *prop = opaque;
2077 bool value;
2078 Error *err = NULL;
2080 value = prop->get(obj, &err);
2081 if (err) {
2082 error_propagate(errp, err);
2083 return;
2086 visit_type_bool(v, name, &value, errp);
2089 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2090 void *opaque, Error **errp)
2092 BoolProperty *prop = opaque;
2093 bool value;
2094 Error *local_err = NULL;
2096 visit_type_bool(v, name, &value, &local_err);
2097 if (local_err) {
2098 error_propagate(errp, local_err);
2099 return;
2102 prop->set(obj, value, errp);
2105 static void property_release_bool(Object *obj, const char *name,
2106 void *opaque)
2108 BoolProperty *prop = opaque;
2109 g_free(prop);
2112 void object_property_add_bool(Object *obj, const char *name,
2113 bool (*get)(Object *, Error **),
2114 void (*set)(Object *, bool, Error **),
2115 Error **errp)
2117 Error *local_err = NULL;
2118 BoolProperty *prop = g_malloc0(sizeof(*prop));
2120 prop->get = get;
2121 prop->set = set;
2123 object_property_add(obj, name, "bool",
2124 get ? property_get_bool : NULL,
2125 set ? property_set_bool : NULL,
2126 property_release_bool,
2127 prop, &local_err);
2128 if (local_err) {
2129 error_propagate(errp, local_err);
2130 g_free(prop);
2134 void object_class_property_add_bool(ObjectClass *klass, const char *name,
2135 bool (*get)(Object *, Error **),
2136 void (*set)(Object *, bool, Error **),
2137 Error **errp)
2139 Error *local_err = NULL;
2140 BoolProperty *prop = g_malloc0(sizeof(*prop));
2142 prop->get = get;
2143 prop->set = set;
2145 object_class_property_add(klass, name, "bool",
2146 get ? property_get_bool : NULL,
2147 set ? property_set_bool : NULL,
2148 property_release_bool,
2149 prop, &local_err);
2150 if (local_err) {
2151 error_propagate(errp, local_err);
2152 g_free(prop);
2156 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2157 void *opaque, Error **errp)
2159 EnumProperty *prop = opaque;
2160 int value;
2161 Error *err = NULL;
2163 value = prop->get(obj, &err);
2164 if (err) {
2165 error_propagate(errp, err);
2166 return;
2169 visit_type_enum(v, name, &value, prop->lookup, errp);
2172 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2173 void *opaque, Error **errp)
2175 EnumProperty *prop = opaque;
2176 int value;
2177 Error *err = NULL;
2179 visit_type_enum(v, name, &value, prop->lookup, &err);
2180 if (err) {
2181 error_propagate(errp, err);
2182 return;
2184 prop->set(obj, value, errp);
2187 static void property_release_enum(Object *obj, const char *name,
2188 void *opaque)
2190 EnumProperty *prop = opaque;
2191 g_free(prop);
2194 void object_property_add_enum(Object *obj, const char *name,
2195 const char *typename,
2196 const QEnumLookup *lookup,
2197 int (*get)(Object *, Error **),
2198 void (*set)(Object *, int, Error **),
2199 Error **errp)
2201 Error *local_err = NULL;
2202 EnumProperty *prop = g_malloc(sizeof(*prop));
2204 prop->lookup = lookup;
2205 prop->get = get;
2206 prop->set = set;
2208 object_property_add(obj, name, typename,
2209 get ? property_get_enum : NULL,
2210 set ? property_set_enum : NULL,
2211 property_release_enum,
2212 prop, &local_err);
2213 if (local_err) {
2214 error_propagate(errp, local_err);
2215 g_free(prop);
2219 void object_class_property_add_enum(ObjectClass *klass, const char *name,
2220 const char *typename,
2221 const QEnumLookup *lookup,
2222 int (*get)(Object *, Error **),
2223 void (*set)(Object *, int, Error **),
2224 Error **errp)
2226 Error *local_err = NULL;
2227 EnumProperty *prop = g_malloc(sizeof(*prop));
2229 prop->lookup = lookup;
2230 prop->get = get;
2231 prop->set = set;
2233 object_class_property_add(klass, name, typename,
2234 get ? property_get_enum : NULL,
2235 set ? property_set_enum : NULL,
2236 property_release_enum,
2237 prop, &local_err);
2238 if (local_err) {
2239 error_propagate(errp, local_err);
2240 g_free(prop);
2244 typedef struct TMProperty {
2245 void (*get)(Object *, struct tm *, Error **);
2246 } TMProperty;
2248 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2249 void *opaque, Error **errp)
2251 TMProperty *prop = opaque;
2252 Error *err = NULL;
2253 struct tm value;
2255 prop->get(obj, &value, &err);
2256 if (err) {
2257 goto out;
2260 visit_start_struct(v, name, NULL, 0, &err);
2261 if (err) {
2262 goto out;
2264 visit_type_int32(v, "tm_year", &value.tm_year, &err);
2265 if (err) {
2266 goto out_end;
2268 visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
2269 if (err) {
2270 goto out_end;
2272 visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
2273 if (err) {
2274 goto out_end;
2276 visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
2277 if (err) {
2278 goto out_end;
2280 visit_type_int32(v, "tm_min", &value.tm_min, &err);
2281 if (err) {
2282 goto out_end;
2284 visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
2285 if (err) {
2286 goto out_end;
2288 visit_check_struct(v, &err);
2289 out_end:
2290 visit_end_struct(v, NULL);
2291 out:
2292 error_propagate(errp, err);
2296 static void property_release_tm(Object *obj, const char *name,
2297 void *opaque)
2299 TMProperty *prop = opaque;
2300 g_free(prop);
2303 void object_property_add_tm(Object *obj, const char *name,
2304 void (*get)(Object *, struct tm *, Error **),
2305 Error **errp)
2307 Error *local_err = NULL;
2308 TMProperty *prop = g_malloc0(sizeof(*prop));
2310 prop->get = get;
2312 object_property_add(obj, name, "struct tm",
2313 get ? property_get_tm : NULL, NULL,
2314 property_release_tm,
2315 prop, &local_err);
2316 if (local_err) {
2317 error_propagate(errp, local_err);
2318 g_free(prop);
2322 void object_class_property_add_tm(ObjectClass *klass, const char *name,
2323 void (*get)(Object *, struct tm *, Error **),
2324 Error **errp)
2326 Error *local_err = NULL;
2327 TMProperty *prop = g_malloc0(sizeof(*prop));
2329 prop->get = get;
2331 object_class_property_add(klass, name, "struct tm",
2332 get ? property_get_tm : NULL, NULL,
2333 property_release_tm,
2334 prop, &local_err);
2335 if (local_err) {
2336 error_propagate(errp, local_err);
2337 g_free(prop);
2341 static char *qdev_get_type(Object *obj, Error **errp)
2343 return g_strdup(object_get_typename(obj));
2346 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2347 void *opaque, Error **errp)
2349 uint8_t value = *(uint8_t *)opaque;
2350 visit_type_uint8(v, name, &value, errp);
2353 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2354 void *opaque, Error **errp)
2356 uint16_t value = *(uint16_t *)opaque;
2357 visit_type_uint16(v, name, &value, errp);
2360 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2361 void *opaque, Error **errp)
2363 uint32_t value = *(uint32_t *)opaque;
2364 visit_type_uint32(v, name, &value, errp);
2367 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2368 void *opaque, Error **errp)
2370 uint64_t value = *(uint64_t *)opaque;
2371 visit_type_uint64(v, name, &value, errp);
2374 void object_property_add_uint8_ptr(Object *obj, const char *name,
2375 const uint8_t *v, Error **errp)
2377 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
2378 NULL, NULL, (void *)v, errp);
2381 void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2382 const uint8_t *v, Error **errp)
2384 object_class_property_add(klass, name, "uint8", property_get_uint8_ptr,
2385 NULL, NULL, (void *)v, errp);
2388 void object_property_add_uint16_ptr(Object *obj, const char *name,
2389 const uint16_t *v, Error **errp)
2391 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
2392 NULL, NULL, (void *)v, errp);
2395 void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2396 const uint16_t *v, Error **errp)
2398 object_class_property_add(klass, name, "uint16", property_get_uint16_ptr,
2399 NULL, NULL, (void *)v, errp);
2402 void object_property_add_uint32_ptr(Object *obj, const char *name,
2403 const uint32_t *v, Error **errp)
2405 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
2406 NULL, NULL, (void *)v, errp);
2409 void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2410 const uint32_t *v, Error **errp)
2412 object_class_property_add(klass, name, "uint32", property_get_uint32_ptr,
2413 NULL, NULL, (void *)v, errp);
2416 void object_property_add_uint64_ptr(Object *obj, const char *name,
2417 const uint64_t *v, Error **errp)
2419 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
2420 NULL, NULL, (void *)v, errp);
2423 void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2424 const uint64_t *v, Error **errp)
2426 object_class_property_add(klass, name, "uint64", property_get_uint64_ptr,
2427 NULL, NULL, (void *)v, errp);
2430 typedef struct {
2431 Object *target_obj;
2432 char *target_name;
2433 } AliasProperty;
2435 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2436 void *opaque, Error **errp)
2438 AliasProperty *prop = opaque;
2440 object_property_get(prop->target_obj, v, prop->target_name, errp);
2443 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2444 void *opaque, Error **errp)
2446 AliasProperty *prop = opaque;
2448 object_property_set(prop->target_obj, v, prop->target_name, errp);
2451 static Object *property_resolve_alias(Object *obj, void *opaque,
2452 const gchar *part)
2454 AliasProperty *prop = opaque;
2456 return object_resolve_path_component(prop->target_obj, prop->target_name);
2459 static void property_release_alias(Object *obj, const char *name, void *opaque)
2461 AliasProperty *prop = opaque;
2463 g_free(prop->target_name);
2464 g_free(prop);
2467 void object_property_add_alias(Object *obj, const char *name,
2468 Object *target_obj, const char *target_name,
2469 Error **errp)
2471 AliasProperty *prop;
2472 ObjectProperty *op;
2473 ObjectProperty *target_prop;
2474 gchar *prop_type;
2475 Error *local_err = NULL;
2477 target_prop = object_property_find(target_obj, target_name, errp);
2478 if (!target_prop) {
2479 return;
2482 if (object_property_is_child(target_prop)) {
2483 prop_type = g_strdup_printf("link%s",
2484 target_prop->type + strlen("child"));
2485 } else {
2486 prop_type = g_strdup(target_prop->type);
2489 prop = g_malloc(sizeof(*prop));
2490 prop->target_obj = target_obj;
2491 prop->target_name = g_strdup(target_name);
2493 op = object_property_add(obj, name, prop_type,
2494 property_get_alias,
2495 property_set_alias,
2496 property_release_alias,
2497 prop, &local_err);
2498 if (local_err) {
2499 error_propagate(errp, local_err);
2500 g_free(prop);
2501 goto out;
2503 op->resolve = property_resolve_alias;
2505 object_property_set_description(obj, op->name,
2506 target_prop->description,
2507 &error_abort);
2509 out:
2510 g_free(prop_type);
2513 void object_property_set_description(Object *obj, const char *name,
2514 const char *description, Error **errp)
2516 ObjectProperty *op;
2518 op = object_property_find(obj, name, errp);
2519 if (!op) {
2520 return;
2523 g_free(op->description);
2524 op->description = g_strdup(description);
2527 void object_class_property_set_description(ObjectClass *klass,
2528 const char *name,
2529 const char *description,
2530 Error **errp)
2532 ObjectProperty *op;
2534 op = g_hash_table_lookup(klass->properties, name);
2535 if (!op) {
2536 error_setg(errp, "Property '.%s' not found", name);
2537 return;
2540 g_free(op->description);
2541 op->description = g_strdup(description);
2544 static void object_class_init(ObjectClass *klass, void *data)
2546 object_class_property_add_str(klass, "type", qdev_get_type,
2547 NULL, &error_abort);
2550 static void register_types(void)
2552 static TypeInfo interface_info = {
2553 .name = TYPE_INTERFACE,
2554 .class_size = sizeof(InterfaceClass),
2555 .abstract = true,
2558 static TypeInfo object_info = {
2559 .name = TYPE_OBJECT,
2560 .instance_size = sizeof(Object),
2561 .class_init = object_class_init,
2562 .abstract = true,
2565 type_interface = type_register_internal(&interface_info);
2566 type_register_internal(&object_info);
2569 type_init(register_types)