util/drm: make portable by avoiding struct dirent d_type
[qemu/ar7.git] / qom / object.c
blob76f5f7523958795f92e9284406ec4ccbc1388719
1 /*
2 * QEMU Object Model
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/qapi-builtin-visit.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qmp/qjson.h"
26 #include "trace.h"
28 /* TODO: replace QObject with a simpler visitor to avoid a dependency
29 * of the QOM core on QObject? */
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qemu/error-report.h"
36 #define MAX_INTERFACES 32
38 typedef struct InterfaceImpl InterfaceImpl;
39 typedef struct TypeImpl TypeImpl;
41 struct InterfaceImpl
43 const char *typename;
46 struct TypeImpl
48 const char *name;
50 size_t class_size;
52 size_t instance_size;
54 void (*class_init)(ObjectClass *klass, void *data);
55 void (*class_base_init)(ObjectClass *klass, void *data);
57 void *class_data;
59 void (*instance_init)(Object *obj);
60 void (*instance_post_init)(Object *obj);
61 void (*instance_finalize)(Object *obj);
63 bool abstract;
65 const char *parent;
66 TypeImpl *parent_type;
68 ObjectClass *class;
70 int num_interfaces;
71 InterfaceImpl interfaces[MAX_INTERFACES];
74 static Type type_interface;
76 static GHashTable *type_table_get(void)
78 static GHashTable *type_table;
80 if (type_table == NULL) {
81 type_table = g_hash_table_new(g_str_hash, g_str_equal);
84 return type_table;
87 static bool enumerating_types;
89 static void type_table_add(TypeImpl *ti)
91 assert(!enumerating_types);
92 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
95 static TypeImpl *type_table_lookup(const char *name)
97 return g_hash_table_lookup(type_table_get(), name);
100 static TypeImpl *type_new(const TypeInfo *info)
102 TypeImpl *ti = g_malloc0(sizeof(*ti));
103 int i;
105 g_assert(info->name != NULL);
107 if (type_table_lookup(info->name) != NULL) {
108 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
109 abort();
112 ti->name = g_strdup(info->name);
113 ti->parent = g_strdup(info->parent);
115 ti->class_size = info->class_size;
116 ti->instance_size = info->instance_size;
118 ti->class_init = info->class_init;
119 ti->class_base_init = info->class_base_init;
120 ti->class_data = info->class_data;
122 ti->instance_init = info->instance_init;
123 ti->instance_post_init = info->instance_post_init;
124 ti->instance_finalize = info->instance_finalize;
126 ti->abstract = info->abstract;
128 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
129 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
131 ti->num_interfaces = i;
133 return ti;
136 static TypeImpl *type_register_internal(const TypeInfo *info)
138 TypeImpl *ti;
139 ti = type_new(info);
141 type_table_add(ti);
142 return ti;
145 TypeImpl *type_register(const TypeInfo *info)
147 assert(info->parent);
148 return type_register_internal(info);
151 TypeImpl *type_register_static(const TypeInfo *info)
153 return type_register(info);
156 void type_register_static_array(const TypeInfo *infos, int nr_infos)
158 int i;
160 for (i = 0; i < nr_infos; i++) {
161 type_register_static(&infos[i]);
165 static TypeImpl *type_get_by_name(const char *name)
167 if (name == NULL) {
168 return NULL;
171 return type_table_lookup(name);
174 static TypeImpl *type_get_parent(TypeImpl *type)
176 if (!type->parent_type && type->parent) {
177 type->parent_type = type_get_by_name(type->parent);
178 if (!type->parent_type) {
179 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
180 type->name, type->parent);
181 abort();
185 return type->parent_type;
188 static bool type_has_parent(TypeImpl *type)
190 return (type->parent != NULL);
193 static size_t type_class_get_size(TypeImpl *ti)
195 if (ti->class_size) {
196 return ti->class_size;
199 if (type_has_parent(ti)) {
200 return type_class_get_size(type_get_parent(ti));
203 return sizeof(ObjectClass);
206 static size_t type_object_get_size(TypeImpl *ti)
208 if (ti->instance_size) {
209 return ti->instance_size;
212 if (type_has_parent(ti)) {
213 return type_object_get_size(type_get_parent(ti));
216 return 0;
219 size_t object_type_get_instance_size(const char *typename)
221 TypeImpl *type = type_get_by_name(typename);
223 g_assert(type != NULL);
224 return type_object_get_size(type);
227 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
229 assert(target_type);
231 /* Check if target_type is a direct ancestor of type */
232 while (type) {
233 if (type == target_type) {
234 return true;
237 type = type_get_parent(type);
240 return false;
243 static void type_initialize(TypeImpl *ti);
245 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
246 TypeImpl *parent_type)
248 InterfaceClass *new_iface;
249 TypeInfo info = { };
250 TypeImpl *iface_impl;
252 info.parent = parent_type->name;
253 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
254 info.abstract = true;
256 iface_impl = type_new(&info);
257 iface_impl->parent_type = parent_type;
258 type_initialize(iface_impl);
259 g_free((char *)info.name);
261 new_iface = (InterfaceClass *)iface_impl->class;
262 new_iface->concrete_class = ti->class;
263 new_iface->interface_type = interface_type;
265 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
268 static void object_property_free(gpointer data)
270 ObjectProperty *prop = data;
272 if (prop->defval) {
273 qobject_unref(prop->defval);
274 prop->defval = NULL;
276 g_free(prop->name);
277 g_free(prop->type);
278 g_free(prop->description);
279 g_free(prop);
282 static void type_initialize(TypeImpl *ti)
284 TypeImpl *parent;
286 if (ti->class) {
287 return;
290 ti->class_size = type_class_get_size(ti);
291 ti->instance_size = type_object_get_size(ti);
292 /* Any type with zero instance_size is implicitly abstract.
293 * This means interface types are all abstract.
295 if (ti->instance_size == 0) {
296 ti->abstract = true;
298 if (type_is_ancestor(ti, type_interface)) {
299 assert(ti->instance_size == 0);
300 assert(ti->abstract);
301 assert(!ti->instance_init);
302 assert(!ti->instance_post_init);
303 assert(!ti->instance_finalize);
304 assert(!ti->num_interfaces);
306 ti->class = g_malloc0(ti->class_size);
308 parent = type_get_parent(ti);
309 if (parent) {
310 type_initialize(parent);
311 GSList *e;
312 int i;
314 g_assert(parent->class_size <= ti->class_size);
315 g_assert(parent->instance_size <= ti->instance_size);
316 memcpy(ti->class, parent->class, parent->class_size);
317 ti->class->interfaces = NULL;
319 for (e = parent->class->interfaces; e; e = e->next) {
320 InterfaceClass *iface = e->data;
321 ObjectClass *klass = OBJECT_CLASS(iface);
323 type_initialize_interface(ti, iface->interface_type, klass->type);
326 for (i = 0; i < ti->num_interfaces; i++) {
327 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
328 if (!t) {
329 error_report("missing interface '%s' for object '%s'",
330 ti->interfaces[i].typename, parent->name);
331 abort();
333 for (e = ti->class->interfaces; e; e = e->next) {
334 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
336 if (type_is_ancestor(target_type, t)) {
337 break;
341 if (e) {
342 continue;
345 type_initialize_interface(ti, t, t);
349 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
350 object_property_free);
352 ti->class->type = ti;
354 while (parent) {
355 if (parent->class_base_init) {
356 parent->class_base_init(ti->class, ti->class_data);
358 parent = type_get_parent(parent);
361 if (ti->class_init) {
362 ti->class_init(ti->class, ti->class_data);
366 static void object_init_with_type(Object *obj, TypeImpl *ti)
368 if (type_has_parent(ti)) {
369 object_init_with_type(obj, type_get_parent(ti));
372 if (ti->instance_init) {
373 ti->instance_init(obj);
377 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
379 if (ti->instance_post_init) {
380 ti->instance_post_init(obj);
383 if (type_has_parent(ti)) {
384 object_post_init_with_type(obj, type_get_parent(ti));
388 bool object_apply_global_props(Object *obj, const GPtrArray *props,
389 Error **errp)
391 int i;
393 if (!props) {
394 return true;
397 for (i = 0; i < props->len; i++) {
398 GlobalProperty *p = g_ptr_array_index(props, i);
399 Error *err = NULL;
401 if (object_dynamic_cast(obj, p->driver) == NULL) {
402 continue;
404 if (p->optional && !object_property_find(obj, p->property, NULL)) {
405 continue;
407 p->used = true;
408 if (!object_property_parse(obj, p->property, p->value, &err)) {
409 error_prepend(&err, "can't apply global %s.%s=%s: ",
410 p->driver, p->property, p->value);
412 * If errp != NULL, propagate error and return.
413 * If errp == NULL, report a warning, but keep going
414 * with the remaining globals.
416 if (errp) {
417 error_propagate(errp, err);
418 return false;
419 } else {
420 warn_report_err(err);
425 return true;
429 * Global property defaults
430 * Slot 0: accelerator's global property defaults
431 * Slot 1: machine's global property defaults
432 * Slot 2: global properties from legacy command line option
433 * Each is a GPtrArray of of GlobalProperty.
434 * Applied in order, later entries override earlier ones.
436 static GPtrArray *object_compat_props[3];
439 * Retrieve @GPtrArray for global property defined with options
440 * other than "-global". These are generally used for syntactic
441 * sugar and legacy command line options.
443 void object_register_sugar_prop(const char *driver, const char *prop, const char *value)
445 GlobalProperty *g;
446 if (!object_compat_props[2]) {
447 object_compat_props[2] = g_ptr_array_new();
449 g = g_new0(GlobalProperty, 1);
450 g->driver = g_strdup(driver);
451 g->property = g_strdup(prop);
452 g->value = g_strdup(value);
453 g_ptr_array_add(object_compat_props[2], g);
457 * Set machine's global property defaults to @compat_props.
458 * May be called at most once.
460 void object_set_machine_compat_props(GPtrArray *compat_props)
462 assert(!object_compat_props[1]);
463 object_compat_props[1] = compat_props;
467 * Set accelerator's global property defaults to @compat_props.
468 * May be called at most once.
470 void object_set_accelerator_compat_props(GPtrArray *compat_props)
472 assert(!object_compat_props[0]);
473 object_compat_props[0] = compat_props;
476 void object_apply_compat_props(Object *obj)
478 int i;
480 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
481 object_apply_global_props(obj, object_compat_props[i],
482 i == 2 ? &error_fatal : &error_abort);
486 static void object_class_property_init_all(Object *obj)
488 ObjectPropertyIterator iter;
489 ObjectProperty *prop;
491 object_class_property_iter_init(&iter, object_get_class(obj));
492 while ((prop = object_property_iter_next(&iter))) {
493 if (prop->init) {
494 prop->init(obj, prop);
499 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
501 type_initialize(type);
503 g_assert(type->instance_size >= sizeof(Object));
504 g_assert(type->abstract == false);
505 g_assert(size >= type->instance_size);
507 memset(obj, 0, type->instance_size);
508 obj->class = type->class;
509 object_ref(obj);
510 object_class_property_init_all(obj);
511 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
512 NULL, object_property_free);
513 object_init_with_type(obj, type);
514 object_post_init_with_type(obj, type);
517 void object_initialize(void *data, size_t size, const char *typename)
519 TypeImpl *type = type_get_by_name(typename);
521 if (!type) {
522 error_report("missing object type '%s'", typename);
523 abort();
526 object_initialize_with_type(data, size, type);
529 bool object_initialize_child_with_props(Object *parentobj,
530 const char *propname,
531 void *childobj, size_t size,
532 const char *type,
533 Error **errp, ...)
535 va_list vargs;
536 bool ok;
538 va_start(vargs, errp);
539 ok = object_initialize_child_with_propsv(parentobj, propname,
540 childobj, size, type, errp,
541 vargs);
542 va_end(vargs);
543 return ok;
546 bool object_initialize_child_with_propsv(Object *parentobj,
547 const char *propname,
548 void *childobj, size_t size,
549 const char *type,
550 Error **errp, va_list vargs)
552 bool ok = false;
553 Object *obj;
554 UserCreatable *uc;
556 object_initialize(childobj, size, type);
557 obj = OBJECT(childobj);
559 if (!object_set_propv(obj, errp, vargs)) {
560 goto out;
563 object_property_add_child(parentobj, propname, obj);
565 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
566 if (uc) {
567 if (!user_creatable_complete(uc, errp)) {
568 object_unparent(obj);
569 goto out;
573 ok = true;
575 out:
577 * We want @obj's reference to be 1 on success, 0 on failure.
578 * On success, it's 2: one taken by object_initialize(), and one
579 * by object_property_add_child().
580 * On failure in object_initialize() or earlier, it's 1.
581 * On failure afterwards, it's also 1: object_unparent() releases
582 * the reference taken by object_property_add_child().
584 object_unref(obj);
585 return ok;
588 void object_initialize_child_internal(Object *parent,
589 const char *propname,
590 void *child, size_t size,
591 const char *type)
593 object_initialize_child_with_props(parent, propname, child, size, type,
594 &error_abort, NULL);
597 static inline bool object_property_is_child(ObjectProperty *prop)
599 return strstart(prop->type, "child<", NULL);
602 static void object_property_del_all(Object *obj)
604 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
605 ObjectProperty *prop;
606 ObjectPropertyIterator iter;
607 bool released;
609 do {
610 released = false;
611 object_property_iter_init(&iter, obj);
612 while ((prop = object_property_iter_next(&iter)) != NULL) {
613 if (g_hash_table_add(done, prop)) {
614 if (prop->release) {
615 prop->release(obj, prop->name, prop->opaque);
616 released = true;
617 break;
621 } while (released);
623 g_hash_table_unref(obj->properties);
626 static void object_property_del_child(Object *obj, Object *child)
628 ObjectProperty *prop;
629 GHashTableIter iter;
630 gpointer key, value;
632 g_hash_table_iter_init(&iter, obj->properties);
633 while (g_hash_table_iter_next(&iter, &key, &value)) {
634 prop = value;
635 if (object_property_is_child(prop) && prop->opaque == child) {
636 if (prop->release) {
637 prop->release(obj, prop->name, prop->opaque);
638 prop->release = NULL;
640 break;
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 g_hash_table_iter_remove(&iter);
648 break;
653 void object_unparent(Object *obj)
655 if (obj->parent) {
656 object_property_del_child(obj->parent, obj);
660 static void object_deinit(Object *obj, TypeImpl *type)
662 if (type->instance_finalize) {
663 type->instance_finalize(obj);
666 if (type_has_parent(type)) {
667 object_deinit(obj, type_get_parent(type));
671 static void object_finalize(void *data)
673 Object *obj = data;
674 TypeImpl *ti = obj->class->type;
676 object_property_del_all(obj);
677 object_deinit(obj, ti);
679 g_assert(obj->ref == 0);
680 if (obj->free) {
681 obj->free(obj);
685 static Object *object_new_with_type(Type type)
687 Object *obj;
689 g_assert(type != NULL);
690 type_initialize(type);
692 obj = g_malloc(type->instance_size);
693 object_initialize_with_type(obj, type->instance_size, type);
694 obj->free = g_free;
696 return obj;
699 Object *object_new_with_class(ObjectClass *klass)
701 return object_new_with_type(klass->type);
704 Object *object_new(const char *typename)
706 TypeImpl *ti = type_get_by_name(typename);
708 return object_new_with_type(ti);
712 Object *object_new_with_props(const char *typename,
713 Object *parent,
714 const char *id,
715 Error **errp,
716 ...)
718 va_list vargs;
719 Object *obj;
721 va_start(vargs, errp);
722 obj = object_new_with_propv(typename, parent, id, errp, vargs);
723 va_end(vargs);
725 return obj;
729 Object *object_new_with_propv(const char *typename,
730 Object *parent,
731 const char *id,
732 Error **errp,
733 va_list vargs)
735 Object *obj;
736 ObjectClass *klass;
737 UserCreatable *uc;
739 klass = object_class_by_name(typename);
740 if (!klass) {
741 error_setg(errp, "invalid object type: %s", typename);
742 return NULL;
745 if (object_class_is_abstract(klass)) {
746 error_setg(errp, "object type '%s' is abstract", typename);
747 return NULL;
749 obj = object_new_with_type(klass->type);
751 if (!object_set_propv(obj, errp, vargs)) {
752 goto error;
755 if (id != NULL) {
756 object_property_add_child(parent, id, obj);
759 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
760 if (uc) {
761 if (!user_creatable_complete(uc, errp)) {
762 if (id != NULL) {
763 object_unparent(obj);
765 goto error;
769 object_unref(obj);
770 return obj;
772 error:
773 object_unref(obj);
774 return NULL;
778 bool object_set_props(Object *obj,
779 Error **errp,
780 ...)
782 va_list vargs;
783 bool ret;
785 va_start(vargs, errp);
786 ret = object_set_propv(obj, errp, vargs);
787 va_end(vargs);
789 return ret;
793 bool object_set_propv(Object *obj,
794 Error **errp,
795 va_list vargs)
797 const char *propname;
799 propname = va_arg(vargs, char *);
800 while (propname != NULL) {
801 const char *value = va_arg(vargs, char *);
803 g_assert(value != NULL);
804 if (!object_property_parse(obj, propname, value, errp)) {
805 return false;
807 propname = va_arg(vargs, char *);
810 return true;
814 Object *object_dynamic_cast(Object *obj, const char *typename)
816 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
817 return obj;
820 return NULL;
823 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
824 const char *file, int line, const char *func)
826 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
827 typename, file, line, func);
829 #ifdef CONFIG_QOM_CAST_DEBUG
830 int i;
831 Object *inst;
833 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
834 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
835 goto out;
839 inst = object_dynamic_cast(obj, typename);
841 if (!inst && obj) {
842 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
843 file, line, func, obj, typename);
844 abort();
847 assert(obj == inst);
849 if (obj && obj == inst) {
850 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
851 atomic_set(&obj->class->object_cast_cache[i - 1],
852 atomic_read(&obj->class->object_cast_cache[i]));
854 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
857 out:
858 #endif
859 return obj;
862 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
863 const char *typename)
865 ObjectClass *ret = NULL;
866 TypeImpl *target_type;
867 TypeImpl *type;
869 if (!class) {
870 return NULL;
873 /* A simple fast path that can trigger a lot for leaf classes. */
874 type = class->type;
875 if (type->name == typename) {
876 return class;
879 target_type = type_get_by_name(typename);
880 if (!target_type) {
881 /* target class type unknown, so fail the cast */
882 return NULL;
885 if (type->class->interfaces &&
886 type_is_ancestor(target_type, type_interface)) {
887 int found = 0;
888 GSList *i;
890 for (i = class->interfaces; i; i = i->next) {
891 ObjectClass *target_class = i->data;
893 if (type_is_ancestor(target_class->type, target_type)) {
894 ret = target_class;
895 found++;
899 /* The match was ambiguous, don't allow a cast */
900 if (found > 1) {
901 ret = NULL;
903 } else if (type_is_ancestor(type, target_type)) {
904 ret = class;
907 return ret;
910 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
911 const char *typename,
912 const char *file, int line,
913 const char *func)
915 ObjectClass *ret;
917 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
918 typename, file, line, func);
920 #ifdef CONFIG_QOM_CAST_DEBUG
921 int i;
923 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
924 if (atomic_read(&class->class_cast_cache[i]) == typename) {
925 ret = class;
926 goto out;
929 #else
930 if (!class || !class->interfaces) {
931 return class;
933 #endif
935 ret = object_class_dynamic_cast(class, typename);
936 if (!ret && class) {
937 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
938 file, line, func, class, typename);
939 abort();
942 #ifdef CONFIG_QOM_CAST_DEBUG
943 if (class && ret == class) {
944 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
945 atomic_set(&class->class_cast_cache[i - 1],
946 atomic_read(&class->class_cast_cache[i]));
948 atomic_set(&class->class_cast_cache[i - 1], typename);
950 out:
951 #endif
952 return ret;
955 const char *object_get_typename(const Object *obj)
957 return obj->class->type->name;
960 ObjectClass *object_get_class(Object *obj)
962 return obj->class;
965 bool object_class_is_abstract(ObjectClass *klass)
967 return klass->type->abstract;
970 const char *object_class_get_name(ObjectClass *klass)
972 return klass->type->name;
975 ObjectClass *object_class_by_name(const char *typename)
977 TypeImpl *type = type_get_by_name(typename);
979 if (!type) {
980 return NULL;
983 type_initialize(type);
985 return type->class;
988 ObjectClass *module_object_class_by_name(const char *typename)
990 ObjectClass *oc;
992 oc = object_class_by_name(typename);
993 #ifdef CONFIG_MODULES
994 if (!oc) {
995 module_load_qom_one(typename);
996 oc = object_class_by_name(typename);
998 #endif
999 return oc;
1002 ObjectClass *object_class_get_parent(ObjectClass *class)
1004 TypeImpl *type = type_get_parent(class->type);
1006 if (!type) {
1007 return NULL;
1010 type_initialize(type);
1012 return type->class;
1015 typedef struct OCFData
1017 void (*fn)(ObjectClass *klass, void *opaque);
1018 const char *implements_type;
1019 bool include_abstract;
1020 void *opaque;
1021 } OCFData;
1023 static void object_class_foreach_tramp(gpointer key, gpointer value,
1024 gpointer opaque)
1026 OCFData *data = opaque;
1027 TypeImpl *type = value;
1028 ObjectClass *k;
1030 type_initialize(type);
1031 k = type->class;
1033 if (!data->include_abstract && type->abstract) {
1034 return;
1037 if (data->implements_type &&
1038 !object_class_dynamic_cast(k, data->implements_type)) {
1039 return;
1042 data->fn(k, data->opaque);
1045 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1046 const char *implements_type, bool include_abstract,
1047 void *opaque)
1049 OCFData data = { fn, implements_type, include_abstract, opaque };
1051 enumerating_types = true;
1052 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1053 enumerating_types = false;
1056 static int do_object_child_foreach(Object *obj,
1057 int (*fn)(Object *child, void *opaque),
1058 void *opaque, bool recurse)
1060 GHashTableIter iter;
1061 ObjectProperty *prop;
1062 int ret = 0;
1064 g_hash_table_iter_init(&iter, obj->properties);
1065 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1066 if (object_property_is_child(prop)) {
1067 Object *child = prop->opaque;
1069 ret = fn(child, opaque);
1070 if (ret != 0) {
1071 break;
1073 if (recurse) {
1074 ret = do_object_child_foreach(child, fn, opaque, true);
1075 if (ret != 0) {
1076 break;
1081 return ret;
1084 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1085 void *opaque)
1087 return do_object_child_foreach(obj, fn, opaque, false);
1090 int object_child_foreach_recursive(Object *obj,
1091 int (*fn)(Object *child, void *opaque),
1092 void *opaque)
1094 return do_object_child_foreach(obj, fn, opaque, true);
1097 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1099 GSList **list = opaque;
1101 *list = g_slist_prepend(*list, klass);
1104 GSList *object_class_get_list(const char *implements_type,
1105 bool include_abstract)
1107 GSList *list = NULL;
1109 object_class_foreach(object_class_get_list_tramp,
1110 implements_type, include_abstract, &list);
1111 return list;
1114 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1116 return strcasecmp(object_class_get_name((ObjectClass *)a),
1117 object_class_get_name((ObjectClass *)b));
1120 GSList *object_class_get_list_sorted(const char *implements_type,
1121 bool include_abstract)
1123 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1124 object_class_cmp);
1127 Object *object_ref(Object *obj)
1129 if (!obj) {
1130 return NULL;
1132 atomic_inc(&obj->ref);
1133 return obj;
1136 void object_unref(Object *obj)
1138 if (!obj) {
1139 return;
1141 g_assert(obj->ref > 0);
1143 /* parent always holds a reference to its children */
1144 if (atomic_fetch_dec(&obj->ref) == 1) {
1145 object_finalize(obj);
1149 ObjectProperty *
1150 object_property_try_add(Object *obj, const char *name, const char *type,
1151 ObjectPropertyAccessor *get,
1152 ObjectPropertyAccessor *set,
1153 ObjectPropertyRelease *release,
1154 void *opaque, Error **errp)
1156 ObjectProperty *prop;
1157 size_t name_len = strlen(name);
1159 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1160 int i;
1161 ObjectProperty *ret;
1162 char *name_no_array = g_strdup(name);
1164 name_no_array[name_len - 3] = '\0';
1165 for (i = 0; ; ++i) {
1166 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1168 ret = object_property_try_add(obj, full_name, type, get, set,
1169 release, opaque, NULL);
1170 g_free(full_name);
1171 if (ret) {
1172 break;
1175 g_free(name_no_array);
1176 return ret;
1179 if (object_property_find(obj, name, NULL) != NULL) {
1180 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1181 name, object_get_typename(obj));
1182 return NULL;
1185 prop = g_malloc0(sizeof(*prop));
1187 prop->name = g_strdup(name);
1188 prop->type = g_strdup(type);
1190 prop->get = get;
1191 prop->set = set;
1192 prop->release = release;
1193 prop->opaque = opaque;
1195 g_hash_table_insert(obj->properties, prop->name, prop);
1196 return prop;
1199 ObjectProperty *
1200 object_property_add(Object *obj, const char *name, const char *type,
1201 ObjectPropertyAccessor *get,
1202 ObjectPropertyAccessor *set,
1203 ObjectPropertyRelease *release,
1204 void *opaque)
1206 return object_property_try_add(obj, name, type, get, set, release,
1207 opaque, &error_abort);
1210 ObjectProperty *
1211 object_class_property_add(ObjectClass *klass,
1212 const char *name,
1213 const char *type,
1214 ObjectPropertyAccessor *get,
1215 ObjectPropertyAccessor *set,
1216 ObjectPropertyRelease *release,
1217 void *opaque)
1219 ObjectProperty *prop;
1221 assert(!object_class_property_find(klass, name, NULL));
1223 prop = g_malloc0(sizeof(*prop));
1225 prop->name = g_strdup(name);
1226 prop->type = g_strdup(type);
1228 prop->get = get;
1229 prop->set = set;
1230 prop->release = release;
1231 prop->opaque = opaque;
1233 g_hash_table_insert(klass->properties, prop->name, prop);
1235 return prop;
1238 ObjectProperty *object_property_find(Object *obj, const char *name,
1239 Error **errp)
1241 ObjectProperty *prop;
1242 ObjectClass *klass = object_get_class(obj);
1244 prop = object_class_property_find(klass, name, NULL);
1245 if (prop) {
1246 return prop;
1249 prop = g_hash_table_lookup(obj->properties, name);
1250 if (prop) {
1251 return prop;
1254 error_setg(errp, "Property '.%s' not found", name);
1255 return NULL;
1258 void object_property_iter_init(ObjectPropertyIterator *iter,
1259 Object *obj)
1261 g_hash_table_iter_init(&iter->iter, obj->properties);
1262 iter->nextclass = object_get_class(obj);
1265 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1267 gpointer key, val;
1268 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1269 if (!iter->nextclass) {
1270 return NULL;
1272 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1273 iter->nextclass = object_class_get_parent(iter->nextclass);
1275 return val;
1278 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1279 ObjectClass *klass)
1281 g_hash_table_iter_init(&iter->iter, klass->properties);
1282 iter->nextclass = object_class_get_parent(klass);
1285 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1286 Error **errp)
1288 ObjectProperty *prop;
1289 ObjectClass *parent_klass;
1291 parent_klass = object_class_get_parent(klass);
1292 if (parent_klass) {
1293 prop = object_class_property_find(parent_klass, name, NULL);
1294 if (prop) {
1295 return prop;
1299 prop = g_hash_table_lookup(klass->properties, name);
1300 if (!prop) {
1301 error_setg(errp, "Property '.%s' not found", name);
1303 return prop;
1306 void object_property_del(Object *obj, const char *name)
1308 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1310 if (prop->release) {
1311 prop->release(obj, name, prop->opaque);
1313 g_hash_table_remove(obj->properties, name);
1316 bool object_property_get(Object *obj, const char *name, Visitor *v,
1317 Error **errp)
1319 Error *err = NULL;
1320 ObjectProperty *prop = object_property_find(obj, name, errp);
1322 if (prop == NULL) {
1323 return false;
1326 if (!prop->get) {
1327 error_setg(errp, QERR_PERMISSION_DENIED);
1328 return false;
1330 prop->get(obj, v, name, prop->opaque, &err);
1331 error_propagate(errp, err);
1332 return !err;
1335 bool object_property_set(Object *obj, const char *name, Visitor *v,
1336 Error **errp)
1338 Error *err = NULL;
1339 ObjectProperty *prop = object_property_find(obj, name, errp);
1341 if (prop == NULL) {
1342 return false;
1345 if (!prop->set) {
1346 error_setg(errp, QERR_PERMISSION_DENIED);
1347 return false;
1349 prop->set(obj, v, name, prop->opaque, &err);
1350 error_propagate(errp, err);
1351 return !err;
1354 bool object_property_set_str(Object *obj, const char *name,
1355 const char *value, Error **errp)
1357 QString *qstr = qstring_from_str(value);
1358 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
1360 qobject_unref(qstr);
1361 return ok;
1364 char *object_property_get_str(Object *obj, const char *name,
1365 Error **errp)
1367 QObject *ret = object_property_get_qobject(obj, name, errp);
1368 char *retval;
1370 if (!ret) {
1371 return NULL;
1374 retval = g_strdup(qobject_get_try_str(ret));
1375 if (!retval) {
1376 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1379 qobject_unref(ret);
1380 return retval;
1383 bool object_property_set_link(Object *obj, const char *name,
1384 Object *value, Error **errp)
1386 g_autofree char *path = NULL;
1388 if (value) {
1389 path = object_get_canonical_path(value);
1391 return object_property_set_str(obj, name, path ?: "", errp);
1394 Object *object_property_get_link(Object *obj, const char *name,
1395 Error **errp)
1397 char *str = object_property_get_str(obj, name, errp);
1398 Object *target = NULL;
1400 if (str && *str) {
1401 target = object_resolve_path(str, NULL);
1402 if (!target) {
1403 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1404 "Device '%s' not found", str);
1408 g_free(str);
1409 return target;
1412 bool object_property_set_bool(Object *obj, const char *name,
1413 bool value, Error **errp)
1415 QBool *qbool = qbool_from_bool(value);
1416 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
1418 qobject_unref(qbool);
1419 return ok;
1422 bool object_property_get_bool(Object *obj, const char *name,
1423 Error **errp)
1425 QObject *ret = object_property_get_qobject(obj, name, errp);
1426 QBool *qbool;
1427 bool retval;
1429 if (!ret) {
1430 return false;
1432 qbool = qobject_to(QBool, ret);
1433 if (!qbool) {
1434 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1435 retval = false;
1436 } else {
1437 retval = qbool_get_bool(qbool);
1440 qobject_unref(ret);
1441 return retval;
1444 bool object_property_set_int(Object *obj, const char *name,
1445 int64_t value, Error **errp)
1447 QNum *qnum = qnum_from_int(value);
1448 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1450 qobject_unref(qnum);
1451 return ok;
1454 int64_t object_property_get_int(Object *obj, const char *name,
1455 Error **errp)
1457 QObject *ret = object_property_get_qobject(obj, name, errp);
1458 QNum *qnum;
1459 int64_t retval;
1461 if (!ret) {
1462 return -1;
1465 qnum = qobject_to(QNum, ret);
1466 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1467 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1468 retval = -1;
1471 qobject_unref(ret);
1472 return retval;
1475 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1477 Visitor *v = qobject_input_visitor_new(prop->defval);
1479 assert(prop->set != NULL);
1480 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1482 visit_free(v);
1485 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1487 assert(!prop->defval);
1488 assert(!prop->init);
1490 prop->defval = defval;
1491 prop->init = object_property_init_defval;
1494 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1496 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1499 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1501 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1504 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1506 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1509 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1511 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1514 bool object_property_set_uint(Object *obj, const char *name,
1515 uint64_t value, Error **errp)
1517 QNum *qnum = qnum_from_uint(value);
1518 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1520 qobject_unref(qnum);
1521 return ok;
1524 uint64_t object_property_get_uint(Object *obj, const char *name,
1525 Error **errp)
1527 QObject *ret = object_property_get_qobject(obj, name, errp);
1528 QNum *qnum;
1529 uint64_t retval;
1531 if (!ret) {
1532 return 0;
1534 qnum = qobject_to(QNum, ret);
1535 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1536 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1537 retval = 0;
1540 qobject_unref(ret);
1541 return retval;
1544 typedef struct EnumProperty {
1545 const QEnumLookup *lookup;
1546 int (*get)(Object *, Error **);
1547 void (*set)(Object *, int, Error **);
1548 } EnumProperty;
1550 int object_property_get_enum(Object *obj, const char *name,
1551 const char *typename, Error **errp)
1553 char *str;
1554 int ret;
1555 ObjectProperty *prop = object_property_find(obj, name, errp);
1556 EnumProperty *enumprop;
1558 if (prop == NULL) {
1559 return 0;
1562 if (!g_str_equal(prop->type, typename)) {
1563 error_setg(errp, "Property %s on %s is not '%s' enum type",
1564 name, object_class_get_name(
1565 object_get_class(obj)), typename);
1566 return 0;
1569 enumprop = prop->opaque;
1571 str = object_property_get_str(obj, name, errp);
1572 if (!str) {
1573 return 0;
1576 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1577 g_free(str);
1579 return ret;
1582 bool object_property_parse(Object *obj, const char *name,
1583 const char *string, Error **errp)
1585 Visitor *v = string_input_visitor_new(string);
1586 bool ok = object_property_set(obj, name, v, errp);
1588 visit_free(v);
1589 return ok;
1592 char *object_property_print(Object *obj, const char *name, bool human,
1593 Error **errp)
1595 Visitor *v;
1596 char *string = NULL;
1598 v = string_output_visitor_new(human, &string);
1599 if (!object_property_get(obj, name, v, errp)) {
1600 goto out;
1603 visit_complete(v, &string);
1605 out:
1606 visit_free(v);
1607 return string;
1610 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1612 ObjectProperty *prop = object_property_find(obj, name, errp);
1613 if (prop == NULL) {
1614 return NULL;
1617 return prop->type;
1620 Object *object_get_root(void)
1622 static Object *root;
1624 if (!root) {
1625 root = object_new("container");
1628 return root;
1631 Object *object_get_objects_root(void)
1633 return container_get(object_get_root(), "/objects");
1636 Object *object_get_internal_root(void)
1638 static Object *internal_root;
1640 if (!internal_root) {
1641 internal_root = object_new("container");
1644 return internal_root;
1647 static void object_get_child_property(Object *obj, Visitor *v,
1648 const char *name, void *opaque,
1649 Error **errp)
1651 Object *child = opaque;
1652 char *path;
1654 path = object_get_canonical_path(child);
1655 visit_type_str(v, name, &path, errp);
1656 g_free(path);
1659 static Object *object_resolve_child_property(Object *parent, void *opaque,
1660 const char *part)
1662 return opaque;
1665 static void object_finalize_child_property(Object *obj, const char *name,
1666 void *opaque)
1668 Object *child = opaque;
1670 if (child->class->unparent) {
1671 (child->class->unparent)(child);
1673 child->parent = NULL;
1674 object_unref(child);
1677 ObjectProperty *
1678 object_property_try_add_child(Object *obj, const char *name,
1679 Object *child, Error **errp)
1681 g_autofree char *type = NULL;
1682 ObjectProperty *op;
1684 assert(!child->parent);
1686 type = g_strdup_printf("child<%s>", object_get_typename(child));
1688 op = object_property_try_add(obj, name, type, object_get_child_property,
1689 NULL, object_finalize_child_property,
1690 child, errp);
1691 if (!op) {
1692 return NULL;
1694 op->resolve = object_resolve_child_property;
1695 object_ref(child);
1696 child->parent = obj;
1697 return op;
1700 ObjectProperty *
1701 object_property_add_child(Object *obj, const char *name,
1702 Object *child)
1704 return object_property_try_add_child(obj, name, child, &error_abort);
1707 void object_property_allow_set_link(const Object *obj, const char *name,
1708 Object *val, Error **errp)
1710 /* Allow the link to be set, always */
1713 typedef struct {
1714 union {
1715 Object **targetp;
1716 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1717 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1719 void (*check)(const Object *, const char *, Object *, Error **);
1720 ObjectPropertyLinkFlags flags;
1721 } LinkProperty;
1723 static Object **
1724 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1726 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1727 return &lprop->target;
1728 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1729 return (void *)obj + lprop->offset;
1730 } else {
1731 return lprop->targetp;
1735 static void object_get_link_property(Object *obj, Visitor *v,
1736 const char *name, void *opaque,
1737 Error **errp)
1739 LinkProperty *lprop = opaque;
1740 Object **targetp = object_link_get_targetp(obj, lprop);
1741 char *path;
1743 if (*targetp) {
1744 path = object_get_canonical_path(*targetp);
1745 visit_type_str(v, name, &path, errp);
1746 g_free(path);
1747 } else {
1748 path = (char *)"";
1749 visit_type_str(v, name, &path, errp);
1754 * object_resolve_link:
1756 * Lookup an object and ensure its type matches the link property type. This
1757 * is similar to object_resolve_path() except type verification against the
1758 * link property is performed.
1760 * Returns: The matched object or NULL on path lookup failures.
1762 static Object *object_resolve_link(Object *obj, const char *name,
1763 const char *path, Error **errp)
1765 const char *type;
1766 char *target_type;
1767 bool ambiguous = false;
1768 Object *target;
1770 /* Go from link<FOO> to FOO. */
1771 type = object_property_get_type(obj, name, NULL);
1772 target_type = g_strndup(&type[5], strlen(type) - 6);
1773 target = object_resolve_path_type(path, target_type, &ambiguous);
1775 if (ambiguous) {
1776 error_setg(errp, "Path '%s' does not uniquely identify an object",
1777 path);
1778 } else if (!target) {
1779 target = object_resolve_path(path, &ambiguous);
1780 if (target || ambiguous) {
1781 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1782 } else {
1783 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1784 "Device '%s' not found", path);
1786 target = NULL;
1788 g_free(target_type);
1790 return target;
1793 static void object_set_link_property(Object *obj, Visitor *v,
1794 const char *name, void *opaque,
1795 Error **errp)
1797 Error *local_err = NULL;
1798 LinkProperty *prop = opaque;
1799 Object **targetp = object_link_get_targetp(obj, prop);
1800 Object *old_target = *targetp;
1801 Object *new_target;
1802 char *path = NULL;
1804 if (!visit_type_str(v, name, &path, errp)) {
1805 return;
1808 if (*path) {
1809 new_target = object_resolve_link(obj, name, path, errp);
1810 if (!new_target) {
1811 g_free(path);
1812 return;
1814 } else {
1815 new_target = NULL;
1818 g_free(path);
1820 prop->check(obj, name, new_target, &local_err);
1821 if (local_err) {
1822 error_propagate(errp, local_err);
1823 return;
1826 *targetp = new_target;
1827 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1828 object_ref(new_target);
1829 object_unref(old_target);
1833 static Object *object_resolve_link_property(Object *parent, void *opaque,
1834 const char *part)
1836 LinkProperty *lprop = opaque;
1838 return *object_link_get_targetp(parent, lprop);
1841 static void object_release_link_property(Object *obj, const char *name,
1842 void *opaque)
1844 LinkProperty *prop = opaque;
1845 Object **targetp = object_link_get_targetp(obj, prop);
1847 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1848 object_unref(*targetp);
1850 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1851 g_free(prop);
1855 static ObjectProperty *
1856 object_add_link_prop(Object *obj, const char *name,
1857 const char *type, void *ptr,
1858 void (*check)(const Object *, const char *,
1859 Object *, Error **),
1860 ObjectPropertyLinkFlags flags)
1862 LinkProperty *prop = g_malloc(sizeof(*prop));
1863 g_autofree char *full_type = NULL;
1864 ObjectProperty *op;
1866 if (flags & OBJ_PROP_LINK_DIRECT) {
1867 prop->target = ptr;
1868 } else {
1869 prop->targetp = ptr;
1871 prop->check = check;
1872 prop->flags = flags;
1874 full_type = g_strdup_printf("link<%s>", type);
1876 op = object_property_add(obj, name, full_type,
1877 object_get_link_property,
1878 check ? object_set_link_property : NULL,
1879 object_release_link_property,
1880 prop);
1881 op->resolve = object_resolve_link_property;
1882 return op;
1885 ObjectProperty *
1886 object_property_add_link(Object *obj, const char *name,
1887 const char *type, Object **targetp,
1888 void (*check)(const Object *, const char *,
1889 Object *, Error **),
1890 ObjectPropertyLinkFlags flags)
1892 return object_add_link_prop(obj, name, type, targetp, check, flags);
1895 ObjectProperty *
1896 object_class_property_add_link(ObjectClass *oc,
1897 const char *name,
1898 const char *type, ptrdiff_t offset,
1899 void (*check)(const Object *obj, const char *name,
1900 Object *val, Error **errp),
1901 ObjectPropertyLinkFlags flags)
1903 LinkProperty *prop = g_new0(LinkProperty, 1);
1904 char *full_type;
1905 ObjectProperty *op;
1907 prop->offset = offset;
1908 prop->check = check;
1909 prop->flags = flags | OBJ_PROP_LINK_CLASS;
1911 full_type = g_strdup_printf("link<%s>", type);
1913 op = object_class_property_add(oc, name, full_type,
1914 object_get_link_property,
1915 check ? object_set_link_property : NULL,
1916 object_release_link_property,
1917 prop);
1919 op->resolve = object_resolve_link_property;
1921 g_free(full_type);
1922 return op;
1925 ObjectProperty *
1926 object_property_add_const_link(Object *obj, const char *name,
1927 Object *target)
1929 return object_add_link_prop(obj, name,
1930 object_get_typename(target), target,
1931 NULL, OBJ_PROP_LINK_DIRECT);
1934 char *object_get_canonical_path_component(const Object *obj)
1936 ObjectProperty *prop = NULL;
1937 GHashTableIter iter;
1939 if (obj->parent == NULL) {
1940 return NULL;
1943 g_hash_table_iter_init(&iter, obj->parent->properties);
1944 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1945 if (!object_property_is_child(prop)) {
1946 continue;
1949 if (prop->opaque == obj) {
1950 return g_strdup(prop->name);
1954 /* obj had a parent but was not a child, should never happen */
1955 g_assert_not_reached();
1956 return NULL;
1959 char *object_get_canonical_path(const Object *obj)
1961 Object *root = object_get_root();
1962 char *newpath, *path = NULL;
1964 if (obj == root) {
1965 return g_strdup("/");
1968 do {
1969 char *component = object_get_canonical_path_component(obj);
1971 if (!component) {
1972 /* A canonical path must be complete, so discard what was
1973 * collected so far.
1975 g_free(path);
1976 return NULL;
1979 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1980 g_free(path);
1981 g_free(component);
1982 path = newpath;
1983 obj = obj->parent;
1984 } while (obj != root);
1986 return path;
1989 Object *object_resolve_path_component(Object *parent, const char *part)
1991 ObjectProperty *prop = object_property_find(parent, part, NULL);
1992 if (prop == NULL) {
1993 return NULL;
1996 if (prop->resolve) {
1997 return prop->resolve(parent, prop->opaque, part);
1998 } else {
1999 return NULL;
2003 static Object *object_resolve_abs_path(Object *parent,
2004 char **parts,
2005 const char *typename)
2007 Object *child;
2009 if (*parts == NULL) {
2010 return object_dynamic_cast(parent, typename);
2013 if (strcmp(*parts, "") == 0) {
2014 return object_resolve_abs_path(parent, parts + 1, typename);
2017 child = object_resolve_path_component(parent, *parts);
2018 if (!child) {
2019 return NULL;
2022 return object_resolve_abs_path(child, parts + 1, typename);
2025 static Object *object_resolve_partial_path(Object *parent,
2026 char **parts,
2027 const char *typename,
2028 bool *ambiguous)
2030 Object *obj;
2031 GHashTableIter iter;
2032 ObjectProperty *prop;
2034 obj = object_resolve_abs_path(parent, parts, typename);
2036 g_hash_table_iter_init(&iter, parent->properties);
2037 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2038 Object *found;
2040 if (!object_property_is_child(prop)) {
2041 continue;
2044 found = object_resolve_partial_path(prop->opaque, parts,
2045 typename, ambiguous);
2046 if (found) {
2047 if (obj) {
2048 *ambiguous = true;
2049 return NULL;
2051 obj = found;
2054 if (*ambiguous) {
2055 return NULL;
2059 return obj;
2062 Object *object_resolve_path_type(const char *path, const char *typename,
2063 bool *ambiguousp)
2065 Object *obj;
2066 char **parts;
2068 parts = g_strsplit(path, "/", 0);
2069 assert(parts);
2071 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2072 bool ambiguous = false;
2073 obj = object_resolve_partial_path(object_get_root(), parts,
2074 typename, &ambiguous);
2075 if (ambiguousp) {
2076 *ambiguousp = ambiguous;
2078 } else {
2079 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2082 g_strfreev(parts);
2084 return obj;
2087 Object *object_resolve_path(const char *path, bool *ambiguous)
2089 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2092 typedef struct StringProperty
2094 char *(*get)(Object *, Error **);
2095 void (*set)(Object *, const char *, Error **);
2096 } StringProperty;
2098 static void property_get_str(Object *obj, Visitor *v, const char *name,
2099 void *opaque, Error **errp)
2101 StringProperty *prop = opaque;
2102 char *value;
2103 Error *err = NULL;
2105 value = prop->get(obj, &err);
2106 if (err) {
2107 error_propagate(errp, err);
2108 return;
2111 visit_type_str(v, name, &value, errp);
2112 g_free(value);
2115 static void property_set_str(Object *obj, Visitor *v, const char *name,
2116 void *opaque, Error **errp)
2118 StringProperty *prop = opaque;
2119 char *value;
2121 if (!visit_type_str(v, name, &value, errp)) {
2122 return;
2125 prop->set(obj, value, errp);
2126 g_free(value);
2129 static void property_release_str(Object *obj, const char *name,
2130 void *opaque)
2132 StringProperty *prop = opaque;
2133 g_free(prop);
2136 ObjectProperty *
2137 object_property_add_str(Object *obj, const char *name,
2138 char *(*get)(Object *, Error **),
2139 void (*set)(Object *, const char *, Error **))
2141 StringProperty *prop = g_malloc0(sizeof(*prop));
2143 prop->get = get;
2144 prop->set = set;
2146 return object_property_add(obj, name, "string",
2147 get ? property_get_str : NULL,
2148 set ? property_set_str : NULL,
2149 property_release_str,
2150 prop);
2153 ObjectProperty *
2154 object_class_property_add_str(ObjectClass *klass, const char *name,
2155 char *(*get)(Object *, Error **),
2156 void (*set)(Object *, const char *,
2157 Error **))
2159 StringProperty *prop = g_malloc0(sizeof(*prop));
2161 prop->get = get;
2162 prop->set = set;
2164 return object_class_property_add(klass, name, "string",
2165 get ? property_get_str : NULL,
2166 set ? property_set_str : NULL,
2167 NULL,
2168 prop);
2171 typedef struct BoolProperty
2173 bool (*get)(Object *, Error **);
2174 void (*set)(Object *, bool, Error **);
2175 } BoolProperty;
2177 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2178 void *opaque, Error **errp)
2180 BoolProperty *prop = opaque;
2181 bool value;
2182 Error *err = NULL;
2184 value = prop->get(obj, &err);
2185 if (err) {
2186 error_propagate(errp, err);
2187 return;
2190 visit_type_bool(v, name, &value, errp);
2193 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2194 void *opaque, Error **errp)
2196 BoolProperty *prop = opaque;
2197 bool value;
2199 if (!visit_type_bool(v, name, &value, errp)) {
2200 return;
2203 prop->set(obj, value, errp);
2206 static void property_release_bool(Object *obj, const char *name,
2207 void *opaque)
2209 BoolProperty *prop = opaque;
2210 g_free(prop);
2213 ObjectProperty *
2214 object_property_add_bool(Object *obj, const char *name,
2215 bool (*get)(Object *, Error **),
2216 void (*set)(Object *, bool, Error **))
2218 BoolProperty *prop = g_malloc0(sizeof(*prop));
2220 prop->get = get;
2221 prop->set = set;
2223 return object_property_add(obj, name, "bool",
2224 get ? property_get_bool : NULL,
2225 set ? property_set_bool : NULL,
2226 property_release_bool,
2227 prop);
2230 ObjectProperty *
2231 object_class_property_add_bool(ObjectClass *klass, const char *name,
2232 bool (*get)(Object *, Error **),
2233 void (*set)(Object *, bool, Error **))
2235 BoolProperty *prop = g_malloc0(sizeof(*prop));
2237 prop->get = get;
2238 prop->set = set;
2240 return object_class_property_add(klass, name, "bool",
2241 get ? property_get_bool : NULL,
2242 set ? property_set_bool : NULL,
2243 NULL,
2244 prop);
2247 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2248 void *opaque, Error **errp)
2250 EnumProperty *prop = opaque;
2251 int value;
2252 Error *err = NULL;
2254 value = prop->get(obj, &err);
2255 if (err) {
2256 error_propagate(errp, err);
2257 return;
2260 visit_type_enum(v, name, &value, prop->lookup, errp);
2263 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2264 void *opaque, Error **errp)
2266 EnumProperty *prop = opaque;
2267 int value;
2269 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
2270 return;
2272 prop->set(obj, value, errp);
2275 static void property_release_enum(Object *obj, const char *name,
2276 void *opaque)
2278 EnumProperty *prop = opaque;
2279 g_free(prop);
2282 ObjectProperty *
2283 object_property_add_enum(Object *obj, const char *name,
2284 const char *typename,
2285 const QEnumLookup *lookup,
2286 int (*get)(Object *, Error **),
2287 void (*set)(Object *, int, Error **))
2289 EnumProperty *prop = g_malloc(sizeof(*prop));
2291 prop->lookup = lookup;
2292 prop->get = get;
2293 prop->set = set;
2295 return object_property_add(obj, name, typename,
2296 get ? property_get_enum : NULL,
2297 set ? property_set_enum : NULL,
2298 property_release_enum,
2299 prop);
2302 ObjectProperty *
2303 object_class_property_add_enum(ObjectClass *klass, const char *name,
2304 const char *typename,
2305 const QEnumLookup *lookup,
2306 int (*get)(Object *, Error **),
2307 void (*set)(Object *, int, Error **))
2309 EnumProperty *prop = g_malloc(sizeof(*prop));
2311 prop->lookup = lookup;
2312 prop->get = get;
2313 prop->set = set;
2315 return object_class_property_add(klass, name, typename,
2316 get ? property_get_enum : NULL,
2317 set ? property_set_enum : NULL,
2318 NULL,
2319 prop);
2322 typedef struct TMProperty {
2323 void (*get)(Object *, struct tm *, Error **);
2324 } TMProperty;
2326 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2327 void *opaque, Error **errp)
2329 TMProperty *prop = opaque;
2330 Error *err = NULL;
2331 struct tm value;
2333 prop->get(obj, &value, &err);
2334 if (err) {
2335 error_propagate(errp, err);
2336 return;
2339 if (!visit_start_struct(v, name, NULL, 0, errp)) {
2340 return;
2342 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
2343 goto out_end;
2345 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
2346 goto out_end;
2348 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
2349 goto out_end;
2351 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
2352 goto out_end;
2354 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
2355 goto out_end;
2357 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
2358 goto out_end;
2360 visit_check_struct(v, errp);
2361 out_end:
2362 visit_end_struct(v, NULL);
2365 static void property_release_tm(Object *obj, const char *name,
2366 void *opaque)
2368 TMProperty *prop = opaque;
2369 g_free(prop);
2372 ObjectProperty *
2373 object_property_add_tm(Object *obj, const char *name,
2374 void (*get)(Object *, struct tm *, Error **))
2376 TMProperty *prop = g_malloc0(sizeof(*prop));
2378 prop->get = get;
2380 return object_property_add(obj, name, "struct tm",
2381 get ? property_get_tm : NULL, NULL,
2382 property_release_tm,
2383 prop);
2386 ObjectProperty *
2387 object_class_property_add_tm(ObjectClass *klass, const char *name,
2388 void (*get)(Object *, struct tm *, Error **))
2390 TMProperty *prop = g_malloc0(sizeof(*prop));
2392 prop->get = get;
2394 return object_class_property_add(klass, name, "struct tm",
2395 get ? property_get_tm : NULL,
2396 NULL, NULL, prop);
2399 static char *object_get_type(Object *obj, Error **errp)
2401 return g_strdup(object_get_typename(obj));
2404 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2405 void *opaque, Error **errp)
2407 uint8_t value = *(uint8_t *)opaque;
2408 visit_type_uint8(v, name, &value, errp);
2411 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2412 void *opaque, Error **errp)
2414 uint8_t *field = opaque;
2415 uint8_t value;
2417 if (!visit_type_uint8(v, name, &value, errp)) {
2418 return;
2421 *field = value;
2424 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2425 void *opaque, Error **errp)
2427 uint16_t value = *(uint16_t *)opaque;
2428 visit_type_uint16(v, name, &value, errp);
2431 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2432 void *opaque, Error **errp)
2434 uint16_t *field = opaque;
2435 uint16_t value;
2437 if (!visit_type_uint16(v, name, &value, errp)) {
2438 return;
2441 *field = value;
2444 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2445 void *opaque, Error **errp)
2447 uint32_t value = *(uint32_t *)opaque;
2448 visit_type_uint32(v, name, &value, errp);
2451 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2452 void *opaque, Error **errp)
2454 uint32_t *field = opaque;
2455 uint32_t value;
2457 if (!visit_type_uint32(v, name, &value, errp)) {
2458 return;
2461 *field = value;
2464 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2465 void *opaque, Error **errp)
2467 uint64_t value = *(uint64_t *)opaque;
2468 visit_type_uint64(v, name, &value, errp);
2471 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2472 void *opaque, Error **errp)
2474 uint64_t *field = opaque;
2475 uint64_t value;
2477 if (!visit_type_uint64(v, name, &value, errp)) {
2478 return;
2481 *field = value;
2484 ObjectProperty *
2485 object_property_add_uint8_ptr(Object *obj, const char *name,
2486 const uint8_t *v,
2487 ObjectPropertyFlags flags)
2489 ObjectPropertyAccessor *getter = NULL;
2490 ObjectPropertyAccessor *setter = NULL;
2492 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2493 getter = property_get_uint8_ptr;
2496 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2497 setter = property_set_uint8_ptr;
2500 return object_property_add(obj, name, "uint8",
2501 getter, setter, NULL, (void *)v);
2504 ObjectProperty *
2505 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2506 const uint8_t *v,
2507 ObjectPropertyFlags flags)
2509 ObjectPropertyAccessor *getter = NULL;
2510 ObjectPropertyAccessor *setter = NULL;
2512 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2513 getter = property_get_uint8_ptr;
2516 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2517 setter = property_set_uint8_ptr;
2520 return object_class_property_add(klass, name, "uint8",
2521 getter, setter, NULL, (void *)v);
2524 ObjectProperty *
2525 object_property_add_uint16_ptr(Object *obj, const char *name,
2526 const uint16_t *v,
2527 ObjectPropertyFlags flags)
2529 ObjectPropertyAccessor *getter = NULL;
2530 ObjectPropertyAccessor *setter = NULL;
2532 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2533 getter = property_get_uint16_ptr;
2536 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2537 setter = property_set_uint16_ptr;
2540 return object_property_add(obj, name, "uint16",
2541 getter, setter, NULL, (void *)v);
2544 ObjectProperty *
2545 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2546 const uint16_t *v,
2547 ObjectPropertyFlags flags)
2549 ObjectPropertyAccessor *getter = NULL;
2550 ObjectPropertyAccessor *setter = NULL;
2552 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2553 getter = property_get_uint16_ptr;
2556 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2557 setter = property_set_uint16_ptr;
2560 return object_class_property_add(klass, name, "uint16",
2561 getter, setter, NULL, (void *)v);
2564 ObjectProperty *
2565 object_property_add_uint32_ptr(Object *obj, const char *name,
2566 const uint32_t *v,
2567 ObjectPropertyFlags flags)
2569 ObjectPropertyAccessor *getter = NULL;
2570 ObjectPropertyAccessor *setter = NULL;
2572 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2573 getter = property_get_uint32_ptr;
2576 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2577 setter = property_set_uint32_ptr;
2580 return object_property_add(obj, name, "uint32",
2581 getter, setter, NULL, (void *)v);
2584 ObjectProperty *
2585 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2586 const uint32_t *v,
2587 ObjectPropertyFlags flags)
2589 ObjectPropertyAccessor *getter = NULL;
2590 ObjectPropertyAccessor *setter = NULL;
2592 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2593 getter = property_get_uint32_ptr;
2596 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2597 setter = property_set_uint32_ptr;
2600 return object_class_property_add(klass, name, "uint32",
2601 getter, setter, NULL, (void *)v);
2604 ObjectProperty *
2605 object_property_add_uint64_ptr(Object *obj, const char *name,
2606 const uint64_t *v,
2607 ObjectPropertyFlags flags)
2609 ObjectPropertyAccessor *getter = NULL;
2610 ObjectPropertyAccessor *setter = NULL;
2612 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2613 getter = property_get_uint64_ptr;
2616 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2617 setter = property_set_uint64_ptr;
2620 return object_property_add(obj, name, "uint64",
2621 getter, setter, NULL, (void *)v);
2624 ObjectProperty *
2625 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2626 const uint64_t *v,
2627 ObjectPropertyFlags flags)
2629 ObjectPropertyAccessor *getter = NULL;
2630 ObjectPropertyAccessor *setter = NULL;
2632 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2633 getter = property_get_uint64_ptr;
2636 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2637 setter = property_set_uint64_ptr;
2640 return object_class_property_add(klass, name, "uint64",
2641 getter, setter, NULL, (void *)v);
2644 typedef struct {
2645 Object *target_obj;
2646 char *target_name;
2647 } AliasProperty;
2649 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2650 void *opaque, Error **errp)
2652 AliasProperty *prop = opaque;
2654 object_property_get(prop->target_obj, prop->target_name, v, errp);
2657 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2658 void *opaque, Error **errp)
2660 AliasProperty *prop = opaque;
2662 object_property_set(prop->target_obj, prop->target_name, v, errp);
2665 static Object *property_resolve_alias(Object *obj, void *opaque,
2666 const char *part)
2668 AliasProperty *prop = opaque;
2670 return object_resolve_path_component(prop->target_obj, prop->target_name);
2673 static void property_release_alias(Object *obj, const char *name, void *opaque)
2675 AliasProperty *prop = opaque;
2677 g_free(prop->target_name);
2678 g_free(prop);
2681 ObjectProperty *
2682 object_property_add_alias(Object *obj, const char *name,
2683 Object *target_obj, const char *target_name)
2685 AliasProperty *prop;
2686 ObjectProperty *op;
2687 ObjectProperty *target_prop;
2688 g_autofree char *prop_type = NULL;
2690 target_prop = object_property_find(target_obj, target_name,
2691 &error_abort);
2693 if (object_property_is_child(target_prop)) {
2694 prop_type = g_strdup_printf("link%s",
2695 target_prop->type + strlen("child"));
2696 } else {
2697 prop_type = g_strdup(target_prop->type);
2700 prop = g_malloc(sizeof(*prop));
2701 prop->target_obj = target_obj;
2702 prop->target_name = g_strdup(target_name);
2704 op = object_property_add(obj, name, prop_type,
2705 property_get_alias,
2706 property_set_alias,
2707 property_release_alias,
2708 prop);
2709 op->resolve = property_resolve_alias;
2710 if (target_prop->defval) {
2711 op->defval = qobject_ref(target_prop->defval);
2714 object_property_set_description(obj, op->name,
2715 target_prop->description);
2716 return op;
2719 void object_property_set_description(Object *obj, const char *name,
2720 const char *description)
2722 ObjectProperty *op;
2724 op = object_property_find(obj, name, &error_abort);
2725 g_free(op->description);
2726 op->description = g_strdup(description);
2729 void object_class_property_set_description(ObjectClass *klass,
2730 const char *name,
2731 const char *description)
2733 ObjectProperty *op;
2735 op = g_hash_table_lookup(klass->properties, name);
2736 g_free(op->description);
2737 op->description = g_strdup(description);
2740 static void object_class_init(ObjectClass *klass, void *data)
2742 object_class_property_add_str(klass, "type", object_get_type,
2743 NULL);
2746 static void register_types(void)
2748 static TypeInfo interface_info = {
2749 .name = TYPE_INTERFACE,
2750 .class_size = sizeof(InterfaceClass),
2751 .abstract = true,
2754 static TypeInfo object_info = {
2755 .name = TYPE_OBJECT,
2756 .instance_size = sizeof(Object),
2757 .class_init = object_class_init,
2758 .abstract = true,
2761 type_interface = type_register_internal(&interface_info);
2762 type_register_internal(&object_info);
2765 type_init(register_types)