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_finalize
)(Object
*obj
);
59 TypeImpl
*parent_type
;
64 InterfaceImpl interfaces
[MAX_INTERFACES
];
67 static Type type_interface
;
69 static GHashTable
*type_table_get(void)
71 static GHashTable
*type_table
;
73 if (type_table
== NULL
) {
74 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
80 static void type_table_add(TypeImpl
*ti
)
82 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
85 static TypeImpl
*type_table_lookup(const char *name
)
87 return g_hash_table_lookup(type_table_get(), name
);
90 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
92 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
95 g_assert(info
->name
!= NULL
);
97 if (type_table_lookup(info
->name
) != NULL
) {
98 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
102 ti
->name
= g_strdup(info
->name
);
103 ti
->parent
= g_strdup(info
->parent
);
105 ti
->class_size
= info
->class_size
;
106 ti
->instance_size
= info
->instance_size
;
108 ti
->class_init
= info
->class_init
;
109 ti
->class_base_init
= info
->class_base_init
;
110 ti
->class_finalize
= info
->class_finalize
;
111 ti
->class_data
= info
->class_data
;
113 ti
->instance_init
= info
->instance_init
;
114 ti
->instance_finalize
= info
->instance_finalize
;
116 ti
->abstract
= info
->abstract
;
118 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
119 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
121 ti
->num_interfaces
= i
;
128 TypeImpl
*type_register(const TypeInfo
*info
)
130 assert(info
->parent
);
131 return type_register_internal(info
);
134 TypeImpl
*type_register_static(const TypeInfo
*info
)
136 return type_register(info
);
139 static TypeImpl
*type_get_by_name(const char *name
)
145 return type_table_lookup(name
);
148 static TypeImpl
*type_get_parent(TypeImpl
*type
)
150 if (!type
->parent_type
&& type
->parent
) {
151 type
->parent_type
= type_get_by_name(type
->parent
);
152 g_assert(type
->parent_type
!= NULL
);
155 return type
->parent_type
;
158 static bool type_has_parent(TypeImpl
*type
)
160 return (type
->parent
!= NULL
);
163 static size_t type_class_get_size(TypeImpl
*ti
)
165 if (ti
->class_size
) {
166 return ti
->class_size
;
169 if (type_has_parent(ti
)) {
170 return type_class_get_size(type_get_parent(ti
));
173 return sizeof(ObjectClass
);
176 static size_t type_object_get_size(TypeImpl
*ti
)
178 if (ti
->instance_size
) {
179 return ti
->instance_size
;
182 if (type_has_parent(ti
)) {
183 return type_object_get_size(type_get_parent(ti
));
189 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
193 /* Check if typename is a direct ancestor of type */
195 if (type
== target_type
) {
199 type
= type_get_parent(type
);
205 static void type_initialize(TypeImpl
*ti
);
207 static void type_initialize_interface(TypeImpl
*ti
, const char *parent
)
209 InterfaceClass
*new_iface
;
211 TypeImpl
*iface_impl
;
213 info
.parent
= parent
;
214 info
.name
= g_strdup_printf("%s::%s", ti
->name
, info
.parent
);
215 info
.abstract
= true;
217 iface_impl
= type_register(&info
);
218 type_initialize(iface_impl
);
219 g_free((char *)info
.name
);
221 new_iface
= (InterfaceClass
*)iface_impl
->class;
222 new_iface
->concrete_class
= ti
->class;
224 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
228 static void type_initialize(TypeImpl
*ti
)
236 ti
->class_size
= type_class_get_size(ti
);
237 ti
->instance_size
= type_object_get_size(ti
);
239 ti
->class = g_malloc0(ti
->class_size
);
241 parent
= type_get_parent(ti
);
243 type_initialize(parent
);
247 g_assert(parent
->class_size
<= ti
->class_size
);
248 memcpy(ti
->class, parent
->class, parent
->class_size
);
249 ti
->class->interfaces
= NULL
;
251 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
252 ObjectClass
*iface
= e
->data
;
253 type_initialize_interface(ti
, object_class_get_name(iface
));
256 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
257 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
258 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
259 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
261 if (type_is_ancestor(target_type
, t
)) {
270 type_initialize_interface(ti
, ti
->interfaces
[i
].typename
);
274 ti
->class->type
= ti
;
277 if (parent
->class_base_init
) {
278 parent
->class_base_init(ti
->class, ti
->class_data
);
280 parent
= type_get_parent(parent
);
283 if (ti
->class_init
) {
284 ti
->class_init(ti
->class, ti
->class_data
);
290 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
292 if (type_has_parent(ti
)) {
293 object_init_with_type(obj
, type_get_parent(ti
));
296 if (ti
->instance_init
) {
297 ti
->instance_init(obj
);
301 void object_initialize_with_type(void *data
, TypeImpl
*type
)
305 g_assert(type
!= NULL
);
306 type_initialize(type
);
308 g_assert(type
->instance_size
>= sizeof(Object
));
309 g_assert(type
->abstract
== false);
311 memset(obj
, 0, type
->instance_size
);
312 obj
->class = type
->class;
314 QTAILQ_INIT(&obj
->properties
);
315 object_init_with_type(obj
, type
);
318 void object_initialize(void *data
, const char *typename
)
320 TypeImpl
*type
= type_get_by_name(typename
);
322 object_initialize_with_type(data
, type
);
325 static inline bool object_property_is_child(ObjectProperty
*prop
)
327 return strstart(prop
->type
, "child<", NULL
);
330 static inline bool object_property_is_link(ObjectProperty
*prop
)
332 return strstart(prop
->type
, "link<", NULL
);
335 static void object_property_del_all(Object
*obj
)
337 while (!QTAILQ_EMPTY(&obj
->properties
)) {
338 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
340 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
343 prop
->release(obj
, prop
->name
, prop
->opaque
);
352 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
354 ObjectProperty
*prop
;
356 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
357 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
358 object_property_del(obj
, prop
->name
, errp
);
364 void object_unparent(Object
*obj
)
371 if (obj
->class->unparent
) {
372 (obj
->class->unparent
)(obj
);
375 object_property_del_child(obj
->parent
, obj
, NULL
);
380 static void object_deinit(Object
*obj
, TypeImpl
*type
)
382 if (type
->instance_finalize
) {
383 type
->instance_finalize(obj
);
386 if (type_has_parent(type
)) {
387 object_deinit(obj
, type_get_parent(type
));
391 static void object_finalize(void *data
)
394 TypeImpl
*ti
= obj
->class->type
;
396 object_deinit(obj
, ti
);
397 object_property_del_all(obj
);
399 g_assert(obj
->ref
== 0);
405 Object
*object_new_with_type(Type type
)
409 g_assert(type
!= NULL
);
410 type_initialize(type
);
412 obj
= g_malloc(type
->instance_size
);
413 object_initialize_with_type(obj
, type
);
419 Object
*object_new(const char *typename
)
421 TypeImpl
*ti
= type_get_by_name(typename
);
423 return object_new_with_type(ti
);
426 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
428 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
435 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
436 const char *file
, int line
, const char *func
)
440 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
441 typename
, file
, line
, func
);
443 inst
= object_dynamic_cast(obj
, typename
);
446 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
447 file
, line
, func
, obj
, typename
);
454 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
455 const char *typename
)
457 ObjectClass
*ret
= NULL
;
458 TypeImpl
*target_type
;
465 /* A simple fast path that can trigger a lot for leaf classes. */
467 if (type
->name
== typename
) {
471 target_type
= type_get_by_name(typename
);
473 /* target class type unknown, so fail the cast */
477 if (type
->class->interfaces
&&
478 type_is_ancestor(target_type
, type_interface
)) {
482 for (i
= class->interfaces
; i
; i
= i
->next
) {
483 ObjectClass
*target_class
= i
->data
;
485 if (type_is_ancestor(target_class
->type
, target_type
)) {
491 /* The match was ambiguous, don't allow a cast */
495 } else if (type_is_ancestor(type
, target_type
)) {
502 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
503 const char *typename
,
504 const char *file
, int line
,
509 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
510 typename
, file
, line
, func
);
512 ret
= object_class_dynamic_cast(class, typename
);
514 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
515 file
, line
, func
, class, typename
);
522 const char *object_get_typename(Object
*obj
)
524 return obj
->class->type
->name
;
527 ObjectClass
*object_get_class(Object
*obj
)
532 bool object_class_is_abstract(ObjectClass
*klass
)
534 return klass
->type
->abstract
;
537 const char *object_class_get_name(ObjectClass
*klass
)
539 return klass
->type
->name
;
542 ObjectClass
*object_class_by_name(const char *typename
)
544 TypeImpl
*type
= type_get_by_name(typename
);
550 type_initialize(type
);
555 ObjectClass
*object_class_get_parent(ObjectClass
*class)
557 TypeImpl
*type
= type_get_parent(class->type
);
563 type_initialize(type
);
568 typedef struct OCFData
570 void (*fn
)(ObjectClass
*klass
, void *opaque
);
571 const char *implements_type
;
572 bool include_abstract
;
576 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
579 OCFData
*data
= opaque
;
580 TypeImpl
*type
= value
;
583 type_initialize(type
);
586 if (!data
->include_abstract
&& type
->abstract
) {
590 if (data
->implements_type
&&
591 !object_class_dynamic_cast(k
, data
->implements_type
)) {
595 data
->fn(k
, data
->opaque
);
598 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
599 const char *implements_type
, bool include_abstract
,
602 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
604 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
607 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
610 ObjectProperty
*prop
;
613 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
614 if (object_property_is_child(prop
)) {
615 ret
= fn(prop
->opaque
, opaque
);
624 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
626 GSList
**list
= opaque
;
628 *list
= g_slist_prepend(*list
, klass
);
631 GSList
*object_class_get_list(const char *implements_type
,
632 bool include_abstract
)
636 object_class_foreach(object_class_get_list_tramp
,
637 implements_type
, include_abstract
, &list
);
641 void object_ref(Object
*obj
)
646 void object_unref(Object
*obj
)
648 g_assert(obj
->ref
> 0);
651 /* parent always holds a reference to its children */
653 object_finalize(obj
);
657 void object_property_add(Object
*obj
, const char *name
, const char *type
,
658 ObjectPropertyAccessor
*get
,
659 ObjectPropertyAccessor
*set
,
660 ObjectPropertyRelease
*release
,
661 void *opaque
, Error
**errp
)
663 ObjectProperty
*prop
;
665 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
666 if (strcmp(prop
->name
, name
) == 0) {
667 error_setg(errp
, "attempt to add duplicate property '%s'"
668 " to object (type '%s')", name
,
669 object_get_typename(obj
));
674 prop
= g_malloc0(sizeof(*prop
));
676 prop
->name
= g_strdup(name
);
677 prop
->type
= g_strdup(type
);
681 prop
->release
= release
;
682 prop
->opaque
= opaque
;
684 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
687 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
690 ObjectProperty
*prop
;
692 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
693 if (strcmp(prop
->name
, name
) == 0) {
698 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
702 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
704 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
710 prop
->release(obj
, name
, prop
->opaque
);
713 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
720 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
723 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
729 error_set(errp
, QERR_PERMISSION_DENIED
);
731 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
735 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
738 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
744 error_set(errp
, QERR_PERMISSION_DENIED
);
746 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
750 void object_property_set_str(Object
*obj
, const char *value
,
751 const char *name
, Error
**errp
)
753 QString
*qstr
= qstring_from_str(value
);
754 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
759 char *object_property_get_str(Object
*obj
, const char *name
,
762 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
769 qstring
= qobject_to_qstring(ret
);
771 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
774 retval
= g_strdup(qstring_get_str(qstring
));
781 void object_property_set_link(Object
*obj
, Object
*value
,
782 const char *name
, Error
**errp
)
784 object_property_set_str(obj
, object_get_canonical_path(value
),
788 Object
*object_property_get_link(Object
*obj
, const char *name
,
791 char *str
= object_property_get_str(obj
, name
, errp
);
792 Object
*target
= NULL
;
795 target
= object_resolve_path(str
, NULL
);
797 error_set(errp
, QERR_DEVICE_NOT_FOUND
, str
);
805 void object_property_set_bool(Object
*obj
, bool value
,
806 const char *name
, Error
**errp
)
808 QBool
*qbool
= qbool_from_int(value
);
809 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
814 bool object_property_get_bool(Object
*obj
, const char *name
,
817 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
824 qbool
= qobject_to_qbool(ret
);
826 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
829 retval
= qbool_get_int(qbool
);
836 void object_property_set_int(Object
*obj
, int64_t value
,
837 const char *name
, Error
**errp
)
839 QInt
*qint
= qint_from_int(value
);
840 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
845 int64_t object_property_get_int(Object
*obj
, const char *name
,
848 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
855 qint
= qobject_to_qint(ret
);
857 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
860 retval
= qint_get_int(qint
);
867 void object_property_parse(Object
*obj
, const char *string
,
868 const char *name
, Error
**errp
)
870 StringInputVisitor
*mi
;
871 mi
= string_input_visitor_new(string
);
872 object_property_set(obj
, string_input_get_visitor(mi
), name
, errp
);
874 string_input_visitor_cleanup(mi
);
877 char *object_property_print(Object
*obj
, const char *name
,
880 StringOutputVisitor
*mo
;
883 mo
= string_output_visitor_new();
884 object_property_get(obj
, string_output_get_visitor(mo
), name
, errp
);
885 string
= string_output_get_string(mo
);
886 string_output_visitor_cleanup(mo
);
890 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
892 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
900 Object
*object_get_root(void)
905 root
= object_new("container");
911 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
912 const char *name
, Error
**errp
)
914 Object
*child
= opaque
;
917 path
= object_get_canonical_path(child
);
918 visit_type_str(v
, &path
, name
, errp
);
922 static void object_finalize_child_property(Object
*obj
, const char *name
,
925 Object
*child
= opaque
;
930 void object_property_add_child(Object
*obj
, const char *name
,
931 Object
*child
, Error
**errp
)
935 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
937 object_property_add(obj
, name
, type
, object_get_child_property
,
938 NULL
, object_finalize_child_property
, child
, errp
);
941 g_assert(child
->parent
== NULL
);
947 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
948 const char *name
, Error
**errp
)
950 Object
**child
= opaque
;
954 path
= object_get_canonical_path(*child
);
955 visit_type_str(v
, &path
, name
, errp
);
959 visit_type_str(v
, &path
, name
, errp
);
963 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
964 const char *name
, Error
**errp
)
966 Object
**child
= opaque
;
968 bool ambiguous
= false;
973 type
= object_property_get_type(obj
, name
, NULL
);
975 visit_type_str(v
, &path
, name
, errp
);
980 if (strcmp(path
, "") != 0) {
983 /* Go from link<FOO> to FOO. */
984 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
985 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
988 error_set(errp
, QERR_AMBIGUOUS_PATH
, path
);
993 target
= object_resolve_path(path
, &ambiguous
);
994 if (target
|| ambiguous
) {
995 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
997 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
1000 g_free(target_type
);
1005 if (old_target
!= NULL
) {
1006 object_unref(old_target
);
1010 void object_property_add_link(Object
*obj
, const char *name
,
1011 const char *type
, Object
**child
,
1016 full_type
= g_strdup_printf("link<%s>", type
);
1018 object_property_add(obj
, name
, full_type
,
1019 object_get_link_property
,
1020 object_set_link_property
,
1026 gchar
*object_get_canonical_path(Object
*obj
)
1028 Object
*root
= object_get_root();
1029 char *newpath
= NULL
, *path
= NULL
;
1031 while (obj
!= root
) {
1032 ObjectProperty
*prop
= NULL
;
1034 g_assert(obj
->parent
!= NULL
);
1036 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
1037 if (!object_property_is_child(prop
)) {
1041 if (prop
->opaque
== obj
) {
1043 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1047 path
= g_strdup(prop
->name
);
1053 g_assert(prop
!= NULL
);
1058 newpath
= g_strdup_printf("/%s", path
);
1064 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1066 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1071 if (object_property_is_link(prop
)) {
1072 return *(Object
**)prop
->opaque
;
1073 } else if (object_property_is_child(prop
)) {
1074 return prop
->opaque
;
1080 static Object
*object_resolve_abs_path(Object
*parent
,
1082 const char *typename
,
1087 if (parts
[index
] == NULL
) {
1088 return object_dynamic_cast(parent
, typename
);
1091 if (strcmp(parts
[index
], "") == 0) {
1092 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1095 child
= object_resolve_path_component(parent
, parts
[index
]);
1100 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1103 static Object
*object_resolve_partial_path(Object
*parent
,
1105 const char *typename
,
1109 ObjectProperty
*prop
;
1111 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1113 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1116 if (!object_property_is_child(prop
)) {
1120 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1121 typename
, ambiguous
);
1132 if (ambiguous
&& *ambiguous
) {
1140 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1146 parts
= g_strsplit(path
, "/", 0);
1149 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1153 obj
= object_resolve_partial_path(object_get_root(), parts
,
1154 typename
, ambiguous
);
1156 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1164 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1166 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1169 typedef struct StringProperty
1171 char *(*get
)(Object
*, Error
**);
1172 void (*set
)(Object
*, const char *, Error
**);
1175 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1176 const char *name
, Error
**errp
)
1178 StringProperty
*prop
= opaque
;
1181 value
= prop
->get(obj
, errp
);
1183 visit_type_str(v
, &value
, name
, errp
);
1188 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1189 const char *name
, Error
**errp
)
1191 StringProperty
*prop
= opaque
;
1193 Error
*local_err
= NULL
;
1195 visit_type_str(v
, &value
, name
, &local_err
);
1197 error_propagate(errp
, local_err
);
1201 prop
->set(obj
, value
, errp
);
1205 static void property_release_str(Object
*obj
, const char *name
,
1208 StringProperty
*prop
= opaque
;
1212 void object_property_add_str(Object
*obj
, const char *name
,
1213 char *(*get
)(Object
*, Error
**),
1214 void (*set
)(Object
*, const char *, Error
**),
1217 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1222 object_property_add(obj
, name
, "string",
1223 get
? property_get_str
: NULL
,
1224 set
? property_set_str
: NULL
,
1225 property_release_str
,
1229 typedef struct BoolProperty
1231 bool (*get
)(Object
*, Error
**);
1232 void (*set
)(Object
*, bool, Error
**);
1235 static void property_get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1236 const char *name
, Error
**errp
)
1238 BoolProperty
*prop
= opaque
;
1241 value
= prop
->get(obj
, errp
);
1242 visit_type_bool(v
, &value
, name
, errp
);
1245 static void property_set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1246 const char *name
, Error
**errp
)
1248 BoolProperty
*prop
= opaque
;
1250 Error
*local_err
= NULL
;
1252 visit_type_bool(v
, &value
, name
, &local_err
);
1254 error_propagate(errp
, local_err
);
1258 prop
->set(obj
, value
, errp
);
1261 static void property_release_bool(Object
*obj
, const char *name
,
1264 BoolProperty
*prop
= opaque
;
1268 void object_property_add_bool(Object
*obj
, const char *name
,
1269 bool (*get
)(Object
*, Error
**),
1270 void (*set
)(Object
*, bool, Error
**),
1273 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1278 object_property_add(obj
, name
, "bool",
1279 get
? property_get_bool
: NULL
,
1280 set
? property_set_bool
: NULL
,
1281 property_release_bool
,
1285 static char *qdev_get_type(Object
*obj
, Error
**errp
)
1287 return g_strdup(object_get_typename(obj
));
1290 static void object_instance_init(Object
*obj
)
1292 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
1295 static void register_types(void)
1297 static TypeInfo interface_info
= {
1298 .name
= TYPE_INTERFACE
,
1299 .class_size
= sizeof(InterfaceClass
),
1303 static TypeInfo object_info
= {
1304 .name
= TYPE_OBJECT
,
1305 .instance_size
= sizeof(Object
),
1306 .instance_init
= object_instance_init
,
1310 type_interface
= type_register_internal(&interface_info
);
1311 type_register_internal(&object_info
);
1314 type_init(register_types
)