ppc/pnv: Rename "id" to "quad-id" in PnvQuad
[qemu.git] / qom / object.c
blobe86cb05b84da941a17709381172611db7f0b1470
1 /*
2 * QEMU Object Model
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/forward-visitor.h"
24 #include "qapi/qapi-builtin-visit.h"
25 #include "qapi/qmp/qerror.h"
26 #include "qapi/qmp/qjson.h"
27 #include "trace.h"
29 /* TODO: replace QObject with a simpler visitor to avoid a dependency
30 * of the QOM core on QObject? */
31 #include "qom/qom-qobject.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qnum.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qemu/error-report.h"
37 #define MAX_INTERFACES 32
39 typedef struct InterfaceImpl InterfaceImpl;
40 typedef struct TypeImpl TypeImpl;
42 struct InterfaceImpl
44 const char *typename;
47 struct TypeImpl
49 const char *name;
51 size_t class_size;
53 size_t instance_size;
54 size_t instance_align;
56 void (*class_init)(ObjectClass *klass, void *data);
57 void (*class_base_init)(ObjectClass *klass, void *data);
59 void *class_data;
61 void (*instance_init)(Object *obj);
62 void (*instance_post_init)(Object *obj);
63 void (*instance_finalize)(Object *obj);
65 bool abstract;
67 const char *parent;
68 TypeImpl *parent_type;
70 ObjectClass *class;
72 int num_interfaces;
73 InterfaceImpl interfaces[MAX_INTERFACES];
76 static Type type_interface;
78 static GHashTable *type_table_get(void)
80 static GHashTable *type_table;
82 if (type_table == NULL) {
83 type_table = g_hash_table_new(g_str_hash, g_str_equal);
86 return type_table;
89 static bool enumerating_types;
91 static void type_table_add(TypeImpl *ti)
93 assert(!enumerating_types);
94 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
97 static TypeImpl *type_table_lookup(const char *name)
99 return g_hash_table_lookup(type_table_get(), name);
102 static TypeImpl *type_new(const TypeInfo *info)
104 TypeImpl *ti = g_malloc0(sizeof(*ti));
105 int i;
107 g_assert(info->name != NULL);
109 if (type_table_lookup(info->name) != NULL) {
110 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
111 abort();
114 ti->name = g_strdup(info->name);
115 ti->parent = g_strdup(info->parent);
117 ti->class_size = info->class_size;
118 ti->instance_size = info->instance_size;
119 ti->instance_align = info->instance_align;
121 ti->class_init = info->class_init;
122 ti->class_base_init = info->class_base_init;
123 ti->class_data = info->class_data;
125 ti->instance_init = info->instance_init;
126 ti->instance_post_init = info->instance_post_init;
127 ti->instance_finalize = info->instance_finalize;
129 ti->abstract = info->abstract;
131 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
132 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
134 ti->num_interfaces = i;
136 return ti;
139 static TypeImpl *type_register_internal(const TypeInfo *info)
141 TypeImpl *ti;
142 ti = type_new(info);
144 type_table_add(ti);
145 return ti;
148 TypeImpl *type_register(const TypeInfo *info)
150 assert(info->parent);
151 return type_register_internal(info);
154 TypeImpl *type_register_static(const TypeInfo *info)
156 return type_register(info);
159 void type_register_static_array(const TypeInfo *infos, int nr_infos)
161 int i;
163 for (i = 0; i < nr_infos; i++) {
164 type_register_static(&infos[i]);
168 static TypeImpl *type_get_by_name(const char *name)
170 if (name == NULL) {
171 return NULL;
174 return type_table_lookup(name);
177 static TypeImpl *type_get_parent(TypeImpl *type)
179 if (!type->parent_type && type->parent) {
180 type->parent_type = type_get_by_name(type->parent);
181 if (!type->parent_type) {
182 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
183 type->name, type->parent);
184 abort();
188 return type->parent_type;
191 static bool type_has_parent(TypeImpl *type)
193 return (type->parent != NULL);
196 static size_t type_class_get_size(TypeImpl *ti)
198 if (ti->class_size) {
199 return ti->class_size;
202 if (type_has_parent(ti)) {
203 return type_class_get_size(type_get_parent(ti));
206 return sizeof(ObjectClass);
209 static size_t type_object_get_size(TypeImpl *ti)
211 if (ti->instance_size) {
212 return ti->instance_size;
215 if (type_has_parent(ti)) {
216 return type_object_get_size(type_get_parent(ti));
219 return 0;
222 size_t object_type_get_instance_size(const char *typename)
224 TypeImpl *type = type_get_by_name(typename);
226 g_assert(type != NULL);
227 return type_object_get_size(type);
230 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
232 assert(target_type);
234 /* Check if target_type is a direct ancestor of type */
235 while (type) {
236 if (type == target_type) {
237 return true;
240 type = type_get_parent(type);
243 return false;
246 static void type_initialize(TypeImpl *ti);
248 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
249 TypeImpl *parent_type)
251 InterfaceClass *new_iface;
252 TypeInfo info = { };
253 TypeImpl *iface_impl;
255 info.parent = parent_type->name;
256 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
257 info.abstract = true;
259 iface_impl = type_new(&info);
260 iface_impl->parent_type = parent_type;
261 type_initialize(iface_impl);
262 g_free((char *)info.name);
264 new_iface = (InterfaceClass *)iface_impl->class;
265 new_iface->concrete_class = ti->class;
266 new_iface->interface_type = interface_type;
268 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
271 static void object_property_free(gpointer data)
273 ObjectProperty *prop = data;
275 if (prop->defval) {
276 qobject_unref(prop->defval);
277 prop->defval = NULL;
279 g_free(prop->name);
280 g_free(prop->type);
281 g_free(prop->description);
282 g_free(prop);
285 static void type_initialize(TypeImpl *ti)
287 TypeImpl *parent;
289 if (ti->class) {
290 return;
293 ti->class_size = type_class_get_size(ti);
294 ti->instance_size = type_object_get_size(ti);
295 /* Any type with zero instance_size is implicitly abstract.
296 * This means interface types are all abstract.
298 if (ti->instance_size == 0) {
299 ti->abstract = true;
301 if (type_is_ancestor(ti, type_interface)) {
302 assert(ti->instance_size == 0);
303 assert(ti->abstract);
304 assert(!ti->instance_init);
305 assert(!ti->instance_post_init);
306 assert(!ti->instance_finalize);
307 assert(!ti->num_interfaces);
309 ti->class = g_malloc0(ti->class_size);
311 parent = type_get_parent(ti);
312 if (parent) {
313 type_initialize(parent);
314 GSList *e;
315 int i;
317 g_assert(parent->class_size <= ti->class_size);
318 g_assert(parent->instance_size <= ti->instance_size);
319 memcpy(ti->class, parent->class, parent->class_size);
320 ti->class->interfaces = NULL;
322 for (e = parent->class->interfaces; e; e = e->next) {
323 InterfaceClass *iface = e->data;
324 ObjectClass *klass = OBJECT_CLASS(iface);
326 type_initialize_interface(ti, iface->interface_type, klass->type);
329 for (i = 0; i < ti->num_interfaces; i++) {
330 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
331 if (!t) {
332 error_report("missing interface '%s' for object '%s'",
333 ti->interfaces[i].typename, parent->name);
334 abort();
336 for (e = ti->class->interfaces; e; e = e->next) {
337 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
339 if (type_is_ancestor(target_type, t)) {
340 break;
344 if (e) {
345 continue;
348 type_initialize_interface(ti, t, t);
352 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
353 object_property_free);
355 ti->class->type = ti;
357 while (parent) {
358 if (parent->class_base_init) {
359 parent->class_base_init(ti->class, ti->class_data);
361 parent = type_get_parent(parent);
364 if (ti->class_init) {
365 ti->class_init(ti->class, ti->class_data);
369 static void object_init_with_type(Object *obj, TypeImpl *ti)
371 if (type_has_parent(ti)) {
372 object_init_with_type(obj, type_get_parent(ti));
375 if (ti->instance_init) {
376 ti->instance_init(obj);
380 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
382 if (ti->instance_post_init) {
383 ti->instance_post_init(obj);
386 if (type_has_parent(ti)) {
387 object_post_init_with_type(obj, type_get_parent(ti));
391 bool object_apply_global_props(Object *obj, const GPtrArray *props,
392 Error **errp)
394 int i;
396 if (!props) {
397 return true;
400 for (i = 0; i < props->len; i++) {
401 GlobalProperty *p = g_ptr_array_index(props, i);
402 Error *err = NULL;
404 if (object_dynamic_cast(obj, p->driver) == NULL) {
405 continue;
407 if (p->optional && !object_property_find(obj, p->property)) {
408 continue;
410 p->used = true;
411 if (!object_property_parse(obj, p->property, p->value, &err)) {
412 error_prepend(&err, "can't apply global %s.%s=%s: ",
413 p->driver, p->property, p->value);
415 * If errp != NULL, propagate error and return.
416 * If errp == NULL, report a warning, but keep going
417 * with the remaining globals.
419 if (errp) {
420 error_propagate(errp, err);
421 return false;
422 } else {
423 warn_report_err(err);
428 return true;
432 * Global property defaults
433 * Slot 0: accelerator's global property defaults
434 * Slot 1: machine's global property defaults
435 * Slot 2: global properties from legacy command line option
436 * Each is a GPtrArray of of GlobalProperty.
437 * Applied in order, later entries override earlier ones.
439 static GPtrArray *object_compat_props[3];
442 * Retrieve @GPtrArray for global property defined with options
443 * other than "-global". These are generally used for syntactic
444 * sugar and legacy command line options.
446 void object_register_sugar_prop(const char *driver, const char *prop,
447 const char *value, bool optional)
449 GlobalProperty *g;
450 if (!object_compat_props[2]) {
451 object_compat_props[2] = g_ptr_array_new();
453 g = g_new0(GlobalProperty, 1);
454 g->driver = g_strdup(driver);
455 g->property = g_strdup(prop);
456 g->value = g_strdup(value);
457 g->optional = optional;
458 g_ptr_array_add(object_compat_props[2], g);
462 * Set machine's global property defaults to @compat_props.
463 * May be called at most once.
465 void object_set_machine_compat_props(GPtrArray *compat_props)
467 assert(!object_compat_props[1]);
468 object_compat_props[1] = compat_props;
472 * Set accelerator's global property defaults to @compat_props.
473 * May be called at most once.
475 void object_set_accelerator_compat_props(GPtrArray *compat_props)
477 assert(!object_compat_props[0]);
478 object_compat_props[0] = compat_props;
481 void object_apply_compat_props(Object *obj)
483 int i;
485 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
486 object_apply_global_props(obj, object_compat_props[i],
487 i == 2 ? &error_fatal : &error_abort);
491 static void object_class_property_init_all(Object *obj)
493 ObjectPropertyIterator iter;
494 ObjectProperty *prop;
496 object_class_property_iter_init(&iter, object_get_class(obj));
497 while ((prop = object_property_iter_next(&iter))) {
498 if (prop->init) {
499 prop->init(obj, prop);
504 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
506 type_initialize(type);
508 g_assert(type->instance_size >= sizeof(Object));
509 g_assert(type->abstract == false);
510 g_assert(size >= type->instance_size);
512 memset(obj, 0, type->instance_size);
513 obj->class = type->class;
514 object_ref(obj);
515 object_class_property_init_all(obj);
516 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
517 NULL, object_property_free);
518 object_init_with_type(obj, type);
519 object_post_init_with_type(obj, type);
522 void object_initialize(void *data, size_t size, const char *typename)
524 TypeImpl *type = type_get_by_name(typename);
526 #ifdef CONFIG_MODULES
527 if (!type) {
528 module_load_qom_one(typename);
529 type = type_get_by_name(typename);
531 #endif
532 if (!type) {
533 error_report("missing object type '%s'", typename);
534 abort();
537 object_initialize_with_type(data, size, type);
540 bool object_initialize_child_with_props(Object *parentobj,
541 const char *propname,
542 void *childobj, size_t size,
543 const char *type,
544 Error **errp, ...)
546 va_list vargs;
547 bool ok;
549 va_start(vargs, errp);
550 ok = object_initialize_child_with_propsv(parentobj, propname,
551 childobj, size, type, errp,
552 vargs);
553 va_end(vargs);
554 return ok;
557 bool object_initialize_child_with_propsv(Object *parentobj,
558 const char *propname,
559 void *childobj, size_t size,
560 const char *type,
561 Error **errp, va_list vargs)
563 bool ok = false;
564 Object *obj;
565 UserCreatable *uc;
567 object_initialize(childobj, size, type);
568 obj = OBJECT(childobj);
570 if (!object_set_propv(obj, errp, vargs)) {
571 goto out;
574 object_property_add_child(parentobj, propname, obj);
576 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
577 if (uc) {
578 if (!user_creatable_complete(uc, errp)) {
579 object_unparent(obj);
580 goto out;
584 ok = true;
586 out:
588 * We want @obj's reference to be 1 on success, 0 on failure.
589 * On success, it's 2: one taken by object_initialize(), and one
590 * by object_property_add_child().
591 * On failure in object_initialize() or earlier, it's 1.
592 * On failure afterwards, it's also 1: object_unparent() releases
593 * the reference taken by object_property_add_child().
595 object_unref(obj);
596 return ok;
599 void object_initialize_child_internal(Object *parent,
600 const char *propname,
601 void *child, size_t size,
602 const char *type)
604 object_initialize_child_with_props(parent, propname, child, size, type,
605 &error_abort, NULL);
608 static inline bool object_property_is_child(ObjectProperty *prop)
610 return strstart(prop->type, "child<", NULL);
613 static void object_property_del_all(Object *obj)
615 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
616 ObjectProperty *prop;
617 ObjectPropertyIterator iter;
618 bool released;
620 do {
621 released = false;
622 object_property_iter_init(&iter, obj);
623 while ((prop = object_property_iter_next(&iter)) != NULL) {
624 if (g_hash_table_add(done, prop)) {
625 if (prop->release) {
626 prop->release(obj, prop->name, prop->opaque);
627 released = true;
628 break;
632 } while (released);
634 g_hash_table_unref(obj->properties);
637 static void object_property_del_child(Object *obj, Object *child)
639 ObjectProperty *prop;
640 GHashTableIter iter;
641 gpointer key, value;
643 g_hash_table_iter_init(&iter, obj->properties);
644 while (g_hash_table_iter_next(&iter, &key, &value)) {
645 prop = value;
646 if (object_property_is_child(prop) && prop->opaque == child) {
647 if (prop->release) {
648 prop->release(obj, prop->name, prop->opaque);
649 prop->release = NULL;
651 break;
654 g_hash_table_iter_init(&iter, obj->properties);
655 while (g_hash_table_iter_next(&iter, &key, &value)) {
656 prop = value;
657 if (object_property_is_child(prop) && prop->opaque == child) {
658 g_hash_table_iter_remove(&iter);
659 break;
664 void object_unparent(Object *obj)
666 if (obj->parent) {
667 object_property_del_child(obj->parent, obj);
671 static void object_deinit(Object *obj, TypeImpl *type)
673 if (type->instance_finalize) {
674 type->instance_finalize(obj);
677 if (type_has_parent(type)) {
678 object_deinit(obj, type_get_parent(type));
682 static void object_finalize(void *data)
684 Object *obj = data;
685 TypeImpl *ti = obj->class->type;
687 object_property_del_all(obj);
688 object_deinit(obj, ti);
690 g_assert(obj->ref == 0);
691 g_assert(obj->parent == NULL);
692 if (obj->free) {
693 obj->free(obj);
697 /* Find the minimum alignment guaranteed by the system malloc. */
698 #if __STDC_VERSION__ >= 201112L
699 typedef max_align_t qemu_max_align_t;
700 #else
701 typedef union {
702 long l;
703 void *p;
704 double d;
705 long double ld;
706 } qemu_max_align_t;
707 #endif
709 static Object *object_new_with_type(Type type)
711 Object *obj;
712 size_t size, align;
713 void (*obj_free)(void *);
715 g_assert(type != NULL);
716 type_initialize(type);
718 size = type->instance_size;
719 align = type->instance_align;
722 * Do not use qemu_memalign unless required. Depending on the
723 * implementation, extra alignment implies extra overhead.
725 if (likely(align <= __alignof__(qemu_max_align_t))) {
726 obj = g_malloc(size);
727 obj_free = g_free;
728 } else {
729 obj = qemu_memalign(align, size);
730 obj_free = qemu_vfree;
733 object_initialize_with_type(obj, size, type);
734 obj->free = obj_free;
736 return obj;
739 Object *object_new_with_class(ObjectClass *klass)
741 return object_new_with_type(klass->type);
744 Object *object_new(const char *typename)
746 TypeImpl *ti = type_get_by_name(typename);
748 return object_new_with_type(ti);
752 Object *object_new_with_props(const char *typename,
753 Object *parent,
754 const char *id,
755 Error **errp,
756 ...)
758 va_list vargs;
759 Object *obj;
761 va_start(vargs, errp);
762 obj = object_new_with_propv(typename, parent, id, errp, vargs);
763 va_end(vargs);
765 return obj;
769 Object *object_new_with_propv(const char *typename,
770 Object *parent,
771 const char *id,
772 Error **errp,
773 va_list vargs)
775 Object *obj;
776 ObjectClass *klass;
777 UserCreatable *uc;
779 klass = object_class_by_name(typename);
780 if (!klass) {
781 error_setg(errp, "invalid object type: %s", typename);
782 return NULL;
785 if (object_class_is_abstract(klass)) {
786 error_setg(errp, "object type '%s' is abstract", typename);
787 return NULL;
789 obj = object_new_with_type(klass->type);
791 if (!object_set_propv(obj, errp, vargs)) {
792 goto error;
795 if (id != NULL) {
796 object_property_add_child(parent, id, obj);
799 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
800 if (uc) {
801 if (!user_creatable_complete(uc, errp)) {
802 if (id != NULL) {
803 object_unparent(obj);
805 goto error;
809 object_unref(obj);
810 return obj;
812 error:
813 object_unref(obj);
814 return NULL;
818 bool object_set_props(Object *obj,
819 Error **errp,
820 ...)
822 va_list vargs;
823 bool ret;
825 va_start(vargs, errp);
826 ret = object_set_propv(obj, errp, vargs);
827 va_end(vargs);
829 return ret;
833 bool object_set_propv(Object *obj,
834 Error **errp,
835 va_list vargs)
837 const char *propname;
839 propname = va_arg(vargs, char *);
840 while (propname != NULL) {
841 const char *value = va_arg(vargs, char *);
843 g_assert(value != NULL);
844 if (!object_property_parse(obj, propname, value, errp)) {
845 return false;
847 propname = va_arg(vargs, char *);
850 return true;
854 Object *object_dynamic_cast(Object *obj, const char *typename)
856 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
857 return obj;
860 return NULL;
863 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
864 const char *file, int line, const char *func)
866 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
867 typename, file, line, func);
869 #ifdef CONFIG_QOM_CAST_DEBUG
870 int i;
871 Object *inst;
873 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
874 if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) {
875 goto out;
879 inst = object_dynamic_cast(obj, typename);
881 if (!inst && obj) {
882 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
883 file, line, func, obj, typename);
884 abort();
887 assert(obj == inst);
889 if (obj && obj == inst) {
890 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
891 qatomic_set(&obj->class->object_cast_cache[i - 1],
892 qatomic_read(&obj->class->object_cast_cache[i]));
894 qatomic_set(&obj->class->object_cast_cache[i - 1], typename);
897 out:
898 #endif
899 return obj;
902 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
903 const char *typename)
905 ObjectClass *ret = NULL;
906 TypeImpl *target_type;
907 TypeImpl *type;
909 if (!class) {
910 return NULL;
913 /* A simple fast path that can trigger a lot for leaf classes. */
914 type = class->type;
915 if (type->name == typename) {
916 return class;
919 target_type = type_get_by_name(typename);
920 if (!target_type) {
921 /* target class type unknown, so fail the cast */
922 return NULL;
925 if (type->class->interfaces &&
926 type_is_ancestor(target_type, type_interface)) {
927 int found = 0;
928 GSList *i;
930 for (i = class->interfaces; i; i = i->next) {
931 ObjectClass *target_class = i->data;
933 if (type_is_ancestor(target_class->type, target_type)) {
934 ret = target_class;
935 found++;
939 /* The match was ambiguous, don't allow a cast */
940 if (found > 1) {
941 ret = NULL;
943 } else if (type_is_ancestor(type, target_type)) {
944 ret = class;
947 return ret;
950 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
951 const char *typename,
952 const char *file, int line,
953 const char *func)
955 ObjectClass *ret;
957 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
958 typename, file, line, func);
960 #ifdef CONFIG_QOM_CAST_DEBUG
961 int i;
963 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
964 if (qatomic_read(&class->class_cast_cache[i]) == typename) {
965 ret = class;
966 goto out;
969 #else
970 if (!class || !class->interfaces) {
971 return class;
973 #endif
975 ret = object_class_dynamic_cast(class, typename);
976 if (!ret && class) {
977 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
978 file, line, func, class, typename);
979 abort();
982 #ifdef CONFIG_QOM_CAST_DEBUG
983 if (class && ret == class) {
984 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
985 qatomic_set(&class->class_cast_cache[i - 1],
986 qatomic_read(&class->class_cast_cache[i]));
988 qatomic_set(&class->class_cast_cache[i - 1], typename);
990 out:
991 #endif
992 return ret;
995 const char *object_get_typename(const Object *obj)
997 return obj->class->type->name;
1000 ObjectClass *object_get_class(Object *obj)
1002 return obj->class;
1005 bool object_class_is_abstract(ObjectClass *klass)
1007 return klass->type->abstract;
1010 const char *object_class_get_name(ObjectClass *klass)
1012 return klass->type->name;
1015 ObjectClass *object_class_by_name(const char *typename)
1017 TypeImpl *type = type_get_by_name(typename);
1019 if (!type) {
1020 return NULL;
1023 type_initialize(type);
1025 return type->class;
1028 ObjectClass *module_object_class_by_name(const char *typename)
1030 ObjectClass *oc;
1032 oc = object_class_by_name(typename);
1033 #ifdef CONFIG_MODULES
1034 if (!oc) {
1035 module_load_qom_one(typename);
1036 oc = object_class_by_name(typename);
1038 #endif
1039 return oc;
1042 ObjectClass *object_class_get_parent(ObjectClass *class)
1044 TypeImpl *type = type_get_parent(class->type);
1046 if (!type) {
1047 return NULL;
1050 type_initialize(type);
1052 return type->class;
1055 typedef struct OCFData
1057 void (*fn)(ObjectClass *klass, void *opaque);
1058 const char *implements_type;
1059 bool include_abstract;
1060 void *opaque;
1061 } OCFData;
1063 static void object_class_foreach_tramp(gpointer key, gpointer value,
1064 gpointer opaque)
1066 OCFData *data = opaque;
1067 TypeImpl *type = value;
1068 ObjectClass *k;
1070 type_initialize(type);
1071 k = type->class;
1073 if (!data->include_abstract && type->abstract) {
1074 return;
1077 if (data->implements_type &&
1078 !object_class_dynamic_cast(k, data->implements_type)) {
1079 return;
1082 data->fn(k, data->opaque);
1085 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1086 const char *implements_type, bool include_abstract,
1087 void *opaque)
1089 OCFData data = { fn, implements_type, include_abstract, opaque };
1091 enumerating_types = true;
1092 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1093 enumerating_types = false;
1096 static int do_object_child_foreach(Object *obj,
1097 int (*fn)(Object *child, void *opaque),
1098 void *opaque, bool recurse)
1100 GHashTableIter iter;
1101 ObjectProperty *prop;
1102 int ret = 0;
1104 g_hash_table_iter_init(&iter, obj->properties);
1105 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1106 if (object_property_is_child(prop)) {
1107 Object *child = prop->opaque;
1109 ret = fn(child, opaque);
1110 if (ret != 0) {
1111 break;
1113 if (recurse) {
1114 ret = do_object_child_foreach(child, fn, opaque, true);
1115 if (ret != 0) {
1116 break;
1121 return ret;
1124 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1125 void *opaque)
1127 return do_object_child_foreach(obj, fn, opaque, false);
1130 int object_child_foreach_recursive(Object *obj,
1131 int (*fn)(Object *child, void *opaque),
1132 void *opaque)
1134 return do_object_child_foreach(obj, fn, opaque, true);
1137 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1139 GSList **list = opaque;
1141 *list = g_slist_prepend(*list, klass);
1144 GSList *object_class_get_list(const char *implements_type,
1145 bool include_abstract)
1147 GSList *list = NULL;
1149 object_class_foreach(object_class_get_list_tramp,
1150 implements_type, include_abstract, &list);
1151 return list;
1154 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1156 return strcasecmp(object_class_get_name((ObjectClass *)a),
1157 object_class_get_name((ObjectClass *)b));
1160 GSList *object_class_get_list_sorted(const char *implements_type,
1161 bool include_abstract)
1163 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1164 object_class_cmp);
1167 Object *object_ref(void *objptr)
1169 Object *obj = OBJECT(objptr);
1170 if (!obj) {
1171 return NULL;
1173 qatomic_inc(&obj->ref);
1174 return obj;
1177 void object_unref(void *objptr)
1179 Object *obj = OBJECT(objptr);
1180 if (!obj) {
1181 return;
1183 g_assert(obj->ref > 0);
1185 /* parent always holds a reference to its children */
1186 if (qatomic_fetch_dec(&obj->ref) == 1) {
1187 object_finalize(obj);
1191 ObjectProperty *
1192 object_property_try_add(Object *obj, const char *name, const char *type,
1193 ObjectPropertyAccessor *get,
1194 ObjectPropertyAccessor *set,
1195 ObjectPropertyRelease *release,
1196 void *opaque, Error **errp)
1198 ObjectProperty *prop;
1199 size_t name_len = strlen(name);
1201 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1202 int i;
1203 ObjectProperty *ret = NULL;
1204 char *name_no_array = g_strdup(name);
1206 name_no_array[name_len - 3] = '\0';
1207 for (i = 0; i < INT16_MAX; ++i) {
1208 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1210 ret = object_property_try_add(obj, full_name, type, get, set,
1211 release, opaque, NULL);
1212 g_free(full_name);
1213 if (ret) {
1214 break;
1217 g_free(name_no_array);
1218 assert(ret);
1219 return ret;
1222 if (object_property_find(obj, name) != NULL) {
1223 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1224 name, object_get_typename(obj));
1225 return NULL;
1228 prop = g_malloc0(sizeof(*prop));
1230 prop->name = g_strdup(name);
1231 prop->type = g_strdup(type);
1233 prop->get = get;
1234 prop->set = set;
1235 prop->release = release;
1236 prop->opaque = opaque;
1238 g_hash_table_insert(obj->properties, prop->name, prop);
1239 return prop;
1242 ObjectProperty *
1243 object_property_add(Object *obj, const char *name, const char *type,
1244 ObjectPropertyAccessor *get,
1245 ObjectPropertyAccessor *set,
1246 ObjectPropertyRelease *release,
1247 void *opaque)
1249 return object_property_try_add(obj, name, type, get, set, release,
1250 opaque, &error_abort);
1253 ObjectProperty *
1254 object_class_property_add(ObjectClass *klass,
1255 const char *name,
1256 const char *type,
1257 ObjectPropertyAccessor *get,
1258 ObjectPropertyAccessor *set,
1259 ObjectPropertyRelease *release,
1260 void *opaque)
1262 ObjectProperty *prop;
1264 assert(!object_class_property_find(klass, name));
1266 prop = g_malloc0(sizeof(*prop));
1268 prop->name = g_strdup(name);
1269 prop->type = g_strdup(type);
1271 prop->get = get;
1272 prop->set = set;
1273 prop->release = release;
1274 prop->opaque = opaque;
1276 g_hash_table_insert(klass->properties, prop->name, prop);
1278 return prop;
1281 ObjectProperty *object_property_find(Object *obj, const char *name)
1283 ObjectProperty *prop;
1284 ObjectClass *klass = object_get_class(obj);
1286 prop = object_class_property_find(klass, name);
1287 if (prop) {
1288 return prop;
1291 return g_hash_table_lookup(obj->properties, name);
1294 ObjectProperty *object_property_find_err(Object *obj, const char *name,
1295 Error **errp)
1297 ObjectProperty *prop = object_property_find(obj, name);
1298 if (!prop) {
1299 error_setg(errp, "Property '%s.%s' not found",
1300 object_get_typename(obj), name);
1302 return prop;
1305 void object_property_iter_init(ObjectPropertyIterator *iter,
1306 Object *obj)
1308 g_hash_table_iter_init(&iter->iter, obj->properties);
1309 iter->nextclass = object_get_class(obj);
1312 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1314 gpointer key, val;
1315 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1316 if (!iter->nextclass) {
1317 return NULL;
1319 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1320 iter->nextclass = object_class_get_parent(iter->nextclass);
1322 return val;
1325 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1326 ObjectClass *klass)
1328 g_hash_table_iter_init(&iter->iter, klass->properties);
1329 iter->nextclass = object_class_get_parent(klass);
1332 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name)
1334 ObjectClass *parent_klass;
1336 parent_klass = object_class_get_parent(klass);
1337 if (parent_klass) {
1338 ObjectProperty *prop =
1339 object_class_property_find(parent_klass, name);
1340 if (prop) {
1341 return prop;
1345 return g_hash_table_lookup(klass->properties, name);
1348 ObjectProperty *object_class_property_find_err(ObjectClass *klass,
1349 const char *name,
1350 Error **errp)
1352 ObjectProperty *prop = object_class_property_find(klass, name);
1353 if (!prop) {
1354 error_setg(errp, "Property '.%s' not found", name);
1356 return prop;
1360 void object_property_del(Object *obj, const char *name)
1362 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1364 if (prop->release) {
1365 prop->release(obj, name, prop->opaque);
1367 g_hash_table_remove(obj->properties, name);
1370 bool object_property_get(Object *obj, const char *name, Visitor *v,
1371 Error **errp)
1373 Error *err = NULL;
1374 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1376 if (prop == NULL) {
1377 return false;
1380 if (!prop->get) {
1381 error_setg(errp, QERR_PERMISSION_DENIED);
1382 return false;
1384 prop->get(obj, v, name, prop->opaque, &err);
1385 error_propagate(errp, err);
1386 return !err;
1389 bool object_property_set(Object *obj, const char *name, Visitor *v,
1390 Error **errp)
1392 Error *err = NULL;
1393 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1395 if (prop == NULL) {
1396 return false;
1399 if (!prop->set) {
1400 error_setg(errp, QERR_PERMISSION_DENIED);
1401 return false;
1403 prop->set(obj, v, name, prop->opaque, &err);
1404 error_propagate(errp, err);
1405 return !err;
1408 bool object_property_set_str(Object *obj, const char *name,
1409 const char *value, Error **errp)
1411 QString *qstr = qstring_from_str(value);
1412 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
1414 qobject_unref(qstr);
1415 return ok;
1418 char *object_property_get_str(Object *obj, const char *name,
1419 Error **errp)
1421 QObject *ret = object_property_get_qobject(obj, name, errp);
1422 QString *qstring;
1423 char *retval;
1425 if (!ret) {
1426 return NULL;
1428 qstring = qobject_to(QString, ret);
1429 if (!qstring) {
1430 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1431 retval = NULL;
1432 } else {
1433 retval = g_strdup(qstring_get_str(qstring));
1436 qobject_unref(ret);
1437 return retval;
1440 bool object_property_set_link(Object *obj, const char *name,
1441 Object *value, Error **errp)
1443 g_autofree char *path = NULL;
1445 if (value) {
1446 path = object_get_canonical_path(value);
1448 return object_property_set_str(obj, name, path ?: "", errp);
1451 Object *object_property_get_link(Object *obj, const char *name,
1452 Error **errp)
1454 char *str = object_property_get_str(obj, name, errp);
1455 Object *target = NULL;
1457 if (str && *str) {
1458 target = object_resolve_path(str, NULL);
1459 if (!target) {
1460 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1461 "Device '%s' not found", str);
1465 g_free(str);
1466 return target;
1469 bool object_property_set_bool(Object *obj, const char *name,
1470 bool value, Error **errp)
1472 QBool *qbool = qbool_from_bool(value);
1473 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
1475 qobject_unref(qbool);
1476 return ok;
1479 bool object_property_get_bool(Object *obj, const char *name,
1480 Error **errp)
1482 QObject *ret = object_property_get_qobject(obj, name, errp);
1483 QBool *qbool;
1484 bool retval;
1486 if (!ret) {
1487 return false;
1489 qbool = qobject_to(QBool, ret);
1490 if (!qbool) {
1491 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1492 retval = false;
1493 } else {
1494 retval = qbool_get_bool(qbool);
1497 qobject_unref(ret);
1498 return retval;
1501 bool object_property_set_int(Object *obj, const char *name,
1502 int64_t value, Error **errp)
1504 QNum *qnum = qnum_from_int(value);
1505 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1507 qobject_unref(qnum);
1508 return ok;
1511 int64_t object_property_get_int(Object *obj, const char *name,
1512 Error **errp)
1514 QObject *ret = object_property_get_qobject(obj, name, errp);
1515 QNum *qnum;
1516 int64_t retval;
1518 if (!ret) {
1519 return -1;
1522 qnum = qobject_to(QNum, ret);
1523 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1524 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1525 retval = -1;
1528 qobject_unref(ret);
1529 return retval;
1532 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1534 Visitor *v = qobject_input_visitor_new(prop->defval);
1536 assert(prop->set != NULL);
1537 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1539 visit_free(v);
1542 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1544 assert(!prop->defval);
1545 assert(!prop->init);
1547 prop->defval = defval;
1548 prop->init = object_property_init_defval;
1551 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1553 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1556 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1558 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1561 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1563 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1566 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1568 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1571 bool object_property_set_uint(Object *obj, const char *name,
1572 uint64_t value, Error **errp)
1574 QNum *qnum = qnum_from_uint(value);
1575 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1577 qobject_unref(qnum);
1578 return ok;
1581 uint64_t object_property_get_uint(Object *obj, const char *name,
1582 Error **errp)
1584 QObject *ret = object_property_get_qobject(obj, name, errp);
1585 QNum *qnum;
1586 uint64_t retval;
1588 if (!ret) {
1589 return 0;
1591 qnum = qobject_to(QNum, ret);
1592 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1593 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1594 retval = 0;
1597 qobject_unref(ret);
1598 return retval;
1601 typedef struct EnumProperty {
1602 const QEnumLookup *lookup;
1603 int (*get)(Object *, Error **);
1604 void (*set)(Object *, int, Error **);
1605 } EnumProperty;
1607 int object_property_get_enum(Object *obj, const char *name,
1608 const char *typename, Error **errp)
1610 char *str;
1611 int ret;
1612 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1613 EnumProperty *enumprop;
1615 if (prop == NULL) {
1616 return -1;
1619 if (!g_str_equal(prop->type, typename)) {
1620 error_setg(errp, "Property %s on %s is not '%s' enum type",
1621 name, object_class_get_name(
1622 object_get_class(obj)), typename);
1623 return -1;
1626 enumprop = prop->opaque;
1628 str = object_property_get_str(obj, name, errp);
1629 if (!str) {
1630 return -1;
1633 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1634 g_free(str);
1636 return ret;
1639 bool object_property_parse(Object *obj, const char *name,
1640 const char *string, Error **errp)
1642 Visitor *v = string_input_visitor_new(string);
1643 bool ok = object_property_set(obj, name, v, errp);
1645 visit_free(v);
1646 return ok;
1649 char *object_property_print(Object *obj, const char *name, bool human,
1650 Error **errp)
1652 Visitor *v;
1653 char *string = NULL;
1655 v = string_output_visitor_new(human, &string);
1656 if (!object_property_get(obj, name, v, errp)) {
1657 goto out;
1660 visit_complete(v, &string);
1662 out:
1663 visit_free(v);
1664 return string;
1667 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1669 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1670 if (prop == NULL) {
1671 return NULL;
1674 return prop->type;
1677 Object *object_get_root(void)
1679 static Object *root;
1681 if (!root) {
1682 root = object_new("container");
1685 return root;
1688 Object *object_get_objects_root(void)
1690 return container_get(object_get_root(), "/objects");
1693 Object *object_get_internal_root(void)
1695 static Object *internal_root;
1697 if (!internal_root) {
1698 internal_root = object_new("container");
1701 return internal_root;
1704 static void object_get_child_property(Object *obj, Visitor *v,
1705 const char *name, void *opaque,
1706 Error **errp)
1708 Object *child = opaque;
1709 char *path;
1711 path = object_get_canonical_path(child);
1712 visit_type_str(v, name, &path, errp);
1713 g_free(path);
1716 static Object *object_resolve_child_property(Object *parent, void *opaque,
1717 const char *part)
1719 return opaque;
1722 static void object_finalize_child_property(Object *obj, const char *name,
1723 void *opaque)
1725 Object *child = opaque;
1727 if (child->class->unparent) {
1728 (child->class->unparent)(child);
1730 child->parent = NULL;
1731 object_unref(child);
1734 ObjectProperty *
1735 object_property_try_add_child(Object *obj, const char *name,
1736 Object *child, Error **errp)
1738 g_autofree char *type = NULL;
1739 ObjectProperty *op;
1741 assert(!child->parent);
1743 type = g_strdup_printf("child<%s>", object_get_typename(child));
1745 op = object_property_try_add(obj, name, type, object_get_child_property,
1746 NULL, object_finalize_child_property,
1747 child, errp);
1748 if (!op) {
1749 return NULL;
1751 op->resolve = object_resolve_child_property;
1752 object_ref(child);
1753 child->parent = obj;
1754 return op;
1757 ObjectProperty *
1758 object_property_add_child(Object *obj, const char *name,
1759 Object *child)
1761 return object_property_try_add_child(obj, name, child, &error_abort);
1764 void object_property_allow_set_link(const Object *obj, const char *name,
1765 Object *val, Error **errp)
1767 /* Allow the link to be set, always */
1770 typedef struct {
1771 union {
1772 Object **targetp;
1773 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1774 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1776 void (*check)(const Object *, const char *, Object *, Error **);
1777 ObjectPropertyLinkFlags flags;
1778 } LinkProperty;
1780 static Object **
1781 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1783 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1784 return &lprop->target;
1785 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1786 return (void *)obj + lprop->offset;
1787 } else {
1788 return lprop->targetp;
1792 static void object_get_link_property(Object *obj, Visitor *v,
1793 const char *name, void *opaque,
1794 Error **errp)
1796 LinkProperty *lprop = opaque;
1797 Object **targetp = object_link_get_targetp(obj, lprop);
1798 char *path;
1800 if (*targetp) {
1801 path = object_get_canonical_path(*targetp);
1802 visit_type_str(v, name, &path, errp);
1803 g_free(path);
1804 } else {
1805 path = (char *)"";
1806 visit_type_str(v, name, &path, errp);
1811 * object_resolve_link:
1813 * Lookup an object and ensure its type matches the link property type. This
1814 * is similar to object_resolve_path() except type verification against the
1815 * link property is performed.
1817 * Returns: The matched object or NULL on path lookup failures.
1819 static Object *object_resolve_link(Object *obj, const char *name,
1820 const char *path, Error **errp)
1822 const char *type;
1823 char *target_type;
1824 bool ambiguous = false;
1825 Object *target;
1827 /* Go from link<FOO> to FOO. */
1828 type = object_property_get_type(obj, name, NULL);
1829 target_type = g_strndup(&type[5], strlen(type) - 6);
1830 target = object_resolve_path_type(path, target_type, &ambiguous);
1832 if (ambiguous) {
1833 error_setg(errp, "Path '%s' does not uniquely identify an object",
1834 path);
1835 } else if (!target) {
1836 target = object_resolve_path(path, &ambiguous);
1837 if (target || ambiguous) {
1838 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1839 } else {
1840 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1841 "Device '%s' not found", path);
1843 target = NULL;
1845 g_free(target_type);
1847 return target;
1850 static void object_set_link_property(Object *obj, Visitor *v,
1851 const char *name, void *opaque,
1852 Error **errp)
1854 Error *local_err = NULL;
1855 LinkProperty *prop = opaque;
1856 Object **targetp = object_link_get_targetp(obj, prop);
1857 Object *old_target = *targetp;
1858 Object *new_target;
1859 char *path = NULL;
1861 if (!visit_type_str(v, name, &path, errp)) {
1862 return;
1865 if (*path) {
1866 new_target = object_resolve_link(obj, name, path, errp);
1867 if (!new_target) {
1868 g_free(path);
1869 return;
1871 } else {
1872 new_target = NULL;
1875 g_free(path);
1877 prop->check(obj, name, new_target, &local_err);
1878 if (local_err) {
1879 error_propagate(errp, local_err);
1880 return;
1883 *targetp = new_target;
1884 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1885 object_ref(new_target);
1886 object_unref(old_target);
1890 static Object *object_resolve_link_property(Object *parent, void *opaque,
1891 const char *part)
1893 LinkProperty *lprop = opaque;
1895 return *object_link_get_targetp(parent, lprop);
1898 static void object_release_link_property(Object *obj, const char *name,
1899 void *opaque)
1901 LinkProperty *prop = opaque;
1902 Object **targetp = object_link_get_targetp(obj, prop);
1904 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1905 object_unref(*targetp);
1907 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1908 g_free(prop);
1912 static ObjectProperty *
1913 object_add_link_prop(Object *obj, const char *name,
1914 const char *type, void *ptr,
1915 void (*check)(const Object *, const char *,
1916 Object *, Error **),
1917 ObjectPropertyLinkFlags flags)
1919 LinkProperty *prop = g_malloc(sizeof(*prop));
1920 g_autofree char *full_type = NULL;
1921 ObjectProperty *op;
1923 if (flags & OBJ_PROP_LINK_DIRECT) {
1924 prop->target = ptr;
1925 } else {
1926 prop->targetp = ptr;
1928 prop->check = check;
1929 prop->flags = flags;
1931 full_type = g_strdup_printf("link<%s>", type);
1933 op = object_property_add(obj, name, full_type,
1934 object_get_link_property,
1935 check ? object_set_link_property : NULL,
1936 object_release_link_property,
1937 prop);
1938 op->resolve = object_resolve_link_property;
1939 return op;
1942 ObjectProperty *
1943 object_property_add_link(Object *obj, const char *name,
1944 const char *type, Object **targetp,
1945 void (*check)(const Object *, const char *,
1946 Object *, Error **),
1947 ObjectPropertyLinkFlags flags)
1949 return object_add_link_prop(obj, name, type, targetp, check, flags);
1952 ObjectProperty *
1953 object_class_property_add_link(ObjectClass *oc,
1954 const char *name,
1955 const char *type, ptrdiff_t offset,
1956 void (*check)(const Object *obj, const char *name,
1957 Object *val, Error **errp),
1958 ObjectPropertyLinkFlags flags)
1960 LinkProperty *prop = g_new0(LinkProperty, 1);
1961 char *full_type;
1962 ObjectProperty *op;
1964 prop->offset = offset;
1965 prop->check = check;
1966 prop->flags = flags | OBJ_PROP_LINK_CLASS;
1968 full_type = g_strdup_printf("link<%s>", type);
1970 op = object_class_property_add(oc, name, full_type,
1971 object_get_link_property,
1972 check ? object_set_link_property : NULL,
1973 object_release_link_property,
1974 prop);
1976 op->resolve = object_resolve_link_property;
1978 g_free(full_type);
1979 return op;
1982 ObjectProperty *
1983 object_property_add_const_link(Object *obj, const char *name,
1984 Object *target)
1986 return object_add_link_prop(obj, name,
1987 object_get_typename(target), target,
1988 NULL, OBJ_PROP_LINK_DIRECT);
1991 const char *object_get_canonical_path_component(const Object *obj)
1993 ObjectProperty *prop = NULL;
1994 GHashTableIter iter;
1996 if (obj->parent == NULL) {
1997 return NULL;
2000 g_hash_table_iter_init(&iter, obj->parent->properties);
2001 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2002 if (!object_property_is_child(prop)) {
2003 continue;
2006 if (prop->opaque == obj) {
2007 return prop->name;
2011 /* obj had a parent but was not a child, should never happen */
2012 g_assert_not_reached();
2013 return NULL;
2016 char *object_get_canonical_path(const Object *obj)
2018 Object *root = object_get_root();
2019 char *newpath, *path = NULL;
2021 if (obj == root) {
2022 return g_strdup("/");
2025 do {
2026 const char *component = object_get_canonical_path_component(obj);
2028 if (!component) {
2029 /* A canonical path must be complete, so discard what was
2030 * collected so far.
2032 g_free(path);
2033 return NULL;
2036 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
2037 g_free(path);
2038 path = newpath;
2039 obj = obj->parent;
2040 } while (obj != root);
2042 return path;
2045 Object *object_resolve_path_component(Object *parent, const char *part)
2047 ObjectProperty *prop = object_property_find(parent, part);
2048 if (prop == NULL) {
2049 return NULL;
2052 if (prop->resolve) {
2053 return prop->resolve(parent, prop->opaque, part);
2054 } else {
2055 return NULL;
2059 static Object *object_resolve_abs_path(Object *parent,
2060 char **parts,
2061 const char *typename)
2063 Object *child;
2065 if (*parts == NULL) {
2066 return object_dynamic_cast(parent, typename);
2069 if (strcmp(*parts, "") == 0) {
2070 return object_resolve_abs_path(parent, parts + 1, typename);
2073 child = object_resolve_path_component(parent, *parts);
2074 if (!child) {
2075 return NULL;
2078 return object_resolve_abs_path(child, parts + 1, typename);
2081 static Object *object_resolve_partial_path(Object *parent,
2082 char **parts,
2083 const char *typename,
2084 bool *ambiguous)
2086 Object *obj;
2087 GHashTableIter iter;
2088 ObjectProperty *prop;
2090 obj = object_resolve_abs_path(parent, parts, typename);
2092 g_hash_table_iter_init(&iter, parent->properties);
2093 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2094 Object *found;
2096 if (!object_property_is_child(prop)) {
2097 continue;
2100 found = object_resolve_partial_path(prop->opaque, parts,
2101 typename, ambiguous);
2102 if (found) {
2103 if (obj) {
2104 *ambiguous = true;
2105 return NULL;
2107 obj = found;
2110 if (*ambiguous) {
2111 return NULL;
2115 return obj;
2118 Object *object_resolve_path_type(const char *path, const char *typename,
2119 bool *ambiguousp)
2121 Object *obj;
2122 char **parts;
2124 parts = g_strsplit(path, "/", 0);
2125 assert(parts);
2127 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2128 bool ambiguous = false;
2129 obj = object_resolve_partial_path(object_get_root(), parts,
2130 typename, &ambiguous);
2131 if (ambiguousp) {
2132 *ambiguousp = ambiguous;
2134 } else {
2135 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2138 g_strfreev(parts);
2140 return obj;
2143 Object *object_resolve_path(const char *path, bool *ambiguous)
2145 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2148 typedef struct StringProperty
2150 char *(*get)(Object *, Error **);
2151 void (*set)(Object *, const char *, Error **);
2152 } StringProperty;
2154 static void property_get_str(Object *obj, Visitor *v, const char *name,
2155 void *opaque, Error **errp)
2157 StringProperty *prop = opaque;
2158 char *value;
2159 Error *err = NULL;
2161 value = prop->get(obj, &err);
2162 if (err) {
2163 error_propagate(errp, err);
2164 return;
2167 visit_type_str(v, name, &value, errp);
2168 g_free(value);
2171 static void property_set_str(Object *obj, Visitor *v, const char *name,
2172 void *opaque, Error **errp)
2174 StringProperty *prop = opaque;
2175 char *value;
2177 if (!visit_type_str(v, name, &value, errp)) {
2178 return;
2181 prop->set(obj, value, errp);
2182 g_free(value);
2185 static void property_release_data(Object *obj, const char *name,
2186 void *opaque)
2188 g_free(opaque);
2191 ObjectProperty *
2192 object_property_add_str(Object *obj, const char *name,
2193 char *(*get)(Object *, Error **),
2194 void (*set)(Object *, const char *, Error **))
2196 StringProperty *prop = g_malloc0(sizeof(*prop));
2198 prop->get = get;
2199 prop->set = set;
2201 return object_property_add(obj, name, "string",
2202 get ? property_get_str : NULL,
2203 set ? property_set_str : NULL,
2204 property_release_data,
2205 prop);
2208 ObjectProperty *
2209 object_class_property_add_str(ObjectClass *klass, const char *name,
2210 char *(*get)(Object *, Error **),
2211 void (*set)(Object *, const char *,
2212 Error **))
2214 StringProperty *prop = g_malloc0(sizeof(*prop));
2216 prop->get = get;
2217 prop->set = set;
2219 return object_class_property_add(klass, name, "string",
2220 get ? property_get_str : NULL,
2221 set ? property_set_str : NULL,
2222 NULL,
2223 prop);
2226 typedef struct BoolProperty
2228 bool (*get)(Object *, Error **);
2229 void (*set)(Object *, bool, Error **);
2230 } BoolProperty;
2232 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2233 void *opaque, Error **errp)
2235 BoolProperty *prop = opaque;
2236 bool value;
2237 Error *err = NULL;
2239 value = prop->get(obj, &err);
2240 if (err) {
2241 error_propagate(errp, err);
2242 return;
2245 visit_type_bool(v, name, &value, errp);
2248 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2249 void *opaque, Error **errp)
2251 BoolProperty *prop = opaque;
2252 bool value;
2254 if (!visit_type_bool(v, name, &value, errp)) {
2255 return;
2258 prop->set(obj, value, errp);
2261 ObjectProperty *
2262 object_property_add_bool(Object *obj, const char *name,
2263 bool (*get)(Object *, Error **),
2264 void (*set)(Object *, bool, Error **))
2266 BoolProperty *prop = g_malloc0(sizeof(*prop));
2268 prop->get = get;
2269 prop->set = set;
2271 return object_property_add(obj, name, "bool",
2272 get ? property_get_bool : NULL,
2273 set ? property_set_bool : NULL,
2274 property_release_data,
2275 prop);
2278 ObjectProperty *
2279 object_class_property_add_bool(ObjectClass *klass, const char *name,
2280 bool (*get)(Object *, Error **),
2281 void (*set)(Object *, bool, Error **))
2283 BoolProperty *prop = g_malloc0(sizeof(*prop));
2285 prop->get = get;
2286 prop->set = set;
2288 return object_class_property_add(klass, name, "bool",
2289 get ? property_get_bool : NULL,
2290 set ? property_set_bool : NULL,
2291 NULL,
2292 prop);
2295 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2296 void *opaque, Error **errp)
2298 EnumProperty *prop = opaque;
2299 int value;
2300 Error *err = NULL;
2302 value = prop->get(obj, &err);
2303 if (err) {
2304 error_propagate(errp, err);
2305 return;
2308 visit_type_enum(v, name, &value, prop->lookup, errp);
2311 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2312 void *opaque, Error **errp)
2314 EnumProperty *prop = opaque;
2315 int value;
2317 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
2318 return;
2320 prop->set(obj, value, errp);
2323 ObjectProperty *
2324 object_property_add_enum(Object *obj, const char *name,
2325 const char *typename,
2326 const QEnumLookup *lookup,
2327 int (*get)(Object *, Error **),
2328 void (*set)(Object *, int, Error **))
2330 EnumProperty *prop = g_malloc(sizeof(*prop));
2332 prop->lookup = lookup;
2333 prop->get = get;
2334 prop->set = set;
2336 return object_property_add(obj, name, typename,
2337 get ? property_get_enum : NULL,
2338 set ? property_set_enum : NULL,
2339 property_release_data,
2340 prop);
2343 ObjectProperty *
2344 object_class_property_add_enum(ObjectClass *klass, const char *name,
2345 const char *typename,
2346 const QEnumLookup *lookup,
2347 int (*get)(Object *, Error **),
2348 void (*set)(Object *, int, Error **))
2350 EnumProperty *prop = g_malloc(sizeof(*prop));
2352 prop->lookup = lookup;
2353 prop->get = get;
2354 prop->set = set;
2356 return object_class_property_add(klass, name, typename,
2357 get ? property_get_enum : NULL,
2358 set ? property_set_enum : NULL,
2359 NULL,
2360 prop);
2363 typedef struct TMProperty {
2364 void (*get)(Object *, struct tm *, Error **);
2365 } TMProperty;
2367 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2368 void *opaque, Error **errp)
2370 TMProperty *prop = opaque;
2371 Error *err = NULL;
2372 struct tm value;
2374 prop->get(obj, &value, &err);
2375 if (err) {
2376 error_propagate(errp, err);
2377 return;
2380 if (!visit_start_struct(v, name, NULL, 0, errp)) {
2381 return;
2383 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
2384 goto out_end;
2386 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
2387 goto out_end;
2389 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
2390 goto out_end;
2392 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
2393 goto out_end;
2395 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
2396 goto out_end;
2398 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
2399 goto out_end;
2401 visit_check_struct(v, errp);
2402 out_end:
2403 visit_end_struct(v, NULL);
2406 ObjectProperty *
2407 object_property_add_tm(Object *obj, const char *name,
2408 void (*get)(Object *, struct tm *, Error **))
2410 TMProperty *prop = g_malloc0(sizeof(*prop));
2412 prop->get = get;
2414 return object_property_add(obj, name, "struct tm",
2415 get ? property_get_tm : NULL, NULL,
2416 property_release_data,
2417 prop);
2420 ObjectProperty *
2421 object_class_property_add_tm(ObjectClass *klass, const char *name,
2422 void (*get)(Object *, struct tm *, Error **))
2424 TMProperty *prop = g_malloc0(sizeof(*prop));
2426 prop->get = get;
2428 return object_class_property_add(klass, name, "struct tm",
2429 get ? property_get_tm : NULL,
2430 NULL, NULL, prop);
2433 static char *object_get_type(Object *obj, Error **errp)
2435 return g_strdup(object_get_typename(obj));
2438 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2439 void *opaque, Error **errp)
2441 uint8_t value = *(uint8_t *)opaque;
2442 visit_type_uint8(v, name, &value, errp);
2445 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2446 void *opaque, Error **errp)
2448 uint8_t *field = opaque;
2449 uint8_t value;
2451 if (!visit_type_uint8(v, name, &value, errp)) {
2452 return;
2455 *field = value;
2458 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2459 void *opaque, Error **errp)
2461 uint16_t value = *(uint16_t *)opaque;
2462 visit_type_uint16(v, name, &value, errp);
2465 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2466 void *opaque, Error **errp)
2468 uint16_t *field = opaque;
2469 uint16_t value;
2471 if (!visit_type_uint16(v, name, &value, errp)) {
2472 return;
2475 *field = value;
2478 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2479 void *opaque, Error **errp)
2481 uint32_t value = *(uint32_t *)opaque;
2482 visit_type_uint32(v, name, &value, errp);
2485 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2486 void *opaque, Error **errp)
2488 uint32_t *field = opaque;
2489 uint32_t value;
2491 if (!visit_type_uint32(v, name, &value, errp)) {
2492 return;
2495 *field = value;
2498 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2499 void *opaque, Error **errp)
2501 uint64_t value = *(uint64_t *)opaque;
2502 visit_type_uint64(v, name, &value, errp);
2505 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2506 void *opaque, Error **errp)
2508 uint64_t *field = opaque;
2509 uint64_t value;
2511 if (!visit_type_uint64(v, name, &value, errp)) {
2512 return;
2515 *field = value;
2518 ObjectProperty *
2519 object_property_add_uint8_ptr(Object *obj, const char *name,
2520 const uint8_t *v,
2521 ObjectPropertyFlags flags)
2523 ObjectPropertyAccessor *getter = NULL;
2524 ObjectPropertyAccessor *setter = NULL;
2526 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2527 getter = property_get_uint8_ptr;
2530 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2531 setter = property_set_uint8_ptr;
2534 return object_property_add(obj, name, "uint8",
2535 getter, setter, NULL, (void *)v);
2538 ObjectProperty *
2539 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2540 const uint8_t *v,
2541 ObjectPropertyFlags flags)
2543 ObjectPropertyAccessor *getter = NULL;
2544 ObjectPropertyAccessor *setter = NULL;
2546 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2547 getter = property_get_uint8_ptr;
2550 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2551 setter = property_set_uint8_ptr;
2554 return object_class_property_add(klass, name, "uint8",
2555 getter, setter, NULL, (void *)v);
2558 ObjectProperty *
2559 object_property_add_uint16_ptr(Object *obj, const char *name,
2560 const uint16_t *v,
2561 ObjectPropertyFlags flags)
2563 ObjectPropertyAccessor *getter = NULL;
2564 ObjectPropertyAccessor *setter = NULL;
2566 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2567 getter = property_get_uint16_ptr;
2570 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2571 setter = property_set_uint16_ptr;
2574 return object_property_add(obj, name, "uint16",
2575 getter, setter, NULL, (void *)v);
2578 ObjectProperty *
2579 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2580 const uint16_t *v,
2581 ObjectPropertyFlags flags)
2583 ObjectPropertyAccessor *getter = NULL;
2584 ObjectPropertyAccessor *setter = NULL;
2586 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2587 getter = property_get_uint16_ptr;
2590 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2591 setter = property_set_uint16_ptr;
2594 return object_class_property_add(klass, name, "uint16",
2595 getter, setter, NULL, (void *)v);
2598 ObjectProperty *
2599 object_property_add_uint32_ptr(Object *obj, const char *name,
2600 const uint32_t *v,
2601 ObjectPropertyFlags flags)
2603 ObjectPropertyAccessor *getter = NULL;
2604 ObjectPropertyAccessor *setter = NULL;
2606 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2607 getter = property_get_uint32_ptr;
2610 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2611 setter = property_set_uint32_ptr;
2614 return object_property_add(obj, name, "uint32",
2615 getter, setter, NULL, (void *)v);
2618 ObjectProperty *
2619 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2620 const uint32_t *v,
2621 ObjectPropertyFlags flags)
2623 ObjectPropertyAccessor *getter = NULL;
2624 ObjectPropertyAccessor *setter = NULL;
2626 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2627 getter = property_get_uint32_ptr;
2630 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2631 setter = property_set_uint32_ptr;
2634 return object_class_property_add(klass, name, "uint32",
2635 getter, setter, NULL, (void *)v);
2638 ObjectProperty *
2639 object_property_add_uint64_ptr(Object *obj, const char *name,
2640 const uint64_t *v,
2641 ObjectPropertyFlags flags)
2643 ObjectPropertyAccessor *getter = NULL;
2644 ObjectPropertyAccessor *setter = NULL;
2646 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2647 getter = property_get_uint64_ptr;
2650 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2651 setter = property_set_uint64_ptr;
2654 return object_property_add(obj, name, "uint64",
2655 getter, setter, NULL, (void *)v);
2658 ObjectProperty *
2659 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2660 const uint64_t *v,
2661 ObjectPropertyFlags flags)
2663 ObjectPropertyAccessor *getter = NULL;
2664 ObjectPropertyAccessor *setter = NULL;
2666 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2667 getter = property_get_uint64_ptr;
2670 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2671 setter = property_set_uint64_ptr;
2674 return object_class_property_add(klass, name, "uint64",
2675 getter, setter, NULL, (void *)v);
2678 typedef struct {
2679 Object *target_obj;
2680 char *target_name;
2681 } AliasProperty;
2683 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2684 void *opaque, Error **errp)
2686 AliasProperty *prop = opaque;
2687 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2689 object_property_get(prop->target_obj, prop->target_name, alias_v, errp);
2690 visit_free(alias_v);
2693 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2694 void *opaque, Error **errp)
2696 AliasProperty *prop = opaque;
2697 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2699 object_property_set(prop->target_obj, prop->target_name, alias_v, errp);
2700 visit_free(alias_v);
2703 static Object *property_resolve_alias(Object *obj, void *opaque,
2704 const char *part)
2706 AliasProperty *prop = opaque;
2708 return object_resolve_path_component(prop->target_obj, prop->target_name);
2711 static void property_release_alias(Object *obj, const char *name, void *opaque)
2713 AliasProperty *prop = opaque;
2715 g_free(prop->target_name);
2716 g_free(prop);
2719 ObjectProperty *
2720 object_property_add_alias(Object *obj, const char *name,
2721 Object *target_obj, const char *target_name)
2723 AliasProperty *prop;
2724 ObjectProperty *op;
2725 ObjectProperty *target_prop;
2726 g_autofree char *prop_type = NULL;
2728 target_prop = object_property_find_err(target_obj, target_name,
2729 &error_abort);
2731 if (object_property_is_child(target_prop)) {
2732 prop_type = g_strdup_printf("link%s",
2733 target_prop->type + strlen("child"));
2734 } else {
2735 prop_type = g_strdup(target_prop->type);
2738 prop = g_malloc(sizeof(*prop));
2739 prop->target_obj = target_obj;
2740 prop->target_name = g_strdup(target_name);
2742 op = object_property_add(obj, name, prop_type,
2743 property_get_alias,
2744 property_set_alias,
2745 property_release_alias,
2746 prop);
2747 op->resolve = property_resolve_alias;
2748 if (target_prop->defval) {
2749 op->defval = qobject_ref(target_prop->defval);
2752 object_property_set_description(obj, op->name,
2753 target_prop->description);
2754 return op;
2757 void object_property_set_description(Object *obj, const char *name,
2758 const char *description)
2760 ObjectProperty *op;
2762 op = object_property_find_err(obj, name, &error_abort);
2763 g_free(op->description);
2764 op->description = g_strdup(description);
2767 void object_class_property_set_description(ObjectClass *klass,
2768 const char *name,
2769 const char *description)
2771 ObjectProperty *op;
2773 op = g_hash_table_lookup(klass->properties, name);
2774 g_free(op->description);
2775 op->description = g_strdup(description);
2778 static void object_class_init(ObjectClass *klass, void *data)
2780 object_class_property_add_str(klass, "type", object_get_type,
2781 NULL);
2784 static void register_types(void)
2786 static TypeInfo interface_info = {
2787 .name = TYPE_INTERFACE,
2788 .class_size = sizeof(InterfaceClass),
2789 .abstract = true,
2792 static TypeInfo object_info = {
2793 .name = TYPE_OBJECT,
2794 .instance_size = sizeof(Object),
2795 .class_init = object_class_init,
2796 .abstract = true,
2799 type_interface = type_register_internal(&interface_info);
2800 type_register_internal(&object_info);
2803 type_init(register_types)