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/osdep.h"
14 #include "qapi/error.h"
15 #include "qom/object.h"
16 #include "qom/object_interfaces.h"
17 #include "qemu/cutils.h"
18 #include "qapi/visitor.h"
19 #include "qapi-visit.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qmp/qerror.h"
25 /* TODO: replace QObject with a simpler visitor to avoid a dependency
26 * of the QOM core on QObject? */
27 #include "qom/qom-qobject.h"
28 #include "qapi/qmp/qobject.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/qint.h"
31 #include "qapi/qmp/qstring.h"
33 #define MAX_INTERFACES 32
35 typedef struct InterfaceImpl InterfaceImpl
;
36 typedef struct TypeImpl TypeImpl
;
51 void (*class_init
)(ObjectClass
*klass
, void *data
);
52 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
53 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
57 void (*instance_init
)(Object
*obj
);
58 void (*instance_post_init
)(Object
*obj
);
59 void (*instance_finalize
)(Object
*obj
);
64 TypeImpl
*parent_type
;
69 InterfaceImpl interfaces
[MAX_INTERFACES
];
72 static Type type_interface
;
74 static GHashTable
*type_table_get(void)
76 static GHashTable
*type_table
;
78 if (type_table
== NULL
) {
79 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
85 static bool enumerating_types
;
87 static void type_table_add(TypeImpl
*ti
)
89 assert(!enumerating_types
);
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_new(const TypeInfo
*info
)
100 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
103 g_assert(info
->name
!= NULL
);
105 if (type_table_lookup(info
->name
) != NULL
) {
106 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
110 ti
->name
= g_strdup(info
->name
);
111 ti
->parent
= g_strdup(info
->parent
);
113 ti
->class_size
= info
->class_size
;
114 ti
->instance_size
= info
->instance_size
;
116 ti
->class_init
= info
->class_init
;
117 ti
->class_base_init
= info
->class_base_init
;
118 ti
->class_finalize
= info
->class_finalize
;
119 ti
->class_data
= info
->class_data
;
121 ti
->instance_init
= info
->instance_init
;
122 ti
->instance_post_init
= info
->instance_post_init
;
123 ti
->instance_finalize
= info
->instance_finalize
;
125 ti
->abstract
= info
->abstract
;
127 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
128 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
130 ti
->num_interfaces
= i
;
135 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
144 TypeImpl
*type_register(const TypeInfo
*info
)
146 assert(info
->parent
);
147 return type_register_internal(info
);
150 TypeImpl
*type_register_static(const TypeInfo
*info
)
152 return type_register(info
);
155 static TypeImpl
*type_get_by_name(const char *name
)
161 return type_table_lookup(name
);
164 static TypeImpl
*type_get_parent(TypeImpl
*type
)
166 if (!type
->parent_type
&& type
->parent
) {
167 type
->parent_type
= type_get_by_name(type
->parent
);
168 g_assert(type
->parent_type
!= NULL
);
171 return type
->parent_type
;
174 static bool type_has_parent(TypeImpl
*type
)
176 return (type
->parent
!= NULL
);
179 static size_t type_class_get_size(TypeImpl
*ti
)
181 if (ti
->class_size
) {
182 return ti
->class_size
;
185 if (type_has_parent(ti
)) {
186 return type_class_get_size(type_get_parent(ti
));
189 return sizeof(ObjectClass
);
192 static size_t type_object_get_size(TypeImpl
*ti
)
194 if (ti
->instance_size
) {
195 return ti
->instance_size
;
198 if (type_has_parent(ti
)) {
199 return type_object_get_size(type_get_parent(ti
));
205 size_t object_type_get_instance_size(const char *typename
)
207 TypeImpl
*type
= type_get_by_name(typename
);
209 g_assert(type
!= NULL
);
210 return type_object_get_size(type
);
213 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
217 /* Check if target_type is a direct ancestor of type */
219 if (type
== target_type
) {
223 type
= type_get_parent(type
);
229 static void type_initialize(TypeImpl
*ti
);
231 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
232 TypeImpl
*parent_type
)
234 InterfaceClass
*new_iface
;
236 TypeImpl
*iface_impl
;
238 info
.parent
= parent_type
->name
;
239 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
240 info
.abstract
= true;
242 iface_impl
= type_new(&info
);
243 iface_impl
->parent_type
= parent_type
;
244 type_initialize(iface_impl
);
245 g_free((char *)info
.name
);
247 new_iface
= (InterfaceClass
*)iface_impl
->class;
248 new_iface
->concrete_class
= ti
->class;
249 new_iface
->interface_type
= interface_type
;
251 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
255 static void object_property_free(gpointer data
)
257 ObjectProperty
*prop
= data
;
261 g_free(prop
->description
);
265 static void type_initialize(TypeImpl
*ti
)
273 ti
->class_size
= type_class_get_size(ti
);
274 ti
->instance_size
= type_object_get_size(ti
);
276 ti
->class = g_malloc0(ti
->class_size
);
278 parent
= type_get_parent(ti
);
280 type_initialize(parent
);
284 g_assert_cmpint(parent
->class_size
, <=, ti
->class_size
);
285 memcpy(ti
->class, parent
->class, parent
->class_size
);
286 ti
->class->interfaces
= NULL
;
287 ti
->class->properties
= g_hash_table_new_full(
288 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
290 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
291 InterfaceClass
*iface
= e
->data
;
292 ObjectClass
*klass
= OBJECT_CLASS(iface
);
294 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
297 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
298 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
299 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
300 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
302 if (type_is_ancestor(target_type
, t
)) {
311 type_initialize_interface(ti
, t
, t
);
314 ti
->class->properties
= g_hash_table_new_full(
315 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
318 ti
->class->type
= ti
;
321 if (parent
->class_base_init
) {
322 parent
->class_base_init(ti
->class, ti
->class_data
);
324 parent
= type_get_parent(parent
);
327 if (ti
->class_init
) {
328 ti
->class_init(ti
->class, ti
->class_data
);
332 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
334 if (type_has_parent(ti
)) {
335 object_init_with_type(obj
, type_get_parent(ti
));
338 if (ti
->instance_init
) {
339 ti
->instance_init(obj
);
343 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
345 if (ti
->instance_post_init
) {
346 ti
->instance_post_init(obj
);
349 if (type_has_parent(ti
)) {
350 object_post_init_with_type(obj
, type_get_parent(ti
));
354 void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
358 g_assert(type
!= NULL
);
359 type_initialize(type
);
361 g_assert_cmpint(type
->instance_size
, >=, sizeof(Object
));
362 g_assert(type
->abstract
== false);
363 g_assert_cmpint(size
, >=, type
->instance_size
);
365 memset(obj
, 0, type
->instance_size
);
366 obj
->class = type
->class;
368 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
369 NULL
, object_property_free
);
370 object_init_with_type(obj
, type
);
371 object_post_init_with_type(obj
, type
);
374 void object_initialize(void *data
, size_t size
, const char *typename
)
376 TypeImpl
*type
= type_get_by_name(typename
);
378 object_initialize_with_type(data
, size
, type
);
381 static inline bool object_property_is_child(ObjectProperty
*prop
)
383 return strstart(prop
->type
, "child<", NULL
);
386 static void object_property_del_all(Object
*obj
)
388 ObjectProperty
*prop
;
395 g_hash_table_iter_init(&iter
, obj
->properties
);
396 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
399 prop
->release(obj
, prop
->name
, prop
->opaque
);
400 prop
->release
= NULL
;
404 g_hash_table_iter_remove(&iter
);
408 g_hash_table_unref(obj
->properties
);
411 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
413 ObjectProperty
*prop
;
417 g_hash_table_iter_init(&iter
, obj
->properties
);
418 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
420 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
422 prop
->release(obj
, prop
->name
, prop
->opaque
);
423 prop
->release
= NULL
;
428 g_hash_table_iter_init(&iter
, obj
->properties
);
429 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
431 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
432 g_hash_table_iter_remove(&iter
);
438 void object_unparent(Object
*obj
)
441 object_property_del_child(obj
->parent
, obj
, NULL
);
445 static void object_deinit(Object
*obj
, TypeImpl
*type
)
447 if (type
->instance_finalize
) {
448 type
->instance_finalize(obj
);
451 if (type_has_parent(type
)) {
452 object_deinit(obj
, type_get_parent(type
));
456 static void object_finalize(void *data
)
459 TypeImpl
*ti
= obj
->class->type
;
461 object_property_del_all(obj
);
462 object_deinit(obj
, ti
);
464 g_assert_cmpint(obj
->ref
, ==, 0);
470 Object
*object_new_with_type(Type type
)
474 g_assert(type
!= NULL
);
475 type_initialize(type
);
477 obj
= g_malloc(type
->instance_size
);
478 object_initialize_with_type(obj
, type
->instance_size
, type
);
484 Object
*object_new(const char *typename
)
486 TypeImpl
*ti
= type_get_by_name(typename
);
488 return object_new_with_type(ti
);
492 Object
*object_new_with_props(const char *typename
,
501 va_start(vargs
, errp
);
502 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
509 Object
*object_new_with_propv(const char *typename
,
517 Error
*local_err
= NULL
;
519 klass
= object_class_by_name(typename
);
521 error_setg(errp
, "invalid object type: %s", typename
);
525 if (object_class_is_abstract(klass
)) {
526 error_setg(errp
, "object type '%s' is abstract", typename
);
529 obj
= object_new(typename
);
531 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
535 object_property_add_child(parent
, id
, obj
, &local_err
);
540 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
541 user_creatable_complete(obj
, &local_err
);
543 object_unparent(obj
);
548 object_unref(OBJECT(obj
));
552 error_propagate(errp
, local_err
);
558 int object_set_props(Object
*obj
,
565 va_start(vargs
, errp
);
566 ret
= object_set_propv(obj
, errp
, vargs
);
573 int object_set_propv(Object
*obj
,
577 const char *propname
;
578 Error
*local_err
= NULL
;
580 propname
= va_arg(vargs
, char *);
581 while (propname
!= NULL
) {
582 const char *value
= va_arg(vargs
, char *);
584 g_assert(value
!= NULL
);
585 object_property_parse(obj
, value
, propname
, &local_err
);
587 error_propagate(errp
, local_err
);
590 propname
= va_arg(vargs
, char *);
597 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
599 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
606 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
607 const char *file
, int line
, const char *func
)
609 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
610 typename
, file
, line
, func
);
612 #ifdef CONFIG_QOM_CAST_DEBUG
616 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
617 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
622 inst
= object_dynamic_cast(obj
, typename
);
625 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
626 file
, line
, func
, obj
, typename
);
632 if (obj
&& obj
== inst
) {
633 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
634 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
635 atomic_read(&obj
->class->object_cast_cache
[i
]));
637 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
645 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
646 const char *typename
)
648 ObjectClass
*ret
= NULL
;
649 TypeImpl
*target_type
;
656 /* A simple fast path that can trigger a lot for leaf classes. */
658 if (type
->name
== typename
) {
662 target_type
= type_get_by_name(typename
);
664 /* target class type unknown, so fail the cast */
668 if (type
->class->interfaces
&&
669 type_is_ancestor(target_type
, type_interface
)) {
673 for (i
= class->interfaces
; i
; i
= i
->next
) {
674 ObjectClass
*target_class
= i
->data
;
676 if (type_is_ancestor(target_class
->type
, target_type
)) {
682 /* The match was ambiguous, don't allow a cast */
686 } else if (type_is_ancestor(type
, target_type
)) {
693 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
694 const char *typename
,
695 const char *file
, int line
,
700 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
701 typename
, file
, line
, func
);
703 #ifdef CONFIG_QOM_CAST_DEBUG
706 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
707 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
713 if (!class || !class->interfaces
) {
718 ret
= object_class_dynamic_cast(class, typename
);
720 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
721 file
, line
, func
, class, typename
);
725 #ifdef CONFIG_QOM_CAST_DEBUG
726 if (class && ret
== class) {
727 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
728 atomic_set(&class->class_cast_cache
[i
- 1],
729 atomic_read(&class->class_cast_cache
[i
]));
731 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
738 const char *object_get_typename(Object
*obj
)
740 return obj
->class->type
->name
;
743 ObjectClass
*object_get_class(Object
*obj
)
748 bool object_class_is_abstract(ObjectClass
*klass
)
750 return klass
->type
->abstract
;
753 const char *object_class_get_name(ObjectClass
*klass
)
755 return klass
->type
->name
;
758 ObjectClass
*object_class_by_name(const char *typename
)
760 TypeImpl
*type
= type_get_by_name(typename
);
766 type_initialize(type
);
771 ObjectClass
*object_class_get_parent(ObjectClass
*class)
773 TypeImpl
*type
= type_get_parent(class->type
);
779 type_initialize(type
);
784 typedef struct OCFData
786 void (*fn
)(ObjectClass
*klass
, void *opaque
);
787 const char *implements_type
;
788 bool include_abstract
;
792 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
795 OCFData
*data
= opaque
;
796 TypeImpl
*type
= value
;
799 type_initialize(type
);
802 if (!data
->include_abstract
&& type
->abstract
) {
806 if (data
->implements_type
&&
807 !object_class_dynamic_cast(k
, data
->implements_type
)) {
811 data
->fn(k
, data
->opaque
);
814 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
815 const char *implements_type
, bool include_abstract
,
818 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
820 enumerating_types
= true;
821 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
822 enumerating_types
= false;
825 static int do_object_child_foreach(Object
*obj
,
826 int (*fn
)(Object
*child
, void *opaque
),
827 void *opaque
, bool recurse
)
830 ObjectProperty
*prop
;
833 g_hash_table_iter_init(&iter
, obj
->properties
);
834 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
835 if (object_property_is_child(prop
)) {
836 Object
*child
= prop
->opaque
;
838 ret
= fn(child
, opaque
);
843 do_object_child_foreach(child
, fn
, opaque
, true);
850 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
853 return do_object_child_foreach(obj
, fn
, opaque
, false);
856 int object_child_foreach_recursive(Object
*obj
,
857 int (*fn
)(Object
*child
, void *opaque
),
860 return do_object_child_foreach(obj
, fn
, opaque
, true);
863 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
865 GSList
**list
= opaque
;
867 *list
= g_slist_prepend(*list
, klass
);
870 GSList
*object_class_get_list(const char *implements_type
,
871 bool include_abstract
)
875 object_class_foreach(object_class_get_list_tramp
,
876 implements_type
, include_abstract
, &list
);
880 void object_ref(Object
*obj
)
885 atomic_inc(&obj
->ref
);
888 void object_unref(Object
*obj
)
893 g_assert_cmpint(obj
->ref
, >, 0);
895 /* parent always holds a reference to its children */
896 if (atomic_fetch_dec(&obj
->ref
) == 1) {
897 object_finalize(obj
);
902 object_property_add(Object
*obj
, const char *name
, const char *type
,
903 ObjectPropertyAccessor
*get
,
904 ObjectPropertyAccessor
*set
,
905 ObjectPropertyRelease
*release
,
906 void *opaque
, Error
**errp
)
908 ObjectProperty
*prop
;
909 size_t name_len
= strlen(name
);
911 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
914 char *name_no_array
= g_strdup(name
);
916 name_no_array
[name_len
- 3] = '\0';
918 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
920 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
921 release
, opaque
, NULL
);
927 g_free(name_no_array
);
931 if (object_property_find(obj
, name
, NULL
) != NULL
) {
932 error_setg(errp
, "attempt to add duplicate property '%s'"
933 " to object (type '%s')", name
,
934 object_get_typename(obj
));
938 prop
= g_malloc0(sizeof(*prop
));
940 prop
->name
= g_strdup(name
);
941 prop
->type
= g_strdup(type
);
945 prop
->release
= release
;
946 prop
->opaque
= opaque
;
948 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
953 object_class_property_add(ObjectClass
*klass
,
956 ObjectPropertyAccessor
*get
,
957 ObjectPropertyAccessor
*set
,
958 ObjectPropertyRelease
*release
,
962 ObjectProperty
*prop
;
964 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
965 error_setg(errp
, "attempt to add duplicate property '%s'"
966 " to object (type '%s')", name
,
967 object_class_get_name(klass
));
971 prop
= g_malloc0(sizeof(*prop
));
973 prop
->name
= g_strdup(name
);
974 prop
->type
= g_strdup(type
);
978 prop
->release
= release
;
979 prop
->opaque
= opaque
;
981 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
986 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
989 ObjectProperty
*prop
;
990 ObjectClass
*klass
= object_get_class(obj
);
992 prop
= object_class_property_find(klass
, name
, NULL
);
997 prop
= g_hash_table_lookup(obj
->properties
, name
);
1002 error_setg(errp
, "Property '.%s' not found", name
);
1006 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1009 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1010 iter
->nextclass
= object_get_class(obj
);
1013 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1016 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1017 if (!iter
->nextclass
) {
1020 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1021 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1026 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1029 ObjectProperty
*prop
;
1030 ObjectClass
*parent_klass
;
1032 parent_klass
= object_class_get_parent(klass
);
1034 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1040 prop
= g_hash_table_lookup(klass
->properties
, name
);
1042 error_setg(errp
, "Property '.%s' not found", name
);
1047 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1049 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1052 error_setg(errp
, "Property '.%s' not found", name
);
1056 if (prop
->release
) {
1057 prop
->release(obj
, name
, prop
->opaque
);
1059 g_hash_table_remove(obj
->properties
, name
);
1062 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1065 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1071 error_setg(errp
, QERR_PERMISSION_DENIED
);
1073 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1077 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1080 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1086 error_setg(errp
, QERR_PERMISSION_DENIED
);
1088 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1092 void object_property_set_str(Object
*obj
, const char *value
,
1093 const char *name
, Error
**errp
)
1095 QString
*qstr
= qstring_from_str(value
);
1096 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1101 char *object_property_get_str(Object
*obj
, const char *name
,
1104 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1111 qstring
= qobject_to_qstring(ret
);
1113 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1116 retval
= g_strdup(qstring_get_str(qstring
));
1123 void object_property_set_link(Object
*obj
, Object
*value
,
1124 const char *name
, Error
**errp
)
1127 gchar
*path
= object_get_canonical_path(value
);
1128 object_property_set_str(obj
, path
, name
, errp
);
1131 object_property_set_str(obj
, "", name
, errp
);
1135 Object
*object_property_get_link(Object
*obj
, const char *name
,
1138 char *str
= object_property_get_str(obj
, name
, errp
);
1139 Object
*target
= NULL
;
1142 target
= object_resolve_path(str
, NULL
);
1144 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1145 "Device '%s' not found", str
);
1153 void object_property_set_bool(Object
*obj
, bool value
,
1154 const char *name
, Error
**errp
)
1156 QBool
*qbool
= qbool_from_bool(value
);
1157 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1162 bool object_property_get_bool(Object
*obj
, const char *name
,
1165 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1172 qbool
= qobject_to_qbool(ret
);
1174 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1177 retval
= qbool_get_bool(qbool
);
1184 void object_property_set_int(Object
*obj
, int64_t value
,
1185 const char *name
, Error
**errp
)
1187 QInt
*qint
= qint_from_int(value
);
1188 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
1193 int64_t object_property_get_int(Object
*obj
, const char *name
,
1196 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1203 qint
= qobject_to_qint(ret
);
1205 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1208 retval
= qint_get_int(qint
);
1215 typedef struct EnumProperty
{
1216 const char * const *strings
;
1217 int (*get
)(Object
*, Error
**);
1218 void (*set
)(Object
*, int, Error
**);
1221 int object_property_get_enum(Object
*obj
, const char *name
,
1222 const char *typename
, Error
**errp
)
1228 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1229 EnumProperty
*enumprop
;
1235 if (!g_str_equal(prop
->type
, typename
)) {
1236 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1237 name
, object_class_get_name(
1238 object_get_class(obj
)), typename
);
1242 enumprop
= prop
->opaque
;
1244 v
= string_output_visitor_new(false, &str
);
1245 object_property_get(obj
, v
, name
, &err
);
1247 error_propagate(errp
, err
);
1251 visit_complete(v
, &str
);
1253 v
= string_input_visitor_new(str
);
1254 visit_type_enum(v
, name
, &ret
, enumprop
->strings
, errp
);
1262 void object_property_get_uint16List(Object
*obj
, const char *name
,
1263 uint16List
**list
, Error
**errp
)
1269 v
= string_output_visitor_new(false, &str
);
1270 object_property_get(obj
, v
, name
, &err
);
1272 error_propagate(errp
, err
);
1275 visit_complete(v
, &str
);
1277 v
= string_input_visitor_new(str
);
1278 visit_type_uint16List(v
, NULL
, list
, errp
);
1285 void object_property_parse(Object
*obj
, const char *string
,
1286 const char *name
, Error
**errp
)
1288 Visitor
*v
= string_input_visitor_new(string
);
1289 object_property_set(obj
, v
, name
, errp
);
1293 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1297 char *string
= NULL
;
1298 Error
*local_err
= NULL
;
1300 v
= string_output_visitor_new(human
, &string
);
1301 object_property_get(obj
, v
, name
, &local_err
);
1303 error_propagate(errp
, local_err
);
1307 visit_complete(v
, &string
);
1314 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1316 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1324 Object
*object_get_root(void)
1326 static Object
*root
;
1329 root
= object_new("container");
1335 Object
*object_get_objects_root(void)
1337 return container_get(object_get_root(), "/objects");
1340 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1341 const char *name
, void *opaque
,
1344 Object
*child
= opaque
;
1347 path
= object_get_canonical_path(child
);
1348 visit_type_str(v
, name
, &path
, errp
);
1352 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1357 static void object_finalize_child_property(Object
*obj
, const char *name
,
1360 Object
*child
= opaque
;
1362 if (child
->class->unparent
) {
1363 (child
->class->unparent
)(child
);
1365 child
->parent
= NULL
;
1366 object_unref(child
);
1369 void object_property_add_child(Object
*obj
, const char *name
,
1370 Object
*child
, Error
**errp
)
1372 Error
*local_err
= NULL
;
1376 if (child
->parent
!= NULL
) {
1377 error_setg(errp
, "child object is already parented");
1381 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1383 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1384 object_finalize_child_property
, child
, &local_err
);
1386 error_propagate(errp
, local_err
);
1390 op
->resolve
= object_resolve_child_property
;
1392 child
->parent
= obj
;
1398 void object_property_allow_set_link(Object
*obj
, const char *name
,
1399 Object
*val
, Error
**errp
)
1401 /* Allow the link to be set, always */
1406 void (*check
)(Object
*, const char *, Object
*, Error
**);
1407 ObjectPropertyLinkFlags flags
;
1410 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1411 const char *name
, void *opaque
,
1414 LinkProperty
*lprop
= opaque
;
1415 Object
**child
= lprop
->child
;
1419 path
= object_get_canonical_path(*child
);
1420 visit_type_str(v
, name
, &path
, errp
);
1424 visit_type_str(v
, name
, &path
, errp
);
1429 * object_resolve_link:
1431 * Lookup an object and ensure its type matches the link property type. This
1432 * is similar to object_resolve_path() except type verification against the
1433 * link property is performed.
1435 * Returns: The matched object or NULL on path lookup failures.
1437 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1438 const char *path
, Error
**errp
)
1442 bool ambiguous
= false;
1445 /* Go from link<FOO> to FOO. */
1446 type
= object_property_get_type(obj
, name
, NULL
);
1447 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1448 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1451 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1453 } else if (!target
) {
1454 target
= object_resolve_path(path
, &ambiguous
);
1455 if (target
|| ambiguous
) {
1456 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1458 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1459 "Device '%s' not found", path
);
1463 g_free(target_type
);
1468 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1469 const char *name
, void *opaque
,
1472 Error
*local_err
= NULL
;
1473 LinkProperty
*prop
= opaque
;
1474 Object
**child
= prop
->child
;
1475 Object
*old_target
= *child
;
1476 Object
*new_target
= NULL
;
1479 visit_type_str(v
, name
, &path
, &local_err
);
1481 if (!local_err
&& strcmp(path
, "") != 0) {
1482 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1487 error_propagate(errp
, local_err
);
1491 prop
->check(obj
, name
, new_target
, &local_err
);
1493 error_propagate(errp
, local_err
);
1497 object_ref(new_target
);
1498 *child
= new_target
;
1499 object_unref(old_target
);
1502 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1504 LinkProperty
*lprop
= opaque
;
1506 return *lprop
->child
;
1509 static void object_release_link_property(Object
*obj
, const char *name
,
1512 LinkProperty
*prop
= opaque
;
1514 if ((prop
->flags
& OBJ_PROP_LINK_UNREF_ON_RELEASE
) && *prop
->child
) {
1515 object_unref(*prop
->child
);
1520 void object_property_add_link(Object
*obj
, const char *name
,
1521 const char *type
, Object
**child
,
1522 void (*check
)(Object
*, const char *,
1523 Object
*, Error
**),
1524 ObjectPropertyLinkFlags flags
,
1527 Error
*local_err
= NULL
;
1528 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1532 prop
->child
= child
;
1533 prop
->check
= check
;
1534 prop
->flags
= flags
;
1536 full_type
= g_strdup_printf("link<%s>", type
);
1538 op
= object_property_add(obj
, name
, full_type
,
1539 object_get_link_property
,
1540 check
? object_set_link_property
: NULL
,
1541 object_release_link_property
,
1545 error_propagate(errp
, local_err
);
1550 op
->resolve
= object_resolve_link_property
;
1556 void object_property_add_const_link(Object
*obj
, const char *name
,
1557 Object
*target
, Error
**errp
)
1562 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1563 op
= object_property_add(obj
, name
, link_type
,
1564 object_get_child_property
, NULL
,
1565 NULL
, target
, errp
);
1567 op
->resolve
= object_resolve_child_property
;
1572 gchar
*object_get_canonical_path_component(Object
*obj
)
1574 ObjectProperty
*prop
= NULL
;
1575 GHashTableIter iter
;
1578 g_assert(obj
->parent
!= NULL
);
1580 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1581 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1582 if (!object_property_is_child(prop
)) {
1586 if (prop
->opaque
== obj
) {
1587 return g_strdup(prop
->name
);
1591 /* obj had a parent but was not a child, should never happen */
1592 g_assert_not_reached();
1596 gchar
*object_get_canonical_path(Object
*obj
)
1598 Object
*root
= object_get_root();
1599 char *newpath
, *path
= NULL
;
1601 while (obj
!= root
) {
1602 char *component
= object_get_canonical_path_component(obj
);
1605 newpath
= g_strdup_printf("%s/%s", component
, path
);
1616 newpath
= g_strdup_printf("/%s", path
? path
: "");
1622 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1624 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1629 if (prop
->resolve
) {
1630 return prop
->resolve(parent
, prop
->opaque
, part
);
1636 static Object
*object_resolve_abs_path(Object
*parent
,
1638 const char *typename
,
1643 if (parts
[index
] == NULL
) {
1644 return object_dynamic_cast(parent
, typename
);
1647 if (strcmp(parts
[index
], "") == 0) {
1648 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1651 child
= object_resolve_path_component(parent
, parts
[index
]);
1656 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1659 static Object
*object_resolve_partial_path(Object
*parent
,
1661 const char *typename
,
1665 GHashTableIter iter
;
1666 ObjectProperty
*prop
;
1668 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1670 g_hash_table_iter_init(&iter
, parent
->properties
);
1671 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1674 if (!object_property_is_child(prop
)) {
1678 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1679 typename
, ambiguous
);
1690 if (ambiguous
&& *ambiguous
) {
1698 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1704 parts
= g_strsplit(path
, "/", 0);
1707 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1711 obj
= object_resolve_partial_path(object_get_root(), parts
,
1712 typename
, ambiguous
);
1714 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1722 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1724 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1727 typedef struct StringProperty
1729 char *(*get
)(Object
*, Error
**);
1730 void (*set
)(Object
*, const char *, Error
**);
1733 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1734 void *opaque
, Error
**errp
)
1736 StringProperty
*prop
= opaque
;
1740 value
= prop
->get(obj
, &err
);
1742 error_propagate(errp
, err
);
1746 visit_type_str(v
, name
, &value
, errp
);
1750 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1751 void *opaque
, Error
**errp
)
1753 StringProperty
*prop
= opaque
;
1755 Error
*local_err
= NULL
;
1757 visit_type_str(v
, name
, &value
, &local_err
);
1759 error_propagate(errp
, local_err
);
1763 prop
->set(obj
, value
, errp
);
1767 static void property_release_str(Object
*obj
, const char *name
,
1770 StringProperty
*prop
= opaque
;
1774 void object_property_add_str(Object
*obj
, const char *name
,
1775 char *(*get
)(Object
*, Error
**),
1776 void (*set
)(Object
*, const char *, Error
**),
1779 Error
*local_err
= NULL
;
1780 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1785 object_property_add(obj
, name
, "string",
1786 get
? property_get_str
: NULL
,
1787 set
? property_set_str
: NULL
,
1788 property_release_str
,
1791 error_propagate(errp
, local_err
);
1796 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
1797 char *(*get
)(Object
*, Error
**),
1798 void (*set
)(Object
*, const char *,
1802 Error
*local_err
= NULL
;
1803 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1808 object_class_property_add(klass
, name
, "string",
1809 get
? property_get_str
: NULL
,
1810 set
? property_set_str
: NULL
,
1811 property_release_str
,
1814 error_propagate(errp
, local_err
);
1819 typedef struct BoolProperty
1821 bool (*get
)(Object
*, Error
**);
1822 void (*set
)(Object
*, bool, Error
**);
1825 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
1826 void *opaque
, Error
**errp
)
1828 BoolProperty
*prop
= opaque
;
1832 value
= prop
->get(obj
, &err
);
1834 error_propagate(errp
, err
);
1838 visit_type_bool(v
, name
, &value
, errp
);
1841 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
1842 void *opaque
, Error
**errp
)
1844 BoolProperty
*prop
= opaque
;
1846 Error
*local_err
= NULL
;
1848 visit_type_bool(v
, name
, &value
, &local_err
);
1850 error_propagate(errp
, local_err
);
1854 prop
->set(obj
, value
, errp
);
1857 static void property_release_bool(Object
*obj
, const char *name
,
1860 BoolProperty
*prop
= opaque
;
1864 void object_property_add_bool(Object
*obj
, const char *name
,
1865 bool (*get
)(Object
*, Error
**),
1866 void (*set
)(Object
*, bool, Error
**),
1869 Error
*local_err
= NULL
;
1870 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1875 object_property_add(obj
, name
, "bool",
1876 get
? property_get_bool
: NULL
,
1877 set
? property_set_bool
: NULL
,
1878 property_release_bool
,
1881 error_propagate(errp
, local_err
);
1886 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
1887 bool (*get
)(Object
*, Error
**),
1888 void (*set
)(Object
*, bool, Error
**),
1891 Error
*local_err
= NULL
;
1892 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1897 object_class_property_add(klass
, name
, "bool",
1898 get
? property_get_bool
: NULL
,
1899 set
? property_set_bool
: NULL
,
1900 property_release_bool
,
1903 error_propagate(errp
, local_err
);
1908 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
1909 void *opaque
, Error
**errp
)
1911 EnumProperty
*prop
= opaque
;
1915 value
= prop
->get(obj
, &err
);
1917 error_propagate(errp
, err
);
1921 visit_type_enum(v
, name
, &value
, prop
->strings
, errp
);
1924 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
1925 void *opaque
, Error
**errp
)
1927 EnumProperty
*prop
= opaque
;
1931 visit_type_enum(v
, name
, &value
, prop
->strings
, &err
);
1933 error_propagate(errp
, err
);
1936 prop
->set(obj
, value
, errp
);
1939 static void property_release_enum(Object
*obj
, const char *name
,
1942 EnumProperty
*prop
= opaque
;
1946 void object_property_add_enum(Object
*obj
, const char *name
,
1947 const char *typename
,
1948 const char * const *strings
,
1949 int (*get
)(Object
*, Error
**),
1950 void (*set
)(Object
*, int, Error
**),
1953 Error
*local_err
= NULL
;
1954 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
1956 prop
->strings
= strings
;
1960 object_property_add(obj
, name
, typename
,
1961 get
? property_get_enum
: NULL
,
1962 set
? property_set_enum
: NULL
,
1963 property_release_enum
,
1966 error_propagate(errp
, local_err
);
1971 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
1972 const char *typename
,
1973 const char * const *strings
,
1974 int (*get
)(Object
*, Error
**),
1975 void (*set
)(Object
*, int, Error
**),
1978 Error
*local_err
= NULL
;
1979 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
1981 prop
->strings
= strings
;
1985 object_class_property_add(klass
, name
, typename
,
1986 get
? property_get_enum
: NULL
,
1987 set
? property_set_enum
: NULL
,
1988 property_release_enum
,
1991 error_propagate(errp
, local_err
);
1996 typedef struct TMProperty
{
1997 void (*get
)(Object
*, struct tm
*, Error
**);
2000 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2001 void *opaque
, Error
**errp
)
2003 TMProperty
*prop
= opaque
;
2007 prop
->get(obj
, &value
, &err
);
2012 visit_start_struct(v
, name
, NULL
, 0, &err
);
2016 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2020 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2024 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2028 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2032 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2036 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2040 visit_check_struct(v
, &err
);
2042 visit_end_struct(v
, NULL
);
2044 error_propagate(errp
, err
);
2048 static void property_release_tm(Object
*obj
, const char *name
,
2051 TMProperty
*prop
= opaque
;
2055 void object_property_add_tm(Object
*obj
, const char *name
,
2056 void (*get
)(Object
*, struct tm
*, Error
**),
2059 Error
*local_err
= NULL
;
2060 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2064 object_property_add(obj
, name
, "struct tm",
2065 get
? property_get_tm
: NULL
, NULL
,
2066 property_release_tm
,
2069 error_propagate(errp
, local_err
);
2074 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2075 void (*get
)(Object
*, struct tm
*, Error
**),
2078 Error
*local_err
= NULL
;
2079 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2083 object_class_property_add(klass
, name
, "struct tm",
2084 get
? property_get_tm
: NULL
, NULL
,
2085 property_release_tm
,
2088 error_propagate(errp
, local_err
);
2093 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2095 return g_strdup(object_get_typename(obj
));
2098 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2099 void *opaque
, Error
**errp
)
2101 uint8_t value
= *(uint8_t *)opaque
;
2102 visit_type_uint8(v
, name
, &value
, errp
);
2105 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2106 void *opaque
, Error
**errp
)
2108 uint16_t value
= *(uint16_t *)opaque
;
2109 visit_type_uint16(v
, name
, &value
, errp
);
2112 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2113 void *opaque
, Error
**errp
)
2115 uint32_t value
= *(uint32_t *)opaque
;
2116 visit_type_uint32(v
, name
, &value
, errp
);
2119 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2120 void *opaque
, Error
**errp
)
2122 uint64_t value
= *(uint64_t *)opaque
;
2123 visit_type_uint64(v
, name
, &value
, errp
);
2126 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2127 const uint8_t *v
, Error
**errp
)
2129 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2130 NULL
, NULL
, (void *)v
, errp
);
2133 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2134 const uint8_t *v
, Error
**errp
)
2136 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2137 NULL
, NULL
, (void *)v
, errp
);
2140 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2141 const uint16_t *v
, Error
**errp
)
2143 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2144 NULL
, NULL
, (void *)v
, errp
);
2147 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2148 const uint16_t *v
, Error
**errp
)
2150 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2151 NULL
, NULL
, (void *)v
, errp
);
2154 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2155 const uint32_t *v
, Error
**errp
)
2157 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2158 NULL
, NULL
, (void *)v
, errp
);
2161 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2162 const uint32_t *v
, Error
**errp
)
2164 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2165 NULL
, NULL
, (void *)v
, errp
);
2168 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2169 const uint64_t *v
, Error
**errp
)
2171 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2172 NULL
, NULL
, (void *)v
, errp
);
2175 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2176 const uint64_t *v
, Error
**errp
)
2178 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2179 NULL
, NULL
, (void *)v
, errp
);
2187 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2188 void *opaque
, Error
**errp
)
2190 AliasProperty
*prop
= opaque
;
2192 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2195 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2196 void *opaque
, Error
**errp
)
2198 AliasProperty
*prop
= opaque
;
2200 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2203 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2206 AliasProperty
*prop
= opaque
;
2208 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2211 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2213 AliasProperty
*prop
= opaque
;
2215 g_free(prop
->target_name
);
2219 void object_property_add_alias(Object
*obj
, const char *name
,
2220 Object
*target_obj
, const char *target_name
,
2223 AliasProperty
*prop
;
2225 ObjectProperty
*target_prop
;
2227 Error
*local_err
= NULL
;
2229 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2234 if (object_property_is_child(target_prop
)) {
2235 prop_type
= g_strdup_printf("link%s",
2236 target_prop
->type
+ strlen("child"));
2238 prop_type
= g_strdup(target_prop
->type
);
2241 prop
= g_malloc(sizeof(*prop
));
2242 prop
->target_obj
= target_obj
;
2243 prop
->target_name
= g_strdup(target_name
);
2245 op
= object_property_add(obj
, name
, prop_type
,
2248 property_release_alias
,
2251 error_propagate(errp
, local_err
);
2255 op
->resolve
= property_resolve_alias
;
2257 object_property_set_description(obj
, op
->name
,
2258 target_prop
->description
,
2265 void object_property_set_description(Object
*obj
, const char *name
,
2266 const char *description
, Error
**errp
)
2270 op
= object_property_find(obj
, name
, errp
);
2275 g_free(op
->description
);
2276 op
->description
= g_strdup(description
);
2279 void object_class_property_set_description(ObjectClass
*klass
,
2281 const char *description
,
2286 op
= g_hash_table_lookup(klass
->properties
, name
);
2288 error_setg(errp
, "Property '.%s' not found", name
);
2292 g_free(op
->description
);
2293 op
->description
= g_strdup(description
);
2296 static void object_instance_init(Object
*obj
)
2298 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
2301 static void register_types(void)
2303 static TypeInfo interface_info
= {
2304 .name
= TYPE_INTERFACE
,
2305 .class_size
= sizeof(InterfaceClass
),
2309 static TypeInfo object_info
= {
2310 .name
= TYPE_OBJECT
,
2311 .instance_size
= sizeof(Object
),
2312 .instance_init
= object_instance_init
,
2316 type_interface
= type_register_internal(&interface_info
);
2317 type_register_internal(&object_info
);
2320 type_init(register_types
)