4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qobject-input-visitor.h"
23 #include "qapi/qapi-builtin-visit.h"
24 #include "qapi/qmp/qerror.h"
25 #include "qapi/qmp/qjson.h"
28 /* TODO: replace QObject with a simpler visitor to avoid a dependency
29 * of the QOM core on QObject? */
30 #include "qom/qom-qobject.h"
31 #include "qapi/qmp/qbool.h"
32 #include "qapi/qmp/qnum.h"
33 #include "qapi/qmp/qstring.h"
34 #include "qemu/error-report.h"
36 #define MAX_INTERFACES 32
38 typedef struct InterfaceImpl InterfaceImpl
;
39 typedef struct TypeImpl TypeImpl
;
54 void (*class_init
)(ObjectClass
*klass
, void *data
);
55 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
59 void (*instance_init
)(Object
*obj
);
60 void (*instance_post_init
)(Object
*obj
);
61 void (*instance_finalize
)(Object
*obj
);
66 TypeImpl
*parent_type
;
71 InterfaceImpl interfaces
[MAX_INTERFACES
];
74 static Type type_interface
;
76 static GHashTable
*type_table_get(void)
78 static GHashTable
*type_table
;
80 if (type_table
== NULL
) {
81 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
87 static bool enumerating_types
;
89 static void type_table_add(TypeImpl
*ti
)
91 assert(!enumerating_types
);
92 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
95 static TypeImpl
*type_table_lookup(const char *name
)
97 return g_hash_table_lookup(type_table_get(), name
);
100 static TypeImpl
*type_new(const TypeInfo
*info
)
102 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
105 g_assert(info
->name
!= NULL
);
107 if (type_table_lookup(info
->name
) != NULL
) {
108 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
112 ti
->name
= g_strdup(info
->name
);
113 ti
->parent
= g_strdup(info
->parent
);
115 ti
->class_size
= info
->class_size
;
116 ti
->instance_size
= info
->instance_size
;
118 ti
->class_init
= info
->class_init
;
119 ti
->class_base_init
= info
->class_base_init
;
120 ti
->class_data
= info
->class_data
;
122 ti
->instance_init
= info
->instance_init
;
123 ti
->instance_post_init
= info
->instance_post_init
;
124 ti
->instance_finalize
= info
->instance_finalize
;
126 ti
->abstract
= info
->abstract
;
128 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
129 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
131 ti
->num_interfaces
= i
;
136 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
145 TypeImpl
*type_register(const TypeInfo
*info
)
147 assert(info
->parent
);
148 return type_register_internal(info
);
151 TypeImpl
*type_register_static(const TypeInfo
*info
)
153 return type_register(info
);
156 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
160 for (i
= 0; i
< nr_infos
; i
++) {
161 type_register_static(&infos
[i
]);
165 static TypeImpl
*type_get_by_name(const char *name
)
171 return type_table_lookup(name
);
174 static TypeImpl
*type_get_parent(TypeImpl
*type
)
176 if (!type
->parent_type
&& type
->parent
) {
177 type
->parent_type
= type_get_by_name(type
->parent
);
178 if (!type
->parent_type
) {
179 fprintf(stderr
, "Type '%s' is missing its parent '%s'\n",
180 type
->name
, type
->parent
);
185 return type
->parent_type
;
188 static bool type_has_parent(TypeImpl
*type
)
190 return (type
->parent
!= NULL
);
193 static size_t type_class_get_size(TypeImpl
*ti
)
195 if (ti
->class_size
) {
196 return ti
->class_size
;
199 if (type_has_parent(ti
)) {
200 return type_class_get_size(type_get_parent(ti
));
203 return sizeof(ObjectClass
);
206 static size_t type_object_get_size(TypeImpl
*ti
)
208 if (ti
->instance_size
) {
209 return ti
->instance_size
;
212 if (type_has_parent(ti
)) {
213 return type_object_get_size(type_get_parent(ti
));
219 size_t object_type_get_instance_size(const char *typename
)
221 TypeImpl
*type
= type_get_by_name(typename
);
223 g_assert(type
!= NULL
);
224 return type_object_get_size(type
);
227 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
231 /* Check if target_type is a direct ancestor of type */
233 if (type
== target_type
) {
237 type
= type_get_parent(type
);
243 static void type_initialize(TypeImpl
*ti
);
245 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
246 TypeImpl
*parent_type
)
248 InterfaceClass
*new_iface
;
250 TypeImpl
*iface_impl
;
252 info
.parent
= parent_type
->name
;
253 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
254 info
.abstract
= true;
256 iface_impl
= type_new(&info
);
257 iface_impl
->parent_type
= parent_type
;
258 type_initialize(iface_impl
);
259 g_free((char *)info
.name
);
261 new_iface
= (InterfaceClass
*)iface_impl
->class;
262 new_iface
->concrete_class
= ti
->class;
263 new_iface
->interface_type
= interface_type
;
265 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
, new_iface
);
268 static void object_property_free(gpointer data
)
270 ObjectProperty
*prop
= data
;
273 qobject_unref(prop
->defval
);
278 g_free(prop
->description
);
282 static void type_initialize(TypeImpl
*ti
)
290 ti
->class_size
= type_class_get_size(ti
);
291 ti
->instance_size
= type_object_get_size(ti
);
292 /* Any type with zero instance_size is implicitly abstract.
293 * This means interface types are all abstract.
295 if (ti
->instance_size
== 0) {
298 if (type_is_ancestor(ti
, type_interface
)) {
299 assert(ti
->instance_size
== 0);
300 assert(ti
->abstract
);
301 assert(!ti
->instance_init
);
302 assert(!ti
->instance_post_init
);
303 assert(!ti
->instance_finalize
);
304 assert(!ti
->num_interfaces
);
306 ti
->class = g_malloc0(ti
->class_size
);
308 parent
= type_get_parent(ti
);
310 type_initialize(parent
);
314 g_assert(parent
->class_size
<= ti
->class_size
);
315 g_assert(parent
->instance_size
<= ti
->instance_size
);
316 memcpy(ti
->class, parent
->class, parent
->class_size
);
317 ti
->class->interfaces
= NULL
;
319 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
320 InterfaceClass
*iface
= e
->data
;
321 ObjectClass
*klass
= OBJECT_CLASS(iface
);
323 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
326 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
327 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
329 error_report("missing interface '%s' for object '%s'",
330 ti
->interfaces
[i
].typename
, parent
->name
);
333 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
334 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
336 if (type_is_ancestor(target_type
, t
)) {
345 type_initialize_interface(ti
, t
, t
);
349 ti
->class->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
350 object_property_free
);
352 ti
->class->type
= ti
;
355 if (parent
->class_base_init
) {
356 parent
->class_base_init(ti
->class, ti
->class_data
);
358 parent
= type_get_parent(parent
);
361 if (ti
->class_init
) {
362 ti
->class_init(ti
->class, ti
->class_data
);
366 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
368 if (type_has_parent(ti
)) {
369 object_init_with_type(obj
, type_get_parent(ti
));
372 if (ti
->instance_init
) {
373 ti
->instance_init(obj
);
377 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
379 if (ti
->instance_post_init
) {
380 ti
->instance_post_init(obj
);
383 if (type_has_parent(ti
)) {
384 object_post_init_with_type(obj
, type_get_parent(ti
));
388 void object_apply_global_props(Object
*obj
, const GPtrArray
*props
, Error
**errp
)
396 for (i
= 0; i
< props
->len
; i
++) {
397 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
400 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
403 if (p
->optional
&& !object_property_find(obj
, p
->property
, NULL
)) {
407 object_property_parse(obj
, p
->value
, p
->property
, &err
);
409 error_prepend(&err
, "can't apply global %s.%s=%s: ",
410 p
->driver
, p
->property
, p
->value
);
412 * If errp != NULL, propagate error and return.
413 * If errp == NULL, report a warning, but keep going
414 * with the remaining globals.
417 error_propagate(errp
, err
);
420 warn_report_err(err
);
427 * Global property defaults
428 * Slot 0: accelerator's global property defaults
429 * Slot 1: machine's global property defaults
430 * Slot 2: global properties from legacy command line option
431 * Each is a GPtrArray of of GlobalProperty.
432 * Applied in order, later entries override earlier ones.
434 static GPtrArray
*object_compat_props
[3];
437 * Retrieve @GPtrArray for global property defined with options
438 * other than "-global". These are generally used for syntactic
439 * sugar and legacy command line options.
441 void object_register_sugar_prop(const char *driver
, const char *prop
, const char *value
)
444 if (!object_compat_props
[2]) {
445 object_compat_props
[2] = g_ptr_array_new();
447 g
= g_new0(GlobalProperty
, 1);
448 g
->driver
= g_strdup(driver
);
449 g
->property
= g_strdup(prop
);
450 g
->value
= g_strdup(value
);
451 g_ptr_array_add(object_compat_props
[2], g
);
455 * Set machine's global property defaults to @compat_props.
456 * May be called at most once.
458 void object_set_machine_compat_props(GPtrArray
*compat_props
)
460 assert(!object_compat_props
[1]);
461 object_compat_props
[1] = compat_props
;
465 * Set accelerator's global property defaults to @compat_props.
466 * May be called at most once.
468 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
470 assert(!object_compat_props
[0]);
471 object_compat_props
[0] = compat_props
;
474 void object_apply_compat_props(Object
*obj
)
478 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
479 object_apply_global_props(obj
, object_compat_props
[i
],
480 i
== 2 ? &error_fatal
: &error_abort
);
484 static void object_class_property_init_all(Object
*obj
)
486 ObjectPropertyIterator iter
;
487 ObjectProperty
*prop
;
489 object_class_property_iter_init(&iter
, object_get_class(obj
));
490 while ((prop
= object_property_iter_next(&iter
))) {
492 prop
->init(obj
, prop
);
497 static void object_initialize_with_type(Object
*obj
, size_t size
, TypeImpl
*type
)
499 type_initialize(type
);
501 g_assert(type
->instance_size
>= sizeof(Object
));
502 g_assert(type
->abstract
== false);
503 g_assert(size
>= type
->instance_size
);
505 memset(obj
, 0, type
->instance_size
);
506 obj
->class = type
->class;
508 object_class_property_init_all(obj
);
509 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
510 NULL
, object_property_free
);
511 object_init_with_type(obj
, type
);
512 object_post_init_with_type(obj
, type
);
515 void object_initialize(void *data
, size_t size
, const char *typename
)
517 TypeImpl
*type
= type_get_by_name(typename
);
520 error_report("missing object type '%s'", typename
);
524 object_initialize_with_type(data
, size
, type
);
527 void object_initialize_child(Object
*parentobj
, const char *propname
,
528 void *childobj
, size_t size
, const char *type
,
533 va_start(vargs
, errp
);
534 object_initialize_childv(parentobj
, propname
, childobj
, size
, type
, errp
,
539 void object_initialize_childv(Object
*parentobj
, const char *propname
,
540 void *childobj
, size_t size
, const char *type
,
541 Error
**errp
, va_list vargs
)
543 Error
*local_err
= NULL
;
547 object_initialize(childobj
, size
, type
);
548 obj
= OBJECT(childobj
);
550 object_set_propv(obj
, &local_err
, vargs
);
555 object_property_add_child(parentobj
, propname
, obj
);
557 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
559 user_creatable_complete(uc
, &local_err
);
561 object_unparent(obj
);
568 * We want @obj's reference to be 1 on success, 0 on failure.
569 * On success, it's 2: one taken by object_initialize(), and one
570 * by object_property_add_child().
571 * On failure in object_initialize() or earlier, it's 1.
572 * On failure afterwards, it's also 1: object_unparent() releases
573 * the reference taken by object_property_add_child().
577 error_propagate(errp
, local_err
);
580 static inline bool object_property_is_child(ObjectProperty
*prop
)
582 return strstart(prop
->type
, "child<", NULL
);
585 static void object_property_del_all(Object
*obj
)
587 g_autoptr(GHashTable
) done
= g_hash_table_new(NULL
, NULL
);
588 ObjectProperty
*prop
;
589 ObjectPropertyIterator iter
;
594 object_property_iter_init(&iter
, obj
);
595 while ((prop
= object_property_iter_next(&iter
)) != NULL
) {
596 if (g_hash_table_add(done
, prop
)) {
598 prop
->release(obj
, prop
->name
, prop
->opaque
);
606 g_hash_table_unref(obj
->properties
);
609 static void object_property_del_child(Object
*obj
, Object
*child
)
611 ObjectProperty
*prop
;
615 g_hash_table_iter_init(&iter
, obj
->properties
);
616 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
618 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
620 prop
->release(obj
, prop
->name
, prop
->opaque
);
621 prop
->release
= NULL
;
626 g_hash_table_iter_init(&iter
, obj
->properties
);
627 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
629 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
630 g_hash_table_iter_remove(&iter
);
636 void object_unparent(Object
*obj
)
639 object_property_del_child(obj
->parent
, obj
);
643 static void object_deinit(Object
*obj
, TypeImpl
*type
)
645 if (type
->instance_finalize
) {
646 type
->instance_finalize(obj
);
649 if (type_has_parent(type
)) {
650 object_deinit(obj
, type_get_parent(type
));
654 static void object_finalize(void *data
)
657 TypeImpl
*ti
= obj
->class->type
;
659 object_property_del_all(obj
);
660 object_deinit(obj
, ti
);
662 g_assert(obj
->ref
== 0);
668 static Object
*object_new_with_type(Type type
)
672 g_assert(type
!= NULL
);
673 type_initialize(type
);
675 obj
= g_malloc(type
->instance_size
);
676 object_initialize_with_type(obj
, type
->instance_size
, type
);
682 Object
*object_new_with_class(ObjectClass
*klass
)
684 return object_new_with_type(klass
->type
);
687 Object
*object_new(const char *typename
)
689 TypeImpl
*ti
= type_get_by_name(typename
);
691 return object_new_with_type(ti
);
695 Object
*object_new_with_props(const char *typename
,
704 va_start(vargs
, errp
);
705 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
712 Object
*object_new_with_propv(const char *typename
,
720 Error
*local_err
= NULL
;
723 klass
= object_class_by_name(typename
);
725 error_setg(errp
, "invalid object type: %s", typename
);
729 if (object_class_is_abstract(klass
)) {
730 error_setg(errp
, "object type '%s' is abstract", typename
);
733 obj
= object_new_with_type(klass
->type
);
735 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
740 object_property_add_child(parent
, id
, obj
);
743 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
745 user_creatable_complete(uc
, &local_err
);
748 object_unparent(obj
);
758 error_propagate(errp
, local_err
);
764 int object_set_props(Object
*obj
,
771 va_start(vargs
, errp
);
772 ret
= object_set_propv(obj
, errp
, vargs
);
779 int object_set_propv(Object
*obj
,
783 const char *propname
;
784 Error
*local_err
= NULL
;
786 propname
= va_arg(vargs
, char *);
787 while (propname
!= NULL
) {
788 const char *value
= va_arg(vargs
, char *);
790 g_assert(value
!= NULL
);
791 object_property_parse(obj
, value
, propname
, &local_err
);
793 error_propagate(errp
, local_err
);
796 propname
= va_arg(vargs
, char *);
803 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
805 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
812 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
813 const char *file
, int line
, const char *func
)
815 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
816 typename
, file
, line
, func
);
818 #ifdef CONFIG_QOM_CAST_DEBUG
822 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
823 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
828 inst
= object_dynamic_cast(obj
, typename
);
831 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
832 file
, line
, func
, obj
, typename
);
838 if (obj
&& obj
== inst
) {
839 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
840 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
841 atomic_read(&obj
->class->object_cast_cache
[i
]));
843 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
851 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
852 const char *typename
)
854 ObjectClass
*ret
= NULL
;
855 TypeImpl
*target_type
;
862 /* A simple fast path that can trigger a lot for leaf classes. */
864 if (type
->name
== typename
) {
868 target_type
= type_get_by_name(typename
);
870 /* target class type unknown, so fail the cast */
874 if (type
->class->interfaces
&&
875 type_is_ancestor(target_type
, type_interface
)) {
879 for (i
= class->interfaces
; i
; i
= i
->next
) {
880 ObjectClass
*target_class
= i
->data
;
882 if (type_is_ancestor(target_class
->type
, target_type
)) {
888 /* The match was ambiguous, don't allow a cast */
892 } else if (type_is_ancestor(type
, target_type
)) {
899 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
900 const char *typename
,
901 const char *file
, int line
,
906 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
907 typename
, file
, line
, func
);
909 #ifdef CONFIG_QOM_CAST_DEBUG
912 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
913 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
919 if (!class || !class->interfaces
) {
924 ret
= object_class_dynamic_cast(class, typename
);
926 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
927 file
, line
, func
, class, typename
);
931 #ifdef CONFIG_QOM_CAST_DEBUG
932 if (class && ret
== class) {
933 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
934 atomic_set(&class->class_cast_cache
[i
- 1],
935 atomic_read(&class->class_cast_cache
[i
]));
937 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
944 const char *object_get_typename(const Object
*obj
)
946 return obj
->class->type
->name
;
949 ObjectClass
*object_get_class(Object
*obj
)
954 bool object_class_is_abstract(ObjectClass
*klass
)
956 return klass
->type
->abstract
;
959 const char *object_class_get_name(ObjectClass
*klass
)
961 return klass
->type
->name
;
964 ObjectClass
*object_class_by_name(const char *typename
)
966 TypeImpl
*type
= type_get_by_name(typename
);
972 type_initialize(type
);
977 ObjectClass
*object_class_get_parent(ObjectClass
*class)
979 TypeImpl
*type
= type_get_parent(class->type
);
985 type_initialize(type
);
990 typedef struct OCFData
992 void (*fn
)(ObjectClass
*klass
, void *opaque
);
993 const char *implements_type
;
994 bool include_abstract
;
998 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
1001 OCFData
*data
= opaque
;
1002 TypeImpl
*type
= value
;
1005 type_initialize(type
);
1008 if (!data
->include_abstract
&& type
->abstract
) {
1012 if (data
->implements_type
&&
1013 !object_class_dynamic_cast(k
, data
->implements_type
)) {
1017 data
->fn(k
, data
->opaque
);
1020 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1021 const char *implements_type
, bool include_abstract
,
1024 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1026 enumerating_types
= true;
1027 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1028 enumerating_types
= false;
1031 static int do_object_child_foreach(Object
*obj
,
1032 int (*fn
)(Object
*child
, void *opaque
),
1033 void *opaque
, bool recurse
)
1035 GHashTableIter iter
;
1036 ObjectProperty
*prop
;
1039 g_hash_table_iter_init(&iter
, obj
->properties
);
1040 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1041 if (object_property_is_child(prop
)) {
1042 Object
*child
= prop
->opaque
;
1044 ret
= fn(child
, opaque
);
1049 ret
= do_object_child_foreach(child
, fn
, opaque
, true);
1059 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1062 return do_object_child_foreach(obj
, fn
, opaque
, false);
1065 int object_child_foreach_recursive(Object
*obj
,
1066 int (*fn
)(Object
*child
, void *opaque
),
1069 return do_object_child_foreach(obj
, fn
, opaque
, true);
1072 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1074 GSList
**list
= opaque
;
1076 *list
= g_slist_prepend(*list
, klass
);
1079 GSList
*object_class_get_list(const char *implements_type
,
1080 bool include_abstract
)
1082 GSList
*list
= NULL
;
1084 object_class_foreach(object_class_get_list_tramp
,
1085 implements_type
, include_abstract
, &list
);
1089 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1091 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1092 object_class_get_name((ObjectClass
*)b
));
1095 GSList
*object_class_get_list_sorted(const char *implements_type
,
1096 bool include_abstract
)
1098 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1102 Object
*object_ref(Object
*obj
)
1107 atomic_inc(&obj
->ref
);
1111 void object_unref(Object
*obj
)
1116 g_assert(obj
->ref
> 0);
1118 /* parent always holds a reference to its children */
1119 if (atomic_fetch_dec(&obj
->ref
) == 1) {
1120 object_finalize(obj
);
1124 static ObjectProperty
*
1125 object_property_try_add(Object
*obj
, const char *name
, const char *type
,
1126 ObjectPropertyAccessor
*get
,
1127 ObjectPropertyAccessor
*set
,
1128 ObjectPropertyRelease
*release
,
1129 void *opaque
, Error
**errp
)
1131 ObjectProperty
*prop
;
1132 size_t name_len
= strlen(name
);
1134 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1136 ObjectProperty
*ret
;
1137 char *name_no_array
= g_strdup(name
);
1139 name_no_array
[name_len
- 3] = '\0';
1140 for (i
= 0; ; ++i
) {
1141 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1143 ret
= object_property_try_add(obj
, full_name
, type
, get
, set
,
1144 release
, opaque
, NULL
);
1150 g_free(name_no_array
);
1154 if (object_property_find(obj
, name
, NULL
) != NULL
) {
1155 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1156 name
, object_get_typename(obj
));
1160 prop
= g_malloc0(sizeof(*prop
));
1162 prop
->name
= g_strdup(name
);
1163 prop
->type
= g_strdup(type
);
1167 prop
->release
= release
;
1168 prop
->opaque
= opaque
;
1170 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1175 object_property_add(Object
*obj
, const char *name
, const char *type
,
1176 ObjectPropertyAccessor
*get
,
1177 ObjectPropertyAccessor
*set
,
1178 ObjectPropertyRelease
*release
,
1181 return object_property_try_add(obj
, name
, type
, get
, set
, release
,
1182 opaque
, &error_abort
);
1186 object_class_property_add(ObjectClass
*klass
,
1189 ObjectPropertyAccessor
*get
,
1190 ObjectPropertyAccessor
*set
,
1191 ObjectPropertyRelease
*release
,
1194 ObjectProperty
*prop
;
1196 assert(!object_class_property_find(klass
, name
, NULL
));
1198 prop
= g_malloc0(sizeof(*prop
));
1200 prop
->name
= g_strdup(name
);
1201 prop
->type
= g_strdup(type
);
1205 prop
->release
= release
;
1206 prop
->opaque
= opaque
;
1208 g_hash_table_insert(klass
->properties
, prop
->name
, prop
);
1213 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1216 ObjectProperty
*prop
;
1217 ObjectClass
*klass
= object_get_class(obj
);
1219 prop
= object_class_property_find(klass
, name
, NULL
);
1224 prop
= g_hash_table_lookup(obj
->properties
, name
);
1229 error_setg(errp
, "Property '.%s' not found", name
);
1233 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1236 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1237 iter
->nextclass
= object_get_class(obj
);
1240 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1243 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1244 if (!iter
->nextclass
) {
1247 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1248 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1253 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1256 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1257 iter
->nextclass
= object_class_get_parent(klass
);
1260 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1263 ObjectProperty
*prop
;
1264 ObjectClass
*parent_klass
;
1266 parent_klass
= object_class_get_parent(klass
);
1268 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1274 prop
= g_hash_table_lookup(klass
->properties
, name
);
1276 error_setg(errp
, "Property '.%s' not found", name
);
1281 void object_property_del(Object
*obj
, const char *name
)
1283 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1285 if (prop
->release
) {
1286 prop
->release(obj
, name
, prop
->opaque
);
1288 g_hash_table_remove(obj
->properties
, name
);
1291 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1294 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1300 error_setg(errp
, QERR_PERMISSION_DENIED
);
1302 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1306 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1309 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1315 error_setg(errp
, QERR_PERMISSION_DENIED
);
1317 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1321 void object_property_set_str(Object
*obj
, const char *value
,
1322 const char *name
, Error
**errp
)
1324 QString
*qstr
= qstring_from_str(value
);
1325 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1327 qobject_unref(qstr
);
1330 char *object_property_get_str(Object
*obj
, const char *name
,
1333 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1340 retval
= g_strdup(qobject_get_try_str(ret
));
1342 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1349 void object_property_set_link(Object
*obj
, Object
*value
,
1350 const char *name
, Error
**errp
)
1353 char *path
= object_get_canonical_path(value
);
1354 object_property_set_str(obj
, path
, name
, errp
);
1357 object_property_set_str(obj
, "", name
, errp
);
1361 Object
*object_property_get_link(Object
*obj
, const char *name
,
1364 char *str
= object_property_get_str(obj
, name
, errp
);
1365 Object
*target
= NULL
;
1368 target
= object_resolve_path(str
, NULL
);
1370 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1371 "Device '%s' not found", str
);
1379 void object_property_set_bool(Object
*obj
, bool value
,
1380 const char *name
, Error
**errp
)
1382 QBool
*qbool
= qbool_from_bool(value
);
1383 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1385 qobject_unref(qbool
);
1388 bool object_property_get_bool(Object
*obj
, const char *name
,
1391 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1398 qbool
= qobject_to(QBool
, ret
);
1400 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1403 retval
= qbool_get_bool(qbool
);
1410 void object_property_set_int(Object
*obj
, int64_t value
,
1411 const char *name
, Error
**errp
)
1413 QNum
*qnum
= qnum_from_int(value
);
1414 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1416 qobject_unref(qnum
);
1419 int64_t object_property_get_int(Object
*obj
, const char *name
,
1422 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1430 qnum
= qobject_to(QNum
, ret
);
1431 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1432 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1440 static void object_property_init_defval(Object
*obj
, ObjectProperty
*prop
)
1442 Visitor
*v
= qobject_input_visitor_new(prop
->defval
);
1444 assert(prop
->set
!= NULL
);
1445 prop
->set(obj
, v
, prop
->name
, prop
->opaque
, &error_abort
);
1450 static void object_property_set_default(ObjectProperty
*prop
, QObject
*defval
)
1452 assert(!prop
->defval
);
1453 assert(!prop
->init
);
1455 prop
->defval
= defval
;
1456 prop
->init
= object_property_init_defval
;
1459 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
)
1461 object_property_set_default(prop
, QOBJECT(qbool_from_bool(value
)));
1464 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
)
1466 object_property_set_default(prop
, QOBJECT(qstring_from_str(value
)));
1469 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
)
1471 object_property_set_default(prop
, QOBJECT(qnum_from_int(value
)));
1474 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
)
1476 object_property_set_default(prop
, QOBJECT(qnum_from_uint(value
)));
1479 void object_property_set_uint(Object
*obj
, uint64_t value
,
1480 const char *name
, Error
**errp
)
1482 QNum
*qnum
= qnum_from_uint(value
);
1484 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1485 qobject_unref(qnum
);
1488 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1491 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1498 qnum
= qobject_to(QNum
, ret
);
1499 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1500 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1508 typedef struct EnumProperty
{
1509 const QEnumLookup
*lookup
;
1510 int (*get
)(Object
*, Error
**);
1511 void (*set
)(Object
*, int, Error
**);
1514 int object_property_get_enum(Object
*obj
, const char *name
,
1515 const char *typename
, Error
**errp
)
1519 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1520 EnumProperty
*enumprop
;
1526 if (!g_str_equal(prop
->type
, typename
)) {
1527 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1528 name
, object_class_get_name(
1529 object_get_class(obj
)), typename
);
1533 enumprop
= prop
->opaque
;
1535 str
= object_property_get_str(obj
, name
, errp
);
1540 ret
= qapi_enum_parse(enumprop
->lookup
, str
, -1, errp
);
1546 void object_property_parse(Object
*obj
, const char *string
,
1547 const char *name
, Error
**errp
)
1549 Visitor
*v
= string_input_visitor_new(string
);
1550 object_property_set(obj
, v
, name
, errp
);
1554 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1558 char *string
= NULL
;
1559 Error
*local_err
= NULL
;
1561 v
= string_output_visitor_new(human
, &string
);
1562 object_property_get(obj
, v
, name
, &local_err
);
1564 error_propagate(errp
, local_err
);
1568 visit_complete(v
, &string
);
1575 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1577 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1585 Object
*object_get_root(void)
1587 static Object
*root
;
1590 root
= object_new("container");
1596 Object
*object_get_objects_root(void)
1598 return container_get(object_get_root(), "/objects");
1601 Object
*object_get_internal_root(void)
1603 static Object
*internal_root
;
1605 if (!internal_root
) {
1606 internal_root
= object_new("container");
1609 return internal_root
;
1612 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1613 const char *name
, void *opaque
,
1616 Object
*child
= opaque
;
1619 path
= object_get_canonical_path(child
);
1620 visit_type_str(v
, name
, &path
, errp
);
1624 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
,
1630 static void object_finalize_child_property(Object
*obj
, const char *name
,
1633 Object
*child
= opaque
;
1635 if (child
->class->unparent
) {
1636 (child
->class->unparent
)(child
);
1638 child
->parent
= NULL
;
1639 object_unref(child
);
1643 object_property_add_child(Object
*obj
, const char *name
,
1646 g_autofree
char *type
= NULL
;
1649 assert(!child
->parent
);
1651 type
= g_strdup_printf("child<%s>", object_get_typename(child
));
1653 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1654 object_finalize_child_property
, child
);
1655 op
->resolve
= object_resolve_child_property
;
1657 child
->parent
= obj
;
1661 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1662 Object
*val
, Error
**errp
)
1664 /* Allow the link to be set, always */
1670 Object
*target
; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1671 ptrdiff_t offset
; /* if OBJ_PROP_LINK_CLASS */
1673 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1674 ObjectPropertyLinkFlags flags
;
1678 object_link_get_targetp(Object
*obj
, LinkProperty
*lprop
)
1680 if (lprop
->flags
& OBJ_PROP_LINK_DIRECT
) {
1681 return &lprop
->target
;
1682 } else if (lprop
->flags
& OBJ_PROP_LINK_CLASS
) {
1683 return (void *)obj
+ lprop
->offset
;
1685 return lprop
->targetp
;
1689 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1690 const char *name
, void *opaque
,
1693 LinkProperty
*lprop
= opaque
;
1694 Object
**targetp
= object_link_get_targetp(obj
, lprop
);
1698 path
= object_get_canonical_path(*targetp
);
1699 visit_type_str(v
, name
, &path
, errp
);
1703 visit_type_str(v
, name
, &path
, errp
);
1708 * object_resolve_link:
1710 * Lookup an object and ensure its type matches the link property type. This
1711 * is similar to object_resolve_path() except type verification against the
1712 * link property is performed.
1714 * Returns: The matched object or NULL on path lookup failures.
1716 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1717 const char *path
, Error
**errp
)
1721 bool ambiguous
= false;
1724 /* Go from link<FOO> to FOO. */
1725 type
= object_property_get_type(obj
, name
, NULL
);
1726 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1727 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1730 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1732 } else if (!target
) {
1733 target
= object_resolve_path(path
, &ambiguous
);
1734 if (target
|| ambiguous
) {
1735 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1737 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1738 "Device '%s' not found", path
);
1742 g_free(target_type
);
1747 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1748 const char *name
, void *opaque
,
1751 Error
*local_err
= NULL
;
1752 LinkProperty
*prop
= opaque
;
1753 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1754 Object
*old_target
= *targetp
;
1755 Object
*new_target
= NULL
;
1758 visit_type_str(v
, name
, &path
, &local_err
);
1760 if (!local_err
&& strcmp(path
, "") != 0) {
1761 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1766 error_propagate(errp
, local_err
);
1770 prop
->check(obj
, name
, new_target
, &local_err
);
1772 error_propagate(errp
, local_err
);
1776 *targetp
= new_target
;
1777 if (prop
->flags
& OBJ_PROP_LINK_STRONG
) {
1778 object_ref(new_target
);
1779 object_unref(old_target
);
1783 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
,
1786 LinkProperty
*lprop
= opaque
;
1788 return *object_link_get_targetp(parent
, lprop
);
1791 static void object_release_link_property(Object
*obj
, const char *name
,
1794 LinkProperty
*prop
= opaque
;
1795 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1797 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *targetp
) {
1798 object_unref(*targetp
);
1800 if (!(prop
->flags
& OBJ_PROP_LINK_CLASS
)) {
1805 static ObjectProperty
*
1806 object_add_link_prop(Object
*obj
, const char *name
,
1807 const char *type
, void *ptr
,
1808 void (*check
)(const Object
*, const char *,
1809 Object
*, Error
**),
1810 ObjectPropertyLinkFlags flags
)
1812 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1813 g_autofree
char *full_type
= NULL
;
1816 if (flags
& OBJ_PROP_LINK_DIRECT
) {
1819 prop
->targetp
= ptr
;
1821 prop
->check
= check
;
1822 prop
->flags
= flags
;
1824 full_type
= g_strdup_printf("link<%s>", type
);
1826 op
= object_property_add(obj
, name
, full_type
,
1827 object_get_link_property
,
1828 check
? object_set_link_property
: NULL
,
1829 object_release_link_property
,
1831 op
->resolve
= object_resolve_link_property
;
1836 object_property_add_link(Object
*obj
, const char *name
,
1837 const char *type
, Object
**targetp
,
1838 void (*check
)(const Object
*, const char *,
1839 Object
*, Error
**),
1840 ObjectPropertyLinkFlags flags
)
1842 return object_add_link_prop(obj
, name
, type
, targetp
, check
, flags
);
1846 object_class_property_add_link(ObjectClass
*oc
,
1848 const char *type
, ptrdiff_t offset
,
1849 void (*check
)(const Object
*obj
, const char *name
,
1850 Object
*val
, Error
**errp
),
1851 ObjectPropertyLinkFlags flags
)
1853 LinkProperty
*prop
= g_new0(LinkProperty
, 1);
1857 prop
->offset
= offset
;
1858 prop
->check
= check
;
1859 prop
->flags
= flags
| OBJ_PROP_LINK_CLASS
;
1861 full_type
= g_strdup_printf("link<%s>", type
);
1863 op
= object_class_property_add(oc
, name
, full_type
,
1864 object_get_link_property
,
1865 check
? object_set_link_property
: NULL
,
1866 object_release_link_property
,
1869 op
->resolve
= object_resolve_link_property
;
1876 object_property_add_const_link(Object
*obj
, const char *name
,
1879 return object_add_link_prop(obj
, name
,
1880 object_get_typename(target
), target
,
1881 NULL
, OBJ_PROP_LINK_DIRECT
);
1884 char *object_get_canonical_path_component(const Object
*obj
)
1886 ObjectProperty
*prop
= NULL
;
1887 GHashTableIter iter
;
1889 if (obj
->parent
== NULL
) {
1893 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1894 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1895 if (!object_property_is_child(prop
)) {
1899 if (prop
->opaque
== obj
) {
1900 return g_strdup(prop
->name
);
1904 /* obj had a parent but was not a child, should never happen */
1905 g_assert_not_reached();
1909 char *object_get_canonical_path(const Object
*obj
)
1911 Object
*root
= object_get_root();
1912 char *newpath
, *path
= NULL
;
1915 return g_strdup("/");
1919 char *component
= object_get_canonical_path_component(obj
);
1922 /* A canonical path must be complete, so discard what was
1929 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
1934 } while (obj
!= root
);
1939 Object
*object_resolve_path_component(Object
*parent
, const char *part
)
1941 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1946 if (prop
->resolve
) {
1947 return prop
->resolve(parent
, prop
->opaque
, part
);
1953 static Object
*object_resolve_abs_path(Object
*parent
,
1955 const char *typename
)
1959 if (*parts
== NULL
) {
1960 return object_dynamic_cast(parent
, typename
);
1963 if (strcmp(*parts
, "") == 0) {
1964 return object_resolve_abs_path(parent
, parts
+ 1, typename
);
1967 child
= object_resolve_path_component(parent
, *parts
);
1972 return object_resolve_abs_path(child
, parts
+ 1, typename
);
1975 static Object
*object_resolve_partial_path(Object
*parent
,
1977 const char *typename
,
1981 GHashTableIter iter
;
1982 ObjectProperty
*prop
;
1984 obj
= object_resolve_abs_path(parent
, parts
, typename
);
1986 g_hash_table_iter_init(&iter
, parent
->properties
);
1987 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1990 if (!object_property_is_child(prop
)) {
1994 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1995 typename
, ambiguous
);
2012 Object
*object_resolve_path_type(const char *path
, const char *typename
,
2018 parts
= g_strsplit(path
, "/", 0);
2021 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
2022 bool ambiguous
= false;
2023 obj
= object_resolve_partial_path(object_get_root(), parts
,
2024 typename
, &ambiguous
);
2026 *ambiguousp
= ambiguous
;
2029 obj
= object_resolve_abs_path(object_get_root(), parts
+ 1, typename
);
2037 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
2039 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
2042 typedef struct StringProperty
2044 char *(*get
)(Object
*, Error
**);
2045 void (*set
)(Object
*, const char *, Error
**);
2048 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
2049 void *opaque
, Error
**errp
)
2051 StringProperty
*prop
= opaque
;
2055 value
= prop
->get(obj
, &err
);
2057 error_propagate(errp
, err
);
2061 visit_type_str(v
, name
, &value
, errp
);
2065 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
2066 void *opaque
, Error
**errp
)
2068 StringProperty
*prop
= opaque
;
2070 Error
*local_err
= NULL
;
2072 visit_type_str(v
, name
, &value
, &local_err
);
2074 error_propagate(errp
, local_err
);
2078 prop
->set(obj
, value
, errp
);
2082 static void property_release_str(Object
*obj
, const char *name
,
2085 StringProperty
*prop
= opaque
;
2090 object_property_add_str(Object
*obj
, const char *name
,
2091 char *(*get
)(Object
*, Error
**),
2092 void (*set
)(Object
*, const char *, Error
**))
2094 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2099 return object_property_add(obj
, name
, "string",
2100 get
? property_get_str
: NULL
,
2101 set
? property_set_str
: NULL
,
2102 property_release_str
,
2107 object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2108 char *(*get
)(Object
*, Error
**),
2109 void (*set
)(Object
*, const char *,
2112 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2117 return object_class_property_add(klass
, name
, "string",
2118 get
? property_get_str
: NULL
,
2119 set
? property_set_str
: NULL
,
2124 typedef struct BoolProperty
2126 bool (*get
)(Object
*, Error
**);
2127 void (*set
)(Object
*, bool, Error
**);
2130 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2131 void *opaque
, Error
**errp
)
2133 BoolProperty
*prop
= opaque
;
2137 value
= prop
->get(obj
, &err
);
2139 error_propagate(errp
, err
);
2143 visit_type_bool(v
, name
, &value
, errp
);
2146 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2147 void *opaque
, Error
**errp
)
2149 BoolProperty
*prop
= opaque
;
2151 Error
*local_err
= NULL
;
2153 visit_type_bool(v
, name
, &value
, &local_err
);
2155 error_propagate(errp
, local_err
);
2159 prop
->set(obj
, value
, errp
);
2162 static void property_release_bool(Object
*obj
, const char *name
,
2165 BoolProperty
*prop
= opaque
;
2170 object_property_add_bool(Object
*obj
, const char *name
,
2171 bool (*get
)(Object
*, Error
**),
2172 void (*set
)(Object
*, bool, Error
**))
2174 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2179 return object_property_add(obj
, name
, "bool",
2180 get
? property_get_bool
: NULL
,
2181 set
? property_set_bool
: NULL
,
2182 property_release_bool
,
2187 object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2188 bool (*get
)(Object
*, Error
**),
2189 void (*set
)(Object
*, bool, Error
**))
2191 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2196 return object_class_property_add(klass
, name
, "bool",
2197 get
? property_get_bool
: NULL
,
2198 set
? property_set_bool
: NULL
,
2203 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2204 void *opaque
, Error
**errp
)
2206 EnumProperty
*prop
= opaque
;
2210 value
= prop
->get(obj
, &err
);
2212 error_propagate(errp
, err
);
2216 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2219 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2220 void *opaque
, Error
**errp
)
2222 EnumProperty
*prop
= opaque
;
2226 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
2228 error_propagate(errp
, err
);
2231 prop
->set(obj
, value
, errp
);
2234 static void property_release_enum(Object
*obj
, const char *name
,
2237 EnumProperty
*prop
= opaque
;
2242 object_property_add_enum(Object
*obj
, const char *name
,
2243 const char *typename
,
2244 const QEnumLookup
*lookup
,
2245 int (*get
)(Object
*, Error
**),
2246 void (*set
)(Object
*, int, Error
**))
2248 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2250 prop
->lookup
= lookup
;
2254 return object_property_add(obj
, name
, typename
,
2255 get
? property_get_enum
: NULL
,
2256 set
? property_set_enum
: NULL
,
2257 property_release_enum
,
2262 object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2263 const char *typename
,
2264 const QEnumLookup
*lookup
,
2265 int (*get
)(Object
*, Error
**),
2266 void (*set
)(Object
*, int, Error
**))
2268 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2270 prop
->lookup
= lookup
;
2274 return object_class_property_add(klass
, name
, typename
,
2275 get
? property_get_enum
: NULL
,
2276 set
? property_set_enum
: NULL
,
2281 typedef struct TMProperty
{
2282 void (*get
)(Object
*, struct tm
*, Error
**);
2285 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2286 void *opaque
, Error
**errp
)
2288 TMProperty
*prop
= opaque
;
2292 prop
->get(obj
, &value
, &err
);
2297 visit_start_struct(v
, name
, NULL
, 0, &err
);
2301 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2305 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2309 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2313 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2317 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2321 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2325 visit_check_struct(v
, &err
);
2327 visit_end_struct(v
, NULL
);
2329 error_propagate(errp
, err
);
2333 static void property_release_tm(Object
*obj
, const char *name
,
2336 TMProperty
*prop
= opaque
;
2341 object_property_add_tm(Object
*obj
, const char *name
,
2342 void (*get
)(Object
*, struct tm
*, Error
**))
2344 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2348 return object_property_add(obj
, name
, "struct tm",
2349 get
? property_get_tm
: NULL
, NULL
,
2350 property_release_tm
,
2355 object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2356 void (*get
)(Object
*, struct tm
*, Error
**))
2358 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2362 return object_class_property_add(klass
, name
, "struct tm",
2363 get
? property_get_tm
: NULL
,
2367 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2369 return g_strdup(object_get_typename(obj
));
2372 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2373 void *opaque
, Error
**errp
)
2375 uint8_t value
= *(uint8_t *)opaque
;
2376 visit_type_uint8(v
, name
, &value
, errp
);
2379 static void property_set_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2380 void *opaque
, Error
**errp
)
2382 uint8_t *field
= opaque
;
2384 Error
*local_err
= NULL
;
2386 visit_type_uint8(v
, name
, &value
, &local_err
);
2388 error_propagate(errp
, local_err
);
2395 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2396 void *opaque
, Error
**errp
)
2398 uint16_t value
= *(uint16_t *)opaque
;
2399 visit_type_uint16(v
, name
, &value
, errp
);
2402 static void property_set_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2403 void *opaque
, Error
**errp
)
2405 uint16_t *field
= opaque
;
2407 Error
*local_err
= NULL
;
2409 visit_type_uint16(v
, name
, &value
, &local_err
);
2411 error_propagate(errp
, local_err
);
2418 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2419 void *opaque
, Error
**errp
)
2421 uint32_t value
= *(uint32_t *)opaque
;
2422 visit_type_uint32(v
, name
, &value
, errp
);
2425 static void property_set_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2426 void *opaque
, Error
**errp
)
2428 uint32_t *field
= opaque
;
2430 Error
*local_err
= NULL
;
2432 visit_type_uint32(v
, name
, &value
, &local_err
);
2434 error_propagate(errp
, local_err
);
2441 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2442 void *opaque
, Error
**errp
)
2444 uint64_t value
= *(uint64_t *)opaque
;
2445 visit_type_uint64(v
, name
, &value
, errp
);
2448 static void property_set_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2449 void *opaque
, Error
**errp
)
2451 uint64_t *field
= opaque
;
2453 Error
*local_err
= NULL
;
2455 visit_type_uint64(v
, name
, &value
, &local_err
);
2457 error_propagate(errp
, local_err
);
2465 object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2467 ObjectPropertyFlags flags
)
2469 ObjectPropertyAccessor
*getter
= NULL
;
2470 ObjectPropertyAccessor
*setter
= NULL
;
2472 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2473 getter
= property_get_uint8_ptr
;
2476 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2477 setter
= property_set_uint8_ptr
;
2480 return object_property_add(obj
, name
, "uint8",
2481 getter
, setter
, NULL
, (void *)v
);
2485 object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2487 ObjectPropertyFlags flags
)
2489 ObjectPropertyAccessor
*getter
= NULL
;
2490 ObjectPropertyAccessor
*setter
= NULL
;
2492 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2493 getter
= property_get_uint8_ptr
;
2496 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2497 setter
= property_set_uint8_ptr
;
2500 return object_class_property_add(klass
, name
, "uint8",
2501 getter
, setter
, NULL
, (void *)v
);
2505 object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2507 ObjectPropertyFlags flags
)
2509 ObjectPropertyAccessor
*getter
= NULL
;
2510 ObjectPropertyAccessor
*setter
= NULL
;
2512 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2513 getter
= property_get_uint16_ptr
;
2516 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2517 setter
= property_set_uint16_ptr
;
2520 return object_property_add(obj
, name
, "uint16",
2521 getter
, setter
, NULL
, (void *)v
);
2525 object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2527 ObjectPropertyFlags flags
)
2529 ObjectPropertyAccessor
*getter
= NULL
;
2530 ObjectPropertyAccessor
*setter
= NULL
;
2532 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2533 getter
= property_get_uint16_ptr
;
2536 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2537 setter
= property_set_uint16_ptr
;
2540 return object_class_property_add(klass
, name
, "uint16",
2541 getter
, setter
, NULL
, (void *)v
);
2545 object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2547 ObjectPropertyFlags flags
)
2549 ObjectPropertyAccessor
*getter
= NULL
;
2550 ObjectPropertyAccessor
*setter
= NULL
;
2552 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2553 getter
= property_get_uint32_ptr
;
2556 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2557 setter
= property_set_uint32_ptr
;
2560 return object_property_add(obj
, name
, "uint32",
2561 getter
, setter
, NULL
, (void *)v
);
2565 object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2567 ObjectPropertyFlags flags
)
2569 ObjectPropertyAccessor
*getter
= NULL
;
2570 ObjectPropertyAccessor
*setter
= NULL
;
2572 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2573 getter
= property_get_uint32_ptr
;
2576 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2577 setter
= property_set_uint32_ptr
;
2580 return object_class_property_add(klass
, name
, "uint32",
2581 getter
, setter
, NULL
, (void *)v
);
2585 object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2587 ObjectPropertyFlags flags
)
2589 ObjectPropertyAccessor
*getter
= NULL
;
2590 ObjectPropertyAccessor
*setter
= NULL
;
2592 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2593 getter
= property_get_uint64_ptr
;
2596 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2597 setter
= property_set_uint64_ptr
;
2600 return object_property_add(obj
, name
, "uint64",
2601 getter
, setter
, NULL
, (void *)v
);
2605 object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2607 ObjectPropertyFlags flags
)
2609 ObjectPropertyAccessor
*getter
= NULL
;
2610 ObjectPropertyAccessor
*setter
= NULL
;
2612 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2613 getter
= property_get_uint64_ptr
;
2616 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2617 setter
= property_set_uint64_ptr
;
2620 return object_class_property_add(klass
, name
, "uint64",
2621 getter
, setter
, NULL
, (void *)v
);
2629 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2630 void *opaque
, Error
**errp
)
2632 AliasProperty
*prop
= opaque
;
2634 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2637 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2638 void *opaque
, Error
**errp
)
2640 AliasProperty
*prop
= opaque
;
2642 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2645 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2648 AliasProperty
*prop
= opaque
;
2650 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2653 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2655 AliasProperty
*prop
= opaque
;
2657 g_free(prop
->target_name
);
2662 object_property_add_alias(Object
*obj
, const char *name
,
2663 Object
*target_obj
, const char *target_name
)
2665 AliasProperty
*prop
;
2667 ObjectProperty
*target_prop
;
2668 g_autofree
char *prop_type
= NULL
;
2670 target_prop
= object_property_find(target_obj
, target_name
,
2673 if (object_property_is_child(target_prop
)) {
2674 prop_type
= g_strdup_printf("link%s",
2675 target_prop
->type
+ strlen("child"));
2677 prop_type
= g_strdup(target_prop
->type
);
2680 prop
= g_malloc(sizeof(*prop
));
2681 prop
->target_obj
= target_obj
;
2682 prop
->target_name
= g_strdup(target_name
);
2684 op
= object_property_add(obj
, name
, prop_type
,
2687 property_release_alias
,
2689 op
->resolve
= property_resolve_alias
;
2690 if (target_prop
->defval
) {
2691 op
->defval
= qobject_ref(target_prop
->defval
);
2694 object_property_set_description(obj
, op
->name
,
2695 target_prop
->description
);
2699 void object_property_set_description(Object
*obj
, const char *name
,
2700 const char *description
)
2704 op
= object_property_find(obj
, name
, &error_abort
);
2705 g_free(op
->description
);
2706 op
->description
= g_strdup(description
);
2709 void object_class_property_set_description(ObjectClass
*klass
,
2711 const char *description
)
2715 op
= g_hash_table_lookup(klass
->properties
, name
);
2716 g_free(op
->description
);
2717 op
->description
= g_strdup(description
);
2720 static void object_class_init(ObjectClass
*klass
, void *data
)
2722 object_class_property_add_str(klass
, "type", qdev_get_type
,
2726 static void register_types(void)
2728 static TypeInfo interface_info
= {
2729 .name
= TYPE_INTERFACE
,
2730 .class_size
= sizeof(InterfaceClass
),
2734 static TypeInfo object_info
= {
2735 .name
= TYPE_OBJECT
,
2736 .instance_size
= sizeof(Object
),
2737 .class_init
= object_class_init
,
2741 type_interface
= type_register_internal(&interface_info
);
2742 type_register_internal(&object_info
);
2745 type_init(register_types
)