4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/forward-visitor.h"
24 #include "qapi/qapi-builtin-visit.h"
25 #include "qapi/qmp/qerror.h"
26 #include "qapi/qmp/qjson.h"
29 /* TODO: replace QObject with a simpler visitor to avoid a dependency
30 * of the QOM core on QObject? */
31 #include "qom/qom-qobject.h"
32 #include "qapi/qmp/qbool.h"
33 #include "qapi/qmp/qnum.h"
34 #include "qapi/qmp/qstring.h"
35 #include "qemu/error-report.h"
37 #define MAX_INTERFACES 32
39 typedef struct InterfaceImpl InterfaceImpl
;
40 typedef struct TypeImpl TypeImpl
;
54 size_t instance_align
;
56 void (*class_init
)(ObjectClass
*klass
, void *data
);
57 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
61 void (*instance_init
)(Object
*obj
);
62 void (*instance_post_init
)(Object
*obj
);
63 void (*instance_finalize
)(Object
*obj
);
68 TypeImpl
*parent_type
;
73 InterfaceImpl interfaces
[MAX_INTERFACES
];
76 static Type type_interface
;
78 static GHashTable
*type_table_get(void)
80 static GHashTable
*type_table
;
82 if (type_table
== NULL
) {
83 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
89 static bool enumerating_types
;
91 static void type_table_add(TypeImpl
*ti
)
93 assert(!enumerating_types
);
94 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
97 static TypeImpl
*type_table_lookup(const char *name
)
99 return g_hash_table_lookup(type_table_get(), name
);
102 static TypeImpl
*type_new(const TypeInfo
*info
)
104 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
107 g_assert(info
->name
!= NULL
);
109 if (type_table_lookup(info
->name
) != NULL
) {
110 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
114 ti
->name
= g_strdup(info
->name
);
115 ti
->parent
= g_strdup(info
->parent
);
117 ti
->class_size
= info
->class_size
;
118 ti
->instance_size
= info
->instance_size
;
119 ti
->instance_align
= info
->instance_align
;
121 ti
->class_init
= info
->class_init
;
122 ti
->class_base_init
= info
->class_base_init
;
123 ti
->class_data
= info
->class_data
;
125 ti
->instance_init
= info
->instance_init
;
126 ti
->instance_post_init
= info
->instance_post_init
;
127 ti
->instance_finalize
= info
->instance_finalize
;
129 ti
->abstract
= info
->abstract
;
131 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
132 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
134 ti
->num_interfaces
= i
;
139 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
148 TypeImpl
*type_register(const TypeInfo
*info
)
150 assert(info
->parent
);
151 return type_register_internal(info
);
154 TypeImpl
*type_register_static(const TypeInfo
*info
)
156 return type_register(info
);
159 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
163 for (i
= 0; i
< nr_infos
; i
++) {
164 type_register_static(&infos
[i
]);
168 static TypeImpl
*type_get_by_name(const char *name
)
174 return type_table_lookup(name
);
177 static TypeImpl
*type_get_parent(TypeImpl
*type
)
179 if (!type
->parent_type
&& type
->parent
) {
180 type
->parent_type
= type_get_by_name(type
->parent
);
181 if (!type
->parent_type
) {
182 fprintf(stderr
, "Type '%s' is missing its parent '%s'\n",
183 type
->name
, type
->parent
);
188 return type
->parent_type
;
191 static bool type_has_parent(TypeImpl
*type
)
193 return (type
->parent
!= NULL
);
196 static size_t type_class_get_size(TypeImpl
*ti
)
198 if (ti
->class_size
) {
199 return ti
->class_size
;
202 if (type_has_parent(ti
)) {
203 return type_class_get_size(type_get_parent(ti
));
206 return sizeof(ObjectClass
);
209 static size_t type_object_get_size(TypeImpl
*ti
)
211 if (ti
->instance_size
) {
212 return ti
->instance_size
;
215 if (type_has_parent(ti
)) {
216 return type_object_get_size(type_get_parent(ti
));
222 size_t object_type_get_instance_size(const char *typename
)
224 TypeImpl
*type
= type_get_by_name(typename
);
226 g_assert(type
!= NULL
);
227 return type_object_get_size(type
);
230 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
234 /* Check if target_type is a direct ancestor of type */
236 if (type
== target_type
) {
240 type
= type_get_parent(type
);
246 static void type_initialize(TypeImpl
*ti
);
248 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
249 TypeImpl
*parent_type
)
251 InterfaceClass
*new_iface
;
253 TypeImpl
*iface_impl
;
255 info
.parent
= parent_type
->name
;
256 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
257 info
.abstract
= true;
259 iface_impl
= type_new(&info
);
260 iface_impl
->parent_type
= parent_type
;
261 type_initialize(iface_impl
);
262 g_free((char *)info
.name
);
264 new_iface
= (InterfaceClass
*)iface_impl
->class;
265 new_iface
->concrete_class
= ti
->class;
266 new_iface
->interface_type
= interface_type
;
268 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
, new_iface
);
271 static void object_property_free(gpointer data
)
273 ObjectProperty
*prop
= data
;
276 qobject_unref(prop
->defval
);
281 g_free(prop
->description
);
285 static void type_initialize(TypeImpl
*ti
)
293 ti
->class_size
= type_class_get_size(ti
);
294 ti
->instance_size
= type_object_get_size(ti
);
295 /* Any type with zero instance_size is implicitly abstract.
296 * This means interface types are all abstract.
298 if (ti
->instance_size
== 0) {
301 if (type_is_ancestor(ti
, type_interface
)) {
302 assert(ti
->instance_size
== 0);
303 assert(ti
->abstract
);
304 assert(!ti
->instance_init
);
305 assert(!ti
->instance_post_init
);
306 assert(!ti
->instance_finalize
);
307 assert(!ti
->num_interfaces
);
309 ti
->class = g_malloc0(ti
->class_size
);
311 parent
= type_get_parent(ti
);
313 type_initialize(parent
);
317 g_assert(parent
->class_size
<= ti
->class_size
);
318 g_assert(parent
->instance_size
<= ti
->instance_size
);
319 memcpy(ti
->class, parent
->class, parent
->class_size
);
320 ti
->class->interfaces
= NULL
;
322 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
323 InterfaceClass
*iface
= e
->data
;
324 ObjectClass
*klass
= OBJECT_CLASS(iface
);
326 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
329 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
330 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
332 error_report("missing interface '%s' for object '%s'",
333 ti
->interfaces
[i
].typename
, parent
->name
);
336 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
337 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
339 if (type_is_ancestor(target_type
, t
)) {
348 type_initialize_interface(ti
, t
, t
);
352 ti
->class->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
353 object_property_free
);
355 ti
->class->type
= ti
;
358 if (parent
->class_base_init
) {
359 parent
->class_base_init(ti
->class, ti
->class_data
);
361 parent
= type_get_parent(parent
);
364 if (ti
->class_init
) {
365 ti
->class_init(ti
->class, ti
->class_data
);
369 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
371 if (type_has_parent(ti
)) {
372 object_init_with_type(obj
, type_get_parent(ti
));
375 if (ti
->instance_init
) {
376 ti
->instance_init(obj
);
380 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
382 if (ti
->instance_post_init
) {
383 ti
->instance_post_init(obj
);
386 if (type_has_parent(ti
)) {
387 object_post_init_with_type(obj
, type_get_parent(ti
));
391 bool object_apply_global_props(Object
*obj
, const GPtrArray
*props
,
400 for (i
= 0; i
< props
->len
; i
++) {
401 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
404 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
407 if (p
->optional
&& !object_property_find(obj
, p
->property
)) {
411 if (!object_property_parse(obj
, p
->property
, p
->value
, &err
)) {
412 error_prepend(&err
, "can't apply global %s.%s=%s: ",
413 p
->driver
, p
->property
, p
->value
);
415 * If errp != NULL, propagate error and return.
416 * If errp == NULL, report a warning, but keep going
417 * with the remaining globals.
420 error_propagate(errp
, err
);
423 warn_report_err(err
);
432 * Global property defaults
433 * Slot 0: accelerator's global property defaults
434 * Slot 1: machine's global property defaults
435 * Slot 2: global properties from legacy command line option
436 * Each is a GPtrArray of of GlobalProperty.
437 * Applied in order, later entries override earlier ones.
439 static GPtrArray
*object_compat_props
[3];
442 * Retrieve @GPtrArray for global property defined with options
443 * other than "-global". These are generally used for syntactic
444 * sugar and legacy command line options.
446 void object_register_sugar_prop(const char *driver
, const char *prop
,
447 const char *value
, bool optional
)
450 if (!object_compat_props
[2]) {
451 object_compat_props
[2] = g_ptr_array_new();
453 g
= g_new0(GlobalProperty
, 1);
454 g
->driver
= g_strdup(driver
);
455 g
->property
= g_strdup(prop
);
456 g
->value
= g_strdup(value
);
457 g
->optional
= optional
;
458 g_ptr_array_add(object_compat_props
[2], g
);
462 * Set machine's global property defaults to @compat_props.
463 * May be called at most once.
465 void object_set_machine_compat_props(GPtrArray
*compat_props
)
467 assert(!object_compat_props
[1]);
468 object_compat_props
[1] = compat_props
;
472 * Set accelerator's global property defaults to @compat_props.
473 * May be called at most once.
475 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
477 assert(!object_compat_props
[0]);
478 object_compat_props
[0] = compat_props
;
481 void object_apply_compat_props(Object
*obj
)
485 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
486 object_apply_global_props(obj
, object_compat_props
[i
],
487 i
== 2 ? &error_fatal
: &error_abort
);
491 static void object_class_property_init_all(Object
*obj
)
493 ObjectPropertyIterator iter
;
494 ObjectProperty
*prop
;
496 object_class_property_iter_init(&iter
, object_get_class(obj
));
497 while ((prop
= object_property_iter_next(&iter
))) {
499 prop
->init(obj
, prop
);
504 static void object_initialize_with_type(Object
*obj
, size_t size
, TypeImpl
*type
)
506 type_initialize(type
);
508 g_assert(type
->instance_size
>= sizeof(Object
));
509 g_assert(type
->abstract
== false);
510 g_assert(size
>= type
->instance_size
);
512 memset(obj
, 0, type
->instance_size
);
513 obj
->class = type
->class;
515 object_class_property_init_all(obj
);
516 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
517 NULL
, object_property_free
);
518 object_init_with_type(obj
, type
);
519 object_post_init_with_type(obj
, type
);
522 void object_initialize(void *data
, size_t size
, const char *typename
)
524 TypeImpl
*type
= type_get_by_name(typename
);
526 #ifdef CONFIG_MODULES
528 module_load_qom_one(typename
);
529 type
= type_get_by_name(typename
);
533 error_report("missing object type '%s'", typename
);
537 object_initialize_with_type(data
, size
, type
);
540 bool object_initialize_child_with_props(Object
*parentobj
,
541 const char *propname
,
542 void *childobj
, size_t size
,
549 va_start(vargs
, errp
);
550 ok
= object_initialize_child_with_propsv(parentobj
, propname
,
551 childobj
, size
, type
, errp
,
557 bool object_initialize_child_with_propsv(Object
*parentobj
,
558 const char *propname
,
559 void *childobj
, size_t size
,
561 Error
**errp
, va_list vargs
)
567 object_initialize(childobj
, size
, type
);
568 obj
= OBJECT(childobj
);
570 if (!object_set_propv(obj
, errp
, vargs
)) {
574 object_property_add_child(parentobj
, propname
, obj
);
576 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
578 if (!user_creatable_complete(uc
, errp
)) {
579 object_unparent(obj
);
588 * We want @obj's reference to be 1 on success, 0 on failure.
589 * On success, it's 2: one taken by object_initialize(), and one
590 * by object_property_add_child().
591 * On failure in object_initialize() or earlier, it's 1.
592 * On failure afterwards, it's also 1: object_unparent() releases
593 * the reference taken by object_property_add_child().
599 void object_initialize_child_internal(Object
*parent
,
600 const char *propname
,
601 void *child
, size_t size
,
604 object_initialize_child_with_props(parent
, propname
, child
, size
, type
,
608 static inline bool object_property_is_child(ObjectProperty
*prop
)
610 return strstart(prop
->type
, "child<", NULL
);
613 static void object_property_del_all(Object
*obj
)
615 g_autoptr(GHashTable
) done
= g_hash_table_new(NULL
, NULL
);
616 ObjectProperty
*prop
;
617 ObjectPropertyIterator iter
;
622 object_property_iter_init(&iter
, obj
);
623 while ((prop
= object_property_iter_next(&iter
)) != NULL
) {
624 if (g_hash_table_add(done
, prop
)) {
626 prop
->release(obj
, prop
->name
, prop
->opaque
);
634 g_hash_table_unref(obj
->properties
);
637 static void object_property_del_child(Object
*obj
, Object
*child
)
639 ObjectProperty
*prop
;
643 g_hash_table_iter_init(&iter
, obj
->properties
);
644 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
646 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
648 prop
->release(obj
, prop
->name
, prop
->opaque
);
649 prop
->release
= NULL
;
654 g_hash_table_iter_init(&iter
, obj
->properties
);
655 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
657 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
658 g_hash_table_iter_remove(&iter
);
664 void object_unparent(Object
*obj
)
667 object_property_del_child(obj
->parent
, obj
);
671 static void object_deinit(Object
*obj
, TypeImpl
*type
)
673 if (type
->instance_finalize
) {
674 type
->instance_finalize(obj
);
677 if (type_has_parent(type
)) {
678 object_deinit(obj
, type_get_parent(type
));
682 static void object_finalize(void *data
)
685 TypeImpl
*ti
= obj
->class->type
;
687 object_property_del_all(obj
);
688 object_deinit(obj
, ti
);
690 g_assert(obj
->ref
== 0);
691 g_assert(obj
->parent
== NULL
);
697 /* Find the minimum alignment guaranteed by the system malloc. */
698 #if __STDC_VERSION__ >= 201112L
699 typedef max_align_t qemu_max_align_t
;
709 static Object
*object_new_with_type(Type type
)
713 void (*obj_free
)(void *);
715 g_assert(type
!= NULL
);
716 type_initialize(type
);
718 size
= type
->instance_size
;
719 align
= type
->instance_align
;
722 * Do not use qemu_memalign unless required. Depending on the
723 * implementation, extra alignment implies extra overhead.
725 if (likely(align
<= __alignof__(qemu_max_align_t
))) {
726 obj
= g_malloc(size
);
729 obj
= qemu_memalign(align
, size
);
730 obj_free
= qemu_vfree
;
733 object_initialize_with_type(obj
, size
, type
);
734 obj
->free
= obj_free
;
739 Object
*object_new_with_class(ObjectClass
*klass
)
741 return object_new_with_type(klass
->type
);
744 Object
*object_new(const char *typename
)
746 TypeImpl
*ti
= type_get_by_name(typename
);
748 return object_new_with_type(ti
);
752 Object
*object_new_with_props(const char *typename
,
761 va_start(vargs
, errp
);
762 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
769 Object
*object_new_with_propv(const char *typename
,
779 klass
= object_class_by_name(typename
);
781 error_setg(errp
, "invalid object type: %s", typename
);
785 if (object_class_is_abstract(klass
)) {
786 error_setg(errp
, "object type '%s' is abstract", typename
);
789 obj
= object_new_with_type(klass
->type
);
791 if (!object_set_propv(obj
, errp
, vargs
)) {
796 object_property_add_child(parent
, id
, obj
);
799 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
801 if (!user_creatable_complete(uc
, errp
)) {
803 object_unparent(obj
);
818 bool object_set_props(Object
*obj
,
825 va_start(vargs
, errp
);
826 ret
= object_set_propv(obj
, errp
, vargs
);
833 bool object_set_propv(Object
*obj
,
837 const char *propname
;
839 propname
= va_arg(vargs
, char *);
840 while (propname
!= NULL
) {
841 const char *value
= va_arg(vargs
, char *);
843 g_assert(value
!= NULL
);
844 if (!object_property_parse(obj
, propname
, value
, errp
)) {
847 propname
= va_arg(vargs
, char *);
854 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
856 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
863 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
864 const char *file
, int line
, const char *func
)
866 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
867 typename
, file
, line
, func
);
869 #ifdef CONFIG_QOM_CAST_DEBUG
873 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
874 if (qatomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
879 inst
= object_dynamic_cast(obj
, typename
);
882 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
883 file
, line
, func
, obj
, typename
);
889 if (obj
&& obj
== inst
) {
890 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
891 qatomic_set(&obj
->class->object_cast_cache
[i
- 1],
892 qatomic_read(&obj
->class->object_cast_cache
[i
]));
894 qatomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
902 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
903 const char *typename
)
905 ObjectClass
*ret
= NULL
;
906 TypeImpl
*target_type
;
913 /* A simple fast path that can trigger a lot for leaf classes. */
915 if (type
->name
== typename
) {
919 target_type
= type_get_by_name(typename
);
921 /* target class type unknown, so fail the cast */
925 if (type
->class->interfaces
&&
926 type_is_ancestor(target_type
, type_interface
)) {
930 for (i
= class->interfaces
; i
; i
= i
->next
) {
931 ObjectClass
*target_class
= i
->data
;
933 if (type_is_ancestor(target_class
->type
, target_type
)) {
939 /* The match was ambiguous, don't allow a cast */
943 } else if (type_is_ancestor(type
, target_type
)) {
950 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
951 const char *typename
,
952 const char *file
, int line
,
957 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
958 typename
, file
, line
, func
);
960 #ifdef CONFIG_QOM_CAST_DEBUG
963 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
964 if (qatomic_read(&class->class_cast_cache
[i
]) == typename
) {
970 if (!class || !class->interfaces
) {
975 ret
= object_class_dynamic_cast(class, typename
);
977 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
978 file
, line
, func
, class, typename
);
982 #ifdef CONFIG_QOM_CAST_DEBUG
983 if (class && ret
== class) {
984 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
985 qatomic_set(&class->class_cast_cache
[i
- 1],
986 qatomic_read(&class->class_cast_cache
[i
]));
988 qatomic_set(&class->class_cast_cache
[i
- 1], typename
);
995 const char *object_get_typename(const Object
*obj
)
997 return obj
->class->type
->name
;
1000 ObjectClass
*object_get_class(Object
*obj
)
1005 bool object_class_is_abstract(ObjectClass
*klass
)
1007 return klass
->type
->abstract
;
1010 const char *object_class_get_name(ObjectClass
*klass
)
1012 return klass
->type
->name
;
1015 ObjectClass
*object_class_by_name(const char *typename
)
1017 TypeImpl
*type
= type_get_by_name(typename
);
1023 type_initialize(type
);
1028 ObjectClass
*module_object_class_by_name(const char *typename
)
1032 oc
= object_class_by_name(typename
);
1033 #ifdef CONFIG_MODULES
1035 module_load_qom_one(typename
);
1036 oc
= object_class_by_name(typename
);
1042 ObjectClass
*object_class_get_parent(ObjectClass
*class)
1044 TypeImpl
*type
= type_get_parent(class->type
);
1050 type_initialize(type
);
1055 typedef struct OCFData
1057 void (*fn
)(ObjectClass
*klass
, void *opaque
);
1058 const char *implements_type
;
1059 bool include_abstract
;
1063 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
1066 OCFData
*data
= opaque
;
1067 TypeImpl
*type
= value
;
1070 type_initialize(type
);
1073 if (!data
->include_abstract
&& type
->abstract
) {
1077 if (data
->implements_type
&&
1078 !object_class_dynamic_cast(k
, data
->implements_type
)) {
1082 data
->fn(k
, data
->opaque
);
1085 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1086 const char *implements_type
, bool include_abstract
,
1089 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1091 enumerating_types
= true;
1092 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1093 enumerating_types
= false;
1096 static int do_object_child_foreach(Object
*obj
,
1097 int (*fn
)(Object
*child
, void *opaque
),
1098 void *opaque
, bool recurse
)
1100 GHashTableIter iter
;
1101 ObjectProperty
*prop
;
1104 g_hash_table_iter_init(&iter
, obj
->properties
);
1105 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1106 if (object_property_is_child(prop
)) {
1107 Object
*child
= prop
->opaque
;
1109 ret
= fn(child
, opaque
);
1114 ret
= do_object_child_foreach(child
, fn
, opaque
, true);
1124 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1127 return do_object_child_foreach(obj
, fn
, opaque
, false);
1130 int object_child_foreach_recursive(Object
*obj
,
1131 int (*fn
)(Object
*child
, void *opaque
),
1134 return do_object_child_foreach(obj
, fn
, opaque
, true);
1137 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1139 GSList
**list
= opaque
;
1141 *list
= g_slist_prepend(*list
, klass
);
1144 GSList
*object_class_get_list(const char *implements_type
,
1145 bool include_abstract
)
1147 GSList
*list
= NULL
;
1149 object_class_foreach(object_class_get_list_tramp
,
1150 implements_type
, include_abstract
, &list
);
1154 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1156 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1157 object_class_get_name((ObjectClass
*)b
));
1160 GSList
*object_class_get_list_sorted(const char *implements_type
,
1161 bool include_abstract
)
1163 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1167 Object
*object_ref(void *objptr
)
1169 Object
*obj
= OBJECT(objptr
);
1173 qatomic_inc(&obj
->ref
);
1177 void object_unref(void *objptr
)
1179 Object
*obj
= OBJECT(objptr
);
1183 g_assert(obj
->ref
> 0);
1185 /* parent always holds a reference to its children */
1186 if (qatomic_fetch_dec(&obj
->ref
) == 1) {
1187 object_finalize(obj
);
1192 object_property_try_add(Object
*obj
, const char *name
, const char *type
,
1193 ObjectPropertyAccessor
*get
,
1194 ObjectPropertyAccessor
*set
,
1195 ObjectPropertyRelease
*release
,
1196 void *opaque
, Error
**errp
)
1198 ObjectProperty
*prop
;
1199 size_t name_len
= strlen(name
);
1201 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1203 ObjectProperty
*ret
= NULL
;
1204 char *name_no_array
= g_strdup(name
);
1206 name_no_array
[name_len
- 3] = '\0';
1207 for (i
= 0; i
< INT16_MAX
; ++i
) {
1208 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1210 ret
= object_property_try_add(obj
, full_name
, type
, get
, set
,
1211 release
, opaque
, NULL
);
1217 g_free(name_no_array
);
1222 if (object_property_find(obj
, name
) != NULL
) {
1223 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1224 name
, object_get_typename(obj
));
1228 prop
= g_malloc0(sizeof(*prop
));
1230 prop
->name
= g_strdup(name
);
1231 prop
->type
= g_strdup(type
);
1235 prop
->release
= release
;
1236 prop
->opaque
= opaque
;
1238 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1243 object_property_add(Object
*obj
, const char *name
, const char *type
,
1244 ObjectPropertyAccessor
*get
,
1245 ObjectPropertyAccessor
*set
,
1246 ObjectPropertyRelease
*release
,
1249 return object_property_try_add(obj
, name
, type
, get
, set
, release
,
1250 opaque
, &error_abort
);
1254 object_class_property_add(ObjectClass
*klass
,
1257 ObjectPropertyAccessor
*get
,
1258 ObjectPropertyAccessor
*set
,
1259 ObjectPropertyRelease
*release
,
1262 ObjectProperty
*prop
;
1264 assert(!object_class_property_find(klass
, name
));
1266 prop
= g_malloc0(sizeof(*prop
));
1268 prop
->name
= g_strdup(name
);
1269 prop
->type
= g_strdup(type
);
1273 prop
->release
= release
;
1274 prop
->opaque
= opaque
;
1276 g_hash_table_insert(klass
->properties
, prop
->name
, prop
);
1281 ObjectProperty
*object_property_find(Object
*obj
, const char *name
)
1283 ObjectProperty
*prop
;
1284 ObjectClass
*klass
= object_get_class(obj
);
1286 prop
= object_class_property_find(klass
, name
);
1291 return g_hash_table_lookup(obj
->properties
, name
);
1294 ObjectProperty
*object_property_find_err(Object
*obj
, const char *name
,
1297 ObjectProperty
*prop
= object_property_find(obj
, name
);
1299 error_setg(errp
, "Property '%s.%s' not found",
1300 object_get_typename(obj
), name
);
1305 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1308 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1309 iter
->nextclass
= object_get_class(obj
);
1312 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1315 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1316 if (!iter
->nextclass
) {
1319 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1320 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1325 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1328 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1329 iter
->nextclass
= object_class_get_parent(klass
);
1332 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
)
1334 ObjectClass
*parent_klass
;
1336 parent_klass
= object_class_get_parent(klass
);
1338 ObjectProperty
*prop
=
1339 object_class_property_find(parent_klass
, name
);
1345 return g_hash_table_lookup(klass
->properties
, name
);
1348 ObjectProperty
*object_class_property_find_err(ObjectClass
*klass
,
1352 ObjectProperty
*prop
= object_class_property_find(klass
, name
);
1354 error_setg(errp
, "Property '.%s' not found", name
);
1360 void object_property_del(Object
*obj
, const char *name
)
1362 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1364 if (prop
->release
) {
1365 prop
->release(obj
, name
, prop
->opaque
);
1367 g_hash_table_remove(obj
->properties
, name
);
1370 bool object_property_get(Object
*obj
, const char *name
, Visitor
*v
,
1374 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1381 error_setg(errp
, QERR_PERMISSION_DENIED
);
1384 prop
->get(obj
, v
, name
, prop
->opaque
, &err
);
1385 error_propagate(errp
, err
);
1389 bool object_property_set(Object
*obj
, const char *name
, Visitor
*v
,
1393 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1400 error_setg(errp
, QERR_PERMISSION_DENIED
);
1403 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1407 bool object_property_set_str(Object
*obj
, const char *name
,
1408 const char *value
, Error
**errp
)
1410 QString
*qstr
= qstring_from_str(value
);
1411 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qstr
), errp
);
1413 qobject_unref(qstr
);
1417 char *object_property_get_str(Object
*obj
, const char *name
,
1420 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1427 qstring
= qobject_to(QString
, ret
);
1429 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1432 retval
= g_strdup(qstring_get_str(qstring
));
1439 bool object_property_set_link(Object
*obj
, const char *name
,
1440 Object
*value
, Error
**errp
)
1442 g_autofree
char *path
= NULL
;
1445 path
= object_get_canonical_path(value
);
1447 return object_property_set_str(obj
, name
, path
?: "", errp
);
1450 Object
*object_property_get_link(Object
*obj
, const char *name
,
1453 char *str
= object_property_get_str(obj
, name
, errp
);
1454 Object
*target
= NULL
;
1457 target
= object_resolve_path(str
, NULL
);
1459 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1460 "Device '%s' not found", str
);
1468 bool object_property_set_bool(Object
*obj
, const char *name
,
1469 bool value
, Error
**errp
)
1471 QBool
*qbool
= qbool_from_bool(value
);
1472 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qbool
), errp
);
1474 qobject_unref(qbool
);
1478 bool object_property_get_bool(Object
*obj
, const char *name
,
1481 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1488 qbool
= qobject_to(QBool
, ret
);
1490 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1493 retval
= qbool_get_bool(qbool
);
1500 bool object_property_set_int(Object
*obj
, const char *name
,
1501 int64_t value
, Error
**errp
)
1503 QNum
*qnum
= qnum_from_int(value
);
1504 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1506 qobject_unref(qnum
);
1510 int64_t object_property_get_int(Object
*obj
, const char *name
,
1513 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1521 qnum
= qobject_to(QNum
, ret
);
1522 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1523 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1531 static void object_property_init_defval(Object
*obj
, ObjectProperty
*prop
)
1533 Visitor
*v
= qobject_input_visitor_new(prop
->defval
);
1535 assert(prop
->set
!= NULL
);
1536 prop
->set(obj
, v
, prop
->name
, prop
->opaque
, &error_abort
);
1541 static void object_property_set_default(ObjectProperty
*prop
, QObject
*defval
)
1543 assert(!prop
->defval
);
1544 assert(!prop
->init
);
1546 prop
->defval
= defval
;
1547 prop
->init
= object_property_init_defval
;
1550 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
)
1552 object_property_set_default(prop
, QOBJECT(qbool_from_bool(value
)));
1555 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
)
1557 object_property_set_default(prop
, QOBJECT(qstring_from_str(value
)));
1560 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
)
1562 object_property_set_default(prop
, QOBJECT(qnum_from_int(value
)));
1565 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
)
1567 object_property_set_default(prop
, QOBJECT(qnum_from_uint(value
)));
1570 bool object_property_set_uint(Object
*obj
, const char *name
,
1571 uint64_t value
, Error
**errp
)
1573 QNum
*qnum
= qnum_from_uint(value
);
1574 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1576 qobject_unref(qnum
);
1580 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1583 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1590 qnum
= qobject_to(QNum
, ret
);
1591 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1592 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1600 typedef struct EnumProperty
{
1601 const QEnumLookup
*lookup
;
1602 int (*get
)(Object
*, Error
**);
1603 void (*set
)(Object
*, int, Error
**);
1606 int object_property_get_enum(Object
*obj
, const char *name
,
1607 const char *typename
, Error
**errp
)
1611 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1612 EnumProperty
*enumprop
;
1618 if (!g_str_equal(prop
->type
, typename
)) {
1619 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1620 name
, object_class_get_name(
1621 object_get_class(obj
)), typename
);
1625 enumprop
= prop
->opaque
;
1627 str
= object_property_get_str(obj
, name
, errp
);
1632 ret
= qapi_enum_parse(enumprop
->lookup
, str
, -1, errp
);
1638 bool object_property_parse(Object
*obj
, const char *name
,
1639 const char *string
, Error
**errp
)
1641 Visitor
*v
= string_input_visitor_new(string
);
1642 bool ok
= object_property_set(obj
, name
, v
, errp
);
1648 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1652 char *string
= NULL
;
1654 v
= string_output_visitor_new(human
, &string
);
1655 if (!object_property_get(obj
, name
, v
, errp
)) {
1659 visit_complete(v
, &string
);
1666 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1668 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1676 Object
*object_get_root(void)
1678 static Object
*root
;
1681 root
= object_new("container");
1687 Object
*object_get_objects_root(void)
1689 return container_get(object_get_root(), "/objects");
1692 Object
*object_get_internal_root(void)
1694 static Object
*internal_root
;
1696 if (!internal_root
) {
1697 internal_root
= object_new("container");
1700 return internal_root
;
1703 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1704 const char *name
, void *opaque
,
1707 Object
*child
= opaque
;
1710 path
= object_get_canonical_path(child
);
1711 visit_type_str(v
, name
, &path
, errp
);
1715 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
,
1721 static void object_finalize_child_property(Object
*obj
, const char *name
,
1724 Object
*child
= opaque
;
1726 if (child
->class->unparent
) {
1727 (child
->class->unparent
)(child
);
1729 child
->parent
= NULL
;
1730 object_unref(child
);
1734 object_property_try_add_child(Object
*obj
, const char *name
,
1735 Object
*child
, Error
**errp
)
1737 g_autofree
char *type
= NULL
;
1740 assert(!child
->parent
);
1742 type
= g_strdup_printf("child<%s>", object_get_typename(child
));
1744 op
= object_property_try_add(obj
, name
, type
, object_get_child_property
,
1745 NULL
, object_finalize_child_property
,
1750 op
->resolve
= object_resolve_child_property
;
1752 child
->parent
= obj
;
1757 object_property_add_child(Object
*obj
, const char *name
,
1760 return object_property_try_add_child(obj
, name
, child
, &error_abort
);
1763 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1764 Object
*val
, Error
**errp
)
1766 /* Allow the link to be set, always */
1772 Object
*target
; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1773 ptrdiff_t offset
; /* if OBJ_PROP_LINK_CLASS */
1775 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1776 ObjectPropertyLinkFlags flags
;
1780 object_link_get_targetp(Object
*obj
, LinkProperty
*lprop
)
1782 if (lprop
->flags
& OBJ_PROP_LINK_DIRECT
) {
1783 return &lprop
->target
;
1784 } else if (lprop
->flags
& OBJ_PROP_LINK_CLASS
) {
1785 return (void *)obj
+ lprop
->offset
;
1787 return lprop
->targetp
;
1791 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1792 const char *name
, void *opaque
,
1795 LinkProperty
*lprop
= opaque
;
1796 Object
**targetp
= object_link_get_targetp(obj
, lprop
);
1800 path
= object_get_canonical_path(*targetp
);
1801 visit_type_str(v
, name
, &path
, errp
);
1805 visit_type_str(v
, name
, &path
, errp
);
1810 * object_resolve_link:
1812 * Lookup an object and ensure its type matches the link property type. This
1813 * is similar to object_resolve_path() except type verification against the
1814 * link property is performed.
1816 * Returns: The matched object or NULL on path lookup failures.
1818 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1819 const char *path
, Error
**errp
)
1823 bool ambiguous
= false;
1826 /* Go from link<FOO> to FOO. */
1827 type
= object_property_get_type(obj
, name
, NULL
);
1828 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1829 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1832 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1834 } else if (!target
) {
1835 target
= object_resolve_path(path
, &ambiguous
);
1836 if (target
|| ambiguous
) {
1837 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1839 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1840 "Device '%s' not found", path
);
1844 g_free(target_type
);
1849 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1850 const char *name
, void *opaque
,
1853 Error
*local_err
= NULL
;
1854 LinkProperty
*prop
= opaque
;
1855 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1856 Object
*old_target
= *targetp
;
1860 if (!visit_type_str(v
, name
, &path
, errp
)) {
1865 new_target
= object_resolve_link(obj
, name
, path
, errp
);
1876 prop
->check(obj
, name
, new_target
, &local_err
);
1878 error_propagate(errp
, local_err
);
1882 *targetp
= new_target
;
1883 if (prop
->flags
& OBJ_PROP_LINK_STRONG
) {
1884 object_ref(new_target
);
1885 object_unref(old_target
);
1889 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
,
1892 LinkProperty
*lprop
= opaque
;
1894 return *object_link_get_targetp(parent
, lprop
);
1897 static void object_release_link_property(Object
*obj
, const char *name
,
1900 LinkProperty
*prop
= opaque
;
1901 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1903 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *targetp
) {
1904 object_unref(*targetp
);
1906 if (!(prop
->flags
& OBJ_PROP_LINK_CLASS
)) {
1911 static ObjectProperty
*
1912 object_add_link_prop(Object
*obj
, const char *name
,
1913 const char *type
, void *ptr
,
1914 void (*check
)(const Object
*, const char *,
1915 Object
*, Error
**),
1916 ObjectPropertyLinkFlags flags
)
1918 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1919 g_autofree
char *full_type
= NULL
;
1922 if (flags
& OBJ_PROP_LINK_DIRECT
) {
1925 prop
->targetp
= ptr
;
1927 prop
->check
= check
;
1928 prop
->flags
= flags
;
1930 full_type
= g_strdup_printf("link<%s>", type
);
1932 op
= object_property_add(obj
, name
, full_type
,
1933 object_get_link_property
,
1934 check
? object_set_link_property
: NULL
,
1935 object_release_link_property
,
1937 op
->resolve
= object_resolve_link_property
;
1942 object_property_add_link(Object
*obj
, const char *name
,
1943 const char *type
, Object
**targetp
,
1944 void (*check
)(const Object
*, const char *,
1945 Object
*, Error
**),
1946 ObjectPropertyLinkFlags flags
)
1948 return object_add_link_prop(obj
, name
, type
, targetp
, check
, flags
);
1952 object_class_property_add_link(ObjectClass
*oc
,
1954 const char *type
, ptrdiff_t offset
,
1955 void (*check
)(const Object
*obj
, const char *name
,
1956 Object
*val
, Error
**errp
),
1957 ObjectPropertyLinkFlags flags
)
1959 LinkProperty
*prop
= g_new0(LinkProperty
, 1);
1963 prop
->offset
= offset
;
1964 prop
->check
= check
;
1965 prop
->flags
= flags
| OBJ_PROP_LINK_CLASS
;
1967 full_type
= g_strdup_printf("link<%s>", type
);
1969 op
= object_class_property_add(oc
, name
, full_type
,
1970 object_get_link_property
,
1971 check
? object_set_link_property
: NULL
,
1972 object_release_link_property
,
1975 op
->resolve
= object_resolve_link_property
;
1982 object_property_add_const_link(Object
*obj
, const char *name
,
1985 return object_add_link_prop(obj
, name
,
1986 object_get_typename(target
), target
,
1987 NULL
, OBJ_PROP_LINK_DIRECT
);
1990 const char *object_get_canonical_path_component(const Object
*obj
)
1992 ObjectProperty
*prop
= NULL
;
1993 GHashTableIter iter
;
1995 if (obj
->parent
== NULL
) {
1999 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
2000 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2001 if (!object_property_is_child(prop
)) {
2005 if (prop
->opaque
== obj
) {
2010 /* obj had a parent but was not a child, should never happen */
2011 g_assert_not_reached();
2015 char *object_get_canonical_path(const Object
*obj
)
2017 Object
*root
= object_get_root();
2018 char *newpath
, *path
= NULL
;
2021 return g_strdup("/");
2025 const char *component
= object_get_canonical_path_component(obj
);
2028 /* A canonical path must be complete, so discard what was
2035 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
2039 } while (obj
!= root
);
2044 Object
*object_resolve_path_component(Object
*parent
, const char *part
)
2046 ObjectProperty
*prop
= object_property_find(parent
, part
);
2051 if (prop
->resolve
) {
2052 return prop
->resolve(parent
, prop
->opaque
, part
);
2058 static Object
*object_resolve_abs_path(Object
*parent
,
2060 const char *typename
)
2064 if (*parts
== NULL
) {
2065 return object_dynamic_cast(parent
, typename
);
2068 if (strcmp(*parts
, "") == 0) {
2069 return object_resolve_abs_path(parent
, parts
+ 1, typename
);
2072 child
= object_resolve_path_component(parent
, *parts
);
2077 return object_resolve_abs_path(child
, parts
+ 1, typename
);
2080 static Object
*object_resolve_partial_path(Object
*parent
,
2082 const char *typename
,
2086 GHashTableIter iter
;
2087 ObjectProperty
*prop
;
2089 obj
= object_resolve_abs_path(parent
, parts
, typename
);
2091 g_hash_table_iter_init(&iter
, parent
->properties
);
2092 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2095 if (!object_property_is_child(prop
)) {
2099 found
= object_resolve_partial_path(prop
->opaque
, parts
,
2100 typename
, ambiguous
);
2117 Object
*object_resolve_path_type(const char *path
, const char *typename
,
2123 parts
= g_strsplit(path
, "/", 0);
2126 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
2127 bool ambiguous
= false;
2128 obj
= object_resolve_partial_path(object_get_root(), parts
,
2129 typename
, &ambiguous
);
2131 *ambiguousp
= ambiguous
;
2134 obj
= object_resolve_abs_path(object_get_root(), parts
+ 1, typename
);
2142 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
2144 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
2147 Object
*object_resolve_path_at(Object
*parent
, const char *path
)
2149 g_auto(GStrv
) parts
= g_strsplit(path
, "/", 0);
2152 return object_resolve_abs_path(object_get_root(), parts
+ 1,
2155 return object_resolve_abs_path(parent
, parts
, TYPE_OBJECT
);
2158 typedef struct StringProperty
2160 char *(*get
)(Object
*, Error
**);
2161 void (*set
)(Object
*, const char *, Error
**);
2164 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
2165 void *opaque
, Error
**errp
)
2167 StringProperty
*prop
= opaque
;
2171 value
= prop
->get(obj
, &err
);
2173 error_propagate(errp
, err
);
2177 visit_type_str(v
, name
, &value
, errp
);
2181 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
2182 void *opaque
, Error
**errp
)
2184 StringProperty
*prop
= opaque
;
2187 if (!visit_type_str(v
, name
, &value
, errp
)) {
2191 prop
->set(obj
, value
, errp
);
2195 static void property_release_data(Object
*obj
, const char *name
,
2202 object_property_add_str(Object
*obj
, const char *name
,
2203 char *(*get
)(Object
*, Error
**),
2204 void (*set
)(Object
*, const char *, Error
**))
2206 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2211 return object_property_add(obj
, name
, "string",
2212 get
? property_get_str
: NULL
,
2213 set
? property_set_str
: NULL
,
2214 property_release_data
,
2219 object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2220 char *(*get
)(Object
*, Error
**),
2221 void (*set
)(Object
*, const char *,
2224 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2229 return object_class_property_add(klass
, name
, "string",
2230 get
? property_get_str
: NULL
,
2231 set
? property_set_str
: NULL
,
2236 typedef struct BoolProperty
2238 bool (*get
)(Object
*, Error
**);
2239 void (*set
)(Object
*, bool, Error
**);
2242 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2243 void *opaque
, Error
**errp
)
2245 BoolProperty
*prop
= opaque
;
2249 value
= prop
->get(obj
, &err
);
2251 error_propagate(errp
, err
);
2255 visit_type_bool(v
, name
, &value
, errp
);
2258 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2259 void *opaque
, Error
**errp
)
2261 BoolProperty
*prop
= opaque
;
2264 if (!visit_type_bool(v
, name
, &value
, errp
)) {
2268 prop
->set(obj
, value
, errp
);
2272 object_property_add_bool(Object
*obj
, const char *name
,
2273 bool (*get
)(Object
*, Error
**),
2274 void (*set
)(Object
*, bool, Error
**))
2276 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2281 return object_property_add(obj
, name
, "bool",
2282 get
? property_get_bool
: NULL
,
2283 set
? property_set_bool
: NULL
,
2284 property_release_data
,
2289 object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2290 bool (*get
)(Object
*, Error
**),
2291 void (*set
)(Object
*, bool, Error
**))
2293 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2298 return object_class_property_add(klass
, name
, "bool",
2299 get
? property_get_bool
: NULL
,
2300 set
? property_set_bool
: NULL
,
2305 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2306 void *opaque
, Error
**errp
)
2308 EnumProperty
*prop
= opaque
;
2312 value
= prop
->get(obj
, &err
);
2314 error_propagate(errp
, err
);
2318 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2321 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2322 void *opaque
, Error
**errp
)
2324 EnumProperty
*prop
= opaque
;
2327 if (!visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
)) {
2330 prop
->set(obj
, value
, errp
);
2334 object_property_add_enum(Object
*obj
, const char *name
,
2335 const char *typename
,
2336 const QEnumLookup
*lookup
,
2337 int (*get
)(Object
*, Error
**),
2338 void (*set
)(Object
*, int, Error
**))
2340 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2342 prop
->lookup
= lookup
;
2346 return object_property_add(obj
, name
, typename
,
2347 get
? property_get_enum
: NULL
,
2348 set
? property_set_enum
: NULL
,
2349 property_release_data
,
2354 object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2355 const char *typename
,
2356 const QEnumLookup
*lookup
,
2357 int (*get
)(Object
*, Error
**),
2358 void (*set
)(Object
*, int, Error
**))
2360 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2362 prop
->lookup
= lookup
;
2366 return object_class_property_add(klass
, name
, typename
,
2367 get
? property_get_enum
: NULL
,
2368 set
? property_set_enum
: NULL
,
2373 typedef struct TMProperty
{
2374 void (*get
)(Object
*, struct tm
*, Error
**);
2377 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2378 void *opaque
, Error
**errp
)
2380 TMProperty
*prop
= opaque
;
2384 prop
->get(obj
, &value
, &err
);
2386 error_propagate(errp
, err
);
2390 if (!visit_start_struct(v
, name
, NULL
, 0, errp
)) {
2393 if (!visit_type_int32(v
, "tm_year", &value
.tm_year
, errp
)) {
2396 if (!visit_type_int32(v
, "tm_mon", &value
.tm_mon
, errp
)) {
2399 if (!visit_type_int32(v
, "tm_mday", &value
.tm_mday
, errp
)) {
2402 if (!visit_type_int32(v
, "tm_hour", &value
.tm_hour
, errp
)) {
2405 if (!visit_type_int32(v
, "tm_min", &value
.tm_min
, errp
)) {
2408 if (!visit_type_int32(v
, "tm_sec", &value
.tm_sec
, errp
)) {
2411 visit_check_struct(v
, errp
);
2413 visit_end_struct(v
, NULL
);
2417 object_property_add_tm(Object
*obj
, const char *name
,
2418 void (*get
)(Object
*, struct tm
*, Error
**))
2420 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2424 return object_property_add(obj
, name
, "struct tm",
2425 get
? property_get_tm
: NULL
, NULL
,
2426 property_release_data
,
2431 object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2432 void (*get
)(Object
*, struct tm
*, Error
**))
2434 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2438 return object_class_property_add(klass
, name
, "struct tm",
2439 get
? property_get_tm
: NULL
,
2443 static char *object_get_type(Object
*obj
, Error
**errp
)
2445 return g_strdup(object_get_typename(obj
));
2448 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2449 void *opaque
, Error
**errp
)
2451 uint8_t value
= *(uint8_t *)opaque
;
2452 visit_type_uint8(v
, name
, &value
, errp
);
2455 static void property_set_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2456 void *opaque
, Error
**errp
)
2458 uint8_t *field
= opaque
;
2461 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
2468 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2469 void *opaque
, Error
**errp
)
2471 uint16_t value
= *(uint16_t *)opaque
;
2472 visit_type_uint16(v
, name
, &value
, errp
);
2475 static void property_set_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2476 void *opaque
, Error
**errp
)
2478 uint16_t *field
= opaque
;
2481 if (!visit_type_uint16(v
, name
, &value
, errp
)) {
2488 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2489 void *opaque
, Error
**errp
)
2491 uint32_t value
= *(uint32_t *)opaque
;
2492 visit_type_uint32(v
, name
, &value
, errp
);
2495 static void property_set_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2496 void *opaque
, Error
**errp
)
2498 uint32_t *field
= opaque
;
2501 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
2508 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2509 void *opaque
, Error
**errp
)
2511 uint64_t value
= *(uint64_t *)opaque
;
2512 visit_type_uint64(v
, name
, &value
, errp
);
2515 static void property_set_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2516 void *opaque
, Error
**errp
)
2518 uint64_t *field
= opaque
;
2521 if (!visit_type_uint64(v
, name
, &value
, errp
)) {
2529 object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2531 ObjectPropertyFlags flags
)
2533 ObjectPropertyAccessor
*getter
= NULL
;
2534 ObjectPropertyAccessor
*setter
= NULL
;
2536 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2537 getter
= property_get_uint8_ptr
;
2540 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2541 setter
= property_set_uint8_ptr
;
2544 return object_property_add(obj
, name
, "uint8",
2545 getter
, setter
, NULL
, (void *)v
);
2549 object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2551 ObjectPropertyFlags flags
)
2553 ObjectPropertyAccessor
*getter
= NULL
;
2554 ObjectPropertyAccessor
*setter
= NULL
;
2556 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2557 getter
= property_get_uint8_ptr
;
2560 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2561 setter
= property_set_uint8_ptr
;
2564 return object_class_property_add(klass
, name
, "uint8",
2565 getter
, setter
, NULL
, (void *)v
);
2569 object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2571 ObjectPropertyFlags flags
)
2573 ObjectPropertyAccessor
*getter
= NULL
;
2574 ObjectPropertyAccessor
*setter
= NULL
;
2576 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2577 getter
= property_get_uint16_ptr
;
2580 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2581 setter
= property_set_uint16_ptr
;
2584 return object_property_add(obj
, name
, "uint16",
2585 getter
, setter
, NULL
, (void *)v
);
2589 object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2591 ObjectPropertyFlags flags
)
2593 ObjectPropertyAccessor
*getter
= NULL
;
2594 ObjectPropertyAccessor
*setter
= NULL
;
2596 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2597 getter
= property_get_uint16_ptr
;
2600 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2601 setter
= property_set_uint16_ptr
;
2604 return object_class_property_add(klass
, name
, "uint16",
2605 getter
, setter
, NULL
, (void *)v
);
2609 object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2611 ObjectPropertyFlags flags
)
2613 ObjectPropertyAccessor
*getter
= NULL
;
2614 ObjectPropertyAccessor
*setter
= NULL
;
2616 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2617 getter
= property_get_uint32_ptr
;
2620 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2621 setter
= property_set_uint32_ptr
;
2624 return object_property_add(obj
, name
, "uint32",
2625 getter
, setter
, NULL
, (void *)v
);
2629 object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2631 ObjectPropertyFlags flags
)
2633 ObjectPropertyAccessor
*getter
= NULL
;
2634 ObjectPropertyAccessor
*setter
= NULL
;
2636 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2637 getter
= property_get_uint32_ptr
;
2640 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2641 setter
= property_set_uint32_ptr
;
2644 return object_class_property_add(klass
, name
, "uint32",
2645 getter
, setter
, NULL
, (void *)v
);
2649 object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2651 ObjectPropertyFlags flags
)
2653 ObjectPropertyAccessor
*getter
= NULL
;
2654 ObjectPropertyAccessor
*setter
= NULL
;
2656 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2657 getter
= property_get_uint64_ptr
;
2660 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2661 setter
= property_set_uint64_ptr
;
2664 return object_property_add(obj
, name
, "uint64",
2665 getter
, setter
, NULL
, (void *)v
);
2669 object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2671 ObjectPropertyFlags flags
)
2673 ObjectPropertyAccessor
*getter
= NULL
;
2674 ObjectPropertyAccessor
*setter
= NULL
;
2676 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2677 getter
= property_get_uint64_ptr
;
2680 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2681 setter
= property_set_uint64_ptr
;
2684 return object_class_property_add(klass
, name
, "uint64",
2685 getter
, setter
, NULL
, (void *)v
);
2693 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2694 void *opaque
, Error
**errp
)
2696 AliasProperty
*prop
= opaque
;
2697 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2699 object_property_get(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2700 visit_free(alias_v
);
2703 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2704 void *opaque
, Error
**errp
)
2706 AliasProperty
*prop
= opaque
;
2707 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2709 object_property_set(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2710 visit_free(alias_v
);
2713 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2716 AliasProperty
*prop
= opaque
;
2718 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2721 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2723 AliasProperty
*prop
= opaque
;
2725 g_free(prop
->target_name
);
2730 object_property_add_alias(Object
*obj
, const char *name
,
2731 Object
*target_obj
, const char *target_name
)
2733 AliasProperty
*prop
;
2735 ObjectProperty
*target_prop
;
2736 g_autofree
char *prop_type
= NULL
;
2738 target_prop
= object_property_find_err(target_obj
, target_name
,
2741 if (object_property_is_child(target_prop
)) {
2742 prop_type
= g_strdup_printf("link%s",
2743 target_prop
->type
+ strlen("child"));
2745 prop_type
= g_strdup(target_prop
->type
);
2748 prop
= g_malloc(sizeof(*prop
));
2749 prop
->target_obj
= target_obj
;
2750 prop
->target_name
= g_strdup(target_name
);
2752 op
= object_property_add(obj
, name
, prop_type
,
2755 property_release_alias
,
2757 op
->resolve
= property_resolve_alias
;
2758 if (target_prop
->defval
) {
2759 op
->defval
= qobject_ref(target_prop
->defval
);
2762 object_property_set_description(obj
, op
->name
,
2763 target_prop
->description
);
2767 void object_property_set_description(Object
*obj
, const char *name
,
2768 const char *description
)
2772 op
= object_property_find_err(obj
, name
, &error_abort
);
2773 g_free(op
->description
);
2774 op
->description
= g_strdup(description
);
2777 void object_class_property_set_description(ObjectClass
*klass
,
2779 const char *description
)
2783 op
= g_hash_table_lookup(klass
->properties
, name
);
2784 g_free(op
->description
);
2785 op
->description
= g_strdup(description
);
2788 static void object_class_init(ObjectClass
*klass
, void *data
)
2790 object_class_property_add_str(klass
, "type", object_get_type
,
2794 static void register_types(void)
2796 static const TypeInfo interface_info
= {
2797 .name
= TYPE_INTERFACE
,
2798 .class_size
= sizeof(InterfaceClass
),
2802 static const TypeInfo object_info
= {
2803 .name
= TYPE_OBJECT
,
2804 .instance_size
= sizeof(Object
),
2805 .class_init
= object_class_init
,
2809 type_interface
= type_register_internal(&interface_info
);
2810 type_register_internal(&object_info
);
2813 type_init(register_types
)