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/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 TypeImpl
*type_register_internal(const TypeInfo
*info
)
149 TypeImpl
*type_register(const TypeInfo
*info
)
151 assert(info
->parent
);
152 return type_register_internal(info
);
155 TypeImpl
*type_register_static(const TypeInfo
*info
)
157 return type_register(info
);
160 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
164 for (i
= 0; i
< nr_infos
; i
++) {
165 type_register_static(&infos
[i
]);
169 static TypeImpl
*type_get_by_name(const char *name
)
175 return type_table_lookup(name
);
178 static TypeImpl
*type_get_parent(TypeImpl
*type
)
180 if (!type
->parent_type
&& type
->parent
) {
181 type
->parent_type
= type_get_by_name(type
->parent
);
182 if (!type
->parent_type
) {
183 fprintf(stderr
, "Type '%s' is missing its parent '%s'\n",
184 type
->name
, type
->parent
);
189 return type
->parent_type
;
192 static bool type_has_parent(TypeImpl
*type
)
194 return (type
->parent
!= NULL
);
197 static size_t type_class_get_size(TypeImpl
*ti
)
199 if (ti
->class_size
) {
200 return ti
->class_size
;
203 if (type_has_parent(ti
)) {
204 return type_class_get_size(type_get_parent(ti
));
207 return sizeof(ObjectClass
);
210 static size_t type_object_get_size(TypeImpl
*ti
)
212 if (ti
->instance_size
) {
213 return ti
->instance_size
;
216 if (type_has_parent(ti
)) {
217 return type_object_get_size(type_get_parent(ti
));
223 size_t object_type_get_instance_size(const char *typename
)
225 TypeImpl
*type
= type_get_by_name(typename
);
227 g_assert(type
!= NULL
);
228 return type_object_get_size(type
);
231 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
235 /* Check if target_type is a direct ancestor of type */
237 if (type
== target_type
) {
241 type
= type_get_parent(type
);
247 static void type_initialize(TypeImpl
*ti
);
249 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
250 TypeImpl
*parent_type
)
252 InterfaceClass
*new_iface
;
254 TypeImpl
*iface_impl
;
256 info
.parent
= parent_type
->name
;
257 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
258 info
.abstract
= true;
260 iface_impl
= type_new(&info
);
261 iface_impl
->parent_type
= parent_type
;
262 type_initialize(iface_impl
);
263 g_free((char *)info
.name
);
265 new_iface
= (InterfaceClass
*)iface_impl
->class;
266 new_iface
->concrete_class
= ti
->class;
267 new_iface
->interface_type
= interface_type
;
269 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
, new_iface
);
272 static void object_property_free(gpointer data
)
274 ObjectProperty
*prop
= data
;
277 qobject_unref(prop
->defval
);
282 g_free(prop
->description
);
286 static void type_initialize(TypeImpl
*ti
)
294 ti
->class_size
= type_class_get_size(ti
);
295 ti
->instance_size
= type_object_get_size(ti
);
296 /* Any type with zero instance_size is implicitly abstract.
297 * This means interface types are all abstract.
299 if (ti
->instance_size
== 0) {
302 if (type_is_ancestor(ti
, type_interface
)) {
303 assert(ti
->instance_size
== 0);
304 assert(ti
->abstract
);
305 assert(!ti
->instance_init
);
306 assert(!ti
->instance_post_init
);
307 assert(!ti
->instance_finalize
);
308 assert(!ti
->num_interfaces
);
310 ti
->class = g_malloc0(ti
->class_size
);
312 parent
= type_get_parent(ti
);
314 type_initialize(parent
);
318 g_assert(parent
->class_size
<= ti
->class_size
);
319 g_assert(parent
->instance_size
<= ti
->instance_size
);
320 memcpy(ti
->class, parent
->class, parent
->class_size
);
321 ti
->class->interfaces
= NULL
;
323 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
324 InterfaceClass
*iface
= e
->data
;
325 ObjectClass
*klass
= OBJECT_CLASS(iface
);
327 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
330 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
331 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
333 error_report("missing interface '%s' for object '%s'",
334 ti
->interfaces
[i
].typename
, parent
->name
);
337 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
338 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
340 if (type_is_ancestor(target_type
, t
)) {
349 type_initialize_interface(ti
, t
, t
);
353 ti
->class->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
354 object_property_free
);
356 ti
->class->type
= ti
;
359 if (parent
->class_base_init
) {
360 parent
->class_base_init(ti
->class, ti
->class_data
);
362 parent
= type_get_parent(parent
);
365 if (ti
->class_init
) {
366 ti
->class_init(ti
->class, ti
->class_data
);
370 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
372 if (type_has_parent(ti
)) {
373 object_init_with_type(obj
, type_get_parent(ti
));
376 if (ti
->instance_init
) {
377 ti
->instance_init(obj
);
381 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
383 if (ti
->instance_post_init
) {
384 ti
->instance_post_init(obj
);
387 if (type_has_parent(ti
)) {
388 object_post_init_with_type(obj
, type_get_parent(ti
));
392 bool object_apply_global_props(Object
*obj
, const GPtrArray
*props
,
401 for (i
= 0; i
< props
->len
; i
++) {
402 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
405 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
408 if (p
->optional
&& !object_property_find(obj
, p
->property
)) {
412 if (!object_property_parse(obj
, p
->property
, p
->value
, &err
)) {
413 error_prepend(&err
, "can't apply global %s.%s=%s: ",
414 p
->driver
, p
->property
, p
->value
);
416 * If errp != NULL, propagate error and return.
417 * If errp == NULL, report a warning, but keep going
418 * with the remaining globals.
421 error_propagate(errp
, err
);
424 warn_report_err(err
);
433 * Global property defaults
434 * Slot 0: accelerator's global property defaults
435 * Slot 1: machine's global property defaults
436 * Slot 2: global properties from legacy command line option
437 * Each is a GPtrArray of of GlobalProperty.
438 * Applied in order, later entries override earlier ones.
440 static GPtrArray
*object_compat_props
[3];
443 * Retrieve @GPtrArray for global property defined with options
444 * other than "-global". These are generally used for syntactic
445 * sugar and legacy command line options.
447 void object_register_sugar_prop(const char *driver
, const char *prop
,
448 const char *value
, bool optional
)
451 if (!object_compat_props
[2]) {
452 object_compat_props
[2] = g_ptr_array_new();
454 g
= g_new0(GlobalProperty
, 1);
455 g
->driver
= g_strdup(driver
);
456 g
->property
= g_strdup(prop
);
457 g
->value
= g_strdup(value
);
458 g
->optional
= optional
;
459 g_ptr_array_add(object_compat_props
[2], g
);
463 * Set machine's global property defaults to @compat_props.
464 * May be called at most once.
466 void object_set_machine_compat_props(GPtrArray
*compat_props
)
468 assert(!object_compat_props
[1]);
469 object_compat_props
[1] = compat_props
;
473 * Set accelerator's global property defaults to @compat_props.
474 * May be called at most once.
476 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
478 assert(!object_compat_props
[0]);
479 object_compat_props
[0] = compat_props
;
482 void object_apply_compat_props(Object
*obj
)
486 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
487 object_apply_global_props(obj
, object_compat_props
[i
],
488 i
== 2 ? &error_fatal
: &error_abort
);
492 static void object_class_property_init_all(Object
*obj
)
494 ObjectPropertyIterator iter
;
495 ObjectProperty
*prop
;
497 object_class_property_iter_init(&iter
, object_get_class(obj
));
498 while ((prop
= object_property_iter_next(&iter
))) {
500 prop
->init(obj
, prop
);
505 static void object_initialize_with_type(Object
*obj
, size_t size
, TypeImpl
*type
)
507 type_initialize(type
);
509 g_assert(type
->instance_size
>= sizeof(Object
));
510 g_assert(type
->abstract
== false);
511 g_assert(size
>= type
->instance_size
);
513 memset(obj
, 0, type
->instance_size
);
514 obj
->class = type
->class;
516 object_class_property_init_all(obj
);
517 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
518 NULL
, object_property_free
);
519 object_init_with_type(obj
, type
);
520 object_post_init_with_type(obj
, type
);
523 void object_initialize(void *data
, size_t size
, const char *typename
)
525 TypeImpl
*type
= type_get_by_name(typename
);
527 #ifdef CONFIG_MODULES
529 int rv
= module_load_qom(typename
, &error_fatal
);
531 type
= type_get_by_name(typename
);
533 error_report("missing object type '%s'", typename
);
539 error_report("missing object type '%s'", typename
);
543 object_initialize_with_type(data
, size
, type
);
546 bool object_initialize_child_with_props(Object
*parentobj
,
547 const char *propname
,
548 void *childobj
, size_t size
,
555 va_start(vargs
, errp
);
556 ok
= object_initialize_child_with_propsv(parentobj
, propname
,
557 childobj
, size
, type
, errp
,
563 bool object_initialize_child_with_propsv(Object
*parentobj
,
564 const char *propname
,
565 void *childobj
, size_t size
,
567 Error
**errp
, va_list vargs
)
573 object_initialize(childobj
, size
, type
);
574 obj
= OBJECT(childobj
);
576 if (!object_set_propv(obj
, errp
, vargs
)) {
580 object_property_add_child(parentobj
, propname
, obj
);
582 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
584 if (!user_creatable_complete(uc
, errp
)) {
585 object_unparent(obj
);
594 * We want @obj's reference to be 1 on success, 0 on failure.
595 * On success, it's 2: one taken by object_initialize(), and one
596 * by object_property_add_child().
597 * On failure in object_initialize() or earlier, it's 1.
598 * On failure afterwards, it's also 1: object_unparent() releases
599 * the reference taken by object_property_add_child().
605 void object_initialize_child_internal(Object
*parent
,
606 const char *propname
,
607 void *child
, size_t size
,
610 object_initialize_child_with_props(parent
, propname
, child
, size
, type
,
614 static inline bool object_property_is_child(ObjectProperty
*prop
)
616 return strstart(prop
->type
, "child<", NULL
);
619 static void object_property_del_all(Object
*obj
)
621 g_autoptr(GHashTable
) done
= g_hash_table_new(NULL
, NULL
);
622 ObjectProperty
*prop
;
623 ObjectPropertyIterator iter
;
628 object_property_iter_init(&iter
, obj
);
629 while ((prop
= object_property_iter_next(&iter
)) != NULL
) {
630 if (g_hash_table_add(done
, prop
)) {
632 prop
->release(obj
, prop
->name
, prop
->opaque
);
640 g_hash_table_unref(obj
->properties
);
643 static void object_property_del_child(Object
*obj
, Object
*child
)
645 ObjectProperty
*prop
;
649 g_hash_table_iter_init(&iter
, obj
->properties
);
650 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
652 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
654 prop
->release(obj
, prop
->name
, prop
->opaque
);
655 prop
->release
= NULL
;
660 g_hash_table_iter_init(&iter
, obj
->properties
);
661 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
663 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
664 g_hash_table_iter_remove(&iter
);
670 void object_unparent(Object
*obj
)
673 object_property_del_child(obj
->parent
, obj
);
677 static void object_deinit(Object
*obj
, TypeImpl
*type
)
679 if (type
->instance_finalize
) {
680 type
->instance_finalize(obj
);
683 if (type_has_parent(type
)) {
684 object_deinit(obj
, type_get_parent(type
));
688 static void object_finalize(void *data
)
691 TypeImpl
*ti
= obj
->class->type
;
693 object_property_del_all(obj
);
694 object_deinit(obj
, ti
);
696 g_assert(obj
->ref
== 0);
697 g_assert(obj
->parent
== NULL
);
703 /* Find the minimum alignment guaranteed by the system malloc. */
704 #if __STDC_VERSION__ >= 201112L
705 typedef max_align_t qemu_max_align_t
;
715 static Object
*object_new_with_type(Type type
)
719 void (*obj_free
)(void *);
721 g_assert(type
!= NULL
);
722 type_initialize(type
);
724 size
= type
->instance_size
;
725 align
= type
->instance_align
;
728 * Do not use qemu_memalign unless required. Depending on the
729 * implementation, extra alignment implies extra overhead.
731 if (likely(align
<= __alignof__(qemu_max_align_t
))) {
732 obj
= g_malloc(size
);
735 obj
= qemu_memalign(align
, size
);
736 obj_free
= qemu_vfree
;
739 object_initialize_with_type(obj
, size
, type
);
740 obj
->free
= obj_free
;
745 Object
*object_new_with_class(ObjectClass
*klass
)
747 return object_new_with_type(klass
->type
);
750 Object
*object_new(const char *typename
)
752 TypeImpl
*ti
= type_get_by_name(typename
);
754 return object_new_with_type(ti
);
758 Object
*object_new_with_props(const char *typename
,
767 va_start(vargs
, errp
);
768 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
775 Object
*object_new_with_propv(const char *typename
,
785 klass
= object_class_by_name(typename
);
787 error_setg(errp
, "invalid object type: %s", typename
);
791 if (object_class_is_abstract(klass
)) {
792 error_setg(errp
, "object type '%s' is abstract", typename
);
795 obj
= object_new_with_type(klass
->type
);
797 if (!object_set_propv(obj
, errp
, vargs
)) {
802 object_property_add_child(parent
, id
, obj
);
805 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
807 if (!user_creatable_complete(uc
, errp
)) {
809 object_unparent(obj
);
824 bool object_set_props(Object
*obj
,
831 va_start(vargs
, errp
);
832 ret
= object_set_propv(obj
, errp
, vargs
);
839 bool object_set_propv(Object
*obj
,
843 const char *propname
;
845 propname
= va_arg(vargs
, char *);
846 while (propname
!= NULL
) {
847 const char *value
= va_arg(vargs
, char *);
849 g_assert(value
!= NULL
);
850 if (!object_property_parse(obj
, propname
, value
, errp
)) {
853 propname
= va_arg(vargs
, char *);
860 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
862 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
869 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
870 const char *file
, int line
, const char *func
)
872 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
873 typename
, file
, line
, func
);
875 #ifdef CONFIG_QOM_CAST_DEBUG
879 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
880 if (qatomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
885 inst
= object_dynamic_cast(obj
, typename
);
888 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
889 file
, line
, func
, obj
, typename
);
895 if (obj
&& obj
== inst
) {
896 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
897 qatomic_set(&obj
->class->object_cast_cache
[i
- 1],
898 qatomic_read(&obj
->class->object_cast_cache
[i
]));
900 qatomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
908 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
909 const char *typename
)
911 ObjectClass
*ret
= NULL
;
912 TypeImpl
*target_type
;
919 /* A simple fast path that can trigger a lot for leaf classes. */
921 if (type
->name
== typename
) {
925 target_type
= type_get_by_name(typename
);
927 /* target class type unknown, so fail the cast */
931 if (type
->class->interfaces
&&
932 type_is_ancestor(target_type
, type_interface
)) {
936 for (i
= class->interfaces
; i
; i
= i
->next
) {
937 ObjectClass
*target_class
= i
->data
;
939 if (type_is_ancestor(target_class
->type
, target_type
)) {
945 /* The match was ambiguous, don't allow a cast */
949 } else if (type_is_ancestor(type
, target_type
)) {
956 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
957 const char *typename
,
958 const char *file
, int line
,
963 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
964 typename
, file
, line
, func
);
966 #ifdef CONFIG_QOM_CAST_DEBUG
969 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
970 if (qatomic_read(&class->class_cast_cache
[i
]) == typename
) {
976 if (!class || !class->interfaces
) {
981 ret
= object_class_dynamic_cast(class, typename
);
983 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
984 file
, line
, func
, class, typename
);
988 #ifdef CONFIG_QOM_CAST_DEBUG
989 if (class && ret
== class) {
990 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
991 qatomic_set(&class->class_cast_cache
[i
- 1],
992 qatomic_read(&class->class_cast_cache
[i
]));
994 qatomic_set(&class->class_cast_cache
[i
- 1], typename
);
1001 const char *object_get_typename(const Object
*obj
)
1003 return obj
->class->type
->name
;
1006 ObjectClass
*object_get_class(Object
*obj
)
1011 bool object_class_is_abstract(ObjectClass
*klass
)
1013 return klass
->type
->abstract
;
1016 const char *object_class_get_name(ObjectClass
*klass
)
1018 return klass
->type
->name
;
1021 ObjectClass
*object_class_by_name(const char *typename
)
1023 TypeImpl
*type
= type_get_by_name(typename
);
1029 type_initialize(type
);
1034 ObjectClass
*module_object_class_by_name(const char *typename
)
1038 oc
= object_class_by_name(typename
);
1039 #ifdef CONFIG_MODULES
1041 Error
*local_err
= NULL
;
1042 int rv
= module_load_qom(typename
, &local_err
);
1044 oc
= object_class_by_name(typename
);
1045 } else if (rv
< 0) {
1046 error_report_err(local_err
);
1053 ObjectClass
*object_class_get_parent(ObjectClass
*class)
1055 TypeImpl
*type
= type_get_parent(class->type
);
1061 type_initialize(type
);
1066 typedef struct OCFData
1068 void (*fn
)(ObjectClass
*klass
, void *opaque
);
1069 const char *implements_type
;
1070 bool include_abstract
;
1074 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
1077 OCFData
*data
= opaque
;
1078 TypeImpl
*type
= value
;
1081 type_initialize(type
);
1084 if (!data
->include_abstract
&& type
->abstract
) {
1088 if (data
->implements_type
&&
1089 !object_class_dynamic_cast(k
, data
->implements_type
)) {
1093 data
->fn(k
, data
->opaque
);
1096 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1097 const char *implements_type
, bool include_abstract
,
1100 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1102 enumerating_types
= true;
1103 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1104 enumerating_types
= false;
1107 static int do_object_child_foreach(Object
*obj
,
1108 int (*fn
)(Object
*child
, void *opaque
),
1109 void *opaque
, bool recurse
)
1111 GHashTableIter iter
;
1112 ObjectProperty
*prop
;
1115 g_hash_table_iter_init(&iter
, obj
->properties
);
1116 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1117 if (object_property_is_child(prop
)) {
1118 Object
*child
= prop
->opaque
;
1120 ret
= fn(child
, opaque
);
1125 ret
= do_object_child_foreach(child
, fn
, opaque
, true);
1135 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1138 return do_object_child_foreach(obj
, fn
, opaque
, false);
1141 int object_child_foreach_recursive(Object
*obj
,
1142 int (*fn
)(Object
*child
, void *opaque
),
1145 return do_object_child_foreach(obj
, fn
, opaque
, true);
1148 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1150 GSList
**list
= opaque
;
1152 *list
= g_slist_prepend(*list
, klass
);
1155 GSList
*object_class_get_list(const char *implements_type
,
1156 bool include_abstract
)
1158 GSList
*list
= NULL
;
1160 object_class_foreach(object_class_get_list_tramp
,
1161 implements_type
, include_abstract
, &list
);
1165 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1167 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1168 object_class_get_name((ObjectClass
*)b
));
1171 GSList
*object_class_get_list_sorted(const char *implements_type
,
1172 bool include_abstract
)
1174 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1178 Object
*object_ref(void *objptr
)
1180 Object
*obj
= OBJECT(objptr
);
1186 ref
= qatomic_fetch_inc(&obj
->ref
);
1187 /* Assert waaay before the integer overflows */
1188 g_assert(ref
< INT_MAX
);
1192 void object_unref(void *objptr
)
1194 Object
*obj
= OBJECT(objptr
);
1198 g_assert(obj
->ref
> 0);
1200 /* parent always holds a reference to its children */
1201 if (qatomic_fetch_dec(&obj
->ref
) == 1) {
1202 object_finalize(obj
);
1207 object_property_try_add(Object
*obj
, const char *name
, const char *type
,
1208 ObjectPropertyAccessor
*get
,
1209 ObjectPropertyAccessor
*set
,
1210 ObjectPropertyRelease
*release
,
1211 void *opaque
, Error
**errp
)
1213 ObjectProperty
*prop
;
1214 size_t name_len
= strlen(name
);
1216 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1218 ObjectProperty
*ret
= NULL
;
1219 char *name_no_array
= g_strdup(name
);
1221 name_no_array
[name_len
- 3] = '\0';
1222 for (i
= 0; i
< INT16_MAX
; ++i
) {
1223 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1225 ret
= object_property_try_add(obj
, full_name
, type
, get
, set
,
1226 release
, opaque
, NULL
);
1232 g_free(name_no_array
);
1237 if (object_property_find(obj
, name
) != NULL
) {
1238 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1239 name
, object_get_typename(obj
));
1243 prop
= g_malloc0(sizeof(*prop
));
1245 prop
->name
= g_strdup(name
);
1246 prop
->type
= g_strdup(type
);
1250 prop
->release
= release
;
1251 prop
->opaque
= opaque
;
1253 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1258 object_property_add(Object
*obj
, const char *name
, const char *type
,
1259 ObjectPropertyAccessor
*get
,
1260 ObjectPropertyAccessor
*set
,
1261 ObjectPropertyRelease
*release
,
1264 return object_property_try_add(obj
, name
, type
, get
, set
, release
,
1265 opaque
, &error_abort
);
1269 object_class_property_add(ObjectClass
*klass
,
1272 ObjectPropertyAccessor
*get
,
1273 ObjectPropertyAccessor
*set
,
1274 ObjectPropertyRelease
*release
,
1277 ObjectProperty
*prop
;
1279 assert(!object_class_property_find(klass
, name
));
1281 prop
= g_malloc0(sizeof(*prop
));
1283 prop
->name
= g_strdup(name
);
1284 prop
->type
= g_strdup(type
);
1288 prop
->release
= release
;
1289 prop
->opaque
= opaque
;
1291 g_hash_table_insert(klass
->properties
, prop
->name
, prop
);
1296 ObjectProperty
*object_property_find(Object
*obj
, const char *name
)
1298 ObjectProperty
*prop
;
1299 ObjectClass
*klass
= object_get_class(obj
);
1301 prop
= object_class_property_find(klass
, name
);
1306 return g_hash_table_lookup(obj
->properties
, name
);
1309 ObjectProperty
*object_property_find_err(Object
*obj
, const char *name
,
1312 ObjectProperty
*prop
= object_property_find(obj
, name
);
1314 error_setg(errp
, "Property '%s.%s' not found",
1315 object_get_typename(obj
), name
);
1320 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1323 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1324 iter
->nextclass
= object_get_class(obj
);
1327 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1330 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1331 if (!iter
->nextclass
) {
1334 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1335 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1340 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1343 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1344 iter
->nextclass
= object_class_get_parent(klass
);
1347 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
)
1349 ObjectClass
*parent_klass
;
1351 parent_klass
= object_class_get_parent(klass
);
1353 ObjectProperty
*prop
=
1354 object_class_property_find(parent_klass
, name
);
1360 return g_hash_table_lookup(klass
->properties
, name
);
1363 ObjectProperty
*object_class_property_find_err(ObjectClass
*klass
,
1367 ObjectProperty
*prop
= object_class_property_find(klass
, name
);
1369 error_setg(errp
, "Property '.%s' not found", name
);
1375 void object_property_del(Object
*obj
, const char *name
)
1377 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1379 if (prop
->release
) {
1380 prop
->release(obj
, name
, prop
->opaque
);
1382 g_hash_table_remove(obj
->properties
, name
);
1385 bool object_property_get(Object
*obj
, const char *name
, Visitor
*v
,
1389 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1396 error_setg(errp
, "Property '%s.%s' is not readable",
1397 object_get_typename(obj
), name
);
1400 prop
->get(obj
, v
, name
, prop
->opaque
, &err
);
1401 error_propagate(errp
, err
);
1405 bool object_property_set(Object
*obj
, const char *name
, Visitor
*v
,
1409 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1416 error_setg(errp
, "Property '%s.%s' is not writable",
1417 object_get_typename(obj
), name
);
1420 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1424 bool object_property_set_str(Object
*obj
, const char *name
,
1425 const char *value
, Error
**errp
)
1427 QString
*qstr
= qstring_from_str(value
);
1428 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qstr
), errp
);
1430 qobject_unref(qstr
);
1434 char *object_property_get_str(Object
*obj
, const char *name
,
1437 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1444 qstring
= qobject_to(QString
, ret
);
1446 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1449 retval
= g_strdup(qstring_get_str(qstring
));
1456 bool object_property_set_link(Object
*obj
, const char *name
,
1457 Object
*value
, Error
**errp
)
1459 g_autofree
char *path
= NULL
;
1462 path
= object_get_canonical_path(value
);
1464 return object_property_set_str(obj
, name
, path
?: "", errp
);
1467 Object
*object_property_get_link(Object
*obj
, const char *name
,
1470 char *str
= object_property_get_str(obj
, name
, errp
);
1471 Object
*target
= NULL
;
1474 target
= object_resolve_path(str
, NULL
);
1476 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1477 "Device '%s' not found", str
);
1485 bool object_property_set_bool(Object
*obj
, const char *name
,
1486 bool value
, Error
**errp
)
1488 QBool
*qbool
= qbool_from_bool(value
);
1489 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qbool
), errp
);
1491 qobject_unref(qbool
);
1495 bool object_property_get_bool(Object
*obj
, const char *name
,
1498 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1505 qbool
= qobject_to(QBool
, ret
);
1507 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1510 retval
= qbool_get_bool(qbool
);
1517 bool object_property_set_int(Object
*obj
, const char *name
,
1518 int64_t value
, Error
**errp
)
1520 QNum
*qnum
= qnum_from_int(value
);
1521 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1523 qobject_unref(qnum
);
1527 int64_t object_property_get_int(Object
*obj
, const char *name
,
1530 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1538 qnum
= qobject_to(QNum
, ret
);
1539 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1540 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1548 static void object_property_init_defval(Object
*obj
, ObjectProperty
*prop
)
1550 Visitor
*v
= qobject_input_visitor_new(prop
->defval
);
1552 assert(prop
->set
!= NULL
);
1553 prop
->set(obj
, v
, prop
->name
, prop
->opaque
, &error_abort
);
1558 static void object_property_set_default(ObjectProperty
*prop
, QObject
*defval
)
1560 assert(!prop
->defval
);
1561 assert(!prop
->init
);
1563 prop
->defval
= defval
;
1564 prop
->init
= object_property_init_defval
;
1567 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
)
1569 object_property_set_default(prop
, QOBJECT(qbool_from_bool(value
)));
1572 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
)
1574 object_property_set_default(prop
, QOBJECT(qstring_from_str(value
)));
1577 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
)
1579 object_property_set_default(prop
, QOBJECT(qnum_from_int(value
)));
1582 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
)
1584 object_property_set_default(prop
, QOBJECT(qnum_from_uint(value
)));
1587 bool object_property_set_uint(Object
*obj
, const char *name
,
1588 uint64_t value
, Error
**errp
)
1590 QNum
*qnum
= qnum_from_uint(value
);
1591 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1593 qobject_unref(qnum
);
1597 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1600 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1607 qnum
= qobject_to(QNum
, ret
);
1608 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1609 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1617 typedef struct EnumProperty
{
1618 const QEnumLookup
*lookup
;
1619 int (*get
)(Object
*, Error
**);
1620 void (*set
)(Object
*, int, Error
**);
1623 int object_property_get_enum(Object
*obj
, const char *name
,
1624 const char *typename
, Error
**errp
)
1628 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1629 EnumProperty
*enumprop
;
1635 if (!g_str_equal(prop
->type
, typename
)) {
1636 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1637 name
, object_class_get_name(
1638 object_get_class(obj
)), typename
);
1642 enumprop
= prop
->opaque
;
1644 str
= object_property_get_str(obj
, name
, errp
);
1649 ret
= qapi_enum_parse(enumprop
->lookup
, str
, -1, errp
);
1655 bool object_property_parse(Object
*obj
, const char *name
,
1656 const char *string
, Error
**errp
)
1658 Visitor
*v
= string_input_visitor_new(string
);
1659 bool ok
= object_property_set(obj
, name
, v
, errp
);
1665 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1669 char *string
= NULL
;
1671 v
= string_output_visitor_new(human
, &string
);
1672 if (!object_property_get(obj
, name
, v
, errp
)) {
1676 visit_complete(v
, &string
);
1683 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1685 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1693 Object
*object_get_root(void)
1695 static Object
*root
;
1698 root
= object_new("container");
1704 Object
*object_get_objects_root(void)
1706 return container_get(object_get_root(), "/objects");
1709 Object
*object_get_internal_root(void)
1711 static Object
*internal_root
;
1713 if (!internal_root
) {
1714 internal_root
= object_new("container");
1717 return internal_root
;
1720 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1721 const char *name
, void *opaque
,
1724 Object
*child
= opaque
;
1727 path
= object_get_canonical_path(child
);
1728 visit_type_str(v
, name
, &path
, errp
);
1732 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
,
1738 static void object_finalize_child_property(Object
*obj
, const char *name
,
1741 Object
*child
= opaque
;
1743 if (child
->class->unparent
) {
1744 (child
->class->unparent
)(child
);
1746 child
->parent
= NULL
;
1747 object_unref(child
);
1751 object_property_try_add_child(Object
*obj
, const char *name
,
1752 Object
*child
, Error
**errp
)
1754 g_autofree
char *type
= NULL
;
1757 assert(!child
->parent
);
1759 type
= g_strdup_printf("child<%s>", object_get_typename(child
));
1761 op
= object_property_try_add(obj
, name
, type
, object_get_child_property
,
1762 NULL
, object_finalize_child_property
,
1767 op
->resolve
= object_resolve_child_property
;
1769 child
->parent
= obj
;
1774 object_property_add_child(Object
*obj
, const char *name
,
1777 return object_property_try_add_child(obj
, name
, child
, &error_abort
);
1780 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1781 Object
*val
, Error
**errp
)
1783 /* Allow the link to be set, always */
1789 Object
*target
; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1790 ptrdiff_t offset
; /* if OBJ_PROP_LINK_CLASS */
1792 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1793 ObjectPropertyLinkFlags flags
;
1797 object_link_get_targetp(Object
*obj
, LinkProperty
*lprop
)
1799 if (lprop
->flags
& OBJ_PROP_LINK_DIRECT
) {
1800 return &lprop
->target
;
1801 } else if (lprop
->flags
& OBJ_PROP_LINK_CLASS
) {
1802 return (void *)obj
+ lprop
->offset
;
1804 return lprop
->targetp
;
1808 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1809 const char *name
, void *opaque
,
1812 LinkProperty
*lprop
= opaque
;
1813 Object
**targetp
= object_link_get_targetp(obj
, lprop
);
1817 path
= object_get_canonical_path(*targetp
);
1818 visit_type_str(v
, name
, &path
, errp
);
1822 visit_type_str(v
, name
, &path
, errp
);
1827 * object_resolve_link:
1829 * Lookup an object and ensure its type matches the link property type. This
1830 * is similar to object_resolve_path() except type verification against the
1831 * link property is performed.
1833 * Returns: The matched object or NULL on path lookup failures.
1835 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1836 const char *path
, Error
**errp
)
1840 bool ambiguous
= false;
1843 /* Go from link<FOO> to FOO. */
1844 type
= object_property_get_type(obj
, name
, NULL
);
1845 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1846 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1849 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1851 } else if (!target
) {
1852 target
= object_resolve_path(path
, &ambiguous
);
1853 if (target
|| ambiguous
) {
1854 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1856 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1857 "Device '%s' not found", path
);
1861 g_free(target_type
);
1866 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1867 const char *name
, void *opaque
,
1870 Error
*local_err
= NULL
;
1871 LinkProperty
*prop
= opaque
;
1872 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1873 Object
*old_target
= *targetp
;
1877 if (!visit_type_str(v
, name
, &path
, errp
)) {
1882 new_target
= object_resolve_link(obj
, name
, path
, errp
);
1893 prop
->check(obj
, name
, new_target
, &local_err
);
1895 error_propagate(errp
, local_err
);
1899 *targetp
= new_target
;
1900 if (prop
->flags
& OBJ_PROP_LINK_STRONG
) {
1901 object_ref(new_target
);
1902 object_unref(old_target
);
1906 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
,
1909 LinkProperty
*lprop
= opaque
;
1911 return *object_link_get_targetp(parent
, lprop
);
1914 static void object_release_link_property(Object
*obj
, const char *name
,
1917 LinkProperty
*prop
= opaque
;
1918 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1920 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *targetp
) {
1921 object_unref(*targetp
);
1923 if (!(prop
->flags
& OBJ_PROP_LINK_CLASS
)) {
1928 static ObjectProperty
*
1929 object_add_link_prop(Object
*obj
, const char *name
,
1930 const char *type
, void *ptr
,
1931 void (*check
)(const Object
*, const char *,
1932 Object
*, Error
**),
1933 ObjectPropertyLinkFlags flags
)
1935 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1936 g_autofree
char *full_type
= NULL
;
1939 if (flags
& OBJ_PROP_LINK_DIRECT
) {
1942 prop
->targetp
= ptr
;
1944 prop
->check
= check
;
1945 prop
->flags
= flags
;
1947 full_type
= g_strdup_printf("link<%s>", type
);
1949 op
= object_property_add(obj
, name
, full_type
,
1950 object_get_link_property
,
1951 check
? object_set_link_property
: NULL
,
1952 object_release_link_property
,
1954 op
->resolve
= object_resolve_link_property
;
1959 object_property_add_link(Object
*obj
, const char *name
,
1960 const char *type
, Object
**targetp
,
1961 void (*check
)(const Object
*, const char *,
1962 Object
*, Error
**),
1963 ObjectPropertyLinkFlags flags
)
1965 return object_add_link_prop(obj
, name
, type
, targetp
, check
, flags
);
1969 object_class_property_add_link(ObjectClass
*oc
,
1971 const char *type
, ptrdiff_t offset
,
1972 void (*check
)(const Object
*obj
, const char *name
,
1973 Object
*val
, Error
**errp
),
1974 ObjectPropertyLinkFlags flags
)
1976 LinkProperty
*prop
= g_new0(LinkProperty
, 1);
1980 prop
->offset
= offset
;
1981 prop
->check
= check
;
1982 prop
->flags
= flags
| OBJ_PROP_LINK_CLASS
;
1984 full_type
= g_strdup_printf("link<%s>", type
);
1986 op
= object_class_property_add(oc
, name
, full_type
,
1987 object_get_link_property
,
1988 check
? object_set_link_property
: NULL
,
1989 object_release_link_property
,
1992 op
->resolve
= object_resolve_link_property
;
1999 object_property_add_const_link(Object
*obj
, const char *name
,
2002 return object_add_link_prop(obj
, name
,
2003 object_get_typename(target
), target
,
2004 NULL
, OBJ_PROP_LINK_DIRECT
);
2007 const char *object_get_canonical_path_component(const Object
*obj
)
2009 ObjectProperty
*prop
= NULL
;
2010 GHashTableIter iter
;
2012 if (obj
->parent
== NULL
) {
2016 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
2017 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2018 if (!object_property_is_child(prop
)) {
2022 if (prop
->opaque
== obj
) {
2027 /* obj had a parent but was not a child, should never happen */
2028 g_assert_not_reached();
2032 char *object_get_canonical_path(const Object
*obj
)
2034 Object
*root
= object_get_root();
2035 char *newpath
, *path
= NULL
;
2038 return g_strdup("/");
2042 const char *component
= object_get_canonical_path_component(obj
);
2045 /* A canonical path must be complete, so discard what was
2052 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
2056 } while (obj
!= root
);
2061 Object
*object_resolve_path_component(Object
*parent
, const char *part
)
2063 ObjectProperty
*prop
= object_property_find(parent
, part
);
2068 if (prop
->resolve
) {
2069 return prop
->resolve(parent
, prop
->opaque
, part
);
2075 static Object
*object_resolve_abs_path(Object
*parent
,
2077 const char *typename
)
2081 if (*parts
== NULL
) {
2082 return object_dynamic_cast(parent
, typename
);
2085 if (strcmp(*parts
, "") == 0) {
2086 return object_resolve_abs_path(parent
, parts
+ 1, typename
);
2089 child
= object_resolve_path_component(parent
, *parts
);
2094 return object_resolve_abs_path(child
, parts
+ 1, typename
);
2097 static Object
*object_resolve_partial_path(Object
*parent
,
2099 const char *typename
,
2103 GHashTableIter iter
;
2104 ObjectProperty
*prop
;
2106 obj
= object_resolve_abs_path(parent
, parts
, typename
);
2108 g_hash_table_iter_init(&iter
, parent
->properties
);
2109 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2112 if (!object_property_is_child(prop
)) {
2116 found
= object_resolve_partial_path(prop
->opaque
, parts
,
2117 typename
, ambiguous
);
2134 Object
*object_resolve_path_type(const char *path
, const char *typename
,
2140 parts
= g_strsplit(path
, "/", 0);
2143 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
2144 bool ambiguous
= false;
2145 obj
= object_resolve_partial_path(object_get_root(), parts
,
2146 typename
, &ambiguous
);
2148 *ambiguousp
= ambiguous
;
2151 obj
= object_resolve_abs_path(object_get_root(), parts
+ 1, typename
);
2159 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
2161 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
2164 Object
*object_resolve_path_at(Object
*parent
, const char *path
)
2166 g_auto(GStrv
) parts
= g_strsplit(path
, "/", 0);
2169 return object_resolve_abs_path(object_get_root(), parts
+ 1,
2172 return object_resolve_abs_path(parent
, parts
, TYPE_OBJECT
);
2175 typedef struct StringProperty
2177 char *(*get
)(Object
*, Error
**);
2178 void (*set
)(Object
*, const char *, Error
**);
2181 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
2182 void *opaque
, Error
**errp
)
2184 StringProperty
*prop
= opaque
;
2188 value
= prop
->get(obj
, &err
);
2190 error_propagate(errp
, err
);
2194 visit_type_str(v
, name
, &value
, errp
);
2198 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
2199 void *opaque
, Error
**errp
)
2201 StringProperty
*prop
= opaque
;
2204 if (!visit_type_str(v
, name
, &value
, errp
)) {
2208 prop
->set(obj
, value
, errp
);
2212 static void property_release_data(Object
*obj
, const char *name
,
2219 object_property_add_str(Object
*obj
, const char *name
,
2220 char *(*get
)(Object
*, Error
**),
2221 void (*set
)(Object
*, const char *, Error
**))
2223 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2228 return object_property_add(obj
, name
, "string",
2229 get
? property_get_str
: NULL
,
2230 set
? property_set_str
: NULL
,
2231 property_release_data
,
2236 object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2237 char *(*get
)(Object
*, Error
**),
2238 void (*set
)(Object
*, const char *,
2241 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2246 return object_class_property_add(klass
, name
, "string",
2247 get
? property_get_str
: NULL
,
2248 set
? property_set_str
: NULL
,
2253 typedef struct BoolProperty
2255 bool (*get
)(Object
*, Error
**);
2256 void (*set
)(Object
*, bool, Error
**);
2259 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2260 void *opaque
, Error
**errp
)
2262 BoolProperty
*prop
= opaque
;
2266 value
= prop
->get(obj
, &err
);
2268 error_propagate(errp
, err
);
2272 visit_type_bool(v
, name
, &value
, errp
);
2275 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2276 void *opaque
, Error
**errp
)
2278 BoolProperty
*prop
= opaque
;
2281 if (!visit_type_bool(v
, name
, &value
, errp
)) {
2285 prop
->set(obj
, value
, errp
);
2289 object_property_add_bool(Object
*obj
, const char *name
,
2290 bool (*get
)(Object
*, Error
**),
2291 void (*set
)(Object
*, bool, Error
**))
2293 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2298 return object_property_add(obj
, name
, "bool",
2299 get
? property_get_bool
: NULL
,
2300 set
? property_set_bool
: NULL
,
2301 property_release_data
,
2306 object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2307 bool (*get
)(Object
*, Error
**),
2308 void (*set
)(Object
*, bool, Error
**))
2310 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2315 return object_class_property_add(klass
, name
, "bool",
2316 get
? property_get_bool
: NULL
,
2317 set
? property_set_bool
: NULL
,
2322 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2323 void *opaque
, Error
**errp
)
2325 EnumProperty
*prop
= opaque
;
2329 value
= prop
->get(obj
, &err
);
2331 error_propagate(errp
, err
);
2335 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2338 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2339 void *opaque
, Error
**errp
)
2341 EnumProperty
*prop
= opaque
;
2344 if (!visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
)) {
2347 prop
->set(obj
, value
, errp
);
2351 object_property_add_enum(Object
*obj
, const char *name
,
2352 const char *typename
,
2353 const QEnumLookup
*lookup
,
2354 int (*get
)(Object
*, Error
**),
2355 void (*set
)(Object
*, int, Error
**))
2357 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2359 prop
->lookup
= lookup
;
2363 return object_property_add(obj
, name
, typename
,
2364 get
? property_get_enum
: NULL
,
2365 set
? property_set_enum
: NULL
,
2366 property_release_data
,
2371 object_class_property_add_enum(ObjectClass
*klass
, 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_class_property_add(klass
, name
, typename
,
2384 get
? property_get_enum
: NULL
,
2385 set
? property_set_enum
: NULL
,
2390 typedef struct TMProperty
{
2391 void (*get
)(Object
*, struct tm
*, Error
**);
2394 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2395 void *opaque
, Error
**errp
)
2397 TMProperty
*prop
= opaque
;
2401 prop
->get(obj
, &value
, &err
);
2403 error_propagate(errp
, err
);
2407 if (!visit_start_struct(v
, name
, NULL
, 0, errp
)) {
2410 if (!visit_type_int32(v
, "tm_year", &value
.tm_year
, errp
)) {
2413 if (!visit_type_int32(v
, "tm_mon", &value
.tm_mon
, errp
)) {
2416 if (!visit_type_int32(v
, "tm_mday", &value
.tm_mday
, errp
)) {
2419 if (!visit_type_int32(v
, "tm_hour", &value
.tm_hour
, errp
)) {
2422 if (!visit_type_int32(v
, "tm_min", &value
.tm_min
, errp
)) {
2425 if (!visit_type_int32(v
, "tm_sec", &value
.tm_sec
, errp
)) {
2428 visit_check_struct(v
, errp
);
2430 visit_end_struct(v
, NULL
);
2434 object_property_add_tm(Object
*obj
, const char *name
,
2435 void (*get
)(Object
*, struct tm
*, Error
**))
2437 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2441 return object_property_add(obj
, name
, "struct tm",
2442 get
? property_get_tm
: NULL
, NULL
,
2443 property_release_data
,
2448 object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2449 void (*get
)(Object
*, struct tm
*, Error
**))
2451 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2455 return object_class_property_add(klass
, name
, "struct tm",
2456 get
? property_get_tm
: NULL
,
2460 static char *object_get_type(Object
*obj
, Error
**errp
)
2462 return g_strdup(object_get_typename(obj
));
2465 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2466 void *opaque
, Error
**errp
)
2468 uint8_t value
= *(uint8_t *)opaque
;
2469 visit_type_uint8(v
, name
, &value
, errp
);
2472 static void property_set_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2473 void *opaque
, Error
**errp
)
2475 uint8_t *field
= opaque
;
2478 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
2485 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2486 void *opaque
, Error
**errp
)
2488 uint16_t value
= *(uint16_t *)opaque
;
2489 visit_type_uint16(v
, name
, &value
, errp
);
2492 static void property_set_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2493 void *opaque
, Error
**errp
)
2495 uint16_t *field
= opaque
;
2498 if (!visit_type_uint16(v
, name
, &value
, errp
)) {
2505 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2506 void *opaque
, Error
**errp
)
2508 uint32_t value
= *(uint32_t *)opaque
;
2509 visit_type_uint32(v
, name
, &value
, errp
);
2512 static void property_set_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2513 void *opaque
, Error
**errp
)
2515 uint32_t *field
= opaque
;
2518 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
2525 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2526 void *opaque
, Error
**errp
)
2528 uint64_t value
= *(uint64_t *)opaque
;
2529 visit_type_uint64(v
, name
, &value
, errp
);
2532 static void property_set_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2533 void *opaque
, Error
**errp
)
2535 uint64_t *field
= opaque
;
2538 if (!visit_type_uint64(v
, name
, &value
, errp
)) {
2546 object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2548 ObjectPropertyFlags flags
)
2550 ObjectPropertyAccessor
*getter
= NULL
;
2551 ObjectPropertyAccessor
*setter
= NULL
;
2553 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2554 getter
= property_get_uint8_ptr
;
2557 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2558 setter
= property_set_uint8_ptr
;
2561 return object_property_add(obj
, name
, "uint8",
2562 getter
, setter
, NULL
, (void *)v
);
2566 object_class_property_add_uint8_ptr(ObjectClass
*klass
, 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_class_property_add(klass
, name
, "uint8",
2582 getter
, setter
, NULL
, (void *)v
);
2586 object_property_add_uint16_ptr(Object
*obj
, 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_uint16_ptr
;
2597 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2598 setter
= property_set_uint16_ptr
;
2601 return object_property_add(obj
, name
, "uint16",
2602 getter
, setter
, NULL
, (void *)v
);
2606 object_class_property_add_uint16_ptr(ObjectClass
*klass
, 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_class_property_add(klass
, name
, "uint16",
2622 getter
, setter
, NULL
, (void *)v
);
2626 object_property_add_uint32_ptr(Object
*obj
, 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_uint32_ptr
;
2637 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2638 setter
= property_set_uint32_ptr
;
2641 return object_property_add(obj
, name
, "uint32",
2642 getter
, setter
, NULL
, (void *)v
);
2646 object_class_property_add_uint32_ptr(ObjectClass
*klass
, 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_class_property_add(klass
, name
, "uint32",
2662 getter
, setter
, NULL
, (void *)v
);
2666 object_property_add_uint64_ptr(Object
*obj
, 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_uint64_ptr
;
2677 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2678 setter
= property_set_uint64_ptr
;
2681 return object_property_add(obj
, name
, "uint64",
2682 getter
, setter
, NULL
, (void *)v
);
2686 object_class_property_add_uint64_ptr(ObjectClass
*klass
, 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_class_property_add(klass
, name
, "uint64",
2702 getter
, setter
, NULL
, (void *)v
);
2710 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2711 void *opaque
, Error
**errp
)
2713 AliasProperty
*prop
= opaque
;
2714 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2716 object_property_get(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2717 visit_free(alias_v
);
2720 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2721 void *opaque
, Error
**errp
)
2723 AliasProperty
*prop
= opaque
;
2724 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2726 object_property_set(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2727 visit_free(alias_v
);
2730 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2733 AliasProperty
*prop
= opaque
;
2735 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2738 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2740 AliasProperty
*prop
= opaque
;
2742 g_free(prop
->target_name
);
2747 object_property_add_alias(Object
*obj
, const char *name
,
2748 Object
*target_obj
, const char *target_name
)
2750 AliasProperty
*prop
;
2752 ObjectProperty
*target_prop
;
2753 g_autofree
char *prop_type
= NULL
;
2755 target_prop
= object_property_find_err(target_obj
, target_name
,
2758 if (object_property_is_child(target_prop
)) {
2759 prop_type
= g_strdup_printf("link%s",
2760 target_prop
->type
+ strlen("child"));
2762 prop_type
= g_strdup(target_prop
->type
);
2765 prop
= g_malloc(sizeof(*prop
));
2766 prop
->target_obj
= target_obj
;
2767 prop
->target_name
= g_strdup(target_name
);
2769 op
= object_property_add(obj
, name
, prop_type
,
2772 property_release_alias
,
2774 op
->resolve
= property_resolve_alias
;
2775 if (target_prop
->defval
) {
2776 op
->defval
= qobject_ref(target_prop
->defval
);
2779 object_property_set_description(obj
, op
->name
,
2780 target_prop
->description
);
2784 void object_property_set_description(Object
*obj
, const char *name
,
2785 const char *description
)
2789 op
= object_property_find_err(obj
, name
, &error_abort
);
2790 g_free(op
->description
);
2791 op
->description
= g_strdup(description
);
2794 void object_class_property_set_description(ObjectClass
*klass
,
2796 const char *description
)
2800 op
= g_hash_table_lookup(klass
->properties
, name
);
2801 g_free(op
->description
);
2802 op
->description
= g_strdup(description
);
2805 static void object_class_init(ObjectClass
*klass
, void *data
)
2807 object_class_property_add_str(klass
, "type", object_get_type
,
2811 static void register_types(void)
2813 static const TypeInfo interface_info
= {
2814 .name
= TYPE_INTERFACE
,
2815 .class_size
= sizeof(InterfaceClass
),
2819 static const TypeInfo object_info
= {
2820 .name
= TYPE_OBJECT
,
2821 .instance_size
= sizeof(Object
),
2822 .class_init
= object_class_init
,
2826 type_interface
= type_register_internal(&interface_info
);
2827 type_register_internal(&object_info
);
2830 type_init(register_types
)