4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/qapi-builtin-visit.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qmp/qjson.h"
28 /* TODO: replace QObject with a simpler visitor to avoid a dependency
29 * of the QOM core on QObject? */
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qemu/error-report.h"
36 #define MAX_INTERFACES 32
38 typedef struct InterfaceImpl InterfaceImpl
;
39 typedef struct TypeImpl TypeImpl
;
53 size_t instance_align
;
55 void (*class_init
)(ObjectClass
*klass
, void *data
);
56 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
60 void (*instance_init
)(Object
*obj
);
61 void (*instance_post_init
)(Object
*obj
);
62 void (*instance_finalize
)(Object
*obj
);
67 TypeImpl
*parent_type
;
72 InterfaceImpl interfaces
[MAX_INTERFACES
];
75 static Type type_interface
;
77 static GHashTable
*type_table_get(void)
79 static GHashTable
*type_table
;
81 if (type_table
== NULL
) {
82 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
88 static bool enumerating_types
;
90 static void type_table_add(TypeImpl
*ti
)
92 assert(!enumerating_types
);
93 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
96 static TypeImpl
*type_table_lookup(const char *name
)
98 return g_hash_table_lookup(type_table_get(), name
);
101 static TypeImpl
*type_new(const TypeInfo
*info
)
103 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
106 g_assert(info
->name
!= NULL
);
108 if (type_table_lookup(info
->name
) != NULL
) {
109 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
113 ti
->name
= g_strdup(info
->name
);
114 ti
->parent
= g_strdup(info
->parent
);
116 ti
->class_size
= info
->class_size
;
117 ti
->instance_size
= info
->instance_size
;
118 ti
->instance_align
= info
->instance_align
;
120 ti
->class_init
= info
->class_init
;
121 ti
->class_base_init
= info
->class_base_init
;
122 ti
->class_data
= info
->class_data
;
124 ti
->instance_init
= info
->instance_init
;
125 ti
->instance_post_init
= info
->instance_post_init
;
126 ti
->instance_finalize
= info
->instance_finalize
;
128 ti
->abstract
= info
->abstract
;
130 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
131 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
133 ti
->num_interfaces
= i
;
138 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
147 TypeImpl
*type_register(const TypeInfo
*info
)
149 assert(info
->parent
);
150 return type_register_internal(info
);
153 TypeImpl
*type_register_static(const TypeInfo
*info
)
155 return type_register(info
);
158 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
162 for (i
= 0; i
< nr_infos
; i
++) {
163 type_register_static(&infos
[i
]);
167 static TypeImpl
*type_get_by_name(const char *name
)
173 return type_table_lookup(name
);
176 static TypeImpl
*type_get_parent(TypeImpl
*type
)
178 if (!type
->parent_type
&& type
->parent
) {
179 type
->parent_type
= type_get_by_name(type
->parent
);
180 if (!type
->parent_type
) {
181 fprintf(stderr
, "Type '%s' is missing its parent '%s'\n",
182 type
->name
, type
->parent
);
187 return type
->parent_type
;
190 static bool type_has_parent(TypeImpl
*type
)
192 return (type
->parent
!= NULL
);
195 static size_t type_class_get_size(TypeImpl
*ti
)
197 if (ti
->class_size
) {
198 return ti
->class_size
;
201 if (type_has_parent(ti
)) {
202 return type_class_get_size(type_get_parent(ti
));
205 return sizeof(ObjectClass
);
208 static size_t type_object_get_size(TypeImpl
*ti
)
210 if (ti
->instance_size
) {
211 return ti
->instance_size
;
214 if (type_has_parent(ti
)) {
215 return type_object_get_size(type_get_parent(ti
));
221 size_t object_type_get_instance_size(const char *typename
)
223 TypeImpl
*type
= type_get_by_name(typename
);
225 g_assert(type
!= NULL
);
226 return type_object_get_size(type
);
229 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
233 /* Check if target_type is a direct ancestor of type */
235 if (type
== target_type
) {
239 type
= type_get_parent(type
);
245 static void type_initialize(TypeImpl
*ti
);
247 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
248 TypeImpl
*parent_type
)
250 InterfaceClass
*new_iface
;
252 TypeImpl
*iface_impl
;
254 info
.parent
= parent_type
->name
;
255 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
256 info
.abstract
= true;
258 iface_impl
= type_new(&info
);
259 iface_impl
->parent_type
= parent_type
;
260 type_initialize(iface_impl
);
261 g_free((char *)info
.name
);
263 new_iface
= (InterfaceClass
*)iface_impl
->class;
264 new_iface
->concrete_class
= ti
->class;
265 new_iface
->interface_type
= interface_type
;
267 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
, new_iface
);
270 static void object_property_free(gpointer data
)
272 ObjectProperty
*prop
= data
;
275 qobject_unref(prop
->defval
);
280 g_free(prop
->description
);
284 static void type_initialize(TypeImpl
*ti
)
292 ti
->class_size
= type_class_get_size(ti
);
293 ti
->instance_size
= type_object_get_size(ti
);
294 /* Any type with zero instance_size is implicitly abstract.
295 * This means interface types are all abstract.
297 if (ti
->instance_size
== 0) {
300 if (type_is_ancestor(ti
, type_interface
)) {
301 assert(ti
->instance_size
== 0);
302 assert(ti
->abstract
);
303 assert(!ti
->instance_init
);
304 assert(!ti
->instance_post_init
);
305 assert(!ti
->instance_finalize
);
306 assert(!ti
->num_interfaces
);
308 ti
->class = g_malloc0(ti
->class_size
);
310 parent
= type_get_parent(ti
);
312 type_initialize(parent
);
316 g_assert(parent
->class_size
<= ti
->class_size
);
317 g_assert(parent
->instance_size
<= ti
->instance_size
);
318 memcpy(ti
->class, parent
->class, parent
->class_size
);
319 ti
->class->interfaces
= NULL
;
321 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
322 InterfaceClass
*iface
= e
->data
;
323 ObjectClass
*klass
= OBJECT_CLASS(iface
);
325 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
328 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
329 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
331 error_report("missing interface '%s' for object '%s'",
332 ti
->interfaces
[i
].typename
, parent
->name
);
335 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
336 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
338 if (type_is_ancestor(target_type
, t
)) {
347 type_initialize_interface(ti
, t
, t
);
351 ti
->class->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
352 object_property_free
);
354 ti
->class->type
= ti
;
357 if (parent
->class_base_init
) {
358 parent
->class_base_init(ti
->class, ti
->class_data
);
360 parent
= type_get_parent(parent
);
363 if (ti
->class_init
) {
364 ti
->class_init(ti
->class, ti
->class_data
);
368 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
370 if (type_has_parent(ti
)) {
371 object_init_with_type(obj
, type_get_parent(ti
));
374 if (ti
->instance_init
) {
375 ti
->instance_init(obj
);
379 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
381 if (ti
->instance_post_init
) {
382 ti
->instance_post_init(obj
);
385 if (type_has_parent(ti
)) {
386 object_post_init_with_type(obj
, type_get_parent(ti
));
390 bool object_apply_global_props(Object
*obj
, const GPtrArray
*props
,
399 for (i
= 0; i
< props
->len
; i
++) {
400 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
403 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
406 if (p
->optional
&& !object_property_find(obj
, p
->property
)) {
410 if (!object_property_parse(obj
, p
->property
, p
->value
, &err
)) {
411 error_prepend(&err
, "can't apply global %s.%s=%s: ",
412 p
->driver
, p
->property
, p
->value
);
414 * If errp != NULL, propagate error and return.
415 * If errp == NULL, report a warning, but keep going
416 * with the remaining globals.
419 error_propagate(errp
, err
);
422 warn_report_err(err
);
431 * Global property defaults
432 * Slot 0: accelerator's global property defaults
433 * Slot 1: machine's global property defaults
434 * Slot 2: global properties from legacy command line option
435 * Each is a GPtrArray of of GlobalProperty.
436 * Applied in order, later entries override earlier ones.
438 static GPtrArray
*object_compat_props
[3];
441 * Retrieve @GPtrArray for global property defined with options
442 * other than "-global". These are generally used for syntactic
443 * sugar and legacy command line options.
445 void object_register_sugar_prop(const char *driver
, const char *prop
, const char *value
)
448 if (!object_compat_props
[2]) {
449 object_compat_props
[2] = g_ptr_array_new();
451 g
= g_new0(GlobalProperty
, 1);
452 g
->driver
= g_strdup(driver
);
453 g
->property
= g_strdup(prop
);
454 g
->value
= g_strdup(value
);
455 g_ptr_array_add(object_compat_props
[2], g
);
459 * Set machine's global property defaults to @compat_props.
460 * May be called at most once.
462 void object_set_machine_compat_props(GPtrArray
*compat_props
)
464 assert(!object_compat_props
[1]);
465 object_compat_props
[1] = compat_props
;
469 * Set accelerator's global property defaults to @compat_props.
470 * May be called at most once.
472 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
474 assert(!object_compat_props
[0]);
475 object_compat_props
[0] = compat_props
;
478 void object_apply_compat_props(Object
*obj
)
482 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
483 object_apply_global_props(obj
, object_compat_props
[i
],
484 i
== 2 ? &error_fatal
: &error_abort
);
488 static void object_class_property_init_all(Object
*obj
)
490 ObjectPropertyIterator iter
;
491 ObjectProperty
*prop
;
493 object_class_property_iter_init(&iter
, object_get_class(obj
));
494 while ((prop
= object_property_iter_next(&iter
))) {
496 prop
->init(obj
, prop
);
501 static void object_initialize_with_type(Object
*obj
, size_t size
, TypeImpl
*type
)
503 type_initialize(type
);
505 g_assert(type
->instance_size
>= sizeof(Object
));
506 g_assert(type
->abstract
== false);
507 g_assert(size
>= type
->instance_size
);
509 memset(obj
, 0, type
->instance_size
);
510 obj
->class = type
->class;
512 object_class_property_init_all(obj
);
513 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
514 NULL
, object_property_free
);
515 object_init_with_type(obj
, type
);
516 object_post_init_with_type(obj
, type
);
519 void object_initialize(void *data
, size_t size
, const char *typename
)
521 TypeImpl
*type
= type_get_by_name(typename
);
523 #ifdef CONFIG_MODULES
525 module_load_qom_one(typename
);
526 type
= type_get_by_name(typename
);
530 error_report("missing object type '%s'", typename
);
534 object_initialize_with_type(data
, size
, type
);
537 bool object_initialize_child_with_props(Object
*parentobj
,
538 const char *propname
,
539 void *childobj
, size_t size
,
546 va_start(vargs
, errp
);
547 ok
= object_initialize_child_with_propsv(parentobj
, propname
,
548 childobj
, size
, type
, errp
,
554 bool object_initialize_child_with_propsv(Object
*parentobj
,
555 const char *propname
,
556 void *childobj
, size_t size
,
558 Error
**errp
, va_list vargs
)
564 object_initialize(childobj
, size
, type
);
565 obj
= OBJECT(childobj
);
567 if (!object_set_propv(obj
, errp
, vargs
)) {
571 object_property_add_child(parentobj
, propname
, obj
);
573 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
575 if (!user_creatable_complete(uc
, errp
)) {
576 object_unparent(obj
);
585 * We want @obj's reference to be 1 on success, 0 on failure.
586 * On success, it's 2: one taken by object_initialize(), and one
587 * by object_property_add_child().
588 * On failure in object_initialize() or earlier, it's 1.
589 * On failure afterwards, it's also 1: object_unparent() releases
590 * the reference taken by object_property_add_child().
596 void object_initialize_child_internal(Object
*parent
,
597 const char *propname
,
598 void *child
, size_t size
,
601 object_initialize_child_with_props(parent
, propname
, child
, size
, type
,
605 static inline bool object_property_is_child(ObjectProperty
*prop
)
607 return strstart(prop
->type
, "child<", NULL
);
610 static void object_property_del_all(Object
*obj
)
612 g_autoptr(GHashTable
) done
= g_hash_table_new(NULL
, NULL
);
613 ObjectProperty
*prop
;
614 ObjectPropertyIterator iter
;
619 object_property_iter_init(&iter
, obj
);
620 while ((prop
= object_property_iter_next(&iter
)) != NULL
) {
621 if (g_hash_table_add(done
, prop
)) {
623 prop
->release(obj
, prop
->name
, prop
->opaque
);
631 g_hash_table_unref(obj
->properties
);
634 static void object_property_del_child(Object
*obj
, Object
*child
)
636 ObjectProperty
*prop
;
640 g_hash_table_iter_init(&iter
, obj
->properties
);
641 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
643 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
645 prop
->release(obj
, prop
->name
, prop
->opaque
);
646 prop
->release
= NULL
;
651 g_hash_table_iter_init(&iter
, obj
->properties
);
652 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
654 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
655 g_hash_table_iter_remove(&iter
);
661 void object_unparent(Object
*obj
)
664 object_property_del_child(obj
->parent
, obj
);
668 static void object_deinit(Object
*obj
, TypeImpl
*type
)
670 if (type
->instance_finalize
) {
671 type
->instance_finalize(obj
);
674 if (type_has_parent(type
)) {
675 object_deinit(obj
, type_get_parent(type
));
679 static void object_finalize(void *data
)
682 TypeImpl
*ti
= obj
->class->type
;
684 object_property_del_all(obj
);
685 object_deinit(obj
, ti
);
687 g_assert(obj
->ref
== 0);
693 /* Find the minimum alignment guaranteed by the system malloc. */
694 #if __STDC_VERSION__ >= 201112L
695 typddef max_align_t qemu_max_align_t
;
705 static Object
*object_new_with_type(Type type
)
709 void (*obj_free
)(void *);
711 g_assert(type
!= NULL
);
712 type_initialize(type
);
714 size
= type
->instance_size
;
715 align
= type
->instance_align
;
718 * Do not use qemu_memalign unless required. Depending on the
719 * implementation, extra alignment implies extra overhead.
721 if (likely(align
<= __alignof__(qemu_max_align_t
))) {
722 obj
= g_malloc(size
);
725 obj
= qemu_memalign(align
, size
);
726 obj_free
= qemu_vfree
;
729 object_initialize_with_type(obj
, size
, type
);
730 obj
->free
= obj_free
;
735 Object
*object_new_with_class(ObjectClass
*klass
)
737 return object_new_with_type(klass
->type
);
740 Object
*object_new(const char *typename
)
742 TypeImpl
*ti
= type_get_by_name(typename
);
744 return object_new_with_type(ti
);
748 Object
*object_new_with_props(const char *typename
,
757 va_start(vargs
, errp
);
758 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
765 Object
*object_new_with_propv(const char *typename
,
775 klass
= object_class_by_name(typename
);
777 error_setg(errp
, "invalid object type: %s", typename
);
781 if (object_class_is_abstract(klass
)) {
782 error_setg(errp
, "object type '%s' is abstract", typename
);
785 obj
= object_new_with_type(klass
->type
);
787 if (!object_set_propv(obj
, errp
, vargs
)) {
792 object_property_add_child(parent
, id
, obj
);
795 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
797 if (!user_creatable_complete(uc
, errp
)) {
799 object_unparent(obj
);
814 bool object_set_props(Object
*obj
,
821 va_start(vargs
, errp
);
822 ret
= object_set_propv(obj
, errp
, vargs
);
829 bool object_set_propv(Object
*obj
,
833 const char *propname
;
835 propname
= va_arg(vargs
, char *);
836 while (propname
!= NULL
) {
837 const char *value
= va_arg(vargs
, char *);
839 g_assert(value
!= NULL
);
840 if (!object_property_parse(obj
, propname
, value
, errp
)) {
843 propname
= va_arg(vargs
, char *);
850 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
852 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
859 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
860 const char *file
, int line
, const char *func
)
862 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
863 typename
, file
, line
, func
);
865 #ifdef CONFIG_QOM_CAST_DEBUG
869 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
870 if (qatomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
875 inst
= object_dynamic_cast(obj
, typename
);
878 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
879 file
, line
, func
, obj
, typename
);
885 if (obj
&& obj
== inst
) {
886 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
887 qatomic_set(&obj
->class->object_cast_cache
[i
- 1],
888 qatomic_read(&obj
->class->object_cast_cache
[i
]));
890 qatomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
898 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
899 const char *typename
)
901 ObjectClass
*ret
= NULL
;
902 TypeImpl
*target_type
;
909 /* A simple fast path that can trigger a lot for leaf classes. */
911 if (type
->name
== typename
) {
915 target_type
= type_get_by_name(typename
);
917 /* target class type unknown, so fail the cast */
921 if (type
->class->interfaces
&&
922 type_is_ancestor(target_type
, type_interface
)) {
926 for (i
= class->interfaces
; i
; i
= i
->next
) {
927 ObjectClass
*target_class
= i
->data
;
929 if (type_is_ancestor(target_class
->type
, target_type
)) {
935 /* The match was ambiguous, don't allow a cast */
939 } else if (type_is_ancestor(type
, target_type
)) {
946 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
947 const char *typename
,
948 const char *file
, int line
,
953 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
954 typename
, file
, line
, func
);
956 #ifdef CONFIG_QOM_CAST_DEBUG
959 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
960 if (qatomic_read(&class->class_cast_cache
[i
]) == typename
) {
966 if (!class || !class->interfaces
) {
971 ret
= object_class_dynamic_cast(class, typename
);
973 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
974 file
, line
, func
, class, typename
);
978 #ifdef CONFIG_QOM_CAST_DEBUG
979 if (class && ret
== class) {
980 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
981 qatomic_set(&class->class_cast_cache
[i
- 1],
982 qatomic_read(&class->class_cast_cache
[i
]));
984 qatomic_set(&class->class_cast_cache
[i
- 1], typename
);
991 const char *object_get_typename(const Object
*obj
)
993 return obj
->class->type
->name
;
996 ObjectClass
*object_get_class(Object
*obj
)
1001 bool object_class_is_abstract(ObjectClass
*klass
)
1003 return klass
->type
->abstract
;
1006 const char *object_class_get_name(ObjectClass
*klass
)
1008 return klass
->type
->name
;
1011 ObjectClass
*object_class_by_name(const char *typename
)
1013 TypeImpl
*type
= type_get_by_name(typename
);
1019 type_initialize(type
);
1024 ObjectClass
*module_object_class_by_name(const char *typename
)
1028 oc
= object_class_by_name(typename
);
1029 #ifdef CONFIG_MODULES
1031 module_load_qom_one(typename
);
1032 oc
= object_class_by_name(typename
);
1038 ObjectClass
*object_class_get_parent(ObjectClass
*class)
1040 TypeImpl
*type
= type_get_parent(class->type
);
1046 type_initialize(type
);
1051 typedef struct OCFData
1053 void (*fn
)(ObjectClass
*klass
, void *opaque
);
1054 const char *implements_type
;
1055 bool include_abstract
;
1059 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
1062 OCFData
*data
= opaque
;
1063 TypeImpl
*type
= value
;
1066 type_initialize(type
);
1069 if (!data
->include_abstract
&& type
->abstract
) {
1073 if (data
->implements_type
&&
1074 !object_class_dynamic_cast(k
, data
->implements_type
)) {
1078 data
->fn(k
, data
->opaque
);
1081 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1082 const char *implements_type
, bool include_abstract
,
1085 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1087 enumerating_types
= true;
1088 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1089 enumerating_types
= false;
1092 static int do_object_child_foreach(Object
*obj
,
1093 int (*fn
)(Object
*child
, void *opaque
),
1094 void *opaque
, bool recurse
)
1096 GHashTableIter iter
;
1097 ObjectProperty
*prop
;
1100 g_hash_table_iter_init(&iter
, obj
->properties
);
1101 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1102 if (object_property_is_child(prop
)) {
1103 Object
*child
= prop
->opaque
;
1105 ret
= fn(child
, opaque
);
1110 ret
= do_object_child_foreach(child
, fn
, opaque
, true);
1120 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1123 return do_object_child_foreach(obj
, fn
, opaque
, false);
1126 int object_child_foreach_recursive(Object
*obj
,
1127 int (*fn
)(Object
*child
, void *opaque
),
1130 return do_object_child_foreach(obj
, fn
, opaque
, true);
1133 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1135 GSList
**list
= opaque
;
1137 *list
= g_slist_prepend(*list
, klass
);
1140 GSList
*object_class_get_list(const char *implements_type
,
1141 bool include_abstract
)
1143 GSList
*list
= NULL
;
1145 object_class_foreach(object_class_get_list_tramp
,
1146 implements_type
, include_abstract
, &list
);
1150 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1152 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1153 object_class_get_name((ObjectClass
*)b
));
1156 GSList
*object_class_get_list_sorted(const char *implements_type
,
1157 bool include_abstract
)
1159 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1163 Object
*object_ref(void *objptr
)
1165 Object
*obj
= OBJECT(objptr
);
1169 qatomic_inc(&obj
->ref
);
1173 void object_unref(void *objptr
)
1175 Object
*obj
= OBJECT(objptr
);
1179 g_assert(obj
->ref
> 0);
1181 /* parent always holds a reference to its children */
1182 if (qatomic_fetch_dec(&obj
->ref
) == 1) {
1183 object_finalize(obj
);
1188 object_property_try_add(Object
*obj
, const char *name
, const char *type
,
1189 ObjectPropertyAccessor
*get
,
1190 ObjectPropertyAccessor
*set
,
1191 ObjectPropertyRelease
*release
,
1192 void *opaque
, Error
**errp
)
1194 ObjectProperty
*prop
;
1195 size_t name_len
= strlen(name
);
1197 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1199 ObjectProperty
*ret
= NULL
;
1200 char *name_no_array
= g_strdup(name
);
1202 name_no_array
[name_len
- 3] = '\0';
1203 for (i
= 0; i
< INT16_MAX
; ++i
) {
1204 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1206 ret
= object_property_try_add(obj
, full_name
, type
, get
, set
,
1207 release
, opaque
, NULL
);
1213 g_free(name_no_array
);
1218 if (object_property_find(obj
, name
) != NULL
) {
1219 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1220 name
, object_get_typename(obj
));
1224 prop
= g_malloc0(sizeof(*prop
));
1226 prop
->name
= g_strdup(name
);
1227 prop
->type
= g_strdup(type
);
1231 prop
->release
= release
;
1232 prop
->opaque
= opaque
;
1234 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1239 object_property_add(Object
*obj
, const char *name
, const char *type
,
1240 ObjectPropertyAccessor
*get
,
1241 ObjectPropertyAccessor
*set
,
1242 ObjectPropertyRelease
*release
,
1245 return object_property_try_add(obj
, name
, type
, get
, set
, release
,
1246 opaque
, &error_abort
);
1250 object_class_property_add(ObjectClass
*klass
,
1253 ObjectPropertyAccessor
*get
,
1254 ObjectPropertyAccessor
*set
,
1255 ObjectPropertyRelease
*release
,
1258 ObjectProperty
*prop
;
1260 assert(!object_class_property_find(klass
, name
));
1262 prop
= g_malloc0(sizeof(*prop
));
1264 prop
->name
= g_strdup(name
);
1265 prop
->type
= g_strdup(type
);
1269 prop
->release
= release
;
1270 prop
->opaque
= opaque
;
1272 g_hash_table_insert(klass
->properties
, prop
->name
, prop
);
1277 ObjectProperty
*object_property_find(Object
*obj
, const char *name
)
1279 ObjectProperty
*prop
;
1280 ObjectClass
*klass
= object_get_class(obj
);
1282 prop
= object_class_property_find(klass
, name
);
1287 return g_hash_table_lookup(obj
->properties
, name
);
1290 ObjectProperty
*object_property_find_err(Object
*obj
, const char *name
,
1293 ObjectProperty
*prop
= object_property_find(obj
, name
);
1295 error_setg(errp
, "Property '%s.%s' not found",
1296 object_get_typename(obj
), name
);
1301 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1304 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1305 iter
->nextclass
= object_get_class(obj
);
1308 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1311 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1312 if (!iter
->nextclass
) {
1315 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1316 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1321 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1324 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1325 iter
->nextclass
= object_class_get_parent(klass
);
1328 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
)
1330 ObjectClass
*parent_klass
;
1332 parent_klass
= object_class_get_parent(klass
);
1334 ObjectProperty
*prop
=
1335 object_class_property_find(parent_klass
, name
);
1341 return g_hash_table_lookup(klass
->properties
, name
);
1344 ObjectProperty
*object_class_property_find_err(ObjectClass
*klass
,
1348 ObjectProperty
*prop
= object_class_property_find(klass
, name
);
1350 error_setg(errp
, "Property '.%s' not found", name
);
1356 void object_property_del(Object
*obj
, const char *name
)
1358 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1360 if (prop
->release
) {
1361 prop
->release(obj
, name
, prop
->opaque
);
1363 g_hash_table_remove(obj
->properties
, name
);
1366 bool object_property_get(Object
*obj
, const char *name
, Visitor
*v
,
1370 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1377 error_setg(errp
, QERR_PERMISSION_DENIED
);
1380 prop
->get(obj
, v
, name
, prop
->opaque
, &err
);
1381 error_propagate(errp
, err
);
1385 bool object_property_set(Object
*obj
, const char *name
, Visitor
*v
,
1389 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1396 error_setg(errp
, QERR_PERMISSION_DENIED
);
1399 prop
->set(obj
, v
, name
, prop
->opaque
, &err
);
1400 error_propagate(errp
, err
);
1404 bool object_property_set_str(Object
*obj
, const char *name
,
1405 const char *value
, Error
**errp
)
1407 QString
*qstr
= qstring_from_str(value
);
1408 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qstr
), errp
);
1410 qobject_unref(qstr
);
1414 char *object_property_get_str(Object
*obj
, const char *name
,
1417 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1424 retval
= g_strdup(qobject_get_try_str(ret
));
1426 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1433 bool object_property_set_link(Object
*obj
, const char *name
,
1434 Object
*value
, Error
**errp
)
1436 g_autofree
char *path
= NULL
;
1439 path
= object_get_canonical_path(value
);
1441 return object_property_set_str(obj
, name
, path
?: "", errp
);
1444 Object
*object_property_get_link(Object
*obj
, const char *name
,
1447 char *str
= object_property_get_str(obj
, name
, errp
);
1448 Object
*target
= NULL
;
1451 target
= object_resolve_path(str
, NULL
);
1453 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1454 "Device '%s' not found", str
);
1462 bool object_property_set_bool(Object
*obj
, const char *name
,
1463 bool value
, Error
**errp
)
1465 QBool
*qbool
= qbool_from_bool(value
);
1466 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qbool
), errp
);
1468 qobject_unref(qbool
);
1472 bool object_property_get_bool(Object
*obj
, const char *name
,
1475 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1482 qbool
= qobject_to(QBool
, ret
);
1484 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1487 retval
= qbool_get_bool(qbool
);
1494 bool object_property_set_int(Object
*obj
, const char *name
,
1495 int64_t value
, Error
**errp
)
1497 QNum
*qnum
= qnum_from_int(value
);
1498 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1500 qobject_unref(qnum
);
1504 int64_t object_property_get_int(Object
*obj
, const char *name
,
1507 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1515 qnum
= qobject_to(QNum
, ret
);
1516 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1517 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1525 static void object_property_init_defval(Object
*obj
, ObjectProperty
*prop
)
1527 Visitor
*v
= qobject_input_visitor_new(prop
->defval
);
1529 assert(prop
->set
!= NULL
);
1530 prop
->set(obj
, v
, prop
->name
, prop
->opaque
, &error_abort
);
1535 static void object_property_set_default(ObjectProperty
*prop
, QObject
*defval
)
1537 assert(!prop
->defval
);
1538 assert(!prop
->init
);
1540 prop
->defval
= defval
;
1541 prop
->init
= object_property_init_defval
;
1544 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
)
1546 object_property_set_default(prop
, QOBJECT(qbool_from_bool(value
)));
1549 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
)
1551 object_property_set_default(prop
, QOBJECT(qstring_from_str(value
)));
1554 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
)
1556 object_property_set_default(prop
, QOBJECT(qnum_from_int(value
)));
1559 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
)
1561 object_property_set_default(prop
, QOBJECT(qnum_from_uint(value
)));
1564 bool object_property_set_uint(Object
*obj
, const char *name
,
1565 uint64_t value
, Error
**errp
)
1567 QNum
*qnum
= qnum_from_uint(value
);
1568 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1570 qobject_unref(qnum
);
1574 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1577 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1584 qnum
= qobject_to(QNum
, ret
);
1585 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1586 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1594 typedef struct EnumProperty
{
1595 const QEnumLookup
*lookup
;
1596 int (*get
)(Object
*, Error
**);
1597 void (*set
)(Object
*, int, Error
**);
1600 int object_property_get_enum(Object
*obj
, const char *name
,
1601 const char *typename
, Error
**errp
)
1605 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1606 EnumProperty
*enumprop
;
1612 if (!g_str_equal(prop
->type
, typename
)) {
1613 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1614 name
, object_class_get_name(
1615 object_get_class(obj
)), typename
);
1619 enumprop
= prop
->opaque
;
1621 str
= object_property_get_str(obj
, name
, errp
);
1626 ret
= qapi_enum_parse(enumprop
->lookup
, str
, -1, errp
);
1632 bool object_property_parse(Object
*obj
, const char *name
,
1633 const char *string
, Error
**errp
)
1635 Visitor
*v
= string_input_visitor_new(string
);
1636 bool ok
= object_property_set(obj
, name
, v
, errp
);
1642 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1646 char *string
= NULL
;
1648 v
= string_output_visitor_new(human
, &string
);
1649 if (!object_property_get(obj
, name
, v
, errp
)) {
1653 visit_complete(v
, &string
);
1660 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1662 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1670 Object
*object_get_root(void)
1672 static Object
*root
;
1675 root
= object_new("container");
1681 Object
*object_get_objects_root(void)
1683 return container_get(object_get_root(), "/objects");
1686 Object
*object_get_internal_root(void)
1688 static Object
*internal_root
;
1690 if (!internal_root
) {
1691 internal_root
= object_new("container");
1694 return internal_root
;
1697 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1698 const char *name
, void *opaque
,
1701 Object
*child
= opaque
;
1704 path
= object_get_canonical_path(child
);
1705 visit_type_str(v
, name
, &path
, errp
);
1709 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
,
1715 static void object_finalize_child_property(Object
*obj
, const char *name
,
1718 Object
*child
= opaque
;
1720 if (child
->class->unparent
) {
1721 (child
->class->unparent
)(child
);
1723 child
->parent
= NULL
;
1724 object_unref(child
);
1728 object_property_try_add_child(Object
*obj
, const char *name
,
1729 Object
*child
, Error
**errp
)
1731 g_autofree
char *type
= NULL
;
1734 assert(!child
->parent
);
1736 type
= g_strdup_printf("child<%s>", object_get_typename(child
));
1738 op
= object_property_try_add(obj
, name
, type
, object_get_child_property
,
1739 NULL
, object_finalize_child_property
,
1744 op
->resolve
= object_resolve_child_property
;
1746 child
->parent
= obj
;
1751 object_property_add_child(Object
*obj
, const char *name
,
1754 return object_property_try_add_child(obj
, name
, child
, &error_abort
);
1757 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1758 Object
*val
, Error
**errp
)
1760 /* Allow the link to be set, always */
1766 Object
*target
; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1767 ptrdiff_t offset
; /* if OBJ_PROP_LINK_CLASS */
1769 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1770 ObjectPropertyLinkFlags flags
;
1774 object_link_get_targetp(Object
*obj
, LinkProperty
*lprop
)
1776 if (lprop
->flags
& OBJ_PROP_LINK_DIRECT
) {
1777 return &lprop
->target
;
1778 } else if (lprop
->flags
& OBJ_PROP_LINK_CLASS
) {
1779 return (void *)obj
+ lprop
->offset
;
1781 return lprop
->targetp
;
1785 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1786 const char *name
, void *opaque
,
1789 LinkProperty
*lprop
= opaque
;
1790 Object
**targetp
= object_link_get_targetp(obj
, lprop
);
1794 path
= object_get_canonical_path(*targetp
);
1795 visit_type_str(v
, name
, &path
, errp
);
1799 visit_type_str(v
, name
, &path
, errp
);
1804 * object_resolve_link:
1806 * Lookup an object and ensure its type matches the link property type. This
1807 * is similar to object_resolve_path() except type verification against the
1808 * link property is performed.
1810 * Returns: The matched object or NULL on path lookup failures.
1812 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1813 const char *path
, Error
**errp
)
1817 bool ambiguous
= false;
1820 /* Go from link<FOO> to FOO. */
1821 type
= object_property_get_type(obj
, name
, NULL
);
1822 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1823 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1826 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1828 } else if (!target
) {
1829 target
= object_resolve_path(path
, &ambiguous
);
1830 if (target
|| ambiguous
) {
1831 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1833 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1834 "Device '%s' not found", path
);
1838 g_free(target_type
);
1843 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1844 const char *name
, void *opaque
,
1847 Error
*local_err
= NULL
;
1848 LinkProperty
*prop
= opaque
;
1849 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1850 Object
*old_target
= *targetp
;
1854 if (!visit_type_str(v
, name
, &path
, errp
)) {
1859 new_target
= object_resolve_link(obj
, name
, path
, errp
);
1870 prop
->check(obj
, name
, new_target
, &local_err
);
1872 error_propagate(errp
, local_err
);
1876 *targetp
= new_target
;
1877 if (prop
->flags
& OBJ_PROP_LINK_STRONG
) {
1878 object_ref(new_target
);
1879 object_unref(old_target
);
1883 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
,
1886 LinkProperty
*lprop
= opaque
;
1888 return *object_link_get_targetp(parent
, lprop
);
1891 static void object_release_link_property(Object
*obj
, const char *name
,
1894 LinkProperty
*prop
= opaque
;
1895 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1897 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *targetp
) {
1898 object_unref(*targetp
);
1900 if (!(prop
->flags
& OBJ_PROP_LINK_CLASS
)) {
1905 static ObjectProperty
*
1906 object_add_link_prop(Object
*obj
, const char *name
,
1907 const char *type
, void *ptr
,
1908 void (*check
)(const Object
*, const char *,
1909 Object
*, Error
**),
1910 ObjectPropertyLinkFlags flags
)
1912 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1913 g_autofree
char *full_type
= NULL
;
1916 if (flags
& OBJ_PROP_LINK_DIRECT
) {
1919 prop
->targetp
= ptr
;
1921 prop
->check
= check
;
1922 prop
->flags
= flags
;
1924 full_type
= g_strdup_printf("link<%s>", type
);
1926 op
= object_property_add(obj
, name
, full_type
,
1927 object_get_link_property
,
1928 check
? object_set_link_property
: NULL
,
1929 object_release_link_property
,
1931 op
->resolve
= object_resolve_link_property
;
1936 object_property_add_link(Object
*obj
, const char *name
,
1937 const char *type
, Object
**targetp
,
1938 void (*check
)(const Object
*, const char *,
1939 Object
*, Error
**),
1940 ObjectPropertyLinkFlags flags
)
1942 return object_add_link_prop(obj
, name
, type
, targetp
, check
, flags
);
1946 object_class_property_add_link(ObjectClass
*oc
,
1948 const char *type
, ptrdiff_t offset
,
1949 void (*check
)(const Object
*obj
, const char *name
,
1950 Object
*val
, Error
**errp
),
1951 ObjectPropertyLinkFlags flags
)
1953 LinkProperty
*prop
= g_new0(LinkProperty
, 1);
1957 prop
->offset
= offset
;
1958 prop
->check
= check
;
1959 prop
->flags
= flags
| OBJ_PROP_LINK_CLASS
;
1961 full_type
= g_strdup_printf("link<%s>", type
);
1963 op
= object_class_property_add(oc
, name
, full_type
,
1964 object_get_link_property
,
1965 check
? object_set_link_property
: NULL
,
1966 object_release_link_property
,
1969 op
->resolve
= object_resolve_link_property
;
1976 object_property_add_const_link(Object
*obj
, const char *name
,
1979 return object_add_link_prop(obj
, name
,
1980 object_get_typename(target
), target
,
1981 NULL
, OBJ_PROP_LINK_DIRECT
);
1984 const char *object_get_canonical_path_component(const Object
*obj
)
1986 ObjectProperty
*prop
= NULL
;
1987 GHashTableIter iter
;
1989 if (obj
->parent
== NULL
) {
1993 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1994 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1995 if (!object_property_is_child(prop
)) {
1999 if (prop
->opaque
== obj
) {
2004 /* obj had a parent but was not a child, should never happen */
2005 g_assert_not_reached();
2009 char *object_get_canonical_path(const Object
*obj
)
2011 Object
*root
= object_get_root();
2012 char *newpath
, *path
= NULL
;
2015 return g_strdup("/");
2019 const char *component
= object_get_canonical_path_component(obj
);
2022 /* A canonical path must be complete, so discard what was
2029 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
2033 } while (obj
!= root
);
2038 Object
*object_resolve_path_component(Object
*parent
, const char *part
)
2040 ObjectProperty
*prop
= object_property_find(parent
, part
);
2045 if (prop
->resolve
) {
2046 return prop
->resolve(parent
, prop
->opaque
, part
);
2052 static Object
*object_resolve_abs_path(Object
*parent
,
2054 const char *typename
)
2058 if (*parts
== NULL
) {
2059 return object_dynamic_cast(parent
, typename
);
2062 if (strcmp(*parts
, "") == 0) {
2063 return object_resolve_abs_path(parent
, parts
+ 1, typename
);
2066 child
= object_resolve_path_component(parent
, *parts
);
2071 return object_resolve_abs_path(child
, parts
+ 1, typename
);
2074 static Object
*object_resolve_partial_path(Object
*parent
,
2076 const char *typename
,
2080 GHashTableIter iter
;
2081 ObjectProperty
*prop
;
2083 obj
= object_resolve_abs_path(parent
, parts
, typename
);
2085 g_hash_table_iter_init(&iter
, parent
->properties
);
2086 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2089 if (!object_property_is_child(prop
)) {
2093 found
= object_resolve_partial_path(prop
->opaque
, parts
,
2094 typename
, ambiguous
);
2111 Object
*object_resolve_path_type(const char *path
, const char *typename
,
2117 parts
= g_strsplit(path
, "/", 0);
2120 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
2121 bool ambiguous
= false;
2122 obj
= object_resolve_partial_path(object_get_root(), parts
,
2123 typename
, &ambiguous
);
2125 *ambiguousp
= ambiguous
;
2128 obj
= object_resolve_abs_path(object_get_root(), parts
+ 1, typename
);
2136 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
2138 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
2141 typedef struct StringProperty
2143 char *(*get
)(Object
*, Error
**);
2144 void (*set
)(Object
*, const char *, Error
**);
2147 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
2148 void *opaque
, Error
**errp
)
2150 StringProperty
*prop
= opaque
;
2154 value
= prop
->get(obj
, &err
);
2156 error_propagate(errp
, err
);
2160 visit_type_str(v
, name
, &value
, errp
);
2164 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
2165 void *opaque
, Error
**errp
)
2167 StringProperty
*prop
= opaque
;
2170 if (!visit_type_str(v
, name
, &value
, errp
)) {
2174 prop
->set(obj
, value
, errp
);
2178 static void property_release_data(Object
*obj
, const char *name
,
2185 object_property_add_str(Object
*obj
, const char *name
,
2186 char *(*get
)(Object
*, Error
**),
2187 void (*set
)(Object
*, const char *, Error
**))
2189 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2194 return object_property_add(obj
, name
, "string",
2195 get
? property_get_str
: NULL
,
2196 set
? property_set_str
: NULL
,
2197 property_release_data
,
2202 object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2203 char *(*get
)(Object
*, Error
**),
2204 void (*set
)(Object
*, const char *,
2207 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2212 return object_class_property_add(klass
, name
, "string",
2213 get
? property_get_str
: NULL
,
2214 set
? property_set_str
: NULL
,
2219 typedef struct BoolProperty
2221 bool (*get
)(Object
*, Error
**);
2222 void (*set
)(Object
*, bool, Error
**);
2225 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2226 void *opaque
, Error
**errp
)
2228 BoolProperty
*prop
= opaque
;
2232 value
= prop
->get(obj
, &err
);
2234 error_propagate(errp
, err
);
2238 visit_type_bool(v
, name
, &value
, errp
);
2241 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2242 void *opaque
, Error
**errp
)
2244 BoolProperty
*prop
= opaque
;
2247 if (!visit_type_bool(v
, name
, &value
, errp
)) {
2251 prop
->set(obj
, value
, errp
);
2255 object_property_add_bool(Object
*obj
, const char *name
,
2256 bool (*get
)(Object
*, Error
**),
2257 void (*set
)(Object
*, bool, Error
**))
2259 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2264 return object_property_add(obj
, name
, "bool",
2265 get
? property_get_bool
: NULL
,
2266 set
? property_set_bool
: NULL
,
2267 property_release_data
,
2272 object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2273 bool (*get
)(Object
*, Error
**),
2274 void (*set
)(Object
*, bool, Error
**))
2276 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2281 return object_class_property_add(klass
, name
, "bool",
2282 get
? property_get_bool
: NULL
,
2283 set
? property_set_bool
: NULL
,
2288 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2289 void *opaque
, Error
**errp
)
2291 EnumProperty
*prop
= opaque
;
2295 value
= prop
->get(obj
, &err
);
2297 error_propagate(errp
, err
);
2301 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2304 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2305 void *opaque
, Error
**errp
)
2307 EnumProperty
*prop
= opaque
;
2310 if (!visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
)) {
2313 prop
->set(obj
, value
, errp
);
2317 object_property_add_enum(Object
*obj
, const char *name
,
2318 const char *typename
,
2319 const QEnumLookup
*lookup
,
2320 int (*get
)(Object
*, Error
**),
2321 void (*set
)(Object
*, int, Error
**))
2323 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2325 prop
->lookup
= lookup
;
2329 return object_property_add(obj
, name
, typename
,
2330 get
? property_get_enum
: NULL
,
2331 set
? property_set_enum
: NULL
,
2332 property_release_data
,
2337 object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2338 const char *typename
,
2339 const QEnumLookup
*lookup
,
2340 int (*get
)(Object
*, Error
**),
2341 void (*set
)(Object
*, int, Error
**))
2343 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2345 prop
->lookup
= lookup
;
2349 return object_class_property_add(klass
, name
, typename
,
2350 get
? property_get_enum
: NULL
,
2351 set
? property_set_enum
: NULL
,
2356 typedef struct TMProperty
{
2357 void (*get
)(Object
*, struct tm
*, Error
**);
2360 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2361 void *opaque
, Error
**errp
)
2363 TMProperty
*prop
= opaque
;
2367 prop
->get(obj
, &value
, &err
);
2369 error_propagate(errp
, err
);
2373 if (!visit_start_struct(v
, name
, NULL
, 0, errp
)) {
2376 if (!visit_type_int32(v
, "tm_year", &value
.tm_year
, errp
)) {
2379 if (!visit_type_int32(v
, "tm_mon", &value
.tm_mon
, errp
)) {
2382 if (!visit_type_int32(v
, "tm_mday", &value
.tm_mday
, errp
)) {
2385 if (!visit_type_int32(v
, "tm_hour", &value
.tm_hour
, errp
)) {
2388 if (!visit_type_int32(v
, "tm_min", &value
.tm_min
, errp
)) {
2391 if (!visit_type_int32(v
, "tm_sec", &value
.tm_sec
, errp
)) {
2394 visit_check_struct(v
, errp
);
2396 visit_end_struct(v
, NULL
);
2400 object_property_add_tm(Object
*obj
, const char *name
,
2401 void (*get
)(Object
*, struct tm
*, Error
**))
2403 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2407 return object_property_add(obj
, name
, "struct tm",
2408 get
? property_get_tm
: NULL
, NULL
,
2409 property_release_data
,
2414 object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2415 void (*get
)(Object
*, struct tm
*, Error
**))
2417 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2421 return object_class_property_add(klass
, name
, "struct tm",
2422 get
? property_get_tm
: NULL
,
2426 static char *object_get_type(Object
*obj
, Error
**errp
)
2428 return g_strdup(object_get_typename(obj
));
2431 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2432 void *opaque
, Error
**errp
)
2434 uint8_t value
= *(uint8_t *)opaque
;
2435 visit_type_uint8(v
, name
, &value
, errp
);
2438 static void property_set_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2439 void *opaque
, Error
**errp
)
2441 uint8_t *field
= opaque
;
2444 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
2451 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2452 void *opaque
, Error
**errp
)
2454 uint16_t value
= *(uint16_t *)opaque
;
2455 visit_type_uint16(v
, name
, &value
, errp
);
2458 static void property_set_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2459 void *opaque
, Error
**errp
)
2461 uint16_t *field
= opaque
;
2464 if (!visit_type_uint16(v
, name
, &value
, errp
)) {
2471 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2472 void *opaque
, Error
**errp
)
2474 uint32_t value
= *(uint32_t *)opaque
;
2475 visit_type_uint32(v
, name
, &value
, errp
);
2478 static void property_set_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2479 void *opaque
, Error
**errp
)
2481 uint32_t *field
= opaque
;
2484 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
2491 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2492 void *opaque
, Error
**errp
)
2494 uint64_t value
= *(uint64_t *)opaque
;
2495 visit_type_uint64(v
, name
, &value
, errp
);
2498 static void property_set_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2499 void *opaque
, Error
**errp
)
2501 uint64_t *field
= opaque
;
2504 if (!visit_type_uint64(v
, name
, &value
, errp
)) {
2512 object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2514 ObjectPropertyFlags flags
)
2516 ObjectPropertyAccessor
*getter
= NULL
;
2517 ObjectPropertyAccessor
*setter
= NULL
;
2519 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2520 getter
= property_get_uint8_ptr
;
2523 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2524 setter
= property_set_uint8_ptr
;
2527 return object_property_add(obj
, name
, "uint8",
2528 getter
, setter
, NULL
, (void *)v
);
2532 object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2534 ObjectPropertyFlags flags
)
2536 ObjectPropertyAccessor
*getter
= NULL
;
2537 ObjectPropertyAccessor
*setter
= NULL
;
2539 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2540 getter
= property_get_uint8_ptr
;
2543 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2544 setter
= property_set_uint8_ptr
;
2547 return object_class_property_add(klass
, name
, "uint8",
2548 getter
, setter
, NULL
, (void *)v
);
2552 object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2554 ObjectPropertyFlags flags
)
2556 ObjectPropertyAccessor
*getter
= NULL
;
2557 ObjectPropertyAccessor
*setter
= NULL
;
2559 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2560 getter
= property_get_uint16_ptr
;
2563 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2564 setter
= property_set_uint16_ptr
;
2567 return object_property_add(obj
, name
, "uint16",
2568 getter
, setter
, NULL
, (void *)v
);
2572 object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2574 ObjectPropertyFlags flags
)
2576 ObjectPropertyAccessor
*getter
= NULL
;
2577 ObjectPropertyAccessor
*setter
= NULL
;
2579 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2580 getter
= property_get_uint16_ptr
;
2583 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2584 setter
= property_set_uint16_ptr
;
2587 return object_class_property_add(klass
, name
, "uint16",
2588 getter
, setter
, NULL
, (void *)v
);
2592 object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2594 ObjectPropertyFlags flags
)
2596 ObjectPropertyAccessor
*getter
= NULL
;
2597 ObjectPropertyAccessor
*setter
= NULL
;
2599 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2600 getter
= property_get_uint32_ptr
;
2603 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2604 setter
= property_set_uint32_ptr
;
2607 return object_property_add(obj
, name
, "uint32",
2608 getter
, setter
, NULL
, (void *)v
);
2612 object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2614 ObjectPropertyFlags flags
)
2616 ObjectPropertyAccessor
*getter
= NULL
;
2617 ObjectPropertyAccessor
*setter
= NULL
;
2619 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2620 getter
= property_get_uint32_ptr
;
2623 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2624 setter
= property_set_uint32_ptr
;
2627 return object_class_property_add(klass
, name
, "uint32",
2628 getter
, setter
, NULL
, (void *)v
);
2632 object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2634 ObjectPropertyFlags flags
)
2636 ObjectPropertyAccessor
*getter
= NULL
;
2637 ObjectPropertyAccessor
*setter
= NULL
;
2639 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2640 getter
= property_get_uint64_ptr
;
2643 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2644 setter
= property_set_uint64_ptr
;
2647 return object_property_add(obj
, name
, "uint64",
2648 getter
, setter
, NULL
, (void *)v
);
2652 object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2654 ObjectPropertyFlags flags
)
2656 ObjectPropertyAccessor
*getter
= NULL
;
2657 ObjectPropertyAccessor
*setter
= NULL
;
2659 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2660 getter
= property_get_uint64_ptr
;
2663 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2664 setter
= property_set_uint64_ptr
;
2667 return object_class_property_add(klass
, name
, "uint64",
2668 getter
, setter
, NULL
, (void *)v
);
2676 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2677 void *opaque
, Error
**errp
)
2679 AliasProperty
*prop
= opaque
;
2681 object_property_get(prop
->target_obj
, prop
->target_name
, v
, errp
);
2684 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2685 void *opaque
, Error
**errp
)
2687 AliasProperty
*prop
= opaque
;
2689 object_property_set(prop
->target_obj
, prop
->target_name
, v
, errp
);
2692 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2695 AliasProperty
*prop
= opaque
;
2697 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2700 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2702 AliasProperty
*prop
= opaque
;
2704 g_free(prop
->target_name
);
2709 object_property_add_alias(Object
*obj
, const char *name
,
2710 Object
*target_obj
, const char *target_name
)
2712 AliasProperty
*prop
;
2714 ObjectProperty
*target_prop
;
2715 g_autofree
char *prop_type
= NULL
;
2717 target_prop
= object_property_find_err(target_obj
, target_name
,
2720 if (object_property_is_child(target_prop
)) {
2721 prop_type
= g_strdup_printf("link%s",
2722 target_prop
->type
+ strlen("child"));
2724 prop_type
= g_strdup(target_prop
->type
);
2727 prop
= g_malloc(sizeof(*prop
));
2728 prop
->target_obj
= target_obj
;
2729 prop
->target_name
= g_strdup(target_name
);
2731 op
= object_property_add(obj
, name
, prop_type
,
2734 property_release_alias
,
2736 op
->resolve
= property_resolve_alias
;
2737 if (target_prop
->defval
) {
2738 op
->defval
= qobject_ref(target_prop
->defval
);
2741 object_property_set_description(obj
, op
->name
,
2742 target_prop
->description
);
2746 void object_property_set_description(Object
*obj
, const char *name
,
2747 const char *description
)
2751 op
= object_property_find_err(obj
, name
, &error_abort
);
2752 g_free(op
->description
);
2753 op
->description
= g_strdup(description
);
2756 void object_class_property_set_description(ObjectClass
*klass
,
2758 const char *description
)
2762 op
= g_hash_table_lookup(klass
->properties
, name
);
2763 g_free(op
->description
);
2764 op
->description
= g_strdup(description
);
2767 static void object_class_init(ObjectClass
*klass
, void *data
)
2769 object_class_property_add_str(klass
, "type", object_get_type
,
2773 static void register_types(void)
2775 static TypeInfo interface_info
= {
2776 .name
= TYPE_INTERFACE
,
2777 .class_size
= sizeof(InterfaceClass
),
2781 static TypeInfo object_info
= {
2782 .name
= TYPE_OBJECT
,
2783 .instance_size
= sizeof(Object
),
2784 .class_init
= object_class_init
,
2788 type_interface
= type_register_internal(&interface_info
);
2789 type_register_internal(&object_info
);
2792 type_init(register_types
)