4 * Copyright IBM, Corp. 2011
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 "qemu/memalign.h"
20 #include "qapi/visitor.h"
21 #include "qapi/string-input-visitor.h"
22 #include "qapi/string-output-visitor.h"
23 #include "qapi/qobject-input-visitor.h"
24 #include "qapi/forward-visitor.h"
25 #include "qapi/qapi-builtin-visit.h"
26 #include "qapi/qmp/qerror.h"
27 #include "qapi/qmp/qjson.h"
30 /* TODO: replace QObject with a simpler visitor to avoid a dependency
31 * of the QOM core on QObject? */
32 #include "qom/qom-qobject.h"
33 #include "qapi/qmp/qbool.h"
34 #include "qapi/qmp/qnum.h"
35 #include "qapi/qmp/qstring.h"
36 #include "qemu/error-report.h"
38 #define MAX_INTERFACES 32
40 typedef struct InterfaceImpl InterfaceImpl;
41 typedef struct TypeImpl TypeImpl;
55 size_t instance_align;
57 void (*class_init)(ObjectClass *klass, void *data);
58 void (*class_base_init)(ObjectClass *klass, void *data);
62 void (*instance_init)(Object *obj);
63 void (*instance_post_init)(Object *obj);
64 void (*instance_finalize)(Object *obj);
69 TypeImpl *parent_type;
74 InterfaceImpl interfaces[MAX_INTERFACES];
77 static Type type_interface;
79 static GHashTable *type_table_get(void)
81 static GHashTable *type_table;
83 if (type_table == NULL) {
84 type_table = g_hash_table_new(g_str_hash, g_str_equal);
90 static bool enumerating_types;
92 static void type_table_add(TypeImpl *ti)
94 assert(!enumerating_types);
95 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
98 static TypeImpl *type_table_lookup(const char *name)
100 return g_hash_table_lookup(type_table_get(), name);
103 static TypeImpl *type_new(const TypeInfo *info)
105 TypeImpl *ti = g_malloc0(sizeof(*ti));
108 g_assert(info->name != NULL);
110 if (type_table_lookup(info->name) != NULL) {
111 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
115 ti->name = g_strdup(info->name);
116 ti->parent = g_strdup(info->parent);
118 ti->class_size = info->class_size;
119 ti->instance_size = info->instance_size;
120 ti->instance_align = info->instance_align;
122 ti->class_init = info->class_init;
123 ti->class_base_init = info->class_base_init;
124 ti->class_data = info->class_data;
126 ti->instance_init = info->instance_init;
127 ti->instance_post_init = info->instance_post_init;
128 ti->instance_finalize = info->instance_finalize;
130 ti->abstract = info->abstract;
132 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
133 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
135 ti->num_interfaces = i;
140 static TypeImpl *type_register_internal(const TypeInfo *info)
149 TypeImpl *type_register(const TypeInfo *info)
151 assert(info->parent);
152 return type_register_internal(info);
155 TypeImpl *type_register_static(const TypeInfo *info)
157 return type_register(info);
160 void type_register_static_array(const TypeInfo *infos, int nr_infos)
164 for (i = 0; i < nr_infos; i++) {
165 type_register_static(&infos[i]);
169 static TypeImpl *type_get_by_name(const char *name)
175 return type_table_lookup(name);
178 static TypeImpl *type_get_parent(TypeImpl *type)
180 if (!type->parent_type && type->parent) {
181 type->parent_type = type_get_by_name(type->parent);
182 if (!type->parent_type) {
183 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
184 type->name, type->parent);
189 return type->parent_type;
192 static bool type_has_parent(TypeImpl *type)
194 return (type->parent != NULL);
197 static size_t type_class_get_size(TypeImpl *ti)
199 if (ti->class_size) {
200 return ti->class_size;
203 if (type_has_parent(ti)) {
204 return type_class_get_size(type_get_parent(ti));
207 return sizeof(ObjectClass);
210 static size_t type_object_get_size(TypeImpl *ti)
212 if (ti->instance_size) {
213 return ti->instance_size;
216 if (type_has_parent(ti)) {
217 return type_object_get_size(type_get_parent(ti));
223 static size_t type_object_get_align(TypeImpl *ti)
225 if (ti->instance_align) {
226 return ti->instance_align;
229 if (type_has_parent(ti)) {
230 return type_object_get_align(type_get_parent(ti));
236 size_t object_type_get_instance_size(const char *typename)
238 TypeImpl *type = type_get_by_name(typename);
240 g_assert(type != NULL);
241 return type_object_get_size(type);
244 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
248 /* Check if target_type is a direct ancestor of type */
250 if (type == target_type) {
254 type = type_get_parent(type);
260 static void type_initialize(TypeImpl *ti);
262 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
263 TypeImpl *parent_type)
265 InterfaceClass *new_iface;
267 TypeImpl *iface_impl;
269 info.parent = parent_type->name;
270 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
271 info.abstract = true;
273 iface_impl = type_new(&info);
274 iface_impl->parent_type = parent_type;
275 type_initialize(iface_impl);
276 g_free((char *)info.name);
278 new_iface = (InterfaceClass *)iface_impl->class;
279 new_iface->concrete_class = ti->class;
280 new_iface->interface_type = interface_type;
282 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
285 static void object_property_free(gpointer data)
287 ObjectProperty *prop = data;
290 qobject_unref(prop->defval);
295 g_free(prop->description);
299 static void type_initialize(TypeImpl *ti)
307 ti->class_size = type_class_get_size(ti);
308 ti->instance_size = type_object_get_size(ti);
309 ti->instance_align = type_object_get_align(ti);
310 /* Any type with zero instance_size is implicitly abstract.
311 * This means interface types are all abstract.
313 if (ti->instance_size == 0) {
316 if (type_is_ancestor(ti, type_interface)) {
317 assert(ti->instance_size == 0);
318 assert(ti->abstract);
319 assert(!ti->instance_init);
320 assert(!ti->instance_post_init);
321 assert(!ti->instance_finalize);
322 assert(!ti->num_interfaces);
324 ti->class = g_malloc0(ti->class_size);
326 parent = type_get_parent(ti);
328 type_initialize(parent);
332 g_assert(parent->class_size <= ti->class_size);
333 g_assert(parent->instance_size <= ti->instance_size);
334 memcpy(ti->class, parent->class, parent->class_size);
335 ti->class->interfaces = NULL;
337 for (e = parent->class->interfaces; e; e = e->next) {
338 InterfaceClass *iface = e->data;
339 ObjectClass *klass = OBJECT_CLASS(iface);
341 type_initialize_interface(ti, iface->interface_type, klass->type);
344 for (i = 0; i < ti->num_interfaces; i++) {
345 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
347 error_report("missing interface '%s' for object '%s'",
348 ti->interfaces[i].typename, parent->name);
351 for (e = ti->class->interfaces; e; e = e->next) {
352 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
354 if (type_is_ancestor(target_type, t)) {
363 type_initialize_interface(ti, t, t);
367 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
368 object_property_free);
370 ti->class->type = ti;
373 if (parent->class_base_init) {
374 parent->class_base_init(ti->class, ti->class_data);
376 parent = type_get_parent(parent);
379 if (ti->class_init) {
380 ti->class_init(ti->class, ti->class_data);
384 static void object_init_with_type(Object *obj, TypeImpl *ti)
386 if (type_has_parent(ti)) {
387 object_init_with_type(obj, type_get_parent(ti));
390 if (ti->instance_init) {
391 ti->instance_init(obj);
395 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
397 if (ti->instance_post_init) {
398 ti->instance_post_init(obj);
401 if (type_has_parent(ti)) {
402 object_post_init_with_type(obj, type_get_parent(ti));
406 bool object_apply_global_props(Object *obj, const GPtrArray *props,
415 for (i = 0; i < props->len; i++) {
416 GlobalProperty *p = g_ptr_array_index(props, i);
419 if (object_dynamic_cast(obj, p->driver) == NULL) {
422 if (p->optional && !object_property_find(obj, p->property)) {
426 if (!object_property_parse(obj, p->property, p->value, &err)) {
427 error_prepend(&err, "can't apply global %s.%s=%s: ",
428 p->driver, p->property, p->value);
430 * If errp != NULL, propagate error and return.
431 * If errp == NULL, report a warning, but keep going
432 * with the remaining globals.
435 error_propagate(errp, err);
438 warn_report_err(err);
447 * Global property defaults
448 * Slot 0: accelerator's global property defaults
449 * Slot 1: machine's global property defaults
450 * Slot 2: global properties from legacy command line option
451 * Each is a GPtrArray of of GlobalProperty.
452 * Applied in order, later entries override earlier ones.
454 static GPtrArray *object_compat_props[3];
457 * Retrieve @GPtrArray for global property defined with options
458 * other than "-global". These are generally used for syntactic
459 * sugar and legacy command line options.
461 void object_register_sugar_prop(const char *driver, const char *prop,
462 const char *value, bool optional)
465 if (!object_compat_props[2]) {
466 object_compat_props[2] = g_ptr_array_new();
468 g = g_new0(GlobalProperty, 1);
469 g->driver = g_strdup(driver);
470 g->property = g_strdup(prop);
471 g->value = g_strdup(value);
472 g->optional = optional;
473 g_ptr_array_add(object_compat_props[2], g);
477 * Set machine's global property defaults to @compat_props.
478 * May be called at most once.
480 void object_set_machine_compat_props(GPtrArray *compat_props)
482 assert(!object_compat_props[1]);
483 object_compat_props[1] = compat_props;
487 * Set accelerator's global property defaults to @compat_props.
488 * May be called at most once.
490 void object_set_accelerator_compat_props(GPtrArray *compat_props)
492 assert(!object_compat_props[0]);
493 object_compat_props[0] = compat_props;
496 void object_apply_compat_props(Object *obj)
500 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
501 object_apply_global_props(obj, object_compat_props[i],
502 i == 2 ? &error_fatal : &error_abort);
506 static void object_class_property_init_all(Object *obj)
508 ObjectPropertyIterator iter;
509 ObjectProperty *prop;
511 object_class_property_iter_init(&iter, object_get_class(obj));
512 while ((prop = object_property_iter_next(&iter))) {
514 prop->init(obj, prop);
519 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
521 type_initialize(type);
523 g_assert(type->instance_size >= sizeof(Object));
524 g_assert(type->abstract == false);
525 g_assert(size >= type->instance_size);
527 memset(obj, 0, type->instance_size);
528 obj->class = type->class;
530 object_class_property_init_all(obj);
531 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
532 NULL, object_property_free);
533 object_init_with_type(obj, type);
534 object_post_init_with_type(obj, type);
537 void object_initialize(void *data, size_t size, const char *typename)
539 TypeImpl *type = type_get_by_name(typename);
541 #ifdef CONFIG_MODULES
543 int rv = module_load_qom(typename, &error_fatal);
545 type = type_get_by_name(typename);
547 error_report("missing object type '%s'", typename);
553 error_report("missing object type '%s'", typename);
557 object_initialize_with_type(data, size, type);
560 bool object_initialize_child_with_props(Object *parentobj,
561 const char *propname,
562 void *childobj, size_t size,
569 va_start(vargs, errp);
570 ok = object_initialize_child_with_propsv(parentobj, propname,
571 childobj, size, type, errp,
577 bool object_initialize_child_with_propsv(Object *parentobj,
578 const char *propname,
579 void *childobj, size_t size,
581 Error **errp, va_list vargs)
587 object_initialize(childobj, size, type);
588 obj = OBJECT(childobj);
590 if (!object_set_propv(obj, errp, vargs)) {
594 object_property_add_child(parentobj, propname, obj);
596 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
598 if (!user_creatable_complete(uc, errp)) {
599 object_unparent(obj);
608 * We want @obj's reference to be 1 on success, 0 on failure.
609 * On success, it's 2: one taken by object_initialize(), and one
610 * by object_property_add_child().
611 * On failure in object_initialize() or earlier, it's 1.
612 * On failure afterwards, it's also 1: object_unparent() releases
613 * the reference taken by object_property_add_child().
619 void object_initialize_child_internal(Object *parent,
620 const char *propname,
621 void *child, size_t size,
624 object_initialize_child_with_props(parent, propname, child, size, type,
628 static inline bool object_property_is_child(ObjectProperty *prop)
630 return strstart(prop->type, "child<", NULL);
633 static void object_property_del_all(Object *obj)
635 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
636 ObjectProperty *prop;
637 ObjectPropertyIterator iter;
642 object_property_iter_init(&iter, obj);
643 while ((prop = object_property_iter_next(&iter)) != NULL) {
644 if (g_hash_table_add(done, prop)) {
646 prop->release(obj, prop->name, prop->opaque);
654 g_hash_table_unref(obj->properties);
657 static void object_property_del_child(Object *obj, Object *child)
659 ObjectProperty *prop;
663 g_hash_table_iter_init(&iter, obj->properties);
664 while (g_hash_table_iter_next(&iter, &key, &value)) {
666 if (object_property_is_child(prop) && prop->opaque == child) {
668 prop->release(obj, prop->name, prop->opaque);
669 prop->release = NULL;
674 g_hash_table_iter_init(&iter, obj->properties);
675 while (g_hash_table_iter_next(&iter, &key, &value)) {
677 if (object_property_is_child(prop) && prop->opaque == child) {
678 g_hash_table_iter_remove(&iter);
684 void object_unparent(Object *obj)
687 object_property_del_child(obj->parent, obj);
691 static void object_deinit(Object *obj, TypeImpl *type)
693 if (type->instance_finalize) {
694 type->instance_finalize(obj);
697 if (type_has_parent(type)) {
698 object_deinit(obj, type_get_parent(type));
702 static void object_finalize(void *data)
705 TypeImpl *ti = obj->class->type;
707 object_property_del_all(obj);
708 object_deinit(obj, ti);
710 g_assert(obj->ref == 0);
711 g_assert(obj->parent == NULL);
717 /* Find the minimum alignment guaranteed by the system malloc. */
718 #if __STDC_VERSION__ >= 201112L
719 typedef max_align_t qemu_max_align_t;
729 static Object *object_new_with_type(Type type)
733 void (*obj_free)(void *);
735 g_assert(type != NULL);
736 type_initialize(type);
738 size = type->instance_size;
739 align = type->instance_align;
742 * Do not use qemu_memalign unless required. Depending on the
743 * implementation, extra alignment implies extra overhead.
745 if (likely(align <= __alignof__(qemu_max_align_t))) {
746 obj = g_malloc(size);
749 obj = qemu_memalign(align, size);
750 obj_free = qemu_vfree;
753 object_initialize_with_type(obj, size, type);
754 obj->free = obj_free;
759 Object *object_new_with_class(ObjectClass *klass)
761 return object_new_with_type(klass->type);
764 Object *object_new(const char *typename)
766 TypeImpl *ti = type_get_by_name(typename);
768 return object_new_with_type(ti);
772 Object *object_new_with_props(const char *typename,
781 va_start(vargs, errp);
782 obj = object_new_with_propv(typename, parent, id, errp, vargs);
789 Object *object_new_with_propv(const char *typename,
799 klass = object_class_by_name(typename);
801 error_setg(errp, "invalid object type: %s", typename);
805 if (object_class_is_abstract(klass)) {
806 error_setg(errp, "object type '%s' is abstract", typename);
809 obj = object_new_with_type(klass->type);
811 if (!object_set_propv(obj, errp, vargs)) {
816 object_property_add_child(parent, id, obj);
819 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
821 if (!user_creatable_complete(uc, errp)) {
823 object_unparent(obj);
838 bool object_set_props(Object *obj,
845 va_start(vargs, errp);
846 ret = object_set_propv(obj, errp, vargs);
853 bool object_set_propv(Object *obj,
857 const char *propname;
859 propname = va_arg(vargs, char *);
860 while (propname != NULL) {
861 const char *value = va_arg(vargs, char *);
863 g_assert(value != NULL);
864 if (!object_property_parse(obj, propname, value, errp)) {
867 propname = va_arg(vargs, char *);
874 Object *object_dynamic_cast(Object *obj, const char *typename)
876 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
883 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
884 const char *file, int line, const char *func)
886 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
887 typename, file, line, func);
889 #ifdef CONFIG_QOM_CAST_DEBUG
893 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
894 if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) {
899 inst = object_dynamic_cast(obj, typename);
902 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
903 file, line, func, obj, typename);
909 if (obj && obj == inst) {
910 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
911 qatomic_set(&obj->class->object_cast_cache[i - 1],
912 qatomic_read(&obj->class->object_cast_cache[i]));
914 qatomic_set(&obj->class->object_cast_cache[i - 1], typename);
922 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
923 const char *typename)
925 ObjectClass *ret = NULL;
926 TypeImpl *target_type;
933 /* A simple fast path that can trigger a lot for leaf classes. */
935 if (type->name == typename) {
939 target_type = type_get_by_name(typename);
941 /* target class type unknown, so fail the cast */
945 if (type->class->interfaces &&
946 type_is_ancestor(target_type, type_interface)) {
950 for (i = class->interfaces; i; i = i->next) {
951 ObjectClass *target_class = i->data;
953 if (type_is_ancestor(target_class->type, target_type)) {
959 /* The match was ambiguous, don't allow a cast */
963 } else if (type_is_ancestor(type, target_type)) {
970 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
971 const char *typename,
972 const char *file, int line,
977 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
978 typename, file, line, func);
980 #ifdef CONFIG_QOM_CAST_DEBUG
983 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
984 if (qatomic_read(&class->class_cast_cache[i]) == typename) {
990 if (!class || !class->interfaces) {
995 ret = object_class_dynamic_cast(class, typename);
997 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
998 file, line, func, class, typename);
1002 #ifdef CONFIG_QOM_CAST_DEBUG
1003 if (class && ret == class) {
1004 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
1005 qatomic_set(&class->class_cast_cache[i - 1],
1006 qatomic_read(&class->class_cast_cache[i]));
1008 qatomic_set(&class->class_cast_cache[i - 1], typename);
1015 const char *object_get_typename(const Object *obj)
1017 return obj->class->type->name;
1020 ObjectClass *object_get_class(Object *obj)
1025 bool object_class_is_abstract(ObjectClass *klass)
1027 return klass->type->abstract;
1030 const char *object_class_get_name(ObjectClass *klass)
1032 return klass->type->name;
1035 ObjectClass *object_class_by_name(const char *typename)
1037 TypeImpl *type = type_get_by_name(typename);
1043 type_initialize(type);
1048 ObjectClass *module_object_class_by_name(const char *typename)
1052 oc = object_class_by_name(typename);
1053 #ifdef CONFIG_MODULES
1055 Error *local_err = NULL;
1056 int rv = module_load_qom(typename, &local_err);
1058 oc = object_class_by_name(typename);
1059 } else if (rv < 0) {
1060 error_report_err(local_err);
1067 ObjectClass *object_class_get_parent(ObjectClass *class)
1069 TypeImpl *type = type_get_parent(class->type);
1075 type_initialize(type);
1080 typedef struct OCFData
1082 void (*fn)(ObjectClass *klass, void *opaque);
1083 const char *implements_type;
1084 bool include_abstract;
1088 static void object_class_foreach_tramp(gpointer key, gpointer value,
1091 OCFData *data = opaque;
1092 TypeImpl *type = value;
1095 type_initialize(type);
1098 if (!data->include_abstract && type->abstract) {
1102 if (data->implements_type &&
1103 !object_class_dynamic_cast(k, data->implements_type)) {
1107 data->fn(k, data->opaque);
1110 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1111 const char *implements_type, bool include_abstract,
1114 OCFData data = { fn, implements_type, include_abstract, opaque };
1116 enumerating_types = true;
1117 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1118 enumerating_types = false;
1121 static int do_object_child_foreach(Object *obj,
1122 int (*fn)(Object *child, void *opaque),
1123 void *opaque, bool recurse)
1125 GHashTableIter iter;
1126 ObjectProperty *prop;
1129 g_hash_table_iter_init(&iter, obj->properties);
1130 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1131 if (object_property_is_child(prop)) {
1132 Object *child = prop->opaque;
1134 ret = fn(child, opaque);
1139 ret = do_object_child_foreach(child, fn, opaque, true);
1149 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1152 return do_object_child_foreach(obj, fn, opaque, false);
1155 int object_child_foreach_recursive(Object *obj,
1156 int (*fn)(Object *child, void *opaque),
1159 return do_object_child_foreach(obj, fn, opaque, true);
1162 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1164 GSList **list = opaque;
1166 *list = g_slist_prepend(*list, klass);
1169 GSList *object_class_get_list(const char *implements_type,
1170 bool include_abstract)
1172 GSList *list = NULL;
1174 object_class_foreach(object_class_get_list_tramp,
1175 implements_type, include_abstract, &list);
1179 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1181 return strcasecmp(object_class_get_name((ObjectClass *)a),
1182 object_class_get_name((ObjectClass *)b));
1185 GSList *object_class_get_list_sorted(const char *implements_type,
1186 bool include_abstract)
1188 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1192 Object *object_ref(void *objptr)
1194 Object *obj = OBJECT(objptr);
1200 ref = qatomic_fetch_inc(&obj->ref);
1201 /* Assert waaay before the integer overflows */
1202 g_assert(ref < INT_MAX);
1206 void object_unref(void *objptr)
1208 Object *obj = OBJECT(objptr);
1212 g_assert(obj->ref > 0);
1214 /* parent always holds a reference to its children */
1215 if (qatomic_fetch_dec(&obj->ref) == 1) {
1216 object_finalize(obj);
1221 object_property_try_add(Object *obj, const char *name, const char *type,
1222 ObjectPropertyAccessor *get,
1223 ObjectPropertyAccessor *set,
1224 ObjectPropertyRelease *release,
1225 void *opaque, Error **errp)
1227 ObjectProperty *prop;
1228 size_t name_len = strlen(name);
1230 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1232 ObjectProperty *ret = NULL;
1233 char *name_no_array = g_strdup(name);
1235 name_no_array[name_len - 3] = '\0';
1236 for (i = 0; i < INT16_MAX; ++i) {
1237 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1239 ret = object_property_try_add(obj, full_name, type, get, set,
1240 release, opaque, NULL);
1246 g_free(name_no_array);
1251 if (object_property_find(obj, name) != NULL) {
1252 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1253 name, object_get_typename(obj));
1257 prop = g_malloc0(sizeof(*prop));
1259 prop->name = g_strdup(name);
1260 prop->type = g_strdup(type);
1264 prop->release = release;
1265 prop->opaque = opaque;
1267 g_hash_table_insert(obj->properties, prop->name, prop);
1272 object_property_add(Object *obj, const char *name, const char *type,
1273 ObjectPropertyAccessor *get,
1274 ObjectPropertyAccessor *set,
1275 ObjectPropertyRelease *release,
1278 return object_property_try_add(obj, name, type, get, set, release,
1279 opaque, &error_abort);
1283 object_class_property_add(ObjectClass *klass,
1286 ObjectPropertyAccessor *get,
1287 ObjectPropertyAccessor *set,
1288 ObjectPropertyRelease *release,
1291 ObjectProperty *prop;
1293 assert(!object_class_property_find(klass, name));
1295 prop = g_malloc0(sizeof(*prop));
1297 prop->name = g_strdup(name);
1298 prop->type = g_strdup(type);
1302 prop->release = release;
1303 prop->opaque = opaque;
1305 g_hash_table_insert(klass->properties, prop->name, prop);
1310 ObjectProperty *object_property_find(Object *obj, const char *name)
1312 ObjectProperty *prop;
1313 ObjectClass *klass = object_get_class(obj);
1315 prop = object_class_property_find(klass, name);
1320 return g_hash_table_lookup(obj->properties, name);
1323 ObjectProperty *object_property_find_err(Object *obj, const char *name,
1326 ObjectProperty *prop = object_property_find(obj, name);
1328 error_setg(errp, "Property '%s.%s' not found",
1329 object_get_typename(obj), name);
1334 void object_property_iter_init(ObjectPropertyIterator *iter,
1337 g_hash_table_iter_init(&iter->iter, obj->properties);
1338 iter->nextclass = object_get_class(obj);
1341 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1344 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1345 if (!iter->nextclass) {
1348 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1349 iter->nextclass = object_class_get_parent(iter->nextclass);
1354 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1357 g_hash_table_iter_init(&iter->iter, klass->properties);
1358 iter->nextclass = object_class_get_parent(klass);
1361 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name)
1363 ObjectClass *parent_klass;
1365 parent_klass = object_class_get_parent(klass);
1367 ObjectProperty *prop =
1368 object_class_property_find(parent_klass, name);
1374 return g_hash_table_lookup(klass->properties, name);
1377 ObjectProperty *object_class_property_find_err(ObjectClass *klass,
1381 ObjectProperty *prop = object_class_property_find(klass, name);
1383 error_setg(errp, "Property '.%s' not found", name);
1389 void object_property_del(Object *obj, const char *name)
1391 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1393 if (prop->release) {
1394 prop->release(obj, name, prop->opaque);
1396 g_hash_table_remove(obj->properties, name);
1399 bool object_property_get(Object *obj, const char *name, Visitor *v,
1403 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1410 error_setg(errp, "Property '%s.%s' is not readable",
1411 object_get_typename(obj), name);
1414 prop->get(obj, v, name, prop->opaque, &err);
1415 error_propagate(errp, err);
1419 bool object_property_set(Object *obj, const char *name, Visitor *v,
1423 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1430 error_setg(errp, "Property '%s.%s' is not writable",
1431 object_get_typename(obj), name);
1434 prop->set(obj, v, name, prop->opaque, errp);
1438 bool object_property_set_str(Object *obj, const char *name,
1439 const char *value, Error **errp)
1441 QString *qstr = qstring_from_str(value);
1442 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
1444 qobject_unref(qstr);
1448 char *object_property_get_str(Object *obj, const char *name,
1451 QObject *ret = object_property_get_qobject(obj, name, errp);
1458 qstring = qobject_to(QString, ret);
1460 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1463 retval = g_strdup(qstring_get_str(qstring));
1470 bool object_property_set_link(Object *obj, const char *name,
1471 Object *value, Error **errp)
1473 g_autofree char *path = NULL;
1476 path = object_get_canonical_path(value);
1478 return object_property_set_str(obj, name, path ?: "", errp);
1481 Object *object_property_get_link(Object *obj, const char *name,
1484 char *str = object_property_get_str(obj, name, errp);
1485 Object *target = NULL;
1488 target = object_resolve_path(str, NULL);
1490 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1491 "Device '%s' not found", str);
1499 bool object_property_set_bool(Object *obj, const char *name,
1500 bool value, Error **errp)
1502 QBool *qbool = qbool_from_bool(value);
1503 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
1505 qobject_unref(qbool);
1509 bool object_property_get_bool(Object *obj, const char *name,
1512 QObject *ret = object_property_get_qobject(obj, name, errp);
1519 qbool = qobject_to(QBool, ret);
1521 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1524 retval = qbool_get_bool(qbool);
1531 bool object_property_set_int(Object *obj, const char *name,
1532 int64_t value, Error **errp)
1534 QNum *qnum = qnum_from_int(value);
1535 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1537 qobject_unref(qnum);
1541 int64_t object_property_get_int(Object *obj, const char *name,
1544 QObject *ret = object_property_get_qobject(obj, name, errp);
1552 qnum = qobject_to(QNum, ret);
1553 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1554 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1562 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1564 Visitor *v = qobject_input_visitor_new(prop->defval);
1566 assert(prop->set != NULL);
1567 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1572 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1574 assert(!prop->defval);
1575 assert(!prop->init);
1577 prop->defval = defval;
1578 prop->init = object_property_init_defval;
1581 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1583 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1586 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1588 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1591 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1593 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1596 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1598 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1601 bool object_property_set_uint(Object *obj, const char *name,
1602 uint64_t value, Error **errp)
1604 QNum *qnum = qnum_from_uint(value);
1605 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1607 qobject_unref(qnum);
1611 uint64_t object_property_get_uint(Object *obj, const char *name,
1614 QObject *ret = object_property_get_qobject(obj, name, errp);
1621 qnum = qobject_to(QNum, ret);
1622 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1623 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1631 typedef struct EnumProperty {
1632 const QEnumLookup *lookup;
1633 int (*get)(Object *, Error **);
1634 void (*set)(Object *, int, Error **);
1637 int object_property_get_enum(Object *obj, const char *name,
1638 const char *typename, Error **errp)
1642 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1643 EnumProperty *enumprop;
1649 if (!g_str_equal(prop->type, typename)) {
1650 error_setg(errp, "Property %s on %s is not '%s' enum type",
1651 name, object_class_get_name(
1652 object_get_class(obj)), typename);
1656 enumprop = prop->opaque;
1658 str = object_property_get_str(obj, name, errp);
1663 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1669 bool object_property_parse(Object *obj, const char *name,
1670 const char *string, Error **errp)
1672 Visitor *v = string_input_visitor_new(string);
1673 bool ok = object_property_set(obj, name, v, errp);
1679 char *object_property_print(Object *obj, const char *name, bool human,
1683 char *string = NULL;
1685 v = string_output_visitor_new(human, &string);
1686 if (!object_property_get(obj, name, v, errp)) {
1690 visit_complete(v, &string);
1697 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1699 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1707 Object *object_get_root(void)
1709 static Object *root;
1712 root = object_new("container");
1718 Object *object_get_objects_root(void)
1720 return container_get(object_get_root(), "/objects");
1723 Object *object_get_internal_root(void)
1725 static Object *internal_root;
1727 if (!internal_root) {
1728 internal_root = object_new("container");
1731 return internal_root;
1734 static void object_get_child_property(Object *obj, Visitor *v,
1735 const char *name, void *opaque,
1738 Object *child = opaque;
1741 path = object_get_canonical_path(child);
1742 visit_type_str(v, name, &path, errp);
1746 static Object *object_resolve_child_property(Object *parent, void *opaque,
1752 static void object_finalize_child_property(Object *obj, const char *name,
1755 Object *child = opaque;
1757 if (child->class->unparent) {
1758 (child->class->unparent)(child);
1760 child->parent = NULL;
1761 object_unref(child);
1765 object_property_try_add_child(Object *obj, const char *name,
1766 Object *child, Error **errp)
1768 g_autofree char *type = NULL;
1771 assert(!child->parent);
1773 type = g_strdup_printf("child<%s>", object_get_typename(child));
1775 op = object_property_try_add(obj, name, type, object_get_child_property,
1776 NULL, object_finalize_child_property,
1781 op->resolve = object_resolve_child_property;
1783 child->parent = obj;
1788 object_property_add_child(Object *obj, const char *name,
1791 return object_property_try_add_child(obj, name, child, &error_abort);
1794 void object_property_allow_set_link(const Object *obj, const char *name,
1795 Object *val, Error **errp)
1797 /* Allow the link to be set, always */
1803 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1804 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1806 void (*check)(const Object *, const char *, Object *, Error **);
1807 ObjectPropertyLinkFlags flags;
1811 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1813 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1814 return &lprop->target;
1815 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1816 return (void *)obj + lprop->offset;
1818 return lprop->targetp;
1822 static void object_get_link_property(Object *obj, Visitor *v,
1823 const char *name, void *opaque,
1826 LinkProperty *lprop = opaque;
1827 Object **targetp = object_link_get_targetp(obj, lprop);
1831 path = object_get_canonical_path(*targetp);
1832 visit_type_str(v, name, &path, errp);
1836 visit_type_str(v, name, &path, errp);
1841 * object_resolve_link:
1843 * Lookup an object and ensure its type matches the link property type. This
1844 * is similar to object_resolve_path() except type verification against the
1845 * link property is performed.
1847 * Returns: The matched object or NULL on path lookup failures.
1849 static Object *object_resolve_link(Object *obj, const char *name,
1850 const char *path, Error **errp)
1854 bool ambiguous = false;
1857 /* Go from link<FOO> to FOO. */
1858 type = object_property_get_type(obj, name, NULL);
1859 target_type = g_strndup(&type[5], strlen(type) - 6);
1860 target = object_resolve_path_type(path, target_type, &ambiguous);
1863 error_setg(errp, "Path '%s' does not uniquely identify an object",
1865 } else if (!target) {
1866 target = object_resolve_path(path, &ambiguous);
1867 if (target || ambiguous) {
1868 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1870 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1871 "Device '%s' not found", path);
1875 g_free(target_type);
1880 static void object_set_link_property(Object *obj, Visitor *v,
1881 const char *name, void *opaque,
1884 Error *local_err = NULL;
1885 LinkProperty *prop = opaque;
1886 Object **targetp = object_link_get_targetp(obj, prop);
1887 Object *old_target = *targetp;
1891 if (!visit_type_str(v, name, &path, errp)) {
1896 new_target = object_resolve_link(obj, name, path, errp);
1907 prop->check(obj, name, new_target, &local_err);
1909 error_propagate(errp, local_err);
1913 *targetp = new_target;
1914 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1915 object_ref(new_target);
1916 object_unref(old_target);
1920 static Object *object_resolve_link_property(Object *parent, void *opaque,
1923 LinkProperty *lprop = opaque;
1925 return *object_link_get_targetp(parent, lprop);
1928 static void object_release_link_property(Object *obj, const char *name,
1931 LinkProperty *prop = opaque;
1932 Object **targetp = object_link_get_targetp(obj, prop);
1934 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1935 object_unref(*targetp);
1937 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1942 static ObjectProperty *
1943 object_add_link_prop(Object *obj, const char *name,
1944 const char *type, void *ptr,
1945 void (*check)(const Object *, const char *,
1946 Object *, Error **),
1947 ObjectPropertyLinkFlags flags)
1949 LinkProperty *prop = g_malloc(sizeof(*prop));
1950 g_autofree char *full_type = NULL;
1953 if (flags & OBJ_PROP_LINK_DIRECT) {
1956 prop->targetp = ptr;
1958 prop->check = check;
1959 prop->flags = flags;
1961 full_type = g_strdup_printf("link<%s>", type);
1963 op = object_property_add(obj, name, full_type,
1964 object_get_link_property,
1965 check ? object_set_link_property : NULL,
1966 object_release_link_property,
1968 op->resolve = object_resolve_link_property;
1973 object_property_add_link(Object *obj, const char *name,
1974 const char *type, Object **targetp,
1975 void (*check)(const Object *, const char *,
1976 Object *, Error **),
1977 ObjectPropertyLinkFlags flags)
1979 return object_add_link_prop(obj, name, type, targetp, check, flags);
1983 object_class_property_add_link(ObjectClass *oc,
1985 const char *type, ptrdiff_t offset,
1986 void (*check)(const Object *obj, const char *name,
1987 Object *val, Error **errp),
1988 ObjectPropertyLinkFlags flags)
1990 LinkProperty *prop = g_new0(LinkProperty, 1);
1994 prop->offset = offset;
1995 prop->check = check;
1996 prop->flags = flags | OBJ_PROP_LINK_CLASS;
1998 full_type = g_strdup_printf("link<%s>", type);
2000 op = object_class_property_add(oc, name, full_type,
2001 object_get_link_property,
2002 check ? object_set_link_property : NULL,
2003 object_release_link_property,
2006 op->resolve = object_resolve_link_property;
2013 object_property_add_const_link(Object *obj, const char *name,
2016 return object_add_link_prop(obj, name,
2017 object_get_typename(target), target,
2018 NULL, OBJ_PROP_LINK_DIRECT);
2021 const char *object_get_canonical_path_component(const Object *obj)
2023 ObjectProperty *prop = NULL;
2024 GHashTableIter iter;
2026 if (obj->parent == NULL) {
2030 g_hash_table_iter_init(&iter, obj->parent->properties);
2031 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2032 if (!object_property_is_child(prop)) {
2036 if (prop->opaque == obj) {
2041 /* obj had a parent but was not a child, should never happen */
2042 g_assert_not_reached();
2046 char *object_get_canonical_path(const Object *obj)
2048 Object *root = object_get_root();
2049 char *newpath, *path = NULL;
2052 return g_strdup("/");
2056 const char *component = object_get_canonical_path_component(obj);
2059 /* A canonical path must be complete, so discard what was
2066 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
2070 } while (obj != root);
2075 Object *object_resolve_path_component(Object *parent, const char *part)
2077 ObjectProperty *prop = object_property_find(parent, part);
2082 if (prop->resolve) {
2083 return prop->resolve(parent, prop->opaque, part);
2089 static Object *object_resolve_abs_path(Object *parent,
2091 const char *typename)
2095 if (*parts == NULL) {
2096 return object_dynamic_cast(parent, typename);
2099 if (strcmp(*parts, "") == 0) {
2100 return object_resolve_abs_path(parent, parts + 1, typename);
2103 child = object_resolve_path_component(parent, *parts);
2108 return object_resolve_abs_path(child, parts + 1, typename);
2111 static Object *object_resolve_partial_path(Object *parent,
2113 const char *typename,
2117 GHashTableIter iter;
2118 ObjectProperty *prop;
2120 obj = object_resolve_abs_path(parent, parts, typename);
2122 g_hash_table_iter_init(&iter, parent->properties);
2123 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2126 if (!object_property_is_child(prop)) {
2130 found = object_resolve_partial_path(prop->opaque, parts,
2131 typename, ambiguous);
2148 Object *object_resolve_path_type(const char *path, const char *typename,
2154 parts = g_strsplit(path, "/", 0);
2157 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2158 bool ambiguous = false;
2159 obj = object_resolve_partial_path(object_get_root(), parts,
2160 typename, &ambiguous);
2162 *ambiguousp = ambiguous;
2165 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2173 Object *object_resolve_path(const char *path, bool *ambiguous)
2175 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2178 Object *object_resolve_path_at(Object *parent, const char *path)
2180 g_auto(GStrv) parts = g_strsplit(path, "/", 0);
2183 return object_resolve_abs_path(object_get_root(), parts + 1,
2186 return object_resolve_abs_path(parent, parts, TYPE_OBJECT);
2189 typedef struct StringProperty
2191 char *(*get)(Object *, Error **);
2192 void (*set)(Object *, const char *, Error **);
2195 static void property_get_str(Object *obj, Visitor *v, const char *name,
2196 void *opaque, Error **errp)
2198 StringProperty *prop = opaque;
2202 value = prop->get(obj, &err);
2204 error_propagate(errp, err);
2208 visit_type_str(v, name, &value, errp);
2212 static void property_set_str(Object *obj, Visitor *v, const char *name,
2213 void *opaque, Error **errp)
2215 StringProperty *prop = opaque;
2218 if (!visit_type_str(v, name, &value, errp)) {
2222 prop->set(obj, value, errp);
2226 static void property_release_data(Object *obj, const char *name,
2233 object_property_add_str(Object *obj, const char *name,
2234 char *(*get)(Object *, Error **),
2235 void (*set)(Object *, const char *, Error **))
2237 StringProperty *prop = g_malloc0(sizeof(*prop));
2242 return object_property_add(obj, name, "string",
2243 get ? property_get_str : NULL,
2244 set ? property_set_str : NULL,
2245 property_release_data,
2250 object_class_property_add_str(ObjectClass *klass, const char *name,
2251 char *(*get)(Object *, Error **),
2252 void (*set)(Object *, const char *,
2255 StringProperty *prop = g_malloc0(sizeof(*prop));
2260 return object_class_property_add(klass, name, "string",
2261 get ? property_get_str : NULL,
2262 set ? property_set_str : NULL,
2267 typedef struct BoolProperty
2269 bool (*get)(Object *, Error **);
2270 void (*set)(Object *, bool, Error **);
2273 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2274 void *opaque, Error **errp)
2276 BoolProperty *prop = opaque;
2280 value = prop->get(obj, &err);
2282 error_propagate(errp, err);
2286 visit_type_bool(v, name, &value, errp);
2289 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2290 void *opaque, Error **errp)
2292 BoolProperty *prop = opaque;
2295 if (!visit_type_bool(v, name, &value, errp)) {
2299 prop->set(obj, value, errp);
2303 object_property_add_bool(Object *obj, const char *name,
2304 bool (*get)(Object *, Error **),
2305 void (*set)(Object *, bool, Error **))
2307 BoolProperty *prop = g_malloc0(sizeof(*prop));
2312 return object_property_add(obj, name, "bool",
2313 get ? property_get_bool : NULL,
2314 set ? property_set_bool : NULL,
2315 property_release_data,
2320 object_class_property_add_bool(ObjectClass *klass, const char *name,
2321 bool (*get)(Object *, Error **),
2322 void (*set)(Object *, bool, Error **))
2324 BoolProperty *prop = g_malloc0(sizeof(*prop));
2329 return object_class_property_add(klass, name, "bool",
2330 get ? property_get_bool : NULL,
2331 set ? property_set_bool : NULL,
2336 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2337 void *opaque, Error **errp)
2339 EnumProperty *prop = opaque;
2343 value = prop->get(obj, &err);
2345 error_propagate(errp, err);
2349 visit_type_enum(v, name, &value, prop->lookup, errp);
2352 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2353 void *opaque, Error **errp)
2355 EnumProperty *prop = opaque;
2358 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
2361 prop->set(obj, value, errp);
2365 object_property_add_enum(Object *obj, const char *name,
2366 const char *typename,
2367 const QEnumLookup *lookup,
2368 int (*get)(Object *, Error **),
2369 void (*set)(Object *, int, Error **))
2371 EnumProperty *prop = g_malloc(sizeof(*prop));
2373 prop->lookup = lookup;
2377 return object_property_add(obj, name, typename,
2378 get ? property_get_enum : NULL,
2379 set ? property_set_enum : NULL,
2380 property_release_data,
2385 object_class_property_add_enum(ObjectClass *klass, const char *name,
2386 const char *typename,
2387 const QEnumLookup *lookup,
2388 int (*get)(Object *, Error **),
2389 void (*set)(Object *, int, Error **))
2391 EnumProperty *prop = g_malloc(sizeof(*prop));
2393 prop->lookup = lookup;
2397 return object_class_property_add(klass, name, typename,
2398 get ? property_get_enum : NULL,
2399 set ? property_set_enum : NULL,
2404 typedef struct TMProperty {
2405 void (*get)(Object *, struct tm *, Error **);
2408 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2409 void *opaque, Error **errp)
2411 TMProperty *prop = opaque;
2415 prop->get(obj, &value, &err);
2417 error_propagate(errp, err);
2421 if (!visit_start_struct(v, name, NULL, 0, errp)) {
2424 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
2427 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
2430 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
2433 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
2436 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
2439 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
2442 visit_check_struct(v, errp);
2444 visit_end_struct(v, NULL);
2448 object_property_add_tm(Object *obj, const char *name,
2449 void (*get)(Object *, struct tm *, Error **))
2451 TMProperty *prop = g_malloc0(sizeof(*prop));
2455 return object_property_add(obj, name, "struct tm",
2456 get ? property_get_tm : NULL, NULL,
2457 property_release_data,
2462 object_class_property_add_tm(ObjectClass *klass, const char *name,
2463 void (*get)(Object *, struct tm *, Error **))
2465 TMProperty *prop = g_malloc0(sizeof(*prop));
2469 return object_class_property_add(klass, name, "struct tm",
2470 get ? property_get_tm : NULL,
2474 static char *object_get_type(Object *obj, Error **errp)
2476 return g_strdup(object_get_typename(obj));
2479 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2480 void *opaque, Error **errp)
2482 uint8_t value = *(uint8_t *)opaque;
2483 visit_type_uint8(v, name, &value, errp);
2486 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2487 void *opaque, Error **errp)
2489 uint8_t *field = opaque;
2492 if (!visit_type_uint8(v, name, &value, errp)) {
2499 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2500 void *opaque, Error **errp)
2502 uint16_t value = *(uint16_t *)opaque;
2503 visit_type_uint16(v, name, &value, errp);
2506 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2507 void *opaque, Error **errp)
2509 uint16_t *field = opaque;
2512 if (!visit_type_uint16(v, name, &value, errp)) {
2519 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2520 void *opaque, Error **errp)
2522 uint32_t value = *(uint32_t *)opaque;
2523 visit_type_uint32(v, name, &value, errp);
2526 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2527 void *opaque, Error **errp)
2529 uint32_t *field = opaque;
2532 if (!visit_type_uint32(v, name, &value, errp)) {
2539 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2540 void *opaque, Error **errp)
2542 uint64_t value = *(uint64_t *)opaque;
2543 visit_type_uint64(v, name, &value, errp);
2546 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2547 void *opaque, Error **errp)
2549 uint64_t *field = opaque;
2552 if (!visit_type_uint64(v, name, &value, errp)) {
2560 object_property_add_uint8_ptr(Object *obj, const char *name,
2562 ObjectPropertyFlags flags)
2564 ObjectPropertyAccessor *getter = NULL;
2565 ObjectPropertyAccessor *setter = NULL;
2567 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2568 getter = property_get_uint8_ptr;
2571 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2572 setter = property_set_uint8_ptr;
2575 return object_property_add(obj, name, "uint8",
2576 getter, setter, NULL, (void *)v);
2580 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2582 ObjectPropertyFlags flags)
2584 ObjectPropertyAccessor *getter = NULL;
2585 ObjectPropertyAccessor *setter = NULL;
2587 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2588 getter = property_get_uint8_ptr;
2591 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2592 setter = property_set_uint8_ptr;
2595 return object_class_property_add(klass, name, "uint8",
2596 getter, setter, NULL, (void *)v);
2600 object_property_add_uint16_ptr(Object *obj, const char *name,
2602 ObjectPropertyFlags flags)
2604 ObjectPropertyAccessor *getter = NULL;
2605 ObjectPropertyAccessor *setter = NULL;
2607 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2608 getter = property_get_uint16_ptr;
2611 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2612 setter = property_set_uint16_ptr;
2615 return object_property_add(obj, name, "uint16",
2616 getter, setter, NULL, (void *)v);
2620 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2622 ObjectPropertyFlags flags)
2624 ObjectPropertyAccessor *getter = NULL;
2625 ObjectPropertyAccessor *setter = NULL;
2627 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2628 getter = property_get_uint16_ptr;
2631 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2632 setter = property_set_uint16_ptr;
2635 return object_class_property_add(klass, name, "uint16",
2636 getter, setter, NULL, (void *)v);
2640 object_property_add_uint32_ptr(Object *obj, const char *name,
2642 ObjectPropertyFlags flags)
2644 ObjectPropertyAccessor *getter = NULL;
2645 ObjectPropertyAccessor *setter = NULL;
2647 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2648 getter = property_get_uint32_ptr;
2651 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2652 setter = property_set_uint32_ptr;
2655 return object_property_add(obj, name, "uint32",
2656 getter, setter, NULL, (void *)v);
2660 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2662 ObjectPropertyFlags flags)
2664 ObjectPropertyAccessor *getter = NULL;
2665 ObjectPropertyAccessor *setter = NULL;
2667 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2668 getter = property_get_uint32_ptr;
2671 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2672 setter = property_set_uint32_ptr;
2675 return object_class_property_add(klass, name, "uint32",
2676 getter, setter, NULL, (void *)v);
2680 object_property_add_uint64_ptr(Object *obj, const char *name,
2682 ObjectPropertyFlags flags)
2684 ObjectPropertyAccessor *getter = NULL;
2685 ObjectPropertyAccessor *setter = NULL;
2687 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2688 getter = property_get_uint64_ptr;
2691 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2692 setter = property_set_uint64_ptr;
2695 return object_property_add(obj, name, "uint64",
2696 getter, setter, NULL, (void *)v);
2700 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2702 ObjectPropertyFlags flags)
2704 ObjectPropertyAccessor *getter = NULL;
2705 ObjectPropertyAccessor *setter = NULL;
2707 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2708 getter = property_get_uint64_ptr;
2711 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2712 setter = property_set_uint64_ptr;
2715 return object_class_property_add(klass, name, "uint64",
2716 getter, setter, NULL, (void *)v);
2724 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2725 void *opaque, Error **errp)
2727 AliasProperty *prop = opaque;
2728 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2730 object_property_get(prop->target_obj, prop->target_name, alias_v, errp);
2731 visit_free(alias_v);
2734 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2735 void *opaque, Error **errp)
2737 AliasProperty *prop = opaque;
2738 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2740 object_property_set(prop->target_obj, prop->target_name, alias_v, errp);
2741 visit_free(alias_v);
2744 static Object *property_resolve_alias(Object *obj, void *opaque,
2747 AliasProperty *prop = opaque;
2749 return object_resolve_path_component(prop->target_obj, prop->target_name);
2752 static void property_release_alias(Object *obj, const char *name, void *opaque)
2754 AliasProperty *prop = opaque;
2756 g_free(prop->target_name);
2761 object_property_add_alias(Object *obj, const char *name,
2762 Object *target_obj, const char *target_name)
2764 AliasProperty *prop;
2766 ObjectProperty *target_prop;
2767 g_autofree char *prop_type = NULL;
2769 target_prop = object_property_find_err(target_obj, target_name,
2772 if (object_property_is_child(target_prop)) {
2773 prop_type = g_strdup_printf("link%s",
2774 target_prop->type + strlen("child"));
2776 prop_type = g_strdup(target_prop->type);
2779 prop = g_malloc(sizeof(*prop));
2780 prop->target_obj = target_obj;
2781 prop->target_name = g_strdup(target_name);
2783 op = object_property_add(obj, name, prop_type,
2786 property_release_alias,
2788 op->resolve = property_resolve_alias;
2789 if (target_prop->defval) {
2790 op->defval = qobject_ref(target_prop->defval);
2793 object_property_set_description(obj, op->name,
2794 target_prop->description);
2798 void object_property_set_description(Object *obj, const char *name,
2799 const char *description)
2803 op = object_property_find_err(obj, name, &error_abort);
2804 g_free(op->description);
2805 op->description = g_strdup(description);
2808 void object_class_property_set_description(ObjectClass *klass,
2810 const char *description)
2814 op = g_hash_table_lookup(klass->properties, name);
2815 g_free(op->description);
2816 op->description = g_strdup(description);
2819 static void object_class_init(ObjectClass *klass, void *data)
2821 object_class_property_add_str(klass, "type", object_get_type,
2825 static void register_types(void)
2827 static const TypeInfo interface_info = {
2828 .name = TYPE_INTERFACE,
2829 .class_size = sizeof(InterfaceClass),
2833 static const TypeInfo object_info = {
2834 .name = TYPE_OBJECT,
2835 .instance_size = sizeof(Object),
2836 .class_init = object_class_init,
2840 type_interface = type_register_internal(&interface_info);
2841 type_register_internal(&object_info);
2844 type_init(register_types)