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 "qemu-common.h"
15 #include "qapi/visitor.h"
16 #include "qapi/string-input-visitor.h"
17 #include "qapi/string-output-visitor.h"
18 #include "qapi/qmp/qerror.h"
21 /* TODO: replace QObject with a simpler visitor to avoid a dependency
22 * of the QOM core on QObject? */
23 #include "qom/qom-qobject.h"
24 #include "qapi/qmp/qobject.h"
25 #include "qapi/qmp/qbool.h"
26 #include "qapi/qmp/qint.h"
27 #include "qapi/qmp/qstring.h"
29 #define MAX_INTERFACES 32
31 typedef struct InterfaceImpl InterfaceImpl
;
32 typedef struct TypeImpl TypeImpl
;
47 void (*class_init
)(ObjectClass
*klass
, void *data
);
48 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
49 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
53 void (*instance_init
)(Object
*obj
);
54 void (*instance_post_init
)(Object
*obj
);
55 void (*instance_finalize
)(Object
*obj
);
60 TypeImpl
*parent_type
;
65 InterfaceImpl interfaces
[MAX_INTERFACES
];
68 static Type type_interface
;
70 static GHashTable
*type_table_get(void)
72 static GHashTable
*type_table
;
74 if (type_table
== NULL
) {
75 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
81 static void type_table_add(TypeImpl
*ti
)
83 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
86 static TypeImpl
*type_table_lookup(const char *name
)
88 return g_hash_table_lookup(type_table_get(), name
);
91 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
93 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
96 g_assert(info
->name
!= NULL
);
98 if (type_table_lookup(info
->name
) != NULL
) {
99 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
103 ti
->name
= g_strdup(info
->name
);
104 ti
->parent
= g_strdup(info
->parent
);
106 ti
->class_size
= info
->class_size
;
107 ti
->instance_size
= info
->instance_size
;
109 ti
->class_init
= info
->class_init
;
110 ti
->class_base_init
= info
->class_base_init
;
111 ti
->class_finalize
= info
->class_finalize
;
112 ti
->class_data
= info
->class_data
;
114 ti
->instance_init
= info
->instance_init
;
115 ti
->instance_post_init
= info
->instance_post_init
;
116 ti
->instance_finalize
= info
->instance_finalize
;
118 ti
->abstract
= info
->abstract
;
120 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
121 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
123 ti
->num_interfaces
= i
;
130 TypeImpl
*type_register(const TypeInfo
*info
)
132 assert(info
->parent
);
133 return type_register_internal(info
);
136 TypeImpl
*type_register_static(const TypeInfo
*info
)
138 return type_register(info
);
141 static TypeImpl
*type_get_by_name(const char *name
)
147 return type_table_lookup(name
);
150 static TypeImpl
*type_get_parent(TypeImpl
*type
)
152 if (!type
->parent_type
&& type
->parent
) {
153 type
->parent_type
= type_get_by_name(type
->parent
);
154 g_assert(type
->parent_type
!= NULL
);
157 return type
->parent_type
;
160 static bool type_has_parent(TypeImpl
*type
)
162 return (type
->parent
!= NULL
);
165 static size_t type_class_get_size(TypeImpl
*ti
)
167 if (ti
->class_size
) {
168 return ti
->class_size
;
171 if (type_has_parent(ti
)) {
172 return type_class_get_size(type_get_parent(ti
));
175 return sizeof(ObjectClass
);
178 static size_t type_object_get_size(TypeImpl
*ti
)
180 if (ti
->instance_size
) {
181 return ti
->instance_size
;
184 if (type_has_parent(ti
)) {
185 return type_object_get_size(type_get_parent(ti
));
191 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
195 /* Check if typename is a direct ancestor of type */
197 if (type
== target_type
) {
201 type
= type_get_parent(type
);
207 static void type_initialize(TypeImpl
*ti
);
209 static void type_initialize_interface(TypeImpl
*ti
, const char *parent
)
211 InterfaceClass
*new_iface
;
213 TypeImpl
*iface_impl
;
215 info
.parent
= parent
;
216 info
.name
= g_strdup_printf("%s::%s", ti
->name
, info
.parent
);
217 info
.abstract
= true;
219 iface_impl
= type_register(&info
);
220 type_initialize(iface_impl
);
221 g_free((char *)info
.name
);
223 new_iface
= (InterfaceClass
*)iface_impl
->class;
224 new_iface
->concrete_class
= ti
->class;
226 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
230 static void type_initialize(TypeImpl
*ti
)
238 ti
->class_size
= type_class_get_size(ti
);
239 ti
->instance_size
= type_object_get_size(ti
);
241 ti
->class = g_malloc0(ti
->class_size
);
243 parent
= type_get_parent(ti
);
245 type_initialize(parent
);
249 g_assert(parent
->class_size
<= ti
->class_size
);
250 memcpy(ti
->class, parent
->class, parent
->class_size
);
251 ti
->class->interfaces
= NULL
;
253 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
254 ObjectClass
*iface
= e
->data
;
255 type_initialize_interface(ti
, object_class_get_name(iface
));
258 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
259 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
260 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
261 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
263 if (type_is_ancestor(target_type
, t
)) {
272 type_initialize_interface(ti
, ti
->interfaces
[i
].typename
);
276 ti
->class->type
= ti
;
279 if (parent
->class_base_init
) {
280 parent
->class_base_init(ti
->class, ti
->class_data
);
282 parent
= type_get_parent(parent
);
285 if (ti
->class_init
) {
286 ti
->class_init(ti
->class, ti
->class_data
);
292 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
294 if (type_has_parent(ti
)) {
295 object_init_with_type(obj
, type_get_parent(ti
));
298 if (ti
->instance_init
) {
299 ti
->instance_init(obj
);
303 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
305 if (ti
->instance_post_init
) {
306 ti
->instance_post_init(obj
);
309 if (type_has_parent(ti
)) {
310 object_post_init_with_type(obj
, type_get_parent(ti
));
314 void object_initialize_with_type(void *data
, TypeImpl
*type
)
318 g_assert(type
!= NULL
);
319 type_initialize(type
);
321 g_assert(type
->instance_size
>= sizeof(Object
));
322 g_assert(type
->abstract
== false);
324 memset(obj
, 0, type
->instance_size
);
325 obj
->class = type
->class;
327 QTAILQ_INIT(&obj
->properties
);
328 object_init_with_type(obj
, type
);
329 object_post_init_with_type(obj
, type
);
332 void object_initialize(void *data
, const char *typename
)
334 TypeImpl
*type
= type_get_by_name(typename
);
336 object_initialize_with_type(data
, type
);
339 static inline bool object_property_is_child(ObjectProperty
*prop
)
341 return strstart(prop
->type
, "child<", NULL
);
344 static inline bool object_property_is_link(ObjectProperty
*prop
)
346 return strstart(prop
->type
, "link<", NULL
);
349 static void object_property_del_all(Object
*obj
)
351 while (!QTAILQ_EMPTY(&obj
->properties
)) {
352 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
354 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
357 prop
->release(obj
, prop
->name
, prop
->opaque
);
366 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
368 ObjectProperty
*prop
;
370 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
371 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
372 object_property_del(obj
, prop
->name
, errp
);
378 void object_unparent(Object
*obj
)
385 if (obj
->class->unparent
) {
386 (obj
->class->unparent
)(obj
);
389 object_property_del_child(obj
->parent
, obj
, NULL
);
394 static void object_deinit(Object
*obj
, TypeImpl
*type
)
396 if (type
->instance_finalize
) {
397 type
->instance_finalize(obj
);
400 if (type_has_parent(type
)) {
401 object_deinit(obj
, type_get_parent(type
));
405 static void object_finalize(void *data
)
408 TypeImpl
*ti
= obj
->class->type
;
410 object_deinit(obj
, ti
);
411 object_property_del_all(obj
);
413 g_assert(obj
->ref
== 0);
419 Object
*object_new_with_type(Type type
)
423 g_assert(type
!= NULL
);
424 type_initialize(type
);
426 obj
= g_malloc(type
->instance_size
);
427 object_initialize_with_type(obj
, type
);
433 Object
*object_new(const char *typename
)
435 TypeImpl
*ti
= type_get_by_name(typename
);
437 return object_new_with_type(ti
);
440 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
442 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
449 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
450 const char *file
, int line
, const char *func
)
452 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
453 typename
, file
, line
, func
);
455 #ifdef CONFIG_QOM_CAST_DEBUG
459 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
460 if (obj
->class->cast_cache
[i
] == typename
) {
465 inst
= object_dynamic_cast(obj
, typename
);
468 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
469 file
, line
, func
, obj
, typename
);
475 if (obj
&& obj
== inst
) {
476 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
477 obj
->class->cast_cache
[i
- 1] = obj
->class->cast_cache
[i
];
479 obj
->class->cast_cache
[i
- 1] = typename
;
487 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
488 const char *typename
)
490 ObjectClass
*ret
= NULL
;
491 TypeImpl
*target_type
;
498 /* A simple fast path that can trigger a lot for leaf classes. */
500 if (type
->name
== typename
) {
504 target_type
= type_get_by_name(typename
);
506 /* target class type unknown, so fail the cast */
510 if (type
->class->interfaces
&&
511 type_is_ancestor(target_type
, type_interface
)) {
515 for (i
= class->interfaces
; i
; i
= i
->next
) {
516 ObjectClass
*target_class
= i
->data
;
518 if (type_is_ancestor(target_class
->type
, target_type
)) {
524 /* The match was ambiguous, don't allow a cast */
528 } else if (type_is_ancestor(type
, target_type
)) {
535 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
536 const char *typename
,
537 const char *file
, int line
,
542 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
543 typename
, file
, line
, func
);
545 #ifdef CONFIG_QOM_CAST_DEBUG
548 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
549 if (class->cast_cache
[i
] == typename
) {
555 if (!class || !class->interfaces
) {
560 ret
= object_class_dynamic_cast(class, typename
);
562 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
563 file
, line
, func
, class, typename
);
567 #ifdef CONFIG_QOM_CAST_DEBUG
568 if (class && ret
== class) {
569 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
570 class->cast_cache
[i
- 1] = class->cast_cache
[i
];
572 class->cast_cache
[i
- 1] = typename
;
579 const char *object_get_typename(Object
*obj
)
581 return obj
->class->type
->name
;
584 ObjectClass
*object_get_class(Object
*obj
)
589 bool object_class_is_abstract(ObjectClass
*klass
)
591 return klass
->type
->abstract
;
594 const char *object_class_get_name(ObjectClass
*klass
)
596 return klass
->type
->name
;
599 ObjectClass
*object_class_by_name(const char *typename
)
601 TypeImpl
*type
= type_get_by_name(typename
);
607 type_initialize(type
);
612 ObjectClass
*object_class_get_parent(ObjectClass
*class)
614 TypeImpl
*type
= type_get_parent(class->type
);
620 type_initialize(type
);
625 typedef struct OCFData
627 void (*fn
)(ObjectClass
*klass
, void *opaque
);
628 const char *implements_type
;
629 bool include_abstract
;
633 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
636 OCFData
*data
= opaque
;
637 TypeImpl
*type
= value
;
640 type_initialize(type
);
643 if (!data
->include_abstract
&& type
->abstract
) {
647 if (data
->implements_type
&&
648 !object_class_dynamic_cast(k
, data
->implements_type
)) {
652 data
->fn(k
, data
->opaque
);
655 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
656 const char *implements_type
, bool include_abstract
,
659 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
661 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
664 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
667 ObjectProperty
*prop
;
670 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
671 if (object_property_is_child(prop
)) {
672 ret
= fn(prop
->opaque
, opaque
);
681 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
683 GSList
**list
= opaque
;
685 *list
= g_slist_prepend(*list
, klass
);
688 GSList
*object_class_get_list(const char *implements_type
,
689 bool include_abstract
)
693 object_class_foreach(object_class_get_list_tramp
,
694 implements_type
, include_abstract
, &list
);
698 void object_ref(Object
*obj
)
700 atomic_inc(&obj
->ref
);
703 void object_unref(Object
*obj
)
705 g_assert(obj
->ref
> 0);
707 /* parent always holds a reference to its children */
708 if (atomic_fetch_dec(&obj
->ref
) == 1) {
709 object_finalize(obj
);
713 void object_property_add(Object
*obj
, const char *name
, const char *type
,
714 ObjectPropertyAccessor
*get
,
715 ObjectPropertyAccessor
*set
,
716 ObjectPropertyRelease
*release
,
717 void *opaque
, Error
**errp
)
719 ObjectProperty
*prop
;
721 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
722 if (strcmp(prop
->name
, name
) == 0) {
723 error_setg(errp
, "attempt to add duplicate property '%s'"
724 " to object (type '%s')", name
,
725 object_get_typename(obj
));
730 prop
= g_malloc0(sizeof(*prop
));
732 prop
->name
= g_strdup(name
);
733 prop
->type
= g_strdup(type
);
737 prop
->release
= release
;
738 prop
->opaque
= opaque
;
740 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
743 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
746 ObjectProperty
*prop
;
748 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
749 if (strcmp(prop
->name
, name
) == 0) {
754 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
758 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
760 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
766 prop
->release(obj
, name
, prop
->opaque
);
769 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
776 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
779 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
785 error_set(errp
, QERR_PERMISSION_DENIED
);
787 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
791 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
794 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
800 error_set(errp
, QERR_PERMISSION_DENIED
);
802 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
806 void object_property_set_str(Object
*obj
, const char *value
,
807 const char *name
, Error
**errp
)
809 QString
*qstr
= qstring_from_str(value
);
810 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
815 char *object_property_get_str(Object
*obj
, const char *name
,
818 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
825 qstring
= qobject_to_qstring(ret
);
827 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
830 retval
= g_strdup(qstring_get_str(qstring
));
837 void object_property_set_link(Object
*obj
, Object
*value
,
838 const char *name
, Error
**errp
)
840 object_property_set_str(obj
, object_get_canonical_path(value
),
844 Object
*object_property_get_link(Object
*obj
, const char *name
,
847 char *str
= object_property_get_str(obj
, name
, errp
);
848 Object
*target
= NULL
;
851 target
= object_resolve_path(str
, NULL
);
853 error_set(errp
, QERR_DEVICE_NOT_FOUND
, str
);
861 void object_property_set_bool(Object
*obj
, bool value
,
862 const char *name
, Error
**errp
)
864 QBool
*qbool
= qbool_from_int(value
);
865 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
870 bool object_property_get_bool(Object
*obj
, const char *name
,
873 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
880 qbool
= qobject_to_qbool(ret
);
882 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
885 retval
= qbool_get_int(qbool
);
892 void object_property_set_int(Object
*obj
, int64_t value
,
893 const char *name
, Error
**errp
)
895 QInt
*qint
= qint_from_int(value
);
896 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
901 int64_t object_property_get_int(Object
*obj
, const char *name
,
904 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
911 qint
= qobject_to_qint(ret
);
913 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
916 retval
= qint_get_int(qint
);
923 void object_property_parse(Object
*obj
, const char *string
,
924 const char *name
, Error
**errp
)
926 StringInputVisitor
*mi
;
927 mi
= string_input_visitor_new(string
);
928 object_property_set(obj
, string_input_get_visitor(mi
), name
, errp
);
930 string_input_visitor_cleanup(mi
);
933 char *object_property_print(Object
*obj
, const char *name
,
936 StringOutputVisitor
*mo
;
939 mo
= string_output_visitor_new();
940 object_property_get(obj
, string_output_get_visitor(mo
), name
, errp
);
941 string
= string_output_get_string(mo
);
942 string_output_visitor_cleanup(mo
);
946 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
948 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
956 Object
*object_get_root(void)
961 root
= object_new("container");
967 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
968 const char *name
, Error
**errp
)
970 Object
*child
= opaque
;
973 path
= object_get_canonical_path(child
);
974 visit_type_str(v
, &path
, name
, errp
);
978 static void object_finalize_child_property(Object
*obj
, const char *name
,
981 Object
*child
= opaque
;
986 void object_property_add_child(Object
*obj
, const char *name
,
987 Object
*child
, Error
**errp
)
991 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
993 object_property_add(obj
, name
, type
, object_get_child_property
,
994 NULL
, object_finalize_child_property
, child
, errp
);
997 g_assert(child
->parent
== NULL
);
1003 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1004 const char *name
, Error
**errp
)
1006 Object
**child
= opaque
;
1010 path
= object_get_canonical_path(*child
);
1011 visit_type_str(v
, &path
, name
, errp
);
1015 visit_type_str(v
, &path
, name
, errp
);
1019 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1020 const char *name
, Error
**errp
)
1022 Object
**child
= opaque
;
1024 bool ambiguous
= false;
1029 type
= object_property_get_type(obj
, name
, NULL
);
1031 visit_type_str(v
, &path
, name
, errp
);
1033 old_target
= *child
;
1036 if (strcmp(path
, "") != 0) {
1039 /* Go from link<FOO> to FOO. */
1040 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1041 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1044 error_set(errp
, QERR_AMBIGUOUS_PATH
, path
);
1045 } else if (target
) {
1049 target
= object_resolve_path(path
, &ambiguous
);
1050 if (target
|| ambiguous
) {
1051 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1053 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
1056 g_free(target_type
);
1061 if (old_target
!= NULL
) {
1062 object_unref(old_target
);
1066 void object_property_add_link(Object
*obj
, const char *name
,
1067 const char *type
, Object
**child
,
1072 full_type
= g_strdup_printf("link<%s>", type
);
1074 object_property_add(obj
, name
, full_type
,
1075 object_get_link_property
,
1076 object_set_link_property
,
1082 gchar
*object_get_canonical_path(Object
*obj
)
1084 Object
*root
= object_get_root();
1085 char *newpath
= NULL
, *path
= NULL
;
1087 while (obj
!= root
) {
1088 ObjectProperty
*prop
= NULL
;
1090 g_assert(obj
->parent
!= NULL
);
1092 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
1093 if (!object_property_is_child(prop
)) {
1097 if (prop
->opaque
== obj
) {
1099 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1103 path
= g_strdup(prop
->name
);
1109 g_assert(prop
!= NULL
);
1114 newpath
= g_strdup_printf("/%s", path
);
1120 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1122 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1127 if (object_property_is_link(prop
)) {
1128 return *(Object
**)prop
->opaque
;
1129 } else if (object_property_is_child(prop
)) {
1130 return prop
->opaque
;
1136 static Object
*object_resolve_abs_path(Object
*parent
,
1138 const char *typename
,
1143 if (parts
[index
] == NULL
) {
1144 return object_dynamic_cast(parent
, typename
);
1147 if (strcmp(parts
[index
], "") == 0) {
1148 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1151 child
= object_resolve_path_component(parent
, parts
[index
]);
1156 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1159 static Object
*object_resolve_partial_path(Object
*parent
,
1161 const char *typename
,
1165 ObjectProperty
*prop
;
1167 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1169 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1172 if (!object_property_is_child(prop
)) {
1176 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1177 typename
, ambiguous
);
1188 if (ambiguous
&& *ambiguous
) {
1196 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1202 parts
= g_strsplit(path
, "/", 0);
1205 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1209 obj
= object_resolve_partial_path(object_get_root(), parts
,
1210 typename
, ambiguous
);
1212 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1220 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1222 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1225 typedef struct StringProperty
1227 char *(*get
)(Object
*, Error
**);
1228 void (*set
)(Object
*, const char *, Error
**);
1231 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1232 const char *name
, Error
**errp
)
1234 StringProperty
*prop
= opaque
;
1237 value
= prop
->get(obj
, errp
);
1239 visit_type_str(v
, &value
, name
, errp
);
1244 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1245 const char *name
, Error
**errp
)
1247 StringProperty
*prop
= opaque
;
1249 Error
*local_err
= NULL
;
1251 visit_type_str(v
, &value
, name
, &local_err
);
1253 error_propagate(errp
, local_err
);
1257 prop
->set(obj
, value
, errp
);
1261 static void property_release_str(Object
*obj
, const char *name
,
1264 StringProperty
*prop
= opaque
;
1268 void object_property_add_str(Object
*obj
, const char *name
,
1269 char *(*get
)(Object
*, Error
**),
1270 void (*set
)(Object
*, const char *, Error
**),
1273 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1278 object_property_add(obj
, name
, "string",
1279 get
? property_get_str
: NULL
,
1280 set
? property_set_str
: NULL
,
1281 property_release_str
,
1285 typedef struct BoolProperty
1287 bool (*get
)(Object
*, Error
**);
1288 void (*set
)(Object
*, bool, Error
**);
1291 static void property_get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1292 const char *name
, Error
**errp
)
1294 BoolProperty
*prop
= opaque
;
1297 value
= prop
->get(obj
, errp
);
1298 visit_type_bool(v
, &value
, name
, errp
);
1301 static void property_set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1302 const char *name
, Error
**errp
)
1304 BoolProperty
*prop
= opaque
;
1306 Error
*local_err
= NULL
;
1308 visit_type_bool(v
, &value
, name
, &local_err
);
1310 error_propagate(errp
, local_err
);
1314 prop
->set(obj
, value
, errp
);
1317 static void property_release_bool(Object
*obj
, const char *name
,
1320 BoolProperty
*prop
= opaque
;
1324 void object_property_add_bool(Object
*obj
, const char *name
,
1325 bool (*get
)(Object
*, Error
**),
1326 void (*set
)(Object
*, bool, Error
**),
1329 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1334 object_property_add(obj
, name
, "bool",
1335 get
? property_get_bool
: NULL
,
1336 set
? property_set_bool
: NULL
,
1337 property_release_bool
,
1341 static char *qdev_get_type(Object
*obj
, Error
**errp
)
1343 return g_strdup(object_get_typename(obj
));
1346 static void object_instance_init(Object
*obj
)
1348 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
1351 static void register_types(void)
1353 static TypeInfo interface_info
= {
1354 .name
= TYPE_INTERFACE
,
1355 .class_size
= sizeof(InterfaceClass
),
1359 static TypeInfo object_info
= {
1360 .name
= TYPE_OBJECT
,
1361 .instance_size
= sizeof(Object
),
1362 .instance_init
= object_instance_init
,
1366 type_interface
= type_register_internal(&interface_info
);
1367 type_register_internal(&object_info
);
1370 type_init(register_types
)