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 "qom/object.h"
14 #include "qom/object_interfaces.h"
15 #include "qemu-common.h"
16 #include "qapi/visitor.h"
17 #include "qapi-visit.h"
18 #include "qapi/string-input-visitor.h"
19 #include "qapi/string-output-visitor.h"
20 #include "qapi/qmp/qerror.h"
23 /* TODO: replace QObject with a simpler visitor to avoid a dependency
24 * of the QOM core on QObject? */
25 #include "qom/qom-qobject.h"
26 #include "qapi/qmp/qobject.h"
27 #include "qapi/qmp/qbool.h"
28 #include "qapi/qmp/qint.h"
29 #include "qapi/qmp/qstring.h"
31 #define MAX_INTERFACES 32
33 typedef struct InterfaceImpl InterfaceImpl
;
34 typedef struct TypeImpl TypeImpl
;
49 void (*class_init
)(ObjectClass
*klass
, void *data
);
50 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
51 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
55 void (*instance_init
)(Object
*obj
);
56 void (*instance_post_init
)(Object
*obj
);
57 void (*instance_finalize
)(Object
*obj
);
62 TypeImpl
*parent_type
;
67 InterfaceImpl interfaces
[MAX_INTERFACES
];
70 static Type type_interface
;
72 static GHashTable
*type_table_get(void)
74 static GHashTable
*type_table
;
76 if (type_table
== NULL
) {
77 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
83 static bool enumerating_types
;
85 static void type_table_add(TypeImpl
*ti
)
87 assert(!enumerating_types
);
88 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
91 static TypeImpl
*type_table_lookup(const char *name
)
93 return g_hash_table_lookup(type_table_get(), name
);
96 static TypeImpl
*type_new(const TypeInfo
*info
)
98 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
101 g_assert(info
->name
!= NULL
);
103 if (type_table_lookup(info
->name
) != NULL
) {
104 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
108 ti
->name
= g_strdup(info
->name
);
109 ti
->parent
= g_strdup(info
->parent
);
111 ti
->class_size
= info
->class_size
;
112 ti
->instance_size
= info
->instance_size
;
114 ti
->class_init
= info
->class_init
;
115 ti
->class_base_init
= info
->class_base_init
;
116 ti
->class_finalize
= info
->class_finalize
;
117 ti
->class_data
= info
->class_data
;
119 ti
->instance_init
= info
->instance_init
;
120 ti
->instance_post_init
= info
->instance_post_init
;
121 ti
->instance_finalize
= info
->instance_finalize
;
123 ti
->abstract
= info
->abstract
;
125 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
126 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
128 ti
->num_interfaces
= i
;
133 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
142 TypeImpl
*type_register(const TypeInfo
*info
)
144 assert(info
->parent
);
145 return type_register_internal(info
);
148 TypeImpl
*type_register_static(const TypeInfo
*info
)
150 return type_register(info
);
153 static TypeImpl
*type_get_by_name(const char *name
)
159 return type_table_lookup(name
);
162 static TypeImpl
*type_get_parent(TypeImpl
*type
)
164 if (!type
->parent_type
&& type
->parent
) {
165 type
->parent_type
= type_get_by_name(type
->parent
);
166 g_assert(type
->parent_type
!= NULL
);
169 return type
->parent_type
;
172 static bool type_has_parent(TypeImpl
*type
)
174 return (type
->parent
!= NULL
);
177 static size_t type_class_get_size(TypeImpl
*ti
)
179 if (ti
->class_size
) {
180 return ti
->class_size
;
183 if (type_has_parent(ti
)) {
184 return type_class_get_size(type_get_parent(ti
));
187 return sizeof(ObjectClass
);
190 static size_t type_object_get_size(TypeImpl
*ti
)
192 if (ti
->instance_size
) {
193 return ti
->instance_size
;
196 if (type_has_parent(ti
)) {
197 return type_object_get_size(type_get_parent(ti
));
203 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
207 /* Check if typename is a direct ancestor of type */
209 if (type
== target_type
) {
213 type
= type_get_parent(type
);
219 static void type_initialize(TypeImpl
*ti
);
221 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
222 TypeImpl
*parent_type
)
224 InterfaceClass
*new_iface
;
226 TypeImpl
*iface_impl
;
228 info
.parent
= parent_type
->name
;
229 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
230 info
.abstract
= true;
232 iface_impl
= type_new(&info
);
233 iface_impl
->parent_type
= parent_type
;
234 type_initialize(iface_impl
);
235 g_free((char *)info
.name
);
237 new_iface
= (InterfaceClass
*)iface_impl
->class;
238 new_iface
->concrete_class
= ti
->class;
239 new_iface
->interface_type
= interface_type
;
241 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
245 static void type_initialize(TypeImpl
*ti
)
253 ti
->class_size
= type_class_get_size(ti
);
254 ti
->instance_size
= type_object_get_size(ti
);
256 ti
->class = g_malloc0(ti
->class_size
);
258 parent
= type_get_parent(ti
);
260 type_initialize(parent
);
264 g_assert(parent
->class_size
<= ti
->class_size
);
265 memcpy(ti
->class, parent
->class, parent
->class_size
);
266 ti
->class->interfaces
= NULL
;
268 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
269 InterfaceClass
*iface
= e
->data
;
270 ObjectClass
*klass
= OBJECT_CLASS(iface
);
272 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
275 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
276 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
277 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
278 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
280 if (type_is_ancestor(target_type
, t
)) {
289 type_initialize_interface(ti
, t
, t
);
293 ti
->class->type
= ti
;
296 if (parent
->class_base_init
) {
297 parent
->class_base_init(ti
->class, ti
->class_data
);
299 parent
= type_get_parent(parent
);
302 if (ti
->class_init
) {
303 ti
->class_init(ti
->class, ti
->class_data
);
307 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
309 if (type_has_parent(ti
)) {
310 object_init_with_type(obj
, type_get_parent(ti
));
313 if (ti
->instance_init
) {
314 ti
->instance_init(obj
);
318 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
320 if (ti
->instance_post_init
) {
321 ti
->instance_post_init(obj
);
324 if (type_has_parent(ti
)) {
325 object_post_init_with_type(obj
, type_get_parent(ti
));
329 void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
333 g_assert(type
!= NULL
);
334 type_initialize(type
);
336 g_assert(type
->instance_size
>= sizeof(Object
));
337 g_assert(type
->abstract
== false);
338 g_assert(size
>= type
->instance_size
);
340 memset(obj
, 0, type
->instance_size
);
341 obj
->class = type
->class;
343 QTAILQ_INIT(&obj
->properties
);
344 object_init_with_type(obj
, type
);
345 object_post_init_with_type(obj
, type
);
348 void object_initialize(void *data
, size_t size
, const char *typename
)
350 TypeImpl
*type
= type_get_by_name(typename
);
352 object_initialize_with_type(data
, size
, type
);
355 static inline bool object_property_is_child(ObjectProperty
*prop
)
357 return strstart(prop
->type
, "child<", NULL
);
360 static void object_property_del_all(Object
*obj
)
362 while (!QTAILQ_EMPTY(&obj
->properties
)) {
363 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
365 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
368 prop
->release(obj
, prop
->name
, prop
->opaque
);
373 g_free(prop
->description
);
378 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
380 ObjectProperty
*prop
;
382 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
383 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
384 object_property_del(obj
, prop
->name
, errp
);
390 void object_unparent(Object
*obj
)
393 object_property_del_child(obj
->parent
, obj
, NULL
);
397 static void object_deinit(Object
*obj
, TypeImpl
*type
)
399 if (type
->instance_finalize
) {
400 type
->instance_finalize(obj
);
403 if (type_has_parent(type
)) {
404 object_deinit(obj
, type_get_parent(type
));
408 static void object_finalize(void *data
)
411 TypeImpl
*ti
= obj
->class->type
;
413 object_property_del_all(obj
);
414 object_deinit(obj
, ti
);
416 g_assert(obj
->ref
== 0);
422 Object
*object_new_with_type(Type type
)
426 g_assert(type
!= NULL
);
427 type_initialize(type
);
429 obj
= g_malloc(type
->instance_size
);
430 object_initialize_with_type(obj
, type
->instance_size
, type
);
436 Object
*object_new(const char *typename
)
438 TypeImpl
*ti
= type_get_by_name(typename
);
440 return object_new_with_type(ti
);
444 Object
*object_new_with_props(const char *typename
,
453 va_start(vargs
, errp
);
454 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
461 Object
*object_new_with_propv(const char *typename
,
469 Error
*local_err
= NULL
;
471 klass
= object_class_by_name(typename
);
473 error_setg(errp
, "invalid object type: %s", typename
);
477 if (object_class_is_abstract(klass
)) {
478 error_setg(errp
, "object type '%s' is abstract", typename
);
481 obj
= object_new(typename
);
483 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
487 object_property_add_child(parent
, id
, obj
, &local_err
);
492 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
493 user_creatable_complete(obj
, &local_err
);
495 object_unparent(obj
);
500 object_unref(OBJECT(obj
));
505 error_propagate(errp
, local_err
);
512 int object_set_props(Object
*obj
,
519 va_start(vargs
, errp
);
520 ret
= object_set_propv(obj
, errp
, vargs
);
527 int object_set_propv(Object
*obj
,
531 const char *propname
;
532 Error
*local_err
= NULL
;
534 propname
= va_arg(vargs
, char *);
535 while (propname
!= NULL
) {
536 const char *value
= va_arg(vargs
, char *);
538 g_assert(value
!= NULL
);
539 object_property_parse(obj
, value
, propname
, &local_err
);
541 error_propagate(errp
, local_err
);
544 propname
= va_arg(vargs
, char *);
551 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
553 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
560 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
561 const char *file
, int line
, const char *func
)
563 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
564 typename
, file
, line
, func
);
566 #ifdef CONFIG_QOM_CAST_DEBUG
570 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
571 if (obj
->class->object_cast_cache
[i
] == typename
) {
576 inst
= object_dynamic_cast(obj
, typename
);
579 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
580 file
, line
, func
, obj
, typename
);
586 if (obj
&& obj
== inst
) {
587 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
588 obj
->class->object_cast_cache
[i
- 1] =
589 obj
->class->object_cast_cache
[i
];
591 obj
->class->object_cast_cache
[i
- 1] = typename
;
599 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
600 const char *typename
)
602 ObjectClass
*ret
= NULL
;
603 TypeImpl
*target_type
;
610 /* A simple fast path that can trigger a lot for leaf classes. */
612 if (type
->name
== typename
) {
616 target_type
= type_get_by_name(typename
);
618 /* target class type unknown, so fail the cast */
622 if (type
->class->interfaces
&&
623 type_is_ancestor(target_type
, type_interface
)) {
627 for (i
= class->interfaces
; i
; i
= i
->next
) {
628 ObjectClass
*target_class
= i
->data
;
630 if (type_is_ancestor(target_class
->type
, target_type
)) {
636 /* The match was ambiguous, don't allow a cast */
640 } else if (type_is_ancestor(type
, target_type
)) {
647 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
648 const char *typename
,
649 const char *file
, int line
,
654 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
655 typename
, file
, line
, func
);
657 #ifdef CONFIG_QOM_CAST_DEBUG
660 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
661 if (class->class_cast_cache
[i
] == typename
) {
667 if (!class || !class->interfaces
) {
672 ret
= object_class_dynamic_cast(class, typename
);
674 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
675 file
, line
, func
, class, typename
);
679 #ifdef CONFIG_QOM_CAST_DEBUG
680 if (class && ret
== class) {
681 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
682 class->class_cast_cache
[i
- 1] = class->class_cast_cache
[i
];
684 class->class_cast_cache
[i
- 1] = typename
;
691 const char *object_get_typename(Object
*obj
)
693 return obj
->class->type
->name
;
696 ObjectClass
*object_get_class(Object
*obj
)
701 bool object_class_is_abstract(ObjectClass
*klass
)
703 return klass
->type
->abstract
;
706 const char *object_class_get_name(ObjectClass
*klass
)
708 return klass
->type
->name
;
711 ObjectClass
*object_class_by_name(const char *typename
)
713 TypeImpl
*type
= type_get_by_name(typename
);
719 type_initialize(type
);
724 ObjectClass
*object_class_get_parent(ObjectClass
*class)
726 TypeImpl
*type
= type_get_parent(class->type
);
732 type_initialize(type
);
737 typedef struct OCFData
739 void (*fn
)(ObjectClass
*klass
, void *opaque
);
740 const char *implements_type
;
741 bool include_abstract
;
745 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
748 OCFData
*data
= opaque
;
749 TypeImpl
*type
= value
;
752 type_initialize(type
);
755 if (!data
->include_abstract
&& type
->abstract
) {
759 if (data
->implements_type
&&
760 !object_class_dynamic_cast(k
, data
->implements_type
)) {
764 data
->fn(k
, data
->opaque
);
767 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
768 const char *implements_type
, bool include_abstract
,
771 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
773 enumerating_types
= true;
774 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
775 enumerating_types
= false;
778 static int do_object_child_foreach(Object
*obj
,
779 int (*fn
)(Object
*child
, void *opaque
),
780 void *opaque
, bool recurse
)
782 ObjectProperty
*prop
, *next
;
785 QTAILQ_FOREACH_SAFE(prop
, &obj
->properties
, node
, next
) {
786 if (object_property_is_child(prop
)) {
787 Object
*child
= prop
->opaque
;
789 ret
= fn(child
, opaque
);
794 do_object_child_foreach(child
, fn
, opaque
, true);
801 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
804 return do_object_child_foreach(obj
, fn
, opaque
, false);
807 int object_child_foreach_recursive(Object
*obj
,
808 int (*fn
)(Object
*child
, void *opaque
),
811 return do_object_child_foreach(obj
, fn
, opaque
, true);
814 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
816 GSList
**list
= opaque
;
818 *list
= g_slist_prepend(*list
, klass
);
821 GSList
*object_class_get_list(const char *implements_type
,
822 bool include_abstract
)
826 object_class_foreach(object_class_get_list_tramp
,
827 implements_type
, include_abstract
, &list
);
831 void object_ref(Object
*obj
)
836 atomic_inc(&obj
->ref
);
839 void object_unref(Object
*obj
)
844 g_assert(obj
->ref
> 0);
846 /* parent always holds a reference to its children */
847 if (atomic_fetch_dec(&obj
->ref
) == 1) {
848 object_finalize(obj
);
853 object_property_add(Object
*obj
, const char *name
, const char *type
,
854 ObjectPropertyAccessor
*get
,
855 ObjectPropertyAccessor
*set
,
856 ObjectPropertyRelease
*release
,
857 void *opaque
, Error
**errp
)
859 ObjectProperty
*prop
;
860 size_t name_len
= strlen(name
);
862 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
865 char *name_no_array
= g_strdup(name
);
867 name_no_array
[name_len
- 3] = '\0';
869 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
871 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
872 release
, opaque
, NULL
);
878 g_free(name_no_array
);
882 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
883 if (strcmp(prop
->name
, name
) == 0) {
884 error_setg(errp
, "attempt to add duplicate property '%s'"
885 " to object (type '%s')", name
,
886 object_get_typename(obj
));
891 prop
= g_malloc0(sizeof(*prop
));
893 prop
->name
= g_strdup(name
);
894 prop
->type
= g_strdup(type
);
898 prop
->release
= release
;
899 prop
->opaque
= opaque
;
901 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
905 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
908 ObjectProperty
*prop
;
910 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
911 if (strcmp(prop
->name
, name
) == 0) {
916 error_setg(errp
, "Property '.%s' not found", name
);
920 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
922 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
928 prop
->release(obj
, name
, prop
->opaque
);
931 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
935 g_free(prop
->description
);
939 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
942 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
948 error_setg(errp
, QERR_PERMISSION_DENIED
);
950 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
954 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
957 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
963 error_setg(errp
, QERR_PERMISSION_DENIED
);
965 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
969 void object_property_set_str(Object
*obj
, const char *value
,
970 const char *name
, Error
**errp
)
972 QString
*qstr
= qstring_from_str(value
);
973 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
978 char *object_property_get_str(Object
*obj
, const char *name
,
981 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
988 qstring
= qobject_to_qstring(ret
);
990 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
993 retval
= g_strdup(qstring_get_str(qstring
));
1000 void object_property_set_link(Object
*obj
, Object
*value
,
1001 const char *name
, Error
**errp
)
1004 gchar
*path
= object_get_canonical_path(value
);
1005 object_property_set_str(obj
, path
, name
, errp
);
1008 object_property_set_str(obj
, "", name
, errp
);
1012 Object
*object_property_get_link(Object
*obj
, const char *name
,
1015 char *str
= object_property_get_str(obj
, name
, errp
);
1016 Object
*target
= NULL
;
1019 target
= object_resolve_path(str
, NULL
);
1021 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1022 "Device '%s' not found", str
);
1030 void object_property_set_bool(Object
*obj
, bool value
,
1031 const char *name
, Error
**errp
)
1033 QBool
*qbool
= qbool_from_bool(value
);
1034 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1039 bool object_property_get_bool(Object
*obj
, const char *name
,
1042 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1049 qbool
= qobject_to_qbool(ret
);
1051 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1054 retval
= qbool_get_bool(qbool
);
1061 void object_property_set_int(Object
*obj
, int64_t value
,
1062 const char *name
, Error
**errp
)
1064 QInt
*qint
= qint_from_int(value
);
1065 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
1070 int64_t object_property_get_int(Object
*obj
, const char *name
,
1073 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1080 qint
= qobject_to_qint(ret
);
1082 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1085 retval
= qint_get_int(qint
);
1092 typedef struct EnumProperty
{
1093 const char * const *strings
;
1094 int (*get
)(Object
*, Error
**);
1095 void (*set
)(Object
*, int, Error
**);
1098 int object_property_get_enum(Object
*obj
, const char *name
,
1099 const char *typename
, Error
**errp
)
1102 StringOutputVisitor
*sov
;
1103 StringInputVisitor
*siv
;
1106 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1107 EnumProperty
*enumprop
;
1113 if (!g_str_equal(prop
->type
, typename
)) {
1114 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1115 name
, object_class_get_name(
1116 object_get_class(obj
)), typename
);
1120 enumprop
= prop
->opaque
;
1122 sov
= string_output_visitor_new(false);
1123 object_property_get(obj
, string_output_get_visitor(sov
), name
, &err
);
1125 error_propagate(errp
, err
);
1126 string_output_visitor_cleanup(sov
);
1129 str
= string_output_get_string(sov
);
1130 siv
= string_input_visitor_new(str
);
1131 string_output_visitor_cleanup(sov
);
1132 visit_type_enum(string_input_get_visitor(siv
),
1133 &ret
, enumprop
->strings
, NULL
, name
, errp
);
1136 string_input_visitor_cleanup(siv
);
1141 void object_property_get_uint16List(Object
*obj
, const char *name
,
1142 uint16List
**list
, Error
**errp
)
1145 StringOutputVisitor
*ov
;
1146 StringInputVisitor
*iv
;
1149 ov
= string_output_visitor_new(false);
1150 object_property_get(obj
, string_output_get_visitor(ov
),
1153 error_propagate(errp
, err
);
1156 str
= string_output_get_string(ov
);
1157 iv
= string_input_visitor_new(str
);
1158 visit_type_uint16List(string_input_get_visitor(iv
),
1162 string_input_visitor_cleanup(iv
);
1164 string_output_visitor_cleanup(ov
);
1167 void object_property_parse(Object
*obj
, const char *string
,
1168 const char *name
, Error
**errp
)
1170 StringInputVisitor
*siv
;
1171 siv
= string_input_visitor_new(string
);
1172 object_property_set(obj
, string_input_get_visitor(siv
), name
, errp
);
1174 string_input_visitor_cleanup(siv
);
1177 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1180 StringOutputVisitor
*sov
;
1181 char *string
= NULL
;
1182 Error
*local_err
= NULL
;
1184 sov
= string_output_visitor_new(human
);
1185 object_property_get(obj
, string_output_get_visitor(sov
), name
, &local_err
);
1187 error_propagate(errp
, local_err
);
1191 string
= string_output_get_string(sov
);
1194 string_output_visitor_cleanup(sov
);
1198 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1200 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1208 Object
*object_get_root(void)
1210 static Object
*root
;
1213 root
= object_new("container");
1219 Object
*object_get_objects_root(void)
1221 return container_get(object_get_root(), "/objects");
1224 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
1225 const char *name
, Error
**errp
)
1227 Object
*child
= opaque
;
1230 path
= object_get_canonical_path(child
);
1231 visit_type_str(v
, &path
, name
, errp
);
1235 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1240 static void object_finalize_child_property(Object
*obj
, const char *name
,
1243 Object
*child
= opaque
;
1245 if (child
->class->unparent
) {
1246 (child
->class->unparent
)(child
);
1248 child
->parent
= NULL
;
1249 object_unref(child
);
1252 void object_property_add_child(Object
*obj
, const char *name
,
1253 Object
*child
, Error
**errp
)
1255 Error
*local_err
= NULL
;
1259 if (child
->parent
!= NULL
) {
1260 error_setg(errp
, "child object is already parented");
1264 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1266 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1267 object_finalize_child_property
, child
, &local_err
);
1269 error_propagate(errp
, local_err
);
1273 op
->resolve
= object_resolve_child_property
;
1275 child
->parent
= obj
;
1281 void object_property_allow_set_link(Object
*obj
, const char *name
,
1282 Object
*val
, Error
**errp
)
1284 /* Allow the link to be set, always */
1289 void (*check
)(Object
*, const char *, Object
*, Error
**);
1290 ObjectPropertyLinkFlags flags
;
1293 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1294 const char *name
, Error
**errp
)
1296 LinkProperty
*lprop
= opaque
;
1297 Object
**child
= lprop
->child
;
1301 path
= object_get_canonical_path(*child
);
1302 visit_type_str(v
, &path
, name
, errp
);
1306 visit_type_str(v
, &path
, name
, errp
);
1311 * object_resolve_link:
1313 * Lookup an object and ensure its type matches the link property type. This
1314 * is similar to object_resolve_path() except type verification against the
1315 * link property is performed.
1317 * Returns: The matched object or NULL on path lookup failures.
1319 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1320 const char *path
, Error
**errp
)
1324 bool ambiguous
= false;
1327 /* Go from link<FOO> to FOO. */
1328 type
= object_property_get_type(obj
, name
, NULL
);
1329 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1330 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1333 error_set(errp
, ERROR_CLASS_GENERIC_ERROR
,
1334 "Path '%s' does not uniquely identify an object", path
);
1335 } else if (!target
) {
1336 target
= object_resolve_path(path
, &ambiguous
);
1337 if (target
|| ambiguous
) {
1338 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1340 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1341 "Device '%s' not found", path
);
1345 g_free(target_type
);
1350 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1351 const char *name
, Error
**errp
)
1353 Error
*local_err
= NULL
;
1354 LinkProperty
*prop
= opaque
;
1355 Object
**child
= prop
->child
;
1356 Object
*old_target
= *child
;
1357 Object
*new_target
= NULL
;
1360 visit_type_str(v
, &path
, name
, &local_err
);
1362 if (!local_err
&& strcmp(path
, "") != 0) {
1363 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1368 error_propagate(errp
, local_err
);
1372 prop
->check(obj
, name
, new_target
, &local_err
);
1374 error_propagate(errp
, local_err
);
1378 object_ref(new_target
);
1379 *child
= new_target
;
1380 object_unref(old_target
);
1383 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1385 LinkProperty
*lprop
= opaque
;
1387 return *lprop
->child
;
1390 static void object_release_link_property(Object
*obj
, const char *name
,
1393 LinkProperty
*prop
= opaque
;
1395 if ((prop
->flags
& OBJ_PROP_LINK_UNREF_ON_RELEASE
) && *prop
->child
) {
1396 object_unref(*prop
->child
);
1401 void object_property_add_link(Object
*obj
, const char *name
,
1402 const char *type
, Object
**child
,
1403 void (*check
)(Object
*, const char *,
1404 Object
*, Error
**),
1405 ObjectPropertyLinkFlags flags
,
1408 Error
*local_err
= NULL
;
1409 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1413 prop
->child
= child
;
1414 prop
->check
= check
;
1415 prop
->flags
= flags
;
1417 full_type
= g_strdup_printf("link<%s>", type
);
1419 op
= object_property_add(obj
, name
, full_type
,
1420 object_get_link_property
,
1421 check
? object_set_link_property
: NULL
,
1422 object_release_link_property
,
1426 error_propagate(errp
, local_err
);
1431 op
->resolve
= object_resolve_link_property
;
1437 void object_property_add_const_link(Object
*obj
, const char *name
,
1438 Object
*target
, Error
**errp
)
1443 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1444 op
= object_property_add(obj
, name
, link_type
,
1445 object_get_child_property
, NULL
,
1446 NULL
, target
, errp
);
1448 op
->resolve
= object_resolve_child_property
;
1453 gchar
*object_get_canonical_path_component(Object
*obj
)
1455 ObjectProperty
*prop
= NULL
;
1458 g_assert(obj
->parent
!= NULL
);
1460 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
1461 if (!object_property_is_child(prop
)) {
1465 if (prop
->opaque
== obj
) {
1466 return g_strdup(prop
->name
);
1470 /* obj had a parent but was not a child, should never happen */
1471 g_assert_not_reached();
1475 gchar
*object_get_canonical_path(Object
*obj
)
1477 Object
*root
= object_get_root();
1478 char *newpath
, *path
= NULL
;
1480 while (obj
!= root
) {
1481 char *component
= object_get_canonical_path_component(obj
);
1484 newpath
= g_strdup_printf("%s/%s", component
, path
);
1495 newpath
= g_strdup_printf("/%s", path
? path
: "");
1501 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1503 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1508 if (prop
->resolve
) {
1509 return prop
->resolve(parent
, prop
->opaque
, part
);
1515 static Object
*object_resolve_abs_path(Object
*parent
,
1517 const char *typename
,
1522 if (parts
[index
] == NULL
) {
1523 return object_dynamic_cast(parent
, typename
);
1526 if (strcmp(parts
[index
], "") == 0) {
1527 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1530 child
= object_resolve_path_component(parent
, parts
[index
]);
1535 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1538 static Object
*object_resolve_partial_path(Object
*parent
,
1540 const char *typename
,
1544 ObjectProperty
*prop
;
1546 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1548 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1551 if (!object_property_is_child(prop
)) {
1555 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1556 typename
, ambiguous
);
1567 if (ambiguous
&& *ambiguous
) {
1575 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1581 parts
= g_strsplit(path
, "/", 0);
1584 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1588 obj
= object_resolve_partial_path(object_get_root(), parts
,
1589 typename
, ambiguous
);
1591 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1599 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1601 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1604 typedef struct StringProperty
1606 char *(*get
)(Object
*, Error
**);
1607 void (*set
)(Object
*, const char *, Error
**);
1610 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1611 const char *name
, Error
**errp
)
1613 StringProperty
*prop
= opaque
;
1617 value
= prop
->get(obj
, &err
);
1619 error_propagate(errp
, err
);
1623 visit_type_str(v
, &value
, name
, errp
);
1627 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1628 const char *name
, Error
**errp
)
1630 StringProperty
*prop
= opaque
;
1632 Error
*local_err
= NULL
;
1634 visit_type_str(v
, &value
, name
, &local_err
);
1636 error_propagate(errp
, local_err
);
1640 prop
->set(obj
, value
, errp
);
1644 static void property_release_str(Object
*obj
, const char *name
,
1647 StringProperty
*prop
= opaque
;
1651 void object_property_add_str(Object
*obj
, const char *name
,
1652 char *(*get
)(Object
*, Error
**),
1653 void (*set
)(Object
*, const char *, Error
**),
1656 Error
*local_err
= NULL
;
1657 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1662 object_property_add(obj
, name
, "string",
1663 get
? property_get_str
: NULL
,
1664 set
? property_set_str
: NULL
,
1665 property_release_str
,
1668 error_propagate(errp
, local_err
);
1673 typedef struct BoolProperty
1675 bool (*get
)(Object
*, Error
**);
1676 void (*set
)(Object
*, bool, Error
**);
1679 static void property_get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1680 const char *name
, Error
**errp
)
1682 BoolProperty
*prop
= opaque
;
1686 value
= prop
->get(obj
, &err
);
1688 error_propagate(errp
, err
);
1692 visit_type_bool(v
, &value
, name
, errp
);
1695 static void property_set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1696 const char *name
, Error
**errp
)
1698 BoolProperty
*prop
= opaque
;
1700 Error
*local_err
= NULL
;
1702 visit_type_bool(v
, &value
, name
, &local_err
);
1704 error_propagate(errp
, local_err
);
1708 prop
->set(obj
, value
, errp
);
1711 static void property_release_bool(Object
*obj
, const char *name
,
1714 BoolProperty
*prop
= opaque
;
1718 void object_property_add_bool(Object
*obj
, const char *name
,
1719 bool (*get
)(Object
*, Error
**),
1720 void (*set
)(Object
*, bool, Error
**),
1723 Error
*local_err
= NULL
;
1724 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1729 object_property_add(obj
, name
, "bool",
1730 get
? property_get_bool
: NULL
,
1731 set
? property_set_bool
: NULL
,
1732 property_release_bool
,
1735 error_propagate(errp
, local_err
);
1740 static void property_get_enum(Object
*obj
, Visitor
*v
, void *opaque
,
1741 const char *name
, Error
**errp
)
1743 EnumProperty
*prop
= opaque
;
1747 value
= prop
->get(obj
, &err
);
1749 error_propagate(errp
, err
);
1753 visit_type_enum(v
, &value
, prop
->strings
, NULL
, name
, errp
);
1756 static void property_set_enum(Object
*obj
, Visitor
*v
, void *opaque
,
1757 const char *name
, Error
**errp
)
1759 EnumProperty
*prop
= opaque
;
1763 visit_type_enum(v
, &value
, prop
->strings
, NULL
, name
, &err
);
1765 error_propagate(errp
, err
);
1768 prop
->set(obj
, value
, errp
);
1771 static void property_release_enum(Object
*obj
, const char *name
,
1774 EnumProperty
*prop
= opaque
;
1778 void object_property_add_enum(Object
*obj
, const char *name
,
1779 const char *typename
,
1780 const char * const *strings
,
1781 int (*get
)(Object
*, Error
**),
1782 void (*set
)(Object
*, int, Error
**),
1785 Error
*local_err
= NULL
;
1786 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
1788 prop
->strings
= strings
;
1792 object_property_add(obj
, name
, typename
,
1793 get
? property_get_enum
: NULL
,
1794 set
? property_set_enum
: NULL
,
1795 property_release_enum
,
1798 error_propagate(errp
, local_err
);
1803 typedef struct TMProperty
{
1804 void (*get
)(Object
*, struct tm
*, Error
**);
1807 static void property_get_tm(Object
*obj
, Visitor
*v
, void *opaque
,
1808 const char *name
, Error
**errp
)
1810 TMProperty
*prop
= opaque
;
1814 prop
->get(obj
, &value
, &err
);
1819 visit_start_struct(v
, NULL
, "struct tm", name
, 0, &err
);
1823 visit_type_int32(v
, &value
.tm_year
, "tm_year", &err
);
1827 visit_type_int32(v
, &value
.tm_mon
, "tm_mon", &err
);
1831 visit_type_int32(v
, &value
.tm_mday
, "tm_mday", &err
);
1835 visit_type_int32(v
, &value
.tm_hour
, "tm_hour", &err
);
1839 visit_type_int32(v
, &value
.tm_min
, "tm_min", &err
);
1843 visit_type_int32(v
, &value
.tm_sec
, "tm_sec", &err
);
1848 error_propagate(errp
, err
);
1850 visit_end_struct(v
, errp
);
1852 error_propagate(errp
, err
);
1856 static void property_release_tm(Object
*obj
, const char *name
,
1859 TMProperty
*prop
= opaque
;
1863 void object_property_add_tm(Object
*obj
, const char *name
,
1864 void (*get
)(Object
*, struct tm
*, Error
**),
1867 Error
*local_err
= NULL
;
1868 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
1872 object_property_add(obj
, name
, "struct tm",
1873 get
? property_get_tm
: NULL
, NULL
,
1874 property_release_tm
,
1877 error_propagate(errp
, local_err
);
1882 static char *qdev_get_type(Object
*obj
, Error
**errp
)
1884 return g_strdup(object_get_typename(obj
));
1887 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
,
1888 void *opaque
, const char *name
,
1891 uint8_t value
= *(uint8_t *)opaque
;
1892 visit_type_uint8(v
, &value
, name
, errp
);
1895 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
,
1896 void *opaque
, const char *name
,
1899 uint16_t value
= *(uint16_t *)opaque
;
1900 visit_type_uint16(v
, &value
, name
, errp
);
1903 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
,
1904 void *opaque
, const char *name
,
1907 uint32_t value
= *(uint32_t *)opaque
;
1908 visit_type_uint32(v
, &value
, name
, errp
);
1911 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
,
1912 void *opaque
, const char *name
,
1915 uint64_t value
= *(uint64_t *)opaque
;
1916 visit_type_uint64(v
, &value
, name
, errp
);
1919 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
1920 const uint8_t *v
, Error
**errp
)
1922 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
1923 NULL
, NULL
, (void *)v
, errp
);
1926 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
1927 const uint16_t *v
, Error
**errp
)
1929 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
1930 NULL
, NULL
, (void *)v
, errp
);
1933 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
1934 const uint32_t *v
, Error
**errp
)
1936 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
1937 NULL
, NULL
, (void *)v
, errp
);
1940 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
1941 const uint64_t *v
, Error
**errp
)
1943 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
1944 NULL
, NULL
, (void *)v
, errp
);
1952 static void property_get_alias(Object
*obj
, struct Visitor
*v
, void *opaque
,
1953 const char *name
, Error
**errp
)
1955 AliasProperty
*prop
= opaque
;
1957 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
1960 static void property_set_alias(Object
*obj
, struct Visitor
*v
, void *opaque
,
1961 const char *name
, Error
**errp
)
1963 AliasProperty
*prop
= opaque
;
1965 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
1968 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
1971 AliasProperty
*prop
= opaque
;
1973 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
1976 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
1978 AliasProperty
*prop
= opaque
;
1980 g_free(prop
->target_name
);
1984 void object_property_add_alias(Object
*obj
, const char *name
,
1985 Object
*target_obj
, const char *target_name
,
1988 AliasProperty
*prop
;
1990 ObjectProperty
*target_prop
;
1992 Error
*local_err
= NULL
;
1994 target_prop
= object_property_find(target_obj
, target_name
, errp
);
1999 if (object_property_is_child(target_prop
)) {
2000 prop_type
= g_strdup_printf("link%s",
2001 target_prop
->type
+ strlen("child"));
2003 prop_type
= g_strdup(target_prop
->type
);
2006 prop
= g_malloc(sizeof(*prop
));
2007 prop
->target_obj
= target_obj
;
2008 prop
->target_name
= g_strdup(target_name
);
2010 op
= object_property_add(obj
, name
, prop_type
,
2013 property_release_alias
,
2016 error_propagate(errp
, local_err
);
2020 op
->resolve
= property_resolve_alias
;
2022 object_property_set_description(obj
, op
->name
,
2023 target_prop
->description
,
2030 void object_property_set_description(Object
*obj
, const char *name
,
2031 const char *description
, Error
**errp
)
2035 op
= object_property_find(obj
, name
, errp
);
2040 g_free(op
->description
);
2041 op
->description
= g_strdup(description
);
2044 static void object_instance_init(Object
*obj
)
2046 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
2049 static void register_types(void)
2051 static TypeInfo interface_info
= {
2052 .name
= TYPE_INTERFACE
,
2053 .class_size
= sizeof(InterfaceClass
),
2057 static TypeInfo object_info
= {
2058 .name
= TYPE_OBJECT
,
2059 .instance_size
= sizeof(Object
),
2060 .instance_init
= object_instance_init
,
2064 type_interface
= type_register_internal(&interface_info
);
2065 type_register_internal(&object_info
);
2068 type_init(register_types
)