tcg/loongarch64: Simplify tcg_out_dup_vec
[qemu/armbru.git] / qom / object.c
blob157a45c5f8b2ab3d7117e6c3424fe6a036135658
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 "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/qjson.h"
27 #include "trace.h"
29 /* TODO: replace QObject with a simpler visitor to avoid a dependency
30 * of the QOM core on QObject? */
31 #include "qom/qom-qobject.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qlist.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;
43 struct InterfaceImpl
45 const char *typename;
48 struct TypeImpl
50 const char *name;
52 size_t class_size;
54 size_t instance_size;
55 size_t instance_align;
57 void (*class_init)(ObjectClass *klass, void *data);
58 void (*class_base_init)(ObjectClass *klass, void *data);
60 void *class_data;
62 void (*instance_init)(Object *obj);
63 void (*instance_post_init)(Object *obj);
64 void (*instance_finalize)(Object *obj);
66 bool abstract;
68 const char *parent;
69 TypeImpl *parent_type;
71 ObjectClass *class;
73 int num_interfaces;
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);
87 return type_table;
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));
106 int i;
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);
112 abort();
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;
137 return ti;
140 static bool type_name_is_valid(const char *name)
142 const int slen = strlen(name);
143 int plen;
145 g_assert(slen > 1);
148 * Ideally, the name should start with a letter - however, we've got
149 * too many names starting with a digit already, so allow digits here,
150 * too (except '0' which is not used yet)
152 if (!g_ascii_isalnum(name[0]) || name[0] == '0') {
153 return false;
156 plen = strspn(name, "abcdefghijklmnopqrstuvwxyz"
157 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
158 "0123456789-_.");
160 return plen == slen;
163 static TypeImpl *type_register_internal(const TypeInfo *info)
165 TypeImpl *ti;
167 if (!type_name_is_valid(info->name)) {
168 fprintf(stderr, "Registering '%s' with illegal type name\n", info->name);
169 abort();
172 ti = type_new(info);
174 type_table_add(ti);
175 return ti;
178 TypeImpl *type_register(const TypeInfo *info)
180 assert(info->parent);
181 return type_register_internal(info);
184 TypeImpl *type_register_static(const TypeInfo *info)
186 return type_register(info);
189 void type_register_static_array(const TypeInfo *infos, int nr_infos)
191 int i;
193 for (i = 0; i < nr_infos; i++) {
194 type_register_static(&infos[i]);
198 static TypeImpl *type_get_by_name(const char *name)
200 if (name == NULL) {
201 return NULL;
204 return type_table_lookup(name);
207 static TypeImpl *type_get_parent(TypeImpl *type)
209 if (!type->parent_type && type->parent) {
210 type->parent_type = type_get_by_name(type->parent);
211 if (!type->parent_type) {
212 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
213 type->name, type->parent);
214 abort();
218 return type->parent_type;
221 static bool type_has_parent(TypeImpl *type)
223 return (type->parent != NULL);
226 static size_t type_class_get_size(TypeImpl *ti)
228 if (ti->class_size) {
229 return ti->class_size;
232 if (type_has_parent(ti)) {
233 return type_class_get_size(type_get_parent(ti));
236 return sizeof(ObjectClass);
239 static size_t type_object_get_size(TypeImpl *ti)
241 if (ti->instance_size) {
242 return ti->instance_size;
245 if (type_has_parent(ti)) {
246 return type_object_get_size(type_get_parent(ti));
249 return 0;
252 static size_t type_object_get_align(TypeImpl *ti)
254 if (ti->instance_align) {
255 return ti->instance_align;
258 if (type_has_parent(ti)) {
259 return type_object_get_align(type_get_parent(ti));
262 return 0;
265 size_t object_type_get_instance_size(const char *typename)
267 TypeImpl *type = type_get_by_name(typename);
269 g_assert(type != NULL);
270 return type_object_get_size(type);
273 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
275 assert(target_type);
277 /* Check if target_type is a direct ancestor of type */
278 while (type) {
279 if (type == target_type) {
280 return true;
283 type = type_get_parent(type);
286 return false;
289 static void type_initialize(TypeImpl *ti);
291 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
292 TypeImpl *parent_type)
294 InterfaceClass *new_iface;
295 TypeInfo info = { };
296 TypeImpl *iface_impl;
298 info.parent = parent_type->name;
299 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
300 info.abstract = true;
302 iface_impl = type_new(&info);
303 iface_impl->parent_type = parent_type;
304 type_initialize(iface_impl);
305 g_free((char *)info.name);
307 new_iface = (InterfaceClass *)iface_impl->class;
308 new_iface->concrete_class = ti->class;
309 new_iface->interface_type = interface_type;
311 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
314 static void object_property_free(gpointer data)
316 ObjectProperty *prop = data;
318 if (prop->defval) {
319 qobject_unref(prop->defval);
320 prop->defval = NULL;
322 g_free(prop->name);
323 g_free(prop->type);
324 g_free(prop->description);
325 g_free(prop);
328 static void type_initialize(TypeImpl *ti)
330 TypeImpl *parent;
332 if (ti->class) {
333 return;
336 ti->class_size = type_class_get_size(ti);
337 ti->instance_size = type_object_get_size(ti);
338 ti->instance_align = type_object_get_align(ti);
339 /* Any type with zero instance_size is implicitly abstract.
340 * This means interface types are all abstract.
342 if (ti->instance_size == 0) {
343 ti->abstract = true;
345 if (type_is_ancestor(ti, type_interface)) {
346 assert(ti->instance_size == 0);
347 assert(ti->abstract);
348 assert(!ti->instance_init);
349 assert(!ti->instance_post_init);
350 assert(!ti->instance_finalize);
351 assert(!ti->num_interfaces);
353 ti->class = g_malloc0(ti->class_size);
355 parent = type_get_parent(ti);
356 if (parent) {
357 type_initialize(parent);
358 GSList *e;
359 int i;
361 g_assert(parent->class_size <= ti->class_size);
362 g_assert(parent->instance_size <= ti->instance_size);
363 memcpy(ti->class, parent->class, parent->class_size);
364 ti->class->interfaces = NULL;
366 for (e = parent->class->interfaces; e; e = e->next) {
367 InterfaceClass *iface = e->data;
368 ObjectClass *klass = OBJECT_CLASS(iface);
370 type_initialize_interface(ti, iface->interface_type, klass->type);
373 for (i = 0; i < ti->num_interfaces; i++) {
374 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
375 if (!t) {
376 error_report("missing interface '%s' for object '%s'",
377 ti->interfaces[i].typename, parent->name);
378 abort();
380 for (e = ti->class->interfaces; e; e = e->next) {
381 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
383 if (type_is_ancestor(target_type, t)) {
384 break;
388 if (e) {
389 continue;
392 type_initialize_interface(ti, t, t);
396 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
397 object_property_free);
399 ti->class->type = ti;
401 while (parent) {
402 if (parent->class_base_init) {
403 parent->class_base_init(ti->class, ti->class_data);
405 parent = type_get_parent(parent);
408 if (ti->class_init) {
409 ti->class_init(ti->class, ti->class_data);
413 static void object_init_with_type(Object *obj, TypeImpl *ti)
415 if (type_has_parent(ti)) {
416 object_init_with_type(obj, type_get_parent(ti));
419 if (ti->instance_init) {
420 ti->instance_init(obj);
424 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
426 if (ti->instance_post_init) {
427 ti->instance_post_init(obj);
430 if (type_has_parent(ti)) {
431 object_post_init_with_type(obj, type_get_parent(ti));
435 bool object_apply_global_props(Object *obj, const GPtrArray *props,
436 Error **errp)
438 int i;
440 if (!props) {
441 return true;
444 for (i = 0; i < props->len; i++) {
445 GlobalProperty *p = g_ptr_array_index(props, i);
446 Error *err = NULL;
448 if (object_dynamic_cast(obj, p->driver) == NULL) {
449 continue;
451 if (p->optional && !object_property_find(obj, p->property)) {
452 continue;
454 p->used = true;
455 if (!object_property_parse(obj, p->property, p->value, &err)) {
456 error_prepend(&err, "can't apply global %s.%s=%s: ",
457 p->driver, p->property, p->value);
459 * If errp != NULL, propagate error and return.
460 * If errp == NULL, report a warning, but keep going
461 * with the remaining globals.
463 if (errp) {
464 error_propagate(errp, err);
465 return false;
466 } else {
467 warn_report_err(err);
472 return true;
476 * Global property defaults
477 * Slot 0: accelerator's global property defaults
478 * Slot 1: machine's global property defaults
479 * Slot 2: global properties from legacy command line option
480 * Each is a GPtrArray of of GlobalProperty.
481 * Applied in order, later entries override earlier ones.
483 static GPtrArray *object_compat_props[3];
486 * Retrieve @GPtrArray for global property defined with options
487 * other than "-global". These are generally used for syntactic
488 * sugar and legacy command line options.
490 void object_register_sugar_prop(const char *driver, const char *prop,
491 const char *value, bool optional)
493 GlobalProperty *g;
494 if (!object_compat_props[2]) {
495 object_compat_props[2] = g_ptr_array_new();
497 g = g_new0(GlobalProperty, 1);
498 g->driver = g_strdup(driver);
499 g->property = g_strdup(prop);
500 g->value = g_strdup(value);
501 g->optional = optional;
502 g_ptr_array_add(object_compat_props[2], g);
506 * Set machine's global property defaults to @compat_props.
507 * May be called at most once.
509 void object_set_machine_compat_props(GPtrArray *compat_props)
511 assert(!object_compat_props[1]);
512 object_compat_props[1] = compat_props;
516 * Set accelerator's global property defaults to @compat_props.
517 * May be called at most once.
519 void object_set_accelerator_compat_props(GPtrArray *compat_props)
521 assert(!object_compat_props[0]);
522 object_compat_props[0] = compat_props;
525 void object_apply_compat_props(Object *obj)
527 int i;
529 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
530 object_apply_global_props(obj, object_compat_props[i],
531 i == 2 ? &error_fatal : &error_abort);
535 static void object_class_property_init_all(Object *obj)
537 ObjectPropertyIterator iter;
538 ObjectProperty *prop;
540 object_class_property_iter_init(&iter, object_get_class(obj));
541 while ((prop = object_property_iter_next(&iter))) {
542 if (prop->init) {
543 prop->init(obj, prop);
548 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
550 type_initialize(type);
552 g_assert(type->instance_size >= sizeof(Object));
553 g_assert(type->abstract == false);
554 g_assert(size >= type->instance_size);
556 memset(obj, 0, type->instance_size);
557 obj->class = type->class;
558 object_ref(obj);
559 object_class_property_init_all(obj);
560 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
561 NULL, object_property_free);
562 object_init_with_type(obj, type);
563 object_post_init_with_type(obj, type);
566 void object_initialize(void *data, size_t size, const char *typename)
568 TypeImpl *type = type_get_by_name(typename);
570 #ifdef CONFIG_MODULES
571 if (!type) {
572 int rv = module_load_qom(typename, &error_fatal);
573 if (rv > 0) {
574 type = type_get_by_name(typename);
575 } else {
576 error_report("missing object type '%s'", typename);
577 exit(1);
580 #endif
581 if (!type) {
582 error_report("missing object type '%s'", typename);
583 abort();
586 object_initialize_with_type(data, size, type);
589 bool object_initialize_child_with_props(Object *parentobj,
590 const char *propname,
591 void *childobj, size_t size,
592 const char *type,
593 Error **errp, ...)
595 va_list vargs;
596 bool ok;
598 va_start(vargs, errp);
599 ok = object_initialize_child_with_propsv(parentobj, propname,
600 childobj, size, type, errp,
601 vargs);
602 va_end(vargs);
603 return ok;
606 bool object_initialize_child_with_propsv(Object *parentobj,
607 const char *propname,
608 void *childobj, size_t size,
609 const char *type,
610 Error **errp, va_list vargs)
612 bool ok = false;
613 Object *obj;
614 UserCreatable *uc;
616 object_initialize(childobj, size, type);
617 obj = OBJECT(childobj);
619 if (!object_set_propv(obj, errp, vargs)) {
620 goto out;
623 object_property_add_child(parentobj, propname, obj);
625 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
626 if (uc) {
627 if (!user_creatable_complete(uc, errp)) {
628 object_unparent(obj);
629 goto out;
633 ok = true;
635 out:
637 * We want @obj's reference to be 1 on success, 0 on failure.
638 * On success, it's 2: one taken by object_initialize(), and one
639 * by object_property_add_child().
640 * On failure in object_initialize() or earlier, it's 1.
641 * On failure afterwards, it's also 1: object_unparent() releases
642 * the reference taken by object_property_add_child().
644 object_unref(obj);
645 return ok;
648 void object_initialize_child_internal(Object *parent,
649 const char *propname,
650 void *child, size_t size,
651 const char *type)
653 object_initialize_child_with_props(parent, propname, child, size, type,
654 &error_abort, NULL);
657 static inline bool object_property_is_child(ObjectProperty *prop)
659 return strstart(prop->type, "child<", NULL);
662 static void object_property_del_all(Object *obj)
664 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
665 ObjectProperty *prop;
666 ObjectPropertyIterator iter;
667 bool released;
669 do {
670 released = false;
671 object_property_iter_init(&iter, obj);
672 while ((prop = object_property_iter_next(&iter)) != NULL) {
673 if (g_hash_table_add(done, prop)) {
674 if (prop->release) {
675 prop->release(obj, prop->name, prop->opaque);
676 released = true;
677 break;
681 } while (released);
683 g_hash_table_unref(obj->properties);
686 static void object_property_del_child(Object *obj, Object *child)
688 ObjectProperty *prop;
689 GHashTableIter iter;
690 gpointer key, value;
692 g_hash_table_iter_init(&iter, obj->properties);
693 while (g_hash_table_iter_next(&iter, &key, &value)) {
694 prop = value;
695 if (object_property_is_child(prop) && prop->opaque == child) {
696 if (prop->release) {
697 prop->release(obj, prop->name, prop->opaque);
698 prop->release = NULL;
700 break;
703 g_hash_table_iter_init(&iter, obj->properties);
704 while (g_hash_table_iter_next(&iter, &key, &value)) {
705 prop = value;
706 if (object_property_is_child(prop) && prop->opaque == child) {
707 g_hash_table_iter_remove(&iter);
708 break;
713 void object_unparent(Object *obj)
715 if (obj->parent) {
716 object_property_del_child(obj->parent, obj);
720 static void object_deinit(Object *obj, TypeImpl *type)
722 if (type->instance_finalize) {
723 type->instance_finalize(obj);
726 if (type_has_parent(type)) {
727 object_deinit(obj, type_get_parent(type));
731 static void object_finalize(void *data)
733 Object *obj = data;
734 TypeImpl *ti = obj->class->type;
736 object_property_del_all(obj);
737 object_deinit(obj, ti);
739 g_assert(obj->ref == 0);
740 g_assert(obj->parent == NULL);
741 if (obj->free) {
742 obj->free(obj);
746 /* Find the minimum alignment guaranteed by the system malloc. */
747 #if __STDC_VERSION__ >= 201112L
748 typedef max_align_t qemu_max_align_t;
749 #else
750 typedef union {
751 long l;
752 void *p;
753 double d;
754 long double ld;
755 } qemu_max_align_t;
756 #endif
758 static Object *object_new_with_type(Type type)
760 Object *obj;
761 size_t size, align;
762 void (*obj_free)(void *);
764 g_assert(type != NULL);
765 type_initialize(type);
767 size = type->instance_size;
768 align = type->instance_align;
771 * Do not use qemu_memalign unless required. Depending on the
772 * implementation, extra alignment implies extra overhead.
774 if (likely(align <= __alignof__(qemu_max_align_t))) {
775 obj = g_malloc(size);
776 obj_free = g_free;
777 } else {
778 obj = qemu_memalign(align, size);
779 obj_free = qemu_vfree;
782 object_initialize_with_type(obj, size, type);
783 obj->free = obj_free;
785 return obj;
788 Object *object_new_with_class(ObjectClass *klass)
790 return object_new_with_type(klass->type);
793 Object *object_new(const char *typename)
795 TypeImpl *ti = type_get_by_name(typename);
797 return object_new_with_type(ti);
801 Object *object_new_with_props(const char *typename,
802 Object *parent,
803 const char *id,
804 Error **errp,
805 ...)
807 va_list vargs;
808 Object *obj;
810 va_start(vargs, errp);
811 obj = object_new_with_propv(typename, parent, id, errp, vargs);
812 va_end(vargs);
814 return obj;
818 Object *object_new_with_propv(const char *typename,
819 Object *parent,
820 const char *id,
821 Error **errp,
822 va_list vargs)
824 Object *obj;
825 ObjectClass *klass;
826 UserCreatable *uc;
828 klass = object_class_by_name(typename);
829 if (!klass) {
830 error_setg(errp, "invalid object type: %s", typename);
831 return NULL;
834 if (object_class_is_abstract(klass)) {
835 error_setg(errp, "object type '%s' is abstract", typename);
836 return NULL;
838 obj = object_new_with_type(klass->type);
840 if (!object_set_propv(obj, errp, vargs)) {
841 goto error;
844 if (id != NULL) {
845 object_property_add_child(parent, id, obj);
848 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
849 if (uc) {
850 if (!user_creatable_complete(uc, errp)) {
851 if (id != NULL) {
852 object_unparent(obj);
854 goto error;
858 object_unref(obj);
859 return obj;
861 error:
862 object_unref(obj);
863 return NULL;
867 bool object_set_props(Object *obj,
868 Error **errp,
869 ...)
871 va_list vargs;
872 bool ret;
874 va_start(vargs, errp);
875 ret = object_set_propv(obj, errp, vargs);
876 va_end(vargs);
878 return ret;
882 bool object_set_propv(Object *obj,
883 Error **errp,
884 va_list vargs)
886 const char *propname;
888 propname = va_arg(vargs, char *);
889 while (propname != NULL) {
890 const char *value = va_arg(vargs, char *);
892 g_assert(value != NULL);
893 if (!object_property_parse(obj, propname, value, errp)) {
894 return false;
896 propname = va_arg(vargs, char *);
899 return true;
903 Object *object_dynamic_cast(Object *obj, const char *typename)
905 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
906 return obj;
909 return NULL;
912 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
913 const char *file, int line, const char *func)
915 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
916 typename, file, line, func);
918 #ifdef CONFIG_QOM_CAST_DEBUG
919 int i;
920 Object *inst;
922 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
923 if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) {
924 goto out;
928 inst = object_dynamic_cast(obj, typename);
930 if (!inst && obj) {
931 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
932 file, line, func, obj, typename);
933 abort();
936 assert(obj == inst);
938 if (obj && obj == inst) {
939 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
940 qatomic_set(&obj->class->object_cast_cache[i - 1],
941 qatomic_read(&obj->class->object_cast_cache[i]));
943 qatomic_set(&obj->class->object_cast_cache[i - 1], typename);
946 out:
947 #endif
948 return obj;
951 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
952 const char *typename)
954 ObjectClass *ret = NULL;
955 TypeImpl *target_type;
956 TypeImpl *type;
958 if (!class) {
959 return NULL;
962 /* A simple fast path that can trigger a lot for leaf classes. */
963 type = class->type;
964 if (type->name == typename) {
965 return class;
968 target_type = type_get_by_name(typename);
969 if (!target_type) {
970 /* target class type unknown, so fail the cast */
971 return NULL;
974 if (type->class->interfaces &&
975 type_is_ancestor(target_type, type_interface)) {
976 int found = 0;
977 GSList *i;
979 for (i = class->interfaces; i; i = i->next) {
980 ObjectClass *target_class = i->data;
982 if (type_is_ancestor(target_class->type, target_type)) {
983 ret = target_class;
984 found++;
988 /* The match was ambiguous, don't allow a cast */
989 if (found > 1) {
990 ret = NULL;
992 } else if (type_is_ancestor(type, target_type)) {
993 ret = class;
996 return ret;
999 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
1000 const char *typename,
1001 const char *file, int line,
1002 const char *func)
1004 ObjectClass *ret;
1006 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
1007 typename, file, line, func);
1009 #ifdef CONFIG_QOM_CAST_DEBUG
1010 int i;
1012 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
1013 if (qatomic_read(&class->class_cast_cache[i]) == typename) {
1014 ret = class;
1015 goto out;
1018 #else
1019 if (!class || !class->interfaces) {
1020 return class;
1022 #endif
1024 ret = object_class_dynamic_cast(class, typename);
1025 if (!ret && class) {
1026 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
1027 file, line, func, class, typename);
1028 abort();
1031 #ifdef CONFIG_QOM_CAST_DEBUG
1032 if (class && ret == class) {
1033 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
1034 qatomic_set(&class->class_cast_cache[i - 1],
1035 qatomic_read(&class->class_cast_cache[i]));
1037 qatomic_set(&class->class_cast_cache[i - 1], typename);
1039 out:
1040 #endif
1041 return ret;
1044 const char *object_get_typename(const Object *obj)
1046 return obj->class->type->name;
1049 ObjectClass *object_get_class(Object *obj)
1051 return obj->class;
1054 bool object_class_is_abstract(ObjectClass *klass)
1056 return klass->type->abstract;
1059 const char *object_class_get_name(ObjectClass *klass)
1061 return klass->type->name;
1064 ObjectClass *object_class_by_name(const char *typename)
1066 TypeImpl *type = type_get_by_name(typename);
1068 if (!type) {
1069 return NULL;
1072 type_initialize(type);
1074 return type->class;
1077 ObjectClass *module_object_class_by_name(const char *typename)
1079 ObjectClass *oc;
1081 oc = object_class_by_name(typename);
1082 #ifdef CONFIG_MODULES
1083 if (!oc) {
1084 Error *local_err = NULL;
1085 int rv = module_load_qom(typename, &local_err);
1086 if (rv > 0) {
1087 oc = object_class_by_name(typename);
1088 } else if (rv < 0) {
1089 error_report_err(local_err);
1092 #endif
1093 return oc;
1096 ObjectClass *object_class_get_parent(ObjectClass *class)
1098 TypeImpl *type = type_get_parent(class->type);
1100 if (!type) {
1101 return NULL;
1104 type_initialize(type);
1106 return type->class;
1109 typedef struct OCFData
1111 void (*fn)(ObjectClass *klass, void *opaque);
1112 const char *implements_type;
1113 bool include_abstract;
1114 void *opaque;
1115 } OCFData;
1117 static void object_class_foreach_tramp(gpointer key, gpointer value,
1118 gpointer opaque)
1120 OCFData *data = opaque;
1121 TypeImpl *type = value;
1122 ObjectClass *k;
1124 type_initialize(type);
1125 k = type->class;
1127 if (!data->include_abstract && type->abstract) {
1128 return;
1131 if (data->implements_type &&
1132 !object_class_dynamic_cast(k, data->implements_type)) {
1133 return;
1136 data->fn(k, data->opaque);
1139 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1140 const char *implements_type, bool include_abstract,
1141 void *opaque)
1143 OCFData data = { fn, implements_type, include_abstract, opaque };
1145 enumerating_types = true;
1146 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1147 enumerating_types = false;
1150 static int do_object_child_foreach(Object *obj,
1151 int (*fn)(Object *child, void *opaque),
1152 void *opaque, bool recurse)
1154 GHashTableIter iter;
1155 ObjectProperty *prop;
1156 int ret = 0;
1158 g_hash_table_iter_init(&iter, obj->properties);
1159 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1160 if (object_property_is_child(prop)) {
1161 Object *child = prop->opaque;
1163 ret = fn(child, opaque);
1164 if (ret != 0) {
1165 break;
1167 if (recurse) {
1168 ret = do_object_child_foreach(child, fn, opaque, true);
1169 if (ret != 0) {
1170 break;
1175 return ret;
1178 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1179 void *opaque)
1181 return do_object_child_foreach(obj, fn, opaque, false);
1184 int object_child_foreach_recursive(Object *obj,
1185 int (*fn)(Object *child, void *opaque),
1186 void *opaque)
1188 return do_object_child_foreach(obj, fn, opaque, true);
1191 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1193 GSList **list = opaque;
1195 *list = g_slist_prepend(*list, klass);
1198 GSList *object_class_get_list(const char *implements_type,
1199 bool include_abstract)
1201 GSList *list = NULL;
1203 object_class_foreach(object_class_get_list_tramp,
1204 implements_type, include_abstract, &list);
1205 return list;
1208 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1210 return strcasecmp(object_class_get_name((ObjectClass *)a),
1211 object_class_get_name((ObjectClass *)b));
1214 GSList *object_class_get_list_sorted(const char *implements_type,
1215 bool include_abstract)
1217 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1218 object_class_cmp);
1221 Object *object_ref(void *objptr)
1223 Object *obj = OBJECT(objptr);
1224 uint32_t ref;
1226 if (!obj) {
1227 return NULL;
1229 ref = qatomic_fetch_inc(&obj->ref);
1230 /* Assert waaay before the integer overflows */
1231 g_assert(ref < INT_MAX);
1232 return obj;
1235 void object_unref(void *objptr)
1237 Object *obj = OBJECT(objptr);
1238 if (!obj) {
1239 return;
1241 g_assert(obj->ref > 0);
1243 /* parent always holds a reference to its children */
1244 if (qatomic_fetch_dec(&obj->ref) == 1) {
1245 object_finalize(obj);
1249 ObjectProperty *
1250 object_property_try_add(Object *obj, const char *name, const char *type,
1251 ObjectPropertyAccessor *get,
1252 ObjectPropertyAccessor *set,
1253 ObjectPropertyRelease *release,
1254 void *opaque, Error **errp)
1256 ObjectProperty *prop;
1257 size_t name_len = strlen(name);
1259 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1260 int i;
1261 ObjectProperty *ret = NULL;
1262 char *name_no_array = g_strdup(name);
1264 name_no_array[name_len - 3] = '\0';
1265 for (i = 0; i < INT16_MAX; ++i) {
1266 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1268 ret = object_property_try_add(obj, full_name, type, get, set,
1269 release, opaque, NULL);
1270 g_free(full_name);
1271 if (ret) {
1272 break;
1275 g_free(name_no_array);
1276 assert(ret);
1277 return ret;
1280 if (object_property_find(obj, name) != NULL) {
1281 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1282 name, object_get_typename(obj));
1283 return NULL;
1286 prop = g_malloc0(sizeof(*prop));
1288 prop->name = g_strdup(name);
1289 prop->type = g_strdup(type);
1291 prop->get = get;
1292 prop->set = set;
1293 prop->release = release;
1294 prop->opaque = opaque;
1296 g_hash_table_insert(obj->properties, prop->name, prop);
1297 return prop;
1300 ObjectProperty *
1301 object_property_add(Object *obj, const char *name, const char *type,
1302 ObjectPropertyAccessor *get,
1303 ObjectPropertyAccessor *set,
1304 ObjectPropertyRelease *release,
1305 void *opaque)
1307 return object_property_try_add(obj, name, type, get, set, release,
1308 opaque, &error_abort);
1311 ObjectProperty *
1312 object_class_property_add(ObjectClass *klass,
1313 const char *name,
1314 const char *type,
1315 ObjectPropertyAccessor *get,
1316 ObjectPropertyAccessor *set,
1317 ObjectPropertyRelease *release,
1318 void *opaque)
1320 ObjectProperty *prop;
1322 assert(!object_class_property_find(klass, name));
1324 prop = g_malloc0(sizeof(*prop));
1326 prop->name = g_strdup(name);
1327 prop->type = g_strdup(type);
1329 prop->get = get;
1330 prop->set = set;
1331 prop->release = release;
1332 prop->opaque = opaque;
1334 g_hash_table_insert(klass->properties, prop->name, prop);
1336 return prop;
1339 ObjectProperty *object_property_find(Object *obj, const char *name)
1341 ObjectProperty *prop;
1342 ObjectClass *klass = object_get_class(obj);
1344 prop = object_class_property_find(klass, name);
1345 if (prop) {
1346 return prop;
1349 return g_hash_table_lookup(obj->properties, name);
1352 ObjectProperty *object_property_find_err(Object *obj, const char *name,
1353 Error **errp)
1355 ObjectProperty *prop = object_property_find(obj, name);
1356 if (!prop) {
1357 error_setg(errp, "Property '%s.%s' not found",
1358 object_get_typename(obj), name);
1360 return prop;
1363 void object_property_iter_init(ObjectPropertyIterator *iter,
1364 Object *obj)
1366 g_hash_table_iter_init(&iter->iter, obj->properties);
1367 iter->nextclass = object_get_class(obj);
1370 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1372 gpointer key, val;
1373 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1374 if (!iter->nextclass) {
1375 return NULL;
1377 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1378 iter->nextclass = object_class_get_parent(iter->nextclass);
1380 return val;
1383 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1384 ObjectClass *klass)
1386 g_hash_table_iter_init(&iter->iter, klass->properties);
1387 iter->nextclass = object_class_get_parent(klass);
1390 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name)
1392 ObjectClass *parent_klass;
1394 parent_klass = object_class_get_parent(klass);
1395 if (parent_klass) {
1396 ObjectProperty *prop =
1397 object_class_property_find(parent_klass, name);
1398 if (prop) {
1399 return prop;
1403 return g_hash_table_lookup(klass->properties, name);
1406 ObjectProperty *object_class_property_find_err(ObjectClass *klass,
1407 const char *name,
1408 Error **errp)
1410 ObjectProperty *prop = object_class_property_find(klass, name);
1411 if (!prop) {
1412 error_setg(errp, "Property '.%s' not found", name);
1414 return prop;
1418 void object_property_del(Object *obj, const char *name)
1420 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1422 if (prop->release) {
1423 prop->release(obj, name, prop->opaque);
1425 g_hash_table_remove(obj->properties, name);
1428 bool object_property_get(Object *obj, const char *name, Visitor *v,
1429 Error **errp)
1431 Error *err = NULL;
1432 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1434 if (prop == NULL) {
1435 return false;
1438 if (!prop->get) {
1439 error_setg(errp, "Property '%s.%s' is not readable",
1440 object_get_typename(obj), name);
1441 return false;
1443 prop->get(obj, v, name, prop->opaque, &err);
1444 error_propagate(errp, err);
1445 return !err;
1448 bool object_property_set(Object *obj, const char *name, Visitor *v,
1449 Error **errp)
1451 ERRP_GUARD();
1452 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1454 if (prop == NULL) {
1455 return false;
1458 if (!prop->set) {
1459 error_setg(errp, "Property '%s.%s' is not writable",
1460 object_get_typename(obj), name);
1461 return false;
1463 prop->set(obj, v, name, prop->opaque, errp);
1464 return !*errp;
1467 bool object_property_set_str(Object *obj, const char *name,
1468 const char *value, Error **errp)
1470 QString *qstr = qstring_from_str(value);
1471 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
1473 qobject_unref(qstr);
1474 return ok;
1477 char *object_property_get_str(Object *obj, const char *name,
1478 Error **errp)
1480 QObject *ret = object_property_get_qobject(obj, name, errp);
1481 QString *qstring;
1482 char *retval;
1484 if (!ret) {
1485 return NULL;
1487 qstring = qobject_to(QString, ret);
1488 if (!qstring) {
1489 error_setg(errp, "Invalid parameter type for '%s', expected: string",
1490 name);
1491 retval = NULL;
1492 } else {
1493 retval = g_strdup(qstring_get_str(qstring));
1496 qobject_unref(ret);
1497 return retval;
1500 bool object_property_set_link(Object *obj, const char *name,
1501 Object *value, Error **errp)
1503 g_autofree char *path = NULL;
1505 if (value) {
1506 path = object_get_canonical_path(value);
1508 return object_property_set_str(obj, name, path ?: "", errp);
1511 Object *object_property_get_link(Object *obj, const char *name,
1512 Error **errp)
1514 char *str = object_property_get_str(obj, name, errp);
1515 Object *target = NULL;
1517 if (str && *str) {
1518 target = object_resolve_path(str, NULL);
1519 if (!target) {
1520 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1521 "Device '%s' not found", str);
1525 g_free(str);
1526 return target;
1529 bool object_property_set_bool(Object *obj, const char *name,
1530 bool value, Error **errp)
1532 QBool *qbool = qbool_from_bool(value);
1533 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
1535 qobject_unref(qbool);
1536 return ok;
1539 bool object_property_get_bool(Object *obj, const char *name,
1540 Error **errp)
1542 QObject *ret = object_property_get_qobject(obj, name, errp);
1543 QBool *qbool;
1544 bool retval;
1546 if (!ret) {
1547 return false;
1549 qbool = qobject_to(QBool, ret);
1550 if (!qbool) {
1551 error_setg(errp, "Invalid parameter type for '%s', expected: boolean",
1552 name);
1553 retval = false;
1554 } else {
1555 retval = qbool_get_bool(qbool);
1558 qobject_unref(ret);
1559 return retval;
1562 bool object_property_set_int(Object *obj, const char *name,
1563 int64_t value, Error **errp)
1565 QNum *qnum = qnum_from_int(value);
1566 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1568 qobject_unref(qnum);
1569 return ok;
1572 int64_t object_property_get_int(Object *obj, const char *name,
1573 Error **errp)
1575 QObject *ret = object_property_get_qobject(obj, name, errp);
1576 QNum *qnum;
1577 int64_t retval;
1579 if (!ret) {
1580 return -1;
1583 qnum = qobject_to(QNum, ret);
1584 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1585 error_setg(errp, "Invalid parameter type for '%s', expected: int",
1586 name);
1587 retval = -1;
1590 qobject_unref(ret);
1591 return retval;
1594 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1596 Visitor *v = qobject_input_visitor_new(prop->defval);
1598 assert(prop->set != NULL);
1599 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1601 visit_free(v);
1604 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1606 assert(!prop->defval);
1607 assert(!prop->init);
1609 prop->defval = defval;
1610 prop->init = object_property_init_defval;
1613 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1615 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1618 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1620 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1623 void object_property_set_default_list(ObjectProperty *prop)
1625 object_property_set_default(prop, QOBJECT(qlist_new()));
1628 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1630 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1633 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1635 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1638 bool object_property_set_uint(Object *obj, const char *name,
1639 uint64_t value, Error **errp)
1641 QNum *qnum = qnum_from_uint(value);
1642 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1644 qobject_unref(qnum);
1645 return ok;
1648 uint64_t object_property_get_uint(Object *obj, const char *name,
1649 Error **errp)
1651 QObject *ret = object_property_get_qobject(obj, name, errp);
1652 QNum *qnum;
1653 uint64_t retval;
1655 if (!ret) {
1656 return 0;
1658 qnum = qobject_to(QNum, ret);
1659 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1660 error_setg(errp, "Invalid parameter type for '%s', expected: uint",
1661 name);
1662 retval = 0;
1665 qobject_unref(ret);
1666 return retval;
1669 typedef struct EnumProperty {
1670 const QEnumLookup *lookup;
1671 int (*get)(Object *, Error **);
1672 void (*set)(Object *, int, Error **);
1673 } EnumProperty;
1675 int object_property_get_enum(Object *obj, const char *name,
1676 const char *typename, Error **errp)
1678 char *str;
1679 int ret;
1680 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1681 EnumProperty *enumprop;
1683 if (prop == NULL) {
1684 return -1;
1687 if (!g_str_equal(prop->type, typename)) {
1688 error_setg(errp, "Property %s on %s is not '%s' enum type",
1689 name, object_class_get_name(
1690 object_get_class(obj)), typename);
1691 return -1;
1694 enumprop = prop->opaque;
1696 str = object_property_get_str(obj, name, errp);
1697 if (!str) {
1698 return -1;
1701 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1702 g_free(str);
1704 return ret;
1707 bool object_property_parse(Object *obj, const char *name,
1708 const char *string, Error **errp)
1710 Visitor *v = string_input_visitor_new(string);
1711 bool ok = object_property_set(obj, name, v, errp);
1713 visit_free(v);
1714 return ok;
1717 char *object_property_print(Object *obj, const char *name, bool human,
1718 Error **errp)
1720 Visitor *v;
1721 char *string = NULL;
1723 v = string_output_visitor_new(human, &string);
1724 if (!object_property_get(obj, name, v, errp)) {
1725 goto out;
1728 visit_complete(v, &string);
1730 out:
1731 visit_free(v);
1732 return string;
1735 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1737 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1738 if (prop == NULL) {
1739 return NULL;
1742 return prop->type;
1745 Object *object_get_root(void)
1747 static Object *root;
1749 if (!root) {
1750 root = object_new("container");
1753 return root;
1756 Object *object_get_objects_root(void)
1758 return container_get(object_get_root(), "/objects");
1761 Object *object_get_internal_root(void)
1763 static Object *internal_root;
1765 if (!internal_root) {
1766 internal_root = object_new("container");
1769 return internal_root;
1772 static void object_get_child_property(Object *obj, Visitor *v,
1773 const char *name, void *opaque,
1774 Error **errp)
1776 Object *child = opaque;
1777 char *path;
1779 path = object_get_canonical_path(child);
1780 visit_type_str(v, name, &path, errp);
1781 g_free(path);
1784 static Object *object_resolve_child_property(Object *parent, void *opaque,
1785 const char *part)
1787 return opaque;
1790 static void object_finalize_child_property(Object *obj, const char *name,
1791 void *opaque)
1793 Object *child = opaque;
1795 if (child->class->unparent) {
1796 (child->class->unparent)(child);
1798 child->parent = NULL;
1799 object_unref(child);
1802 ObjectProperty *
1803 object_property_try_add_child(Object *obj, const char *name,
1804 Object *child, Error **errp)
1806 g_autofree char *type = NULL;
1807 ObjectProperty *op;
1809 assert(!child->parent);
1811 type = g_strdup_printf("child<%s>", object_get_typename(child));
1813 op = object_property_try_add(obj, name, type, object_get_child_property,
1814 NULL, object_finalize_child_property,
1815 child, errp);
1816 if (!op) {
1817 return NULL;
1819 op->resolve = object_resolve_child_property;
1820 object_ref(child);
1821 child->parent = obj;
1822 return op;
1825 ObjectProperty *
1826 object_property_add_child(Object *obj, const char *name,
1827 Object *child)
1829 return object_property_try_add_child(obj, name, child, &error_abort);
1832 void object_property_allow_set_link(const Object *obj, const char *name,
1833 Object *val, Error **errp)
1835 /* Allow the link to be set, always */
1838 typedef struct {
1839 union {
1840 Object **targetp;
1841 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1842 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1844 void (*check)(const Object *, const char *, Object *, Error **);
1845 ObjectPropertyLinkFlags flags;
1846 } LinkProperty;
1848 static Object **
1849 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1851 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1852 return &lprop->target;
1853 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1854 return (void *)obj + lprop->offset;
1855 } else {
1856 return lprop->targetp;
1860 static void object_get_link_property(Object *obj, Visitor *v,
1861 const char *name, void *opaque,
1862 Error **errp)
1864 LinkProperty *lprop = opaque;
1865 Object **targetp = object_link_get_targetp(obj, lprop);
1866 char *path;
1868 if (*targetp) {
1869 path = object_get_canonical_path(*targetp);
1870 visit_type_str(v, name, &path, errp);
1871 g_free(path);
1872 } else {
1873 path = (char *)"";
1874 visit_type_str(v, name, &path, errp);
1879 * object_resolve_link:
1881 * Lookup an object and ensure its type matches the link property type. This
1882 * is similar to object_resolve_path() except type verification against the
1883 * link property is performed.
1885 * Returns: The matched object or NULL on path lookup failures.
1887 static Object *object_resolve_link(Object *obj, const char *name,
1888 const char *path, Error **errp)
1890 const char *type;
1891 char *target_type;
1892 bool ambiguous = false;
1893 Object *target;
1895 /* Go from link<FOO> to FOO. */
1896 type = object_property_get_type(obj, name, NULL);
1897 target_type = g_strndup(&type[5], strlen(type) - 6);
1898 target = object_resolve_path_type(path, target_type, &ambiguous);
1900 if (ambiguous) {
1901 error_setg(errp, "Path '%s' does not uniquely identify an object",
1902 path);
1903 } else if (!target) {
1904 target = object_resolve_path(path, &ambiguous);
1905 if (target || ambiguous) {
1906 error_setg(errp, "Invalid parameter type for '%s', expected: %s",
1907 name, target_type);
1908 } else {
1909 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1910 "Device '%s' not found", path);
1912 target = NULL;
1914 g_free(target_type);
1916 return target;
1919 static void object_set_link_property(Object *obj, Visitor *v,
1920 const char *name, void *opaque,
1921 Error **errp)
1923 Error *local_err = NULL;
1924 LinkProperty *prop = opaque;
1925 Object **targetp = object_link_get_targetp(obj, prop);
1926 Object *old_target = *targetp;
1927 Object *new_target;
1928 char *path = NULL;
1930 if (!visit_type_str(v, name, &path, errp)) {
1931 return;
1934 if (*path) {
1935 new_target = object_resolve_link(obj, name, path, errp);
1936 if (!new_target) {
1937 g_free(path);
1938 return;
1940 } else {
1941 new_target = NULL;
1944 g_free(path);
1946 prop->check(obj, name, new_target, &local_err);
1947 if (local_err) {
1948 error_propagate(errp, local_err);
1949 return;
1952 *targetp = new_target;
1953 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1954 object_ref(new_target);
1955 object_unref(old_target);
1959 static Object *object_resolve_link_property(Object *parent, void *opaque,
1960 const char *part)
1962 LinkProperty *lprop = opaque;
1964 return *object_link_get_targetp(parent, lprop);
1967 static void object_release_link_property(Object *obj, const char *name,
1968 void *opaque)
1970 LinkProperty *prop = opaque;
1971 Object **targetp = object_link_get_targetp(obj, prop);
1973 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1974 object_unref(*targetp);
1976 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1977 g_free(prop);
1981 static ObjectProperty *
1982 object_add_link_prop(Object *obj, const char *name,
1983 const char *type, void *ptr,
1984 void (*check)(const Object *, const char *,
1985 Object *, Error **),
1986 ObjectPropertyLinkFlags flags)
1988 LinkProperty *prop = g_malloc(sizeof(*prop));
1989 g_autofree char *full_type = NULL;
1990 ObjectProperty *op;
1992 if (flags & OBJ_PROP_LINK_DIRECT) {
1993 prop->target = ptr;
1994 } else {
1995 prop->targetp = ptr;
1997 prop->check = check;
1998 prop->flags = flags;
2000 full_type = g_strdup_printf("link<%s>", type);
2002 op = object_property_add(obj, name, full_type,
2003 object_get_link_property,
2004 check ? object_set_link_property : NULL,
2005 object_release_link_property,
2006 prop);
2007 op->resolve = object_resolve_link_property;
2008 return op;
2011 ObjectProperty *
2012 object_property_add_link(Object *obj, const char *name,
2013 const char *type, Object **targetp,
2014 void (*check)(const Object *, const char *,
2015 Object *, Error **),
2016 ObjectPropertyLinkFlags flags)
2018 return object_add_link_prop(obj, name, type, targetp, check, flags);
2021 ObjectProperty *
2022 object_class_property_add_link(ObjectClass *oc,
2023 const char *name,
2024 const char *type, ptrdiff_t offset,
2025 void (*check)(const Object *obj, const char *name,
2026 Object *val, Error **errp),
2027 ObjectPropertyLinkFlags flags)
2029 LinkProperty *prop = g_new0(LinkProperty, 1);
2030 char *full_type;
2031 ObjectProperty *op;
2033 prop->offset = offset;
2034 prop->check = check;
2035 prop->flags = flags | OBJ_PROP_LINK_CLASS;
2037 full_type = g_strdup_printf("link<%s>", type);
2039 op = object_class_property_add(oc, name, full_type,
2040 object_get_link_property,
2041 check ? object_set_link_property : NULL,
2042 object_release_link_property,
2043 prop);
2045 op->resolve = object_resolve_link_property;
2047 g_free(full_type);
2048 return op;
2051 ObjectProperty *
2052 object_property_add_const_link(Object *obj, const char *name,
2053 Object *target)
2055 return object_add_link_prop(obj, name,
2056 object_get_typename(target), target,
2057 NULL, OBJ_PROP_LINK_DIRECT);
2060 const char *object_get_canonical_path_component(const Object *obj)
2062 ObjectProperty *prop = NULL;
2063 GHashTableIter iter;
2065 if (obj->parent == NULL) {
2066 return NULL;
2069 g_hash_table_iter_init(&iter, obj->parent->properties);
2070 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2071 if (!object_property_is_child(prop)) {
2072 continue;
2075 if (prop->opaque == obj) {
2076 return prop->name;
2080 /* obj had a parent but was not a child, should never happen */
2081 g_assert_not_reached();
2082 return NULL;
2085 char *object_get_canonical_path(const Object *obj)
2087 Object *root = object_get_root();
2088 char *newpath, *path = NULL;
2090 if (obj == root) {
2091 return g_strdup("/");
2094 do {
2095 const char *component = object_get_canonical_path_component(obj);
2097 if (!component) {
2098 /* A canonical path must be complete, so discard what was
2099 * collected so far.
2101 g_free(path);
2102 return NULL;
2105 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
2106 g_free(path);
2107 path = newpath;
2108 obj = obj->parent;
2109 } while (obj != root);
2111 return path;
2114 Object *object_resolve_path_component(Object *parent, const char *part)
2116 ObjectProperty *prop = object_property_find(parent, part);
2117 if (prop == NULL) {
2118 return NULL;
2121 if (prop->resolve) {
2122 return prop->resolve(parent, prop->opaque, part);
2123 } else {
2124 return NULL;
2128 static Object *object_resolve_abs_path(Object *parent,
2129 char **parts,
2130 const char *typename)
2132 Object *child;
2134 if (*parts == NULL) {
2135 return object_dynamic_cast(parent, typename);
2138 if (strcmp(*parts, "") == 0) {
2139 return object_resolve_abs_path(parent, parts + 1, typename);
2142 child = object_resolve_path_component(parent, *parts);
2143 if (!child) {
2144 return NULL;
2147 return object_resolve_abs_path(child, parts + 1, typename);
2150 static Object *object_resolve_partial_path(Object *parent,
2151 char **parts,
2152 const char *typename,
2153 bool *ambiguous)
2155 Object *obj;
2156 GHashTableIter iter;
2157 ObjectProperty *prop;
2159 obj = object_resolve_abs_path(parent, parts, typename);
2161 g_hash_table_iter_init(&iter, parent->properties);
2162 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2163 Object *found;
2165 if (!object_property_is_child(prop)) {
2166 continue;
2169 found = object_resolve_partial_path(prop->opaque, parts,
2170 typename, ambiguous);
2171 if (found) {
2172 if (obj) {
2173 *ambiguous = true;
2174 return NULL;
2176 obj = found;
2179 if (*ambiguous) {
2180 return NULL;
2184 return obj;
2187 Object *object_resolve_path_type(const char *path, const char *typename,
2188 bool *ambiguousp)
2190 Object *obj;
2191 char **parts;
2193 parts = g_strsplit(path, "/", 0);
2194 assert(parts);
2196 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2197 bool ambiguous = false;
2198 obj = object_resolve_partial_path(object_get_root(), parts,
2199 typename, &ambiguous);
2200 if (ambiguousp) {
2201 *ambiguousp = ambiguous;
2203 } else {
2204 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2207 g_strfreev(parts);
2209 return obj;
2212 Object *object_resolve_path(const char *path, bool *ambiguous)
2214 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2217 Object *object_resolve_path_at(Object *parent, const char *path)
2219 g_auto(GStrv) parts = g_strsplit(path, "/", 0);
2221 if (*path == '/') {
2222 return object_resolve_abs_path(object_get_root(), parts + 1,
2223 TYPE_OBJECT);
2225 return object_resolve_abs_path(parent, parts, TYPE_OBJECT);
2228 Object *object_resolve_type_unambiguous(const char *typename, Error **errp)
2230 bool ambig;
2231 Object *o = object_resolve_path_type("", typename, &ambig);
2233 if (ambig) {
2234 error_setg(errp, "More than one object of type %s", typename);
2235 return NULL;
2237 if (!o) {
2238 error_setg(errp, "No object found of type %s", typename);
2239 return NULL;
2241 return o;
2244 typedef struct StringProperty
2246 char *(*get)(Object *, Error **);
2247 void (*set)(Object *, const char *, Error **);
2248 } StringProperty;
2250 static void property_get_str(Object *obj, Visitor *v, const char *name,
2251 void *opaque, Error **errp)
2253 StringProperty *prop = opaque;
2254 char *value;
2255 Error *err = NULL;
2257 value = prop->get(obj, &err);
2258 if (err) {
2259 error_propagate(errp, err);
2260 return;
2263 visit_type_str(v, name, &value, errp);
2264 g_free(value);
2267 static void property_set_str(Object *obj, Visitor *v, const char *name,
2268 void *opaque, Error **errp)
2270 StringProperty *prop = opaque;
2271 char *value;
2273 if (!visit_type_str(v, name, &value, errp)) {
2274 return;
2277 prop->set(obj, value, errp);
2278 g_free(value);
2281 static void property_release_data(Object *obj, const char *name,
2282 void *opaque)
2284 g_free(opaque);
2287 ObjectProperty *
2288 object_property_add_str(Object *obj, const char *name,
2289 char *(*get)(Object *, Error **),
2290 void (*set)(Object *, const char *, Error **))
2292 StringProperty *prop = g_malloc0(sizeof(*prop));
2294 prop->get = get;
2295 prop->set = set;
2297 return object_property_add(obj, name, "string",
2298 get ? property_get_str : NULL,
2299 set ? property_set_str : NULL,
2300 property_release_data,
2301 prop);
2304 ObjectProperty *
2305 object_class_property_add_str(ObjectClass *klass, const char *name,
2306 char *(*get)(Object *, Error **),
2307 void (*set)(Object *, const char *,
2308 Error **))
2310 StringProperty *prop = g_malloc0(sizeof(*prop));
2312 prop->get = get;
2313 prop->set = set;
2315 return object_class_property_add(klass, name, "string",
2316 get ? property_get_str : NULL,
2317 set ? property_set_str : NULL,
2318 NULL,
2319 prop);
2322 typedef struct BoolProperty
2324 bool (*get)(Object *, Error **);
2325 void (*set)(Object *, bool, Error **);
2326 } BoolProperty;
2328 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2329 void *opaque, Error **errp)
2331 BoolProperty *prop = opaque;
2332 bool value;
2333 Error *err = NULL;
2335 value = prop->get(obj, &err);
2336 if (err) {
2337 error_propagate(errp, err);
2338 return;
2341 visit_type_bool(v, name, &value, errp);
2344 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2345 void *opaque, Error **errp)
2347 BoolProperty *prop = opaque;
2348 bool value;
2350 if (!visit_type_bool(v, name, &value, errp)) {
2351 return;
2354 prop->set(obj, value, errp);
2357 ObjectProperty *
2358 object_property_add_bool(Object *obj, const char *name,
2359 bool (*get)(Object *, Error **),
2360 void (*set)(Object *, bool, Error **))
2362 BoolProperty *prop = g_malloc0(sizeof(*prop));
2364 prop->get = get;
2365 prop->set = set;
2367 return object_property_add(obj, name, "bool",
2368 get ? property_get_bool : NULL,
2369 set ? property_set_bool : NULL,
2370 property_release_data,
2371 prop);
2374 ObjectProperty *
2375 object_class_property_add_bool(ObjectClass *klass, const char *name,
2376 bool (*get)(Object *, Error **),
2377 void (*set)(Object *, bool, Error **))
2379 BoolProperty *prop = g_malloc0(sizeof(*prop));
2381 prop->get = get;
2382 prop->set = set;
2384 return object_class_property_add(klass, name, "bool",
2385 get ? property_get_bool : NULL,
2386 set ? property_set_bool : NULL,
2387 NULL,
2388 prop);
2391 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2392 void *opaque, Error **errp)
2394 EnumProperty *prop = opaque;
2395 int value;
2396 Error *err = NULL;
2398 value = prop->get(obj, &err);
2399 if (err) {
2400 error_propagate(errp, err);
2401 return;
2404 visit_type_enum(v, name, &value, prop->lookup, errp);
2407 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2408 void *opaque, Error **errp)
2410 EnumProperty *prop = opaque;
2411 int value;
2413 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
2414 return;
2416 prop->set(obj, value, errp);
2419 ObjectProperty *
2420 object_property_add_enum(Object *obj, const char *name,
2421 const char *typename,
2422 const QEnumLookup *lookup,
2423 int (*get)(Object *, Error **),
2424 void (*set)(Object *, int, Error **))
2426 EnumProperty *prop = g_malloc(sizeof(*prop));
2428 prop->lookup = lookup;
2429 prop->get = get;
2430 prop->set = set;
2432 return object_property_add(obj, name, typename,
2433 get ? property_get_enum : NULL,
2434 set ? property_set_enum : NULL,
2435 property_release_data,
2436 prop);
2439 ObjectProperty *
2440 object_class_property_add_enum(ObjectClass *klass, const char *name,
2441 const char *typename,
2442 const QEnumLookup *lookup,
2443 int (*get)(Object *, Error **),
2444 void (*set)(Object *, int, Error **))
2446 EnumProperty *prop = g_malloc(sizeof(*prop));
2448 prop->lookup = lookup;
2449 prop->get = get;
2450 prop->set = set;
2452 return object_class_property_add(klass, name, typename,
2453 get ? property_get_enum : NULL,
2454 set ? property_set_enum : NULL,
2455 NULL,
2456 prop);
2459 typedef struct TMProperty {
2460 void (*get)(Object *, struct tm *, Error **);
2461 } TMProperty;
2463 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2464 void *opaque, Error **errp)
2466 TMProperty *prop = opaque;
2467 Error *err = NULL;
2468 struct tm value;
2470 prop->get(obj, &value, &err);
2471 if (err) {
2472 error_propagate(errp, err);
2473 return;
2476 if (!visit_start_struct(v, name, NULL, 0, errp)) {
2477 return;
2479 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
2480 goto out_end;
2482 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
2483 goto out_end;
2485 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
2486 goto out_end;
2488 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
2489 goto out_end;
2491 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
2492 goto out_end;
2494 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
2495 goto out_end;
2497 visit_check_struct(v, errp);
2498 out_end:
2499 visit_end_struct(v, NULL);
2502 ObjectProperty *
2503 object_property_add_tm(Object *obj, const char *name,
2504 void (*get)(Object *, struct tm *, Error **))
2506 TMProperty *prop = g_malloc0(sizeof(*prop));
2508 prop->get = get;
2510 return object_property_add(obj, name, "struct tm",
2511 get ? property_get_tm : NULL, NULL,
2512 property_release_data,
2513 prop);
2516 ObjectProperty *
2517 object_class_property_add_tm(ObjectClass *klass, const char *name,
2518 void (*get)(Object *, struct tm *, Error **))
2520 TMProperty *prop = g_malloc0(sizeof(*prop));
2522 prop->get = get;
2524 return object_class_property_add(klass, name, "struct tm",
2525 get ? property_get_tm : NULL,
2526 NULL, NULL, prop);
2529 static char *object_get_type(Object *obj, Error **errp)
2531 return g_strdup(object_get_typename(obj));
2534 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2535 void *opaque, Error **errp)
2537 uint8_t value = *(uint8_t *)opaque;
2538 visit_type_uint8(v, name, &value, errp);
2541 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2542 void *opaque, Error **errp)
2544 uint8_t *field = opaque;
2545 uint8_t value;
2547 if (!visit_type_uint8(v, name, &value, errp)) {
2548 return;
2551 *field = value;
2554 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2555 void *opaque, Error **errp)
2557 uint16_t value = *(uint16_t *)opaque;
2558 visit_type_uint16(v, name, &value, errp);
2561 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2562 void *opaque, Error **errp)
2564 uint16_t *field = opaque;
2565 uint16_t value;
2567 if (!visit_type_uint16(v, name, &value, errp)) {
2568 return;
2571 *field = value;
2574 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2575 void *opaque, Error **errp)
2577 uint32_t value = *(uint32_t *)opaque;
2578 visit_type_uint32(v, name, &value, errp);
2581 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2582 void *opaque, Error **errp)
2584 uint32_t *field = opaque;
2585 uint32_t value;
2587 if (!visit_type_uint32(v, name, &value, errp)) {
2588 return;
2591 *field = value;
2594 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2595 void *opaque, Error **errp)
2597 uint64_t value = *(uint64_t *)opaque;
2598 visit_type_uint64(v, name, &value, errp);
2601 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2602 void *opaque, Error **errp)
2604 uint64_t *field = opaque;
2605 uint64_t value;
2607 if (!visit_type_uint64(v, name, &value, errp)) {
2608 return;
2611 *field = value;
2614 ObjectProperty *
2615 object_property_add_uint8_ptr(Object *obj, const char *name,
2616 const uint8_t *v,
2617 ObjectPropertyFlags flags)
2619 ObjectPropertyAccessor *getter = NULL;
2620 ObjectPropertyAccessor *setter = NULL;
2622 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2623 getter = property_get_uint8_ptr;
2626 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2627 setter = property_set_uint8_ptr;
2630 return object_property_add(obj, name, "uint8",
2631 getter, setter, NULL, (void *)v);
2634 ObjectProperty *
2635 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2636 const uint8_t *v,
2637 ObjectPropertyFlags flags)
2639 ObjectPropertyAccessor *getter = NULL;
2640 ObjectPropertyAccessor *setter = NULL;
2642 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2643 getter = property_get_uint8_ptr;
2646 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2647 setter = property_set_uint8_ptr;
2650 return object_class_property_add(klass, name, "uint8",
2651 getter, setter, NULL, (void *)v);
2654 ObjectProperty *
2655 object_property_add_uint16_ptr(Object *obj, const char *name,
2656 const uint16_t *v,
2657 ObjectPropertyFlags flags)
2659 ObjectPropertyAccessor *getter = NULL;
2660 ObjectPropertyAccessor *setter = NULL;
2662 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2663 getter = property_get_uint16_ptr;
2666 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2667 setter = property_set_uint16_ptr;
2670 return object_property_add(obj, name, "uint16",
2671 getter, setter, NULL, (void *)v);
2674 ObjectProperty *
2675 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2676 const uint16_t *v,
2677 ObjectPropertyFlags flags)
2679 ObjectPropertyAccessor *getter = NULL;
2680 ObjectPropertyAccessor *setter = NULL;
2682 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2683 getter = property_get_uint16_ptr;
2686 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2687 setter = property_set_uint16_ptr;
2690 return object_class_property_add(klass, name, "uint16",
2691 getter, setter, NULL, (void *)v);
2694 ObjectProperty *
2695 object_property_add_uint32_ptr(Object *obj, const char *name,
2696 const uint32_t *v,
2697 ObjectPropertyFlags flags)
2699 ObjectPropertyAccessor *getter = NULL;
2700 ObjectPropertyAccessor *setter = NULL;
2702 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2703 getter = property_get_uint32_ptr;
2706 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2707 setter = property_set_uint32_ptr;
2710 return object_property_add(obj, name, "uint32",
2711 getter, setter, NULL, (void *)v);
2714 ObjectProperty *
2715 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2716 const uint32_t *v,
2717 ObjectPropertyFlags flags)
2719 ObjectPropertyAccessor *getter = NULL;
2720 ObjectPropertyAccessor *setter = NULL;
2722 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2723 getter = property_get_uint32_ptr;
2726 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2727 setter = property_set_uint32_ptr;
2730 return object_class_property_add(klass, name, "uint32",
2731 getter, setter, NULL, (void *)v);
2734 ObjectProperty *
2735 object_property_add_uint64_ptr(Object *obj, const char *name,
2736 const uint64_t *v,
2737 ObjectPropertyFlags flags)
2739 ObjectPropertyAccessor *getter = NULL;
2740 ObjectPropertyAccessor *setter = NULL;
2742 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2743 getter = property_get_uint64_ptr;
2746 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2747 setter = property_set_uint64_ptr;
2750 return object_property_add(obj, name, "uint64",
2751 getter, setter, NULL, (void *)v);
2754 ObjectProperty *
2755 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2756 const uint64_t *v,
2757 ObjectPropertyFlags flags)
2759 ObjectPropertyAccessor *getter = NULL;
2760 ObjectPropertyAccessor *setter = NULL;
2762 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2763 getter = property_get_uint64_ptr;
2766 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2767 setter = property_set_uint64_ptr;
2770 return object_class_property_add(klass, name, "uint64",
2771 getter, setter, NULL, (void *)v);
2774 typedef struct {
2775 Object *target_obj;
2776 char *target_name;
2777 } AliasProperty;
2779 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2780 void *opaque, Error **errp)
2782 AliasProperty *prop = opaque;
2783 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2785 object_property_get(prop->target_obj, prop->target_name, alias_v, errp);
2786 visit_free(alias_v);
2789 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2790 void *opaque, Error **errp)
2792 AliasProperty *prop = opaque;
2793 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2795 object_property_set(prop->target_obj, prop->target_name, alias_v, errp);
2796 visit_free(alias_v);
2799 static Object *property_resolve_alias(Object *obj, void *opaque,
2800 const char *part)
2802 AliasProperty *prop = opaque;
2804 return object_resolve_path_component(prop->target_obj, prop->target_name);
2807 static void property_release_alias(Object *obj, const char *name, void *opaque)
2809 AliasProperty *prop = opaque;
2811 g_free(prop->target_name);
2812 g_free(prop);
2815 ObjectProperty *
2816 object_property_add_alias(Object *obj, const char *name,
2817 Object *target_obj, const char *target_name)
2819 AliasProperty *prop;
2820 ObjectProperty *op;
2821 ObjectProperty *target_prop;
2822 g_autofree char *prop_type = NULL;
2824 target_prop = object_property_find_err(target_obj, target_name,
2825 &error_abort);
2827 if (object_property_is_child(target_prop)) {
2828 prop_type = g_strdup_printf("link%s",
2829 target_prop->type + strlen("child"));
2830 } else {
2831 prop_type = g_strdup(target_prop->type);
2834 prop = g_malloc(sizeof(*prop));
2835 prop->target_obj = target_obj;
2836 prop->target_name = g_strdup(target_name);
2838 op = object_property_add(obj, name, prop_type,
2839 property_get_alias,
2840 property_set_alias,
2841 property_release_alias,
2842 prop);
2843 op->resolve = property_resolve_alias;
2844 if (target_prop->defval) {
2845 op->defval = qobject_ref(target_prop->defval);
2848 object_property_set_description(obj, op->name,
2849 target_prop->description);
2850 return op;
2853 void object_property_set_description(Object *obj, const char *name,
2854 const char *description)
2856 ObjectProperty *op;
2858 op = object_property_find_err(obj, name, &error_abort);
2859 g_free(op->description);
2860 op->description = g_strdup(description);
2863 void object_class_property_set_description(ObjectClass *klass,
2864 const char *name,
2865 const char *description)
2867 ObjectProperty *op;
2869 op = g_hash_table_lookup(klass->properties, name);
2870 g_free(op->description);
2871 op->description = g_strdup(description);
2874 static void object_class_init(ObjectClass *klass, void *data)
2876 object_class_property_add_str(klass, "type", object_get_type,
2877 NULL);
2880 static void register_types(void)
2882 static const TypeInfo interface_info = {
2883 .name = TYPE_INTERFACE,
2884 .class_size = sizeof(InterfaceClass),
2885 .abstract = true,
2888 static const TypeInfo object_info = {
2889 .name = TYPE_OBJECT,
2890 .instance_size = sizeof(Object),
2891 .class_init = object_class_init,
2892 .abstract = true,
2895 type_interface = type_register_internal(&interface_info);
2896 type_register_internal(&object_info);
2899 type_init(register_types)