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
,
269 static void object_property_free(gpointer data
)
271 ObjectProperty
*prop
= data
;
274 qobject_unref(prop
->defval
);
279 g_free(prop
->description
);
283 static void type_initialize(TypeImpl
*ti
)
291 ti
->class_size
= type_class_get_size(ti
);
292 ti
->instance_size
= type_object_get_size(ti
);
293 /* Any type with zero instance_size is implicitly abstract.
294 * This means interface types are all abstract.
296 if (ti
->instance_size
== 0) {
299 if (type_is_ancestor(ti
, type_interface
)) {
300 assert(ti
->instance_size
== 0);
301 assert(ti
->abstract
);
302 assert(!ti
->instance_init
);
303 assert(!ti
->instance_post_init
);
304 assert(!ti
->instance_finalize
);
305 assert(!ti
->num_interfaces
);
307 ti
->class = g_malloc0(ti
->class_size
);
309 parent
= type_get_parent(ti
);
311 type_initialize(parent
);
315 g_assert(parent
->class_size
<= ti
->class_size
);
316 g_assert(parent
->instance_size
<= ti
->instance_size
);
317 memcpy(ti
->class, parent
->class, parent
->class_size
);
318 ti
->class->interfaces
= NULL
;
319 ti
->class->properties
= g_hash_table_new_full(
320 g_str_hash
, g_str_equal
, NULL
, object_property_free
);
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
);
351 ti
->class->properties
= g_hash_table_new_full(
352 g_str_hash
, g_str_equal
, NULL
, 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 void object_apply_global_props(Object
*obj
, const GPtrArray
*props
, Error
**errp
)
399 for (i
= 0; i
< props
->len
; i
++) {
400 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
403 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
406 if (p
->optional
&& !object_property_find(obj
, p
->property
, NULL
)) {
410 object_property_parse(obj
, p
->value
, p
->property
, &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
);
430 * Global property defaults
431 * Slot 0: accelerator's global property defaults
432 * Slot 1: machine's global property defaults
433 * Slot 2: global properties from legacy command line option
434 * Each is a GPtrArray of of GlobalProperty.
435 * Applied in order, later entries override earlier ones.
437 static GPtrArray
*object_compat_props
[3];
440 * Retrieve @GPtrArray for global property defined with options
441 * other than "-global". These are generally used for syntactic
442 * sugar and legacy command line options.
444 void object_register_sugar_prop(const char *driver
, const char *prop
, const char *value
)
447 if (!object_compat_props
[2]) {
448 object_compat_props
[2] = g_ptr_array_new();
450 g
= g_new0(GlobalProperty
, 1);
451 g
->driver
= g_strdup(driver
);
452 g
->property
= g_strdup(prop
);
453 g
->value
= g_strdup(value
);
454 g_ptr_array_add(object_compat_props
[2], g
);
458 * Set machine's global property defaults to @compat_props.
459 * May be called at most once.
461 void object_set_machine_compat_props(GPtrArray
*compat_props
)
463 assert(!object_compat_props
[1]);
464 object_compat_props
[1] = compat_props
;
468 * Set accelerator's global property defaults to @compat_props.
469 * May be called at most once.
471 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
473 assert(!object_compat_props
[0]);
474 object_compat_props
[0] = compat_props
;
477 void object_apply_compat_props(Object
*obj
)
481 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
482 object_apply_global_props(obj
, object_compat_props
[i
],
483 i
== 2 ? &error_fatal
: &error_abort
);
487 static void object_class_property_init_all(Object
*obj
)
489 ObjectPropertyIterator iter
;
490 ObjectProperty
*prop
;
492 object_class_property_iter_init(&iter
, object_get_class(obj
));
493 while ((prop
= object_property_iter_next(&iter
))) {
495 prop
->init(obj
, prop
);
500 static void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
504 type_initialize(type
);
506 g_assert(type
->instance_size
>= sizeof(Object
));
507 g_assert(type
->abstract
== false);
508 g_assert(size
>= type
->instance_size
);
510 memset(obj
, 0, type
->instance_size
);
511 obj
->class = type
->class;
513 object_class_property_init_all(obj
);
514 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
515 NULL
, object_property_free
);
516 object_init_with_type(obj
, type
);
517 object_post_init_with_type(obj
, type
);
520 void object_initialize(void *data
, size_t size
, const char *typename
)
522 TypeImpl
*type
= type_get_by_name(typename
);
525 error_report("missing object type '%s'", typename
);
529 object_initialize_with_type(data
, size
, type
);
532 void object_initialize_child(Object
*parentobj
, const char *propname
,
533 void *childobj
, size_t size
, const char *type
,
538 va_start(vargs
, errp
);
539 object_initialize_childv(parentobj
, propname
, childobj
, size
, type
, errp
,
544 void object_initialize_childv(Object
*parentobj
, const char *propname
,
545 void *childobj
, size_t size
, const char *type
,
546 Error
**errp
, va_list vargs
)
548 Error
*local_err
= NULL
;
552 object_initialize(childobj
, size
, type
);
553 obj
= OBJECT(childobj
);
555 object_set_propv(obj
, &local_err
, vargs
);
560 object_property_add_child(parentobj
, propname
, obj
);
562 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
564 user_creatable_complete(uc
, &local_err
);
566 object_unparent(obj
);
573 * We want @obj's reference to be 1 on success, 0 on failure.
574 * On success, it's 2: one taken by object_initialize(), and one
575 * by object_property_add_child().
576 * On failure in object_initialize() or earlier, it's 1.
577 * On failure afterwards, it's also 1: object_unparent() releases
578 * the reference taken by object_property_add_child().
582 error_propagate(errp
, local_err
);
585 static inline bool object_property_is_child(ObjectProperty
*prop
)
587 return strstart(prop
->type
, "child<", NULL
);
590 static void object_property_del_all(Object
*obj
)
592 g_autoptr(GHashTable
) done
= g_hash_table_new(NULL
, NULL
);
593 ObjectProperty
*prop
;
594 ObjectPropertyIterator iter
;
599 object_property_iter_init(&iter
, obj
);
600 while ((prop
= object_property_iter_next(&iter
)) != NULL
) {
601 if (g_hash_table_add(done
, prop
)) {
603 prop
->release(obj
, prop
->name
, prop
->opaque
);
611 g_hash_table_unref(obj
->properties
);
614 static void object_property_del_child(Object
*obj
, Object
*child
)
616 ObjectProperty
*prop
;
620 g_hash_table_iter_init(&iter
, obj
->properties
);
621 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
623 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
625 prop
->release(obj
, prop
->name
, prop
->opaque
);
626 prop
->release
= NULL
;
631 g_hash_table_iter_init(&iter
, obj
->properties
);
632 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
634 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
635 g_hash_table_iter_remove(&iter
);
641 void object_unparent(Object
*obj
)
644 object_property_del_child(obj
->parent
, obj
);
648 static void object_deinit(Object
*obj
, TypeImpl
*type
)
650 if (type
->instance_finalize
) {
651 type
->instance_finalize(obj
);
654 if (type_has_parent(type
)) {
655 object_deinit(obj
, type_get_parent(type
));
659 static void object_finalize(void *data
)
662 TypeImpl
*ti
= obj
->class->type
;
664 object_property_del_all(obj
);
665 object_deinit(obj
, ti
);
667 g_assert(obj
->ref
== 0);
673 static Object
*object_new_with_type(Type type
)
677 g_assert(type
!= NULL
);
678 type_initialize(type
);
680 obj
= g_malloc(type
->instance_size
);
681 object_initialize_with_type(obj
, type
->instance_size
, type
);
687 Object
*object_new_with_class(ObjectClass
*klass
)
689 return object_new_with_type(klass
->type
);
692 Object
*object_new(const char *typename
)
694 TypeImpl
*ti
= type_get_by_name(typename
);
696 return object_new_with_type(ti
);
700 Object
*object_new_with_props(const char *typename
,
709 va_start(vargs
, errp
);
710 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
717 Object
*object_new_with_propv(const char *typename
,
725 Error
*local_err
= NULL
;
728 klass
= object_class_by_name(typename
);
730 error_setg(errp
, "invalid object type: %s", typename
);
734 if (object_class_is_abstract(klass
)) {
735 error_setg(errp
, "object type '%s' is abstract", typename
);
738 obj
= object_new_with_type(klass
->type
);
740 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
745 object_property_add_child(parent
, id
, obj
);
748 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
750 user_creatable_complete(uc
, &local_err
);
753 object_unparent(obj
);
763 error_propagate(errp
, local_err
);
769 int object_set_props(Object
*obj
,
776 va_start(vargs
, errp
);
777 ret
= object_set_propv(obj
, errp
, vargs
);
784 int object_set_propv(Object
*obj
,
788 const char *propname
;
789 Error
*local_err
= NULL
;
791 propname
= va_arg(vargs
, char *);
792 while (propname
!= NULL
) {
793 const char *value
= va_arg(vargs
, char *);
795 g_assert(value
!= NULL
);
796 object_property_parse(obj
, value
, propname
, &local_err
);
798 error_propagate(errp
, local_err
);
801 propname
= va_arg(vargs
, char *);
808 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
810 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
817 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
818 const char *file
, int line
, const char *func
)
820 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
821 typename
, file
, line
, func
);
823 #ifdef CONFIG_QOM_CAST_DEBUG
827 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
828 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
833 inst
= object_dynamic_cast(obj
, typename
);
836 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
837 file
, line
, func
, obj
, typename
);
843 if (obj
&& obj
== inst
) {
844 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
845 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
846 atomic_read(&obj
->class->object_cast_cache
[i
]));
848 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
856 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
857 const char *typename
)
859 ObjectClass
*ret
= NULL
;
860 TypeImpl
*target_type
;
867 /* A simple fast path that can trigger a lot for leaf classes. */
869 if (type
->name
== typename
) {
873 target_type
= type_get_by_name(typename
);
875 /* target class type unknown, so fail the cast */
879 if (type
->class->interfaces
&&
880 type_is_ancestor(target_type
, type_interface
)) {
884 for (i
= class->interfaces
; i
; i
= i
->next
) {
885 ObjectClass
*target_class
= i
->data
;
887 if (type_is_ancestor(target_class
->type
, target_type
)) {
893 /* The match was ambiguous, don't allow a cast */
897 } else if (type_is_ancestor(type
, target_type
)) {
904 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
905 const char *typename
,
906 const char *file
, int line
,
911 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
912 typename
, file
, line
, func
);
914 #ifdef CONFIG_QOM_CAST_DEBUG
917 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
918 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
924 if (!class || !class->interfaces
) {
929 ret
= object_class_dynamic_cast(class, typename
);
931 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
932 file
, line
, func
, class, typename
);
936 #ifdef CONFIG_QOM_CAST_DEBUG
937 if (class && ret
== class) {
938 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
939 atomic_set(&class->class_cast_cache
[i
- 1],
940 atomic_read(&class->class_cast_cache
[i
]));
942 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
949 const char *object_get_typename(const Object
*obj
)
951 return obj
->class->type
->name
;
954 ObjectClass
*object_get_class(Object
*obj
)
959 bool object_class_is_abstract(ObjectClass
*klass
)
961 return klass
->type
->abstract
;
964 const char *object_class_get_name(ObjectClass
*klass
)
966 return klass
->type
->name
;
969 ObjectClass
*object_class_by_name(const char *typename
)
971 TypeImpl
*type
= type_get_by_name(typename
);
977 type_initialize(type
);
982 ObjectClass
*object_class_get_parent(ObjectClass
*class)
984 TypeImpl
*type
= type_get_parent(class->type
);
990 type_initialize(type
);
995 typedef struct OCFData
997 void (*fn
)(ObjectClass
*klass
, void *opaque
);
998 const char *implements_type
;
999 bool include_abstract
;
1003 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
1006 OCFData
*data
= opaque
;
1007 TypeImpl
*type
= value
;
1010 type_initialize(type
);
1013 if (!data
->include_abstract
&& type
->abstract
) {
1017 if (data
->implements_type
&&
1018 !object_class_dynamic_cast(k
, data
->implements_type
)) {
1022 data
->fn(k
, data
->opaque
);
1025 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1026 const char *implements_type
, bool include_abstract
,
1029 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1031 enumerating_types
= true;
1032 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1033 enumerating_types
= false;
1036 static int do_object_child_foreach(Object
*obj
,
1037 int (*fn
)(Object
*child
, void *opaque
),
1038 void *opaque
, bool recurse
)
1040 GHashTableIter iter
;
1041 ObjectProperty
*prop
;
1044 g_hash_table_iter_init(&iter
, obj
->properties
);
1045 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1046 if (object_property_is_child(prop
)) {
1047 Object
*child
= prop
->opaque
;
1049 ret
= fn(child
, opaque
);
1054 do_object_child_foreach(child
, fn
, opaque
, true);
1061 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1064 return do_object_child_foreach(obj
, fn
, opaque
, false);
1067 int object_child_foreach_recursive(Object
*obj
,
1068 int (*fn
)(Object
*child
, void *opaque
),
1071 return do_object_child_foreach(obj
, fn
, opaque
, true);
1074 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1076 GSList
**list
= opaque
;
1078 *list
= g_slist_prepend(*list
, klass
);
1081 GSList
*object_class_get_list(const char *implements_type
,
1082 bool include_abstract
)
1084 GSList
*list
= NULL
;
1086 object_class_foreach(object_class_get_list_tramp
,
1087 implements_type
, include_abstract
, &list
);
1091 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1093 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1094 object_class_get_name((ObjectClass
*)b
));
1097 GSList
*object_class_get_list_sorted(const char *implements_type
,
1098 bool include_abstract
)
1100 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1104 Object
*object_ref(Object
*obj
)
1109 atomic_inc(&obj
->ref
);
1113 void object_unref(Object
*obj
)
1118 g_assert(obj
->ref
> 0);
1120 /* parent always holds a reference to its children */
1121 if (atomic_fetch_dec(&obj
->ref
) == 1) {
1122 object_finalize(obj
);
1126 static ObjectProperty
*
1127 object_property_try_add(Object
*obj
, const char *name
, const char *type
,
1128 ObjectPropertyAccessor
*get
,
1129 ObjectPropertyAccessor
*set
,
1130 ObjectPropertyRelease
*release
,
1131 void *opaque
, Error
**errp
)
1133 ObjectProperty
*prop
;
1134 size_t name_len
= strlen(name
);
1136 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1138 ObjectProperty
*ret
;
1139 char *name_no_array
= g_strdup(name
);
1141 name_no_array
[name_len
- 3] = '\0';
1142 for (i
= 0; ; ++i
) {
1143 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1145 ret
= object_property_try_add(obj
, full_name
, type
, get
, set
,
1146 release
, opaque
, NULL
);
1152 g_free(name_no_array
);
1156 if (object_property_find(obj
, name
, NULL
) != NULL
) {
1157 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1158 name
, object_get_typename(obj
));
1162 prop
= g_malloc0(sizeof(*prop
));
1164 prop
->name
= g_strdup(name
);
1165 prop
->type
= g_strdup(type
);
1169 prop
->release
= release
;
1170 prop
->opaque
= opaque
;
1172 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1177 object_property_add(Object
*obj
, const char *name
, const char *type
,
1178 ObjectPropertyAccessor
*get
,
1179 ObjectPropertyAccessor
*set
,
1180 ObjectPropertyRelease
*release
,
1183 return object_property_try_add(obj
, name
, type
, get
, set
, release
,
1184 opaque
, &error_abort
);
1188 object_class_property_add(ObjectClass
*klass
,
1191 ObjectPropertyAccessor
*get
,
1192 ObjectPropertyAccessor
*set
,
1193 ObjectPropertyRelease
*release
,
1196 ObjectProperty
*prop
;
1198 assert(!object_class_property_find(klass
, name
, NULL
));
1200 prop
= g_malloc0(sizeof(*prop
));
1202 prop
->name
= g_strdup(name
);
1203 prop
->type
= g_strdup(type
);
1207 prop
->release
= release
;
1208 prop
->opaque
= opaque
;
1210 g_hash_table_insert(klass
->properties
, prop
->name
, prop
);
1215 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1218 ObjectProperty
*prop
;
1219 ObjectClass
*klass
= object_get_class(obj
);
1221 prop
= object_class_property_find(klass
, name
, NULL
);
1226 prop
= g_hash_table_lookup(obj
->properties
, name
);
1231 error_setg(errp
, "Property '.%s' not found", name
);
1235 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1238 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1239 iter
->nextclass
= object_get_class(obj
);
1242 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1245 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1246 if (!iter
->nextclass
) {
1249 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1250 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1255 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1258 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1259 iter
->nextclass
= object_class_get_parent(klass
);
1262 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1265 ObjectProperty
*prop
;
1266 ObjectClass
*parent_klass
;
1268 parent_klass
= object_class_get_parent(klass
);
1270 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1276 prop
= g_hash_table_lookup(klass
->properties
, name
);
1278 error_setg(errp
, "Property '.%s' not found", name
);
1283 void object_property_del(Object
*obj
, const char *name
)
1285 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1287 if (prop
->release
) {
1288 prop
->release(obj
, name
, prop
->opaque
);
1290 g_hash_table_remove(obj
->properties
, name
);
1293 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1296 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1302 error_setg(errp
, QERR_PERMISSION_DENIED
);
1304 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1308 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1311 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1317 error_setg(errp
, QERR_PERMISSION_DENIED
);
1319 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1323 void object_property_set_str(Object
*obj
, const char *value
,
1324 const char *name
, Error
**errp
)
1326 QString
*qstr
= qstring_from_str(value
);
1327 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1329 qobject_unref(qstr
);
1332 char *object_property_get_str(Object
*obj
, const char *name
,
1335 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1342 retval
= g_strdup(qobject_get_try_str(ret
));
1344 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1351 void object_property_set_link(Object
*obj
, Object
*value
,
1352 const char *name
, Error
**errp
)
1355 char *path
= object_get_canonical_path(value
);
1356 object_property_set_str(obj
, path
, name
, errp
);
1359 object_property_set_str(obj
, "", name
, errp
);
1363 Object
*object_property_get_link(Object
*obj
, const char *name
,
1366 char *str
= object_property_get_str(obj
, name
, errp
);
1367 Object
*target
= NULL
;
1370 target
= object_resolve_path(str
, NULL
);
1372 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1373 "Device '%s' not found", str
);
1381 void object_property_set_bool(Object
*obj
, bool value
,
1382 const char *name
, Error
**errp
)
1384 QBool
*qbool
= qbool_from_bool(value
);
1385 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1387 qobject_unref(qbool
);
1390 bool object_property_get_bool(Object
*obj
, const char *name
,
1393 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1400 qbool
= qobject_to(QBool
, ret
);
1402 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1405 retval
= qbool_get_bool(qbool
);
1412 void object_property_set_int(Object
*obj
, int64_t value
,
1413 const char *name
, Error
**errp
)
1415 QNum
*qnum
= qnum_from_int(value
);
1416 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1418 qobject_unref(qnum
);
1421 int64_t object_property_get_int(Object
*obj
, const char *name
,
1424 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1432 qnum
= qobject_to(QNum
, ret
);
1433 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1434 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1442 static void object_property_init_defval(Object
*obj
, ObjectProperty
*prop
)
1444 Visitor
*v
= qobject_input_visitor_new(prop
->defval
);
1446 assert(prop
->set
!= NULL
);
1447 prop
->set(obj
, v
, prop
->name
, prop
->opaque
, &error_abort
);
1452 static void object_property_set_default(ObjectProperty
*prop
, QObject
*defval
)
1454 assert(!prop
->defval
);
1455 assert(!prop
->init
);
1457 prop
->defval
= defval
;
1458 prop
->init
= object_property_init_defval
;
1461 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
)
1463 object_property_set_default(prop
, QOBJECT(qbool_from_bool(value
)));
1466 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
)
1468 object_property_set_default(prop
, QOBJECT(qstring_from_str(value
)));
1471 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
)
1473 object_property_set_default(prop
, QOBJECT(qnum_from_int(value
)));
1476 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
)
1478 object_property_set_default(prop
, QOBJECT(qnum_from_uint(value
)));
1481 void object_property_set_uint(Object
*obj
, uint64_t value
,
1482 const char *name
, Error
**errp
)
1484 QNum
*qnum
= qnum_from_uint(value
);
1486 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1487 qobject_unref(qnum
);
1490 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1493 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1500 qnum
= qobject_to(QNum
, ret
);
1501 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1502 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1510 typedef struct EnumProperty
{
1511 const QEnumLookup
*lookup
;
1512 int (*get
)(Object
*, Error
**);
1513 void (*set
)(Object
*, int, Error
**);
1516 int object_property_get_enum(Object
*obj
, const char *name
,
1517 const char *typename
, Error
**errp
)
1521 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1522 EnumProperty
*enumprop
;
1528 if (!g_str_equal(prop
->type
, typename
)) {
1529 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1530 name
, object_class_get_name(
1531 object_get_class(obj
)), typename
);
1535 enumprop
= prop
->opaque
;
1537 str
= object_property_get_str(obj
, name
, errp
);
1542 ret
= qapi_enum_parse(enumprop
->lookup
, str
, -1, errp
);
1548 void object_property_parse(Object
*obj
, const char *string
,
1549 const char *name
, Error
**errp
)
1551 Visitor
*v
= string_input_visitor_new(string
);
1552 object_property_set(obj
, v
, name
, errp
);
1556 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1560 char *string
= NULL
;
1561 Error
*local_err
= NULL
;
1563 v
= string_output_visitor_new(human
, &string
);
1564 object_property_get(obj
, v
, name
, &local_err
);
1566 error_propagate(errp
, local_err
);
1570 visit_complete(v
, &string
);
1577 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1579 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1587 Object
*object_get_root(void)
1589 static Object
*root
;
1592 root
= object_new("container");
1598 Object
*object_get_objects_root(void)
1600 return container_get(object_get_root(), "/objects");
1603 Object
*object_get_internal_root(void)
1605 static Object
*internal_root
;
1607 if (!internal_root
) {
1608 internal_root
= object_new("container");
1611 return internal_root
;
1614 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1615 const char *name
, void *opaque
,
1618 Object
*child
= opaque
;
1621 path
= object_get_canonical_path(child
);
1622 visit_type_str(v
, name
, &path
, errp
);
1626 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
,
1632 static void object_finalize_child_property(Object
*obj
, const char *name
,
1635 Object
*child
= opaque
;
1637 if (child
->class->unparent
) {
1638 (child
->class->unparent
)(child
);
1640 child
->parent
= NULL
;
1641 object_unref(child
);
1645 object_property_add_child(Object
*obj
, const char *name
,
1648 g_autofree
char *type
= NULL
;
1651 assert(!child
->parent
);
1653 type
= g_strdup_printf("child<%s>", object_get_typename(child
));
1655 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1656 object_finalize_child_property
, child
);
1657 op
->resolve
= object_resolve_child_property
;
1659 child
->parent
= obj
;
1663 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1664 Object
*val
, Error
**errp
)
1666 /* Allow the link to be set, always */
1672 Object
*target
; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1673 ptrdiff_t offset
; /* if OBJ_PROP_LINK_CLASS */
1675 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1676 ObjectPropertyLinkFlags flags
;
1680 object_link_get_targetp(Object
*obj
, LinkProperty
*lprop
)
1682 if (lprop
->flags
& OBJ_PROP_LINK_DIRECT
) {
1683 return &lprop
->target
;
1684 } else if (lprop
->flags
& OBJ_PROP_LINK_CLASS
) {
1685 return (void *)obj
+ lprop
->offset
;
1687 return lprop
->targetp
;
1691 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1692 const char *name
, void *opaque
,
1695 LinkProperty
*lprop
= opaque
;
1696 Object
**targetp
= object_link_get_targetp(obj
, lprop
);
1700 path
= object_get_canonical_path(*targetp
);
1701 visit_type_str(v
, name
, &path
, errp
);
1705 visit_type_str(v
, name
, &path
, errp
);
1710 * object_resolve_link:
1712 * Lookup an object and ensure its type matches the link property type. This
1713 * is similar to object_resolve_path() except type verification against the
1714 * link property is performed.
1716 * Returns: The matched object or NULL on path lookup failures.
1718 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1719 const char *path
, Error
**errp
)
1723 bool ambiguous
= false;
1726 /* Go from link<FOO> to FOO. */
1727 type
= object_property_get_type(obj
, name
, NULL
);
1728 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1729 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1732 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1734 } else if (!target
) {
1735 target
= object_resolve_path(path
, &ambiguous
);
1736 if (target
|| ambiguous
) {
1737 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1739 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1740 "Device '%s' not found", path
);
1744 g_free(target_type
);
1749 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1750 const char *name
, void *opaque
,
1753 Error
*local_err
= NULL
;
1754 LinkProperty
*prop
= opaque
;
1755 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1756 Object
*old_target
= *targetp
;
1757 Object
*new_target
= NULL
;
1760 visit_type_str(v
, name
, &path
, &local_err
);
1762 if (!local_err
&& strcmp(path
, "") != 0) {
1763 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1768 error_propagate(errp
, local_err
);
1772 prop
->check(obj
, name
, new_target
, &local_err
);
1774 error_propagate(errp
, local_err
);
1778 *targetp
= new_target
;
1779 if (prop
->flags
& OBJ_PROP_LINK_STRONG
) {
1780 object_ref(new_target
);
1781 object_unref(old_target
);
1785 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
,
1788 LinkProperty
*lprop
= opaque
;
1790 return *object_link_get_targetp(parent
, lprop
);
1793 static void object_release_link_property(Object
*obj
, const char *name
,
1796 LinkProperty
*prop
= opaque
;
1797 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1799 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *targetp
) {
1800 object_unref(*targetp
);
1802 if (!(prop
->flags
& OBJ_PROP_LINK_CLASS
)) {
1807 static ObjectProperty
*
1808 object_add_link_prop(Object
*obj
, const char *name
,
1809 const char *type
, void *ptr
,
1810 void (*check
)(const Object
*, const char *,
1811 Object
*, Error
**),
1812 ObjectPropertyLinkFlags flags
)
1814 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1815 g_autofree
char *full_type
= NULL
;
1818 if (flags
& OBJ_PROP_LINK_DIRECT
) {
1821 prop
->targetp
= ptr
;
1823 prop
->check
= check
;
1824 prop
->flags
= flags
;
1826 full_type
= g_strdup_printf("link<%s>", type
);
1828 op
= object_property_add(obj
, name
, full_type
,
1829 object_get_link_property
,
1830 check
? object_set_link_property
: NULL
,
1831 object_release_link_property
,
1833 op
->resolve
= object_resolve_link_property
;
1838 object_property_add_link(Object
*obj
, const char *name
,
1839 const char *type
, Object
**targetp
,
1840 void (*check
)(const Object
*, const char *,
1841 Object
*, Error
**),
1842 ObjectPropertyLinkFlags flags
)
1844 return object_add_link_prop(obj
, name
, type
, targetp
, check
, flags
);
1848 object_class_property_add_link(ObjectClass
*oc
,
1850 const char *type
, ptrdiff_t offset
,
1851 void (*check
)(const Object
*obj
, const char *name
,
1852 Object
*val
, Error
**errp
),
1853 ObjectPropertyLinkFlags flags
)
1855 LinkProperty
*prop
= g_new0(LinkProperty
, 1);
1859 prop
->offset
= offset
;
1860 prop
->check
= check
;
1861 prop
->flags
= flags
| OBJ_PROP_LINK_CLASS
;
1863 full_type
= g_strdup_printf("link<%s>", type
);
1865 op
= object_class_property_add(oc
, name
, full_type
,
1866 object_get_link_property
,
1867 check
? object_set_link_property
: NULL
,
1868 object_release_link_property
,
1871 op
->resolve
= object_resolve_link_property
;
1878 object_property_add_const_link(Object
*obj
, const char *name
,
1881 return object_add_link_prop(obj
, name
,
1882 object_get_typename(target
), target
,
1883 NULL
, OBJ_PROP_LINK_DIRECT
);
1886 char *object_get_canonical_path_component(Object
*obj
)
1888 ObjectProperty
*prop
= NULL
;
1889 GHashTableIter iter
;
1891 if (obj
->parent
== NULL
) {
1895 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1896 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1897 if (!object_property_is_child(prop
)) {
1901 if (prop
->opaque
== obj
) {
1902 return g_strdup(prop
->name
);
1906 /* obj had a parent but was not a child, should never happen */
1907 g_assert_not_reached();
1911 char *object_get_canonical_path(Object
*obj
)
1913 Object
*root
= object_get_root();
1914 char *newpath
, *path
= NULL
;
1917 return g_strdup("/");
1921 char *component
= object_get_canonical_path_component(obj
);
1924 /* A canonical path must be complete, so discard what was
1931 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
1936 } while (obj
!= root
);
1941 Object
*object_resolve_path_component(Object
*parent
, const char *part
)
1943 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1948 if (prop
->resolve
) {
1949 return prop
->resolve(parent
, prop
->opaque
, part
);
1955 static Object
*object_resolve_abs_path(Object
*parent
,
1957 const char *typename
,
1962 if (parts
[index
] == NULL
) {
1963 return object_dynamic_cast(parent
, typename
);
1966 if (strcmp(parts
[index
], "") == 0) {
1967 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1970 child
= object_resolve_path_component(parent
, parts
[index
]);
1975 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1978 static Object
*object_resolve_partial_path(Object
*parent
,
1980 const char *typename
,
1984 GHashTableIter iter
;
1985 ObjectProperty
*prop
;
1987 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1989 g_hash_table_iter_init(&iter
, parent
->properties
);
1990 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1993 if (!object_property_is_child(prop
)) {
1997 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1998 typename
, ambiguous
);
2015 Object
*object_resolve_path_type(const char *path
, const char *typename
,
2021 parts
= g_strsplit(path
, "/", 0);
2024 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
2025 bool ambiguous
= false;
2026 obj
= object_resolve_partial_path(object_get_root(), parts
,
2027 typename
, &ambiguous
);
2029 *ambiguousp
= ambiguous
;
2032 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
2040 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
2042 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
2045 typedef struct StringProperty
2047 char *(*get
)(Object
*, Error
**);
2048 void (*set
)(Object
*, const char *, Error
**);
2051 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
2052 void *opaque
, Error
**errp
)
2054 StringProperty
*prop
= opaque
;
2058 value
= prop
->get(obj
, &err
);
2060 error_propagate(errp
, err
);
2064 visit_type_str(v
, name
, &value
, errp
);
2068 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
2069 void *opaque
, Error
**errp
)
2071 StringProperty
*prop
= opaque
;
2073 Error
*local_err
= NULL
;
2075 visit_type_str(v
, name
, &value
, &local_err
);
2077 error_propagate(errp
, local_err
);
2081 prop
->set(obj
, value
, errp
);
2085 static void property_release_str(Object
*obj
, const char *name
,
2088 StringProperty
*prop
= opaque
;
2093 object_property_add_str(Object
*obj
, const char *name
,
2094 char *(*get
)(Object
*, Error
**),
2095 void (*set
)(Object
*, const char *, Error
**))
2097 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2102 return object_property_add(obj
, name
, "string",
2103 get
? property_get_str
: NULL
,
2104 set
? property_set_str
: NULL
,
2105 property_release_str
,
2110 object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2111 char *(*get
)(Object
*, Error
**),
2112 void (*set
)(Object
*, const char *,
2115 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2120 return object_class_property_add(klass
, name
, "string",
2121 get
? property_get_str
: NULL
,
2122 set
? property_set_str
: NULL
,
2127 typedef struct BoolProperty
2129 bool (*get
)(Object
*, Error
**);
2130 void (*set
)(Object
*, bool, Error
**);
2133 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2134 void *opaque
, Error
**errp
)
2136 BoolProperty
*prop
= opaque
;
2140 value
= prop
->get(obj
, &err
);
2142 error_propagate(errp
, err
);
2146 visit_type_bool(v
, name
, &value
, errp
);
2149 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2150 void *opaque
, Error
**errp
)
2152 BoolProperty
*prop
= opaque
;
2154 Error
*local_err
= NULL
;
2156 visit_type_bool(v
, name
, &value
, &local_err
);
2158 error_propagate(errp
, local_err
);
2162 prop
->set(obj
, value
, errp
);
2165 static void property_release_bool(Object
*obj
, const char *name
,
2168 BoolProperty
*prop
= opaque
;
2173 object_property_add_bool(Object
*obj
, const char *name
,
2174 bool (*get
)(Object
*, Error
**),
2175 void (*set
)(Object
*, bool, Error
**))
2177 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2182 return object_property_add(obj
, name
, "bool",
2183 get
? property_get_bool
: NULL
,
2184 set
? property_set_bool
: NULL
,
2185 property_release_bool
,
2190 object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2191 bool (*get
)(Object
*, Error
**),
2192 void (*set
)(Object
*, bool, Error
**))
2194 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2199 return object_class_property_add(klass
, name
, "bool",
2200 get
? property_get_bool
: NULL
,
2201 set
? property_set_bool
: NULL
,
2206 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2207 void *opaque
, Error
**errp
)
2209 EnumProperty
*prop
= opaque
;
2213 value
= prop
->get(obj
, &err
);
2215 error_propagate(errp
, err
);
2219 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2222 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2223 void *opaque
, Error
**errp
)
2225 EnumProperty
*prop
= opaque
;
2229 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
2231 error_propagate(errp
, err
);
2234 prop
->set(obj
, value
, errp
);
2237 static void property_release_enum(Object
*obj
, const char *name
,
2240 EnumProperty
*prop
= opaque
;
2245 object_property_add_enum(Object
*obj
, const char *name
,
2246 const char *typename
,
2247 const QEnumLookup
*lookup
,
2248 int (*get
)(Object
*, Error
**),
2249 void (*set
)(Object
*, int, Error
**))
2251 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2253 prop
->lookup
= lookup
;
2257 return object_property_add(obj
, name
, typename
,
2258 get
? property_get_enum
: NULL
,
2259 set
? property_set_enum
: NULL
,
2260 property_release_enum
,
2265 object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2266 const char *typename
,
2267 const QEnumLookup
*lookup
,
2268 int (*get
)(Object
*, Error
**),
2269 void (*set
)(Object
*, int, Error
**))
2271 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2273 prop
->lookup
= lookup
;
2277 return object_class_property_add(klass
, name
, typename
,
2278 get
? property_get_enum
: NULL
,
2279 set
? property_set_enum
: NULL
,
2284 typedef struct TMProperty
{
2285 void (*get
)(Object
*, struct tm
*, Error
**);
2288 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2289 void *opaque
, Error
**errp
)
2291 TMProperty
*prop
= opaque
;
2295 prop
->get(obj
, &value
, &err
);
2300 visit_start_struct(v
, name
, NULL
, 0, &err
);
2304 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2308 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2312 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2316 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2320 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2324 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2328 visit_check_struct(v
, &err
);
2330 visit_end_struct(v
, NULL
);
2332 error_propagate(errp
, err
);
2336 static void property_release_tm(Object
*obj
, const char *name
,
2339 TMProperty
*prop
= opaque
;
2344 object_property_add_tm(Object
*obj
, const char *name
,
2345 void (*get
)(Object
*, struct tm
*, Error
**))
2347 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2351 return object_property_add(obj
, name
, "struct tm",
2352 get
? property_get_tm
: NULL
, NULL
,
2353 property_release_tm
,
2358 object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2359 void (*get
)(Object
*, struct tm
*, Error
**))
2361 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2365 return object_class_property_add(klass
, name
, "struct tm",
2366 get
? property_get_tm
: NULL
,
2370 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2372 return g_strdup(object_get_typename(obj
));
2375 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2376 void *opaque
, Error
**errp
)
2378 uint8_t value
= *(uint8_t *)opaque
;
2379 visit_type_uint8(v
, name
, &value
, errp
);
2382 static void property_set_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2383 void *opaque
, Error
**errp
)
2385 uint8_t *field
= opaque
;
2387 Error
*local_err
= NULL
;
2389 visit_type_uint8(v
, name
, &value
, &local_err
);
2391 error_propagate(errp
, local_err
);
2398 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2399 void *opaque
, Error
**errp
)
2401 uint16_t value
= *(uint16_t *)opaque
;
2402 visit_type_uint16(v
, name
, &value
, errp
);
2405 static void property_set_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2406 void *opaque
, Error
**errp
)
2408 uint16_t *field
= opaque
;
2410 Error
*local_err
= NULL
;
2412 visit_type_uint16(v
, name
, &value
, &local_err
);
2414 error_propagate(errp
, local_err
);
2421 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2422 void *opaque
, Error
**errp
)
2424 uint32_t value
= *(uint32_t *)opaque
;
2425 visit_type_uint32(v
, name
, &value
, errp
);
2428 static void property_set_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2429 void *opaque
, Error
**errp
)
2431 uint32_t *field
= opaque
;
2433 Error
*local_err
= NULL
;
2435 visit_type_uint32(v
, name
, &value
, &local_err
);
2437 error_propagate(errp
, local_err
);
2444 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2445 void *opaque
, Error
**errp
)
2447 uint64_t value
= *(uint64_t *)opaque
;
2448 visit_type_uint64(v
, name
, &value
, errp
);
2451 static void property_set_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2452 void *opaque
, Error
**errp
)
2454 uint64_t *field
= opaque
;
2456 Error
*local_err
= NULL
;
2458 visit_type_uint64(v
, name
, &value
, &local_err
);
2460 error_propagate(errp
, local_err
);
2468 object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2470 ObjectPropertyFlags flags
)
2472 ObjectPropertyAccessor
*getter
= NULL
;
2473 ObjectPropertyAccessor
*setter
= NULL
;
2475 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2476 getter
= property_get_uint8_ptr
;
2479 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2480 setter
= property_set_uint8_ptr
;
2483 return object_property_add(obj
, name
, "uint8",
2484 getter
, setter
, NULL
, (void *)v
);
2488 object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2490 ObjectPropertyFlags flags
)
2492 ObjectPropertyAccessor
*getter
= NULL
;
2493 ObjectPropertyAccessor
*setter
= NULL
;
2495 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2496 getter
= property_get_uint8_ptr
;
2499 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2500 setter
= property_set_uint8_ptr
;
2503 return object_class_property_add(klass
, name
, "uint8",
2504 getter
, setter
, NULL
, (void *)v
);
2508 object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2510 ObjectPropertyFlags flags
)
2512 ObjectPropertyAccessor
*getter
= NULL
;
2513 ObjectPropertyAccessor
*setter
= NULL
;
2515 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2516 getter
= property_get_uint16_ptr
;
2519 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2520 setter
= property_set_uint16_ptr
;
2523 return object_property_add(obj
, name
, "uint16",
2524 getter
, setter
, NULL
, (void *)v
);
2528 object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2530 ObjectPropertyFlags flags
)
2532 ObjectPropertyAccessor
*getter
= NULL
;
2533 ObjectPropertyAccessor
*setter
= NULL
;
2535 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2536 getter
= property_get_uint16_ptr
;
2539 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2540 setter
= property_set_uint16_ptr
;
2543 return object_class_property_add(klass
, name
, "uint16",
2544 getter
, setter
, NULL
, (void *)v
);
2548 object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2550 ObjectPropertyFlags flags
)
2552 ObjectPropertyAccessor
*getter
= NULL
;
2553 ObjectPropertyAccessor
*setter
= NULL
;
2555 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2556 getter
= property_get_uint32_ptr
;
2559 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2560 setter
= property_set_uint32_ptr
;
2563 return object_property_add(obj
, name
, "uint32",
2564 getter
, setter
, NULL
, (void *)v
);
2568 object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2570 ObjectPropertyFlags flags
)
2572 ObjectPropertyAccessor
*getter
= NULL
;
2573 ObjectPropertyAccessor
*setter
= NULL
;
2575 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2576 getter
= property_get_uint32_ptr
;
2579 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2580 setter
= property_set_uint32_ptr
;
2583 return object_class_property_add(klass
, name
, "uint32",
2584 getter
, setter
, NULL
, (void *)v
);
2588 object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2590 ObjectPropertyFlags flags
)
2592 ObjectPropertyAccessor
*getter
= NULL
;
2593 ObjectPropertyAccessor
*setter
= NULL
;
2595 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2596 getter
= property_get_uint64_ptr
;
2599 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2600 setter
= property_set_uint64_ptr
;
2603 return object_property_add(obj
, name
, "uint64",
2604 getter
, setter
, NULL
, (void *)v
);
2608 object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2610 ObjectPropertyFlags flags
)
2612 ObjectPropertyAccessor
*getter
= NULL
;
2613 ObjectPropertyAccessor
*setter
= NULL
;
2615 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2616 getter
= property_get_uint64_ptr
;
2619 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2620 setter
= property_set_uint64_ptr
;
2623 return object_class_property_add(klass
, name
, "uint64",
2624 getter
, setter
, NULL
, (void *)v
);
2632 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2633 void *opaque
, Error
**errp
)
2635 AliasProperty
*prop
= opaque
;
2637 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2640 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2641 void *opaque
, Error
**errp
)
2643 AliasProperty
*prop
= opaque
;
2645 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2648 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2651 AliasProperty
*prop
= opaque
;
2653 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2656 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2658 AliasProperty
*prop
= opaque
;
2660 g_free(prop
->target_name
);
2665 object_property_add_alias(Object
*obj
, const char *name
,
2666 Object
*target_obj
, const char *target_name
)
2668 AliasProperty
*prop
;
2670 ObjectProperty
*target_prop
;
2671 g_autofree
char *prop_type
= NULL
;
2673 target_prop
= object_property_find(target_obj
, target_name
,
2676 if (object_property_is_child(target_prop
)) {
2677 prop_type
= g_strdup_printf("link%s",
2678 target_prop
->type
+ strlen("child"));
2680 prop_type
= g_strdup(target_prop
->type
);
2683 prop
= g_malloc(sizeof(*prop
));
2684 prop
->target_obj
= target_obj
;
2685 prop
->target_name
= g_strdup(target_name
);
2687 op
= object_property_add(obj
, name
, prop_type
,
2690 property_release_alias
,
2692 op
->resolve
= property_resolve_alias
;
2693 if (target_prop
->defval
) {
2694 op
->defval
= qobject_ref(target_prop
->defval
);
2697 object_property_set_description(obj
, op
->name
,
2698 target_prop
->description
);
2702 void object_property_set_description(Object
*obj
, const char *name
,
2703 const char *description
)
2707 op
= object_property_find(obj
, name
, &error_abort
);
2708 g_free(op
->description
);
2709 op
->description
= g_strdup(description
);
2712 void object_class_property_set_description(ObjectClass
*klass
,
2714 const char *description
)
2718 op
= g_hash_table_lookup(klass
->properties
, name
);
2719 g_free(op
->description
);
2720 op
->description
= g_strdup(description
);
2723 static void object_class_init(ObjectClass
*klass
, void *data
)
2725 object_class_property_add_str(klass
, "type", qdev_get_type
,
2729 static void register_types(void)
2731 static TypeInfo interface_info
= {
2732 .name
= TYPE_INTERFACE
,
2733 .class_size
= sizeof(InterfaceClass
),
2737 static TypeInfo object_info
= {
2738 .name
= TYPE_OBJECT
,
2739 .instance_size
= sizeof(Object
),
2740 .class_init
= object_class_init
,
2744 type_interface
= type_register_internal(&interface_info
);
2745 type_register_internal(&object_info
);
2748 type_init(register_types
)