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"
20 /* TODO: replace QObject with a simpler visitor to avoid a dependency
21 * of the QOM core on QObject? */
22 #include "qom/qom-qobject.h"
23 #include "qapi/qmp/qobject.h"
24 #include "qapi/qmp/qbool.h"
25 #include "qapi/qmp/qint.h"
26 #include "qapi/qmp/qstring.h"
28 #define MAX_INTERFACES 32
30 typedef struct InterfaceImpl InterfaceImpl
;
31 typedef struct TypeImpl TypeImpl
;
46 void (*class_init
)(ObjectClass
*klass
, void *data
);
47 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
48 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
52 void (*instance_init
)(Object
*obj
);
53 void (*instance_finalize
)(Object
*obj
);
58 TypeImpl
*parent_type
;
63 InterfaceImpl interfaces
[MAX_INTERFACES
];
66 static Type type_interface
;
68 static GHashTable
*type_table_get(void)
70 static GHashTable
*type_table
;
72 if (type_table
== NULL
) {
73 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
79 static void type_table_add(TypeImpl
*ti
)
81 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
84 static TypeImpl
*type_table_lookup(const char *name
)
86 return g_hash_table_lookup(type_table_get(), name
);
89 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
91 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
94 g_assert(info
->name
!= NULL
);
96 if (type_table_lookup(info
->name
) != NULL
) {
97 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
101 ti
->name
= g_strdup(info
->name
);
102 ti
->parent
= g_strdup(info
->parent
);
104 ti
->class_size
= info
->class_size
;
105 ti
->instance_size
= info
->instance_size
;
107 ti
->class_init
= info
->class_init
;
108 ti
->class_base_init
= info
->class_base_init
;
109 ti
->class_finalize
= info
->class_finalize
;
110 ti
->class_data
= info
->class_data
;
112 ti
->instance_init
= info
->instance_init
;
113 ti
->instance_finalize
= info
->instance_finalize
;
115 ti
->abstract
= info
->abstract
;
117 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
118 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
120 ti
->num_interfaces
= i
;
127 TypeImpl
*type_register(const TypeInfo
*info
)
129 assert(info
->parent
);
130 return type_register_internal(info
);
133 TypeImpl
*type_register_static(const TypeInfo
*info
)
135 return type_register(info
);
138 static TypeImpl
*type_get_by_name(const char *name
)
144 return type_table_lookup(name
);
147 static TypeImpl
*type_get_parent(TypeImpl
*type
)
149 if (!type
->parent_type
&& type
->parent
) {
150 type
->parent_type
= type_get_by_name(type
->parent
);
151 g_assert(type
->parent_type
!= NULL
);
154 return type
->parent_type
;
157 static bool type_has_parent(TypeImpl
*type
)
159 return (type
->parent
!= NULL
);
162 static size_t type_class_get_size(TypeImpl
*ti
)
164 if (ti
->class_size
) {
165 return ti
->class_size
;
168 if (type_has_parent(ti
)) {
169 return type_class_get_size(type_get_parent(ti
));
172 return sizeof(ObjectClass
);
175 static size_t type_object_get_size(TypeImpl
*ti
)
177 if (ti
->instance_size
) {
178 return ti
->instance_size
;
181 if (type_has_parent(ti
)) {
182 return type_object_get_size(type_get_parent(ti
));
188 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
192 /* Check if typename is a direct ancestor of type */
194 if (type
== target_type
) {
198 type
= type_get_parent(type
);
204 static void type_initialize(TypeImpl
*ti
);
206 static void type_initialize_interface(TypeImpl
*ti
, const char *parent
)
208 InterfaceClass
*new_iface
;
210 TypeImpl
*iface_impl
;
212 info
.parent
= parent
;
213 info
.name
= g_strdup_printf("%s::%s", ti
->name
, info
.parent
);
214 info
.abstract
= true;
216 iface_impl
= type_register(&info
);
217 type_initialize(iface_impl
);
218 g_free((char *)info
.name
);
220 new_iface
= (InterfaceClass
*)iface_impl
->class;
221 new_iface
->concrete_class
= ti
->class;
223 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
227 static void type_initialize(TypeImpl
*ti
)
235 ti
->class_size
= type_class_get_size(ti
);
236 ti
->instance_size
= type_object_get_size(ti
);
238 ti
->class = g_malloc0(ti
->class_size
);
240 parent
= type_get_parent(ti
);
242 type_initialize(parent
);
246 g_assert(parent
->class_size
<= ti
->class_size
);
247 memcpy(ti
->class, parent
->class, parent
->class_size
);
248 ti
->class->interfaces
= NULL
;
250 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
251 ObjectClass
*iface
= e
->data
;
252 type_initialize_interface(ti
, object_class_get_name(iface
));
255 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
256 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
257 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
258 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
260 if (type_is_ancestor(target_type
, t
)) {
269 type_initialize_interface(ti
, ti
->interfaces
[i
].typename
);
273 ti
->class->type
= ti
;
276 if (parent
->class_base_init
) {
277 parent
->class_base_init(ti
->class, ti
->class_data
);
279 parent
= type_get_parent(parent
);
282 if (ti
->class_init
) {
283 ti
->class_init(ti
->class, ti
->class_data
);
289 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
291 if (type_has_parent(ti
)) {
292 object_init_with_type(obj
, type_get_parent(ti
));
295 if (ti
->instance_init
) {
296 ti
->instance_init(obj
);
300 void object_initialize_with_type(void *data
, TypeImpl
*type
)
304 g_assert(type
!= NULL
);
305 type_initialize(type
);
307 g_assert(type
->instance_size
>= sizeof(Object
));
308 g_assert(type
->abstract
== false);
310 memset(obj
, 0, type
->instance_size
);
311 obj
->class = type
->class;
313 QTAILQ_INIT(&obj
->properties
);
314 object_init_with_type(obj
, type
);
317 void object_initialize(void *data
, const char *typename
)
319 TypeImpl
*type
= type_get_by_name(typename
);
321 object_initialize_with_type(data
, type
);
324 static inline bool object_property_is_child(ObjectProperty
*prop
)
326 return strstart(prop
->type
, "child<", NULL
);
329 static inline bool object_property_is_link(ObjectProperty
*prop
)
331 return strstart(prop
->type
, "link<", NULL
);
334 static void object_property_del_all(Object
*obj
)
336 while (!QTAILQ_EMPTY(&obj
->properties
)) {
337 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
339 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
342 prop
->release(obj
, prop
->name
, prop
->opaque
);
351 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
353 ObjectProperty
*prop
;
355 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
356 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
357 object_property_del(obj
, prop
->name
, errp
);
363 void object_unparent(Object
*obj
)
370 if (obj
->class->unparent
) {
371 (obj
->class->unparent
)(obj
);
374 object_property_del_child(obj
->parent
, obj
, NULL
);
379 static void object_deinit(Object
*obj
, TypeImpl
*type
)
381 if (type
->instance_finalize
) {
382 type
->instance_finalize(obj
);
385 if (type_has_parent(type
)) {
386 object_deinit(obj
, type_get_parent(type
));
390 static void object_finalize(void *data
)
393 TypeImpl
*ti
= obj
->class->type
;
395 object_deinit(obj
, ti
);
396 object_property_del_all(obj
);
398 g_assert(obj
->ref
== 0);
404 Object
*object_new_with_type(Type type
)
408 g_assert(type
!= NULL
);
409 type_initialize(type
);
411 obj
= g_malloc(type
->instance_size
);
412 object_initialize_with_type(obj
, type
);
418 Object
*object_new(const char *typename
)
420 TypeImpl
*ti
= type_get_by_name(typename
);
422 return object_new_with_type(ti
);
425 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
427 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
434 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
435 const char *file
, int line
, const char *func
)
439 inst
= object_dynamic_cast(obj
, typename
);
442 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
443 file
, line
, func
, obj
, typename
);
450 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
451 const char *typename
)
453 ObjectClass
*ret
= NULL
;
454 TypeImpl
*target_type
;
461 /* A simple fast path that can trigger a lot for leaf classes. */
463 if (type
->name
== typename
) {
467 target_type
= type_get_by_name(typename
);
469 /* target class type unknown, so fail the cast */
473 if (type
->class->interfaces
&&
474 type_is_ancestor(target_type
, type_interface
)) {
478 for (i
= class->interfaces
; i
; i
= i
->next
) {
479 ObjectClass
*target_class
= i
->data
;
481 if (type_is_ancestor(target_class
->type
, target_type
)) {
487 /* The match was ambiguous, don't allow a cast */
491 } else if (type_is_ancestor(type
, target_type
)) {
498 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
499 const char *typename
,
500 const char *file
, int line
,
503 ObjectClass
*ret
= object_class_dynamic_cast(class, typename
);
506 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
507 file
, line
, func
, class, typename
);
514 const char *object_get_typename(Object
*obj
)
516 return obj
->class->type
->name
;
519 ObjectClass
*object_get_class(Object
*obj
)
524 bool object_class_is_abstract(ObjectClass
*klass
)
526 return klass
->type
->abstract
;
529 const char *object_class_get_name(ObjectClass
*klass
)
531 return klass
->type
->name
;
534 ObjectClass
*object_class_by_name(const char *typename
)
536 TypeImpl
*type
= type_get_by_name(typename
);
542 type_initialize(type
);
547 ObjectClass
*object_class_get_parent(ObjectClass
*class)
549 TypeImpl
*type
= type_get_parent(class->type
);
555 type_initialize(type
);
560 typedef struct OCFData
562 void (*fn
)(ObjectClass
*klass
, void *opaque
);
563 const char *implements_type
;
564 bool include_abstract
;
568 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
571 OCFData
*data
= opaque
;
572 TypeImpl
*type
= value
;
575 type_initialize(type
);
578 if (!data
->include_abstract
&& type
->abstract
) {
582 if (data
->implements_type
&&
583 !object_class_dynamic_cast(k
, data
->implements_type
)) {
587 data
->fn(k
, data
->opaque
);
590 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
591 const char *implements_type
, bool include_abstract
,
594 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
596 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
599 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
602 ObjectProperty
*prop
;
605 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
606 if (object_property_is_child(prop
)) {
607 ret
= fn(prop
->opaque
, opaque
);
616 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
618 GSList
**list
= opaque
;
620 *list
= g_slist_prepend(*list
, klass
);
623 GSList
*object_class_get_list(const char *implements_type
,
624 bool include_abstract
)
628 object_class_foreach(object_class_get_list_tramp
,
629 implements_type
, include_abstract
, &list
);
633 void object_ref(Object
*obj
)
638 void object_unref(Object
*obj
)
640 g_assert(obj
->ref
> 0);
643 /* parent always holds a reference to its children */
645 object_finalize(obj
);
649 void object_property_add(Object
*obj
, const char *name
, const char *type
,
650 ObjectPropertyAccessor
*get
,
651 ObjectPropertyAccessor
*set
,
652 ObjectPropertyRelease
*release
,
653 void *opaque
, Error
**errp
)
655 ObjectProperty
*prop
;
657 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
658 if (strcmp(prop
->name
, name
) == 0) {
659 error_setg(errp
, "attempt to add duplicate property '%s'"
660 " to object (type '%s')", name
,
661 object_get_typename(obj
));
666 prop
= g_malloc0(sizeof(*prop
));
668 prop
->name
= g_strdup(name
);
669 prop
->type
= g_strdup(type
);
673 prop
->release
= release
;
674 prop
->opaque
= opaque
;
676 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
679 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
682 ObjectProperty
*prop
;
684 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
685 if (strcmp(prop
->name
, name
) == 0) {
690 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
694 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
696 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
702 prop
->release(obj
, name
, prop
->opaque
);
705 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
712 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
715 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
721 error_set(errp
, QERR_PERMISSION_DENIED
);
723 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
727 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
730 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
736 error_set(errp
, QERR_PERMISSION_DENIED
);
738 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
742 void object_property_set_str(Object
*obj
, const char *value
,
743 const char *name
, Error
**errp
)
745 QString
*qstr
= qstring_from_str(value
);
746 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
751 char *object_property_get_str(Object
*obj
, const char *name
,
754 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
761 qstring
= qobject_to_qstring(ret
);
763 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
766 retval
= g_strdup(qstring_get_str(qstring
));
773 void object_property_set_link(Object
*obj
, Object
*value
,
774 const char *name
, Error
**errp
)
776 object_property_set_str(obj
, object_get_canonical_path(value
),
780 Object
*object_property_get_link(Object
*obj
, const char *name
,
783 char *str
= object_property_get_str(obj
, name
, errp
);
784 Object
*target
= NULL
;
787 target
= object_resolve_path(str
, NULL
);
789 error_set(errp
, QERR_DEVICE_NOT_FOUND
, str
);
797 void object_property_set_bool(Object
*obj
, bool value
,
798 const char *name
, Error
**errp
)
800 QBool
*qbool
= qbool_from_int(value
);
801 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
806 bool object_property_get_bool(Object
*obj
, const char *name
,
809 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
816 qbool
= qobject_to_qbool(ret
);
818 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
821 retval
= qbool_get_int(qbool
);
828 void object_property_set_int(Object
*obj
, int64_t value
,
829 const char *name
, Error
**errp
)
831 QInt
*qint
= qint_from_int(value
);
832 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
837 int64_t object_property_get_int(Object
*obj
, const char *name
,
840 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
847 qint
= qobject_to_qint(ret
);
849 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
852 retval
= qint_get_int(qint
);
859 void object_property_parse(Object
*obj
, const char *string
,
860 const char *name
, Error
**errp
)
862 StringInputVisitor
*mi
;
863 mi
= string_input_visitor_new(string
);
864 object_property_set(obj
, string_input_get_visitor(mi
), name
, errp
);
866 string_input_visitor_cleanup(mi
);
869 char *object_property_print(Object
*obj
, const char *name
,
872 StringOutputVisitor
*mo
;
875 mo
= string_output_visitor_new();
876 object_property_get(obj
, string_output_get_visitor(mo
), name
, errp
);
877 string
= string_output_get_string(mo
);
878 string_output_visitor_cleanup(mo
);
882 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
884 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
892 Object
*object_get_root(void)
897 root
= object_new("container");
903 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
904 const char *name
, Error
**errp
)
906 Object
*child
= opaque
;
909 path
= object_get_canonical_path(child
);
910 visit_type_str(v
, &path
, name
, errp
);
914 static void object_finalize_child_property(Object
*obj
, const char *name
,
917 Object
*child
= opaque
;
922 void object_property_add_child(Object
*obj
, const char *name
,
923 Object
*child
, Error
**errp
)
927 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
929 object_property_add(obj
, name
, type
, object_get_child_property
,
930 NULL
, object_finalize_child_property
, child
, errp
);
933 g_assert(child
->parent
== NULL
);
939 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
940 const char *name
, Error
**errp
)
942 Object
**child
= opaque
;
946 path
= object_get_canonical_path(*child
);
947 visit_type_str(v
, &path
, name
, errp
);
951 visit_type_str(v
, &path
, name
, errp
);
955 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
956 const char *name
, Error
**errp
)
958 Object
**child
= opaque
;
960 bool ambiguous
= false;
965 type
= object_property_get_type(obj
, name
, NULL
);
967 visit_type_str(v
, &path
, name
, errp
);
972 if (strcmp(path
, "") != 0) {
975 /* Go from link<FOO> to FOO. */
976 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
977 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
980 error_set(errp
, QERR_AMBIGUOUS_PATH
, path
);
985 target
= object_resolve_path(path
, &ambiguous
);
986 if (target
|| ambiguous
) {
987 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
989 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
997 if (old_target
!= NULL
) {
998 object_unref(old_target
);
1002 void object_property_add_link(Object
*obj
, const char *name
,
1003 const char *type
, Object
**child
,
1008 full_type
= g_strdup_printf("link<%s>", type
);
1010 object_property_add(obj
, name
, full_type
,
1011 object_get_link_property
,
1012 object_set_link_property
,
1018 gchar
*object_get_canonical_path(Object
*obj
)
1020 Object
*root
= object_get_root();
1021 char *newpath
= NULL
, *path
= NULL
;
1023 while (obj
!= root
) {
1024 ObjectProperty
*prop
= NULL
;
1026 g_assert(obj
->parent
!= NULL
);
1028 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
1029 if (!object_property_is_child(prop
)) {
1033 if (prop
->opaque
== obj
) {
1035 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1039 path
= g_strdup(prop
->name
);
1045 g_assert(prop
!= NULL
);
1050 newpath
= g_strdup_printf("/%s", path
);
1056 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1058 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1063 if (object_property_is_link(prop
)) {
1064 return *(Object
**)prop
->opaque
;
1065 } else if (object_property_is_child(prop
)) {
1066 return prop
->opaque
;
1072 static Object
*object_resolve_abs_path(Object
*parent
,
1074 const char *typename
,
1079 if (parts
[index
] == NULL
) {
1080 return object_dynamic_cast(parent
, typename
);
1083 if (strcmp(parts
[index
], "") == 0) {
1084 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1087 child
= object_resolve_path_component(parent
, parts
[index
]);
1092 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1095 static Object
*object_resolve_partial_path(Object
*parent
,
1097 const char *typename
,
1101 ObjectProperty
*prop
;
1103 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1105 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1108 if (!object_property_is_child(prop
)) {
1112 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1113 typename
, ambiguous
);
1124 if (ambiguous
&& *ambiguous
) {
1132 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1138 parts
= g_strsplit(path
, "/", 0);
1141 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1145 obj
= object_resolve_partial_path(object_get_root(), parts
,
1146 typename
, ambiguous
);
1148 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1156 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1158 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1161 typedef struct StringProperty
1163 char *(*get
)(Object
*, Error
**);
1164 void (*set
)(Object
*, const char *, Error
**);
1167 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1168 const char *name
, Error
**errp
)
1170 StringProperty
*prop
= opaque
;
1173 value
= prop
->get(obj
, errp
);
1175 visit_type_str(v
, &value
, name
, errp
);
1180 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1181 const char *name
, Error
**errp
)
1183 StringProperty
*prop
= opaque
;
1185 Error
*local_err
= NULL
;
1187 visit_type_str(v
, &value
, name
, &local_err
);
1189 error_propagate(errp
, local_err
);
1193 prop
->set(obj
, value
, errp
);
1197 static void property_release_str(Object
*obj
, const char *name
,
1200 StringProperty
*prop
= opaque
;
1204 void object_property_add_str(Object
*obj
, const char *name
,
1205 char *(*get
)(Object
*, Error
**),
1206 void (*set
)(Object
*, const char *, Error
**),
1209 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1214 object_property_add(obj
, name
, "string",
1215 get
? property_get_str
: NULL
,
1216 set
? property_set_str
: NULL
,
1217 property_release_str
,
1221 typedef struct BoolProperty
1223 bool (*get
)(Object
*, Error
**);
1224 void (*set
)(Object
*, bool, Error
**);
1227 static void property_get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1228 const char *name
, Error
**errp
)
1230 BoolProperty
*prop
= opaque
;
1233 value
= prop
->get(obj
, errp
);
1234 visit_type_bool(v
, &value
, name
, errp
);
1237 static void property_set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1238 const char *name
, Error
**errp
)
1240 BoolProperty
*prop
= opaque
;
1242 Error
*local_err
= NULL
;
1244 visit_type_bool(v
, &value
, name
, &local_err
);
1246 error_propagate(errp
, local_err
);
1250 prop
->set(obj
, value
, errp
);
1253 static void property_release_bool(Object
*obj
, const char *name
,
1256 BoolProperty
*prop
= opaque
;
1260 void object_property_add_bool(Object
*obj
, const char *name
,
1261 bool (*get
)(Object
*, Error
**),
1262 void (*set
)(Object
*, bool, Error
**),
1265 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1270 object_property_add(obj
, name
, "bool",
1271 get
? property_get_bool
: NULL
,
1272 set
? property_set_bool
: NULL
,
1273 property_release_bool
,
1277 static char *qdev_get_type(Object
*obj
, Error
**errp
)
1279 return g_strdup(object_get_typename(obj
));
1282 static void object_instance_init(Object
*obj
)
1284 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
1287 static void register_types(void)
1289 static TypeInfo interface_info
= {
1290 .name
= TYPE_INTERFACE
,
1291 .class_size
= sizeof(InterfaceClass
),
1295 static TypeInfo object_info
= {
1296 .name
= TYPE_OBJECT
,
1297 .instance_size
= sizeof(Object
),
1298 .instance_init
= object_instance_init
,
1302 type_interface
= type_register_internal(&interface_info
);
1303 type_register_internal(&object_info
);
1306 type_init(register_types
)