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/qerror.h"
27 #include "qapi/qmp/qjson.h"
30 /* TODO: replace QObject with a simpler visitor to avoid a dependency
31 * of the QOM core on QObject? */
32 #include "qom/qom-qobject.h"
33 #include "qapi/qmp/qbool.h"
34 #include "qapi/qmp/qlist.h"
35 #include "qapi/qmp/qnum.h"
36 #include "qapi/qmp/qstring.h"
37 #include "qemu/error-report.h"
39 #define MAX_INTERFACES 32
41 typedef struct InterfaceImpl InterfaceImpl
;
42 typedef struct TypeImpl TypeImpl
;
56 size_t instance_align
;
58 void (*class_init
)(ObjectClass
*klass
, void *data
);
59 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
63 void (*instance_init
)(Object
*obj
);
64 void (*instance_post_init
)(Object
*obj
);
65 void (*instance_finalize
)(Object
*obj
);
70 TypeImpl
*parent_type
;
75 InterfaceImpl interfaces
[MAX_INTERFACES
];
78 static Type type_interface
;
80 static GHashTable
*type_table_get(void)
82 static GHashTable
*type_table
;
84 if (type_table
== NULL
) {
85 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
91 static bool enumerating_types
;
93 static void type_table_add(TypeImpl
*ti
)
95 assert(!enumerating_types
);
96 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
99 static TypeImpl
*type_table_lookup(const char *name
)
101 return g_hash_table_lookup(type_table_get(), name
);
104 static TypeImpl
*type_new(const TypeInfo
*info
)
106 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
109 g_assert(info
->name
!= NULL
);
111 if (type_table_lookup(info
->name
) != NULL
) {
112 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
116 ti
->name
= g_strdup(info
->name
);
117 ti
->parent
= g_strdup(info
->parent
);
119 ti
->class_size
= info
->class_size
;
120 ti
->instance_size
= info
->instance_size
;
121 ti
->instance_align
= info
->instance_align
;
123 ti
->class_init
= info
->class_init
;
124 ti
->class_base_init
= info
->class_base_init
;
125 ti
->class_data
= info
->class_data
;
127 ti
->instance_init
= info
->instance_init
;
128 ti
->instance_post_init
= info
->instance_post_init
;
129 ti
->instance_finalize
= info
->instance_finalize
;
131 ti
->abstract
= info
->abstract
;
133 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
134 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
136 ti
->num_interfaces
= i
;
141 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
150 TypeImpl
*type_register(const TypeInfo
*info
)
152 assert(info
->parent
);
153 return type_register_internal(info
);
156 TypeImpl
*type_register_static(const TypeInfo
*info
)
158 return type_register(info
);
161 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
165 for (i
= 0; i
< nr_infos
; i
++) {
166 type_register_static(&infos
[i
]);
170 static TypeImpl
*type_get_by_name(const char *name
)
176 return type_table_lookup(name
);
179 static TypeImpl
*type_get_parent(TypeImpl
*type
)
181 if (!type
->parent_type
&& type
->parent
) {
182 type
->parent_type
= type_get_by_name(type
->parent
);
183 if (!type
->parent_type
) {
184 fprintf(stderr
, "Type '%s' is missing its parent '%s'\n",
185 type
->name
, type
->parent
);
190 return type
->parent_type
;
193 static bool type_has_parent(TypeImpl
*type
)
195 return (type
->parent
!= NULL
);
198 static size_t type_class_get_size(TypeImpl
*ti
)
200 if (ti
->class_size
) {
201 return ti
->class_size
;
204 if (type_has_parent(ti
)) {
205 return type_class_get_size(type_get_parent(ti
));
208 return sizeof(ObjectClass
);
211 static size_t type_object_get_size(TypeImpl
*ti
)
213 if (ti
->instance_size
) {
214 return ti
->instance_size
;
217 if (type_has_parent(ti
)) {
218 return type_object_get_size(type_get_parent(ti
));
224 static size_t type_object_get_align(TypeImpl
*ti
)
226 if (ti
->instance_align
) {
227 return ti
->instance_align
;
230 if (type_has_parent(ti
)) {
231 return type_object_get_align(type_get_parent(ti
));
237 size_t object_type_get_instance_size(const char *typename
)
239 TypeImpl
*type
= type_get_by_name(typename
);
241 g_assert(type
!= NULL
);
242 return type_object_get_size(type
);
245 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
249 /* Check if target_type is a direct ancestor of type */
251 if (type
== target_type
) {
255 type
= type_get_parent(type
);
261 static void type_initialize(TypeImpl
*ti
);
263 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
264 TypeImpl
*parent_type
)
266 InterfaceClass
*new_iface
;
268 TypeImpl
*iface_impl
;
270 info
.parent
= parent_type
->name
;
271 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
272 info
.abstract
= true;
274 iface_impl
= type_new(&info
);
275 iface_impl
->parent_type
= parent_type
;
276 type_initialize(iface_impl
);
277 g_free((char *)info
.name
);
279 new_iface
= (InterfaceClass
*)iface_impl
->class;
280 new_iface
->concrete_class
= ti
->class;
281 new_iface
->interface_type
= interface_type
;
283 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
, new_iface
);
286 static void object_property_free(gpointer data
)
288 ObjectProperty
*prop
= data
;
291 qobject_unref(prop
->defval
);
296 g_free(prop
->description
);
300 static void type_initialize(TypeImpl
*ti
)
308 ti
->class_size
= type_class_get_size(ti
);
309 ti
->instance_size
= type_object_get_size(ti
);
310 ti
->instance_align
= type_object_get_align(ti
);
311 /* Any type with zero instance_size is implicitly abstract.
312 * This means interface types are all abstract.
314 if (ti
->instance_size
== 0) {
317 if (type_is_ancestor(ti
, type_interface
)) {
318 assert(ti
->instance_size
== 0);
319 assert(ti
->abstract
);
320 assert(!ti
->instance_init
);
321 assert(!ti
->instance_post_init
);
322 assert(!ti
->instance_finalize
);
323 assert(!ti
->num_interfaces
);
325 ti
->class = g_malloc0(ti
->class_size
);
327 parent
= type_get_parent(ti
);
329 type_initialize(parent
);
333 g_assert(parent
->class_size
<= ti
->class_size
);
334 g_assert(parent
->instance_size
<= ti
->instance_size
);
335 memcpy(ti
->class, parent
->class, parent
->class_size
);
336 ti
->class->interfaces
= NULL
;
338 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
339 InterfaceClass
*iface
= e
->data
;
340 ObjectClass
*klass
= OBJECT_CLASS(iface
);
342 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
345 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
346 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
348 error_report("missing interface '%s' for object '%s'",
349 ti
->interfaces
[i
].typename
, parent
->name
);
352 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
353 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
355 if (type_is_ancestor(target_type
, t
)) {
364 type_initialize_interface(ti
, t
, t
);
368 ti
->class->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
369 object_property_free
);
371 ti
->class->type
= ti
;
374 if (parent
->class_base_init
) {
375 parent
->class_base_init(ti
->class, ti
->class_data
);
377 parent
= type_get_parent(parent
);
380 if (ti
->class_init
) {
381 ti
->class_init(ti
->class, ti
->class_data
);
385 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
387 if (type_has_parent(ti
)) {
388 object_init_with_type(obj
, type_get_parent(ti
));
391 if (ti
->instance_init
) {
392 ti
->instance_init(obj
);
396 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
398 if (ti
->instance_post_init
) {
399 ti
->instance_post_init(obj
);
402 if (type_has_parent(ti
)) {
403 object_post_init_with_type(obj
, type_get_parent(ti
));
407 bool object_apply_global_props(Object
*obj
, const GPtrArray
*props
,
416 for (i
= 0; i
< props
->len
; i
++) {
417 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
420 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
423 if (p
->optional
&& !object_property_find(obj
, p
->property
)) {
427 if (!object_property_parse(obj
, p
->property
, p
->value
, &err
)) {
428 error_prepend(&err
, "can't apply global %s.%s=%s: ",
429 p
->driver
, p
->property
, p
->value
);
431 * If errp != NULL, propagate error and return.
432 * If errp == NULL, report a warning, but keep going
433 * with the remaining globals.
436 error_propagate(errp
, err
);
439 warn_report_err(err
);
448 * Global property defaults
449 * Slot 0: accelerator's global property defaults
450 * Slot 1: machine's global property defaults
451 * Slot 2: global properties from legacy command line option
452 * Each is a GPtrArray of of GlobalProperty.
453 * Applied in order, later entries override earlier ones.
455 static GPtrArray
*object_compat_props
[3];
458 * Retrieve @GPtrArray for global property defined with options
459 * other than "-global". These are generally used for syntactic
460 * sugar and legacy command line options.
462 void object_register_sugar_prop(const char *driver
, const char *prop
,
463 const char *value
, bool optional
)
466 if (!object_compat_props
[2]) {
467 object_compat_props
[2] = g_ptr_array_new();
469 g
= g_new0(GlobalProperty
, 1);
470 g
->driver
= g_strdup(driver
);
471 g
->property
= g_strdup(prop
);
472 g
->value
= g_strdup(value
);
473 g
->optional
= optional
;
474 g_ptr_array_add(object_compat_props
[2], g
);
478 * Set machine's global property defaults to @compat_props.
479 * May be called at most once.
481 void object_set_machine_compat_props(GPtrArray
*compat_props
)
483 assert(!object_compat_props
[1]);
484 object_compat_props
[1] = compat_props
;
488 * Set accelerator's global property defaults to @compat_props.
489 * May be called at most once.
491 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
493 assert(!object_compat_props
[0]);
494 object_compat_props
[0] = compat_props
;
497 void object_apply_compat_props(Object
*obj
)
501 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
502 object_apply_global_props(obj
, object_compat_props
[i
],
503 i
== 2 ? &error_fatal
: &error_abort
);
507 static void object_class_property_init_all(Object
*obj
)
509 ObjectPropertyIterator iter
;
510 ObjectProperty
*prop
;
512 object_class_property_iter_init(&iter
, object_get_class(obj
));
513 while ((prop
= object_property_iter_next(&iter
))) {
515 prop
->init(obj
, prop
);
520 static void object_initialize_with_type(Object
*obj
, size_t size
, TypeImpl
*type
)
522 type_initialize(type
);
524 g_assert(type
->instance_size
>= sizeof(Object
));
525 g_assert(type
->abstract
== false);
526 g_assert(size
>= type
->instance_size
);
528 memset(obj
, 0, type
->instance_size
);
529 obj
->class = type
->class;
531 object_class_property_init_all(obj
);
532 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
533 NULL
, object_property_free
);
534 object_init_with_type(obj
, type
);
535 object_post_init_with_type(obj
, type
);
538 void object_initialize(void *data
, size_t size
, const char *typename
)
540 TypeImpl
*type
= type_get_by_name(typename
);
542 #ifdef CONFIG_MODULES
544 int rv
= module_load_qom(typename
, &error_fatal
);
546 type
= type_get_by_name(typename
);
548 error_report("missing object type '%s'", typename
);
554 error_report("missing object type '%s'", typename
);
558 object_initialize_with_type(data
, size
, type
);
561 bool object_initialize_child_with_props(Object
*parentobj
,
562 const char *propname
,
563 void *childobj
, size_t size
,
570 va_start(vargs
, errp
);
571 ok
= object_initialize_child_with_propsv(parentobj
, propname
,
572 childobj
, size
, type
, errp
,
578 bool object_initialize_child_with_propsv(Object
*parentobj
,
579 const char *propname
,
580 void *childobj
, size_t size
,
582 Error
**errp
, va_list vargs
)
588 object_initialize(childobj
, size
, type
);
589 obj
= OBJECT(childobj
);
591 if (!object_set_propv(obj
, errp
, vargs
)) {
595 object_property_add_child(parentobj
, propname
, obj
);
597 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
599 if (!user_creatable_complete(uc
, errp
)) {
600 object_unparent(obj
);
609 * We want @obj's reference to be 1 on success, 0 on failure.
610 * On success, it's 2: one taken by object_initialize(), and one
611 * by object_property_add_child().
612 * On failure in object_initialize() or earlier, it's 1.
613 * On failure afterwards, it's also 1: object_unparent() releases
614 * the reference taken by object_property_add_child().
620 void object_initialize_child_internal(Object
*parent
,
621 const char *propname
,
622 void *child
, size_t size
,
625 object_initialize_child_with_props(parent
, propname
, child
, size
, type
,
629 static inline bool object_property_is_child(ObjectProperty
*prop
)
631 return strstart(prop
->type
, "child<", NULL
);
634 static void object_property_del_all(Object
*obj
)
636 g_autoptr(GHashTable
) done
= g_hash_table_new(NULL
, NULL
);
637 ObjectProperty
*prop
;
638 ObjectPropertyIterator iter
;
643 object_property_iter_init(&iter
, obj
);
644 while ((prop
= object_property_iter_next(&iter
)) != NULL
) {
645 if (g_hash_table_add(done
, prop
)) {
647 prop
->release(obj
, prop
->name
, prop
->opaque
);
655 g_hash_table_unref(obj
->properties
);
658 static void object_property_del_child(Object
*obj
, Object
*child
)
660 ObjectProperty
*prop
;
664 g_hash_table_iter_init(&iter
, obj
->properties
);
665 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
667 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
669 prop
->release(obj
, prop
->name
, prop
->opaque
);
670 prop
->release
= NULL
;
675 g_hash_table_iter_init(&iter
, obj
->properties
);
676 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
678 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
679 g_hash_table_iter_remove(&iter
);
685 void object_unparent(Object
*obj
)
688 object_property_del_child(obj
->parent
, obj
);
692 static void object_deinit(Object
*obj
, TypeImpl
*type
)
694 if (type
->instance_finalize
) {
695 type
->instance_finalize(obj
);
698 if (type_has_parent(type
)) {
699 object_deinit(obj
, type_get_parent(type
));
703 static void object_finalize(void *data
)
706 TypeImpl
*ti
= obj
->class->type
;
708 object_property_del_all(obj
);
709 object_deinit(obj
, ti
);
711 g_assert(obj
->ref
== 0);
712 g_assert(obj
->parent
== NULL
);
718 /* Find the minimum alignment guaranteed by the system malloc. */
719 #if __STDC_VERSION__ >= 201112L
720 typedef max_align_t qemu_max_align_t
;
730 static Object
*object_new_with_type(Type type
)
734 void (*obj_free
)(void *);
736 g_assert(type
!= NULL
);
737 type_initialize(type
);
739 size
= type
->instance_size
;
740 align
= type
->instance_align
;
743 * Do not use qemu_memalign unless required. Depending on the
744 * implementation, extra alignment implies extra overhead.
746 if (likely(align
<= __alignof__(qemu_max_align_t
))) {
747 obj
= g_malloc(size
);
750 obj
= qemu_memalign(align
, size
);
751 obj_free
= qemu_vfree
;
754 object_initialize_with_type(obj
, size
, type
);
755 obj
->free
= obj_free
;
760 Object
*object_new_with_class(ObjectClass
*klass
)
762 return object_new_with_type(klass
->type
);
765 Object
*object_new(const char *typename
)
767 TypeImpl
*ti
= type_get_by_name(typename
);
769 return object_new_with_type(ti
);
773 Object
*object_new_with_props(const char *typename
,
782 va_start(vargs
, errp
);
783 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
790 Object
*object_new_with_propv(const char *typename
,
800 klass
= object_class_by_name(typename
);
802 error_setg(errp
, "invalid object type: %s", typename
);
806 if (object_class_is_abstract(klass
)) {
807 error_setg(errp
, "object type '%s' is abstract", typename
);
810 obj
= object_new_with_type(klass
->type
);
812 if (!object_set_propv(obj
, errp
, vargs
)) {
817 object_property_add_child(parent
, id
, obj
);
820 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
822 if (!user_creatable_complete(uc
, errp
)) {
824 object_unparent(obj
);
839 bool object_set_props(Object
*obj
,
846 va_start(vargs
, errp
);
847 ret
= object_set_propv(obj
, errp
, vargs
);
854 bool object_set_propv(Object
*obj
,
858 const char *propname
;
860 propname
= va_arg(vargs
, char *);
861 while (propname
!= NULL
) {
862 const char *value
= va_arg(vargs
, char *);
864 g_assert(value
!= NULL
);
865 if (!object_property_parse(obj
, propname
, value
, errp
)) {
868 propname
= va_arg(vargs
, char *);
875 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
877 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
884 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
885 const char *file
, int line
, const char *func
)
887 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
888 typename
, file
, line
, func
);
890 #ifdef CONFIG_QOM_CAST_DEBUG
894 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
895 if (qatomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
900 inst
= object_dynamic_cast(obj
, typename
);
903 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
904 file
, line
, func
, obj
, typename
);
910 if (obj
&& obj
== inst
) {
911 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
912 qatomic_set(&obj
->class->object_cast_cache
[i
- 1],
913 qatomic_read(&obj
->class->object_cast_cache
[i
]));
915 qatomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
923 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
924 const char *typename
)
926 ObjectClass
*ret
= NULL
;
927 TypeImpl
*target_type
;
934 /* A simple fast path that can trigger a lot for leaf classes. */
936 if (type
->name
== typename
) {
940 target_type
= type_get_by_name(typename
);
942 /* target class type unknown, so fail the cast */
946 if (type
->class->interfaces
&&
947 type_is_ancestor(target_type
, type_interface
)) {
951 for (i
= class->interfaces
; i
; i
= i
->next
) {
952 ObjectClass
*target_class
= i
->data
;
954 if (type_is_ancestor(target_class
->type
, target_type
)) {
960 /* The match was ambiguous, don't allow a cast */
964 } else if (type_is_ancestor(type
, target_type
)) {
971 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
972 const char *typename
,
973 const char *file
, int line
,
978 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
979 typename
, file
, line
, func
);
981 #ifdef CONFIG_QOM_CAST_DEBUG
984 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
985 if (qatomic_read(&class->class_cast_cache
[i
]) == typename
) {
991 if (!class || !class->interfaces
) {
996 ret
= object_class_dynamic_cast(class, typename
);
998 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
999 file
, line
, func
, class, typename
);
1003 #ifdef CONFIG_QOM_CAST_DEBUG
1004 if (class && ret
== class) {
1005 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
1006 qatomic_set(&class->class_cast_cache
[i
- 1],
1007 qatomic_read(&class->class_cast_cache
[i
]));
1009 qatomic_set(&class->class_cast_cache
[i
- 1], typename
);
1016 const char *object_get_typename(const Object
*obj
)
1018 return obj
->class->type
->name
;
1021 ObjectClass
*object_get_class(Object
*obj
)
1026 bool object_class_is_abstract(ObjectClass
*klass
)
1028 return klass
->type
->abstract
;
1031 const char *object_class_get_name(ObjectClass
*klass
)
1033 return klass
->type
->name
;
1036 ObjectClass
*object_class_by_name(const char *typename
)
1038 TypeImpl
*type
= type_get_by_name(typename
);
1044 type_initialize(type
);
1049 ObjectClass
*module_object_class_by_name(const char *typename
)
1053 oc
= object_class_by_name(typename
);
1054 #ifdef CONFIG_MODULES
1056 Error
*local_err
= NULL
;
1057 int rv
= module_load_qom(typename
, &local_err
);
1059 oc
= object_class_by_name(typename
);
1060 } else if (rv
< 0) {
1061 error_report_err(local_err
);
1068 ObjectClass
*object_class_get_parent(ObjectClass
*class)
1070 TypeImpl
*type
= type_get_parent(class->type
);
1076 type_initialize(type
);
1081 typedef struct OCFData
1083 void (*fn
)(ObjectClass
*klass
, void *opaque
);
1084 const char *implements_type
;
1085 bool include_abstract
;
1089 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
1092 OCFData
*data
= opaque
;
1093 TypeImpl
*type
= value
;
1096 type_initialize(type
);
1099 if (!data
->include_abstract
&& type
->abstract
) {
1103 if (data
->implements_type
&&
1104 !object_class_dynamic_cast(k
, data
->implements_type
)) {
1108 data
->fn(k
, data
->opaque
);
1111 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1112 const char *implements_type
, bool include_abstract
,
1115 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1117 enumerating_types
= true;
1118 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1119 enumerating_types
= false;
1122 static int do_object_child_foreach(Object
*obj
,
1123 int (*fn
)(Object
*child
, void *opaque
),
1124 void *opaque
, bool recurse
)
1126 GHashTableIter iter
;
1127 ObjectProperty
*prop
;
1130 g_hash_table_iter_init(&iter
, obj
->properties
);
1131 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1132 if (object_property_is_child(prop
)) {
1133 Object
*child
= prop
->opaque
;
1135 ret
= fn(child
, opaque
);
1140 ret
= do_object_child_foreach(child
, fn
, opaque
, true);
1150 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1153 return do_object_child_foreach(obj
, fn
, opaque
, false);
1156 int object_child_foreach_recursive(Object
*obj
,
1157 int (*fn
)(Object
*child
, void *opaque
),
1160 return do_object_child_foreach(obj
, fn
, opaque
, true);
1163 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1165 GSList
**list
= opaque
;
1167 *list
= g_slist_prepend(*list
, klass
);
1170 GSList
*object_class_get_list(const char *implements_type
,
1171 bool include_abstract
)
1173 GSList
*list
= NULL
;
1175 object_class_foreach(object_class_get_list_tramp
,
1176 implements_type
, include_abstract
, &list
);
1180 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1182 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1183 object_class_get_name((ObjectClass
*)b
));
1186 GSList
*object_class_get_list_sorted(const char *implements_type
,
1187 bool include_abstract
)
1189 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1193 Object
*object_ref(void *objptr
)
1195 Object
*obj
= OBJECT(objptr
);
1201 ref
= qatomic_fetch_inc(&obj
->ref
);
1202 /* Assert waaay before the integer overflows */
1203 g_assert(ref
< INT_MAX
);
1207 void object_unref(void *objptr
)
1209 Object
*obj
= OBJECT(objptr
);
1213 g_assert(obj
->ref
> 0);
1215 /* parent always holds a reference to its children */
1216 if (qatomic_fetch_dec(&obj
->ref
) == 1) {
1217 object_finalize(obj
);
1222 object_property_try_add(Object
*obj
, const char *name
, const char *type
,
1223 ObjectPropertyAccessor
*get
,
1224 ObjectPropertyAccessor
*set
,
1225 ObjectPropertyRelease
*release
,
1226 void *opaque
, Error
**errp
)
1228 ObjectProperty
*prop
;
1229 size_t name_len
= strlen(name
);
1231 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1233 ObjectProperty
*ret
= NULL
;
1234 char *name_no_array
= g_strdup(name
);
1236 name_no_array
[name_len
- 3] = '\0';
1237 for (i
= 0; i
< INT16_MAX
; ++i
) {
1238 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1240 ret
= object_property_try_add(obj
, full_name
, type
, get
, set
,
1241 release
, opaque
, NULL
);
1247 g_free(name_no_array
);
1252 if (object_property_find(obj
, name
) != NULL
) {
1253 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1254 name
, object_get_typename(obj
));
1258 prop
= g_malloc0(sizeof(*prop
));
1260 prop
->name
= g_strdup(name
);
1261 prop
->type
= g_strdup(type
);
1265 prop
->release
= release
;
1266 prop
->opaque
= opaque
;
1268 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1273 object_property_add(Object
*obj
, const char *name
, const char *type
,
1274 ObjectPropertyAccessor
*get
,
1275 ObjectPropertyAccessor
*set
,
1276 ObjectPropertyRelease
*release
,
1279 return object_property_try_add(obj
, name
, type
, get
, set
, release
,
1280 opaque
, &error_abort
);
1284 object_class_property_add(ObjectClass
*klass
,
1287 ObjectPropertyAccessor
*get
,
1288 ObjectPropertyAccessor
*set
,
1289 ObjectPropertyRelease
*release
,
1292 ObjectProperty
*prop
;
1294 assert(!object_class_property_find(klass
, name
));
1296 prop
= g_malloc0(sizeof(*prop
));
1298 prop
->name
= g_strdup(name
);
1299 prop
->type
= g_strdup(type
);
1303 prop
->release
= release
;
1304 prop
->opaque
= opaque
;
1306 g_hash_table_insert(klass
->properties
, prop
->name
, prop
);
1311 ObjectProperty
*object_property_find(Object
*obj
, const char *name
)
1313 ObjectProperty
*prop
;
1314 ObjectClass
*klass
= object_get_class(obj
);
1316 prop
= object_class_property_find(klass
, name
);
1321 return g_hash_table_lookup(obj
->properties
, name
);
1324 ObjectProperty
*object_property_find_err(Object
*obj
, const char *name
,
1327 ObjectProperty
*prop
= object_property_find(obj
, name
);
1329 error_setg(errp
, "Property '%s.%s' not found",
1330 object_get_typename(obj
), name
);
1335 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1338 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1339 iter
->nextclass
= object_get_class(obj
);
1342 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1345 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1346 if (!iter
->nextclass
) {
1349 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1350 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1355 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1358 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1359 iter
->nextclass
= object_class_get_parent(klass
);
1362 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
)
1364 ObjectClass
*parent_klass
;
1366 parent_klass
= object_class_get_parent(klass
);
1368 ObjectProperty
*prop
=
1369 object_class_property_find(parent_klass
, name
);
1375 return g_hash_table_lookup(klass
->properties
, name
);
1378 ObjectProperty
*object_class_property_find_err(ObjectClass
*klass
,
1382 ObjectProperty
*prop
= object_class_property_find(klass
, name
);
1384 error_setg(errp
, "Property '.%s' not found", name
);
1390 void object_property_del(Object
*obj
, const char *name
)
1392 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1394 if (prop
->release
) {
1395 prop
->release(obj
, name
, prop
->opaque
);
1397 g_hash_table_remove(obj
->properties
, name
);
1400 bool object_property_get(Object
*obj
, const char *name
, Visitor
*v
,
1404 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1411 error_setg(errp
, "Property '%s.%s' is not readable",
1412 object_get_typename(obj
), name
);
1415 prop
->get(obj
, v
, name
, prop
->opaque
, &err
);
1416 error_propagate(errp
, err
);
1420 bool object_property_set(Object
*obj
, const char *name
, Visitor
*v
,
1424 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1431 error_setg(errp
, "Property '%s.%s' is not writable",
1432 object_get_typename(obj
), name
);
1435 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1439 bool object_property_set_str(Object
*obj
, const char *name
,
1440 const char *value
, Error
**errp
)
1442 QString
*qstr
= qstring_from_str(value
);
1443 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qstr
), errp
);
1445 qobject_unref(qstr
);
1449 char *object_property_get_str(Object
*obj
, const char *name
,
1452 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1459 qstring
= qobject_to(QString
, ret
);
1461 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1464 retval
= g_strdup(qstring_get_str(qstring
));
1471 bool object_property_set_link(Object
*obj
, const char *name
,
1472 Object
*value
, Error
**errp
)
1474 g_autofree
char *path
= NULL
;
1477 path
= object_get_canonical_path(value
);
1479 return object_property_set_str(obj
, name
, path
?: "", errp
);
1482 Object
*object_property_get_link(Object
*obj
, const char *name
,
1485 char *str
= object_property_get_str(obj
, name
, errp
);
1486 Object
*target
= NULL
;
1489 target
= object_resolve_path(str
, NULL
);
1491 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1492 "Device '%s' not found", str
);
1500 bool object_property_set_bool(Object
*obj
, const char *name
,
1501 bool value
, Error
**errp
)
1503 QBool
*qbool
= qbool_from_bool(value
);
1504 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qbool
), errp
);
1506 qobject_unref(qbool
);
1510 bool object_property_get_bool(Object
*obj
, const char *name
,
1513 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1520 qbool
= qobject_to(QBool
, ret
);
1522 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1525 retval
= qbool_get_bool(qbool
);
1532 bool object_property_set_int(Object
*obj
, const char *name
,
1533 int64_t value
, Error
**errp
)
1535 QNum
*qnum
= qnum_from_int(value
);
1536 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1538 qobject_unref(qnum
);
1542 int64_t object_property_get_int(Object
*obj
, const char *name
,
1545 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1553 qnum
= qobject_to(QNum
, ret
);
1554 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1555 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1563 static void object_property_init_defval(Object
*obj
, ObjectProperty
*prop
)
1565 Visitor
*v
= qobject_input_visitor_new(prop
->defval
);
1567 assert(prop
->set
!= NULL
);
1568 prop
->set(obj
, v
, prop
->name
, prop
->opaque
, &error_abort
);
1573 static void object_property_set_default(ObjectProperty
*prop
, QObject
*defval
)
1575 assert(!prop
->defval
);
1576 assert(!prop
->init
);
1578 prop
->defval
= defval
;
1579 prop
->init
= object_property_init_defval
;
1582 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
)
1584 object_property_set_default(prop
, QOBJECT(qbool_from_bool(value
)));
1587 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
)
1589 object_property_set_default(prop
, QOBJECT(qstring_from_str(value
)));
1592 void object_property_set_default_list(ObjectProperty
*prop
)
1594 object_property_set_default(prop
, QOBJECT(qlist_new()));
1597 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
)
1599 object_property_set_default(prop
, QOBJECT(qnum_from_int(value
)));
1602 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
)
1604 object_property_set_default(prop
, QOBJECT(qnum_from_uint(value
)));
1607 bool object_property_set_uint(Object
*obj
, const char *name
,
1608 uint64_t value
, Error
**errp
)
1610 QNum
*qnum
= qnum_from_uint(value
);
1611 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1613 qobject_unref(qnum
);
1617 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1620 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1627 qnum
= qobject_to(QNum
, ret
);
1628 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1629 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1637 typedef struct EnumProperty
{
1638 const QEnumLookup
*lookup
;
1639 int (*get
)(Object
*, Error
**);
1640 void (*set
)(Object
*, int, Error
**);
1643 int object_property_get_enum(Object
*obj
, const char *name
,
1644 const char *typename
, Error
**errp
)
1648 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1649 EnumProperty
*enumprop
;
1655 if (!g_str_equal(prop
->type
, typename
)) {
1656 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1657 name
, object_class_get_name(
1658 object_get_class(obj
)), typename
);
1662 enumprop
= prop
->opaque
;
1664 str
= object_property_get_str(obj
, name
, errp
);
1669 ret
= qapi_enum_parse(enumprop
->lookup
, str
, -1, errp
);
1675 bool object_property_parse(Object
*obj
, const char *name
,
1676 const char *string
, Error
**errp
)
1678 Visitor
*v
= string_input_visitor_new(string
);
1679 bool ok
= object_property_set(obj
, name
, v
, errp
);
1685 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1689 char *string
= NULL
;
1691 v
= string_output_visitor_new(human
, &string
);
1692 if (!object_property_get(obj
, name
, v
, errp
)) {
1696 visit_complete(v
, &string
);
1703 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1705 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1713 Object
*object_get_root(void)
1715 static Object
*root
;
1718 root
= object_new("container");
1724 Object
*object_get_objects_root(void)
1726 return container_get(object_get_root(), "/objects");
1729 Object
*object_get_internal_root(void)
1731 static Object
*internal_root
;
1733 if (!internal_root
) {
1734 internal_root
= object_new("container");
1737 return internal_root
;
1740 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1741 const char *name
, void *opaque
,
1744 Object
*child
= opaque
;
1747 path
= object_get_canonical_path(child
);
1748 visit_type_str(v
, name
, &path
, errp
);
1752 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
,
1758 static void object_finalize_child_property(Object
*obj
, const char *name
,
1761 Object
*child
= opaque
;
1763 if (child
->class->unparent
) {
1764 (child
->class->unparent
)(child
);
1766 child
->parent
= NULL
;
1767 object_unref(child
);
1771 object_property_try_add_child(Object
*obj
, const char *name
,
1772 Object
*child
, Error
**errp
)
1774 g_autofree
char *type
= NULL
;
1777 assert(!child
->parent
);
1779 type
= g_strdup_printf("child<%s>", object_get_typename(child
));
1781 op
= object_property_try_add(obj
, name
, type
, object_get_child_property
,
1782 NULL
, object_finalize_child_property
,
1787 op
->resolve
= object_resolve_child_property
;
1789 child
->parent
= obj
;
1794 object_property_add_child(Object
*obj
, const char *name
,
1797 return object_property_try_add_child(obj
, name
, child
, &error_abort
);
1800 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1801 Object
*val
, Error
**errp
)
1803 /* Allow the link to be set, always */
1809 Object
*target
; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1810 ptrdiff_t offset
; /* if OBJ_PROP_LINK_CLASS */
1812 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1813 ObjectPropertyLinkFlags flags
;
1817 object_link_get_targetp(Object
*obj
, LinkProperty
*lprop
)
1819 if (lprop
->flags
& OBJ_PROP_LINK_DIRECT
) {
1820 return &lprop
->target
;
1821 } else if (lprop
->flags
& OBJ_PROP_LINK_CLASS
) {
1822 return (void *)obj
+ lprop
->offset
;
1824 return lprop
->targetp
;
1828 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1829 const char *name
, void *opaque
,
1832 LinkProperty
*lprop
= opaque
;
1833 Object
**targetp
= object_link_get_targetp(obj
, lprop
);
1837 path
= object_get_canonical_path(*targetp
);
1838 visit_type_str(v
, name
, &path
, errp
);
1842 visit_type_str(v
, name
, &path
, errp
);
1847 * object_resolve_link:
1849 * Lookup an object and ensure its type matches the link property type. This
1850 * is similar to object_resolve_path() except type verification against the
1851 * link property is performed.
1853 * Returns: The matched object or NULL on path lookup failures.
1855 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1856 const char *path
, Error
**errp
)
1860 bool ambiguous
= false;
1863 /* Go from link<FOO> to FOO. */
1864 type
= object_property_get_type(obj
, name
, NULL
);
1865 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1866 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1869 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1871 } else if (!target
) {
1872 target
= object_resolve_path(path
, &ambiguous
);
1873 if (target
|| ambiguous
) {
1874 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1876 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1877 "Device '%s' not found", path
);
1881 g_free(target_type
);
1886 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1887 const char *name
, void *opaque
,
1890 Error
*local_err
= NULL
;
1891 LinkProperty
*prop
= opaque
;
1892 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1893 Object
*old_target
= *targetp
;
1897 if (!visit_type_str(v
, name
, &path
, errp
)) {
1902 new_target
= object_resolve_link(obj
, name
, path
, errp
);
1913 prop
->check(obj
, name
, new_target
, &local_err
);
1915 error_propagate(errp
, local_err
);
1919 *targetp
= new_target
;
1920 if (prop
->flags
& OBJ_PROP_LINK_STRONG
) {
1921 object_ref(new_target
);
1922 object_unref(old_target
);
1926 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
,
1929 LinkProperty
*lprop
= opaque
;
1931 return *object_link_get_targetp(parent
, lprop
);
1934 static void object_release_link_property(Object
*obj
, const char *name
,
1937 LinkProperty
*prop
= opaque
;
1938 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1940 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *targetp
) {
1941 object_unref(*targetp
);
1943 if (!(prop
->flags
& OBJ_PROP_LINK_CLASS
)) {
1948 static ObjectProperty
*
1949 object_add_link_prop(Object
*obj
, const char *name
,
1950 const char *type
, void *ptr
,
1951 void (*check
)(const Object
*, const char *,
1952 Object
*, Error
**),
1953 ObjectPropertyLinkFlags flags
)
1955 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1956 g_autofree
char *full_type
= NULL
;
1959 if (flags
& OBJ_PROP_LINK_DIRECT
) {
1962 prop
->targetp
= ptr
;
1964 prop
->check
= check
;
1965 prop
->flags
= flags
;
1967 full_type
= g_strdup_printf("link<%s>", type
);
1969 op
= object_property_add(obj
, name
, full_type
,
1970 object_get_link_property
,
1971 check
? object_set_link_property
: NULL
,
1972 object_release_link_property
,
1974 op
->resolve
= object_resolve_link_property
;
1979 object_property_add_link(Object
*obj
, const char *name
,
1980 const char *type
, Object
**targetp
,
1981 void (*check
)(const Object
*, const char *,
1982 Object
*, Error
**),
1983 ObjectPropertyLinkFlags flags
)
1985 return object_add_link_prop(obj
, name
, type
, targetp
, check
, flags
);
1989 object_class_property_add_link(ObjectClass
*oc
,
1991 const char *type
, ptrdiff_t offset
,
1992 void (*check
)(const Object
*obj
, const char *name
,
1993 Object
*val
, Error
**errp
),
1994 ObjectPropertyLinkFlags flags
)
1996 LinkProperty
*prop
= g_new0(LinkProperty
, 1);
2000 prop
->offset
= offset
;
2001 prop
->check
= check
;
2002 prop
->flags
= flags
| OBJ_PROP_LINK_CLASS
;
2004 full_type
= g_strdup_printf("link<%s>", type
);
2006 op
= object_class_property_add(oc
, name
, full_type
,
2007 object_get_link_property
,
2008 check
? object_set_link_property
: NULL
,
2009 object_release_link_property
,
2012 op
->resolve
= object_resolve_link_property
;
2019 object_property_add_const_link(Object
*obj
, const char *name
,
2022 return object_add_link_prop(obj
, name
,
2023 object_get_typename(target
), target
,
2024 NULL
, OBJ_PROP_LINK_DIRECT
);
2027 const char *object_get_canonical_path_component(const Object
*obj
)
2029 ObjectProperty
*prop
= NULL
;
2030 GHashTableIter iter
;
2032 if (obj
->parent
== NULL
) {
2036 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
2037 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2038 if (!object_property_is_child(prop
)) {
2042 if (prop
->opaque
== obj
) {
2047 /* obj had a parent but was not a child, should never happen */
2048 g_assert_not_reached();
2052 char *object_get_canonical_path(const Object
*obj
)
2054 Object
*root
= object_get_root();
2055 char *newpath
, *path
= NULL
;
2058 return g_strdup("/");
2062 const char *component
= object_get_canonical_path_component(obj
);
2065 /* A canonical path must be complete, so discard what was
2072 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
2076 } while (obj
!= root
);
2081 Object
*object_resolve_path_component(Object
*parent
, const char *part
)
2083 ObjectProperty
*prop
= object_property_find(parent
, part
);
2088 if (prop
->resolve
) {
2089 return prop
->resolve(parent
, prop
->opaque
, part
);
2095 static Object
*object_resolve_abs_path(Object
*parent
,
2097 const char *typename
)
2101 if (*parts
== NULL
) {
2102 return object_dynamic_cast(parent
, typename
);
2105 if (strcmp(*parts
, "") == 0) {
2106 return object_resolve_abs_path(parent
, parts
+ 1, typename
);
2109 child
= object_resolve_path_component(parent
, *parts
);
2114 return object_resolve_abs_path(child
, parts
+ 1, typename
);
2117 static Object
*object_resolve_partial_path(Object
*parent
,
2119 const char *typename
,
2123 GHashTableIter iter
;
2124 ObjectProperty
*prop
;
2126 obj
= object_resolve_abs_path(parent
, parts
, typename
);
2128 g_hash_table_iter_init(&iter
, parent
->properties
);
2129 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2132 if (!object_property_is_child(prop
)) {
2136 found
= object_resolve_partial_path(prop
->opaque
, parts
,
2137 typename
, ambiguous
);
2154 Object
*object_resolve_path_type(const char *path
, const char *typename
,
2160 parts
= g_strsplit(path
, "/", 0);
2163 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
2164 bool ambiguous
= false;
2165 obj
= object_resolve_partial_path(object_get_root(), parts
,
2166 typename
, &ambiguous
);
2168 *ambiguousp
= ambiguous
;
2171 obj
= object_resolve_abs_path(object_get_root(), parts
+ 1, typename
);
2179 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
2181 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
2184 Object
*object_resolve_path_at(Object
*parent
, const char *path
)
2186 g_auto(GStrv
) parts
= g_strsplit(path
, "/", 0);
2189 return object_resolve_abs_path(object_get_root(), parts
+ 1,
2192 return object_resolve_abs_path(parent
, parts
, TYPE_OBJECT
);
2195 typedef struct StringProperty
2197 char *(*get
)(Object
*, Error
**);
2198 void (*set
)(Object
*, const char *, Error
**);
2201 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
2202 void *opaque
, Error
**errp
)
2204 StringProperty
*prop
= opaque
;
2208 value
= prop
->get(obj
, &err
);
2210 error_propagate(errp
, err
);
2214 visit_type_str(v
, name
, &value
, errp
);
2218 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
2219 void *opaque
, Error
**errp
)
2221 StringProperty
*prop
= opaque
;
2224 if (!visit_type_str(v
, name
, &value
, errp
)) {
2228 prop
->set(obj
, value
, errp
);
2232 static void property_release_data(Object
*obj
, const char *name
,
2239 object_property_add_str(Object
*obj
, const char *name
,
2240 char *(*get
)(Object
*, Error
**),
2241 void (*set
)(Object
*, const char *, Error
**))
2243 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2248 return object_property_add(obj
, name
, "string",
2249 get
? property_get_str
: NULL
,
2250 set
? property_set_str
: NULL
,
2251 property_release_data
,
2256 object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2257 char *(*get
)(Object
*, Error
**),
2258 void (*set
)(Object
*, const char *,
2261 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2266 return object_class_property_add(klass
, name
, "string",
2267 get
? property_get_str
: NULL
,
2268 set
? property_set_str
: NULL
,
2273 typedef struct BoolProperty
2275 bool (*get
)(Object
*, Error
**);
2276 void (*set
)(Object
*, bool, Error
**);
2279 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2280 void *opaque
, Error
**errp
)
2282 BoolProperty
*prop
= opaque
;
2286 value
= prop
->get(obj
, &err
);
2288 error_propagate(errp
, err
);
2292 visit_type_bool(v
, name
, &value
, errp
);
2295 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2296 void *opaque
, Error
**errp
)
2298 BoolProperty
*prop
= opaque
;
2301 if (!visit_type_bool(v
, name
, &value
, errp
)) {
2305 prop
->set(obj
, value
, errp
);
2309 object_property_add_bool(Object
*obj
, const char *name
,
2310 bool (*get
)(Object
*, Error
**),
2311 void (*set
)(Object
*, bool, Error
**))
2313 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2318 return object_property_add(obj
, name
, "bool",
2319 get
? property_get_bool
: NULL
,
2320 set
? property_set_bool
: NULL
,
2321 property_release_data
,
2326 object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2327 bool (*get
)(Object
*, Error
**),
2328 void (*set
)(Object
*, bool, Error
**))
2330 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2335 return object_class_property_add(klass
, name
, "bool",
2336 get
? property_get_bool
: NULL
,
2337 set
? property_set_bool
: NULL
,
2342 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2343 void *opaque
, Error
**errp
)
2345 EnumProperty
*prop
= opaque
;
2349 value
= prop
->get(obj
, &err
);
2351 error_propagate(errp
, err
);
2355 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2358 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2359 void *opaque
, Error
**errp
)
2361 EnumProperty
*prop
= opaque
;
2364 if (!visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
)) {
2367 prop
->set(obj
, value
, errp
);
2371 object_property_add_enum(Object
*obj
, const char *name
,
2372 const char *typename
,
2373 const QEnumLookup
*lookup
,
2374 int (*get
)(Object
*, Error
**),
2375 void (*set
)(Object
*, int, Error
**))
2377 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2379 prop
->lookup
= lookup
;
2383 return object_property_add(obj
, name
, typename
,
2384 get
? property_get_enum
: NULL
,
2385 set
? property_set_enum
: NULL
,
2386 property_release_data
,
2391 object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2392 const char *typename
,
2393 const QEnumLookup
*lookup
,
2394 int (*get
)(Object
*, Error
**),
2395 void (*set
)(Object
*, int, Error
**))
2397 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2399 prop
->lookup
= lookup
;
2403 return object_class_property_add(klass
, name
, typename
,
2404 get
? property_get_enum
: NULL
,
2405 set
? property_set_enum
: NULL
,
2410 typedef struct TMProperty
{
2411 void (*get
)(Object
*, struct tm
*, Error
**);
2414 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2415 void *opaque
, Error
**errp
)
2417 TMProperty
*prop
= opaque
;
2421 prop
->get(obj
, &value
, &err
);
2423 error_propagate(errp
, err
);
2427 if (!visit_start_struct(v
, name
, NULL
, 0, errp
)) {
2430 if (!visit_type_int32(v
, "tm_year", &value
.tm_year
, errp
)) {
2433 if (!visit_type_int32(v
, "tm_mon", &value
.tm_mon
, errp
)) {
2436 if (!visit_type_int32(v
, "tm_mday", &value
.tm_mday
, errp
)) {
2439 if (!visit_type_int32(v
, "tm_hour", &value
.tm_hour
, errp
)) {
2442 if (!visit_type_int32(v
, "tm_min", &value
.tm_min
, errp
)) {
2445 if (!visit_type_int32(v
, "tm_sec", &value
.tm_sec
, errp
)) {
2448 visit_check_struct(v
, errp
);
2450 visit_end_struct(v
, NULL
);
2454 object_property_add_tm(Object
*obj
, const char *name
,
2455 void (*get
)(Object
*, struct tm
*, Error
**))
2457 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2461 return object_property_add(obj
, name
, "struct tm",
2462 get
? property_get_tm
: NULL
, NULL
,
2463 property_release_data
,
2468 object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2469 void (*get
)(Object
*, struct tm
*, Error
**))
2471 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2475 return object_class_property_add(klass
, name
, "struct tm",
2476 get
? property_get_tm
: NULL
,
2480 static char *object_get_type(Object
*obj
, Error
**errp
)
2482 return g_strdup(object_get_typename(obj
));
2485 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2486 void *opaque
, Error
**errp
)
2488 uint8_t value
= *(uint8_t *)opaque
;
2489 visit_type_uint8(v
, name
, &value
, errp
);
2492 static void property_set_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2493 void *opaque
, Error
**errp
)
2495 uint8_t *field
= opaque
;
2498 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
2505 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2506 void *opaque
, Error
**errp
)
2508 uint16_t value
= *(uint16_t *)opaque
;
2509 visit_type_uint16(v
, name
, &value
, errp
);
2512 static void property_set_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2513 void *opaque
, Error
**errp
)
2515 uint16_t *field
= opaque
;
2518 if (!visit_type_uint16(v
, name
, &value
, errp
)) {
2525 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2526 void *opaque
, Error
**errp
)
2528 uint32_t value
= *(uint32_t *)opaque
;
2529 visit_type_uint32(v
, name
, &value
, errp
);
2532 static void property_set_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2533 void *opaque
, Error
**errp
)
2535 uint32_t *field
= opaque
;
2538 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
2545 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2546 void *opaque
, Error
**errp
)
2548 uint64_t value
= *(uint64_t *)opaque
;
2549 visit_type_uint64(v
, name
, &value
, errp
);
2552 static void property_set_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2553 void *opaque
, Error
**errp
)
2555 uint64_t *field
= opaque
;
2558 if (!visit_type_uint64(v
, name
, &value
, errp
)) {
2566 object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2568 ObjectPropertyFlags flags
)
2570 ObjectPropertyAccessor
*getter
= NULL
;
2571 ObjectPropertyAccessor
*setter
= NULL
;
2573 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2574 getter
= property_get_uint8_ptr
;
2577 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2578 setter
= property_set_uint8_ptr
;
2581 return object_property_add(obj
, name
, "uint8",
2582 getter
, setter
, NULL
, (void *)v
);
2586 object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2588 ObjectPropertyFlags flags
)
2590 ObjectPropertyAccessor
*getter
= NULL
;
2591 ObjectPropertyAccessor
*setter
= NULL
;
2593 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2594 getter
= property_get_uint8_ptr
;
2597 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2598 setter
= property_set_uint8_ptr
;
2601 return object_class_property_add(klass
, name
, "uint8",
2602 getter
, setter
, NULL
, (void *)v
);
2606 object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2608 ObjectPropertyFlags flags
)
2610 ObjectPropertyAccessor
*getter
= NULL
;
2611 ObjectPropertyAccessor
*setter
= NULL
;
2613 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2614 getter
= property_get_uint16_ptr
;
2617 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2618 setter
= property_set_uint16_ptr
;
2621 return object_property_add(obj
, name
, "uint16",
2622 getter
, setter
, NULL
, (void *)v
);
2626 object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2628 ObjectPropertyFlags flags
)
2630 ObjectPropertyAccessor
*getter
= NULL
;
2631 ObjectPropertyAccessor
*setter
= NULL
;
2633 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2634 getter
= property_get_uint16_ptr
;
2637 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2638 setter
= property_set_uint16_ptr
;
2641 return object_class_property_add(klass
, name
, "uint16",
2642 getter
, setter
, NULL
, (void *)v
);
2646 object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2648 ObjectPropertyFlags flags
)
2650 ObjectPropertyAccessor
*getter
= NULL
;
2651 ObjectPropertyAccessor
*setter
= NULL
;
2653 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2654 getter
= property_get_uint32_ptr
;
2657 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2658 setter
= property_set_uint32_ptr
;
2661 return object_property_add(obj
, name
, "uint32",
2662 getter
, setter
, NULL
, (void *)v
);
2666 object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2668 ObjectPropertyFlags flags
)
2670 ObjectPropertyAccessor
*getter
= NULL
;
2671 ObjectPropertyAccessor
*setter
= NULL
;
2673 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2674 getter
= property_get_uint32_ptr
;
2677 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2678 setter
= property_set_uint32_ptr
;
2681 return object_class_property_add(klass
, name
, "uint32",
2682 getter
, setter
, NULL
, (void *)v
);
2686 object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2688 ObjectPropertyFlags flags
)
2690 ObjectPropertyAccessor
*getter
= NULL
;
2691 ObjectPropertyAccessor
*setter
= NULL
;
2693 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2694 getter
= property_get_uint64_ptr
;
2697 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2698 setter
= property_set_uint64_ptr
;
2701 return object_property_add(obj
, name
, "uint64",
2702 getter
, setter
, NULL
, (void *)v
);
2706 object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2708 ObjectPropertyFlags flags
)
2710 ObjectPropertyAccessor
*getter
= NULL
;
2711 ObjectPropertyAccessor
*setter
= NULL
;
2713 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2714 getter
= property_get_uint64_ptr
;
2717 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2718 setter
= property_set_uint64_ptr
;
2721 return object_class_property_add(klass
, name
, "uint64",
2722 getter
, setter
, NULL
, (void *)v
);
2730 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2731 void *opaque
, Error
**errp
)
2733 AliasProperty
*prop
= opaque
;
2734 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2736 object_property_get(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2737 visit_free(alias_v
);
2740 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2741 void *opaque
, Error
**errp
)
2743 AliasProperty
*prop
= opaque
;
2744 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2746 object_property_set(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2747 visit_free(alias_v
);
2750 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2753 AliasProperty
*prop
= opaque
;
2755 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2758 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2760 AliasProperty
*prop
= opaque
;
2762 g_free(prop
->target_name
);
2767 object_property_add_alias(Object
*obj
, const char *name
,
2768 Object
*target_obj
, const char *target_name
)
2770 AliasProperty
*prop
;
2772 ObjectProperty
*target_prop
;
2773 g_autofree
char *prop_type
= NULL
;
2775 target_prop
= object_property_find_err(target_obj
, target_name
,
2778 if (object_property_is_child(target_prop
)) {
2779 prop_type
= g_strdup_printf("link%s",
2780 target_prop
->type
+ strlen("child"));
2782 prop_type
= g_strdup(target_prop
->type
);
2785 prop
= g_malloc(sizeof(*prop
));
2786 prop
->target_obj
= target_obj
;
2787 prop
->target_name
= g_strdup(target_name
);
2789 op
= object_property_add(obj
, name
, prop_type
,
2792 property_release_alias
,
2794 op
->resolve
= property_resolve_alias
;
2795 if (target_prop
->defval
) {
2796 op
->defval
= qobject_ref(target_prop
->defval
);
2799 object_property_set_description(obj
, op
->name
,
2800 target_prop
->description
);
2804 void object_property_set_description(Object
*obj
, const char *name
,
2805 const char *description
)
2809 op
= object_property_find_err(obj
, name
, &error_abort
);
2810 g_free(op
->description
);
2811 op
->description
= g_strdup(description
);
2814 void object_class_property_set_description(ObjectClass
*klass
,
2816 const char *description
)
2820 op
= g_hash_table_lookup(klass
->properties
, name
);
2821 g_free(op
->description
);
2822 op
->description
= g_strdup(description
);
2825 static void object_class_init(ObjectClass
*klass
, void *data
)
2827 object_class_property_add_str(klass
, "type", object_get_type
,
2831 static void register_types(void)
2833 static const TypeInfo interface_info
= {
2834 .name
= TYPE_INTERFACE
,
2835 .class_size
= sizeof(InterfaceClass
),
2839 static const TypeInfo object_info
= {
2840 .name
= TYPE_OBJECT
,
2841 .instance_size
= sizeof(Object
),
2842 .class_init
= object_class_init
,
2846 type_interface
= type_register_internal(&interface_info
);
2847 type_register_internal(&object_info
);
2850 type_init(register_types
)