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
, size_t size
, 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);
323 g_assert(size
>= type
->instance_size
);
325 memset(obj
, 0, type
->instance_size
);
326 obj
->class = type
->class;
328 QTAILQ_INIT(&obj
->properties
);
329 object_init_with_type(obj
, type
);
330 object_post_init_with_type(obj
, type
);
333 void object_initialize(void *data
, size_t size
, const char *typename
)
335 TypeImpl
*type
= type_get_by_name(typename
);
337 object_initialize_with_type(data
, size
, type
);
340 static inline bool object_property_is_child(ObjectProperty
*prop
)
342 return strstart(prop
->type
, "child<", NULL
);
345 static inline bool object_property_is_link(ObjectProperty
*prop
)
347 return strstart(prop
->type
, "link<", NULL
);
350 static void object_property_del_all(Object
*obj
)
352 while (!QTAILQ_EMPTY(&obj
->properties
)) {
353 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
355 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
358 prop
->release(obj
, prop
->name
, prop
->opaque
);
367 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
369 ObjectProperty
*prop
;
371 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
372 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
373 object_property_del(obj
, prop
->name
, errp
);
379 void object_unparent(Object
*obj
)
386 if (obj
->class->unparent
) {
387 (obj
->class->unparent
)(obj
);
390 object_property_del_child(obj
->parent
, obj
, NULL
);
395 static void object_deinit(Object
*obj
, TypeImpl
*type
)
397 if (type
->instance_finalize
) {
398 type
->instance_finalize(obj
);
401 if (type_has_parent(type
)) {
402 object_deinit(obj
, type_get_parent(type
));
406 static void object_finalize(void *data
)
409 TypeImpl
*ti
= obj
->class->type
;
411 object_deinit(obj
, ti
);
412 object_property_del_all(obj
);
414 g_assert(obj
->ref
== 0);
420 Object
*object_new_with_type(Type type
)
424 g_assert(type
!= NULL
);
425 type_initialize(type
);
427 obj
= g_malloc(type
->instance_size
);
428 object_initialize_with_type(obj
, type
->instance_size
, type
);
434 Object
*object_new(const char *typename
)
436 TypeImpl
*ti
= type_get_by_name(typename
);
438 return object_new_with_type(ti
);
441 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
443 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
450 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
451 const char *file
, int line
, const char *func
)
453 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
454 typename
, file
, line
, func
);
456 #ifdef CONFIG_QOM_CAST_DEBUG
460 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
461 if (obj
->class->cast_cache
[i
] == typename
) {
466 inst
= object_dynamic_cast(obj
, typename
);
469 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
470 file
, line
, func
, obj
, typename
);
476 if (obj
&& obj
== inst
) {
477 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
478 obj
->class->cast_cache
[i
- 1] = obj
->class->cast_cache
[i
];
480 obj
->class->cast_cache
[i
- 1] = typename
;
488 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
489 const char *typename
)
491 ObjectClass
*ret
= NULL
;
492 TypeImpl
*target_type
;
499 /* A simple fast path that can trigger a lot for leaf classes. */
501 if (type
->name
== typename
) {
505 target_type
= type_get_by_name(typename
);
507 /* target class type unknown, so fail the cast */
511 if (type
->class->interfaces
&&
512 type_is_ancestor(target_type
, type_interface
)) {
516 for (i
= class->interfaces
; i
; i
= i
->next
) {
517 ObjectClass
*target_class
= i
->data
;
519 if (type_is_ancestor(target_class
->type
, target_type
)) {
525 /* The match was ambiguous, don't allow a cast */
529 } else if (type_is_ancestor(type
, target_type
)) {
536 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
537 const char *typename
,
538 const char *file
, int line
,
543 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
544 typename
, file
, line
, func
);
546 #ifdef CONFIG_QOM_CAST_DEBUG
549 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
550 if (class->cast_cache
[i
] == typename
) {
556 if (!class || !class->interfaces
) {
561 ret
= object_class_dynamic_cast(class, typename
);
563 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
564 file
, line
, func
, class, typename
);
568 #ifdef CONFIG_QOM_CAST_DEBUG
569 if (class && ret
== class) {
570 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
571 class->cast_cache
[i
- 1] = class->cast_cache
[i
];
573 class->cast_cache
[i
- 1] = typename
;
580 const char *object_get_typename(Object
*obj
)
582 return obj
->class->type
->name
;
585 ObjectClass
*object_get_class(Object
*obj
)
590 bool object_class_is_abstract(ObjectClass
*klass
)
592 return klass
->type
->abstract
;
595 const char *object_class_get_name(ObjectClass
*klass
)
597 return klass
->type
->name
;
600 ObjectClass
*object_class_by_name(const char *typename
)
602 TypeImpl
*type
= type_get_by_name(typename
);
608 type_initialize(type
);
613 ObjectClass
*object_class_get_parent(ObjectClass
*class)
615 TypeImpl
*type
= type_get_parent(class->type
);
621 type_initialize(type
);
626 typedef struct OCFData
628 void (*fn
)(ObjectClass
*klass
, void *opaque
);
629 const char *implements_type
;
630 bool include_abstract
;
634 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
637 OCFData
*data
= opaque
;
638 TypeImpl
*type
= value
;
641 type_initialize(type
);
644 if (!data
->include_abstract
&& type
->abstract
) {
648 if (data
->implements_type
&&
649 !object_class_dynamic_cast(k
, data
->implements_type
)) {
653 data
->fn(k
, data
->opaque
);
656 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
657 const char *implements_type
, bool include_abstract
,
660 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
662 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
665 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
668 ObjectProperty
*prop
;
671 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
672 if (object_property_is_child(prop
)) {
673 ret
= fn(prop
->opaque
, opaque
);
682 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
684 GSList
**list
= opaque
;
686 *list
= g_slist_prepend(*list
, klass
);
689 GSList
*object_class_get_list(const char *implements_type
,
690 bool include_abstract
)
694 object_class_foreach(object_class_get_list_tramp
,
695 implements_type
, include_abstract
, &list
);
699 void object_ref(Object
*obj
)
701 atomic_inc(&obj
->ref
);
704 void object_unref(Object
*obj
)
706 g_assert(obj
->ref
> 0);
708 /* parent always holds a reference to its children */
709 if (atomic_fetch_dec(&obj
->ref
) == 1) {
710 object_finalize(obj
);
714 void object_property_add(Object
*obj
, const char *name
, const char *type
,
715 ObjectPropertyAccessor
*get
,
716 ObjectPropertyAccessor
*set
,
717 ObjectPropertyRelease
*release
,
718 void *opaque
, Error
**errp
)
720 ObjectProperty
*prop
;
722 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
723 if (strcmp(prop
->name
, name
) == 0) {
724 error_setg(errp
, "attempt to add duplicate property '%s'"
725 " to object (type '%s')", name
,
726 object_get_typename(obj
));
731 prop
= g_malloc0(sizeof(*prop
));
733 prop
->name
= g_strdup(name
);
734 prop
->type
= g_strdup(type
);
738 prop
->release
= release
;
739 prop
->opaque
= opaque
;
741 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
744 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
747 ObjectProperty
*prop
;
749 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
750 if (strcmp(prop
->name
, name
) == 0) {
755 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
759 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
761 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
767 prop
->release(obj
, name
, prop
->opaque
);
770 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
777 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
780 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
786 error_set(errp
, QERR_PERMISSION_DENIED
);
788 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
792 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
795 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
801 error_set(errp
, QERR_PERMISSION_DENIED
);
803 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
807 void object_property_set_str(Object
*obj
, const char *value
,
808 const char *name
, Error
**errp
)
810 QString
*qstr
= qstring_from_str(value
);
811 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
816 char *object_property_get_str(Object
*obj
, const char *name
,
819 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
826 qstring
= qobject_to_qstring(ret
);
828 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
831 retval
= g_strdup(qstring_get_str(qstring
));
838 void object_property_set_link(Object
*obj
, Object
*value
,
839 const char *name
, Error
**errp
)
841 gchar
*path
= object_get_canonical_path(value
);
842 object_property_set_str(obj
, path
, name
, errp
);
846 Object
*object_property_get_link(Object
*obj
, const char *name
,
849 char *str
= object_property_get_str(obj
, name
, errp
);
850 Object
*target
= NULL
;
853 target
= object_resolve_path(str
, NULL
);
855 error_set(errp
, QERR_DEVICE_NOT_FOUND
, str
);
863 void object_property_set_bool(Object
*obj
, bool value
,
864 const char *name
, Error
**errp
)
866 QBool
*qbool
= qbool_from_int(value
);
867 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
872 bool object_property_get_bool(Object
*obj
, const char *name
,
875 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
882 qbool
= qobject_to_qbool(ret
);
884 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
887 retval
= qbool_get_int(qbool
);
894 void object_property_set_int(Object
*obj
, int64_t value
,
895 const char *name
, Error
**errp
)
897 QInt
*qint
= qint_from_int(value
);
898 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
903 int64_t object_property_get_int(Object
*obj
, const char *name
,
906 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
913 qint
= qobject_to_qint(ret
);
915 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
918 retval
= qint_get_int(qint
);
925 void object_property_parse(Object
*obj
, const char *string
,
926 const char *name
, Error
**errp
)
928 StringInputVisitor
*mi
;
929 mi
= string_input_visitor_new(string
);
930 object_property_set(obj
, string_input_get_visitor(mi
), name
, errp
);
932 string_input_visitor_cleanup(mi
);
935 char *object_property_print(Object
*obj
, const char *name
,
938 StringOutputVisitor
*mo
;
941 mo
= string_output_visitor_new();
942 object_property_get(obj
, string_output_get_visitor(mo
), name
, errp
);
943 string
= string_output_get_string(mo
);
944 string_output_visitor_cleanup(mo
);
948 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
950 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
958 Object
*object_get_root(void)
963 root
= object_new("container");
969 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
970 const char *name
, Error
**errp
)
972 Object
*child
= opaque
;
975 path
= object_get_canonical_path(child
);
976 visit_type_str(v
, &path
, name
, errp
);
980 static void object_finalize_child_property(Object
*obj
, const char *name
,
983 Object
*child
= opaque
;
988 void object_property_add_child(Object
*obj
, const char *name
,
989 Object
*child
, Error
**errp
)
993 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
995 object_property_add(obj
, name
, type
, object_get_child_property
,
996 NULL
, object_finalize_child_property
, child
, errp
);
999 g_assert(child
->parent
== NULL
);
1000 child
->parent
= obj
;
1005 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1006 const char *name
, Error
**errp
)
1008 Object
**child
= opaque
;
1012 path
= object_get_canonical_path(*child
);
1013 visit_type_str(v
, &path
, name
, errp
);
1017 visit_type_str(v
, &path
, name
, errp
);
1021 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1022 const char *name
, Error
**errp
)
1024 Object
**child
= opaque
;
1026 bool ambiguous
= false;
1031 type
= object_property_get_type(obj
, name
, NULL
);
1033 visit_type_str(v
, &path
, name
, errp
);
1035 old_target
= *child
;
1038 if (strcmp(path
, "") != 0) {
1041 /* Go from link<FOO> to FOO. */
1042 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1043 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1046 error_set(errp
, QERR_AMBIGUOUS_PATH
, path
);
1047 } else if (target
) {
1051 target
= object_resolve_path(path
, &ambiguous
);
1052 if (target
|| ambiguous
) {
1053 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1055 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
1058 g_free(target_type
);
1063 if (old_target
!= NULL
) {
1064 object_unref(old_target
);
1068 void object_property_add_link(Object
*obj
, const char *name
,
1069 const char *type
, Object
**child
,
1074 full_type
= g_strdup_printf("link<%s>", type
);
1076 object_property_add(obj
, name
, full_type
,
1077 object_get_link_property
,
1078 object_set_link_property
,
1084 gchar
*object_get_canonical_path(Object
*obj
)
1086 Object
*root
= object_get_root();
1087 char *newpath
= NULL
, *path
= NULL
;
1089 while (obj
!= root
) {
1090 ObjectProperty
*prop
= NULL
;
1092 g_assert(obj
->parent
!= NULL
);
1094 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
1095 if (!object_property_is_child(prop
)) {
1099 if (prop
->opaque
== obj
) {
1101 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1105 path
= g_strdup(prop
->name
);
1111 g_assert(prop
!= NULL
);
1116 newpath
= g_strdup_printf("/%s", path
);
1122 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1124 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1129 if (object_property_is_link(prop
)) {
1130 return *(Object
**)prop
->opaque
;
1131 } else if (object_property_is_child(prop
)) {
1132 return prop
->opaque
;
1138 static Object
*object_resolve_abs_path(Object
*parent
,
1140 const char *typename
,
1145 if (parts
[index
] == NULL
) {
1146 return object_dynamic_cast(parent
, typename
);
1149 if (strcmp(parts
[index
], "") == 0) {
1150 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1153 child
= object_resolve_path_component(parent
, parts
[index
]);
1158 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1161 static Object
*object_resolve_partial_path(Object
*parent
,
1163 const char *typename
,
1167 ObjectProperty
*prop
;
1169 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1171 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1174 if (!object_property_is_child(prop
)) {
1178 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1179 typename
, ambiguous
);
1190 if (ambiguous
&& *ambiguous
) {
1198 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1204 parts
= g_strsplit(path
, "/", 0);
1207 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1211 obj
= object_resolve_partial_path(object_get_root(), parts
,
1212 typename
, ambiguous
);
1214 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1222 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1224 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1227 typedef struct StringProperty
1229 char *(*get
)(Object
*, Error
**);
1230 void (*set
)(Object
*, const char *, Error
**);
1233 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1234 const char *name
, Error
**errp
)
1236 StringProperty
*prop
= opaque
;
1239 value
= prop
->get(obj
, errp
);
1241 visit_type_str(v
, &value
, name
, errp
);
1246 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1247 const char *name
, Error
**errp
)
1249 StringProperty
*prop
= opaque
;
1251 Error
*local_err
= NULL
;
1253 visit_type_str(v
, &value
, name
, &local_err
);
1255 error_propagate(errp
, local_err
);
1259 prop
->set(obj
, value
, errp
);
1263 static void property_release_str(Object
*obj
, const char *name
,
1266 StringProperty
*prop
= opaque
;
1270 void object_property_add_str(Object
*obj
, const char *name
,
1271 char *(*get
)(Object
*, Error
**),
1272 void (*set
)(Object
*, const char *, Error
**),
1275 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1280 object_property_add(obj
, name
, "string",
1281 get
? property_get_str
: NULL
,
1282 set
? property_set_str
: NULL
,
1283 property_release_str
,
1287 typedef struct BoolProperty
1289 bool (*get
)(Object
*, Error
**);
1290 void (*set
)(Object
*, bool, Error
**);
1293 static void property_get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1294 const char *name
, Error
**errp
)
1296 BoolProperty
*prop
= opaque
;
1299 value
= prop
->get(obj
, errp
);
1300 visit_type_bool(v
, &value
, name
, errp
);
1303 static void property_set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1304 const char *name
, Error
**errp
)
1306 BoolProperty
*prop
= opaque
;
1308 Error
*local_err
= NULL
;
1310 visit_type_bool(v
, &value
, name
, &local_err
);
1312 error_propagate(errp
, local_err
);
1316 prop
->set(obj
, value
, errp
);
1319 static void property_release_bool(Object
*obj
, const char *name
,
1322 BoolProperty
*prop
= opaque
;
1326 void object_property_add_bool(Object
*obj
, const char *name
,
1327 bool (*get
)(Object
*, Error
**),
1328 void (*set
)(Object
*, bool, Error
**),
1331 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1336 object_property_add(obj
, name
, "bool",
1337 get
? property_get_bool
: NULL
,
1338 set
? property_set_bool
: NULL
,
1339 property_release_bool
,
1343 static char *qdev_get_type(Object
*obj
, Error
**errp
)
1345 return g_strdup(object_get_typename(obj
));
1348 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
,
1349 void *opaque
, const char *name
,
1352 uint8_t value
= *(uint8_t *)opaque
;
1353 visit_type_uint8(v
, &value
, name
, errp
);
1356 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
,
1357 void *opaque
, const char *name
,
1360 uint16_t value
= *(uint16_t *)opaque
;
1361 visit_type_uint16(v
, &value
, name
, errp
);
1364 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
,
1365 void *opaque
, const char *name
,
1368 uint32_t value
= *(uint32_t *)opaque
;
1369 visit_type_uint32(v
, &value
, name
, errp
);
1372 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
,
1373 void *opaque
, const char *name
,
1376 uint64_t value
= *(uint64_t *)opaque
;
1377 visit_type_uint64(v
, &value
, name
, errp
);
1380 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
1381 const uint8_t *v
, Error
**errp
)
1383 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
1384 NULL
, NULL
, (void *)v
, errp
);
1387 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
1388 const uint16_t *v
, Error
**errp
)
1390 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
1391 NULL
, NULL
, (void *)v
, errp
);
1394 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
1395 const uint32_t *v
, Error
**errp
)
1397 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
1398 NULL
, NULL
, (void *)v
, errp
);
1401 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
1402 const uint64_t *v
, Error
**errp
)
1404 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
1405 NULL
, NULL
, (void *)v
, errp
);
1408 static void object_instance_init(Object
*obj
)
1410 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
1413 static void register_types(void)
1415 static TypeInfo interface_info
= {
1416 .name
= TYPE_INTERFACE
,
1417 .class_size
= sizeof(InterfaceClass
),
1421 static TypeInfo object_info
= {
1422 .name
= TYPE_OBJECT
,
1423 .instance_size
= sizeof(Object
),
1424 .instance_init
= object_instance_init
,
1428 type_interface
= type_register_internal(&interface_info
);
1429 type_register_internal(&object_info
);
1432 type_init(register_types
)