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 TypeImpl
*type_register(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_static(const TypeInfo
*info
)
142 return type_register(info
);
145 static TypeImpl
*type_get_by_name(const char *name
)
151 return type_table_lookup(name
);
154 static TypeImpl
*type_get_parent(TypeImpl
*type
)
156 if (!type
->parent_type
&& type
->parent
) {
157 type
->parent_type
= type_get_by_name(type
->parent
);
158 g_assert(type
->parent_type
!= NULL
);
161 return type
->parent_type
;
164 static bool type_has_parent(TypeImpl
*type
)
166 return (type
->parent
!= NULL
);
169 static size_t type_class_get_size(TypeImpl
*ti
)
171 if (ti
->class_size
) {
172 return ti
->class_size
;
175 if (type_has_parent(ti
)) {
176 return type_class_get_size(type_get_parent(ti
));
179 return sizeof(ObjectClass
);
182 static size_t type_object_get_size(TypeImpl
*ti
)
184 if (ti
->instance_size
) {
185 return ti
->instance_size
;
188 if (type_has_parent(ti
)) {
189 return type_object_get_size(type_get_parent(ti
));
195 static void type_class_interface_init(TypeImpl
*ti
, InterfaceImpl
*iface
)
198 .instance_size
= sizeof(Interface
),
199 .parent
= iface
->parent
,
200 .class_size
= sizeof(InterfaceClass
),
201 .class_init
= iface
->interface_initfn
,
204 char *name
= g_strdup_printf("<%s::%s>", ti
->name
, iface
->parent
);
207 iface
->type
= type_register(&info
);
211 static void type_initialize(TypeImpl
*ti
)
220 ti
->class_size
= type_class_get_size(ti
);
221 ti
->instance_size
= type_object_get_size(ti
);
223 ti
->class = g_malloc0(ti
->class_size
);
225 parent
= type_get_parent(ti
);
227 type_initialize(parent
);
229 g_assert(parent
->class_size
<= ti
->class_size
);
230 memcpy(ti
->class, parent
->class, parent
->class_size
);
233 ti
->class->type
= ti
;
236 if (parent
->class_base_init
) {
237 parent
->class_base_init(ti
->class, ti
->class_data
);
239 parent
= type_get_parent(parent
);
242 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
243 type_class_interface_init(ti
, &ti
->interfaces
[i
]);
246 if (ti
->class_init
) {
247 ti
->class_init(ti
->class, ti
->class_data
);
251 static void object_interface_init(Object
*obj
, InterfaceImpl
*iface
)
253 TypeImpl
*ti
= iface
->type
;
254 Interface
*iface_obj
;
256 iface_obj
= INTERFACE(object_new(ti
->name
));
257 iface_obj
->obj
= obj
;
259 obj
->interfaces
= g_slist_prepend(obj
->interfaces
, iface_obj
);
262 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
266 if (type_has_parent(ti
)) {
267 object_init_with_type(obj
, type_get_parent(ti
));
270 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
271 object_interface_init(obj
, &ti
->interfaces
[i
]);
274 if (ti
->instance_init
) {
275 ti
->instance_init(obj
);
279 void object_initialize_with_type(void *data
, TypeImpl
*type
)
283 g_assert(type
!= NULL
);
284 type_initialize(type
);
286 g_assert(type
->instance_size
>= sizeof(Object
));
287 g_assert(type
->abstract
== false);
289 memset(obj
, 0, type
->instance_size
);
290 obj
->class = type
->class;
291 QTAILQ_INIT(&obj
->properties
);
292 object_init_with_type(obj
, type
);
295 void object_initialize(void *data
, const char *typename
)
297 TypeImpl
*type
= type_get_by_name(typename
);
299 object_initialize_with_type(data
, type
);
302 static inline bool object_property_is_child(ObjectProperty
*prop
)
304 return strstart(prop
->type
, "child<", NULL
);
307 static inline bool object_property_is_link(ObjectProperty
*prop
)
309 return strstart(prop
->type
, "link<", NULL
);
312 static void object_property_del_all(Object
*obj
)
314 while (!QTAILQ_EMPTY(&obj
->properties
)) {
315 ObjectProperty
*prop
= QTAILQ_FIRST(&obj
->properties
);
317 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
320 prop
->release(obj
, prop
->name
, prop
->opaque
);
329 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
331 ObjectProperty
*prop
;
333 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
334 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
335 object_property_del(obj
, prop
->name
, errp
);
341 void object_unparent(Object
*obj
)
344 object_property_del_child(obj
->parent
, obj
, NULL
);
348 static void object_deinit(Object
*obj
, TypeImpl
*type
)
350 if (type
->instance_finalize
) {
351 type
->instance_finalize(obj
);
354 while (obj
->interfaces
) {
355 Interface
*iface_obj
= obj
->interfaces
->data
;
356 obj
->interfaces
= g_slist_delete_link(obj
->interfaces
, obj
->interfaces
);
357 object_delete(OBJECT(iface_obj
));
360 if (type_has_parent(type
)) {
361 object_deinit(obj
, type_get_parent(type
));
364 object_unparent(obj
);
367 void object_finalize(void *data
)
370 TypeImpl
*ti
= obj
->class->type
;
372 object_deinit(obj
, ti
);
373 object_property_del_all(obj
);
375 g_assert(obj
->ref
== 0);
378 Object
*object_new_with_type(Type type
)
382 g_assert(type
!= NULL
);
383 type_initialize(type
);
385 obj
= g_malloc(type
->instance_size
);
386 object_initialize_with_type(obj
, type
);
392 Object
*object_new(const char *typename
)
394 TypeImpl
*ti
= type_get_by_name(typename
);
396 return object_new_with_type(ti
);
399 void object_delete(Object
*obj
)
402 g_assert(obj
->ref
== 0);
406 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
410 /* Check if typename is a direct ancestor of type */
412 if (type
== target_type
) {
416 type
= type_get_parent(type
);
422 static bool object_is_type(Object
*obj
, TypeImpl
*target_type
)
424 return !target_type
|| type_is_ancestor(obj
->class->type
, target_type
);
427 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
429 TypeImpl
*target_type
= type_get_by_name(typename
);
432 /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT,
433 * we want to go back from interfaces to the parent.
435 if (target_type
&& object_is_type(obj
, target_type
)) {
439 /* Check if obj is an interface and its containing object is a direct
440 * ancestor of typename. In principle we could do this test at the very
441 * beginning of object_dynamic_cast, avoiding a second call to
442 * object_is_type. However, casting between interfaces is relatively
443 * rare, and object_is_type(obj, type_interface) would fail almost always.
445 * Perhaps we could add a magic value to the object header for increased
446 * (run-time) type safety and to speed up tests like this one. If we ever
447 * do that we can revisit the order here.
449 if (object_is_type(obj
, type_interface
)) {
450 assert(!obj
->interfaces
);
451 obj
= INTERFACE(obj
)->obj
;
452 if (object_is_type(obj
, target_type
)) {
461 /* Check if obj has an interface of typename */
462 for (i
= obj
->interfaces
; i
; i
= i
->next
) {
463 Interface
*iface
= i
->data
;
465 if (object_is_type(OBJECT(iface
), target_type
)) {
466 return OBJECT(iface
);
474 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
)
478 inst
= object_dynamic_cast(obj
, typename
);
481 fprintf(stderr
, "Object %p is not an instance of type %s\n",
489 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
490 const char *typename
)
492 TypeImpl
*target_type
= type_get_by_name(typename
);
493 TypeImpl
*type
= class->type
;
496 if (type
== target_type
) {
500 type
= type_get_parent(type
);
506 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
507 const char *typename
)
509 ObjectClass
*ret
= object_class_dynamic_cast(class, typename
);
512 fprintf(stderr
, "Object %p is not an instance of type %s\n",
520 const char *object_get_typename(Object
*obj
)
522 return obj
->class->type
->name
;
525 ObjectClass
*object_get_class(Object
*obj
)
530 const char *object_class_get_name(ObjectClass
*klass
)
532 return klass
->type
->name
;
535 ObjectClass
*object_class_by_name(const char *typename
)
537 TypeImpl
*type
= type_get_by_name(typename
);
543 type_initialize(type
);
548 ObjectClass
*object_class_get_parent(ObjectClass
*class)
550 TypeImpl
*type
= type_get_parent(class->type
);
556 type_initialize(type
);
561 typedef struct OCFData
563 void (*fn
)(ObjectClass
*klass
, void *opaque
);
564 const char *implements_type
;
565 bool include_abstract
;
569 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
572 OCFData
*data
= opaque
;
573 TypeImpl
*type
= value
;
576 type_initialize(type
);
579 if (!data
->include_abstract
&& type
->abstract
) {
583 if (data
->implements_type
&&
584 !object_class_dynamic_cast(k
, data
->implements_type
)) {
588 data
->fn(k
, data
->opaque
);
591 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
592 const char *implements_type
, bool include_abstract
,
595 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
597 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
600 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
603 ObjectProperty
*prop
;
606 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
607 if (object_property_is_child(prop
)) {
608 ret
= fn(prop
->opaque
, opaque
);
617 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
619 GSList
**list
= opaque
;
621 *list
= g_slist_prepend(*list
, klass
);
624 GSList
*object_class_get_list(const char *implements_type
,
625 bool include_abstract
)
629 object_class_foreach(object_class_get_list_tramp
,
630 implements_type
, include_abstract
, &list
);
634 void object_ref(Object
*obj
)
639 void object_unref(Object
*obj
)
641 g_assert(obj
->ref
> 0);
644 /* parent always holds a reference to its children */
646 object_finalize(obj
);
650 void object_property_add(Object
*obj
, const char *name
, const char *type
,
651 ObjectPropertyAccessor
*get
,
652 ObjectPropertyAccessor
*set
,
653 ObjectPropertyRelease
*release
,
654 void *opaque
, Error
**errp
)
656 ObjectProperty
*prop
= g_malloc0(sizeof(*prop
));
658 prop
->name
= g_strdup(name
);
659 prop
->type
= g_strdup(type
);
663 prop
->release
= release
;
664 prop
->opaque
= opaque
;
666 QTAILQ_INSERT_TAIL(&obj
->properties
, prop
, node
);
669 static ObjectProperty
*object_property_find(Object
*obj
, const char *name
)
671 ObjectProperty
*prop
;
673 QTAILQ_FOREACH(prop
, &obj
->properties
, node
) {
674 if (strcmp(prop
->name
, name
) == 0) {
682 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
684 ObjectProperty
*prop
= object_property_find(obj
, name
);
686 QTAILQ_REMOVE(&obj
->properties
, prop
, node
);
688 prop
->release(obj
, prop
->name
, prop
->opaque
);
695 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
698 ObjectProperty
*prop
= object_property_find(obj
, name
);
701 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
706 error_set(errp
, QERR_PERMISSION_DENIED
);
708 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
712 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
715 ObjectProperty
*prop
= object_property_find(obj
, name
);
718 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
723 error_set(errp
, QERR_PERMISSION_DENIED
);
725 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
729 void object_property_set_str(Object
*obj
, const char *value
,
730 const char *name
, Error
**errp
)
732 QString
*qstr
= qstring_from_str(value
);
733 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
738 char *object_property_get_str(Object
*obj
, const char *name
,
741 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
748 qstring
= qobject_to_qstring(ret
);
750 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
753 retval
= g_strdup(qstring_get_str(qstring
));
760 void object_property_set_link(Object
*obj
, Object
*value
,
761 const char *name
, Error
**errp
)
763 object_property_set_str(obj
, object_get_canonical_path(value
),
767 Object
*object_property_get_link(Object
*obj
, const char *name
,
770 char *str
= object_property_get_str(obj
, name
, errp
);
771 Object
*target
= NULL
;
774 target
= object_resolve_path(str
, NULL
);
776 error_set(errp
, QERR_DEVICE_NOT_FOUND
, str
);
784 void object_property_set_bool(Object
*obj
, bool value
,
785 const char *name
, Error
**errp
)
787 QBool
*qbool
= qbool_from_int(value
);
788 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
793 bool object_property_get_bool(Object
*obj
, const char *name
,
796 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
803 qbool
= qobject_to_qbool(ret
);
805 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
808 retval
= qbool_get_int(qbool
);
815 void object_property_set_int(Object
*obj
, int64_t value
,
816 const char *name
, Error
**errp
)
818 QInt
*qint
= qint_from_int(value
);
819 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
824 int64_t object_property_get_int(Object
*obj
, const char *name
,
827 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
834 qint
= qobject_to_qint(ret
);
836 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
839 retval
= qint_get_int(qint
);
846 void object_property_parse(Object
*obj
, const char *string
,
847 const char *name
, Error
**errp
)
849 StringInputVisitor
*mi
;
850 mi
= string_input_visitor_new(string
);
851 object_property_set(obj
, string_input_get_visitor(mi
), name
, errp
);
853 string_input_visitor_cleanup(mi
);
856 char *object_property_print(Object
*obj
, const char *name
,
859 StringOutputVisitor
*mo
;
862 mo
= string_output_visitor_new();
863 object_property_get(obj
, string_output_get_visitor(mo
), name
, errp
);
864 string
= string_output_get_string(mo
);
865 string_output_visitor_cleanup(mo
);
869 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
871 ObjectProperty
*prop
= object_property_find(obj
, name
);
874 error_set(errp
, QERR_PROPERTY_NOT_FOUND
, "", name
);
881 Object
*object_get_root(void)
886 root
= object_new("container");
892 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
893 const char *name
, Error
**errp
)
895 Object
*child
= opaque
;
898 path
= object_get_canonical_path(child
);
899 visit_type_str(v
, &path
, name
, errp
);
903 static void object_finalize_child_property(Object
*obj
, const char *name
,
906 Object
*child
= opaque
;
911 void object_property_add_child(Object
*obj
, const char *name
,
912 Object
*child
, Error
**errp
)
916 /* Registering an interface object in the composition tree will mightily
917 * confuse object_get_canonical_path (which, on the other hand, knows how
918 * to get the canonical path of an interface object).
920 assert(!object_is_type(obj
, type_interface
));
922 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
924 object_property_add(obj
, name
, type
, object_get_child_property
,
925 NULL
, object_finalize_child_property
, child
, errp
);
928 g_assert(child
->parent
== NULL
);
934 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
935 const char *name
, Error
**errp
)
937 Object
**child
= opaque
;
941 path
= object_get_canonical_path(*child
);
942 visit_type_str(v
, &path
, name
, errp
);
946 visit_type_str(v
, &path
, name
, errp
);
950 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
951 const char *name
, Error
**errp
)
953 Object
**child
= opaque
;
955 bool ambiguous
= false;
960 type
= object_property_get_type(obj
, name
, NULL
);
962 visit_type_str(v
, &path
, name
, errp
);
967 if (strcmp(path
, "") != 0) {
970 /* Go from link<FOO> to FOO. */
971 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
972 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
975 error_set(errp
, QERR_AMBIGUOUS_PATH
, path
);
980 target
= object_resolve_path(path
, &ambiguous
);
981 if (target
|| ambiguous
) {
982 error_set(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
984 error_set(errp
, QERR_DEVICE_NOT_FOUND
, path
);
992 if (old_target
!= NULL
) {
993 object_unref(old_target
);
997 void object_property_add_link(Object
*obj
, const char *name
,
998 const char *type
, Object
**child
,
1003 full_type
= g_strdup_printf("link<%s>", type
);
1005 object_property_add(obj
, name
, full_type
,
1006 object_get_link_property
,
1007 object_set_link_property
,
1013 gchar
*object_get_canonical_path(Object
*obj
)
1015 Object
*root
= object_get_root();
1016 char *newpath
= NULL
, *path
= NULL
;
1018 if (object_is_type(obj
, type_interface
)) {
1019 obj
= INTERFACE(obj
)->obj
;
1022 while (obj
!= root
) {
1023 ObjectProperty
*prop
= NULL
;
1025 g_assert(obj
->parent
!= NULL
);
1027 QTAILQ_FOREACH(prop
, &obj
->parent
->properties
, node
) {
1028 if (!object_property_is_child(prop
)) {
1032 if (prop
->opaque
== obj
) {
1034 newpath
= g_strdup_printf("%s/%s", prop
->name
, path
);
1038 path
= g_strdup(prop
->name
);
1044 g_assert(prop
!= NULL
);
1049 newpath
= g_strdup_printf("/%s", path
);
1055 Object
*object_resolve_path_component(Object
*parent
, gchar
*part
)
1057 ObjectProperty
*prop
= object_property_find(parent
, part
);
1062 if (object_property_is_link(prop
)) {
1063 return *(Object
**)prop
->opaque
;
1064 } else if (object_property_is_child(prop
)) {
1065 return prop
->opaque
;
1071 static Object
*object_resolve_abs_path(Object
*parent
,
1073 const char *typename
,
1078 if (parts
[index
] == NULL
) {
1079 return object_dynamic_cast(parent
, typename
);
1082 if (strcmp(parts
[index
], "") == 0) {
1083 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1086 child
= object_resolve_path_component(parent
, parts
[index
]);
1091 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1094 static Object
*object_resolve_partial_path(Object
*parent
,
1096 const char *typename
,
1100 ObjectProperty
*prop
;
1102 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1104 QTAILQ_FOREACH(prop
, &parent
->properties
, node
) {
1107 if (!object_property_is_child(prop
)) {
1111 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1112 typename
, ambiguous
);
1123 if (ambiguous
&& *ambiguous
) {
1131 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1134 bool partial_path
= true;
1138 parts
= g_strsplit(path
, "/", 0);
1139 if (parts
== NULL
|| parts
[0] == NULL
) {
1141 return object_get_root();
1144 if (strcmp(parts
[0], "") == 0) {
1145 partial_path
= false;
1152 obj
= object_resolve_partial_path(object_get_root(), parts
,
1153 typename
, ambiguous
);
1155 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1163 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1165 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1168 typedef struct StringProperty
1170 char *(*get
)(Object
*, Error
**);
1171 void (*set
)(Object
*, const char *, Error
**);
1174 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1175 const char *name
, Error
**errp
)
1177 StringProperty
*prop
= opaque
;
1180 value
= prop
->get(obj
, errp
);
1182 visit_type_str(v
, &value
, name
, errp
);
1187 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1188 const char *name
, Error
**errp
)
1190 StringProperty
*prop
= opaque
;
1192 Error
*local_err
= NULL
;
1194 visit_type_str(v
, &value
, name
, &local_err
);
1196 error_propagate(errp
, local_err
);
1200 prop
->set(obj
, value
, errp
);
1204 static void property_release_str(Object
*obj
, const char *name
,
1207 StringProperty
*prop
= opaque
;
1211 void object_property_add_str(Object
*obj
, const char *name
,
1212 char *(*get
)(Object
*, Error
**),
1213 void (*set
)(Object
*, const char *, Error
**),
1216 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1221 object_property_add(obj
, name
, "string",
1222 get
? property_get_str
: NULL
,
1223 set
? property_set_str
: NULL
,
1224 property_release_str
,
1228 static void register_types(void)
1230 static TypeInfo interface_info
= {
1231 .name
= TYPE_INTERFACE
,
1232 .instance_size
= sizeof(Interface
),
1236 static TypeInfo object_info
= {
1237 .name
= TYPE_OBJECT
,
1238 .instance_size
= sizeof(Object
),
1242 type_interface
= type_register_static(&interface_info
);
1243 type_register_static(&object_info
);
1246 type_init(register_types
)