hw/arm/aspeed: Dynamically allocate AspeedMachineState::soc field
[qemu/ar7.git] / qom / object.c
blob8557fe8e4e0fc1e795c63e7d1281222c1667a494
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/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 TypeImpl *type_register_internal(const TypeInfo *info)
142 TypeImpl *ti;
143 ti = type_new(info);
145 type_table_add(ti);
146 return ti;
149 TypeImpl *type_register(const TypeInfo *info)
151 assert(info->parent);
152 return type_register_internal(info);
155 TypeImpl *type_register_static(const TypeInfo *info)
157 return type_register(info);
160 void type_register_static_array(const TypeInfo *infos, int nr_infos)
162 int i;
164 for (i = 0; i < nr_infos; i++) {
165 type_register_static(&infos[i]);
169 static TypeImpl *type_get_by_name(const char *name)
171 if (name == NULL) {
172 return NULL;
175 return type_table_lookup(name);
178 static TypeImpl *type_get_parent(TypeImpl *type)
180 if (!type->parent_type && type->parent) {
181 type->parent_type = type_get_by_name(type->parent);
182 if (!type->parent_type) {
183 fprintf(stderr, "Type '%s' is missing its parent '%s'\n",
184 type->name, type->parent);
185 abort();
189 return type->parent_type;
192 static bool type_has_parent(TypeImpl *type)
194 return (type->parent != NULL);
197 static size_t type_class_get_size(TypeImpl *ti)
199 if (ti->class_size) {
200 return ti->class_size;
203 if (type_has_parent(ti)) {
204 return type_class_get_size(type_get_parent(ti));
207 return sizeof(ObjectClass);
210 static size_t type_object_get_size(TypeImpl *ti)
212 if (ti->instance_size) {
213 return ti->instance_size;
216 if (type_has_parent(ti)) {
217 return type_object_get_size(type_get_parent(ti));
220 return 0;
223 static size_t type_object_get_align(TypeImpl *ti)
225 if (ti->instance_align) {
226 return ti->instance_align;
229 if (type_has_parent(ti)) {
230 return type_object_get_align(type_get_parent(ti));
233 return 0;
236 size_t object_type_get_instance_size(const char *typename)
238 TypeImpl *type = type_get_by_name(typename);
240 g_assert(type != NULL);
241 return type_object_get_size(type);
244 static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
246 assert(target_type);
248 /* Check if target_type is a direct ancestor of type */
249 while (type) {
250 if (type == target_type) {
251 return true;
254 type = type_get_parent(type);
257 return false;
260 static void type_initialize(TypeImpl *ti);
262 static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
263 TypeImpl *parent_type)
265 InterfaceClass *new_iface;
266 TypeInfo info = { };
267 TypeImpl *iface_impl;
269 info.parent = parent_type->name;
270 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
271 info.abstract = true;
273 iface_impl = type_new(&info);
274 iface_impl->parent_type = parent_type;
275 type_initialize(iface_impl);
276 g_free((char *)info.name);
278 new_iface = (InterfaceClass *)iface_impl->class;
279 new_iface->concrete_class = ti->class;
280 new_iface->interface_type = interface_type;
282 ti->class->interfaces = g_slist_append(ti->class->interfaces, new_iface);
285 static void object_property_free(gpointer data)
287 ObjectProperty *prop = data;
289 if (prop->defval) {
290 qobject_unref(prop->defval);
291 prop->defval = NULL;
293 g_free(prop->name);
294 g_free(prop->type);
295 g_free(prop->description);
296 g_free(prop);
299 static void type_initialize(TypeImpl *ti)
301 TypeImpl *parent;
303 if (ti->class) {
304 return;
307 ti->class_size = type_class_get_size(ti);
308 ti->instance_size = type_object_get_size(ti);
309 ti->instance_align = type_object_get_align(ti);
310 /* Any type with zero instance_size is implicitly abstract.
311 * This means interface types are all abstract.
313 if (ti->instance_size == 0) {
314 ti->abstract = true;
316 if (type_is_ancestor(ti, type_interface)) {
317 assert(ti->instance_size == 0);
318 assert(ti->abstract);
319 assert(!ti->instance_init);
320 assert(!ti->instance_post_init);
321 assert(!ti->instance_finalize);
322 assert(!ti->num_interfaces);
324 ti->class = g_malloc0(ti->class_size);
326 parent = type_get_parent(ti);
327 if (parent) {
328 type_initialize(parent);
329 GSList *e;
330 int i;
332 g_assert(parent->class_size <= ti->class_size);
333 g_assert(parent->instance_size <= ti->instance_size);
334 memcpy(ti->class, parent->class, parent->class_size);
335 ti->class->interfaces = NULL;
337 for (e = parent->class->interfaces; e; e = e->next) {
338 InterfaceClass *iface = e->data;
339 ObjectClass *klass = OBJECT_CLASS(iface);
341 type_initialize_interface(ti, iface->interface_type, klass->type);
344 for (i = 0; i < ti->num_interfaces; i++) {
345 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
346 if (!t) {
347 error_report("missing interface '%s' for object '%s'",
348 ti->interfaces[i].typename, parent->name);
349 abort();
351 for (e = ti->class->interfaces; e; e = e->next) {
352 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
354 if (type_is_ancestor(target_type, t)) {
355 break;
359 if (e) {
360 continue;
363 type_initialize_interface(ti, t, t);
367 ti->class->properties = g_hash_table_new_full(g_str_hash, g_str_equal, NULL,
368 object_property_free);
370 ti->class->type = ti;
372 while (parent) {
373 if (parent->class_base_init) {
374 parent->class_base_init(ti->class, ti->class_data);
376 parent = type_get_parent(parent);
379 if (ti->class_init) {
380 ti->class_init(ti->class, ti->class_data);
384 static void object_init_with_type(Object *obj, TypeImpl *ti)
386 if (type_has_parent(ti)) {
387 object_init_with_type(obj, type_get_parent(ti));
390 if (ti->instance_init) {
391 ti->instance_init(obj);
395 static void object_post_init_with_type(Object *obj, TypeImpl *ti)
397 if (ti->instance_post_init) {
398 ti->instance_post_init(obj);
401 if (type_has_parent(ti)) {
402 object_post_init_with_type(obj, type_get_parent(ti));
406 bool object_apply_global_props(Object *obj, const GPtrArray *props,
407 Error **errp)
409 int i;
411 if (!props) {
412 return true;
415 for (i = 0; i < props->len; i++) {
416 GlobalProperty *p = g_ptr_array_index(props, i);
417 Error *err = NULL;
419 if (object_dynamic_cast(obj, p->driver) == NULL) {
420 continue;
422 if (p->optional && !object_property_find(obj, p->property)) {
423 continue;
425 p->used = true;
426 if (!object_property_parse(obj, p->property, p->value, &err)) {
427 error_prepend(&err, "can't apply global %s.%s=%s: ",
428 p->driver, p->property, p->value);
430 * If errp != NULL, propagate error and return.
431 * If errp == NULL, report a warning, but keep going
432 * with the remaining globals.
434 if (errp) {
435 error_propagate(errp, err);
436 return false;
437 } else {
438 warn_report_err(err);
443 return true;
447 * Global property defaults
448 * Slot 0: accelerator's global property defaults
449 * Slot 1: machine's global property defaults
450 * Slot 2: global properties from legacy command line option
451 * Each is a GPtrArray of of GlobalProperty.
452 * Applied in order, later entries override earlier ones.
454 static GPtrArray *object_compat_props[3];
457 * Retrieve @GPtrArray for global property defined with options
458 * other than "-global". These are generally used for syntactic
459 * sugar and legacy command line options.
461 void object_register_sugar_prop(const char *driver, const char *prop,
462 const char *value, bool optional)
464 GlobalProperty *g;
465 if (!object_compat_props[2]) {
466 object_compat_props[2] = g_ptr_array_new();
468 g = g_new0(GlobalProperty, 1);
469 g->driver = g_strdup(driver);
470 g->property = g_strdup(prop);
471 g->value = g_strdup(value);
472 g->optional = optional;
473 g_ptr_array_add(object_compat_props[2], g);
477 * Set machine's global property defaults to @compat_props.
478 * May be called at most once.
480 void object_set_machine_compat_props(GPtrArray *compat_props)
482 assert(!object_compat_props[1]);
483 object_compat_props[1] = compat_props;
487 * Set accelerator's global property defaults to @compat_props.
488 * May be called at most once.
490 void object_set_accelerator_compat_props(GPtrArray *compat_props)
492 assert(!object_compat_props[0]);
493 object_compat_props[0] = compat_props;
496 void object_apply_compat_props(Object *obj)
498 int i;
500 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
501 object_apply_global_props(obj, object_compat_props[i],
502 i == 2 ? &error_fatal : &error_abort);
506 static void object_class_property_init_all(Object *obj)
508 ObjectPropertyIterator iter;
509 ObjectProperty *prop;
511 object_class_property_iter_init(&iter, object_get_class(obj));
512 while ((prop = object_property_iter_next(&iter))) {
513 if (prop->init) {
514 prop->init(obj, prop);
519 static void object_initialize_with_type(Object *obj, size_t size, TypeImpl *type)
521 type_initialize(type);
523 g_assert(type->instance_size >= sizeof(Object));
524 g_assert(type->abstract == false);
525 g_assert(size >= type->instance_size);
527 memset(obj, 0, type->instance_size);
528 obj->class = type->class;
529 object_ref(obj);
530 object_class_property_init_all(obj);
531 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
532 NULL, object_property_free);
533 object_init_with_type(obj, type);
534 object_post_init_with_type(obj, type);
537 void object_initialize(void *data, size_t size, const char *typename)
539 TypeImpl *type = type_get_by_name(typename);
541 #ifdef CONFIG_MODULES
542 if (!type) {
543 int rv = module_load_qom(typename, &error_fatal);
544 if (rv > 0) {
545 type = type_get_by_name(typename);
546 } else {
547 error_report("missing object type '%s'", typename);
548 exit(1);
551 #endif
552 if (!type) {
553 error_report("missing object type '%s'", typename);
554 abort();
557 object_initialize_with_type(data, size, type);
560 bool object_initialize_child_with_props(Object *parentobj,
561 const char *propname,
562 void *childobj, size_t size,
563 const char *type,
564 Error **errp, ...)
566 va_list vargs;
567 bool ok;
569 va_start(vargs, errp);
570 ok = object_initialize_child_with_propsv(parentobj, propname,
571 childobj, size, type, errp,
572 vargs);
573 va_end(vargs);
574 return ok;
577 bool object_initialize_child_with_propsv(Object *parentobj,
578 const char *propname,
579 void *childobj, size_t size,
580 const char *type,
581 Error **errp, va_list vargs)
583 bool ok = false;
584 Object *obj;
585 UserCreatable *uc;
587 object_initialize(childobj, size, type);
588 obj = OBJECT(childobj);
590 if (!object_set_propv(obj, errp, vargs)) {
591 goto out;
594 object_property_add_child(parentobj, propname, obj);
596 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
597 if (uc) {
598 if (!user_creatable_complete(uc, errp)) {
599 object_unparent(obj);
600 goto out;
604 ok = true;
606 out:
608 * We want @obj's reference to be 1 on success, 0 on failure.
609 * On success, it's 2: one taken by object_initialize(), and one
610 * by object_property_add_child().
611 * On failure in object_initialize() or earlier, it's 1.
612 * On failure afterwards, it's also 1: object_unparent() releases
613 * the reference taken by object_property_add_child().
615 object_unref(obj);
616 return ok;
619 void object_initialize_child_internal(Object *parent,
620 const char *propname,
621 void *child, size_t size,
622 const char *type)
624 object_initialize_child_with_props(parent, propname, child, size, type,
625 &error_abort, NULL);
628 static inline bool object_property_is_child(ObjectProperty *prop)
630 return strstart(prop->type, "child<", NULL);
633 static void object_property_del_all(Object *obj)
635 g_autoptr(GHashTable) done = g_hash_table_new(NULL, NULL);
636 ObjectProperty *prop;
637 ObjectPropertyIterator iter;
638 bool released;
640 do {
641 released = false;
642 object_property_iter_init(&iter, obj);
643 while ((prop = object_property_iter_next(&iter)) != NULL) {
644 if (g_hash_table_add(done, prop)) {
645 if (prop->release) {
646 prop->release(obj, prop->name, prop->opaque);
647 released = true;
648 break;
652 } while (released);
654 g_hash_table_unref(obj->properties);
657 static void object_property_del_child(Object *obj, Object *child)
659 ObjectProperty *prop;
660 GHashTableIter iter;
661 gpointer key, value;
663 g_hash_table_iter_init(&iter, obj->properties);
664 while (g_hash_table_iter_next(&iter, &key, &value)) {
665 prop = value;
666 if (object_property_is_child(prop) && prop->opaque == child) {
667 if (prop->release) {
668 prop->release(obj, prop->name, prop->opaque);
669 prop->release = NULL;
671 break;
674 g_hash_table_iter_init(&iter, obj->properties);
675 while (g_hash_table_iter_next(&iter, &key, &value)) {
676 prop = value;
677 if (object_property_is_child(prop) && prop->opaque == child) {
678 g_hash_table_iter_remove(&iter);
679 break;
684 void object_unparent(Object *obj)
686 if (obj->parent) {
687 object_property_del_child(obj->parent, obj);
691 static void object_deinit(Object *obj, TypeImpl *type)
693 if (type->instance_finalize) {
694 type->instance_finalize(obj);
697 if (type_has_parent(type)) {
698 object_deinit(obj, type_get_parent(type));
702 static void object_finalize(void *data)
704 Object *obj = data;
705 TypeImpl *ti = obj->class->type;
707 object_property_del_all(obj);
708 object_deinit(obj, ti);
710 g_assert(obj->ref == 0);
711 g_assert(obj->parent == NULL);
712 if (obj->free) {
713 obj->free(obj);
717 /* Find the minimum alignment guaranteed by the system malloc. */
718 #if __STDC_VERSION__ >= 201112L
719 typedef max_align_t qemu_max_align_t;
720 #else
721 typedef union {
722 long l;
723 void *p;
724 double d;
725 long double ld;
726 } qemu_max_align_t;
727 #endif
729 static Object *object_new_with_type(Type type)
731 Object *obj;
732 size_t size, align;
733 void (*obj_free)(void *);
735 g_assert(type != NULL);
736 type_initialize(type);
738 size = type->instance_size;
739 align = type->instance_align;
742 * Do not use qemu_memalign unless required. Depending on the
743 * implementation, extra alignment implies extra overhead.
745 if (likely(align <= __alignof__(qemu_max_align_t))) {
746 obj = g_malloc(size);
747 obj_free = g_free;
748 } else {
749 obj = qemu_memalign(align, size);
750 obj_free = qemu_vfree;
753 object_initialize_with_type(obj, size, type);
754 obj->free = obj_free;
756 return obj;
759 Object *object_new_with_class(ObjectClass *klass)
761 return object_new_with_type(klass->type);
764 Object *object_new(const char *typename)
766 TypeImpl *ti = type_get_by_name(typename);
768 return object_new_with_type(ti);
772 Object *object_new_with_props(const char *typename,
773 Object *parent,
774 const char *id,
775 Error **errp,
776 ...)
778 va_list vargs;
779 Object *obj;
781 va_start(vargs, errp);
782 obj = object_new_with_propv(typename, parent, id, errp, vargs);
783 va_end(vargs);
785 return obj;
789 Object *object_new_with_propv(const char *typename,
790 Object *parent,
791 const char *id,
792 Error **errp,
793 va_list vargs)
795 Object *obj;
796 ObjectClass *klass;
797 UserCreatable *uc;
799 klass = object_class_by_name(typename);
800 if (!klass) {
801 error_setg(errp, "invalid object type: %s", typename);
802 return NULL;
805 if (object_class_is_abstract(klass)) {
806 error_setg(errp, "object type '%s' is abstract", typename);
807 return NULL;
809 obj = object_new_with_type(klass->type);
811 if (!object_set_propv(obj, errp, vargs)) {
812 goto error;
815 if (id != NULL) {
816 object_property_add_child(parent, id, obj);
819 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
820 if (uc) {
821 if (!user_creatable_complete(uc, errp)) {
822 if (id != NULL) {
823 object_unparent(obj);
825 goto error;
829 object_unref(obj);
830 return obj;
832 error:
833 object_unref(obj);
834 return NULL;
838 bool object_set_props(Object *obj,
839 Error **errp,
840 ...)
842 va_list vargs;
843 bool ret;
845 va_start(vargs, errp);
846 ret = object_set_propv(obj, errp, vargs);
847 va_end(vargs);
849 return ret;
853 bool object_set_propv(Object *obj,
854 Error **errp,
855 va_list vargs)
857 const char *propname;
859 propname = va_arg(vargs, char *);
860 while (propname != NULL) {
861 const char *value = va_arg(vargs, char *);
863 g_assert(value != NULL);
864 if (!object_property_parse(obj, propname, value, errp)) {
865 return false;
867 propname = va_arg(vargs, char *);
870 return true;
874 Object *object_dynamic_cast(Object *obj, const char *typename)
876 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
877 return obj;
880 return NULL;
883 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
884 const char *file, int line, const char *func)
886 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
887 typename, file, line, func);
889 #ifdef CONFIG_QOM_CAST_DEBUG
890 int i;
891 Object *inst;
893 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
894 if (qatomic_read(&obj->class->object_cast_cache[i]) == typename) {
895 goto out;
899 inst = object_dynamic_cast(obj, typename);
901 if (!inst && obj) {
902 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
903 file, line, func, obj, typename);
904 abort();
907 assert(obj == inst);
909 if (obj && obj == inst) {
910 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
911 qatomic_set(&obj->class->object_cast_cache[i - 1],
912 qatomic_read(&obj->class->object_cast_cache[i]));
914 qatomic_set(&obj->class->object_cast_cache[i - 1], typename);
917 out:
918 #endif
919 return obj;
922 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
923 const char *typename)
925 ObjectClass *ret = NULL;
926 TypeImpl *target_type;
927 TypeImpl *type;
929 if (!class) {
930 return NULL;
933 /* A simple fast path that can trigger a lot for leaf classes. */
934 type = class->type;
935 if (type->name == typename) {
936 return class;
939 target_type = type_get_by_name(typename);
940 if (!target_type) {
941 /* target class type unknown, so fail the cast */
942 return NULL;
945 if (type->class->interfaces &&
946 type_is_ancestor(target_type, type_interface)) {
947 int found = 0;
948 GSList *i;
950 for (i = class->interfaces; i; i = i->next) {
951 ObjectClass *target_class = i->data;
953 if (type_is_ancestor(target_class->type, target_type)) {
954 ret = target_class;
955 found++;
959 /* The match was ambiguous, don't allow a cast */
960 if (found > 1) {
961 ret = NULL;
963 } else if (type_is_ancestor(type, target_type)) {
964 ret = class;
967 return ret;
970 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
971 const char *typename,
972 const char *file, int line,
973 const char *func)
975 ObjectClass *ret;
977 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
978 typename, file, line, func);
980 #ifdef CONFIG_QOM_CAST_DEBUG
981 int i;
983 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
984 if (qatomic_read(&class->class_cast_cache[i]) == typename) {
985 ret = class;
986 goto out;
989 #else
990 if (!class || !class->interfaces) {
991 return class;
993 #endif
995 ret = object_class_dynamic_cast(class, typename);
996 if (!ret && class) {
997 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
998 file, line, func, class, typename);
999 abort();
1002 #ifdef CONFIG_QOM_CAST_DEBUG
1003 if (class && ret == class) {
1004 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
1005 qatomic_set(&class->class_cast_cache[i - 1],
1006 qatomic_read(&class->class_cast_cache[i]));
1008 qatomic_set(&class->class_cast_cache[i - 1], typename);
1010 out:
1011 #endif
1012 return ret;
1015 const char *object_get_typename(const Object *obj)
1017 return obj->class->type->name;
1020 ObjectClass *object_get_class(Object *obj)
1022 return obj->class;
1025 bool object_class_is_abstract(ObjectClass *klass)
1027 return klass->type->abstract;
1030 const char *object_class_get_name(ObjectClass *klass)
1032 return klass->type->name;
1035 ObjectClass *object_class_by_name(const char *typename)
1037 TypeImpl *type = type_get_by_name(typename);
1039 if (!type) {
1040 return NULL;
1043 type_initialize(type);
1045 return type->class;
1048 ObjectClass *module_object_class_by_name(const char *typename)
1050 ObjectClass *oc;
1052 oc = object_class_by_name(typename);
1053 #ifdef CONFIG_MODULES
1054 if (!oc) {
1055 Error *local_err = NULL;
1056 int rv = module_load_qom(typename, &local_err);
1057 if (rv > 0) {
1058 oc = object_class_by_name(typename);
1059 } else if (rv < 0) {
1060 error_report_err(local_err);
1063 #endif
1064 return oc;
1067 ObjectClass *object_class_get_parent(ObjectClass *class)
1069 TypeImpl *type = type_get_parent(class->type);
1071 if (!type) {
1072 return NULL;
1075 type_initialize(type);
1077 return type->class;
1080 typedef struct OCFData
1082 void (*fn)(ObjectClass *klass, void *opaque);
1083 const char *implements_type;
1084 bool include_abstract;
1085 void *opaque;
1086 } OCFData;
1088 static void object_class_foreach_tramp(gpointer key, gpointer value,
1089 gpointer opaque)
1091 OCFData *data = opaque;
1092 TypeImpl *type = value;
1093 ObjectClass *k;
1095 type_initialize(type);
1096 k = type->class;
1098 if (!data->include_abstract && type->abstract) {
1099 return;
1102 if (data->implements_type &&
1103 !object_class_dynamic_cast(k, data->implements_type)) {
1104 return;
1107 data->fn(k, data->opaque);
1110 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1111 const char *implements_type, bool include_abstract,
1112 void *opaque)
1114 OCFData data = { fn, implements_type, include_abstract, opaque };
1116 enumerating_types = true;
1117 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
1118 enumerating_types = false;
1121 static int do_object_child_foreach(Object *obj,
1122 int (*fn)(Object *child, void *opaque),
1123 void *opaque, bool recurse)
1125 GHashTableIter iter;
1126 ObjectProperty *prop;
1127 int ret = 0;
1129 g_hash_table_iter_init(&iter, obj->properties);
1130 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
1131 if (object_property_is_child(prop)) {
1132 Object *child = prop->opaque;
1134 ret = fn(child, opaque);
1135 if (ret != 0) {
1136 break;
1138 if (recurse) {
1139 ret = do_object_child_foreach(child, fn, opaque, true);
1140 if (ret != 0) {
1141 break;
1146 return ret;
1149 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1150 void *opaque)
1152 return do_object_child_foreach(obj, fn, opaque, false);
1155 int object_child_foreach_recursive(Object *obj,
1156 int (*fn)(Object *child, void *opaque),
1157 void *opaque)
1159 return do_object_child_foreach(obj, fn, opaque, true);
1162 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1164 GSList **list = opaque;
1166 *list = g_slist_prepend(*list, klass);
1169 GSList *object_class_get_list(const char *implements_type,
1170 bool include_abstract)
1172 GSList *list = NULL;
1174 object_class_foreach(object_class_get_list_tramp,
1175 implements_type, include_abstract, &list);
1176 return list;
1179 static gint object_class_cmp(gconstpointer a, gconstpointer b)
1181 return strcasecmp(object_class_get_name((ObjectClass *)a),
1182 object_class_get_name((ObjectClass *)b));
1185 GSList *object_class_get_list_sorted(const char *implements_type,
1186 bool include_abstract)
1188 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1189 object_class_cmp);
1192 Object *object_ref(void *objptr)
1194 Object *obj = OBJECT(objptr);
1195 uint32_t ref;
1197 if (!obj) {
1198 return NULL;
1200 ref = qatomic_fetch_inc(&obj->ref);
1201 /* Assert waaay before the integer overflows */
1202 g_assert(ref < INT_MAX);
1203 return obj;
1206 void object_unref(void *objptr)
1208 Object *obj = OBJECT(objptr);
1209 if (!obj) {
1210 return;
1212 g_assert(obj->ref > 0);
1214 /* parent always holds a reference to its children */
1215 if (qatomic_fetch_dec(&obj->ref) == 1) {
1216 object_finalize(obj);
1220 ObjectProperty *
1221 object_property_try_add(Object *obj, const char *name, const char *type,
1222 ObjectPropertyAccessor *get,
1223 ObjectPropertyAccessor *set,
1224 ObjectPropertyRelease *release,
1225 void *opaque, Error **errp)
1227 ObjectProperty *prop;
1228 size_t name_len = strlen(name);
1230 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1231 int i;
1232 ObjectProperty *ret = NULL;
1233 char *name_no_array = g_strdup(name);
1235 name_no_array[name_len - 3] = '\0';
1236 for (i = 0; i < INT16_MAX; ++i) {
1237 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1239 ret = object_property_try_add(obj, full_name, type, get, set,
1240 release, opaque, NULL);
1241 g_free(full_name);
1242 if (ret) {
1243 break;
1246 g_free(name_no_array);
1247 assert(ret);
1248 return ret;
1251 if (object_property_find(obj, name) != NULL) {
1252 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1253 name, object_get_typename(obj));
1254 return NULL;
1257 prop = g_malloc0(sizeof(*prop));
1259 prop->name = g_strdup(name);
1260 prop->type = g_strdup(type);
1262 prop->get = get;
1263 prop->set = set;
1264 prop->release = release;
1265 prop->opaque = opaque;
1267 g_hash_table_insert(obj->properties, prop->name, prop);
1268 return prop;
1271 ObjectProperty *
1272 object_property_add(Object *obj, const char *name, const char *type,
1273 ObjectPropertyAccessor *get,
1274 ObjectPropertyAccessor *set,
1275 ObjectPropertyRelease *release,
1276 void *opaque)
1278 return object_property_try_add(obj, name, type, get, set, release,
1279 opaque, &error_abort);
1282 ObjectProperty *
1283 object_class_property_add(ObjectClass *klass,
1284 const char *name,
1285 const char *type,
1286 ObjectPropertyAccessor *get,
1287 ObjectPropertyAccessor *set,
1288 ObjectPropertyRelease *release,
1289 void *opaque)
1291 ObjectProperty *prop;
1293 assert(!object_class_property_find(klass, name));
1295 prop = g_malloc0(sizeof(*prop));
1297 prop->name = g_strdup(name);
1298 prop->type = g_strdup(type);
1300 prop->get = get;
1301 prop->set = set;
1302 prop->release = release;
1303 prop->opaque = opaque;
1305 g_hash_table_insert(klass->properties, prop->name, prop);
1307 return prop;
1310 ObjectProperty *object_property_find(Object *obj, const char *name)
1312 ObjectProperty *prop;
1313 ObjectClass *klass = object_get_class(obj);
1315 prop = object_class_property_find(klass, name);
1316 if (prop) {
1317 return prop;
1320 return g_hash_table_lookup(obj->properties, name);
1323 ObjectProperty *object_property_find_err(Object *obj, const char *name,
1324 Error **errp)
1326 ObjectProperty *prop = object_property_find(obj, name);
1327 if (!prop) {
1328 error_setg(errp, "Property '%s.%s' not found",
1329 object_get_typename(obj), name);
1331 return prop;
1334 void object_property_iter_init(ObjectPropertyIterator *iter,
1335 Object *obj)
1337 g_hash_table_iter_init(&iter->iter, obj->properties);
1338 iter->nextclass = object_get_class(obj);
1341 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1343 gpointer key, val;
1344 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1345 if (!iter->nextclass) {
1346 return NULL;
1348 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1349 iter->nextclass = object_class_get_parent(iter->nextclass);
1351 return val;
1354 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1355 ObjectClass *klass)
1357 g_hash_table_iter_init(&iter->iter, klass->properties);
1358 iter->nextclass = object_class_get_parent(klass);
1361 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name)
1363 ObjectClass *parent_klass;
1365 parent_klass = object_class_get_parent(klass);
1366 if (parent_klass) {
1367 ObjectProperty *prop =
1368 object_class_property_find(parent_klass, name);
1369 if (prop) {
1370 return prop;
1374 return g_hash_table_lookup(klass->properties, name);
1377 ObjectProperty *object_class_property_find_err(ObjectClass *klass,
1378 const char *name,
1379 Error **errp)
1381 ObjectProperty *prop = object_class_property_find(klass, name);
1382 if (!prop) {
1383 error_setg(errp, "Property '.%s' not found", name);
1385 return prop;
1389 void object_property_del(Object *obj, const char *name)
1391 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1393 if (prop->release) {
1394 prop->release(obj, name, prop->opaque);
1396 g_hash_table_remove(obj->properties, name);
1399 bool object_property_get(Object *obj, const char *name, Visitor *v,
1400 Error **errp)
1402 Error *err = NULL;
1403 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1405 if (prop == NULL) {
1406 return false;
1409 if (!prop->get) {
1410 error_setg(errp, "Property '%s.%s' is not readable",
1411 object_get_typename(obj), name);
1412 return false;
1414 prop->get(obj, v, name, prop->opaque, &err);
1415 error_propagate(errp, err);
1416 return !err;
1419 bool object_property_set(Object *obj, const char *name, Visitor *v,
1420 Error **errp)
1422 ERRP_GUARD();
1423 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1425 if (prop == NULL) {
1426 return false;
1429 if (!prop->set) {
1430 error_setg(errp, "Property '%s.%s' is not writable",
1431 object_get_typename(obj), name);
1432 return false;
1434 prop->set(obj, v, name, prop->opaque, errp);
1435 return !*errp;
1438 bool object_property_set_str(Object *obj, const char *name,
1439 const char *value, Error **errp)
1441 QString *qstr = qstring_from_str(value);
1442 bool ok = object_property_set_qobject(obj, name, QOBJECT(qstr), errp);
1444 qobject_unref(qstr);
1445 return ok;
1448 char *object_property_get_str(Object *obj, const char *name,
1449 Error **errp)
1451 QObject *ret = object_property_get_qobject(obj, name, errp);
1452 QString *qstring;
1453 char *retval;
1455 if (!ret) {
1456 return NULL;
1458 qstring = qobject_to(QString, ret);
1459 if (!qstring) {
1460 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
1461 retval = NULL;
1462 } else {
1463 retval = g_strdup(qstring_get_str(qstring));
1466 qobject_unref(ret);
1467 return retval;
1470 bool object_property_set_link(Object *obj, const char *name,
1471 Object *value, Error **errp)
1473 g_autofree char *path = NULL;
1475 if (value) {
1476 path = object_get_canonical_path(value);
1478 return object_property_set_str(obj, name, path ?: "", errp);
1481 Object *object_property_get_link(Object *obj, const char *name,
1482 Error **errp)
1484 char *str = object_property_get_str(obj, name, errp);
1485 Object *target = NULL;
1487 if (str && *str) {
1488 target = object_resolve_path(str, NULL);
1489 if (!target) {
1490 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1491 "Device '%s' not found", str);
1495 g_free(str);
1496 return target;
1499 bool object_property_set_bool(Object *obj, const char *name,
1500 bool value, Error **errp)
1502 QBool *qbool = qbool_from_bool(value);
1503 bool ok = object_property_set_qobject(obj, name, QOBJECT(qbool), errp);
1505 qobject_unref(qbool);
1506 return ok;
1509 bool object_property_get_bool(Object *obj, const char *name,
1510 Error **errp)
1512 QObject *ret = object_property_get_qobject(obj, name, errp);
1513 QBool *qbool;
1514 bool retval;
1516 if (!ret) {
1517 return false;
1519 qbool = qobject_to(QBool, ret);
1520 if (!qbool) {
1521 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
1522 retval = false;
1523 } else {
1524 retval = qbool_get_bool(qbool);
1527 qobject_unref(ret);
1528 return retval;
1531 bool object_property_set_int(Object *obj, const char *name,
1532 int64_t value, Error **errp)
1534 QNum *qnum = qnum_from_int(value);
1535 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1537 qobject_unref(qnum);
1538 return ok;
1541 int64_t object_property_get_int(Object *obj, const char *name,
1542 Error **errp)
1544 QObject *ret = object_property_get_qobject(obj, name, errp);
1545 QNum *qnum;
1546 int64_t retval;
1548 if (!ret) {
1549 return -1;
1552 qnum = qobject_to(QNum, ret);
1553 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
1554 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
1555 retval = -1;
1558 qobject_unref(ret);
1559 return retval;
1562 static void object_property_init_defval(Object *obj, ObjectProperty *prop)
1564 Visitor *v = qobject_input_visitor_new(prop->defval);
1566 assert(prop->set != NULL);
1567 prop->set(obj, v, prop->name, prop->opaque, &error_abort);
1569 visit_free(v);
1572 static void object_property_set_default(ObjectProperty *prop, QObject *defval)
1574 assert(!prop->defval);
1575 assert(!prop->init);
1577 prop->defval = defval;
1578 prop->init = object_property_init_defval;
1581 void object_property_set_default_bool(ObjectProperty *prop, bool value)
1583 object_property_set_default(prop, QOBJECT(qbool_from_bool(value)));
1586 void object_property_set_default_str(ObjectProperty *prop, const char *value)
1588 object_property_set_default(prop, QOBJECT(qstring_from_str(value)));
1591 void object_property_set_default_int(ObjectProperty *prop, int64_t value)
1593 object_property_set_default(prop, QOBJECT(qnum_from_int(value)));
1596 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value)
1598 object_property_set_default(prop, QOBJECT(qnum_from_uint(value)));
1601 bool object_property_set_uint(Object *obj, const char *name,
1602 uint64_t value, Error **errp)
1604 QNum *qnum = qnum_from_uint(value);
1605 bool ok = object_property_set_qobject(obj, name, QOBJECT(qnum), errp);
1607 qobject_unref(qnum);
1608 return ok;
1611 uint64_t object_property_get_uint(Object *obj, const char *name,
1612 Error **errp)
1614 QObject *ret = object_property_get_qobject(obj, name, errp);
1615 QNum *qnum;
1616 uint64_t retval;
1618 if (!ret) {
1619 return 0;
1621 qnum = qobject_to(QNum, ret);
1622 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1623 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1624 retval = 0;
1627 qobject_unref(ret);
1628 return retval;
1631 typedef struct EnumProperty {
1632 const QEnumLookup *lookup;
1633 int (*get)(Object *, Error **);
1634 void (*set)(Object *, int, Error **);
1635 } EnumProperty;
1637 int object_property_get_enum(Object *obj, const char *name,
1638 const char *typename, Error **errp)
1640 char *str;
1641 int ret;
1642 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1643 EnumProperty *enumprop;
1645 if (prop == NULL) {
1646 return -1;
1649 if (!g_str_equal(prop->type, typename)) {
1650 error_setg(errp, "Property %s on %s is not '%s' enum type",
1651 name, object_class_get_name(
1652 object_get_class(obj)), typename);
1653 return -1;
1656 enumprop = prop->opaque;
1658 str = object_property_get_str(obj, name, errp);
1659 if (!str) {
1660 return -1;
1663 ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
1664 g_free(str);
1666 return ret;
1669 bool object_property_parse(Object *obj, const char *name,
1670 const char *string, Error **errp)
1672 Visitor *v = string_input_visitor_new(string);
1673 bool ok = object_property_set(obj, name, v, errp);
1675 visit_free(v);
1676 return ok;
1679 char *object_property_print(Object *obj, const char *name, bool human,
1680 Error **errp)
1682 Visitor *v;
1683 char *string = NULL;
1685 v = string_output_visitor_new(human, &string);
1686 if (!object_property_get(obj, name, v, errp)) {
1687 goto out;
1690 visit_complete(v, &string);
1692 out:
1693 visit_free(v);
1694 return string;
1697 const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1699 ObjectProperty *prop = object_property_find_err(obj, name, errp);
1700 if (prop == NULL) {
1701 return NULL;
1704 return prop->type;
1707 Object *object_get_root(void)
1709 static Object *root;
1711 if (!root) {
1712 root = object_new("container");
1715 return root;
1718 Object *object_get_objects_root(void)
1720 return container_get(object_get_root(), "/objects");
1723 Object *object_get_internal_root(void)
1725 static Object *internal_root;
1727 if (!internal_root) {
1728 internal_root = object_new("container");
1731 return internal_root;
1734 static void object_get_child_property(Object *obj, Visitor *v,
1735 const char *name, void *opaque,
1736 Error **errp)
1738 Object *child = opaque;
1739 char *path;
1741 path = object_get_canonical_path(child);
1742 visit_type_str(v, name, &path, errp);
1743 g_free(path);
1746 static Object *object_resolve_child_property(Object *parent, void *opaque,
1747 const char *part)
1749 return opaque;
1752 static void object_finalize_child_property(Object *obj, const char *name,
1753 void *opaque)
1755 Object *child = opaque;
1757 if (child->class->unparent) {
1758 (child->class->unparent)(child);
1760 child->parent = NULL;
1761 object_unref(child);
1764 ObjectProperty *
1765 object_property_try_add_child(Object *obj, const char *name,
1766 Object *child, Error **errp)
1768 g_autofree char *type = NULL;
1769 ObjectProperty *op;
1771 assert(!child->parent);
1773 type = g_strdup_printf("child<%s>", object_get_typename(child));
1775 op = object_property_try_add(obj, name, type, object_get_child_property,
1776 NULL, object_finalize_child_property,
1777 child, errp);
1778 if (!op) {
1779 return NULL;
1781 op->resolve = object_resolve_child_property;
1782 object_ref(child);
1783 child->parent = obj;
1784 return op;
1787 ObjectProperty *
1788 object_property_add_child(Object *obj, const char *name,
1789 Object *child)
1791 return object_property_try_add_child(obj, name, child, &error_abort);
1794 void object_property_allow_set_link(const Object *obj, const char *name,
1795 Object *val, Error **errp)
1797 /* Allow the link to be set, always */
1800 typedef struct {
1801 union {
1802 Object **targetp;
1803 Object *target; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1804 ptrdiff_t offset; /* if OBJ_PROP_LINK_CLASS */
1806 void (*check)(const Object *, const char *, Object *, Error **);
1807 ObjectPropertyLinkFlags flags;
1808 } LinkProperty;
1810 static Object **
1811 object_link_get_targetp(Object *obj, LinkProperty *lprop)
1813 if (lprop->flags & OBJ_PROP_LINK_DIRECT) {
1814 return &lprop->target;
1815 } else if (lprop->flags & OBJ_PROP_LINK_CLASS) {
1816 return (void *)obj + lprop->offset;
1817 } else {
1818 return lprop->targetp;
1822 static void object_get_link_property(Object *obj, Visitor *v,
1823 const char *name, void *opaque,
1824 Error **errp)
1826 LinkProperty *lprop = opaque;
1827 Object **targetp = object_link_get_targetp(obj, lprop);
1828 char *path;
1830 if (*targetp) {
1831 path = object_get_canonical_path(*targetp);
1832 visit_type_str(v, name, &path, errp);
1833 g_free(path);
1834 } else {
1835 path = (char *)"";
1836 visit_type_str(v, name, &path, errp);
1841 * object_resolve_link:
1843 * Lookup an object and ensure its type matches the link property type. This
1844 * is similar to object_resolve_path() except type verification against the
1845 * link property is performed.
1847 * Returns: The matched object or NULL on path lookup failures.
1849 static Object *object_resolve_link(Object *obj, const char *name,
1850 const char *path, Error **errp)
1852 const char *type;
1853 char *target_type;
1854 bool ambiguous = false;
1855 Object *target;
1857 /* Go from link<FOO> to FOO. */
1858 type = object_property_get_type(obj, name, NULL);
1859 target_type = g_strndup(&type[5], strlen(type) - 6);
1860 target = object_resolve_path_type(path, target_type, &ambiguous);
1862 if (ambiguous) {
1863 error_setg(errp, "Path '%s' does not uniquely identify an object",
1864 path);
1865 } else if (!target) {
1866 target = object_resolve_path(path, &ambiguous);
1867 if (target || ambiguous) {
1868 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1869 } else {
1870 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1871 "Device '%s' not found", path);
1873 target = NULL;
1875 g_free(target_type);
1877 return target;
1880 static void object_set_link_property(Object *obj, Visitor *v,
1881 const char *name, void *opaque,
1882 Error **errp)
1884 Error *local_err = NULL;
1885 LinkProperty *prop = opaque;
1886 Object **targetp = object_link_get_targetp(obj, prop);
1887 Object *old_target = *targetp;
1888 Object *new_target;
1889 char *path = NULL;
1891 if (!visit_type_str(v, name, &path, errp)) {
1892 return;
1895 if (*path) {
1896 new_target = object_resolve_link(obj, name, path, errp);
1897 if (!new_target) {
1898 g_free(path);
1899 return;
1901 } else {
1902 new_target = NULL;
1905 g_free(path);
1907 prop->check(obj, name, new_target, &local_err);
1908 if (local_err) {
1909 error_propagate(errp, local_err);
1910 return;
1913 *targetp = new_target;
1914 if (prop->flags & OBJ_PROP_LINK_STRONG) {
1915 object_ref(new_target);
1916 object_unref(old_target);
1920 static Object *object_resolve_link_property(Object *parent, void *opaque,
1921 const char *part)
1923 LinkProperty *lprop = opaque;
1925 return *object_link_get_targetp(parent, lprop);
1928 static void object_release_link_property(Object *obj, const char *name,
1929 void *opaque)
1931 LinkProperty *prop = opaque;
1932 Object **targetp = object_link_get_targetp(obj, prop);
1934 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *targetp) {
1935 object_unref(*targetp);
1937 if (!(prop->flags & OBJ_PROP_LINK_CLASS)) {
1938 g_free(prop);
1942 static ObjectProperty *
1943 object_add_link_prop(Object *obj, const char *name,
1944 const char *type, void *ptr,
1945 void (*check)(const Object *, const char *,
1946 Object *, Error **),
1947 ObjectPropertyLinkFlags flags)
1949 LinkProperty *prop = g_malloc(sizeof(*prop));
1950 g_autofree char *full_type = NULL;
1951 ObjectProperty *op;
1953 if (flags & OBJ_PROP_LINK_DIRECT) {
1954 prop->target = ptr;
1955 } else {
1956 prop->targetp = ptr;
1958 prop->check = check;
1959 prop->flags = flags;
1961 full_type = g_strdup_printf("link<%s>", type);
1963 op = object_property_add(obj, name, full_type,
1964 object_get_link_property,
1965 check ? object_set_link_property : NULL,
1966 object_release_link_property,
1967 prop);
1968 op->resolve = object_resolve_link_property;
1969 return op;
1972 ObjectProperty *
1973 object_property_add_link(Object *obj, const char *name,
1974 const char *type, Object **targetp,
1975 void (*check)(const Object *, const char *,
1976 Object *, Error **),
1977 ObjectPropertyLinkFlags flags)
1979 return object_add_link_prop(obj, name, type, targetp, check, flags);
1982 ObjectProperty *
1983 object_class_property_add_link(ObjectClass *oc,
1984 const char *name,
1985 const char *type, ptrdiff_t offset,
1986 void (*check)(const Object *obj, const char *name,
1987 Object *val, Error **errp),
1988 ObjectPropertyLinkFlags flags)
1990 LinkProperty *prop = g_new0(LinkProperty, 1);
1991 char *full_type;
1992 ObjectProperty *op;
1994 prop->offset = offset;
1995 prop->check = check;
1996 prop->flags = flags | OBJ_PROP_LINK_CLASS;
1998 full_type = g_strdup_printf("link<%s>", type);
2000 op = object_class_property_add(oc, name, full_type,
2001 object_get_link_property,
2002 check ? object_set_link_property : NULL,
2003 object_release_link_property,
2004 prop);
2006 op->resolve = object_resolve_link_property;
2008 g_free(full_type);
2009 return op;
2012 ObjectProperty *
2013 object_property_add_const_link(Object *obj, const char *name,
2014 Object *target)
2016 return object_add_link_prop(obj, name,
2017 object_get_typename(target), target,
2018 NULL, OBJ_PROP_LINK_DIRECT);
2021 const char *object_get_canonical_path_component(const Object *obj)
2023 ObjectProperty *prop = NULL;
2024 GHashTableIter iter;
2026 if (obj->parent == NULL) {
2027 return NULL;
2030 g_hash_table_iter_init(&iter, obj->parent->properties);
2031 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2032 if (!object_property_is_child(prop)) {
2033 continue;
2036 if (prop->opaque == obj) {
2037 return prop->name;
2041 /* obj had a parent but was not a child, should never happen */
2042 g_assert_not_reached();
2043 return NULL;
2046 char *object_get_canonical_path(const Object *obj)
2048 Object *root = object_get_root();
2049 char *newpath, *path = NULL;
2051 if (obj == root) {
2052 return g_strdup("/");
2055 do {
2056 const char *component = object_get_canonical_path_component(obj);
2058 if (!component) {
2059 /* A canonical path must be complete, so discard what was
2060 * collected so far.
2062 g_free(path);
2063 return NULL;
2066 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
2067 g_free(path);
2068 path = newpath;
2069 obj = obj->parent;
2070 } while (obj != root);
2072 return path;
2075 Object *object_resolve_path_component(Object *parent, const char *part)
2077 ObjectProperty *prop = object_property_find(parent, part);
2078 if (prop == NULL) {
2079 return NULL;
2082 if (prop->resolve) {
2083 return prop->resolve(parent, prop->opaque, part);
2084 } else {
2085 return NULL;
2089 static Object *object_resolve_abs_path(Object *parent,
2090 char **parts,
2091 const char *typename)
2093 Object *child;
2095 if (*parts == NULL) {
2096 return object_dynamic_cast(parent, typename);
2099 if (strcmp(*parts, "") == 0) {
2100 return object_resolve_abs_path(parent, parts + 1, typename);
2103 child = object_resolve_path_component(parent, *parts);
2104 if (!child) {
2105 return NULL;
2108 return object_resolve_abs_path(child, parts + 1, typename);
2111 static Object *object_resolve_partial_path(Object *parent,
2112 char **parts,
2113 const char *typename,
2114 bool *ambiguous)
2116 Object *obj;
2117 GHashTableIter iter;
2118 ObjectProperty *prop;
2120 obj = object_resolve_abs_path(parent, parts, typename);
2122 g_hash_table_iter_init(&iter, parent->properties);
2123 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
2124 Object *found;
2126 if (!object_property_is_child(prop)) {
2127 continue;
2130 found = object_resolve_partial_path(prop->opaque, parts,
2131 typename, ambiguous);
2132 if (found) {
2133 if (obj) {
2134 *ambiguous = true;
2135 return NULL;
2137 obj = found;
2140 if (*ambiguous) {
2141 return NULL;
2145 return obj;
2148 Object *object_resolve_path_type(const char *path, const char *typename,
2149 bool *ambiguousp)
2151 Object *obj;
2152 char **parts;
2154 parts = g_strsplit(path, "/", 0);
2155 assert(parts);
2157 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
2158 bool ambiguous = false;
2159 obj = object_resolve_partial_path(object_get_root(), parts,
2160 typename, &ambiguous);
2161 if (ambiguousp) {
2162 *ambiguousp = ambiguous;
2164 } else {
2165 obj = object_resolve_abs_path(object_get_root(), parts + 1, typename);
2168 g_strfreev(parts);
2170 return obj;
2173 Object *object_resolve_path(const char *path, bool *ambiguous)
2175 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
2178 Object *object_resolve_path_at(Object *parent, const char *path)
2180 g_auto(GStrv) parts = g_strsplit(path, "/", 0);
2182 if (*path == '/') {
2183 return object_resolve_abs_path(object_get_root(), parts + 1,
2184 TYPE_OBJECT);
2186 return object_resolve_abs_path(parent, parts, TYPE_OBJECT);
2189 typedef struct StringProperty
2191 char *(*get)(Object *, Error **);
2192 void (*set)(Object *, const char *, Error **);
2193 } StringProperty;
2195 static void property_get_str(Object *obj, Visitor *v, const char *name,
2196 void *opaque, Error **errp)
2198 StringProperty *prop = opaque;
2199 char *value;
2200 Error *err = NULL;
2202 value = prop->get(obj, &err);
2203 if (err) {
2204 error_propagate(errp, err);
2205 return;
2208 visit_type_str(v, name, &value, errp);
2209 g_free(value);
2212 static void property_set_str(Object *obj, Visitor *v, const char *name,
2213 void *opaque, Error **errp)
2215 StringProperty *prop = opaque;
2216 char *value;
2218 if (!visit_type_str(v, name, &value, errp)) {
2219 return;
2222 prop->set(obj, value, errp);
2223 g_free(value);
2226 static void property_release_data(Object *obj, const char *name,
2227 void *opaque)
2229 g_free(opaque);
2232 ObjectProperty *
2233 object_property_add_str(Object *obj, const char *name,
2234 char *(*get)(Object *, Error **),
2235 void (*set)(Object *, const char *, Error **))
2237 StringProperty *prop = g_malloc0(sizeof(*prop));
2239 prop->get = get;
2240 prop->set = set;
2242 return object_property_add(obj, name, "string",
2243 get ? property_get_str : NULL,
2244 set ? property_set_str : NULL,
2245 property_release_data,
2246 prop);
2249 ObjectProperty *
2250 object_class_property_add_str(ObjectClass *klass, const char *name,
2251 char *(*get)(Object *, Error **),
2252 void (*set)(Object *, const char *,
2253 Error **))
2255 StringProperty *prop = g_malloc0(sizeof(*prop));
2257 prop->get = get;
2258 prop->set = set;
2260 return object_class_property_add(klass, name, "string",
2261 get ? property_get_str : NULL,
2262 set ? property_set_str : NULL,
2263 NULL,
2264 prop);
2267 typedef struct BoolProperty
2269 bool (*get)(Object *, Error **);
2270 void (*set)(Object *, bool, Error **);
2271 } BoolProperty;
2273 static void property_get_bool(Object *obj, Visitor *v, const char *name,
2274 void *opaque, Error **errp)
2276 BoolProperty *prop = opaque;
2277 bool value;
2278 Error *err = NULL;
2280 value = prop->get(obj, &err);
2281 if (err) {
2282 error_propagate(errp, err);
2283 return;
2286 visit_type_bool(v, name, &value, errp);
2289 static void property_set_bool(Object *obj, Visitor *v, const char *name,
2290 void *opaque, Error **errp)
2292 BoolProperty *prop = opaque;
2293 bool value;
2295 if (!visit_type_bool(v, name, &value, errp)) {
2296 return;
2299 prop->set(obj, value, errp);
2302 ObjectProperty *
2303 object_property_add_bool(Object *obj, const char *name,
2304 bool (*get)(Object *, Error **),
2305 void (*set)(Object *, bool, Error **))
2307 BoolProperty *prop = g_malloc0(sizeof(*prop));
2309 prop->get = get;
2310 prop->set = set;
2312 return object_property_add(obj, name, "bool",
2313 get ? property_get_bool : NULL,
2314 set ? property_set_bool : NULL,
2315 property_release_data,
2316 prop);
2319 ObjectProperty *
2320 object_class_property_add_bool(ObjectClass *klass, const char *name,
2321 bool (*get)(Object *, Error **),
2322 void (*set)(Object *, bool, Error **))
2324 BoolProperty *prop = g_malloc0(sizeof(*prop));
2326 prop->get = get;
2327 prop->set = set;
2329 return object_class_property_add(klass, name, "bool",
2330 get ? property_get_bool : NULL,
2331 set ? property_set_bool : NULL,
2332 NULL,
2333 prop);
2336 static void property_get_enum(Object *obj, Visitor *v, const char *name,
2337 void *opaque, Error **errp)
2339 EnumProperty *prop = opaque;
2340 int value;
2341 Error *err = NULL;
2343 value = prop->get(obj, &err);
2344 if (err) {
2345 error_propagate(errp, err);
2346 return;
2349 visit_type_enum(v, name, &value, prop->lookup, errp);
2352 static void property_set_enum(Object *obj, Visitor *v, const char *name,
2353 void *opaque, Error **errp)
2355 EnumProperty *prop = opaque;
2356 int value;
2358 if (!visit_type_enum(v, name, &value, prop->lookup, errp)) {
2359 return;
2361 prop->set(obj, value, errp);
2364 ObjectProperty *
2365 object_property_add_enum(Object *obj, const char *name,
2366 const char *typename,
2367 const QEnumLookup *lookup,
2368 int (*get)(Object *, Error **),
2369 void (*set)(Object *, int, Error **))
2371 EnumProperty *prop = g_malloc(sizeof(*prop));
2373 prop->lookup = lookup;
2374 prop->get = get;
2375 prop->set = set;
2377 return object_property_add(obj, name, typename,
2378 get ? property_get_enum : NULL,
2379 set ? property_set_enum : NULL,
2380 property_release_data,
2381 prop);
2384 ObjectProperty *
2385 object_class_property_add_enum(ObjectClass *klass, const char *name,
2386 const char *typename,
2387 const QEnumLookup *lookup,
2388 int (*get)(Object *, Error **),
2389 void (*set)(Object *, int, Error **))
2391 EnumProperty *prop = g_malloc(sizeof(*prop));
2393 prop->lookup = lookup;
2394 prop->get = get;
2395 prop->set = set;
2397 return object_class_property_add(klass, name, typename,
2398 get ? property_get_enum : NULL,
2399 set ? property_set_enum : NULL,
2400 NULL,
2401 prop);
2404 typedef struct TMProperty {
2405 void (*get)(Object *, struct tm *, Error **);
2406 } TMProperty;
2408 static void property_get_tm(Object *obj, Visitor *v, const char *name,
2409 void *opaque, Error **errp)
2411 TMProperty *prop = opaque;
2412 Error *err = NULL;
2413 struct tm value;
2415 prop->get(obj, &value, &err);
2416 if (err) {
2417 error_propagate(errp, err);
2418 return;
2421 if (!visit_start_struct(v, name, NULL, 0, errp)) {
2422 return;
2424 if (!visit_type_int32(v, "tm_year", &value.tm_year, errp)) {
2425 goto out_end;
2427 if (!visit_type_int32(v, "tm_mon", &value.tm_mon, errp)) {
2428 goto out_end;
2430 if (!visit_type_int32(v, "tm_mday", &value.tm_mday, errp)) {
2431 goto out_end;
2433 if (!visit_type_int32(v, "tm_hour", &value.tm_hour, errp)) {
2434 goto out_end;
2436 if (!visit_type_int32(v, "tm_min", &value.tm_min, errp)) {
2437 goto out_end;
2439 if (!visit_type_int32(v, "tm_sec", &value.tm_sec, errp)) {
2440 goto out_end;
2442 visit_check_struct(v, errp);
2443 out_end:
2444 visit_end_struct(v, NULL);
2447 ObjectProperty *
2448 object_property_add_tm(Object *obj, const char *name,
2449 void (*get)(Object *, struct tm *, Error **))
2451 TMProperty *prop = g_malloc0(sizeof(*prop));
2453 prop->get = get;
2455 return object_property_add(obj, name, "struct tm",
2456 get ? property_get_tm : NULL, NULL,
2457 property_release_data,
2458 prop);
2461 ObjectProperty *
2462 object_class_property_add_tm(ObjectClass *klass, const char *name,
2463 void (*get)(Object *, struct tm *, Error **))
2465 TMProperty *prop = g_malloc0(sizeof(*prop));
2467 prop->get = get;
2469 return object_class_property_add(klass, name, "struct tm",
2470 get ? property_get_tm : NULL,
2471 NULL, NULL, prop);
2474 static char *object_get_type(Object *obj, Error **errp)
2476 return g_strdup(object_get_typename(obj));
2479 static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2480 void *opaque, Error **errp)
2482 uint8_t value = *(uint8_t *)opaque;
2483 visit_type_uint8(v, name, &value, errp);
2486 static void property_set_uint8_ptr(Object *obj, Visitor *v, const char *name,
2487 void *opaque, Error **errp)
2489 uint8_t *field = opaque;
2490 uint8_t value;
2492 if (!visit_type_uint8(v, name, &value, errp)) {
2493 return;
2496 *field = value;
2499 static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2500 void *opaque, Error **errp)
2502 uint16_t value = *(uint16_t *)opaque;
2503 visit_type_uint16(v, name, &value, errp);
2506 static void property_set_uint16_ptr(Object *obj, Visitor *v, const char *name,
2507 void *opaque, Error **errp)
2509 uint16_t *field = opaque;
2510 uint16_t value;
2512 if (!visit_type_uint16(v, name, &value, errp)) {
2513 return;
2516 *field = value;
2519 static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2520 void *opaque, Error **errp)
2522 uint32_t value = *(uint32_t *)opaque;
2523 visit_type_uint32(v, name, &value, errp);
2526 static void property_set_uint32_ptr(Object *obj, Visitor *v, const char *name,
2527 void *opaque, Error **errp)
2529 uint32_t *field = opaque;
2530 uint32_t value;
2532 if (!visit_type_uint32(v, name, &value, errp)) {
2533 return;
2536 *field = value;
2539 static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2540 void *opaque, Error **errp)
2542 uint64_t value = *(uint64_t *)opaque;
2543 visit_type_uint64(v, name, &value, errp);
2546 static void property_set_uint64_ptr(Object *obj, Visitor *v, const char *name,
2547 void *opaque, Error **errp)
2549 uint64_t *field = opaque;
2550 uint64_t value;
2552 if (!visit_type_uint64(v, name, &value, errp)) {
2553 return;
2556 *field = value;
2559 ObjectProperty *
2560 object_property_add_uint8_ptr(Object *obj, const char *name,
2561 const uint8_t *v,
2562 ObjectPropertyFlags flags)
2564 ObjectPropertyAccessor *getter = NULL;
2565 ObjectPropertyAccessor *setter = NULL;
2567 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2568 getter = property_get_uint8_ptr;
2571 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2572 setter = property_set_uint8_ptr;
2575 return object_property_add(obj, name, "uint8",
2576 getter, setter, NULL, (void *)v);
2579 ObjectProperty *
2580 object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2581 const uint8_t *v,
2582 ObjectPropertyFlags flags)
2584 ObjectPropertyAccessor *getter = NULL;
2585 ObjectPropertyAccessor *setter = NULL;
2587 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2588 getter = property_get_uint8_ptr;
2591 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2592 setter = property_set_uint8_ptr;
2595 return object_class_property_add(klass, name, "uint8",
2596 getter, setter, NULL, (void *)v);
2599 ObjectProperty *
2600 object_property_add_uint16_ptr(Object *obj, const char *name,
2601 const uint16_t *v,
2602 ObjectPropertyFlags flags)
2604 ObjectPropertyAccessor *getter = NULL;
2605 ObjectPropertyAccessor *setter = NULL;
2607 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2608 getter = property_get_uint16_ptr;
2611 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2612 setter = property_set_uint16_ptr;
2615 return object_property_add(obj, name, "uint16",
2616 getter, setter, NULL, (void *)v);
2619 ObjectProperty *
2620 object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2621 const uint16_t *v,
2622 ObjectPropertyFlags flags)
2624 ObjectPropertyAccessor *getter = NULL;
2625 ObjectPropertyAccessor *setter = NULL;
2627 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2628 getter = property_get_uint16_ptr;
2631 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2632 setter = property_set_uint16_ptr;
2635 return object_class_property_add(klass, name, "uint16",
2636 getter, setter, NULL, (void *)v);
2639 ObjectProperty *
2640 object_property_add_uint32_ptr(Object *obj, const char *name,
2641 const uint32_t *v,
2642 ObjectPropertyFlags flags)
2644 ObjectPropertyAccessor *getter = NULL;
2645 ObjectPropertyAccessor *setter = NULL;
2647 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2648 getter = property_get_uint32_ptr;
2651 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2652 setter = property_set_uint32_ptr;
2655 return object_property_add(obj, name, "uint32",
2656 getter, setter, NULL, (void *)v);
2659 ObjectProperty *
2660 object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2661 const uint32_t *v,
2662 ObjectPropertyFlags flags)
2664 ObjectPropertyAccessor *getter = NULL;
2665 ObjectPropertyAccessor *setter = NULL;
2667 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2668 getter = property_get_uint32_ptr;
2671 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2672 setter = property_set_uint32_ptr;
2675 return object_class_property_add(klass, name, "uint32",
2676 getter, setter, NULL, (void *)v);
2679 ObjectProperty *
2680 object_property_add_uint64_ptr(Object *obj, const char *name,
2681 const uint64_t *v,
2682 ObjectPropertyFlags flags)
2684 ObjectPropertyAccessor *getter = NULL;
2685 ObjectPropertyAccessor *setter = NULL;
2687 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2688 getter = property_get_uint64_ptr;
2691 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2692 setter = property_set_uint64_ptr;
2695 return object_property_add(obj, name, "uint64",
2696 getter, setter, NULL, (void *)v);
2699 ObjectProperty *
2700 object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2701 const uint64_t *v,
2702 ObjectPropertyFlags flags)
2704 ObjectPropertyAccessor *getter = NULL;
2705 ObjectPropertyAccessor *setter = NULL;
2707 if ((flags & OBJ_PROP_FLAG_READ) == OBJ_PROP_FLAG_READ) {
2708 getter = property_get_uint64_ptr;
2711 if ((flags & OBJ_PROP_FLAG_WRITE) == OBJ_PROP_FLAG_WRITE) {
2712 setter = property_set_uint64_ptr;
2715 return object_class_property_add(klass, name, "uint64",
2716 getter, setter, NULL, (void *)v);
2719 typedef struct {
2720 Object *target_obj;
2721 char *target_name;
2722 } AliasProperty;
2724 static void property_get_alias(Object *obj, Visitor *v, const char *name,
2725 void *opaque, Error **errp)
2727 AliasProperty *prop = opaque;
2728 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2730 object_property_get(prop->target_obj, prop->target_name, alias_v, errp);
2731 visit_free(alias_v);
2734 static void property_set_alias(Object *obj, Visitor *v, const char *name,
2735 void *opaque, Error **errp)
2737 AliasProperty *prop = opaque;
2738 Visitor *alias_v = visitor_forward_field(v, prop->target_name, name);
2740 object_property_set(prop->target_obj, prop->target_name, alias_v, errp);
2741 visit_free(alias_v);
2744 static Object *property_resolve_alias(Object *obj, void *opaque,
2745 const char *part)
2747 AliasProperty *prop = opaque;
2749 return object_resolve_path_component(prop->target_obj, prop->target_name);
2752 static void property_release_alias(Object *obj, const char *name, void *opaque)
2754 AliasProperty *prop = opaque;
2756 g_free(prop->target_name);
2757 g_free(prop);
2760 ObjectProperty *
2761 object_property_add_alias(Object *obj, const char *name,
2762 Object *target_obj, const char *target_name)
2764 AliasProperty *prop;
2765 ObjectProperty *op;
2766 ObjectProperty *target_prop;
2767 g_autofree char *prop_type = NULL;
2769 target_prop = object_property_find_err(target_obj, target_name,
2770 &error_abort);
2772 if (object_property_is_child(target_prop)) {
2773 prop_type = g_strdup_printf("link%s",
2774 target_prop->type + strlen("child"));
2775 } else {
2776 prop_type = g_strdup(target_prop->type);
2779 prop = g_malloc(sizeof(*prop));
2780 prop->target_obj = target_obj;
2781 prop->target_name = g_strdup(target_name);
2783 op = object_property_add(obj, name, prop_type,
2784 property_get_alias,
2785 property_set_alias,
2786 property_release_alias,
2787 prop);
2788 op->resolve = property_resolve_alias;
2789 if (target_prop->defval) {
2790 op->defval = qobject_ref(target_prop->defval);
2793 object_property_set_description(obj, op->name,
2794 target_prop->description);
2795 return op;
2798 void object_property_set_description(Object *obj, const char *name,
2799 const char *description)
2801 ObjectProperty *op;
2803 op = object_property_find_err(obj, name, &error_abort);
2804 g_free(op->description);
2805 op->description = g_strdup(description);
2808 void object_class_property_set_description(ObjectClass *klass,
2809 const char *name,
2810 const char *description)
2812 ObjectProperty *op;
2814 op = g_hash_table_lookup(klass->properties, name);
2815 g_free(op->description);
2816 op->description = g_strdup(description);
2819 static void object_class_init(ObjectClass *klass, void *data)
2821 object_class_property_add_str(klass, "type", object_get_type,
2822 NULL);
2825 static void register_types(void)
2827 static const TypeInfo interface_info = {
2828 .name = TYPE_INTERFACE,
2829 .class_size = sizeof(InterfaceClass),
2830 .abstract = true,
2833 static const TypeInfo object_info = {
2834 .name = TYPE_OBJECT,
2835 .instance_size = sizeof(Object),
2836 .class_init = object_class_init,
2837 .abstract = true,
2840 type_interface = type_register_internal(&interface_info);
2841 type_register_internal(&object_info);
2844 type_init(register_types)