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
);
249 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
250 ObjectClass
*iface
= e
->data
;
251 type_initialize_interface(ti
, object_class_get_name(iface
));
254 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
255 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
256 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
257 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
259 if (type_is_ancestor(target_type
, t
)) {
268 type_initialize_interface(ti
, ti
->interfaces
[i
].typename
);
272 ti
->class->type
= ti
;
275 if (parent
->class_base_init
) {
276 parent
->class_base_init(ti
->class, ti
->class_data
);
278 parent
= type_get_parent(parent
);
281 if (ti
->class_init
) {
282 ti
->class_init(ti
->class, ti
->class_data
);
288 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
290 if (type_has_parent(ti
)) {
291 object_init_with_type(obj
, type_get_parent(ti
));
294 if (ti
->instance_init
) {
295 ti
->instance_init(obj
);
299 void object_initialize_with_type(void *data
, TypeImpl
*type
)
303 g_assert(type
!= NULL
);
304 type_initialize(type
);
306 g_assert(type
->instance_size
>= sizeof(Object
));
307 g_assert(type
->abstract
== false);
309 memset(obj
, 0, type
->instance_size
);
310 obj
->class = type
->class;
312 QTAILQ_INIT(&obj
->properties
);
313 object_init_with_type(obj
, type
);
316 void object_initialize(void *data
, const char *typename
)
318 TypeImpl
*type
= type_get_by_name(typename
);
320 object_initialize_with_type(data
, type
);
323 static inline bool object_property_is_child(ObjectProperty
*prop
)
325 return strstart(prop
->type
, "child<", NULL
);
328 static inline bool object_property_is_link(ObjectProperty
*prop
)
330 return strstart(prop
->type
, "link<", NULL
);
333 static void object_property_del_all(Object
*obj
)
335 while (!QTAILQ_EMPTY(&obj
->properties
)) {
336 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
338 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
341 prop
->release(obj
, prop
->name
, prop
->opaque
);
350 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
352 ObjectProperty
*prop
;
354 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
355 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
356 object_property_del(obj
, prop
->name
, errp
);
362 void object_unparent(Object
*obj
)
365 object_property_del_child(obj
->parent
, obj
, NULL
);
367 if (obj
->class->unparent
) {
368 (obj
->class->unparent
)(obj
);
372 static void object_deinit(Object
*obj
, TypeImpl
*type
)
374 if (type
->instance_finalize
) {
375 type
->instance_finalize(obj
);
378 if (type_has_parent(type
)) {
379 object_deinit(obj
, type_get_parent(type
));
383 static void object_finalize(void *data
)
386 TypeImpl
*ti
= obj
->class->type
;
388 object_deinit(obj
, ti
);
389 object_property_del_all(obj
);
391 g_assert(obj
->ref
== 0);
397 Object
*object_new_with_type(Type type
)
401 g_assert(type
!= NULL
);
402 type_initialize(type
);
404 obj
= g_malloc(type
->instance_size
);
405 object_initialize_with_type(obj
, type
);
411 Object
*object_new(const char *typename
)
413 TypeImpl
*ti
= type_get_by_name(typename
);
415 return object_new_with_type(ti
);
418 void object_delete(Object
*obj
)
420 object_unparent(obj
);
421 g_assert(obj
->ref
== 1);
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
)
438 inst
= object_dynamic_cast(obj
, typename
);
441 fprintf(stderr
, "Object %p is not an instance of type %s\n",
449 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
450 const char *typename
)
452 TypeImpl
*target_type
= type_get_by_name(typename
);
453 TypeImpl
*type
= class->type
;
454 ObjectClass
*ret
= NULL
;
456 if (type
->num_interfaces
&& type_is_ancestor(target_type
, type_interface
)) {
460 for (i
= class->interfaces
; i
; i
= i
->next
) {
461 ObjectClass
*target_class
= i
->data
;
463 if (type_is_ancestor(target_class
->type
, target_type
)) {
469 /* The match was ambiguous, don't allow a cast */
473 } else if (type_is_ancestor(type
, target_type
)) {
480 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
481 const char *typename
)
483 ObjectClass
*ret
= object_class_dynamic_cast(class, typename
);
486 fprintf(stderr
, "Object %p is not an instance of type %s\n",
494 const char *object_get_typename(Object
*obj
)
496 return obj
->class->type
->name
;
499 ObjectClass
*object_get_class(Object
*obj
)
504 bool object_class_is_abstract(ObjectClass
*klass
)
506 return klass
->type
->abstract
;
509 const char *object_class_get_name(ObjectClass
*klass
)
511 return klass
->type
->name
;
514 ObjectClass
*object_class_by_name(const char *typename
)
516 TypeImpl
*type
= type_get_by_name(typename
);
522 type_initialize(type
);
527 ObjectClass
*object_class_get_parent(ObjectClass
*class)
529 TypeImpl
*type
= type_get_parent(class->type
);
535 type_initialize(type
);
540 typedef struct OCFData
542 void (*fn
)(ObjectClass
*klass
, void *opaque
);
543 const char *implements_type
;
544 bool include_abstract
;
548 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
551 OCFData
*data
= opaque
;
552 TypeImpl
*type
= value
;
555 type_initialize(type
);
558 if (!data
->include_abstract
&& type
->abstract
) {
562 if (data
->implements_type
&&
563 !object_class_dynamic_cast(k
, data
->implements_type
)) {
567 data
->fn(k
, data
->opaque
);
570 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
571 const char *implements_type
, bool include_abstract
,
574 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
576 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
579 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
582 ObjectProperty
*prop
;
585 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
586 if (object_property_is_child(prop
)) {
587 ret
= fn(prop
->opaque
, opaque
);
596 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
598 GSList
**list
= opaque
;
600 *list
= g_slist_prepend(*list
, klass
);
603 GSList
*object_class_get_list(const char *implements_type
,
604 bool include_abstract
)
608 object_class_foreach(object_class_get_list_tramp
,
609 implements_type
, include_abstract
, &list
);
613 void object_ref(Object
*obj
)
618 void object_unref(Object
*obj
)
620 g_assert(obj
->ref
> 0);
623 /* parent always holds a reference to its children */
625 object_finalize(obj
);
629 void object_property_add(Object
*obj
, const char *name
, const char *type
,
630 ObjectPropertyAccessor
*get
,
631 ObjectPropertyAccessor
*set
,
632 ObjectPropertyRelease
*release
,
633 void *opaque
, Error
**errp
)
635 ObjectProperty
*prop
= g_malloc0(sizeof(*prop
));
637 prop
->name
= g_strdup(name
);
638 prop
->type
= g_strdup(type
);
642 prop
->release
= release
;
643 prop
->opaque
= opaque
;
645 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
648 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
651 ObjectProperty
*prop
;
653 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
654 if (strcmp(prop
->name
, name
) == 0) {
659 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
663 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
665 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
671 prop
->release(obj
, name
, prop
->opaque
);
674 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
681 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
684 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
690 error_set(errp
, QERR_PERMISSION_DENIED
);
692 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
696 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
699 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
705 error_set(errp
, QERR_PERMISSION_DENIED
);
707 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
711 void object_property_set_str(Object
*obj
, const char *value
,
712 const char *name
, Error
**errp
)
714 QString
*qstr
= qstring_from_str(value
);
715 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
720 char *object_property_get_str(Object
*obj
, const char *name
,
723 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
730 qstring
= qobject_to_qstring(ret
);
732 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
735 retval
= g_strdup(qstring_get_str(qstring
));
742 void object_property_set_link(Object
*obj
, Object
*value
,
743 const char *name
, Error
**errp
)
745 object_property_set_str(obj
, object_get_canonical_path(value
),
749 Object
*object_property_get_link(Object
*obj
, const char *name
,
752 char *str
= object_property_get_str(obj
, name
, errp
);
753 Object
*target
= NULL
;
756 target
= object_resolve_path(str
, NULL
);
758 error_set(errp
, QERR_DEVICE_NOT_FOUND
, str
);
766 void object_property_set_bool(Object
*obj
, bool value
,
767 const char *name
, Error
**errp
)
769 QBool
*qbool
= qbool_from_int(value
);
770 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
775 bool object_property_get_bool(Object
*obj
, const char *name
,
778 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
785 qbool
= qobject_to_qbool(ret
);
787 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
790 retval
= qbool_get_int(qbool
);
797 void object_property_set_int(Object
*obj
, int64_t value
,
798 const char *name
, Error
**errp
)
800 QInt
*qint
= qint_from_int(value
);
801 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
806 int64_t object_property_get_int(Object
*obj
, const char *name
,
809 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
816 qint
= qobject_to_qint(ret
);
818 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
821 retval
= qint_get_int(qint
);
828 void object_property_parse(Object
*obj
, const char *string
,
829 const char *name
, Error
**errp
)
831 StringInputVisitor
*mi
;
832 mi
= string_input_visitor_new(string
);
833 object_property_set(obj
, string_input_get_visitor(mi
), name
, errp
);
835 string_input_visitor_cleanup(mi
);
838 char *object_property_print(Object
*obj
, const char *name
,
841 StringOutputVisitor
*mo
;
844 mo
= string_output_visitor_new();
845 object_property_get(obj
, string_output_get_visitor(mo
), name
, errp
);
846 string
= string_output_get_string(mo
);
847 string_output_visitor_cleanup(mo
);
851 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
853 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
861 Object
*object_get_root(void)
866 root
= object_new("container");
872 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
873 const char *name
, Error
**errp
)
875 Object
*child
= opaque
;
878 path
= object_get_canonical_path(child
);
879 visit_type_str(v
, &path
, name
, errp
);
883 static void object_finalize_child_property(Object
*obj
, const char *name
,
886 Object
*child
= opaque
;
891 void object_property_add_child(Object
*obj
, const char *name
,
892 Object
*child
, Error
**errp
)
896 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
898 object_property_add(obj
, name
, type
, object_get_child_property
,
899 NULL
, object_finalize_child_property
, child
, errp
);
902 g_assert(child
->parent
== NULL
);
908 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
909 const char *name
, Error
**errp
)
911 Object
**child
= opaque
;
915 path
= object_get_canonical_path(*child
);
916 visit_type_str(v
, &path
, name
, errp
);
920 visit_type_str(v
, &path
, name
, errp
);
924 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
925 const char *name
, Error
**errp
)
927 Object
**child
= opaque
;
929 bool ambiguous
= false;
934 type
= object_property_get_type(obj
, name
, NULL
);
936 visit_type_str(v
, &path
, name
, errp
);
941 if (strcmp(path
, "") != 0) {
944 /* Go from link<FOO> to FOO. */
945 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
946 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
949 error_set(errp
, QERR_AMBIGUOUS_PATH
, path
);
954 target
= object_resolve_path(path
, &ambiguous
);
955 if (target
|| ambiguous
) {
956 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
958 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
966 if (old_target
!= NULL
) {
967 object_unref(old_target
);
971 void object_property_add_link(Object
*obj
, const char *name
,
972 const char *type
, Object
**child
,
977 full_type
= g_strdup_printf("link<%s>", type
);
979 object_property_add(obj
, name
, full_type
,
980 object_get_link_property
,
981 object_set_link_property
,
987 gchar
*object_get_canonical_path(Object
*obj
)
989 Object
*root
= object_get_root();
990 char *newpath
= NULL
, *path
= NULL
;
992 while (obj
!= root
) {
993 ObjectProperty
*prop
= NULL
;
995 g_assert(obj
->parent
!= NULL
);
997 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
998 if (!object_property_is_child(prop
)) {
1002 if (prop
->opaque
== obj
) {
1004 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1008 path
= g_strdup(prop
->name
);
1014 g_assert(prop
!= NULL
);
1019 newpath
= g_strdup_printf("/%s", path
);
1025 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1027 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1032 if (object_property_is_link(prop
)) {
1033 return *(Object
**)prop
->opaque
;
1034 } else if (object_property_is_child(prop
)) {
1035 return prop
->opaque
;
1041 static Object
*object_resolve_abs_path(Object
*parent
,
1043 const char *typename
,
1048 if (parts
[index
] == NULL
) {
1049 return object_dynamic_cast(parent
, typename
);
1052 if (strcmp(parts
[index
], "") == 0) {
1053 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1056 child
= object_resolve_path_component(parent
, parts
[index
]);
1061 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1064 static Object
*object_resolve_partial_path(Object
*parent
,
1066 const char *typename
,
1070 ObjectProperty
*prop
;
1072 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1074 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1077 if (!object_property_is_child(prop
)) {
1081 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1082 typename
, ambiguous
);
1093 if (ambiguous
&& *ambiguous
) {
1101 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1104 bool partial_path
= true;
1108 parts
= g_strsplit(path
, "/", 0);
1109 if (parts
== NULL
|| parts
[0] == NULL
) {
1111 return object_get_root();
1114 if (strcmp(parts
[0], "") == 0) {
1115 partial_path
= false;
1122 obj
= object_resolve_partial_path(object_get_root(), parts
,
1123 typename
, ambiguous
);
1125 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1133 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1135 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1138 typedef struct StringProperty
1140 char *(*get
)(Object
*, Error
**);
1141 void (*set
)(Object
*, const char *, Error
**);
1144 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1145 const char *name
, Error
**errp
)
1147 StringProperty
*prop
= opaque
;
1150 value
= prop
->get(obj
, errp
);
1152 visit_type_str(v
, &value
, name
, errp
);
1157 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1158 const char *name
, Error
**errp
)
1160 StringProperty
*prop
= opaque
;
1162 Error
*local_err
= NULL
;
1164 visit_type_str(v
, &value
, name
, &local_err
);
1166 error_propagate(errp
, local_err
);
1170 prop
->set(obj
, value
, errp
);
1174 static void property_release_str(Object
*obj
, const char *name
,
1177 StringProperty
*prop
= opaque
;
1181 void object_property_add_str(Object
*obj
, const char *name
,
1182 char *(*get
)(Object
*, Error
**),
1183 void (*set
)(Object
*, const char *, Error
**),
1186 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1191 object_property_add(obj
, name
, "string",
1192 get
? property_get_str
: NULL
,
1193 set
? property_set_str
: NULL
,
1194 property_release_str
,
1198 typedef struct BoolProperty
1200 bool (*get
)(Object
*, Error
**);
1201 void (*set
)(Object
*, bool, Error
**);
1204 static void property_get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1205 const char *name
, Error
**errp
)
1207 BoolProperty
*prop
= opaque
;
1210 value
= prop
->get(obj
, errp
);
1211 visit_type_bool(v
, &value
, name
, errp
);
1214 static void property_set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1215 const char *name
, Error
**errp
)
1217 BoolProperty
*prop
= opaque
;
1219 Error
*local_err
= NULL
;
1221 visit_type_bool(v
, &value
, name
, &local_err
);
1223 error_propagate(errp
, local_err
);
1227 prop
->set(obj
, value
, errp
);
1230 static void property_release_bool(Object
*obj
, const char *name
,
1233 BoolProperty
*prop
= opaque
;
1237 void object_property_add_bool(Object
*obj
, const char *name
,
1238 bool (*get
)(Object
*, Error
**),
1239 void (*set
)(Object
*, bool, Error
**),
1242 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1247 object_property_add(obj
, name
, "bool",
1248 get
? property_get_bool
: NULL
,
1249 set
? property_set_bool
: NULL
,
1250 property_release_bool
,
1254 static char *qdev_get_type(Object
*obj
, Error
**errp
)
1256 return g_strdup(object_get_typename(obj
));
1259 static void object_instance_init(Object
*obj
)
1261 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
1264 static void register_types(void)
1266 static TypeInfo interface_info
= {
1267 .name
= TYPE_INTERFACE
,
1268 .class_size
= sizeof(InterfaceClass
),
1272 static TypeInfo object_info
= {
1273 .name
= TYPE_OBJECT
,
1274 .instance_size
= sizeof(Object
),
1275 .instance_init
= object_instance_init
,
1279 type_interface
= type_register_internal(&interface_info
);
1280 type_register_internal(&object_info
);
1283 type_init(register_types
)