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 "qemu/memalign.h"
20 #include "qapi/visitor.h"
21 #include "qapi/string-input-visitor.h"
22 #include "qapi/string-output-visitor.h"
23 #include "qapi/qobject-input-visitor.h"
24 #include "qapi/forward-visitor.h"
25 #include "qapi/qapi-builtin-visit.h"
26 #include "qapi/qmp/qjson.h"
29 /* TODO: replace QObject with a simpler visitor to avoid a dependency
30 * of the QOM core on QObject? */
31 #include "qom/qom-qobject.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qlist.h"
34 #include "qapi/qmp/qnum.h"
35 #include "qapi/qmp/qstring.h"
36 #include "qemu/error-report.h"
38 #define MAX_INTERFACES 32
40 typedef struct InterfaceImpl InterfaceImpl
;
41 typedef struct TypeImpl TypeImpl
;
55 size_t instance_align
;
57 void (*class_init
)(ObjectClass
*klass
, void *data
);
58 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
62 void (*instance_init
)(Object
*obj
);
63 void (*instance_post_init
)(Object
*obj
);
64 void (*instance_finalize
)(Object
*obj
);
69 TypeImpl
*parent_type
;
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
);
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
));
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
);
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
;
140 static bool type_name_is_valid(const char *name
)
142 const int slen
= strlen(name
);
148 * Ideally, the name should start with a letter - however, we've got
149 * too many names starting with a digit already, so allow digits here,
150 * too (except '0' which is not used yet)
152 if (!g_ascii_isalnum(name
[0]) || name
[0] == '0') {
156 plen
= strspn(name
, "abcdefghijklmnopqrstuvwxyz"
157 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
163 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
167 if (!type_name_is_valid(info
->name
)) {
168 fprintf(stderr
, "Registering '%s' with illegal type name\n", info
->name
);
178 TypeImpl
*type_register(const TypeInfo
*info
)
180 assert(info
->parent
);
181 return type_register_internal(info
);
184 TypeImpl
*type_register_static(const TypeInfo
*info
)
186 return type_register(info
);
189 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
193 for (i
= 0; i
< nr_infos
; i
++) {
194 type_register_static(&infos
[i
]);
198 static TypeImpl
*type_get_by_name(const char *name
)
204 return type_table_lookup(name
);
207 static TypeImpl
*type_get_parent(TypeImpl
*type
)
209 if (!type
->parent_type
&& type
->parent
) {
210 type
->parent_type
= type_get_by_name(type
->parent
);
211 if (!type
->parent_type
) {
212 fprintf(stderr
, "Type '%s' is missing its parent '%s'\n",
213 type
->name
, type
->parent
);
218 return type
->parent_type
;
221 static bool type_has_parent(TypeImpl
*type
)
223 return (type
->parent
!= NULL
);
226 static size_t type_class_get_size(TypeImpl
*ti
)
228 if (ti
->class_size
) {
229 return ti
->class_size
;
232 if (type_has_parent(ti
)) {
233 return type_class_get_size(type_get_parent(ti
));
236 return sizeof(ObjectClass
);
239 static size_t type_object_get_size(TypeImpl
*ti
)
241 if (ti
->instance_size
) {
242 return ti
->instance_size
;
245 if (type_has_parent(ti
)) {
246 return type_object_get_size(type_get_parent(ti
));
252 static size_t type_object_get_align(TypeImpl
*ti
)
254 if (ti
->instance_align
) {
255 return ti
->instance_align
;
258 if (type_has_parent(ti
)) {
259 return type_object_get_align(type_get_parent(ti
));
265 size_t object_type_get_instance_size(const char *typename
)
267 TypeImpl
*type
= type_get_by_name(typename
);
269 g_assert(type
!= NULL
);
270 return type_object_get_size(type
);
273 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
277 /* Check if target_type is a direct ancestor of type */
279 if (type
== target_type
) {
283 type
= type_get_parent(type
);
289 static void type_initialize(TypeImpl
*ti
);
291 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
292 TypeImpl
*parent_type
)
294 InterfaceClass
*new_iface
;
296 TypeImpl
*iface_impl
;
298 info
.parent
= parent_type
->name
;
299 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
300 info
.abstract
= true;
302 iface_impl
= type_new(&info
);
303 iface_impl
->parent_type
= parent_type
;
304 type_initialize(iface_impl
);
305 g_free((char *)info
.name
);
307 new_iface
= (InterfaceClass
*)iface_impl
->class;
308 new_iface
->concrete_class
= ti
->class;
309 new_iface
->interface_type
= interface_type
;
311 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
, new_iface
);
314 static void object_property_free(gpointer data
)
316 ObjectProperty
*prop
= data
;
319 qobject_unref(prop
->defval
);
324 g_free(prop
->description
);
328 static void type_initialize(TypeImpl
*ti
)
336 ti
->class_size
= type_class_get_size(ti
);
337 ti
->instance_size
= type_object_get_size(ti
);
338 ti
->instance_align
= type_object_get_align(ti
);
339 /* Any type with zero instance_size is implicitly abstract.
340 * This means interface types are all abstract.
342 if (ti
->instance_size
== 0) {
345 if (type_is_ancestor(ti
, type_interface
)) {
346 assert(ti
->instance_size
== 0);
347 assert(ti
->abstract
);
348 assert(!ti
->instance_init
);
349 assert(!ti
->instance_post_init
);
350 assert(!ti
->instance_finalize
);
351 assert(!ti
->num_interfaces
);
353 ti
->class = g_malloc0(ti
->class_size
);
355 parent
= type_get_parent(ti
);
357 type_initialize(parent
);
361 g_assert(parent
->class_size
<= ti
->class_size
);
362 g_assert(parent
->instance_size
<= ti
->instance_size
);
363 memcpy(ti
->class, parent
->class, parent
->class_size
);
364 ti
->class->interfaces
= NULL
;
366 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
367 InterfaceClass
*iface
= e
->data
;
368 ObjectClass
*klass
= OBJECT_CLASS(iface
);
370 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
373 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
374 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
376 error_report("missing interface '%s' for object '%s'",
377 ti
->interfaces
[i
].typename
, parent
->name
);
380 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
381 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
383 if (type_is_ancestor(target_type
, t
)) {
392 type_initialize_interface(ti
, t
, t
);
396 ti
->class->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
397 object_property_free
);
399 ti
->class->type
= ti
;
402 if (parent
->class_base_init
) {
403 parent
->class_base_init(ti
->class, ti
->class_data
);
405 parent
= type_get_parent(parent
);
408 if (ti
->class_init
) {
409 ti
->class_init(ti
->class, ti
->class_data
);
413 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
415 if (type_has_parent(ti
)) {
416 object_init_with_type(obj
, type_get_parent(ti
));
419 if (ti
->instance_init
) {
420 ti
->instance_init(obj
);
424 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
426 if (ti
->instance_post_init
) {
427 ti
->instance_post_init(obj
);
430 if (type_has_parent(ti
)) {
431 object_post_init_with_type(obj
, type_get_parent(ti
));
435 bool object_apply_global_props(Object
*obj
, const GPtrArray
*props
,
444 for (i
= 0; i
< props
->len
; i
++) {
445 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
448 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
451 if (p
->optional
&& !object_property_find(obj
, p
->property
)) {
455 if (!object_property_parse(obj
, p
->property
, p
->value
, &err
)) {
456 error_prepend(&err
, "can't apply global %s.%s=%s: ",
457 p
->driver
, p
->property
, p
->value
);
459 * If errp != NULL, propagate error and return.
460 * If errp == NULL, report a warning, but keep going
461 * with the remaining globals.
464 error_propagate(errp
, err
);
467 warn_report_err(err
);
476 * Global property defaults
477 * Slot 0: accelerator's global property defaults
478 * Slot 1: machine's global property defaults
479 * Slot 2: global properties from legacy command line option
480 * Each is a GPtrArray of of GlobalProperty.
481 * Applied in order, later entries override earlier ones.
483 static GPtrArray
*object_compat_props
[3];
486 * Retrieve @GPtrArray for global property defined with options
487 * other than "-global". These are generally used for syntactic
488 * sugar and legacy command line options.
490 void object_register_sugar_prop(const char *driver
, const char *prop
,
491 const char *value
, bool optional
)
494 if (!object_compat_props
[2]) {
495 object_compat_props
[2] = g_ptr_array_new();
497 g
= g_new0(GlobalProperty
, 1);
498 g
->driver
= g_strdup(driver
);
499 g
->property
= g_strdup(prop
);
500 g
->value
= g_strdup(value
);
501 g
->optional
= optional
;
502 g_ptr_array_add(object_compat_props
[2], g
);
506 * Set machine's global property defaults to @compat_props.
507 * May be called at most once.
509 void object_set_machine_compat_props(GPtrArray
*compat_props
)
511 assert(!object_compat_props
[1]);
512 object_compat_props
[1] = compat_props
;
516 * Set accelerator's global property defaults to @compat_props.
517 * May be called at most once.
519 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
521 assert(!object_compat_props
[0]);
522 object_compat_props
[0] = compat_props
;
525 void object_apply_compat_props(Object
*obj
)
529 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
530 object_apply_global_props(obj
, object_compat_props
[i
],
531 i
== 2 ? &error_fatal
: &error_abort
);
535 static void object_class_property_init_all(Object
*obj
)
537 ObjectPropertyIterator iter
;
538 ObjectProperty
*prop
;
540 object_class_property_iter_init(&iter
, object_get_class(obj
));
541 while ((prop
= object_property_iter_next(&iter
))) {
543 prop
->init(obj
, prop
);
548 static void object_initialize_with_type(Object
*obj
, size_t size
, TypeImpl
*type
)
550 type_initialize(type
);
552 g_assert(type
->instance_size
>= sizeof(Object
));
553 g_assert(type
->abstract
== false);
554 g_assert(size
>= type
->instance_size
);
556 memset(obj
, 0, type
->instance_size
);
557 obj
->class = type
->class;
559 object_class_property_init_all(obj
);
560 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
561 NULL
, object_property_free
);
562 object_init_with_type(obj
, type
);
563 object_post_init_with_type(obj
, type
);
566 void object_initialize(void *data
, size_t size
, const char *typename
)
568 TypeImpl
*type
= type_get_by_name(typename
);
570 #ifdef CONFIG_MODULES
572 int rv
= module_load_qom(typename
, &error_fatal
);
574 type
= type_get_by_name(typename
);
576 error_report("missing object type '%s'", typename
);
582 error_report("missing object type '%s'", typename
);
586 object_initialize_with_type(data
, size
, type
);
589 bool object_initialize_child_with_props(Object
*parentobj
,
590 const char *propname
,
591 void *childobj
, size_t size
,
598 va_start(vargs
, errp
);
599 ok
= object_initialize_child_with_propsv(parentobj
, propname
,
600 childobj
, size
, type
, errp
,
606 bool object_initialize_child_with_propsv(Object
*parentobj
,
607 const char *propname
,
608 void *childobj
, size_t size
,
610 Error
**errp
, va_list vargs
)
616 object_initialize(childobj
, size
, type
);
617 obj
= OBJECT(childobj
);
619 if (!object_set_propv(obj
, errp
, vargs
)) {
623 object_property_add_child(parentobj
, propname
, obj
);
625 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
627 if (!user_creatable_complete(uc
, errp
)) {
628 object_unparent(obj
);
637 * We want @obj's reference to be 1 on success, 0 on failure.
638 * On success, it's 2: one taken by object_initialize(), and one
639 * by object_property_add_child().
640 * On failure in object_initialize() or earlier, it's 1.
641 * On failure afterwards, it's also 1: object_unparent() releases
642 * the reference taken by object_property_add_child().
648 void object_initialize_child_internal(Object
*parent
,
649 const char *propname
,
650 void *child
, size_t size
,
653 object_initialize_child_with_props(parent
, propname
, child
, size
, type
,
657 static inline bool object_property_is_child(ObjectProperty
*prop
)
659 return strstart(prop
->type
, "child<", NULL
);
662 static void object_property_del_all(Object
*obj
)
664 g_autoptr(GHashTable
) done
= g_hash_table_new(NULL
, NULL
);
665 ObjectProperty
*prop
;
666 ObjectPropertyIterator iter
;
671 object_property_iter_init(&iter
, obj
);
672 while ((prop
= object_property_iter_next(&iter
)) != NULL
) {
673 if (g_hash_table_add(done
, prop
)) {
675 prop
->release(obj
, prop
->name
, prop
->opaque
);
683 g_hash_table_unref(obj
->properties
);
686 static void object_property_del_child(Object
*obj
, Object
*child
)
688 ObjectProperty
*prop
;
692 g_hash_table_iter_init(&iter
, obj
->properties
);
693 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
695 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
697 prop
->release(obj
, prop
->name
, prop
->opaque
);
698 prop
->release
= NULL
;
703 g_hash_table_iter_init(&iter
, obj
->properties
);
704 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
706 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
707 g_hash_table_iter_remove(&iter
);
713 void object_unparent(Object
*obj
)
716 object_property_del_child(obj
->parent
, obj
);
720 static void object_deinit(Object
*obj
, TypeImpl
*type
)
722 if (type
->instance_finalize
) {
723 type
->instance_finalize(obj
);
726 if (type_has_parent(type
)) {
727 object_deinit(obj
, type_get_parent(type
));
731 static void object_finalize(void *data
)
734 TypeImpl
*ti
= obj
->class->type
;
736 object_property_del_all(obj
);
737 object_deinit(obj
, ti
);
739 g_assert(obj
->ref
== 0);
740 g_assert(obj
->parent
== NULL
);
746 /* Find the minimum alignment guaranteed by the system malloc. */
747 #if __STDC_VERSION__ >= 201112L
748 typedef max_align_t qemu_max_align_t
;
758 static Object
*object_new_with_type(Type type
)
762 void (*obj_free
)(void *);
764 g_assert(type
!= NULL
);
765 type_initialize(type
);
767 size
= type
->instance_size
;
768 align
= type
->instance_align
;
771 * Do not use qemu_memalign unless required. Depending on the
772 * implementation, extra alignment implies extra overhead.
774 if (likely(align
<= __alignof__(qemu_max_align_t
))) {
775 obj
= g_malloc(size
);
778 obj
= qemu_memalign(align
, size
);
779 obj_free
= qemu_vfree
;
782 object_initialize_with_type(obj
, size
, type
);
783 obj
->free
= obj_free
;
788 Object
*object_new_with_class(ObjectClass
*klass
)
790 return object_new_with_type(klass
->type
);
793 Object
*object_new(const char *typename
)
795 TypeImpl
*ti
= type_get_by_name(typename
);
797 return object_new_with_type(ti
);
801 Object
*object_new_with_props(const char *typename
,
810 va_start(vargs
, errp
);
811 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
818 Object
*object_new_with_propv(const char *typename
,
828 klass
= object_class_by_name(typename
);
830 error_setg(errp
, "invalid object type: %s", typename
);
834 if (object_class_is_abstract(klass
)) {
835 error_setg(errp
, "object type '%s' is abstract", typename
);
838 obj
= object_new_with_type(klass
->type
);
840 if (!object_set_propv(obj
, errp
, vargs
)) {
845 object_property_add_child(parent
, id
, obj
);
848 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
850 if (!user_creatable_complete(uc
, errp
)) {
852 object_unparent(obj
);
867 bool object_set_props(Object
*obj
,
874 va_start(vargs
, errp
);
875 ret
= object_set_propv(obj
, errp
, vargs
);
882 bool object_set_propv(Object
*obj
,
886 const char *propname
;
888 propname
= va_arg(vargs
, char *);
889 while (propname
!= NULL
) {
890 const char *value
= va_arg(vargs
, char *);
892 g_assert(value
!= NULL
);
893 if (!object_property_parse(obj
, propname
, value
, errp
)) {
896 propname
= va_arg(vargs
, char *);
903 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
905 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
912 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
913 const char *file
, int line
, const char *func
)
915 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
916 typename
, file
, line
, func
);
918 #ifdef CONFIG_QOM_CAST_DEBUG
922 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
923 if (qatomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
928 inst
= object_dynamic_cast(obj
, typename
);
931 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
932 file
, line
, func
, obj
, typename
);
938 if (obj
&& obj
== inst
) {
939 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
940 qatomic_set(&obj
->class->object_cast_cache
[i
- 1],
941 qatomic_read(&obj
->class->object_cast_cache
[i
]));
943 qatomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
951 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
952 const char *typename
)
954 ObjectClass
*ret
= NULL
;
955 TypeImpl
*target_type
;
962 /* A simple fast path that can trigger a lot for leaf classes. */
964 if (type
->name
== typename
) {
968 target_type
= type_get_by_name(typename
);
970 /* target class type unknown, so fail the cast */
974 if (type
->class->interfaces
&&
975 type_is_ancestor(target_type
, type_interface
)) {
979 for (i
= class->interfaces
; i
; i
= i
->next
) {
980 ObjectClass
*target_class
= i
->data
;
982 if (type_is_ancestor(target_class
->type
, target_type
)) {
988 /* The match was ambiguous, don't allow a cast */
992 } else if (type_is_ancestor(type
, target_type
)) {
999 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
1000 const char *typename
,
1001 const char *file
, int line
,
1006 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
1007 typename
, file
, line
, func
);
1009 #ifdef CONFIG_QOM_CAST_DEBUG
1012 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
1013 if (qatomic_read(&class->class_cast_cache
[i
]) == typename
) {
1019 if (!class || !class->interfaces
) {
1024 ret
= object_class_dynamic_cast(class, typename
);
1025 if (!ret
&& class) {
1026 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
1027 file
, line
, func
, class, typename
);
1031 #ifdef CONFIG_QOM_CAST_DEBUG
1032 if (class && ret
== class) {
1033 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
1034 qatomic_set(&class->class_cast_cache
[i
- 1],
1035 qatomic_read(&class->class_cast_cache
[i
]));
1037 qatomic_set(&class->class_cast_cache
[i
- 1], typename
);
1044 const char *object_get_typename(const Object
*obj
)
1046 return obj
->class->type
->name
;
1049 ObjectClass
*object_get_class(Object
*obj
)
1054 bool object_class_is_abstract(ObjectClass
*klass
)
1056 return klass
->type
->abstract
;
1059 const char *object_class_get_name(ObjectClass
*klass
)
1061 return klass
->type
->name
;
1064 ObjectClass
*object_class_by_name(const char *typename
)
1066 TypeImpl
*type
= type_get_by_name(typename
);
1072 type_initialize(type
);
1077 ObjectClass
*module_object_class_by_name(const char *typename
)
1081 oc
= object_class_by_name(typename
);
1082 #ifdef CONFIG_MODULES
1084 Error
*local_err
= NULL
;
1085 int rv
= module_load_qom(typename
, &local_err
);
1087 oc
= object_class_by_name(typename
);
1088 } else if (rv
< 0) {
1089 error_report_err(local_err
);
1096 ObjectClass
*object_class_get_parent(ObjectClass
*class)
1098 TypeImpl
*type
= type_get_parent(class->type
);
1104 type_initialize(type
);
1109 typedef struct OCFData
1111 void (*fn
)(ObjectClass
*klass
, void *opaque
);
1112 const char *implements_type
;
1113 bool include_abstract
;
1117 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
1120 OCFData
*data
= opaque
;
1121 TypeImpl
*type
= value
;
1124 type_initialize(type
);
1127 if (!data
->include_abstract
&& type
->abstract
) {
1131 if (data
->implements_type
&&
1132 !object_class_dynamic_cast(k
, data
->implements_type
)) {
1136 data
->fn(k
, data
->opaque
);
1139 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1140 const char *implements_type
, bool include_abstract
,
1143 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1145 enumerating_types
= true;
1146 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1147 enumerating_types
= false;
1150 static int do_object_child_foreach(Object
*obj
,
1151 int (*fn
)(Object
*child
, void *opaque
),
1152 void *opaque
, bool recurse
)
1154 GHashTableIter iter
;
1155 ObjectProperty
*prop
;
1158 g_hash_table_iter_init(&iter
, obj
->properties
);
1159 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1160 if (object_property_is_child(prop
)) {
1161 Object
*child
= prop
->opaque
;
1163 ret
= fn(child
, opaque
);
1168 ret
= do_object_child_foreach(child
, fn
, opaque
, true);
1178 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1181 return do_object_child_foreach(obj
, fn
, opaque
, false);
1184 int object_child_foreach_recursive(Object
*obj
,
1185 int (*fn
)(Object
*child
, void *opaque
),
1188 return do_object_child_foreach(obj
, fn
, opaque
, true);
1191 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1193 GSList
**list
= opaque
;
1195 *list
= g_slist_prepend(*list
, klass
);
1198 GSList
*object_class_get_list(const char *implements_type
,
1199 bool include_abstract
)
1201 GSList
*list
= NULL
;
1203 object_class_foreach(object_class_get_list_tramp
,
1204 implements_type
, include_abstract
, &list
);
1208 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1210 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1211 object_class_get_name((ObjectClass
*)b
));
1214 GSList
*object_class_get_list_sorted(const char *implements_type
,
1215 bool include_abstract
)
1217 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1221 Object
*object_ref(void *objptr
)
1223 Object
*obj
= OBJECT(objptr
);
1229 ref
= qatomic_fetch_inc(&obj
->ref
);
1230 /* Assert waaay before the integer overflows */
1231 g_assert(ref
< INT_MAX
);
1235 void object_unref(void *objptr
)
1237 Object
*obj
= OBJECT(objptr
);
1241 g_assert(obj
->ref
> 0);
1243 /* parent always holds a reference to its children */
1244 if (qatomic_fetch_dec(&obj
->ref
) == 1) {
1245 object_finalize(obj
);
1250 object_property_try_add(Object
*obj
, const char *name
, const char *type
,
1251 ObjectPropertyAccessor
*get
,
1252 ObjectPropertyAccessor
*set
,
1253 ObjectPropertyRelease
*release
,
1254 void *opaque
, Error
**errp
)
1256 ObjectProperty
*prop
;
1257 size_t name_len
= strlen(name
);
1259 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1261 ObjectProperty
*ret
= NULL
;
1262 char *name_no_array
= g_strdup(name
);
1264 name_no_array
[name_len
- 3] = '\0';
1265 for (i
= 0; i
< INT16_MAX
; ++i
) {
1266 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1268 ret
= object_property_try_add(obj
, full_name
, type
, get
, set
,
1269 release
, opaque
, NULL
);
1275 g_free(name_no_array
);
1280 if (object_property_find(obj
, name
) != NULL
) {
1281 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1282 name
, object_get_typename(obj
));
1286 prop
= g_malloc0(sizeof(*prop
));
1288 prop
->name
= g_strdup(name
);
1289 prop
->type
= g_strdup(type
);
1293 prop
->release
= release
;
1294 prop
->opaque
= opaque
;
1296 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1301 object_property_add(Object
*obj
, const char *name
, const char *type
,
1302 ObjectPropertyAccessor
*get
,
1303 ObjectPropertyAccessor
*set
,
1304 ObjectPropertyRelease
*release
,
1307 return object_property_try_add(obj
, name
, type
, get
, set
, release
,
1308 opaque
, &error_abort
);
1312 object_class_property_add(ObjectClass
*klass
,
1315 ObjectPropertyAccessor
*get
,
1316 ObjectPropertyAccessor
*set
,
1317 ObjectPropertyRelease
*release
,
1320 ObjectProperty
*prop
;
1322 assert(!object_class_property_find(klass
, name
));
1324 prop
= g_malloc0(sizeof(*prop
));
1326 prop
->name
= g_strdup(name
);
1327 prop
->type
= g_strdup(type
);
1331 prop
->release
= release
;
1332 prop
->opaque
= opaque
;
1334 g_hash_table_insert(klass
->properties
, prop
->name
, prop
);
1339 ObjectProperty
*object_property_find(Object
*obj
, const char *name
)
1341 ObjectProperty
*prop
;
1342 ObjectClass
*klass
= object_get_class(obj
);
1344 prop
= object_class_property_find(klass
, name
);
1349 return g_hash_table_lookup(obj
->properties
, name
);
1352 ObjectProperty
*object_property_find_err(Object
*obj
, const char *name
,
1355 ObjectProperty
*prop
= object_property_find(obj
, name
);
1357 error_setg(errp
, "Property '%s.%s' not found",
1358 object_get_typename(obj
), name
);
1363 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1366 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1367 iter
->nextclass
= object_get_class(obj
);
1370 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1373 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1374 if (!iter
->nextclass
) {
1377 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1378 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1383 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1386 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1387 iter
->nextclass
= object_class_get_parent(klass
);
1390 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
)
1392 ObjectClass
*parent_klass
;
1394 parent_klass
= object_class_get_parent(klass
);
1396 ObjectProperty
*prop
=
1397 object_class_property_find(parent_klass
, name
);
1403 return g_hash_table_lookup(klass
->properties
, name
);
1406 ObjectProperty
*object_class_property_find_err(ObjectClass
*klass
,
1410 ObjectProperty
*prop
= object_class_property_find(klass
, name
);
1412 error_setg(errp
, "Property '.%s' not found", name
);
1418 void object_property_del(Object
*obj
, const char *name
)
1420 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1422 if (prop
->release
) {
1423 prop
->release(obj
, name
, prop
->opaque
);
1425 g_hash_table_remove(obj
->properties
, name
);
1428 bool object_property_get(Object
*obj
, const char *name
, Visitor
*v
,
1432 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1439 error_setg(errp
, "Property '%s.%s' is not readable",
1440 object_get_typename(obj
), name
);
1443 prop
->get(obj
, v
, name
, prop
->opaque
, &err
);
1444 error_propagate(errp
, err
);
1448 bool object_property_set(Object
*obj
, const char *name
, Visitor
*v
,
1452 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1459 error_setg(errp
, "Property '%s.%s' is not writable",
1460 object_get_typename(obj
), name
);
1463 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1467 bool object_property_set_str(Object
*obj
, const char *name
,
1468 const char *value
, Error
**errp
)
1470 QString
*qstr
= qstring_from_str(value
);
1471 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qstr
), errp
);
1473 qobject_unref(qstr
);
1477 char *object_property_get_str(Object
*obj
, const char *name
,
1480 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1487 qstring
= qobject_to(QString
, ret
);
1489 error_setg(errp
, "Invalid parameter type for '%s', expected: string",
1493 retval
= g_strdup(qstring_get_str(qstring
));
1500 bool object_property_set_link(Object
*obj
, const char *name
,
1501 Object
*value
, Error
**errp
)
1503 g_autofree
char *path
= NULL
;
1506 path
= object_get_canonical_path(value
);
1508 return object_property_set_str(obj
, name
, path
?: "", errp
);
1511 Object
*object_property_get_link(Object
*obj
, const char *name
,
1514 char *str
= object_property_get_str(obj
, name
, errp
);
1515 Object
*target
= NULL
;
1518 target
= object_resolve_path(str
, NULL
);
1520 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1521 "Device '%s' not found", str
);
1529 bool object_property_set_bool(Object
*obj
, const char *name
,
1530 bool value
, Error
**errp
)
1532 QBool
*qbool
= qbool_from_bool(value
);
1533 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qbool
), errp
);
1535 qobject_unref(qbool
);
1539 bool object_property_get_bool(Object
*obj
, const char *name
,
1542 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1549 qbool
= qobject_to(QBool
, ret
);
1551 error_setg(errp
, "Invalid parameter type for '%s', expected: boolean",
1555 retval
= qbool_get_bool(qbool
);
1562 bool object_property_set_int(Object
*obj
, const char *name
,
1563 int64_t value
, Error
**errp
)
1565 QNum
*qnum
= qnum_from_int(value
);
1566 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1568 qobject_unref(qnum
);
1572 int64_t object_property_get_int(Object
*obj
, const char *name
,
1575 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1583 qnum
= qobject_to(QNum
, ret
);
1584 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1585 error_setg(errp
, "Invalid parameter type for '%s', expected: int",
1594 static void object_property_init_defval(Object
*obj
, ObjectProperty
*prop
)
1596 Visitor
*v
= qobject_input_visitor_new(prop
->defval
);
1598 assert(prop
->set
!= NULL
);
1599 prop
->set(obj
, v
, prop
->name
, prop
->opaque
, &error_abort
);
1604 static void object_property_set_default(ObjectProperty
*prop
, QObject
*defval
)
1606 assert(!prop
->defval
);
1607 assert(!prop
->init
);
1609 prop
->defval
= defval
;
1610 prop
->init
= object_property_init_defval
;
1613 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
)
1615 object_property_set_default(prop
, QOBJECT(qbool_from_bool(value
)));
1618 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
)
1620 object_property_set_default(prop
, QOBJECT(qstring_from_str(value
)));
1623 void object_property_set_default_list(ObjectProperty
*prop
)
1625 object_property_set_default(prop
, QOBJECT(qlist_new()));
1628 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
)
1630 object_property_set_default(prop
, QOBJECT(qnum_from_int(value
)));
1633 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
)
1635 object_property_set_default(prop
, QOBJECT(qnum_from_uint(value
)));
1638 bool object_property_set_uint(Object
*obj
, const char *name
,
1639 uint64_t value
, Error
**errp
)
1641 QNum
*qnum
= qnum_from_uint(value
);
1642 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1644 qobject_unref(qnum
);
1648 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1651 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1658 qnum
= qobject_to(QNum
, ret
);
1659 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1660 error_setg(errp
, "Invalid parameter type for '%s', expected: uint",
1669 typedef struct EnumProperty
{
1670 const QEnumLookup
*lookup
;
1671 int (*get
)(Object
*, Error
**);
1672 void (*set
)(Object
*, int, Error
**);
1675 int object_property_get_enum(Object
*obj
, const char *name
,
1676 const char *typename
, Error
**errp
)
1680 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1681 EnumProperty
*enumprop
;
1687 if (!g_str_equal(prop
->type
, typename
)) {
1688 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1689 name
, object_class_get_name(
1690 object_get_class(obj
)), typename
);
1694 enumprop
= prop
->opaque
;
1696 str
= object_property_get_str(obj
, name
, errp
);
1701 ret
= qapi_enum_parse(enumprop
->lookup
, str
, -1, errp
);
1707 bool object_property_parse(Object
*obj
, const char *name
,
1708 const char *string
, Error
**errp
)
1710 Visitor
*v
= string_input_visitor_new(string
);
1711 bool ok
= object_property_set(obj
, name
, v
, errp
);
1717 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1721 char *string
= NULL
;
1723 v
= string_output_visitor_new(human
, &string
);
1724 if (!object_property_get(obj
, name
, v
, errp
)) {
1728 visit_complete(v
, &string
);
1735 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1737 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1745 Object
*object_get_root(void)
1747 static Object
*root
;
1750 root
= object_new("container");
1756 Object
*object_get_objects_root(void)
1758 return container_get(object_get_root(), "/objects");
1761 Object
*object_get_internal_root(void)
1763 static Object
*internal_root
;
1765 if (!internal_root
) {
1766 internal_root
= object_new("container");
1769 return internal_root
;
1772 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1773 const char *name
, void *opaque
,
1776 Object
*child
= opaque
;
1779 path
= object_get_canonical_path(child
);
1780 visit_type_str(v
, name
, &path
, errp
);
1784 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
,
1790 static void object_finalize_child_property(Object
*obj
, const char *name
,
1793 Object
*child
= opaque
;
1795 if (child
->class->unparent
) {
1796 (child
->class->unparent
)(child
);
1798 child
->parent
= NULL
;
1799 object_unref(child
);
1803 object_property_try_add_child(Object
*obj
, const char *name
,
1804 Object
*child
, Error
**errp
)
1806 g_autofree
char *type
= NULL
;
1809 assert(!child
->parent
);
1811 type
= g_strdup_printf("child<%s>", object_get_typename(child
));
1813 op
= object_property_try_add(obj
, name
, type
, object_get_child_property
,
1814 NULL
, object_finalize_child_property
,
1819 op
->resolve
= object_resolve_child_property
;
1821 child
->parent
= obj
;
1826 object_property_add_child(Object
*obj
, const char *name
,
1829 return object_property_try_add_child(obj
, name
, child
, &error_abort
);
1832 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1833 Object
*val
, Error
**errp
)
1835 /* Allow the link to be set, always */
1841 Object
*target
; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1842 ptrdiff_t offset
; /* if OBJ_PROP_LINK_CLASS */
1844 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1845 ObjectPropertyLinkFlags flags
;
1849 object_link_get_targetp(Object
*obj
, LinkProperty
*lprop
)
1851 if (lprop
->flags
& OBJ_PROP_LINK_DIRECT
) {
1852 return &lprop
->target
;
1853 } else if (lprop
->flags
& OBJ_PROP_LINK_CLASS
) {
1854 return (void *)obj
+ lprop
->offset
;
1856 return lprop
->targetp
;
1860 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1861 const char *name
, void *opaque
,
1864 LinkProperty
*lprop
= opaque
;
1865 Object
**targetp
= object_link_get_targetp(obj
, lprop
);
1869 path
= object_get_canonical_path(*targetp
);
1870 visit_type_str(v
, name
, &path
, errp
);
1874 visit_type_str(v
, name
, &path
, errp
);
1879 * object_resolve_link:
1881 * Lookup an object and ensure its type matches the link property type. This
1882 * is similar to object_resolve_path() except type verification against the
1883 * link property is performed.
1885 * Returns: The matched object or NULL on path lookup failures.
1887 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1888 const char *path
, Error
**errp
)
1892 bool ambiguous
= false;
1895 /* Go from link<FOO> to FOO. */
1896 type
= object_property_get_type(obj
, name
, NULL
);
1897 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1898 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1901 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1903 } else if (!target
) {
1904 target
= object_resolve_path(path
, &ambiguous
);
1905 if (target
|| ambiguous
) {
1906 error_setg(errp
, "Invalid parameter type for '%s', expected: %s",
1909 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1910 "Device '%s' not found", path
);
1914 g_free(target_type
);
1919 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1920 const char *name
, void *opaque
,
1923 Error
*local_err
= NULL
;
1924 LinkProperty
*prop
= opaque
;
1925 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1926 Object
*old_target
= *targetp
;
1930 if (!visit_type_str(v
, name
, &path
, errp
)) {
1935 new_target
= object_resolve_link(obj
, name
, path
, errp
);
1946 prop
->check(obj
, name
, new_target
, &local_err
);
1948 error_propagate(errp
, local_err
);
1952 *targetp
= new_target
;
1953 if (prop
->flags
& OBJ_PROP_LINK_STRONG
) {
1954 object_ref(new_target
);
1955 object_unref(old_target
);
1959 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
,
1962 LinkProperty
*lprop
= opaque
;
1964 return *object_link_get_targetp(parent
, lprop
);
1967 static void object_release_link_property(Object
*obj
, const char *name
,
1970 LinkProperty
*prop
= opaque
;
1971 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1973 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *targetp
) {
1974 object_unref(*targetp
);
1976 if (!(prop
->flags
& OBJ_PROP_LINK_CLASS
)) {
1981 static ObjectProperty
*
1982 object_add_link_prop(Object
*obj
, const char *name
,
1983 const char *type
, void *ptr
,
1984 void (*check
)(const Object
*, const char *,
1985 Object
*, Error
**),
1986 ObjectPropertyLinkFlags flags
)
1988 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1989 g_autofree
char *full_type
= NULL
;
1992 if (flags
& OBJ_PROP_LINK_DIRECT
) {
1995 prop
->targetp
= ptr
;
1997 prop
->check
= check
;
1998 prop
->flags
= flags
;
2000 full_type
= g_strdup_printf("link<%s>", type
);
2002 op
= object_property_add(obj
, name
, full_type
,
2003 object_get_link_property
,
2004 check
? object_set_link_property
: NULL
,
2005 object_release_link_property
,
2007 op
->resolve
= object_resolve_link_property
;
2012 object_property_add_link(Object
*obj
, const char *name
,
2013 const char *type
, Object
**targetp
,
2014 void (*check
)(const Object
*, const char *,
2015 Object
*, Error
**),
2016 ObjectPropertyLinkFlags flags
)
2018 return object_add_link_prop(obj
, name
, type
, targetp
, check
, flags
);
2022 object_class_property_add_link(ObjectClass
*oc
,
2024 const char *type
, ptrdiff_t offset
,
2025 void (*check
)(const Object
*obj
, const char *name
,
2026 Object
*val
, Error
**errp
),
2027 ObjectPropertyLinkFlags flags
)
2029 LinkProperty
*prop
= g_new0(LinkProperty
, 1);
2033 prop
->offset
= offset
;
2034 prop
->check
= check
;
2035 prop
->flags
= flags
| OBJ_PROP_LINK_CLASS
;
2037 full_type
= g_strdup_printf("link<%s>", type
);
2039 op
= object_class_property_add(oc
, name
, full_type
,
2040 object_get_link_property
,
2041 check
? object_set_link_property
: NULL
,
2042 object_release_link_property
,
2045 op
->resolve
= object_resolve_link_property
;
2052 object_property_add_const_link(Object
*obj
, const char *name
,
2055 return object_add_link_prop(obj
, name
,
2056 object_get_typename(target
), target
,
2057 NULL
, OBJ_PROP_LINK_DIRECT
);
2060 const char *object_get_canonical_path_component(const Object
*obj
)
2062 ObjectProperty
*prop
= NULL
;
2063 GHashTableIter iter
;
2065 if (obj
->parent
== NULL
) {
2069 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
2070 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2071 if (!object_property_is_child(prop
)) {
2075 if (prop
->opaque
== obj
) {
2080 /* obj had a parent but was not a child, should never happen */
2081 g_assert_not_reached();
2085 char *object_get_canonical_path(const Object
*obj
)
2087 Object
*root
= object_get_root();
2088 char *newpath
, *path
= NULL
;
2091 return g_strdup("/");
2095 const char *component
= object_get_canonical_path_component(obj
);
2098 /* A canonical path must be complete, so discard what was
2105 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
2109 } while (obj
!= root
);
2114 Object
*object_resolve_path_component(Object
*parent
, const char *part
)
2116 ObjectProperty
*prop
= object_property_find(parent
, part
);
2121 if (prop
->resolve
) {
2122 return prop
->resolve(parent
, prop
->opaque
, part
);
2128 static Object
*object_resolve_abs_path(Object
*parent
,
2130 const char *typename
)
2134 if (*parts
== NULL
) {
2135 return object_dynamic_cast(parent
, typename
);
2138 if (strcmp(*parts
, "") == 0) {
2139 return object_resolve_abs_path(parent
, parts
+ 1, typename
);
2142 child
= object_resolve_path_component(parent
, *parts
);
2147 return object_resolve_abs_path(child
, parts
+ 1, typename
);
2150 static Object
*object_resolve_partial_path(Object
*parent
,
2152 const char *typename
,
2156 GHashTableIter iter
;
2157 ObjectProperty
*prop
;
2159 obj
= object_resolve_abs_path(parent
, parts
, typename
);
2161 g_hash_table_iter_init(&iter
, parent
->properties
);
2162 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2165 if (!object_property_is_child(prop
)) {
2169 found
= object_resolve_partial_path(prop
->opaque
, parts
,
2170 typename
, ambiguous
);
2187 Object
*object_resolve_path_type(const char *path
, const char *typename
,
2193 parts
= g_strsplit(path
, "/", 0);
2196 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
2197 bool ambiguous
= false;
2198 obj
= object_resolve_partial_path(object_get_root(), parts
,
2199 typename
, &ambiguous
);
2201 *ambiguousp
= ambiguous
;
2204 obj
= object_resolve_abs_path(object_get_root(), parts
+ 1, typename
);
2212 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
2214 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
2217 Object
*object_resolve_path_at(Object
*parent
, const char *path
)
2219 g_auto(GStrv
) parts
= g_strsplit(path
, "/", 0);
2222 return object_resolve_abs_path(object_get_root(), parts
+ 1,
2225 return object_resolve_abs_path(parent
, parts
, TYPE_OBJECT
);
2228 Object
*object_resolve_type_unambiguous(const char *typename
, Error
**errp
)
2231 Object
*o
= object_resolve_path_type("", typename
, &ambig
);
2234 error_setg(errp
, "More than one object of type %s", typename
);
2238 error_setg(errp
, "No object found of type %s", typename
);
2244 typedef struct StringProperty
2246 char *(*get
)(Object
*, Error
**);
2247 void (*set
)(Object
*, const char *, Error
**);
2250 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
2251 void *opaque
, Error
**errp
)
2253 StringProperty
*prop
= opaque
;
2257 value
= prop
->get(obj
, &err
);
2259 error_propagate(errp
, err
);
2263 visit_type_str(v
, name
, &value
, errp
);
2267 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
2268 void *opaque
, Error
**errp
)
2270 StringProperty
*prop
= opaque
;
2273 if (!visit_type_str(v
, name
, &value
, errp
)) {
2277 prop
->set(obj
, value
, errp
);
2281 static void property_release_data(Object
*obj
, const char *name
,
2288 object_property_add_str(Object
*obj
, const char *name
,
2289 char *(*get
)(Object
*, Error
**),
2290 void (*set
)(Object
*, const char *, Error
**))
2292 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2297 return object_property_add(obj
, name
, "string",
2298 get
? property_get_str
: NULL
,
2299 set
? property_set_str
: NULL
,
2300 property_release_data
,
2305 object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2306 char *(*get
)(Object
*, Error
**),
2307 void (*set
)(Object
*, const char *,
2310 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2315 return object_class_property_add(klass
, name
, "string",
2316 get
? property_get_str
: NULL
,
2317 set
? property_set_str
: NULL
,
2322 typedef struct BoolProperty
2324 bool (*get
)(Object
*, Error
**);
2325 void (*set
)(Object
*, bool, Error
**);
2328 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2329 void *opaque
, Error
**errp
)
2331 BoolProperty
*prop
= opaque
;
2335 value
= prop
->get(obj
, &err
);
2337 error_propagate(errp
, err
);
2341 visit_type_bool(v
, name
, &value
, errp
);
2344 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2345 void *opaque
, Error
**errp
)
2347 BoolProperty
*prop
= opaque
;
2350 if (!visit_type_bool(v
, name
, &value
, errp
)) {
2354 prop
->set(obj
, value
, errp
);
2358 object_property_add_bool(Object
*obj
, const char *name
,
2359 bool (*get
)(Object
*, Error
**),
2360 void (*set
)(Object
*, bool, Error
**))
2362 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2367 return object_property_add(obj
, name
, "bool",
2368 get
? property_get_bool
: NULL
,
2369 set
? property_set_bool
: NULL
,
2370 property_release_data
,
2375 object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2376 bool (*get
)(Object
*, Error
**),
2377 void (*set
)(Object
*, bool, Error
**))
2379 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2384 return object_class_property_add(klass
, name
, "bool",
2385 get
? property_get_bool
: NULL
,
2386 set
? property_set_bool
: NULL
,
2391 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2392 void *opaque
, Error
**errp
)
2394 EnumProperty
*prop
= opaque
;
2398 value
= prop
->get(obj
, &err
);
2400 error_propagate(errp
, err
);
2404 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2407 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2408 void *opaque
, Error
**errp
)
2410 EnumProperty
*prop
= opaque
;
2413 if (!visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
)) {
2416 prop
->set(obj
, value
, errp
);
2420 object_property_add_enum(Object
*obj
, const char *name
,
2421 const char *typename
,
2422 const QEnumLookup
*lookup
,
2423 int (*get
)(Object
*, Error
**),
2424 void (*set
)(Object
*, int, Error
**))
2426 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2428 prop
->lookup
= lookup
;
2432 return object_property_add(obj
, name
, typename
,
2433 get
? property_get_enum
: NULL
,
2434 set
? property_set_enum
: NULL
,
2435 property_release_data
,
2440 object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2441 const char *typename
,
2442 const QEnumLookup
*lookup
,
2443 int (*get
)(Object
*, Error
**),
2444 void (*set
)(Object
*, int, Error
**))
2446 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2448 prop
->lookup
= lookup
;
2452 return object_class_property_add(klass
, name
, typename
,
2453 get
? property_get_enum
: NULL
,
2454 set
? property_set_enum
: NULL
,
2459 typedef struct TMProperty
{
2460 void (*get
)(Object
*, struct tm
*, Error
**);
2463 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2464 void *opaque
, Error
**errp
)
2466 TMProperty
*prop
= opaque
;
2470 prop
->get(obj
, &value
, &err
);
2472 error_propagate(errp
, err
);
2476 if (!visit_start_struct(v
, name
, NULL
, 0, errp
)) {
2479 if (!visit_type_int32(v
, "tm_year", &value
.tm_year
, errp
)) {
2482 if (!visit_type_int32(v
, "tm_mon", &value
.tm_mon
, errp
)) {
2485 if (!visit_type_int32(v
, "tm_mday", &value
.tm_mday
, errp
)) {
2488 if (!visit_type_int32(v
, "tm_hour", &value
.tm_hour
, errp
)) {
2491 if (!visit_type_int32(v
, "tm_min", &value
.tm_min
, errp
)) {
2494 if (!visit_type_int32(v
, "tm_sec", &value
.tm_sec
, errp
)) {
2497 visit_check_struct(v
, errp
);
2499 visit_end_struct(v
, NULL
);
2503 object_property_add_tm(Object
*obj
, const char *name
,
2504 void (*get
)(Object
*, struct tm
*, Error
**))
2506 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2510 return object_property_add(obj
, name
, "struct tm",
2511 get
? property_get_tm
: NULL
, NULL
,
2512 property_release_data
,
2517 object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2518 void (*get
)(Object
*, struct tm
*, Error
**))
2520 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2524 return object_class_property_add(klass
, name
, "struct tm",
2525 get
? property_get_tm
: NULL
,
2529 static char *object_get_type(Object
*obj
, Error
**errp
)
2531 return g_strdup(object_get_typename(obj
));
2534 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2535 void *opaque
, Error
**errp
)
2537 uint8_t value
= *(uint8_t *)opaque
;
2538 visit_type_uint8(v
, name
, &value
, errp
);
2541 static void property_set_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2542 void *opaque
, Error
**errp
)
2544 uint8_t *field
= opaque
;
2547 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
2554 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2555 void *opaque
, Error
**errp
)
2557 uint16_t value
= *(uint16_t *)opaque
;
2558 visit_type_uint16(v
, name
, &value
, errp
);
2561 static void property_set_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2562 void *opaque
, Error
**errp
)
2564 uint16_t *field
= opaque
;
2567 if (!visit_type_uint16(v
, name
, &value
, errp
)) {
2574 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2575 void *opaque
, Error
**errp
)
2577 uint32_t value
= *(uint32_t *)opaque
;
2578 visit_type_uint32(v
, name
, &value
, errp
);
2581 static void property_set_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2582 void *opaque
, Error
**errp
)
2584 uint32_t *field
= opaque
;
2587 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
2594 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2595 void *opaque
, Error
**errp
)
2597 uint64_t value
= *(uint64_t *)opaque
;
2598 visit_type_uint64(v
, name
, &value
, errp
);
2601 static void property_set_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2602 void *opaque
, Error
**errp
)
2604 uint64_t *field
= opaque
;
2607 if (!visit_type_uint64(v
, name
, &value
, errp
)) {
2615 object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2617 ObjectPropertyFlags flags
)
2619 ObjectPropertyAccessor
*getter
= NULL
;
2620 ObjectPropertyAccessor
*setter
= NULL
;
2622 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2623 getter
= property_get_uint8_ptr
;
2626 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2627 setter
= property_set_uint8_ptr
;
2630 return object_property_add(obj
, name
, "uint8",
2631 getter
, setter
, NULL
, (void *)v
);
2635 object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2637 ObjectPropertyFlags flags
)
2639 ObjectPropertyAccessor
*getter
= NULL
;
2640 ObjectPropertyAccessor
*setter
= NULL
;
2642 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2643 getter
= property_get_uint8_ptr
;
2646 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2647 setter
= property_set_uint8_ptr
;
2650 return object_class_property_add(klass
, name
, "uint8",
2651 getter
, setter
, NULL
, (void *)v
);
2655 object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2657 ObjectPropertyFlags flags
)
2659 ObjectPropertyAccessor
*getter
= NULL
;
2660 ObjectPropertyAccessor
*setter
= NULL
;
2662 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2663 getter
= property_get_uint16_ptr
;
2666 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2667 setter
= property_set_uint16_ptr
;
2670 return object_property_add(obj
, name
, "uint16",
2671 getter
, setter
, NULL
, (void *)v
);
2675 object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2677 ObjectPropertyFlags flags
)
2679 ObjectPropertyAccessor
*getter
= NULL
;
2680 ObjectPropertyAccessor
*setter
= NULL
;
2682 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2683 getter
= property_get_uint16_ptr
;
2686 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2687 setter
= property_set_uint16_ptr
;
2690 return object_class_property_add(klass
, name
, "uint16",
2691 getter
, setter
, NULL
, (void *)v
);
2695 object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2697 ObjectPropertyFlags flags
)
2699 ObjectPropertyAccessor
*getter
= NULL
;
2700 ObjectPropertyAccessor
*setter
= NULL
;
2702 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2703 getter
= property_get_uint32_ptr
;
2706 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2707 setter
= property_set_uint32_ptr
;
2710 return object_property_add(obj
, name
, "uint32",
2711 getter
, setter
, NULL
, (void *)v
);
2715 object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2717 ObjectPropertyFlags flags
)
2719 ObjectPropertyAccessor
*getter
= NULL
;
2720 ObjectPropertyAccessor
*setter
= NULL
;
2722 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2723 getter
= property_get_uint32_ptr
;
2726 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2727 setter
= property_set_uint32_ptr
;
2730 return object_class_property_add(klass
, name
, "uint32",
2731 getter
, setter
, NULL
, (void *)v
);
2735 object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2737 ObjectPropertyFlags flags
)
2739 ObjectPropertyAccessor
*getter
= NULL
;
2740 ObjectPropertyAccessor
*setter
= NULL
;
2742 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2743 getter
= property_get_uint64_ptr
;
2746 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2747 setter
= property_set_uint64_ptr
;
2750 return object_property_add(obj
, name
, "uint64",
2751 getter
, setter
, NULL
, (void *)v
);
2755 object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2757 ObjectPropertyFlags flags
)
2759 ObjectPropertyAccessor
*getter
= NULL
;
2760 ObjectPropertyAccessor
*setter
= NULL
;
2762 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2763 getter
= property_get_uint64_ptr
;
2766 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2767 setter
= property_set_uint64_ptr
;
2770 return object_class_property_add(klass
, name
, "uint64",
2771 getter
, setter
, NULL
, (void *)v
);
2779 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2780 void *opaque
, Error
**errp
)
2782 AliasProperty
*prop
= opaque
;
2783 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2785 object_property_get(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2786 visit_free(alias_v
);
2789 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2790 void *opaque
, Error
**errp
)
2792 AliasProperty
*prop
= opaque
;
2793 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2795 object_property_set(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2796 visit_free(alias_v
);
2799 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2802 AliasProperty
*prop
= opaque
;
2804 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2807 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2809 AliasProperty
*prop
= opaque
;
2811 g_free(prop
->target_name
);
2816 object_property_add_alias(Object
*obj
, const char *name
,
2817 Object
*target_obj
, const char *target_name
)
2819 AliasProperty
*prop
;
2821 ObjectProperty
*target_prop
;
2822 g_autofree
char *prop_type
= NULL
;
2824 target_prop
= object_property_find_err(target_obj
, target_name
,
2827 if (object_property_is_child(target_prop
)) {
2828 prop_type
= g_strdup_printf("link%s",
2829 target_prop
->type
+ strlen("child"));
2831 prop_type
= g_strdup(target_prop
->type
);
2834 prop
= g_malloc(sizeof(*prop
));
2835 prop
->target_obj
= target_obj
;
2836 prop
->target_name
= g_strdup(target_name
);
2838 op
= object_property_add(obj
, name
, prop_type
,
2841 property_release_alias
,
2843 op
->resolve
= property_resolve_alias
;
2844 if (target_prop
->defval
) {
2845 op
->defval
= qobject_ref(target_prop
->defval
);
2848 object_property_set_description(obj
, op
->name
,
2849 target_prop
->description
);
2853 void object_property_set_description(Object
*obj
, const char *name
,
2854 const char *description
)
2858 op
= object_property_find_err(obj
, name
, &error_abort
);
2859 g_free(op
->description
);
2860 op
->description
= g_strdup(description
);
2863 void object_class_property_set_description(ObjectClass
*klass
,
2865 const char *description
)
2869 op
= g_hash_table_lookup(klass
->properties
, name
);
2870 g_free(op
->description
);
2871 op
->description
= g_strdup(description
);
2874 static void object_class_init(ObjectClass
*klass
, void *data
)
2876 object_class_property_add_str(klass
, "type", object_get_type
,
2880 static void register_types(void)
2882 static const TypeInfo interface_info
= {
2883 .name
= TYPE_INTERFACE
,
2884 .class_size
= sizeof(InterfaceClass
),
2888 static const TypeInfo object_info
= {
2889 .name
= TYPE_OBJECT
,
2890 .instance_size
= sizeof(Object
),
2891 .class_init
= object_class_init
,
2895 type_interface
= type_register_internal(&interface_info
);
2896 type_register_internal(&object_info
);
2899 type_init(register_types
)