usb-mtp: add sanity checks on rootdir
[qemu/ar7.git] / qom / object.c
blob6fa9c619fac4956c89d45b9a691f0af9c49fc128
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 * Each is a GPtrArray of of GlobalProperty.
418 * Applied in order, later entries override earlier ones.
420 static GPtrArray *object_compat_props[2];
423 * Set machine's global property defaults to @compat_props.
424 * May be called at most once.
426 void object_set_machine_compat_props(GPtrArray *compat_props)
428 assert(!object_compat_props[1]);
429 object_compat_props[1] = compat_props;
433 * Set accelerator's global property defaults to @compat_props.
434 * May be called at most once.
436 void object_set_accelerator_compat_props(GPtrArray *compat_props)
438 assert(!object_compat_props[0]);
439 object_compat_props[0] = compat_props;
442 void object_apply_compat_props(Object *obj)
444 int i;
446 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
447 object_apply_global_props(obj, object_compat_props[i],
448 &error_abort);
452 static void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
454 Object *obj = data;
456 type_initialize(type);
458 g_assert(type->instance_size >= sizeof(Object));
459 g_assert(type->abstract == false);
460 g_assert(size >= type->instance_size);
462 memset(obj, 0, type->instance_size);
463 obj->class = type->class;
464 object_ref(obj);
465 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
466 NULL, object_property_free);
467 object_init_with_type(obj, type);
468 object_post_init_with_type(obj, type);
471 void object_initialize(void *data, size_t size, const char *typename)
473 TypeImpl *type = type_get_by_name(typename);
475 if (!type) {
476 error_report("missing object type '%s'", typename);
477 abort();
480 object_initialize_with_type(data, size, type);
483 void object_initialize_child(Object *parentobj, const char *propname,
484 void *childobj, size_t size, const char *type,
485 Error **errp, ...)
487 va_list vargs;
489 va_start(vargs, errp);
490 object_initialize_childv(parentobj, propname, childobj, size, type, errp,
491 vargs);
492 va_end(vargs);
495 void object_initialize_childv(Object *parentobj, const char *propname,
496 void *childobj, size_t size, const char *type,
497 Error **errp, va_list vargs)
499 Error *local_err = NULL;
500 Object *obj;
501 UserCreatable *uc;
503 object_initialize(childobj, size, type);
504 obj = OBJECT(childobj);
506 object_set_propv(obj, &local_err, vargs);
507 if (local_err) {
508 goto out;
511 object_property_add_child(parentobj, propname, obj, &local_err);
512 if (local_err) {
513 goto out;
516 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
517 if (uc) {
518 user_creatable_complete(uc, &local_err);
519 if (local_err) {
520 object_unparent(obj);
521 goto out;
526 * Since object_property_add_child added a reference to the child object,
527 * we can drop the reference added by object_initialize(), so the child
528 * property will own the only reference to the object.
530 object_unref(obj);
532 out:
533 if (local_err) {
534 error_propagate(errp, local_err);
535 object_unref(obj);
539 static inline bool object_property_is_child(ObjectProperty *prop)
541 return strstart(prop->type, "child<", NULL);
544 static void object_property_del_all(Object *obj)
546 ObjectProperty *prop;
547 GHashTableIter iter;
548 gpointer key, value;
549 bool released;
551 do {
552 released = false;
553 g_hash_table_iter_init(&iter, obj->properties);
554 while (g_hash_table_iter_next(&iter, &key, &value)) {
555 prop = value;
556 if (prop->release) {
557 prop->release(obj, prop->name, prop->opaque);
558 prop->release = NULL;
559 released = true;
560 break;
562 g_hash_table_iter_remove(&iter);
564 } while (released);
566 g_hash_table_unref(obj->properties);
569 static void object_property_del_child(Object *obj, Object *child, Error **errp)
571 ObjectProperty *prop;
572 GHashTableIter iter;
573 gpointer key, value;
575 g_hash_table_iter_init(&iter, obj->properties);
576 while (g_hash_table_iter_next(&iter, &key, &value)) {
577 prop = value;
578 if (object_property_is_child(prop) && prop->opaque == child) {
579 if (prop->release) {
580 prop->release(obj, prop->name, prop->opaque);
581 prop->release = NULL;
583 break;
586 g_hash_table_iter_init(&iter, obj->properties);
587 while (g_hash_table_iter_next(&iter, &key, &value)) {
588 prop = value;
589 if (object_property_is_child(prop) && prop->opaque == child) {
590 g_hash_table_iter_remove(&iter);
591 break;
596 void object_unparent(Object *obj)
598 if (obj->parent) {
599 object_property_del_child(obj->parent, obj, NULL);
603 static void object_deinit(Object *obj, TypeImpl *type)
605 if (type->instance_finalize) {
606 type->instance_finalize(obj);
609 if (type_has_parent(type)) {
610 object_deinit(obj, type_get_parent(type));
614 static void object_finalize(void *data)
616 Object *obj = data;
617 TypeImpl *ti = obj->class->type;
619 object_property_del_all(obj);
620 object_deinit(obj, ti);
622 g_assert(obj->ref == 0);
623 if (obj->free) {
624 obj->free(obj);
628 static Object *object_new_with_type(Type type)
630 Object *obj;
632 g_assert(type != NULL);
633 type_initialize(type);
635 obj = g_malloc(type->instance_size);
636 object_initialize_with_type(obj, type->instance_size, type);
637 obj->free = g_free;
639 return obj;
642 Object *object_new(const char *typename)
644 TypeImpl *ti = type_get_by_name(typename);
646 return object_new_with_type(ti);
650 Object *object_new_with_props(const char *typename,
651 Object *parent,
652 const char *id,
653 Error **errp,
654 ...)
656 va_list vargs;
657 Object *obj;
659 va_start(vargs, errp);
660 obj = object_new_with_propv(typename, parent, id, errp, vargs);
661 va_end(vargs);
663 return obj;
667 Object *object_new_with_propv(const char *typename,
668 Object *parent,
669 const char *id,
670 Error **errp,
671 va_list vargs)
673 Object *obj;
674 ObjectClass *klass;
675 Error *local_err = NULL;
676 UserCreatable *uc;
678 klass = object_class_by_name(typename);
679 if (!klass) {
680 error_setg(errp, "invalid object type: %s", typename);
681 return NULL;
684 if (object_class_is_abstract(klass)) {
685 error_setg(errp, "object type '%s' is abstract", typename);
686 return NULL;
688 obj = object_new_with_type(klass->type);
690 if (object_set_propv(obj, &local_err, vargs) < 0) {
691 goto error;
694 if (id != NULL) {
695 object_property_add_child(parent, id, obj, &local_err);
696 if (local_err) {
697 goto error;
701 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
702 if (uc) {
703 user_creatable_complete(uc, &local_err);
704 if (local_err) {
705 if (id != NULL) {
706 object_unparent(obj);
708 goto error;
712 object_unref(OBJECT(obj));
713 return obj;
715 error:
716 error_propagate(errp, local_err);
717 object_unref(obj);
718 return NULL;
722 int object_set_props(Object *obj,
723 Error **errp,
724 ...)
726 va_list vargs;
727 int ret;
729 va_start(vargs, errp);
730 ret = object_set_propv(obj, errp, vargs);
731 va_end(vargs);
733 return ret;
737 int object_set_propv(Object *obj,
738 Error **errp,
739 va_list vargs)
741 const char *propname;
742 Error *local_err = NULL;
744 propname = va_arg(vargs, char *);
745 while (propname != NULL) {
746 const char *value = va_arg(vargs, char *);
748 g_assert(value != NULL);
749 object_property_parse(obj, value, propname, &local_err);
750 if (local_err) {
751 error_propagate(errp, local_err);
752 return -1;
754 propname = va_arg(vargs, char *);
757 return 0;
761 Object *object_dynamic_cast(Object *obj, const char *typename)
763 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
764 return obj;
767 return NULL;
770 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
771 const char *file, int line, const char *func)
773 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
774 typename, file, line, func);
776 #ifdef CONFIG_QOM_CAST_DEBUG
777 int i;
778 Object *inst;
780 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
781 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
782 goto out;
786 inst = object_dynamic_cast(obj, typename);
788 if (!inst && obj) {
789 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
790 file, line, func, obj, typename);
791 abort();
794 assert(obj == inst);
796 if (obj && obj == inst) {
797 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
798 atomic_set(&obj->class->object_cast_cache[i - 1],
799 atomic_read(&obj->class->object_cast_cache[i]));
801 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
804 out:
805 #endif
806 return obj;
809 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
810 const char *typename)
812 ObjectClass *ret = NULL;
813 TypeImpl *target_type;
814 TypeImpl *type;
816 if (!class) {
817 return NULL;
820 /* A simple fast path that can trigger a lot for leaf classes. */
821 type = class->type;
822 if (type->name == typename) {
823 return class;
826 target_type = type_get_by_name(typename);
827 if (!target_type) {
828 /* target class type unknown, so fail the cast */
829 return NULL;
832 if (type->class->interfaces &&
833 type_is_ancestor(target_type, type_interface)) {
834 int found = 0;
835 GSList *i;
837 for (i = class->interfaces; i; i = i->next) {
838 ObjectClass *target_class = i->data;
840 if (type_is_ancestor(target_class->type, target_type)) {
841 ret = target_class;
842 found++;
846 /* The match was ambiguous, don't allow a cast */
847 if (found > 1) {
848 ret = NULL;
850 } else if (type_is_ancestor(type, target_type)) {
851 ret = class;
854 return ret;
857 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
858 const char *typename,
859 const char *file, int line,
860 const char *func)
862 ObjectClass *ret;
864 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
865 typename, file, line, func);
867 #ifdef CONFIG_QOM_CAST_DEBUG
868 int i;
870 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
871 if (atomic_read(&class->class_cast_cache[i]) == typename) {
872 ret = class;
873 goto out;
876 #else
877 if (!class || !class->interfaces) {
878 return class;
880 #endif
882 ret = object_class_dynamic_cast(class, typename);
883 if (!ret && class) {
884 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
885 file, line, func, class, typename);
886 abort();
889 #ifdef CONFIG_QOM_CAST_DEBUG
890 if (class && ret == class) {
891 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
892 atomic_set(&class->class_cast_cache[i - 1],
893 atomic_read(&class->class_cast_cache[i]));
895 atomic_set(&class->class_cast_cache[i - 1], typename);
897 out:
898 #endif
899 return ret;
902 const char *object_get_typename(const Object *obj)
904 return obj->class->type->name;
907 ObjectClass *object_get_class(Object *obj)
909 return obj->class;
912 bool object_class_is_abstract(ObjectClass *klass)
914 return klass->type->abstract;
917 const char *object_class_get_name(ObjectClass *klass)
919 return klass->type->name;
922 ObjectClass *object_class_by_name(const char *typename)
924 TypeImpl *type = type_get_by_name(typename);
926 if (!type) {
927 return NULL;
930 type_initialize(type);
932 return type->class;
935 ObjectClass *object_class_get_parent(ObjectClass *class)
937 TypeImpl *type = type_get_parent(class->type);
939 if (!type) {
940 return NULL;
943 type_initialize(type);
945 return type->class;
948 typedef struct OCFData
950 void (*fn)(ObjectClass *klass, void *opaque);
951 const char *implements_type;
952 bool include_abstract;
953 void *opaque;
954 } OCFData;
956 static void object_class_foreach_tramp(gpointer key, gpointer value,
957 gpointer opaque)
959 OCFData *data = opaque;
960 TypeImpl *type = value;
961 ObjectClass *k;
963 type_initialize(type);
964 k = type->class;
966 if (!data->include_abstract && type->abstract) {
967 return;
970 if (data->implements_type &&
971 !object_class_dynamic_cast(k, data->implements_type)) {
972 return;
975 data->fn(k, data->opaque);
978 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
979 const char *implements_type, bool include_abstract,
980 void *opaque)
982 OCFData data = { fn, implements_type, include_abstract, opaque };
984 enumerating_types = true;
985 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
986 enumerating_types = false;
989 static int do_object_child_foreach(Object *obj,
990 int (*fn)(Object *child, void *opaque),
991 void *opaque, bool recurse)
993 GHashTableIter iter;
994 ObjectProperty *prop;
995 int ret = 0;
997 g_hash_table_iter_init(&iter, obj->properties);
998 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
999 if (object_property_is_child(prop)) {
1000 Object *child = prop->opaque;
1002 ret = fn(child, opaque);
1003 if (ret != 0) {
1004 break;
1006 if (recurse) {
1007 do_object_child_foreach(child, fn, opaque, true);
1011 return ret;
1014 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1015 void *opaque)
1017 return do_object_child_foreach(obj, fn, opaque, false);
1020 int object_child_foreach_recursive(Object *obj,
1021 int (*fn)(Object *child, void *opaque),
1022 void *opaque)
1024 return do_object_child_foreach(obj, fn, opaque, true);
1027 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1029 GSList **list = opaque;
1031 *list = g_slist_prepend(*list, klass);
1034 GSList *object_class_get_list(const char *implements_type,
1035 bool include_abstract)
1037 GSList *list = NULL;
1039 object_class_foreach(object_class_get_list_tramp,
1040 implements_type, include_abstract, &list);
1041 return list;
1044 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1046 return strcasecmp(object_class_get_name((ObjectClass *)a),
1047 object_class_get_name((ObjectClass *)b));
1050 GSList *object_class_get_list_sorted(const char *implements_type,
1051 bool include_abstract)
1053 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1054 object_class_cmp);
1057 void object_ref(Object *obj)
1059 if (!obj) {
1060 return;
1062 atomic_inc(&obj->ref);
1065 void object_unref(Object *obj)
1067 if (!obj) {
1068 return;
1070 g_assert(obj->ref > 0);
1072 /* parent always holds a reference to its children */
1073 if (atomic_fetch_dec(&obj->ref) == 1) {
1074 object_finalize(obj);
1078 ObjectProperty *
1079 object_property_add(Object *obj, const char *name, const char *type,
1080 ObjectPropertyAccessor *get,
1081 ObjectPropertyAccessor *set,
1082 ObjectPropertyRelease *release,
1083 void *opaque, Error **errp)
1085 ObjectProperty *prop;
1086 size_t name_len = strlen(name);
1088 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1089 int i;
1090 ObjectProperty *ret;
1091 char *name_no_array = g_strdup(name);
1093 name_no_array[name_len - 3] = '\0';
1094 for (i = 0; ; ++i) {
1095 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1097 ret = object_property_add(obj, full_name, type, get, set,
1098 release, opaque, NULL);
1099 g_free(full_name);
1100 if (ret) {
1101 break;
1104 g_free(name_no_array);
1105 return ret;
1108 if (object_property_find(obj, name, NULL) != NULL) {
1109 error_setg(errp, "attempt to add duplicate property '%s'"
1110 " to object (type '%s')", name,
1111 object_get_typename(obj));
1112 return NULL;
1115 prop = g_malloc0(sizeof(*prop));
1117 prop->name = g_strdup(name);
1118 prop->type = g_strdup(type);
1120 prop->get = get;
1121 prop->set = set;
1122 prop->release = release;
1123 prop->opaque = opaque;
1125 g_hash_table_insert(obj->properties, prop->name, prop);
1126 return prop;
1129 ObjectProperty *
1130 object_class_property_add(ObjectClass *klass,
1131 const char *name,
1132 const char *type,
1133 ObjectPropertyAccessor *get,
1134 ObjectPropertyAccessor *set,
1135 ObjectPropertyRelease *release,
1136 void *opaque,
1137 Error **errp)
1139 ObjectProperty *prop;
1141 if (object_class_property_find(klass, name, NULL) != NULL) {
1142 error_setg(errp, "attempt to add duplicate property '%s'"
1143 " to object (type '%s')", name,
1144 object_class_get_name(klass));
1145 return NULL;
1148 prop = g_malloc0(sizeof(*prop));
1150 prop->name = g_strdup(name);
1151 prop->type = g_strdup(type);
1153 prop->get = get;
1154 prop->set = set;
1155 prop->release = release;
1156 prop->opaque = opaque;
1158 g_hash_table_insert(klass->properties, g_strdup(name), prop);
1160 return prop;
1163 ObjectProperty *object_property_find(Object *obj, const char *name,
1164 Error **errp)
1166 ObjectProperty *prop;
1167 ObjectClass *klass = object_get_class(obj);
1169 prop = object_class_property_find(klass, name, NULL);
1170 if (prop) {
1171 return prop;
1174 prop = g_hash_table_lookup(obj->properties, name);
1175 if (prop) {
1176 return prop;
1179 error_setg(errp, "Property '.%s' not found", name);
1180 return NULL;
1183 void object_property_iter_init(ObjectPropertyIterator *iter,
1184 Object *obj)
1186 g_hash_table_iter_init(&iter->iter, obj->properties);
1187 iter->nextclass = object_get_class(obj);
1190 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1192 gpointer key, val;
1193 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1194 if (!iter->nextclass) {
1195 return NULL;
1197 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1198 iter->nextclass = object_class_get_parent(iter->nextclass);
1200 return val;
1203 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1204 ObjectClass *klass)
1206 g_hash_table_iter_init(&iter->iter, klass->properties);
1207 iter->nextclass = object_class_get_parent(klass);
1210 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1211 Error **errp)
1213 ObjectProperty *prop;
1214 ObjectClass *parent_klass;
1216 parent_klass = object_class_get_parent(klass);
1217 if (parent_klass) {
1218 prop = object_class_property_find(parent_klass, name, NULL);
1219 if (prop) {
1220 return prop;
1224 prop = g_hash_table_lookup(klass->properties, name);
1225 if (!prop) {
1226 error_setg(errp, "Property '.%s' not found", name);
1228 return prop;
1231 void object_property_del(Object *obj, const char *name, Error **errp)
1233 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1235 if (!prop) {
1236 error_setg(errp, "Property '.%s' not found", name);
1237 return;
1240 if (prop->release) {
1241 prop->release(obj, name, prop->opaque);
1243 g_hash_table_remove(obj->properties, name);
1246 void object_property_get(Object *obj, Visitor *v, const char *name,
1247 Error **errp)
1249 ObjectProperty *prop = object_property_find(obj, name, errp);
1250 if (prop == NULL) {
1251 return;
1254 if (!prop->get) {
1255 error_setg(errp, QERR_PERMISSION_DENIED);
1256 } else {
1257 prop->get(obj, v, name, prop->opaque, errp);
1261 void object_property_set(Object *obj, Visitor *v, const char *name,
1262 Error **errp)
1264 ObjectProperty *prop = object_property_find(obj, name, errp);
1265 if (prop == NULL) {
1266 return;
1269 if (!prop->set) {
1270 error_setg(errp, QERR_PERMISSION_DENIED);
1271 } else {
1272 prop->set(obj, v, name, prop->opaque, errp);
1276 void object_property_set_str(Object *obj, const char *value,
1277 const char *name, Error **errp)
1279 QString *qstr = qstring_from_str(value);
1280 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1282 qobject_unref(qstr);
1285 char *object_property_get_str(Object *obj, const char *name,
1286 Error **errp)
1288 QObject *ret = object_property_get_qobject(obj, name, errp);
1289 char *retval;
1291 if (!ret) {
1292 return NULL;
1295 retval = g_strdup(qobject_get_try_str(ret));
1296 if (!retval) {
1297 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1300 qobject_unref(ret);
1301 return retval;
1304 void object_property_set_link(Object *obj, Object *value,
1305 const char *name, Error **errp)
1307 if (value) {
1308 gchar *path = object_get_canonical_path(value);
1309 object_property_set_str(obj, path, name, errp);
1310 g_free(path);
1311 } else {
1312 object_property_set_str(obj, "", name, errp);
1316 Object *object_property_get_link(Object *obj, const char *name,
1317 Error **errp)
1319 char *str = object_property_get_str(obj, name, errp);
1320 Object *target = NULL;
1322 if (str && *str) {
1323 target = object_resolve_path(str, NULL);
1324 if (!target) {
1325 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1326 "Device '%s' not found", str);
1330 g_free(str);
1331 return target;
1334 void object_property_set_bool(Object *obj, bool value,
1335 const char *name, Error **errp)
1337 QBool *qbool = qbool_from_bool(value);
1338 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1340 qobject_unref(qbool);
1343 bool object_property_get_bool(Object *obj, const char *name,
1344 Error **errp)
1346 QObject *ret = object_property_get_qobject(obj, name, errp);
1347 QBool *qbool;
1348 bool retval;
1350 if (!ret) {
1351 return false;
1353 qbool = qobject_to(QBool, ret);
1354 if (!qbool) {
1355 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1356 retval = false;
1357 } else {
1358 retval = qbool_get_bool(qbool);
1361 qobject_unref(ret);
1362 return retval;
1365 void object_property_set_int(Object *obj, int64_t value,
1366 const char *name, Error **errp)
1368 QNum *qnum = qnum_from_int(value);
1369 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1371 qobject_unref(qnum);
1374 int64_t object_property_get_int(Object *obj, const char *name,
1375 Error **errp)
1377 QObject *ret = object_property_get_qobject(obj, name, errp);
1378 QNum *qnum;
1379 int64_t retval;
1381 if (!ret) {
1382 return -1;
1385 qnum = qobject_to(QNum, ret);
1386 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1387 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1388 retval = -1;
1391 qobject_unref(ret);
1392 return retval;
1395 void object_property_set_uint(Object *obj, uint64_t value,
1396 const char *name, Error **errp)
1398 QNum *qnum = qnum_from_uint(value);
1400 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
1401 qobject_unref(qnum);
1404 uint64_t object_property_get_uint(Object *obj, const char *name,
1405 Error **errp)
1407 QObject *ret = object_property_get_qobject(obj, name, errp);
1408 QNum *qnum;
1409 uint64_t retval;
1411 if (!ret) {
1412 return 0;
1414 qnum = qobject_to(QNum, ret);
1415 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1416 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1417 retval = 0;
1420 qobject_unref(ret);
1421 return retval;
1424 typedef struct EnumProperty {
1425 const QEnumLookup *lookup;
1426 int (*get)(Object *, Error **);
1427 void (*set)(Object *, int, Error **);
1428 } EnumProperty;
1430 int object_property_get_enum(Object *obj, const char *name,
1431 const char *typename, Error **errp)
1433 Error *err = NULL;
1434 Visitor *v;
1435 char *str;
1436 int ret;
1437 ObjectProperty *prop = object_property_find(obj, name, errp);
1438 EnumProperty *enumprop;
1440 if (prop == NULL) {
1441 return 0;
1444 if (!g_str_equal(prop->type, typename)) {
1445 error_setg(errp, "Property %s on %s is not '%s' enum type",
1446 name, object_class_get_name(
1447 object_get_class(obj)), typename);
1448 return 0;
1451 enumprop = prop->opaque;
1453 v = string_output_visitor_new(false, &str);
1454 object_property_get(obj, v, name, &err);
1455 if (err) {
1456 error_propagate(errp, err);
1457 visit_free(v);
1458 return 0;
1460 visit_complete(v, &str);
1461 visit_free(v);
1462 v = string_input_visitor_new(str);
1463 visit_type_enum(v, name, &ret, enumprop->lookup, errp);
1465 g_free(str);
1466 visit_free(v);
1468 return ret;
1471 void object_property_get_uint16List(Object *obj, const char *name,
1472 uint16List **list, Error **errp)
1474 Error *err = NULL;
1475 Visitor *v;
1476 char *str;
1478 v = string_output_visitor_new(false, &str);
1479 object_property_get(obj, v, name, &err);
1480 if (err) {
1481 error_propagate(errp, err);
1482 goto out;
1484 visit_complete(v, &str);
1485 visit_free(v);
1486 v = string_input_visitor_new(str);
1487 visit_type_uint16List(v, NULL, list, errp);
1489 g_free(str);
1490 out:
1491 visit_free(v);
1494 void object_property_parse(Object *obj, const char *string,
1495 const char *name, Error **errp)
1497 Visitor *v = string_input_visitor_new(string);
1498 object_property_set(obj, v, name, errp);
1499 visit_free(v);
1502 char *object_property_print(Object *obj, const char *name, bool human,
1503 Error **errp)
1505 Visitor *v;
1506 char *string = NULL;
1507 Error *local_err = NULL;
1509 v = string_output_visitor_new(human, &string);
1510 object_property_get(obj, v, name, &local_err);
1511 if (local_err) {
1512 error_propagate(errp, local_err);
1513 goto out;
1516 visit_complete(v, &string);
1518 out:
1519 visit_free(v);
1520 return string;
1523 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1525 ObjectProperty *prop = object_property_find(obj, name, errp);
1526 if (prop == NULL) {
1527 return NULL;
1530 return prop->type;
1533 Object *object_get_root(void)
1535 static Object *root;
1537 if (!root) {
1538 root = object_new("container");
1541 return root;
1544 Object *object_get_objects_root(void)
1546 return container_get(object_get_root(), "/objects");
1549 Object *object_get_internal_root(void)
1551 static Object *internal_root;
1553 if (!internal_root) {
1554 internal_root = object_new("container");
1557 return internal_root;
1560 static void object_get_child_property(Object *obj, Visitor *v,
1561 const char *name, void *opaque,
1562 Error **errp)
1564 Object *child = opaque;
1565 gchar *path;
1567 path = object_get_canonical_path(child);
1568 visit_type_str(v, name, &path, errp);
1569 g_free(path);
1572 static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1574 return opaque;
1577 static void object_finalize_child_property(Object *obj, const char *name,
1578 void *opaque)
1580 Object *child = opaque;
1582 if (child->class->unparent) {
1583 (child->class->unparent)(child);
1585 child->parent = NULL;
1586 object_unref(child);
1589 void object_property_add_child(Object *obj, const char *name,
1590 Object *child, Error **errp)
1592 Error *local_err = NULL;
1593 gchar *type;
1594 ObjectProperty *op;
1596 if (child->parent != NULL) {
1597 error_setg(errp, "child object is already parented");
1598 return;
1601 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1603 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1604 object_finalize_child_property, child, &local_err);
1605 if (local_err) {
1606 error_propagate(errp, local_err);
1607 goto out;
1610 op->resolve = object_resolve_child_property;
1611 object_ref(child);
1612 child->parent = obj;
1614 out:
1615 g_free(type);
1618 void object_property_allow_set_link(const Object *obj, const char *name,
1619 Object *val, Error **errp)
1621 /* Allow the link to be set, always */
1624 typedef struct {
1625 Object **child;
1626 void (*check)(const Object *, const char *, Object *, Error **);
1627 ObjectPropertyLinkFlags flags;
1628 } LinkProperty;
1630 static void object_get_link_property(Object *obj, Visitor *v,
1631 const char *name, void *opaque,
1632 Error **errp)
1634 LinkProperty *lprop = opaque;
1635 Object **child = lprop->child;
1636 gchar *path;
1638 if (*child) {
1639 path = object_get_canonical_path(*child);
1640 visit_type_str(v, name, &path, errp);
1641 g_free(path);
1642 } else {
1643 path = (gchar *)"";
1644 visit_type_str(v, name, &path, errp);
1649 * object_resolve_link:
1651 * Lookup an object and ensure its type matches the link property type. This
1652 * is similar to object_resolve_path() except type verification against the
1653 * link property is performed.
1655 * Returns: The matched object or NULL on path lookup failures.
1657 static Object *object_resolve_link(Object *obj, const char *name,
1658 const char *path, Error **errp)
1660 const char *type;
1661 gchar *target_type;
1662 bool ambiguous = false;
1663 Object *target;
1665 /* Go from link<FOO> to FOO. */
1666 type = object_property_get_type(obj, name, NULL);
1667 target_type = g_strndup(&type[5], strlen(type) - 6);
1668 target = object_resolve_path_type(path, target_type, &ambiguous);
1670 if (ambiguous) {
1671 error_setg(errp, "Path '%s' does not uniquely identify an object",
1672 path);
1673 } else if (!target) {
1674 target = object_resolve_path(path, &ambiguous);
1675 if (target || ambiguous) {
1676 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1677 } else {
1678 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1679 "Device '%s' not found", path);
1681 target = NULL;
1683 g_free(target_type);
1685 return target;
1688 static void object_set_link_property(Object *obj, Visitor *v,
1689 const char *name, void *opaque,
1690 Error **errp)
1692 Error *local_err = NULL;
1693 LinkProperty *prop = opaque;
1694 Object **child = prop->child;
1695 Object *old_target = *child;
1696 Object *new_target = NULL;
1697 char *path = NULL;
1699 visit_type_str(v, name, &path, &local_err);
1701 if (!local_err && strcmp(path, "") != 0) {
1702 new_target = object_resolve_link(obj, name, path, &local_err);
1705 g_free(path);
1706 if (local_err) {
1707 error_propagate(errp, local_err);
1708 return;
1711 prop->check(obj, name, new_target, &local_err);
1712 if (local_err) {
1713 error_propagate(errp, local_err);
1714 return;
1717 *child = new_target;
1718 if (prop->flags == OBJ_PROP_LINK_STRONG) {
1719 object_ref(new_target);
1720 object_unref(old_target);
1724 static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1726 LinkProperty *lprop = opaque;
1728 return *lprop->child;
1731 static void object_release_link_property(Object *obj, const char *name,
1732 void *opaque)
1734 LinkProperty *prop = opaque;
1736 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *prop->child) {
1737 object_unref(*prop->child);
1739 g_free(prop);
1742 void object_property_add_link(Object *obj, const char *name,
1743 const char *type, Object **child,
1744 void (*check)(const Object *, const char *,
1745 Object *, Error **),
1746 ObjectPropertyLinkFlags flags,
1747 Error **errp)
1749 Error *local_err = NULL;
1750 LinkProperty *prop = g_malloc(sizeof(*prop));
1751 gchar *full_type;
1752 ObjectProperty *op;
1754 prop->child = child;
1755 prop->check = check;
1756 prop->flags = flags;
1758 full_type = g_strdup_printf("link<%s>", type);
1760 op = object_property_add(obj, name, full_type,
1761 object_get_link_property,
1762 check ? object_set_link_property : NULL,
1763 object_release_link_property,
1764 prop,
1765 &local_err);
1766 if (local_err) {
1767 error_propagate(errp, local_err);
1768 g_free(prop);
1769 goto out;
1772 op->resolve = object_resolve_link_property;
1774 out:
1775 g_free(full_type);
1778 void object_property_add_const_link(Object *obj, const char *name,
1779 Object *target, Error **errp)
1781 char *link_type;
1782 ObjectProperty *op;
1784 link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1785 op = object_property_add(obj, name, link_type,
1786 object_get_child_property, NULL,
1787 NULL, target, errp);
1788 if (op != NULL) {
1789 op->resolve = object_resolve_child_property;
1791 g_free(link_type);
1794 gchar *object_get_canonical_path_component(Object *obj)
1796 ObjectProperty *prop = NULL;
1797 GHashTableIter iter;
1799 if (obj->parent == NULL) {
1800 return NULL;
1803 g_hash_table_iter_init(&iter, obj->parent->properties);
1804 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1805 if (!object_property_is_child(prop)) {
1806 continue;
1809 if (prop->opaque == obj) {
1810 return g_strdup(prop->name);
1814 /* obj had a parent but was not a child, should never happen */
1815 g_assert_not_reached();
1816 return NULL;
1819 gchar *object_get_canonical_path(Object *obj)
1821 Object *root = object_get_root();
1822 char *newpath, *path = NULL;
1824 if (obj == root) {
1825 return g_strdup("/");
1828 do {
1829 char *component = object_get_canonical_path_component(obj);
1831 if (!component) {
1832 /* A canonical path must be complete, so discard what was
1833 * collected so far.
1835 g_free(path);
1836 return NULL;
1839 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1840 g_free(path);
1841 g_free(component);
1842 path = newpath;
1843 obj = obj->parent;
1844 } while (obj != root);
1846 return path;
1849 Object *object_resolve_path_component(Object *parent, const gchar *part)
1851 ObjectProperty *prop = object_property_find(parent, part, NULL);
1852 if (prop == NULL) {
1853 return NULL;
1856 if (prop->resolve) {
1857 return prop->resolve(parent, prop->opaque, part);
1858 } else {
1859 return NULL;
1863 static Object *object_resolve_abs_path(Object *parent,
1864 gchar **parts,
1865 const char *typename,
1866 int index)
1868 Object *child;
1870 if (parts[index] == NULL) {
1871 return object_dynamic_cast(parent, typename);
1874 if (strcmp(parts[index], "") == 0) {
1875 return object_resolve_abs_path(parent, parts, typename, index + 1);
1878 child = object_resolve_path_component(parent, parts[index]);
1879 if (!child) {
1880 return NULL;
1883 return object_resolve_abs_path(child, parts, typename, index + 1);
1886 static Object *object_resolve_partial_path(Object *parent,
1887 gchar **parts,
1888 const char *typename,
1889 bool *ambiguous)
1891 Object *obj;
1892 GHashTableIter iter;
1893 ObjectProperty *prop;
1895 obj = object_resolve_abs_path(parent, parts, typename, 0);
1897 g_hash_table_iter_init(&iter, parent->properties);
1898 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1899 Object *found;
1901 if (!object_property_is_child(prop)) {
1902 continue;
1905 found = object_resolve_partial_path(prop->opaque, parts,
1906 typename, ambiguous);
1907 if (found) {
1908 if (obj) {
1909 *ambiguous = true;
1910 return NULL;
1912 obj = found;
1915 if (*ambiguous) {
1916 return NULL;
1920 return obj;
1923 Object *object_resolve_path_type(const char *path, const char *typename,
1924 bool *ambiguousp)
1926 Object *obj;
1927 gchar **parts;
1929 parts = g_strsplit(path, "/", 0);
1930 assert(parts);
1932 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
1933 bool ambiguous = false;
1934 obj = object_resolve_partial_path(object_get_root(), parts,
1935 typename, &ambiguous);
1936 if (ambiguousp) {
1937 *ambiguousp = ambiguous;
1939 } else {
1940 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
1943 g_strfreev(parts);
1945 return obj;
1948 Object *object_resolve_path(const char *path, bool *ambiguous)
1950 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1953 typedef struct StringProperty
1955 char *(*get)(Object *, Error **);
1956 void (*set)(Object *, const char *, Error **);
1957 } StringProperty;
1959 static void property_get_str(Object *obj, Visitor *v, const char *name,
1960 void *opaque, Error **errp)
1962 StringProperty *prop = opaque;
1963 char *value;
1964 Error *err = NULL;
1966 value = prop->get(obj, &err);
1967 if (err) {
1968 error_propagate(errp, err);
1969 return;
1972 visit_type_str(v, name, &value, errp);
1973 g_free(value);
1976 static void property_set_str(Object *obj, Visitor *v, const char *name,
1977 void *opaque, Error **errp)
1979 StringProperty *prop = opaque;
1980 char *value;
1981 Error *local_err = NULL;
1983 visit_type_str(v, name, &value, &local_err);
1984 if (local_err) {
1985 error_propagate(errp, local_err);
1986 return;
1989 prop->set(obj, value, errp);
1990 g_free(value);
1993 static void property_release_str(Object *obj, const char *name,
1994 void *opaque)
1996 StringProperty *prop = opaque;
1997 g_free(prop);
2000 void object_property_add_str(Object *obj, const char *name,
2001 char *(*get)(Object *, Error **),
2002 void (*set)(Object *, const char *, Error **),
2003 Error **errp)
2005 Error *local_err = NULL;
2006 StringProperty *prop = g_malloc0(sizeof(*prop));
2008 prop->get = get;
2009 prop->set = set;
2011 object_property_add(obj, name, "string",
2012 get ? property_get_str : NULL,
2013 set ? property_set_str : NULL,
2014 property_release_str,
2015 prop, &local_err);
2016 if (local_err) {
2017 error_propagate(errp, local_err);
2018 g_free(prop);
2022 void object_class_property_add_str(ObjectClass *klass, const char *name,
2023 char *(*get)(Object *, Error **),
2024 void (*set)(Object *, const char *,
2025 Error **),
2026 Error **errp)
2028 Error *local_err = NULL;
2029 StringProperty *prop = g_malloc0(sizeof(*prop));
2031 prop->get = get;
2032 prop->set = set;
2034 object_class_property_add(klass, name, "string",
2035 get ? property_get_str : NULL,
2036 set ? property_set_str : NULL,
2037 property_release_str,
2038 prop, &local_err);
2039 if (local_err) {
2040 error_propagate(errp, local_err);
2041 g_free(prop);
2045 typedef struct BoolProperty
2047 bool (*get)(Object *, Error **);
2048 void (*set)(Object *, bool, Error **);
2049 } BoolProperty;
2051 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2052 void *opaque, Error **errp)
2054 BoolProperty *prop = opaque;
2055 bool value;
2056 Error *err = NULL;
2058 value = prop->get(obj, &err);
2059 if (err) {
2060 error_propagate(errp, err);
2061 return;
2064 visit_type_bool(v, name, &value, errp);
2067 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2068 void *opaque, Error **errp)
2070 BoolProperty *prop = opaque;
2071 bool value;
2072 Error *local_err = NULL;
2074 visit_type_bool(v, name, &value, &local_err);
2075 if (local_err) {
2076 error_propagate(errp, local_err);
2077 return;
2080 prop->set(obj, value, errp);
2083 static void property_release_bool(Object *obj, const char *name,
2084 void *opaque)
2086 BoolProperty *prop = opaque;
2087 g_free(prop);
2090 void object_property_add_bool(Object *obj, const char *name,
2091 bool (*get)(Object *, Error **),
2092 void (*set)(Object *, bool, Error **),
2093 Error **errp)
2095 Error *local_err = NULL;
2096 BoolProperty *prop = g_malloc0(sizeof(*prop));
2098 prop->get = get;
2099 prop->set = set;
2101 object_property_add(obj, name, "bool",
2102 get ? property_get_bool : NULL,
2103 set ? property_set_bool : NULL,
2104 property_release_bool,
2105 prop, &local_err);
2106 if (local_err) {
2107 error_propagate(errp, local_err);
2108 g_free(prop);
2112 void object_class_property_add_bool(ObjectClass *klass, 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_class_property_add(klass, 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 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2135 void *opaque, Error **errp)
2137 EnumProperty *prop = opaque;
2138 int value;
2139 Error *err = NULL;
2141 value = prop->get(obj, &err);
2142 if (err) {
2143 error_propagate(errp, err);
2144 return;
2147 visit_type_enum(v, name, &value, prop->lookup, errp);
2150 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2151 void *opaque, Error **errp)
2153 EnumProperty *prop = opaque;
2154 int value;
2155 Error *err = NULL;
2157 visit_type_enum(v, name, &value, prop->lookup, &err);
2158 if (err) {
2159 error_propagate(errp, err);
2160 return;
2162 prop->set(obj, value, errp);
2165 static void property_release_enum(Object *obj, const char *name,
2166 void *opaque)
2168 EnumProperty *prop = opaque;
2169 g_free(prop);
2172 void object_property_add_enum(Object *obj, const char *name,
2173 const char *typename,
2174 const QEnumLookup *lookup,
2175 int (*get)(Object *, Error **),
2176 void (*set)(Object *, int, Error **),
2177 Error **errp)
2179 Error *local_err = NULL;
2180 EnumProperty *prop = g_malloc(sizeof(*prop));
2182 prop->lookup = lookup;
2183 prop->get = get;
2184 prop->set = set;
2186 object_property_add(obj, name, typename,
2187 get ? property_get_enum : NULL,
2188 set ? property_set_enum : NULL,
2189 property_release_enum,
2190 prop, &local_err);
2191 if (local_err) {
2192 error_propagate(errp, local_err);
2193 g_free(prop);
2197 void object_class_property_add_enum(ObjectClass *klass, const char *name,
2198 const char *typename,
2199 const QEnumLookup *lookup,
2200 int (*get)(Object *, Error **),
2201 void (*set)(Object *, int, Error **),
2202 Error **errp)
2204 Error *local_err = NULL;
2205 EnumProperty *prop = g_malloc(sizeof(*prop));
2207 prop->lookup = lookup;
2208 prop->get = get;
2209 prop->set = set;
2211 object_class_property_add(klass, name, typename,
2212 get ? property_get_enum : NULL,
2213 set ? property_set_enum : NULL,
2214 property_release_enum,
2215 prop, &local_err);
2216 if (local_err) {
2217 error_propagate(errp, local_err);
2218 g_free(prop);
2222 typedef struct TMProperty {
2223 void (*get)(Object *, struct tm *, Error **);
2224 } TMProperty;
2226 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2227 void *opaque, Error **errp)
2229 TMProperty *prop = opaque;
2230 Error *err = NULL;
2231 struct tm value;
2233 prop->get(obj, &value, &err);
2234 if (err) {
2235 goto out;
2238 visit_start_struct(v, name, NULL, 0, &err);
2239 if (err) {
2240 goto out;
2242 visit_type_int32(v, "tm_year", &value.tm_year, &err);
2243 if (err) {
2244 goto out_end;
2246 visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
2247 if (err) {
2248 goto out_end;
2250 visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
2251 if (err) {
2252 goto out_end;
2254 visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
2255 if (err) {
2256 goto out_end;
2258 visit_type_int32(v, "tm_min", &value.tm_min, &err);
2259 if (err) {
2260 goto out_end;
2262 visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
2263 if (err) {
2264 goto out_end;
2266 visit_check_struct(v, &err);
2267 out_end:
2268 visit_end_struct(v, NULL);
2269 out:
2270 error_propagate(errp, err);
2274 static void property_release_tm(Object *obj, const char *name,
2275 void *opaque)
2277 TMProperty *prop = opaque;
2278 g_free(prop);
2281 void object_property_add_tm(Object *obj, const char *name,
2282 void (*get)(Object *, struct tm *, Error **),
2283 Error **errp)
2285 Error *local_err = NULL;
2286 TMProperty *prop = g_malloc0(sizeof(*prop));
2288 prop->get = get;
2290 object_property_add(obj, name, "struct tm",
2291 get ? property_get_tm : NULL, NULL,
2292 property_release_tm,
2293 prop, &local_err);
2294 if (local_err) {
2295 error_propagate(errp, local_err);
2296 g_free(prop);
2300 void object_class_property_add_tm(ObjectClass *klass, const char *name,
2301 void (*get)(Object *, struct tm *, Error **),
2302 Error **errp)
2304 Error *local_err = NULL;
2305 TMProperty *prop = g_malloc0(sizeof(*prop));
2307 prop->get = get;
2309 object_class_property_add(klass, name, "struct tm",
2310 get ? property_get_tm : NULL, NULL,
2311 property_release_tm,
2312 prop, &local_err);
2313 if (local_err) {
2314 error_propagate(errp, local_err);
2315 g_free(prop);
2319 static char *qdev_get_type(Object *obj, Error **errp)
2321 return g_strdup(object_get_typename(obj));
2324 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2325 void *opaque, Error **errp)
2327 uint8_t value = *(uint8_t *)opaque;
2328 visit_type_uint8(v, name, &value, errp);
2331 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2332 void *opaque, Error **errp)
2334 uint16_t value = *(uint16_t *)opaque;
2335 visit_type_uint16(v, name, &value, errp);
2338 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2339 void *opaque, Error **errp)
2341 uint32_t value = *(uint32_t *)opaque;
2342 visit_type_uint32(v, name, &value, errp);
2345 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2346 void *opaque, Error **errp)
2348 uint64_t value = *(uint64_t *)opaque;
2349 visit_type_uint64(v, name, &value, errp);
2352 void object_property_add_uint8_ptr(Object *obj, const char *name,
2353 const uint8_t *v, Error **errp)
2355 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
2356 NULL, NULL, (void *)v, errp);
2359 void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2360 const uint8_t *v, Error **errp)
2362 object_class_property_add(klass, name, "uint8", property_get_uint8_ptr,
2363 NULL, NULL, (void *)v, errp);
2366 void object_property_add_uint16_ptr(Object *obj, const char *name,
2367 const uint16_t *v, Error **errp)
2369 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
2370 NULL, NULL, (void *)v, errp);
2373 void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2374 const uint16_t *v, Error **errp)
2376 object_class_property_add(klass, name, "uint16", property_get_uint16_ptr,
2377 NULL, NULL, (void *)v, errp);
2380 void object_property_add_uint32_ptr(Object *obj, const char *name,
2381 const uint32_t *v, Error **errp)
2383 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
2384 NULL, NULL, (void *)v, errp);
2387 void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2388 const uint32_t *v, Error **errp)
2390 object_class_property_add(klass, name, "uint32", property_get_uint32_ptr,
2391 NULL, NULL, (void *)v, errp);
2394 void object_property_add_uint64_ptr(Object *obj, const char *name,
2395 const uint64_t *v, Error **errp)
2397 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
2398 NULL, NULL, (void *)v, errp);
2401 void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2402 const uint64_t *v, Error **errp)
2404 object_class_property_add(klass, name, "uint64", property_get_uint64_ptr,
2405 NULL, NULL, (void *)v, errp);
2408 typedef struct {
2409 Object *target_obj;
2410 char *target_name;
2411 } AliasProperty;
2413 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2414 void *opaque, Error **errp)
2416 AliasProperty *prop = opaque;
2418 object_property_get(prop->target_obj, v, prop->target_name, errp);
2421 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2422 void *opaque, Error **errp)
2424 AliasProperty *prop = opaque;
2426 object_property_set(prop->target_obj, v, prop->target_name, errp);
2429 static Object *property_resolve_alias(Object *obj, void *opaque,
2430 const gchar *part)
2432 AliasProperty *prop = opaque;
2434 return object_resolve_path_component(prop->target_obj, prop->target_name);
2437 static void property_release_alias(Object *obj, const char *name, void *opaque)
2439 AliasProperty *prop = opaque;
2441 g_free(prop->target_name);
2442 g_free(prop);
2445 void object_property_add_alias(Object *obj, const char *name,
2446 Object *target_obj, const char *target_name,
2447 Error **errp)
2449 AliasProperty *prop;
2450 ObjectProperty *op;
2451 ObjectProperty *target_prop;
2452 gchar *prop_type;
2453 Error *local_err = NULL;
2455 target_prop = object_property_find(target_obj, target_name, errp);
2456 if (!target_prop) {
2457 return;
2460 if (object_property_is_child(target_prop)) {
2461 prop_type = g_strdup_printf("link%s",
2462 target_prop->type + strlen("child"));
2463 } else {
2464 prop_type = g_strdup(target_prop->type);
2467 prop = g_malloc(sizeof(*prop));
2468 prop->target_obj = target_obj;
2469 prop->target_name = g_strdup(target_name);
2471 op = object_property_add(obj, name, prop_type,
2472 property_get_alias,
2473 property_set_alias,
2474 property_release_alias,
2475 prop, &local_err);
2476 if (local_err) {
2477 error_propagate(errp, local_err);
2478 g_free(prop);
2479 goto out;
2481 op->resolve = property_resolve_alias;
2483 object_property_set_description(obj, op->name,
2484 target_prop->description,
2485 &error_abort);
2487 out:
2488 g_free(prop_type);
2491 void object_property_set_description(Object *obj, const char *name,
2492 const char *description, Error **errp)
2494 ObjectProperty *op;
2496 op = object_property_find(obj, name, errp);
2497 if (!op) {
2498 return;
2501 g_free(op->description);
2502 op->description = g_strdup(description);
2505 void object_class_property_set_description(ObjectClass *klass,
2506 const char *name,
2507 const char *description,
2508 Error **errp)
2510 ObjectProperty *op;
2512 op = g_hash_table_lookup(klass->properties, name);
2513 if (!op) {
2514 error_setg(errp, "Property '.%s' not found", name);
2515 return;
2518 g_free(op->description);
2519 op->description = g_strdup(description);
2522 static void object_class_init(ObjectClass *klass, void *data)
2524 object_class_property_add_str(klass, "type", qdev_get_type,
2525 NULL, &error_abort);
2528 static void register_types(void)
2530 static TypeInfo interface_info = {
2531 .name = TYPE_INTERFACE,
2532 .class_size = sizeof(InterfaceClass),
2533 .abstract = true,
2536 static TypeInfo object_info = {
2537 .name = TYPE_OBJECT,
2538 .instance_size = sizeof(Object),
2539 .class_init = object_class_init,
2540 .abstract = true,
2543 type_interface = type_register_internal(&interface_info);
2544 type_register_internal(&object_info);
2547 type_init(register_types)