qom/object: Limit type names to alphanumerical and some few special characters
[qemu/armbru.git] / qom / object.c
blob654e1afaf2eb8e820459433783860a23e0b5713f
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/qerror.h"
27 #include "qapi/qmp/qjson.h"
28 #include "trace.h"
30 /* TODO: replace QObject with a simpler visitor to avoid a dependency
31 * of the QOM core on QObject? */
32 #include "qom/qom-qobject.h"
33 #include "qapi/qmp/qbool.h"
34 #include "qapi/qmp/qlist.h"
35 #include "qapi/qmp/qnum.h"
36 #include "qapi/qmp/qstring.h"
37 #include "qemu/error-report.h"
39 #define MAX_INTERFACES 32
41 typedef struct InterfaceImpl InterfaceImpl;
42 typedef struct TypeImpl TypeImpl;
44 struct InterfaceImpl
46 const char *typename;
49 struct TypeImpl
51 const char *name;
53 size_t class_size;
55 size_t instance_size;
56 size_t instance_align;
58 void (*class_init)(ObjectClass *klass, void *data);
59 void (*class_base_init)(ObjectClass *klass, void *data);
61 void *class_data;
63 void (*instance_init)(Object *obj);
64 void (*instance_post_init)(Object *obj);
65 void (*instance_finalize)(Object *obj);
67 bool abstract;
69 const char *parent;
70 TypeImpl *parent_type;
72 ObjectClass *class;
74 int num_interfaces;
75 InterfaceImpl interfaces[MAX_INTERFACES];
78 static Type type_interface;
80 static GHashTable *type_table_get(void)
82 static GHashTable *type_table;
84 if (type_table == NULL) {
85 type_table = g_hash_table_new(g_str_hash, g_str_equal);
88 return type_table;
91 static bool enumerating_types;
93 static void type_table_add(TypeImpl *ti)
95 assert(!enumerating_types);
96 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
99 static TypeImpl *type_table_lookup(const char *name)
101 return g_hash_table_lookup(type_table_get(), name);
104 static TypeImpl *type_new(const TypeInfo *info)
106 TypeImpl *ti = g_malloc0(sizeof(*ti));
107 int i;
109 g_assert(info->name != NULL);
111 if (type_table_lookup(info->name) != NULL) {
112 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
113 abort();
116 ti->name = g_strdup(info->name);
117 ti->parent = g_strdup(info->parent);
119 ti->class_size = info->class_size;
120 ti->instance_size = info->instance_size;
121 ti->instance_align = info->instance_align;
123 ti->class_init = info->class_init;
124 ti->class_base_init = info->class_base_init;
125 ti->class_data = info->class_data;
127 ti->instance_init = info->instance_init;
128 ti->instance_post_init = info->instance_post_init;
129 ti->instance_finalize = info->instance_finalize;
131 ti->abstract = info->abstract;
133 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
134 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
136 ti->num_interfaces = i;
138 return ti;
141 static bool type_name_is_valid(const char *name)
143 const int slen = strlen(name);
144 int plen;
146 g_assert(slen > 1);
149 * Ideally, the name should start with a letter - however, we've got
150 * too many names starting with a digit already, so allow digits here,
151 * too (except '0' which is not used yet)
153 if (!g_ascii_isalnum(name[0]) || name[0] == '0') {
154 return false;
157 plen = strspn(name, "abcdefghijklmnopqrstuvwxyz"
158 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
159 "0123456789-_.");
161 /* Allow some legacy names with '+' in it for compatibility reasons */
162 if (name[plen] == '+') {
163 if (plen == 6 && g_str_has_prefix(name, "power")) {
164 /* Allow "power5+" and "power7+" CPU names*/
165 return true;
167 if (plen >= 17 && g_str_has_prefix(name, "Sun-UltraSparc-I")) {
168 /* Allow "Sun-UltraSparc-IV+" and "Sun-UltraSparc-IIIi+" */
169 return true;
173 return plen == slen;
176 static TypeImpl *type_register_internal(const TypeInfo *info)
178 TypeImpl *ti;
180 if (!type_name_is_valid(info->name)) {
181 fprintf(stderr, "Registering '%s' with illegal type name\n", info->name);
182 abort();
185 ti = type_new(info);
187 type_table_add(ti);
188 return ti;
191 TypeImpl *type_register(const TypeInfo *info)
193 assert(info->parent);
194 return type_register_internal(info);
197 TypeImpl *type_register_static(const TypeInfo *info)
199 return type_register(info);
202 void type_register_static_array(const TypeInfo *infos, int nr_infos)
204 int i;
206 for (i = 0; i < nr_infos; i++) {
207 type_register_static(&infos[i]);
211 static TypeImpl *type_get_by_name(const char *name)
213 if (name == NULL) {
214 return NULL;
217 return type_table_lookup(name);
220 static TypeImpl *type_get_parent(TypeImpl *type)
222 if (!type->parent_type && type->parent) {
223 type->parent_type = type_get_by_name(type->parent);
224 if (!type->parent_type) {
225 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
226 type->name, type->parent);
227 abort();
231 return type->parent_type;
234 static bool type_has_parent(TypeImpl *type)
236 return (type->parent != NULL);
239 static size_t type_class_get_size(TypeImpl *ti)
241 if (ti->class_size) {
242 return ti->class_size;
245 if (type_has_parent(ti)) {
246 return type_class_get_size(type_get_parent(ti));
249 return sizeof(ObjectClass);
252 static size_t type_object_get_size(TypeImpl *ti)
254 if (ti->instance_size) {
255 return ti->instance_size;
258 if (type_has_parent(ti)) {
259 return type_object_get_size(type_get_parent(ti));
262 return 0;
265 static size_t type_object_get_align(TypeImpl *ti)
267 if (ti->instance_align) {
268 return ti->instance_align;
271 if (type_has_parent(ti)) {
272 return type_object_get_align(type_get_parent(ti));
275 return 0;
278 size_t object_type_get_instance_size(const char *typename)
280 TypeImpl *type = type_get_by_name(typename);
282 g_assert(type != NULL);
283 return type_object_get_size(type);
286 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
288 assert(target_type);
290 /* Check if target_type is a direct ancestor of type */
291 while (type) {
292 if (type == target_type) {
293 return true;
296 type = type_get_parent(type);
299 return false;
302 static void type_initialize(TypeImpl *ti);
304 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
305 TypeImpl *parent_type)
307 InterfaceClass *new_iface;
308 TypeInfo info = { };
309 TypeImpl *iface_impl;
311 info.parent = parent_type->name;
312 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
313 info.abstract = true;
315 iface_impl = type_new(&info);
316 iface_impl->parent_type = parent_type;
317 type_initialize(iface_impl);
318 g_free((char *)info.name);
320 new_iface = (InterfaceClass *)iface_impl->class;
321 new_iface->concrete_class = ti->class;
322 new_iface->interface_type = interface_type;
324 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
327 static void object_property_free(gpointer data)
329 ObjectProperty *prop = data;
331 if (prop->defval) {
332 qobject_unref(prop->defval);
333 prop->defval = NULL;
335 g_free(prop->name);
336 g_free(prop->type);
337 g_free(prop->description);
338 g_free(prop);
341 static void type_initialize(TypeImpl *ti)
343 TypeImpl *parent;
345 if (ti->class) {
346 return;
349 ti->class_size = type_class_get_size(ti);
350 ti->instance_size = type_object_get_size(ti);
351 ti->instance_align = type_object_get_align(ti);
352 /* Any type with zero instance_size is implicitly abstract.
353 * This means interface types are all abstract.
355 if (ti->instance_size == 0) {
356 ti->abstract = true;
358 if (type_is_ancestor(ti, type_interface)) {
359 assert(ti->instance_size == 0);
360 assert(ti->abstract);
361 assert(!ti->instance_init);
362 assert(!ti->instance_post_init);
363 assert(!ti->instance_finalize);
364 assert(!ti->num_interfaces);
366 ti->class = g_malloc0(ti->class_size);
368 parent = type_get_parent(ti);
369 if (parent) {
370 type_initialize(parent);
371 GSList *e;
372 int i;
374 g_assert(parent->class_size <= ti->class_size);
375 g_assert(parent->instance_size <= ti->instance_size);
376 memcpy(ti->class, parent->class, parent->class_size);
377 ti->class->interfaces = NULL;
379 for (e = parent->class->interfaces; e; e = e->next) {
380 InterfaceClass *iface = e->data;
381 ObjectClass *klass = OBJECT_CLASS(iface);
383 type_initialize_interface(ti, iface->interface_type, klass->type);
386 for (i = 0; i < ti->num_interfaces; i++) {
387 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
388 if (!t) {
389 error_report("missing interface '%s' for object '%s'",
390 ti->interfaces[i].typename, parent->name);
391 abort();
393 for (e = ti->class->interfaces; e; e = e->next) {
394 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
396 if (type_is_ancestor(target_type, t)) {
397 break;
401 if (e) {
402 continue;
405 type_initialize_interface(ti, t, t);
409 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
410 object_property_free);
412 ti->class->type = ti;
414 while (parent) {
415 if (parent->class_base_init) {
416 parent->class_base_init(ti->class, ti->class_data);
418 parent = type_get_parent(parent);
421 if (ti->class_init) {
422 ti->class_init(ti->class, ti->class_data);
426 static void object_init_with_type(Object *obj, TypeImpl *ti)
428 if (type_has_parent(ti)) {
429 object_init_with_type(obj, type_get_parent(ti));
432 if (ti->instance_init) {
433 ti->instance_init(obj);
437 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
439 if (ti->instance_post_init) {
440 ti->instance_post_init(obj);
443 if (type_has_parent(ti)) {
444 object_post_init_with_type(obj, type_get_parent(ti));
448 bool object_apply_global_props(Object *obj, const GPtrArray *props,
449 Error **errp)
451 int i;
453 if (!props) {
454 return true;
457 for (i = 0; i < props->len; i++) {
458 GlobalProperty *p = g_ptr_array_index(props, i);
459 Error *err = NULL;
461 if (object_dynamic_cast(obj, p->driver) == NULL) {
462 continue;
464 if (p->optional && !object_property_find(obj, p->property)) {
465 continue;
467 p->used = true;
468 if (!object_property_parse(obj, p->property, p->value, &err)) {
469 error_prepend(&err, "can't apply global %s.%s=%s: ",
470 p->driver, p->property, p->value);
472 * If errp != NULL, propagate error and return.
473 * If errp == NULL, report a warning, but keep going
474 * with the remaining globals.
476 if (errp) {
477 error_propagate(errp, err);
478 return false;
479 } else {
480 warn_report_err(err);
485 return true;
489 * Global property defaults
490 * Slot 0: accelerator's global property defaults
491 * Slot 1: machine's global property defaults
492 * Slot 2: global properties from legacy command line option
493 * Each is a GPtrArray of of GlobalProperty.
494 * Applied in order, later entries override earlier ones.
496 static GPtrArray *object_compat_props[3];
499 * Retrieve @GPtrArray for global property defined with options
500 * other than "-global". These are generally used for syntactic
501 * sugar and legacy command line options.
503 void object_register_sugar_prop(const char *driver, const char *prop,
504 const char *value, bool optional)
506 GlobalProperty *g;
507 if (!object_compat_props[2]) {
508 object_compat_props[2] = g_ptr_array_new();
510 g = g_new0(GlobalProperty, 1);
511 g->driver = g_strdup(driver);
512 g->property = g_strdup(prop);
513 g->value = g_strdup(value);
514 g->optional = optional;
515 g_ptr_array_add(object_compat_props[2], g);
519 * Set machine's global property defaults to @compat_props.
520 * May be called at most once.
522 void object_set_machine_compat_props(GPtrArray *compat_props)
524 assert(!object_compat_props[1]);
525 object_compat_props[1] = compat_props;
529 * Set accelerator's global property defaults to @compat_props.
530 * May be called at most once.
532 void object_set_accelerator_compat_props(GPtrArray *compat_props)
534 assert(!object_compat_props[0]);
535 object_compat_props[0] = compat_props;
538 void object_apply_compat_props(Object *obj)
540 int i;
542 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
543 object_apply_global_props(obj, object_compat_props[i],
544 i == 2 ? &error_fatal : &error_abort);
548 static void object_class_property_init_all(Object *obj)
550 ObjectPropertyIterator iter;
551 ObjectProperty *prop;
553 object_class_property_iter_init(&iter, object_get_class(obj));
554 while ((prop = object_property_iter_next(&iter))) {
555 if (prop->init) {
556 prop->init(obj, prop);
561 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
563 type_initialize(type);
565 g_assert(type->instance_size >= sizeof(Object));
566 g_assert(type->abstract == false);
567 g_assert(size >= type->instance_size);
569 memset(obj, 0, type->instance_size);
570 obj->class = type->class;
571 object_ref(obj);
572 object_class_property_init_all(obj);
573 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
574 NULL, object_property_free);
575 object_init_with_type(obj, type);
576 object_post_init_with_type(obj, type);
579 void object_initialize(void *data, size_t size, const char *typename)
581 TypeImpl *type = type_get_by_name(typename);
583 #ifdef CONFIG_MODULES
584 if (!type) {
585 int rv = module_load_qom(typename, &error_fatal);
586 if (rv > 0) {
587 type = type_get_by_name(typename);
588 } else {
589 error_report("missing object type '%s'", typename);
590 exit(1);
593 #endif
594 if (!type) {
595 error_report("missing object type '%s'", typename);
596 abort();
599 object_initialize_with_type(data, size, type);
602 bool object_initialize_child_with_props(Object *parentobj,
603 const char *propname,
604 void *childobj, size_t size,
605 const char *type,
606 Error **errp, ...)
608 va_list vargs;
609 bool ok;
611 va_start(vargs, errp);
612 ok = object_initialize_child_with_propsv(parentobj, propname,
613 childobj, size, type, errp,
614 vargs);
615 va_end(vargs);
616 return ok;
619 bool object_initialize_child_with_propsv(Object *parentobj,
620 const char *propname,
621 void *childobj, size_t size,
622 const char *type,
623 Error **errp, va_list vargs)
625 bool ok = false;
626 Object *obj;
627 UserCreatable *uc;
629 object_initialize(childobj, size, type);
630 obj = OBJECT(childobj);
632 if (!object_set_propv(obj, errp, vargs)) {
633 goto out;
636 object_property_add_child(parentobj, propname, obj);
638 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
639 if (uc) {
640 if (!user_creatable_complete(uc, errp)) {
641 object_unparent(obj);
642 goto out;
646 ok = true;
648 out:
650 * We want @obj's reference to be 1 on success, 0 on failure.
651 * On success, it's 2: one taken by object_initialize(), and one
652 * by object_property_add_child().
653 * On failure in object_initialize() or earlier, it's 1.
654 * On failure afterwards, it's also 1: object_unparent() releases
655 * the reference taken by object_property_add_child().
657 object_unref(obj);
658 return ok;
661 void object_initialize_child_internal(Object *parent,
662 const char *propname,
663 void *child, size_t size,
664 const char *type)
666 object_initialize_child_with_props(parent, propname, child, size, type,
667 &error_abort, NULL);
670 static inline bool object_property_is_child(ObjectProperty *prop)
672 return strstart(prop->type, "child<", NULL);
675 static void object_property_del_all(Object *obj)
677 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
678 ObjectProperty *prop;
679 ObjectPropertyIterator iter;
680 bool released;
682 do {
683 released = false;
684 object_property_iter_init(&iter, obj);
685 while ((prop = object_property_iter_next(&iter)) != NULL) {
686 if (g_hash_table_add(done, prop)) {
687 if (prop->release) {
688 prop->release(obj, prop->name, prop->opaque);
689 released = true;
690 break;
694 } while (released);
696 g_hash_table_unref(obj->properties);
699 static void object_property_del_child(Object *obj, Object *child)
701 ObjectProperty *prop;
702 GHashTableIter iter;
703 gpointer key, value;
705 g_hash_table_iter_init(&iter, obj->properties);
706 while (g_hash_table_iter_next(&iter, &key, &value)) {
707 prop = value;
708 if (object_property_is_child(prop) && prop->opaque == child) {
709 if (prop->release) {
710 prop->release(obj, prop->name, prop->opaque);
711 prop->release = NULL;
713 break;
716 g_hash_table_iter_init(&iter, obj->properties);
717 while (g_hash_table_iter_next(&iter, &key, &value)) {
718 prop = value;
719 if (object_property_is_child(prop) && prop->opaque == child) {
720 g_hash_table_iter_remove(&iter);
721 break;
726 void object_unparent(Object *obj)
728 if (obj->parent) {
729 object_property_del_child(obj->parent, obj);
733 static void object_deinit(Object *obj, TypeImpl *type)
735 if (type->instance_finalize) {
736 type->instance_finalize(obj);
739 if (type_has_parent(type)) {
740 object_deinit(obj, type_get_parent(type));
744 static void object_finalize(void *data)
746 Object *obj = data;
747 TypeImpl *ti = obj->class->type;
749 object_property_del_all(obj);
750 object_deinit(obj, ti);
752 g_assert(obj->ref == 0);
753 g_assert(obj->parent == NULL);
754 if (obj->free) {
755 obj->free(obj);
759 /* Find the minimum alignment guaranteed by the system malloc. */
760 #if __STDC_VERSION__ >= 201112L
761 typedef max_align_t qemu_max_align_t;
762 #else
763 typedef union {
764 long l;
765 void *p;
766 double d;
767 long double ld;
768 } qemu_max_align_t;
769 #endif
771 static Object *object_new_with_type(Type type)
773 Object *obj;
774 size_t size, align;
775 void (*obj_free)(void *);
777 g_assert(type != NULL);
778 type_initialize(type);
780 size = type->instance_size;
781 align = type->instance_align;
784 * Do not use qemu_memalign unless required. Depending on the
785 * implementation, extra alignment implies extra overhead.
787 if (likely(align <= __alignof__(qemu_max_align_t))) {
788 obj = g_malloc(size);
789 obj_free = g_free;
790 } else {
791 obj = qemu_memalign(align, size);
792 obj_free = qemu_vfree;
795 object_initialize_with_type(obj, size, type);
796 obj->free = obj_free;
798 return obj;
801 Object *object_new_with_class(ObjectClass *klass)
803 return object_new_with_type(klass->type);
806 Object *object_new(const char *typename)
808 TypeImpl *ti = type_get_by_name(typename);
810 return object_new_with_type(ti);
814 Object *object_new_with_props(const char *typename,
815 Object *parent,
816 const char *id,
817 Error **errp,
818 ...)
820 va_list vargs;
821 Object *obj;
823 va_start(vargs, errp);
824 obj = object_new_with_propv(typename, parent, id, errp, vargs);
825 va_end(vargs);
827 return obj;
831 Object *object_new_with_propv(const char *typename,
832 Object *parent,
833 const char *id,
834 Error **errp,
835 va_list vargs)
837 Object *obj;
838 ObjectClass *klass;
839 UserCreatable *uc;
841 klass = object_class_by_name(typename);
842 if (!klass) {
843 error_setg(errp, "invalid object type: %s", typename);
844 return NULL;
847 if (object_class_is_abstract(klass)) {
848 error_setg(errp, "object type '%s' is abstract", typename);
849 return NULL;
851 obj = object_new_with_type(klass->type);
853 if (!object_set_propv(obj, errp, vargs)) {
854 goto error;
857 if (id != NULL) {
858 object_property_add_child(parent, id, obj);
861 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
862 if (uc) {
863 if (!user_creatable_complete(uc, errp)) {
864 if (id != NULL) {
865 object_unparent(obj);
867 goto error;
871 object_unref(obj);
872 return obj;
874 error:
875 object_unref(obj);
876 return NULL;
880 bool object_set_props(Object *obj,
881 Error **errp,
882 ...)
884 va_list vargs;
885 bool ret;
887 va_start(vargs, errp);
888 ret = object_set_propv(obj, errp, vargs);
889 va_end(vargs);
891 return ret;
895 bool object_set_propv(Object *obj,
896 Error **errp,
897 va_list vargs)
899 const char *propname;
901 propname = va_arg(vargs, char *);
902 while (propname != NULL) {
903 const char *value = va_arg(vargs, char *);
905 g_assert(value != NULL);
906 if (!object_property_parse(obj, propname, value, errp)) {
907 return false;
909 propname = va_arg(vargs, char *);
912 return true;
916 Object *object_dynamic_cast(Object *obj, const char *typename)
918 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
919 return obj;
922 return NULL;
925 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
926 const char *file, int line, const char *func)
928 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
929 typename, file, line, func);
931 #ifdef CONFIG_QOM_CAST_DEBUG
932 int i;
933 Object *inst;
935 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
936 if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) {
937 goto out;
941 inst = object_dynamic_cast(obj, typename);
943 if (!inst && obj) {
944 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
945 file, line, func, obj, typename);
946 abort();
949 assert(obj == inst);
951 if (obj && obj == inst) {
952 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
953 qatomic_set(&obj->class->object_cast_cache[i - 1],
954 qatomic_read(&obj->class->object_cast_cache[i]));
956 qatomic_set(&obj->class->object_cast_cache[i - 1], typename);
959 out:
960 #endif
961 return obj;
964 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
965 const char *typename)
967 ObjectClass *ret = NULL;
968 TypeImpl *target_type;
969 TypeImpl *type;
971 if (!class) {
972 return NULL;
975 /* A simple fast path that can trigger a lot for leaf classes. */
976 type = class->type;
977 if (type->name == typename) {
978 return class;
981 target_type = type_get_by_name(typename);
982 if (!target_type) {
983 /* target class type unknown, so fail the cast */
984 return NULL;
987 if (type->class->interfaces &&
988 type_is_ancestor(target_type, type_interface)) {
989 int found = 0;
990 GSList *i;
992 for (i = class->interfaces; i; i = i->next) {
993 ObjectClass *target_class = i->data;
995 if (type_is_ancestor(target_class->type, target_type)) {
996 ret = target_class;
997 found++;
1001 /* The match was ambiguous, don't allow a cast */
1002 if (found > 1) {
1003 ret = NULL;
1005 } else if (type_is_ancestor(type, target_type)) {
1006 ret = class;
1009 return ret;
1012 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
1013 const char *typename,
1014 const char *file, int line,
1015 const char *func)
1017 ObjectClass *ret;
1019 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
1020 typename, file, line, func);
1022 #ifdef CONFIG_QOM_CAST_DEBUG
1023 int i;
1025 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
1026 if (qatomic_read(&class->class_cast_cache[i]) == typename) {
1027 ret = class;
1028 goto out;
1031 #else
1032 if (!class || !class->interfaces) {
1033 return class;
1035 #endif
1037 ret = object_class_dynamic_cast(class, typename);
1038 if (!ret && class) {
1039 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
1040 file, line, func, class, typename);
1041 abort();
1044 #ifdef CONFIG_QOM_CAST_DEBUG
1045 if (class && ret == class) {
1046 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
1047 qatomic_set(&class->class_cast_cache[i - 1],
1048 qatomic_read(&class->class_cast_cache[i]));
1050 qatomic_set(&class->class_cast_cache[i - 1], typename);
1052 out:
1053 #endif
1054 return ret;
1057 const char *object_get_typename(const Object *obj)
1059 return obj->class->type->name;
1062 ObjectClass *object_get_class(Object *obj)
1064 return obj->class;
1067 bool object_class_is_abstract(ObjectClass *klass)
1069 return klass->type->abstract;
1072 const char *object_class_get_name(ObjectClass *klass)
1074 return klass->type->name;
1077 ObjectClass *object_class_by_name(const char *typename)
1079 TypeImpl *type = type_get_by_name(typename);
1081 if (!type) {
1082 return NULL;
1085 type_initialize(type);
1087 return type->class;
1090 ObjectClass *module_object_class_by_name(const char *typename)
1092 ObjectClass *oc;
1094 oc = object_class_by_name(typename);
1095 #ifdef CONFIG_MODULES
1096 if (!oc) {
1097 Error *local_err = NULL;
1098 int rv = module_load_qom(typename, &local_err);
1099 if (rv > 0) {
1100 oc = object_class_by_name(typename);
1101 } else if (rv < 0) {
1102 error_report_err(local_err);
1105 #endif
1106 return oc;
1109 ObjectClass *object_class_get_parent(ObjectClass *class)
1111 TypeImpl *type = type_get_parent(class->type);
1113 if (!type) {
1114 return NULL;
1117 type_initialize(type);
1119 return type->class;
1122 typedef struct OCFData
1124 void (*fn)(ObjectClass *klass, void *opaque);
1125 const char *implements_type;
1126 bool include_abstract;
1127 void *opaque;
1128 } OCFData;
1130 static void object_class_foreach_tramp(gpointer key, gpointer value,
1131 gpointer opaque)
1133 OCFData *data = opaque;
1134 TypeImpl *type = value;
1135 ObjectClass *k;
1137 type_initialize(type);
1138 k = type->class;
1140 if (!data->include_abstract && type->abstract) {
1141 return;
1144 if (data->implements_type &&
1145 !object_class_dynamic_cast(k, data->implements_type)) {
1146 return;
1149 data->fn(k, data->opaque);
1152 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1153 const char *implements_type, bool include_abstract,
1154 void *opaque)
1156 OCFData data = { fn, implements_type, include_abstract, opaque };
1158 enumerating_types = true;
1159 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1160 enumerating_types = false;
1163 static int do_object_child_foreach(Object *obj,
1164 int (*fn)(Object *child, void *opaque),
1165 void *opaque, bool recurse)
1167 GHashTableIter iter;
1168 ObjectProperty *prop;
1169 int ret = 0;
1171 g_hash_table_iter_init(&iter, obj->properties);
1172 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1173 if (object_property_is_child(prop)) {
1174 Object *child = prop->opaque;
1176 ret = fn(child, opaque);
1177 if (ret != 0) {
1178 break;
1180 if (recurse) {
1181 ret = do_object_child_foreach(child, fn, opaque, true);
1182 if (ret != 0) {
1183 break;
1188 return ret;
1191 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1192 void *opaque)
1194 return do_object_child_foreach(obj, fn, opaque, false);
1197 int object_child_foreach_recursive(Object *obj,
1198 int (*fn)(Object *child, void *opaque),
1199 void *opaque)
1201 return do_object_child_foreach(obj, fn, opaque, true);
1204 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1206 GSList **list = opaque;
1208 *list = g_slist_prepend(*list, klass);
1211 GSList *object_class_get_list(const char *implements_type,
1212 bool include_abstract)
1214 GSList *list = NULL;
1216 object_class_foreach(object_class_get_list_tramp,
1217 implements_type, include_abstract, &list);
1218 return list;
1221 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1223 return strcasecmp(object_class_get_name((ObjectClass *)a),
1224 object_class_get_name((ObjectClass *)b));
1227 GSList *object_class_get_list_sorted(const char *implements_type,
1228 bool include_abstract)
1230 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1231 object_class_cmp);
1234 Object *object_ref(void *objptr)
1236 Object *obj = OBJECT(objptr);
1237 uint32_t ref;
1239 if (!obj) {
1240 return NULL;
1242 ref = qatomic_fetch_inc(&obj->ref);
1243 /* Assert waaay before the integer overflows */
1244 g_assert(ref < INT_MAX);
1245 return obj;
1248 void object_unref(void *objptr)
1250 Object *obj = OBJECT(objptr);
1251 if (!obj) {
1252 return;
1254 g_assert(obj->ref > 0);
1256 /* parent always holds a reference to its children */
1257 if (qatomic_fetch_dec(&obj->ref) == 1) {
1258 object_finalize(obj);
1262 ObjectProperty *
1263 object_property_try_add(Object *obj, const char *name, const char *type,
1264 ObjectPropertyAccessor *get,
1265 ObjectPropertyAccessor *set,
1266 ObjectPropertyRelease *release,
1267 void *opaque, Error **errp)
1269 ObjectProperty *prop;
1270 size_t name_len = strlen(name);
1272 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1273 int i;
1274 ObjectProperty *ret = NULL;
1275 char *name_no_array = g_strdup(name);
1277 name_no_array[name_len - 3] = '\0';
1278 for (i = 0; i < INT16_MAX; ++i) {
1279 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1281 ret = object_property_try_add(obj, full_name, type, get, set,
1282 release, opaque, NULL);
1283 g_free(full_name);
1284 if (ret) {
1285 break;
1288 g_free(name_no_array);
1289 assert(ret);
1290 return ret;
1293 if (object_property_find(obj, name) != NULL) {
1294 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1295 name, object_get_typename(obj));
1296 return NULL;
1299 prop = g_malloc0(sizeof(*prop));
1301 prop->name = g_strdup(name);
1302 prop->type = g_strdup(type);
1304 prop->get = get;
1305 prop->set = set;
1306 prop->release = release;
1307 prop->opaque = opaque;
1309 g_hash_table_insert(obj->properties, prop->name, prop);
1310 return prop;
1313 ObjectProperty *
1314 object_property_add(Object *obj, const char *name, const char *type,
1315 ObjectPropertyAccessor *get,
1316 ObjectPropertyAccessor *set,
1317 ObjectPropertyRelease *release,
1318 void *opaque)
1320 return object_property_try_add(obj, name, type, get, set, release,
1321 opaque, &error_abort);
1324 ObjectProperty *
1325 object_class_property_add(ObjectClass *klass,
1326 const char *name,
1327 const char *type,
1328 ObjectPropertyAccessor *get,
1329 ObjectPropertyAccessor *set,
1330 ObjectPropertyRelease *release,
1331 void *opaque)
1333 ObjectProperty *prop;
1335 assert(!object_class_property_find(klass, name));
1337 prop = g_malloc0(sizeof(*prop));
1339 prop->name = g_strdup(name);
1340 prop->type = g_strdup(type);
1342 prop->get = get;
1343 prop->set = set;
1344 prop->release = release;
1345 prop->opaque = opaque;
1347 g_hash_table_insert(klass->properties, prop->name, prop);
1349 return prop;
1352 ObjectProperty *object_property_find(Object *obj, const char *name)
1354 ObjectProperty *prop;
1355 ObjectClass *klass = object_get_class(obj);
1357 prop = object_class_property_find(klass, name);
1358 if (prop) {
1359 return prop;
1362 return g_hash_table_lookup(obj->properties, name);
1365 ObjectProperty *object_property_find_err(Object *obj, const char *name,
1366 Error **errp)
1368 ObjectProperty *prop = object_property_find(obj, name);
1369 if (!prop) {
1370 error_setg(errp, "Property '%s.%s' not found",
1371 object_get_typename(obj), name);
1373 return prop;
1376 void object_property_iter_init(ObjectPropertyIterator *iter,
1377 Object *obj)
1379 g_hash_table_iter_init(&iter->iter, obj->properties);
1380 iter->nextclass = object_get_class(obj);
1383 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1385 gpointer key, val;
1386 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1387 if (!iter->nextclass) {
1388 return NULL;
1390 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1391 iter->nextclass = object_class_get_parent(iter->nextclass);
1393 return val;
1396 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1397 ObjectClass *klass)
1399 g_hash_table_iter_init(&iter->iter, klass->properties);
1400 iter->nextclass = object_class_get_parent(klass);
1403 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name)
1405 ObjectClass *parent_klass;
1407 parent_klass = object_class_get_parent(klass);
1408 if (parent_klass) {
1409 ObjectProperty *prop =
1410 object_class_property_find(parent_klass, name);
1411 if (prop) {
1412 return prop;
1416 return g_hash_table_lookup(klass->properties, name);
1419 ObjectProperty *object_class_property_find_err(ObjectClass *klass,
1420 const char *name,
1421 Error **errp)
1423 ObjectProperty *prop = object_class_property_find(klass, name);
1424 if (!prop) {
1425 error_setg(errp, "Property '.%s' not found", name);
1427 return prop;
1431 void object_property_del(Object *obj, const char *name)
1433 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1435 if (prop->release) {
1436 prop->release(obj, name, prop->opaque);
1438 g_hash_table_remove(obj->properties, name);
1441 bool object_property_get(Object *obj, const char *name, Visitor *v,
1442 Error **errp)
1444 Error *err = NULL;
1445 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1447 if (prop == NULL) {
1448 return false;
1451 if (!prop->get) {
1452 error_setg(errp, "Property '%s.%s' is not readable",
1453 object_get_typename(obj), name);
1454 return false;
1456 prop->get(obj, v, name, prop->opaque, &err);
1457 error_propagate(errp, err);
1458 return !err;
1461 bool object_property_set(Object *obj, const char *name, Visitor *v,
1462 Error **errp)
1464 ERRP_GUARD();
1465 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1467 if (prop == NULL) {
1468 return false;
1471 if (!prop->set) {
1472 error_setg(errp, "Property '%s.%s' is not writable",
1473 object_get_typename(obj), name);
1474 return false;
1476 prop->set(obj, v, name, prop->opaque, errp);
1477 return !*errp;
1480 bool object_property_set_str(Object *obj, const char *name,
1481 const char *value, Error **errp)
1483 QString *qstr = qstring_from_str(value);
1484 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
1486 qobject_unref(qstr);
1487 return ok;
1490 char *object_property_get_str(Object *obj, const char *name,
1491 Error **errp)
1493 QObject *ret = object_property_get_qobject(obj, name, errp);
1494 QString *qstring;
1495 char *retval;
1497 if (!ret) {
1498 return NULL;
1500 qstring = qobject_to(QString, ret);
1501 if (!qstring) {
1502 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1503 retval = NULL;
1504 } else {
1505 retval = g_strdup(qstring_get_str(qstring));
1508 qobject_unref(ret);
1509 return retval;
1512 bool object_property_set_link(Object *obj, const char *name,
1513 Object *value, Error **errp)
1515 g_autofree char *path = NULL;
1517 if (value) {
1518 path = object_get_canonical_path(value);
1520 return object_property_set_str(obj, name, path ?: "", errp);
1523 Object *object_property_get_link(Object *obj, const char *name,
1524 Error **errp)
1526 char *str = object_property_get_str(obj, name, errp);
1527 Object *target = NULL;
1529 if (str && *str) {
1530 target = object_resolve_path(str, NULL);
1531 if (!target) {
1532 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1533 "Device '%s' not found", str);
1537 g_free(str);
1538 return target;
1541 bool object_property_set_bool(Object *obj, const char *name,
1542 bool value, Error **errp)
1544 QBool *qbool = qbool_from_bool(value);
1545 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
1547 qobject_unref(qbool);
1548 return ok;
1551 bool object_property_get_bool(Object *obj, const char *name,
1552 Error **errp)
1554 QObject *ret = object_property_get_qobject(obj, name, errp);
1555 QBool *qbool;
1556 bool retval;
1558 if (!ret) {
1559 return false;
1561 qbool = qobject_to(QBool, ret);
1562 if (!qbool) {
1563 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1564 retval = false;
1565 } else {
1566 retval = qbool_get_bool(qbool);
1569 qobject_unref(ret);
1570 return retval;
1573 bool object_property_set_int(Object *obj, const char *name,
1574 int64_t value, Error **errp)
1576 QNum *qnum = qnum_from_int(value);
1577 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1579 qobject_unref(qnum);
1580 return ok;
1583 int64_t object_property_get_int(Object *obj, const char *name,
1584 Error **errp)
1586 QObject *ret = object_property_get_qobject(obj, name, errp);
1587 QNum *qnum;
1588 int64_t retval;
1590 if (!ret) {
1591 return -1;
1594 qnum = qobject_to(QNum, ret);
1595 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1596 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1597 retval = -1;
1600 qobject_unref(ret);
1601 return retval;
1604 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1606 Visitor *v = qobject_input_visitor_new(prop->defval);
1608 assert(prop->set != NULL);
1609 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1611 visit_free(v);
1614 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1616 assert(!prop->defval);
1617 assert(!prop->init);
1619 prop->defval = defval;
1620 prop->init = object_property_init_defval;
1623 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1625 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1628 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1630 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1633 void object_property_set_default_list(ObjectProperty *prop)
1635 object_property_set_default(prop, QOBJECT(qlist_new()));
1638 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1640 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1643 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1645 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1648 bool object_property_set_uint(Object *obj, const char *name,
1649 uint64_t value, Error **errp)
1651 QNum *qnum = qnum_from_uint(value);
1652 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1654 qobject_unref(qnum);
1655 return ok;
1658 uint64_t object_property_get_uint(Object *obj, const char *name,
1659 Error **errp)
1661 QObject *ret = object_property_get_qobject(obj, name, errp);
1662 QNum *qnum;
1663 uint64_t retval;
1665 if (!ret) {
1666 return 0;
1668 qnum = qobject_to(QNum, ret);
1669 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1670 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1671 retval = 0;
1674 qobject_unref(ret);
1675 return retval;
1678 typedef struct EnumProperty {
1679 const QEnumLookup *lookup;
1680 int (*get)(Object *, Error **);
1681 void (*set)(Object *, int, Error **);
1682 } EnumProperty;
1684 int object_property_get_enum(Object *obj, const char *name,
1685 const char *typename, Error **errp)
1687 char *str;
1688 int ret;
1689 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1690 EnumProperty *enumprop;
1692 if (prop == NULL) {
1693 return -1;
1696 if (!g_str_equal(prop->type, typename)) {
1697 error_setg(errp, "Property %s on %s is not '%s' enum type",
1698 name, object_class_get_name(
1699 object_get_class(obj)), typename);
1700 return -1;
1703 enumprop = prop->opaque;
1705 str = object_property_get_str(obj, name, errp);
1706 if (!str) {
1707 return -1;
1710 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1711 g_free(str);
1713 return ret;
1716 bool object_property_parse(Object *obj, const char *name,
1717 const char *string, Error **errp)
1719 Visitor *v = string_input_visitor_new(string);
1720 bool ok = object_property_set(obj, name, v, errp);
1722 visit_free(v);
1723 return ok;
1726 char *object_property_print(Object *obj, const char *name, bool human,
1727 Error **errp)
1729 Visitor *v;
1730 char *string = NULL;
1732 v = string_output_visitor_new(human, &string);
1733 if (!object_property_get(obj, name, v, errp)) {
1734 goto out;
1737 visit_complete(v, &string);
1739 out:
1740 visit_free(v);
1741 return string;
1744 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1746 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1747 if (prop == NULL) {
1748 return NULL;
1751 return prop->type;
1754 Object *object_get_root(void)
1756 static Object *root;
1758 if (!root) {
1759 root = object_new("container");
1762 return root;
1765 Object *object_get_objects_root(void)
1767 return container_get(object_get_root(), "/objects");
1770 Object *object_get_internal_root(void)
1772 static Object *internal_root;
1774 if (!internal_root) {
1775 internal_root = object_new("container");
1778 return internal_root;
1781 static void object_get_child_property(Object *obj, Visitor *v,
1782 const char *name, void *opaque,
1783 Error **errp)
1785 Object *child = opaque;
1786 char *path;
1788 path = object_get_canonical_path(child);
1789 visit_type_str(v, name, &path, errp);
1790 g_free(path);
1793 static Object *object_resolve_child_property(Object *parent, void *opaque,
1794 const char *part)
1796 return opaque;
1799 static void object_finalize_child_property(Object *obj, const char *name,
1800 void *opaque)
1802 Object *child = opaque;
1804 if (child->class->unparent) {
1805 (child->class->unparent)(child);
1807 child->parent = NULL;
1808 object_unref(child);
1811 ObjectProperty *
1812 object_property_try_add_child(Object *obj, const char *name,
1813 Object *child, Error **errp)
1815 g_autofree char *type = NULL;
1816 ObjectProperty *op;
1818 assert(!child->parent);
1820 type = g_strdup_printf("child<%s>", object_get_typename(child));
1822 op = object_property_try_add(obj, name, type, object_get_child_property,
1823 NULL, object_finalize_child_property,
1824 child, errp);
1825 if (!op) {
1826 return NULL;
1828 op->resolve = object_resolve_child_property;
1829 object_ref(child);
1830 child->parent = obj;
1831 return op;
1834 ObjectProperty *
1835 object_property_add_child(Object *obj, const char *name,
1836 Object *child)
1838 return object_property_try_add_child(obj, name, child, &error_abort);
1841 void object_property_allow_set_link(const Object *obj, const char *name,
1842 Object *val, Error **errp)
1844 /* Allow the link to be set, always */
1847 typedef struct {
1848 union {
1849 Object **targetp;
1850 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1851 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1853 void (*check)(const Object *, const char *, Object *, Error **);
1854 ObjectPropertyLinkFlags flags;
1855 } LinkProperty;
1857 static Object **
1858 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1860 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1861 return &lprop->target;
1862 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1863 return (void *)obj + lprop->offset;
1864 } else {
1865 return lprop->targetp;
1869 static void object_get_link_property(Object *obj, Visitor *v,
1870 const char *name, void *opaque,
1871 Error **errp)
1873 LinkProperty *lprop = opaque;
1874 Object **targetp = object_link_get_targetp(obj, lprop);
1875 char *path;
1877 if (*targetp) {
1878 path = object_get_canonical_path(*targetp);
1879 visit_type_str(v, name, &path, errp);
1880 g_free(path);
1881 } else {
1882 path = (char *)"";
1883 visit_type_str(v, name, &path, errp);
1888 * object_resolve_link:
1890 * Lookup an object and ensure its type matches the link property type. This
1891 * is similar to object_resolve_path() except type verification against the
1892 * link property is performed.
1894 * Returns: The matched object or NULL on path lookup failures.
1896 static Object *object_resolve_link(Object *obj, const char *name,
1897 const char *path, Error **errp)
1899 const char *type;
1900 char *target_type;
1901 bool ambiguous = false;
1902 Object *target;
1904 /* Go from link<FOO> to FOO. */
1905 type = object_property_get_type(obj, name, NULL);
1906 target_type = g_strndup(&type[5], strlen(type) - 6);
1907 target = object_resolve_path_type(path, target_type, &ambiguous);
1909 if (ambiguous) {
1910 error_setg(errp, "Path '%s' does not uniquely identify an object",
1911 path);
1912 } else if (!target) {
1913 target = object_resolve_path(path, &ambiguous);
1914 if (target || ambiguous) {
1915 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1916 } else {
1917 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1918 "Device '%s' not found", path);
1920 target = NULL;
1922 g_free(target_type);
1924 return target;
1927 static void object_set_link_property(Object *obj, Visitor *v,
1928 const char *name, void *opaque,
1929 Error **errp)
1931 Error *local_err = NULL;
1932 LinkProperty *prop = opaque;
1933 Object **targetp = object_link_get_targetp(obj, prop);
1934 Object *old_target = *targetp;
1935 Object *new_target;
1936 char *path = NULL;
1938 if (!visit_type_str(v, name, &path, errp)) {
1939 return;
1942 if (*path) {
1943 new_target = object_resolve_link(obj, name, path, errp);
1944 if (!new_target) {
1945 g_free(path);
1946 return;
1948 } else {
1949 new_target = NULL;
1952 g_free(path);
1954 prop->check(obj, name, new_target, &local_err);
1955 if (local_err) {
1956 error_propagate(errp, local_err);
1957 return;
1960 *targetp = new_target;
1961 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1962 object_ref(new_target);
1963 object_unref(old_target);
1967 static Object *object_resolve_link_property(Object *parent, void *opaque,
1968 const char *part)
1970 LinkProperty *lprop = opaque;
1972 return *object_link_get_targetp(parent, lprop);
1975 static void object_release_link_property(Object *obj, const char *name,
1976 void *opaque)
1978 LinkProperty *prop = opaque;
1979 Object **targetp = object_link_get_targetp(obj, prop);
1981 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1982 object_unref(*targetp);
1984 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1985 g_free(prop);
1989 static ObjectProperty *
1990 object_add_link_prop(Object *obj, const char *name,
1991 const char *type, void *ptr,
1992 void (*check)(const Object *, const char *,
1993 Object *, Error **),
1994 ObjectPropertyLinkFlags flags)
1996 LinkProperty *prop = g_malloc(sizeof(*prop));
1997 g_autofree char *full_type = NULL;
1998 ObjectProperty *op;
2000 if (flags & OBJ_PROP_LINK_DIRECT) {
2001 prop->target = ptr;
2002 } else {
2003 prop->targetp = ptr;
2005 prop->check = check;
2006 prop->flags = flags;
2008 full_type = g_strdup_printf("link<%s>", type);
2010 op = object_property_add(obj, name, full_type,
2011 object_get_link_property,
2012 check ? object_set_link_property : NULL,
2013 object_release_link_property,
2014 prop);
2015 op->resolve = object_resolve_link_property;
2016 return op;
2019 ObjectProperty *
2020 object_property_add_link(Object *obj, const char *name,
2021 const char *type, Object **targetp,
2022 void (*check)(const Object *, const char *,
2023 Object *, Error **),
2024 ObjectPropertyLinkFlags flags)
2026 return object_add_link_prop(obj, name, type, targetp, check, flags);
2029 ObjectProperty *
2030 object_class_property_add_link(ObjectClass *oc,
2031 const char *name,
2032 const char *type, ptrdiff_t offset,
2033 void (*check)(const Object *obj, const char *name,
2034 Object *val, Error **errp),
2035 ObjectPropertyLinkFlags flags)
2037 LinkProperty *prop = g_new0(LinkProperty, 1);
2038 char *full_type;
2039 ObjectProperty *op;
2041 prop->offset = offset;
2042 prop->check = check;
2043 prop->flags = flags | OBJ_PROP_LINK_CLASS;
2045 full_type = g_strdup_printf("link<%s>", type);
2047 op = object_class_property_add(oc, name, full_type,
2048 object_get_link_property,
2049 check ? object_set_link_property : NULL,
2050 object_release_link_property,
2051 prop);
2053 op->resolve = object_resolve_link_property;
2055 g_free(full_type);
2056 return op;
2059 ObjectProperty *
2060 object_property_add_const_link(Object *obj, const char *name,
2061 Object *target)
2063 return object_add_link_prop(obj, name,
2064 object_get_typename(target), target,
2065 NULL, OBJ_PROP_LINK_DIRECT);
2068 const char *object_get_canonical_path_component(const Object *obj)
2070 ObjectProperty *prop = NULL;
2071 GHashTableIter iter;
2073 if (obj->parent == NULL) {
2074 return NULL;
2077 g_hash_table_iter_init(&iter, obj->parent->properties);
2078 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2079 if (!object_property_is_child(prop)) {
2080 continue;
2083 if (prop->opaque == obj) {
2084 return prop->name;
2088 /* obj had a parent but was not a child, should never happen */
2089 g_assert_not_reached();
2090 return NULL;
2093 char *object_get_canonical_path(const Object *obj)
2095 Object *root = object_get_root();
2096 char *newpath, *path = NULL;
2098 if (obj == root) {
2099 return g_strdup("/");
2102 do {
2103 const char *component = object_get_canonical_path_component(obj);
2105 if (!component) {
2106 /* A canonical path must be complete, so discard what was
2107 * collected so far.
2109 g_free(path);
2110 return NULL;
2113 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
2114 g_free(path);
2115 path = newpath;
2116 obj = obj->parent;
2117 } while (obj != root);
2119 return path;
2122 Object *object_resolve_path_component(Object *parent, const char *part)
2124 ObjectProperty *prop = object_property_find(parent, part);
2125 if (prop == NULL) {
2126 return NULL;
2129 if (prop->resolve) {
2130 return prop->resolve(parent, prop->opaque, part);
2131 } else {
2132 return NULL;
2136 static Object *object_resolve_abs_path(Object *parent,
2137 char **parts,
2138 const char *typename)
2140 Object *child;
2142 if (*parts == NULL) {
2143 return object_dynamic_cast(parent, typename);
2146 if (strcmp(*parts, "") == 0) {
2147 return object_resolve_abs_path(parent, parts + 1, typename);
2150 child = object_resolve_path_component(parent, *parts);
2151 if (!child) {
2152 return NULL;
2155 return object_resolve_abs_path(child, parts + 1, typename);
2158 static Object *object_resolve_partial_path(Object *parent,
2159 char **parts,
2160 const char *typename,
2161 bool *ambiguous)
2163 Object *obj;
2164 GHashTableIter iter;
2165 ObjectProperty *prop;
2167 obj = object_resolve_abs_path(parent, parts, typename);
2169 g_hash_table_iter_init(&iter, parent->properties);
2170 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2171 Object *found;
2173 if (!object_property_is_child(prop)) {
2174 continue;
2177 found = object_resolve_partial_path(prop->opaque, parts,
2178 typename, ambiguous);
2179 if (found) {
2180 if (obj) {
2181 *ambiguous = true;
2182 return NULL;
2184 obj = found;
2187 if (*ambiguous) {
2188 return NULL;
2192 return obj;
2195 Object *object_resolve_path_type(const char *path, const char *typename,
2196 bool *ambiguousp)
2198 Object *obj;
2199 char **parts;
2201 parts = g_strsplit(path, "/", 0);
2202 assert(parts);
2204 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2205 bool ambiguous = false;
2206 obj = object_resolve_partial_path(object_get_root(), parts,
2207 typename, &ambiguous);
2208 if (ambiguousp) {
2209 *ambiguousp = ambiguous;
2211 } else {
2212 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2215 g_strfreev(parts);
2217 return obj;
2220 Object *object_resolve_path(const char *path, bool *ambiguous)
2222 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2225 Object *object_resolve_path_at(Object *parent, const char *path)
2227 g_auto(GStrv) parts = g_strsplit(path, "/", 0);
2229 if (*path == '/') {
2230 return object_resolve_abs_path(object_get_root(), parts + 1,
2231 TYPE_OBJECT);
2233 return object_resolve_abs_path(parent, parts, TYPE_OBJECT);
2236 typedef struct StringProperty
2238 char *(*get)(Object *, Error **);
2239 void (*set)(Object *, const char *, Error **);
2240 } StringProperty;
2242 static void property_get_str(Object *obj, Visitor *v, const char *name,
2243 void *opaque, Error **errp)
2245 StringProperty *prop = opaque;
2246 char *value;
2247 Error *err = NULL;
2249 value = prop->get(obj, &err);
2250 if (err) {
2251 error_propagate(errp, err);
2252 return;
2255 visit_type_str(v, name, &value, errp);
2256 g_free(value);
2259 static void property_set_str(Object *obj, Visitor *v, const char *name,
2260 void *opaque, Error **errp)
2262 StringProperty *prop = opaque;
2263 char *value;
2265 if (!visit_type_str(v, name, &value, errp)) {
2266 return;
2269 prop->set(obj, value, errp);
2270 g_free(value);
2273 static void property_release_data(Object *obj, const char *name,
2274 void *opaque)
2276 g_free(opaque);
2279 ObjectProperty *
2280 object_property_add_str(Object *obj, const char *name,
2281 char *(*get)(Object *, Error **),
2282 void (*set)(Object *, const char *, Error **))
2284 StringProperty *prop = g_malloc0(sizeof(*prop));
2286 prop->get = get;
2287 prop->set = set;
2289 return object_property_add(obj, name, "string",
2290 get ? property_get_str : NULL,
2291 set ? property_set_str : NULL,
2292 property_release_data,
2293 prop);
2296 ObjectProperty *
2297 object_class_property_add_str(ObjectClass *klass, const char *name,
2298 char *(*get)(Object *, Error **),
2299 void (*set)(Object *, const char *,
2300 Error **))
2302 StringProperty *prop = g_malloc0(sizeof(*prop));
2304 prop->get = get;
2305 prop->set = set;
2307 return object_class_property_add(klass, name, "string",
2308 get ? property_get_str : NULL,
2309 set ? property_set_str : NULL,
2310 NULL,
2311 prop);
2314 typedef struct BoolProperty
2316 bool (*get)(Object *, Error **);
2317 void (*set)(Object *, bool, Error **);
2318 } BoolProperty;
2320 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2321 void *opaque, Error **errp)
2323 BoolProperty *prop = opaque;
2324 bool value;
2325 Error *err = NULL;
2327 value = prop->get(obj, &err);
2328 if (err) {
2329 error_propagate(errp, err);
2330 return;
2333 visit_type_bool(v, name, &value, errp);
2336 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2337 void *opaque, Error **errp)
2339 BoolProperty *prop = opaque;
2340 bool value;
2342 if (!visit_type_bool(v, name, &value, errp)) {
2343 return;
2346 prop->set(obj, value, errp);
2349 ObjectProperty *
2350 object_property_add_bool(Object *obj, const char *name,
2351 bool (*get)(Object *, Error **),
2352 void (*set)(Object *, bool, Error **))
2354 BoolProperty *prop = g_malloc0(sizeof(*prop));
2356 prop->get = get;
2357 prop->set = set;
2359 return object_property_add(obj, name, "bool",
2360 get ? property_get_bool : NULL,
2361 set ? property_set_bool : NULL,
2362 property_release_data,
2363 prop);
2366 ObjectProperty *
2367 object_class_property_add_bool(ObjectClass *klass, const char *name,
2368 bool (*get)(Object *, Error **),
2369 void (*set)(Object *, bool, Error **))
2371 BoolProperty *prop = g_malloc0(sizeof(*prop));
2373 prop->get = get;
2374 prop->set = set;
2376 return object_class_property_add(klass, name, "bool",
2377 get ? property_get_bool : NULL,
2378 set ? property_set_bool : NULL,
2379 NULL,
2380 prop);
2383 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2384 void *opaque, Error **errp)
2386 EnumProperty *prop = opaque;
2387 int value;
2388 Error *err = NULL;
2390 value = prop->get(obj, &err);
2391 if (err) {
2392 error_propagate(errp, err);
2393 return;
2396 visit_type_enum(v, name, &value, prop->lookup, errp);
2399 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2400 void *opaque, Error **errp)
2402 EnumProperty *prop = opaque;
2403 int value;
2405 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
2406 return;
2408 prop->set(obj, value, errp);
2411 ObjectProperty *
2412 object_property_add_enum(Object *obj, const char *name,
2413 const char *typename,
2414 const QEnumLookup *lookup,
2415 int (*get)(Object *, Error **),
2416 void (*set)(Object *, int, Error **))
2418 EnumProperty *prop = g_malloc(sizeof(*prop));
2420 prop->lookup = lookup;
2421 prop->get = get;
2422 prop->set = set;
2424 return object_property_add(obj, name, typename,
2425 get ? property_get_enum : NULL,
2426 set ? property_set_enum : NULL,
2427 property_release_data,
2428 prop);
2431 ObjectProperty *
2432 object_class_property_add_enum(ObjectClass *klass, const char *name,
2433 const char *typename,
2434 const QEnumLookup *lookup,
2435 int (*get)(Object *, Error **),
2436 void (*set)(Object *, int, Error **))
2438 EnumProperty *prop = g_malloc(sizeof(*prop));
2440 prop->lookup = lookup;
2441 prop->get = get;
2442 prop->set = set;
2444 return object_class_property_add(klass, name, typename,
2445 get ? property_get_enum : NULL,
2446 set ? property_set_enum : NULL,
2447 NULL,
2448 prop);
2451 typedef struct TMProperty {
2452 void (*get)(Object *, struct tm *, Error **);
2453 } TMProperty;
2455 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2456 void *opaque, Error **errp)
2458 TMProperty *prop = opaque;
2459 Error *err = NULL;
2460 struct tm value;
2462 prop->get(obj, &value, &err);
2463 if (err) {
2464 error_propagate(errp, err);
2465 return;
2468 if (!visit_start_struct(v, name, NULL, 0, errp)) {
2469 return;
2471 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
2472 goto out_end;
2474 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
2475 goto out_end;
2477 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
2478 goto out_end;
2480 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
2481 goto out_end;
2483 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
2484 goto out_end;
2486 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
2487 goto out_end;
2489 visit_check_struct(v, errp);
2490 out_end:
2491 visit_end_struct(v, NULL);
2494 ObjectProperty *
2495 object_property_add_tm(Object *obj, const char *name,
2496 void (*get)(Object *, struct tm *, Error **))
2498 TMProperty *prop = g_malloc0(sizeof(*prop));
2500 prop->get = get;
2502 return object_property_add(obj, name, "struct tm",
2503 get ? property_get_tm : NULL, NULL,
2504 property_release_data,
2505 prop);
2508 ObjectProperty *
2509 object_class_property_add_tm(ObjectClass *klass, const char *name,
2510 void (*get)(Object *, struct tm *, Error **))
2512 TMProperty *prop = g_malloc0(sizeof(*prop));
2514 prop->get = get;
2516 return object_class_property_add(klass, name, "struct tm",
2517 get ? property_get_tm : NULL,
2518 NULL, NULL, prop);
2521 static char *object_get_type(Object *obj, Error **errp)
2523 return g_strdup(object_get_typename(obj));
2526 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2527 void *opaque, Error **errp)
2529 uint8_t value = *(uint8_t *)opaque;
2530 visit_type_uint8(v, name, &value, errp);
2533 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2534 void *opaque, Error **errp)
2536 uint8_t *field = opaque;
2537 uint8_t value;
2539 if (!visit_type_uint8(v, name, &value, errp)) {
2540 return;
2543 *field = value;
2546 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2547 void *opaque, Error **errp)
2549 uint16_t value = *(uint16_t *)opaque;
2550 visit_type_uint16(v, name, &value, errp);
2553 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2554 void *opaque, Error **errp)
2556 uint16_t *field = opaque;
2557 uint16_t value;
2559 if (!visit_type_uint16(v, name, &value, errp)) {
2560 return;
2563 *field = value;
2566 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2567 void *opaque, Error **errp)
2569 uint32_t value = *(uint32_t *)opaque;
2570 visit_type_uint32(v, name, &value, errp);
2573 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2574 void *opaque, Error **errp)
2576 uint32_t *field = opaque;
2577 uint32_t value;
2579 if (!visit_type_uint32(v, name, &value, errp)) {
2580 return;
2583 *field = value;
2586 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2587 void *opaque, Error **errp)
2589 uint64_t value = *(uint64_t *)opaque;
2590 visit_type_uint64(v, name, &value, errp);
2593 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2594 void *opaque, Error **errp)
2596 uint64_t *field = opaque;
2597 uint64_t value;
2599 if (!visit_type_uint64(v, name, &value, errp)) {
2600 return;
2603 *field = value;
2606 ObjectProperty *
2607 object_property_add_uint8_ptr(Object *obj, const char *name,
2608 const uint8_t *v,
2609 ObjectPropertyFlags flags)
2611 ObjectPropertyAccessor *getter = NULL;
2612 ObjectPropertyAccessor *setter = NULL;
2614 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2615 getter = property_get_uint8_ptr;
2618 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2619 setter = property_set_uint8_ptr;
2622 return object_property_add(obj, name, "uint8",
2623 getter, setter, NULL, (void *)v);
2626 ObjectProperty *
2627 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2628 const uint8_t *v,
2629 ObjectPropertyFlags flags)
2631 ObjectPropertyAccessor *getter = NULL;
2632 ObjectPropertyAccessor *setter = NULL;
2634 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2635 getter = property_get_uint8_ptr;
2638 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2639 setter = property_set_uint8_ptr;
2642 return object_class_property_add(klass, name, "uint8",
2643 getter, setter, NULL, (void *)v);
2646 ObjectProperty *
2647 object_property_add_uint16_ptr(Object *obj, const char *name,
2648 const uint16_t *v,
2649 ObjectPropertyFlags flags)
2651 ObjectPropertyAccessor *getter = NULL;
2652 ObjectPropertyAccessor *setter = NULL;
2654 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2655 getter = property_get_uint16_ptr;
2658 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2659 setter = property_set_uint16_ptr;
2662 return object_property_add(obj, name, "uint16",
2663 getter, setter, NULL, (void *)v);
2666 ObjectProperty *
2667 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2668 const uint16_t *v,
2669 ObjectPropertyFlags flags)
2671 ObjectPropertyAccessor *getter = NULL;
2672 ObjectPropertyAccessor *setter = NULL;
2674 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2675 getter = property_get_uint16_ptr;
2678 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2679 setter = property_set_uint16_ptr;
2682 return object_class_property_add(klass, name, "uint16",
2683 getter, setter, NULL, (void *)v);
2686 ObjectProperty *
2687 object_property_add_uint32_ptr(Object *obj, const char *name,
2688 const uint32_t *v,
2689 ObjectPropertyFlags flags)
2691 ObjectPropertyAccessor *getter = NULL;
2692 ObjectPropertyAccessor *setter = NULL;
2694 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2695 getter = property_get_uint32_ptr;
2698 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2699 setter = property_set_uint32_ptr;
2702 return object_property_add(obj, name, "uint32",
2703 getter, setter, NULL, (void *)v);
2706 ObjectProperty *
2707 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2708 const uint32_t *v,
2709 ObjectPropertyFlags flags)
2711 ObjectPropertyAccessor *getter = NULL;
2712 ObjectPropertyAccessor *setter = NULL;
2714 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2715 getter = property_get_uint32_ptr;
2718 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2719 setter = property_set_uint32_ptr;
2722 return object_class_property_add(klass, name, "uint32",
2723 getter, setter, NULL, (void *)v);
2726 ObjectProperty *
2727 object_property_add_uint64_ptr(Object *obj, const char *name,
2728 const uint64_t *v,
2729 ObjectPropertyFlags flags)
2731 ObjectPropertyAccessor *getter = NULL;
2732 ObjectPropertyAccessor *setter = NULL;
2734 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2735 getter = property_get_uint64_ptr;
2738 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2739 setter = property_set_uint64_ptr;
2742 return object_property_add(obj, name, "uint64",
2743 getter, setter, NULL, (void *)v);
2746 ObjectProperty *
2747 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2748 const uint64_t *v,
2749 ObjectPropertyFlags flags)
2751 ObjectPropertyAccessor *getter = NULL;
2752 ObjectPropertyAccessor *setter = NULL;
2754 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2755 getter = property_get_uint64_ptr;
2758 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2759 setter = property_set_uint64_ptr;
2762 return object_class_property_add(klass, name, "uint64",
2763 getter, setter, NULL, (void *)v);
2766 typedef struct {
2767 Object *target_obj;
2768 char *target_name;
2769 } AliasProperty;
2771 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2772 void *opaque, Error **errp)
2774 AliasProperty *prop = opaque;
2775 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2777 object_property_get(prop->target_obj, prop->target_name, alias_v, errp);
2778 visit_free(alias_v);
2781 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2782 void *opaque, Error **errp)
2784 AliasProperty *prop = opaque;
2785 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2787 object_property_set(prop->target_obj, prop->target_name, alias_v, errp);
2788 visit_free(alias_v);
2791 static Object *property_resolve_alias(Object *obj, void *opaque,
2792 const char *part)
2794 AliasProperty *prop = opaque;
2796 return object_resolve_path_component(prop->target_obj, prop->target_name);
2799 static void property_release_alias(Object *obj, const char *name, void *opaque)
2801 AliasProperty *prop = opaque;
2803 g_free(prop->target_name);
2804 g_free(prop);
2807 ObjectProperty *
2808 object_property_add_alias(Object *obj, const char *name,
2809 Object *target_obj, const char *target_name)
2811 AliasProperty *prop;
2812 ObjectProperty *op;
2813 ObjectProperty *target_prop;
2814 g_autofree char *prop_type = NULL;
2816 target_prop = object_property_find_err(target_obj, target_name,
2817 &error_abort);
2819 if (object_property_is_child(target_prop)) {
2820 prop_type = g_strdup_printf("link%s",
2821 target_prop->type + strlen("child"));
2822 } else {
2823 prop_type = g_strdup(target_prop->type);
2826 prop = g_malloc(sizeof(*prop));
2827 prop->target_obj = target_obj;
2828 prop->target_name = g_strdup(target_name);
2830 op = object_property_add(obj, name, prop_type,
2831 property_get_alias,
2832 property_set_alias,
2833 property_release_alias,
2834 prop);
2835 op->resolve = property_resolve_alias;
2836 if (target_prop->defval) {
2837 op->defval = qobject_ref(target_prop->defval);
2840 object_property_set_description(obj, op->name,
2841 target_prop->description);
2842 return op;
2845 void object_property_set_description(Object *obj, const char *name,
2846 const char *description)
2848 ObjectProperty *op;
2850 op = object_property_find_err(obj, name, &error_abort);
2851 g_free(op->description);
2852 op->description = g_strdup(description);
2855 void object_class_property_set_description(ObjectClass *klass,
2856 const char *name,
2857 const char *description)
2859 ObjectProperty *op;
2861 op = g_hash_table_lookup(klass->properties, name);
2862 g_free(op->description);
2863 op->description = g_strdup(description);
2866 static void object_class_init(ObjectClass *klass, void *data)
2868 object_class_property_add_str(klass, "type", object_get_type,
2869 NULL);
2872 static void register_types(void)
2874 static const TypeInfo interface_info = {
2875 .name = TYPE_INTERFACE,
2876 .class_size = sizeof(InterfaceClass),
2877 .abstract = true,
2880 static const TypeInfo object_info = {
2881 .name = TYPE_OBJECT,
2882 .instance_size = sizeof(Object),
2883 .class_init = object_class_init,
2884 .abstract = true,
2887 type_interface = type_register_internal(&interface_info);
2888 type_register_internal(&object_info);
2891 type_init(register_types)