4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/object.h"
14 #include "qemu-common.h"
15 #include "qapi/qapi-visit-core.h"
16 #include "qapi/string-input-visitor.h"
17 #include "qapi/string-output-visitor.h"
19 /* TODO: replace QObject with a simpler visitor to avoid a dependency
20 * of the QOM core on QObject? */
21 #include "qemu/qom-qobject.h"
27 #define MAX_INTERFACES 32
29 typedef struct InterfaceImpl InterfaceImpl
;
30 typedef struct TypeImpl TypeImpl
;
35 void (*interface_initfn
)(ObjectClass
*class, void *data
);
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 typedef struct Interface
73 #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE)
75 static Type type_interface
;
77 static GHashTable
*type_table_get(void)
79 static GHashTable
*type_table
;
81 if (type_table
== NULL
) {
82 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
88 static void type_table_add(TypeImpl
*ti
)
90 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
93 static TypeImpl
*type_table_lookup(const char *name
)
95 return g_hash_table_lookup(type_table_get(), name
);
98 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
100 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
102 g_assert(info
->name
!= NULL
);
104 if (type_table_lookup(info
->name
) != NULL
) {
105 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
109 ti
->name
= g_strdup(info
->name
);
110 ti
->parent
= g_strdup(info
->parent
);
112 ti
->class_size
= info
->class_size
;
113 ti
->instance_size
= info
->instance_size
;
115 ti
->class_init
= info
->class_init
;
116 ti
->class_base_init
= info
->class_base_init
;
117 ti
->class_finalize
= info
->class_finalize
;
118 ti
->class_data
= info
->class_data
;
120 ti
->instance_init
= info
->instance_init
;
121 ti
->instance_finalize
= info
->instance_finalize
;
123 ti
->abstract
= info
->abstract
;
125 if (info
->interfaces
) {
128 for (i
= 0; info
->interfaces
[i
].type
; i
++) {
129 ti
->interfaces
[i
].parent
= info
->interfaces
[i
].type
;
130 ti
->interfaces
[i
].interface_initfn
= info
->interfaces
[i
].interface_initfn
;
131 ti
->num_interfaces
++;
140 TypeImpl
*type_register(const TypeInfo
*info
)
142 assert(info
->parent
);
143 return type_register_internal(info
);
146 TypeImpl
*type_register_static(const TypeInfo
*info
)
148 return type_register(info
);
151 static TypeImpl
*type_get_by_name(const char *name
)
157 return type_table_lookup(name
);
160 static TypeImpl
*type_get_parent(TypeImpl
*type
)
162 if (!type
->parent_type
&& type
->parent
) {
163 type
->parent_type
= type_get_by_name(type
->parent
);
164 g_assert(type
->parent_type
!= NULL
);
167 return type
->parent_type
;
170 static bool type_has_parent(TypeImpl
*type
)
172 return (type
->parent
!= NULL
);
175 static size_t type_class_get_size(TypeImpl
*ti
)
177 if (ti
->class_size
) {
178 return ti
->class_size
;
181 if (type_has_parent(ti
)) {
182 return type_class_get_size(type_get_parent(ti
));
185 return sizeof(ObjectClass
);
188 static size_t type_object_get_size(TypeImpl
*ti
)
190 if (ti
->instance_size
) {
191 return ti
->instance_size
;
194 if (type_has_parent(ti
)) {
195 return type_object_get_size(type_get_parent(ti
));
201 static void type_class_interface_init(TypeImpl
*ti
, InterfaceImpl
*iface
)
204 .instance_size
= sizeof(Interface
),
205 .parent
= iface
->parent
,
206 .class_size
= sizeof(InterfaceClass
),
207 .class_init
= iface
->interface_initfn
,
210 char *name
= g_strdup_printf("<%s::%s>", ti
->name
, iface
->parent
);
213 iface
->type
= type_register_internal(&info
);
217 static void type_initialize(TypeImpl
*ti
)
226 ti
->class_size
= type_class_get_size(ti
);
227 ti
->instance_size
= type_object_get_size(ti
);
229 ti
->class = g_malloc0(ti
->class_size
);
231 parent
= type_get_parent(ti
);
233 type_initialize(parent
);
235 g_assert(parent
->class_size
<= ti
->class_size
);
236 memcpy(ti
->class, parent
->class, parent
->class_size
);
239 ti
->class->type
= ti
;
242 if (parent
->class_base_init
) {
243 parent
->class_base_init(ti
->class, ti
->class_data
);
245 parent
= type_get_parent(parent
);
248 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
249 type_class_interface_init(ti
, &ti
->interfaces
[i
]);
252 if (ti
->class_init
) {
253 ti
->class_init(ti
->class, ti
->class_data
);
257 static void object_interface_init(Object
*obj
, InterfaceImpl
*iface
)
259 TypeImpl
*ti
= iface
->type
;
260 Interface
*iface_obj
;
262 iface_obj
= INTERFACE(object_new(ti
->name
));
263 iface_obj
->obj
= obj
;
265 obj
->interfaces
= g_slist_prepend(obj
->interfaces
, iface_obj
);
268 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
272 if (type_has_parent(ti
)) {
273 object_init_with_type(obj
, type_get_parent(ti
));
276 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
277 object_interface_init(obj
, &ti
->interfaces
[i
]);
280 if (ti
->instance_init
) {
281 ti
->instance_init(obj
);
285 void object_initialize_with_type(void *data
, TypeImpl
*type
)
289 g_assert(type
!= NULL
);
290 type_initialize(type
);
292 g_assert(type
->instance_size
>= sizeof(Object
));
293 g_assert(type
->abstract
== false);
295 memset(obj
, 0, type
->instance_size
);
296 obj
->class = type
->class;
297 QTAILQ_INIT(&obj
->properties
);
298 object_init_with_type(obj
, type
);
301 void object_initialize(void *data
, const char *typename
)
303 TypeImpl
*type
= type_get_by_name(typename
);
305 object_initialize_with_type(data
, type
);
308 static inline bool object_property_is_child(ObjectProperty
*prop
)
310 return strstart(prop
->type
, "child<", NULL
);
313 static inline bool object_property_is_link(ObjectProperty
*prop
)
315 return strstart(prop
->type
, "link<", NULL
);
318 static void object_property_del_all(Object
*obj
)
320 while (!QTAILQ_EMPTY(&obj
->properties
)) {
321 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
323 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
326 prop
->release(obj
, prop
->name
, prop
->opaque
);
335 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
337 ObjectProperty
*prop
;
339 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
340 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
341 object_property_del(obj
, prop
->name
, errp
);
347 void object_unparent(Object
*obj
)
350 object_property_del_child(obj
->parent
, obj
, NULL
);
354 static void object_deinit(Object
*obj
, TypeImpl
*type
)
356 if (type
->instance_finalize
) {
357 type
->instance_finalize(obj
);
360 while (obj
->interfaces
) {
361 Interface
*iface_obj
= obj
->interfaces
->data
;
362 obj
->interfaces
= g_slist_delete_link(obj
->interfaces
, obj
->interfaces
);
363 object_delete(OBJECT(iface_obj
));
366 if (type_has_parent(type
)) {
367 object_deinit(obj
, type_get_parent(type
));
370 object_unparent(obj
);
373 void object_finalize(void *data
)
376 TypeImpl
*ti
= obj
->class->type
;
378 object_deinit(obj
, ti
);
379 object_property_del_all(obj
);
381 g_assert(obj
->ref
== 0);
384 Object
*object_new_with_type(Type type
)
388 g_assert(type
!= NULL
);
389 type_initialize(type
);
391 obj
= g_malloc(type
->instance_size
);
392 object_initialize_with_type(obj
, type
);
398 Object
*object_new(const char *typename
)
400 TypeImpl
*ti
= type_get_by_name(typename
);
402 return object_new_with_type(ti
);
405 void object_delete(Object
*obj
)
408 g_assert(obj
->ref
== 0);
412 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
416 /* Check if typename is a direct ancestor of type */
418 if (type
== target_type
) {
422 type
= type_get_parent(type
);
428 static bool object_is_type(Object
*obj
, TypeImpl
*target_type
)
430 return !target_type
|| type_is_ancestor(obj
->class->type
, target_type
);
433 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
435 TypeImpl
*target_type
= type_get_by_name(typename
);
438 /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT,
439 * we want to go back from interfaces to the parent.
441 if (target_type
&& object_is_type(obj
, target_type
)) {
445 /* Check if obj is an interface and its containing object is a direct
446 * ancestor of typename. In principle we could do this test at the very
447 * beginning of object_dynamic_cast, avoiding a second call to
448 * object_is_type. However, casting between interfaces is relatively
449 * rare, and object_is_type(obj, type_interface) would fail almost always.
451 * Perhaps we could add a magic value to the object header for increased
452 * (run-time) type safety and to speed up tests like this one. If we ever
453 * do that we can revisit the order here.
455 if (object_is_type(obj
, type_interface
)) {
456 assert(!obj
->interfaces
);
457 obj
= INTERFACE(obj
)->obj
;
458 if (object_is_type(obj
, target_type
)) {
467 /* Check if obj has an interface of typename */
468 for (i
= obj
->interfaces
; i
; i
= i
->next
) {
469 Interface
*iface
= i
->data
;
471 if (object_is_type(OBJECT(iface
), target_type
)) {
472 return OBJECT(iface
);
480 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
)
484 inst
= object_dynamic_cast(obj
, typename
);
487 fprintf(stderr
, "Object %p is not an instance of type %s\n",
495 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
496 const char *typename
)
498 TypeImpl
*target_type
= type_get_by_name(typename
);
499 TypeImpl
*type
= class->type
;
502 if (type
== target_type
) {
506 type
= type_get_parent(type
);
512 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
513 const char *typename
)
515 ObjectClass
*ret
= object_class_dynamic_cast(class, typename
);
518 fprintf(stderr
, "Object %p is not an instance of type %s\n",
526 const char *object_get_typename(Object
*obj
)
528 return obj
->class->type
->name
;
531 ObjectClass
*object_get_class(Object
*obj
)
536 const char *object_class_get_name(ObjectClass
*klass
)
538 return klass
->type
->name
;
541 ObjectClass
*object_class_by_name(const char *typename
)
543 TypeImpl
*type
= type_get_by_name(typename
);
549 type_initialize(type
);
554 ObjectClass
*object_class_get_parent(ObjectClass
*class)
556 TypeImpl
*type
= type_get_parent(class->type
);
562 type_initialize(type
);
567 typedef struct OCFData
569 void (*fn
)(ObjectClass
*klass
, void *opaque
);
570 const char *implements_type
;
571 bool include_abstract
;
575 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
578 OCFData
*data
= opaque
;
579 TypeImpl
*type
= value
;
582 type_initialize(type
);
585 if (!data
->include_abstract
&& type
->abstract
) {
589 if (data
->implements_type
&&
590 !object_class_dynamic_cast(k
, data
->implements_type
)) {
594 data
->fn(k
, data
->opaque
);
597 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
598 const char *implements_type
, bool include_abstract
,
601 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
603 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
606 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
609 ObjectProperty
*prop
;
612 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
613 if (object_property_is_child(prop
)) {
614 ret
= fn(prop
->opaque
, opaque
);
623 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
625 GSList
**list
= opaque
;
627 *list
= g_slist_prepend(*list
, klass
);
630 GSList
*object_class_get_list(const char *implements_type
,
631 bool include_abstract
)
635 object_class_foreach(object_class_get_list_tramp
,
636 implements_type
, include_abstract
, &list
);
640 void object_ref(Object
*obj
)
645 void object_unref(Object
*obj
)
647 g_assert(obj
->ref
> 0);
650 /* parent always holds a reference to its children */
652 object_finalize(obj
);
656 void object_property_add(Object
*obj
, const char *name
, const char *type
,
657 ObjectPropertyAccessor
*get
,
658 ObjectPropertyAccessor
*set
,
659 ObjectPropertyRelease
*release
,
660 void *opaque
, Error
**errp
)
662 ObjectProperty
*prop
= g_malloc0(sizeof(*prop
));
664 prop
->name
= g_strdup(name
);
665 prop
->type
= g_strdup(type
);
669 prop
->release
= release
;
670 prop
->opaque
= opaque
;
672 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
675 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
678 ObjectProperty
*prop
;
680 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
681 if (strcmp(prop
->name
, name
) == 0) {
686 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
690 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
692 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
698 prop
->release(obj
, name
, prop
->opaque
);
701 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
708 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
711 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
717 error_set(errp
, QERR_PERMISSION_DENIED
);
719 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
723 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
726 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
732 error_set(errp
, QERR_PERMISSION_DENIED
);
734 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
738 void object_property_set_str(Object
*obj
, const char *value
,
739 const char *name
, Error
**errp
)
741 QString
*qstr
= qstring_from_str(value
);
742 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
747 char *object_property_get_str(Object
*obj
, const char *name
,
750 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
757 qstring
= qobject_to_qstring(ret
);
759 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
762 retval
= g_strdup(qstring_get_str(qstring
));
769 void object_property_set_link(Object
*obj
, Object
*value
,
770 const char *name
, Error
**errp
)
772 object_property_set_str(obj
, object_get_canonical_path(value
),
776 Object
*object_property_get_link(Object
*obj
, const char *name
,
779 char *str
= object_property_get_str(obj
, name
, errp
);
780 Object
*target
= NULL
;
783 target
= object_resolve_path(str
, NULL
);
785 error_set(errp
, QERR_DEVICE_NOT_FOUND
, str
);
793 void object_property_set_bool(Object
*obj
, bool value
,
794 const char *name
, Error
**errp
)
796 QBool
*qbool
= qbool_from_int(value
);
797 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
802 bool object_property_get_bool(Object
*obj
, const char *name
,
805 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
812 qbool
= qobject_to_qbool(ret
);
814 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
817 retval
= qbool_get_int(qbool
);
824 void object_property_set_int(Object
*obj
, int64_t value
,
825 const char *name
, Error
**errp
)
827 QInt
*qint
= qint_from_int(value
);
828 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
833 int64_t object_property_get_int(Object
*obj
, const char *name
,
836 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
843 qint
= qobject_to_qint(ret
);
845 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
848 retval
= qint_get_int(qint
);
855 void object_property_parse(Object
*obj
, const char *string
,
856 const char *name
, Error
**errp
)
858 StringInputVisitor
*mi
;
859 mi
= string_input_visitor_new(string
);
860 object_property_set(obj
, string_input_get_visitor(mi
), name
, errp
);
862 string_input_visitor_cleanup(mi
);
865 char *object_property_print(Object
*obj
, const char *name
,
868 StringOutputVisitor
*mo
;
871 mo
= string_output_visitor_new();
872 object_property_get(obj
, string_output_get_visitor(mo
), name
, errp
);
873 string
= string_output_get_string(mo
);
874 string_output_visitor_cleanup(mo
);
878 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
880 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
888 Object
*object_get_root(void)
893 root
= object_new("container");
899 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
900 const char *name
, Error
**errp
)
902 Object
*child
= opaque
;
905 path
= object_get_canonical_path(child
);
906 visit_type_str(v
, &path
, name
, errp
);
910 static void object_finalize_child_property(Object
*obj
, const char *name
,
913 Object
*child
= opaque
;
918 void object_property_add_child(Object
*obj
, const char *name
,
919 Object
*child
, Error
**errp
)
923 /* Registering an interface object in the composition tree will mightily
924 * confuse object_get_canonical_path (which, on the other hand, knows how
925 * to get the canonical path of an interface object).
927 assert(!object_is_type(obj
, type_interface
));
929 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
931 object_property_add(obj
, name
, type
, object_get_child_property
,
932 NULL
, object_finalize_child_property
, child
, errp
);
935 g_assert(child
->parent
== NULL
);
941 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
942 const char *name
, Error
**errp
)
944 Object
**child
= opaque
;
948 path
= object_get_canonical_path(*child
);
949 visit_type_str(v
, &path
, name
, errp
);
953 visit_type_str(v
, &path
, name
, errp
);
957 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
958 const char *name
, Error
**errp
)
960 Object
**child
= opaque
;
962 bool ambiguous
= false;
967 type
= object_property_get_type(obj
, name
, NULL
);
969 visit_type_str(v
, &path
, name
, errp
);
974 if (strcmp(path
, "") != 0) {
977 /* Go from link<FOO> to FOO. */
978 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
979 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
982 error_set(errp
, QERR_AMBIGUOUS_PATH
, path
);
987 target
= object_resolve_path(path
, &ambiguous
);
988 if (target
|| ambiguous
) {
989 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
991 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
999 if (old_target
!= NULL
) {
1000 object_unref(old_target
);
1004 void object_property_add_link(Object
*obj
, const char *name
,
1005 const char *type
, Object
**child
,
1010 full_type
= g_strdup_printf("link<%s>", type
);
1012 object_property_add(obj
, name
, full_type
,
1013 object_get_link_property
,
1014 object_set_link_property
,
1020 gchar
*object_get_canonical_path(Object
*obj
)
1022 Object
*root
= object_get_root();
1023 char *newpath
= NULL
, *path
= NULL
;
1025 if (object_is_type(obj
, type_interface
)) {
1026 obj
= INTERFACE(obj
)->obj
;
1029 while (obj
!= root
) {
1030 ObjectProperty
*prop
= NULL
;
1032 g_assert(obj
->parent
!= NULL
);
1034 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
1035 if (!object_property_is_child(prop
)) {
1039 if (prop
->opaque
== obj
) {
1041 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1045 path
= g_strdup(prop
->name
);
1051 g_assert(prop
!= NULL
);
1056 newpath
= g_strdup_printf("/%s", path
);
1062 Object
*object_resolve_path_component(Object
*parent
, gchar
*part
)
1064 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1069 if (object_property_is_link(prop
)) {
1070 return *(Object
**)prop
->opaque
;
1071 } else if (object_property_is_child(prop
)) {
1072 return prop
->opaque
;
1078 static Object
*object_resolve_abs_path(Object
*parent
,
1080 const char *typename
,
1085 if (parts
[index
] == NULL
) {
1086 return object_dynamic_cast(parent
, typename
);
1089 if (strcmp(parts
[index
], "") == 0) {
1090 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1093 child
= object_resolve_path_component(parent
, parts
[index
]);
1098 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1101 static Object
*object_resolve_partial_path(Object
*parent
,
1103 const char *typename
,
1107 ObjectProperty
*prop
;
1109 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1111 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1114 if (!object_property_is_child(prop
)) {
1118 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1119 typename
, ambiguous
);
1130 if (ambiguous
&& *ambiguous
) {
1138 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1141 bool partial_path
= true;
1145 parts
= g_strsplit(path
, "/", 0);
1146 if (parts
== NULL
|| parts
[0] == NULL
) {
1148 return object_get_root();
1151 if (strcmp(parts
[0], "") == 0) {
1152 partial_path
= false;
1159 obj
= object_resolve_partial_path(object_get_root(), parts
,
1160 typename
, ambiguous
);
1162 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1170 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1172 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1175 typedef struct StringProperty
1177 char *(*get
)(Object
*, Error
**);
1178 void (*set
)(Object
*, const char *, Error
**);
1181 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1182 const char *name
, Error
**errp
)
1184 StringProperty
*prop
= opaque
;
1187 value
= prop
->get(obj
, errp
);
1189 visit_type_str(v
, &value
, name
, errp
);
1194 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1195 const char *name
, Error
**errp
)
1197 StringProperty
*prop
= opaque
;
1199 Error
*local_err
= NULL
;
1201 visit_type_str(v
, &value
, name
, &local_err
);
1203 error_propagate(errp
, local_err
);
1207 prop
->set(obj
, value
, errp
);
1211 static void property_release_str(Object
*obj
, const char *name
,
1214 StringProperty
*prop
= opaque
;
1218 void object_property_add_str(Object
*obj
, const char *name
,
1219 char *(*get
)(Object
*, Error
**),
1220 void (*set
)(Object
*, const char *, Error
**),
1223 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1228 object_property_add(obj
, name
, "string",
1229 get
? property_get_str
: NULL
,
1230 set
? property_set_str
: NULL
,
1231 property_release_str
,
1235 static char *qdev_get_type(Object
*obj
, Error
**errp
)
1237 return g_strdup(object_get_typename(obj
));
1240 static void object_instance_init(Object
*obj
)
1242 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
1245 static void register_types(void)
1247 static TypeInfo interface_info
= {
1248 .name
= TYPE_INTERFACE
,
1249 .instance_size
= sizeof(Interface
),
1253 static TypeInfo object_info
= {
1254 .name
= TYPE_OBJECT
,
1255 .instance_size
= sizeof(Object
),
1256 .instance_init
= object_instance_init
,
1260 type_interface
= type_register_internal(&interface_info
);
1261 type_register_internal(&object_info
);
1264 type_init(register_types
)