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 "qom/object_interfaces.h"
15 #include "qemu-common.h"
16 #include "qapi/visitor.h"
17 #include "qapi-visit.h"
18 #include "qapi/string-input-visitor.h"
19 #include "qapi/string-output-visitor.h"
20 #include "qapi/qmp/qerror.h"
23 /* TODO: replace QObject with a simpler visitor to avoid a dependency
24 * of the QOM core on QObject? */
25 #include "qom/qom-qobject.h"
26 #include "qapi/qmp/qobject.h"
27 #include "qapi/qmp/qbool.h"
28 #include "qapi/qmp/qint.h"
29 #include "qapi/qmp/qstring.h"
31 #define MAX_INTERFACES 32
33 typedef struct InterfaceImpl InterfaceImpl
;
34 typedef struct TypeImpl TypeImpl
;
49 void (*class_init
)(ObjectClass
*klass
, void *data
);
50 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
51 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
55 void (*instance_init
)(Object
*obj
);
56 void (*instance_post_init
)(Object
*obj
);
57 void (*instance_finalize
)(Object
*obj
);
62 TypeImpl
*parent_type
;
67 InterfaceImpl interfaces
[MAX_INTERFACES
];
70 static Type type_interface
;
72 static GHashTable
*type_table_get(void)
74 static GHashTable
*type_table
;
76 if (type_table
== NULL
) {
77 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
83 static bool enumerating_types
;
85 static void type_table_add(TypeImpl
*ti
)
87 assert(!enumerating_types
);
88 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
91 static TypeImpl
*type_table_lookup(const char *name
)
93 return g_hash_table_lookup(type_table_get(), name
);
96 static TypeImpl
*type_new(const TypeInfo
*info
)
98 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
101 g_assert(info
->name
!= NULL
);
103 if (type_table_lookup(info
->name
) != NULL
) {
104 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
108 ti
->name
= g_strdup(info
->name
);
109 ti
->parent
= g_strdup(info
->parent
);
111 ti
->class_size
= info
->class_size
;
112 ti
->instance_size
= info
->instance_size
;
114 ti
->class_init
= info
->class_init
;
115 ti
->class_base_init
= info
->class_base_init
;
116 ti
->class_finalize
= info
->class_finalize
;
117 ti
->class_data
= info
->class_data
;
119 ti
->instance_init
= info
->instance_init
;
120 ti
->instance_post_init
= info
->instance_post_init
;
121 ti
->instance_finalize
= info
->instance_finalize
;
123 ti
->abstract
= info
->abstract
;
125 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
126 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
128 ti
->num_interfaces
= i
;
133 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
142 TypeImpl
*type_register(const TypeInfo
*info
)
144 assert(info
->parent
);
145 return type_register_internal(info
);
148 TypeImpl
*type_register_static(const TypeInfo
*info
)
150 return type_register(info
);
153 static TypeImpl
*type_get_by_name(const char *name
)
159 return type_table_lookup(name
);
162 static TypeImpl
*type_get_parent(TypeImpl
*type
)
164 if (!type
->parent_type
&& type
->parent
) {
165 type
->parent_type
= type_get_by_name(type
->parent
);
166 g_assert(type
->parent_type
!= NULL
);
169 return type
->parent_type
;
172 static bool type_has_parent(TypeImpl
*type
)
174 return (type
->parent
!= NULL
);
177 static size_t type_class_get_size(TypeImpl
*ti
)
179 if (ti
->class_size
) {
180 return ti
->class_size
;
183 if (type_has_parent(ti
)) {
184 return type_class_get_size(type_get_parent(ti
));
187 return sizeof(ObjectClass
);
190 static size_t type_object_get_size(TypeImpl
*ti
)
192 if (ti
->instance_size
) {
193 return ti
->instance_size
;
196 if (type_has_parent(ti
)) {
197 return type_object_get_size(type_get_parent(ti
));
203 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
207 /* Check if target_type is a direct ancestor of type */
209 if (type
== target_type
) {
213 type
= type_get_parent(type
);
219 static void type_initialize(TypeImpl
*ti
);
221 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
222 TypeImpl
*parent_type
)
224 InterfaceClass
*new_iface
;
226 TypeImpl
*iface_impl
;
228 info
.parent
= parent_type
->name
;
229 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
230 info
.abstract
= true;
232 iface_impl
= type_new(&info
);
233 iface_impl
->parent_type
= parent_type
;
234 type_initialize(iface_impl
);
235 g_free((char *)info
.name
);
237 new_iface
= (InterfaceClass
*)iface_impl
->class;
238 new_iface
->concrete_class
= ti
->class;
239 new_iface
->interface_type
= interface_type
;
241 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
245 static void object_property_free(gpointer data
)
247 ObjectProperty
*prop
= data
;
251 g_free(prop
->description
);
255 static void type_initialize(TypeImpl
*ti
)
263 ti
->class_size
= type_class_get_size(ti
);
264 ti
->instance_size
= type_object_get_size(ti
);
266 ti
->class = g_malloc0(ti
->class_size
);
268 parent
= type_get_parent(ti
);
270 type_initialize(parent
);
274 g_assert_cmpint(parent
->class_size
, <=, ti
->class_size
);
275 memcpy(ti
->class, parent
->class, parent
->class_size
);
276 ti
->class->interfaces
= NULL
;
277 ti
->class->properties
= g_hash_table_new_full(
278 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
280 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
281 InterfaceClass
*iface
= e
->data
;
282 ObjectClass
*klass
= OBJECT_CLASS(iface
);
284 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
287 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
288 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
289 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
290 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
292 if (type_is_ancestor(target_type
, t
)) {
301 type_initialize_interface(ti
, t
, t
);
304 ti
->class->properties
= g_hash_table_new_full(
305 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
308 ti
->class->type
= ti
;
311 if (parent
->class_base_init
) {
312 parent
->class_base_init(ti
->class, ti
->class_data
);
314 parent
= type_get_parent(parent
);
317 if (ti
->class_init
) {
318 ti
->class_init(ti
->class, ti
->class_data
);
322 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
324 if (type_has_parent(ti
)) {
325 object_init_with_type(obj
, type_get_parent(ti
));
328 if (ti
->instance_init
) {
329 ti
->instance_init(obj
);
333 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
335 if (ti
->instance_post_init
) {
336 ti
->instance_post_init(obj
);
339 if (type_has_parent(ti
)) {
340 object_post_init_with_type(obj
, type_get_parent(ti
));
344 void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
348 g_assert(type
!= NULL
);
349 type_initialize(type
);
351 g_assert_cmpint(type
->instance_size
, >=, sizeof(Object
));
352 g_assert(type
->abstract
== false);
353 g_assert_cmpint(size
, >=, type
->instance_size
);
355 memset(obj
, 0, type
->instance_size
);
356 obj
->class = type
->class;
358 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
359 NULL
, object_property_free
);
360 object_init_with_type(obj
, type
);
361 object_post_init_with_type(obj
, type
);
364 void object_initialize(void *data
, size_t size
, const char *typename
)
366 TypeImpl
*type
= type_get_by_name(typename
);
368 object_initialize_with_type(data
, size
, type
);
371 static inline bool object_property_is_child(ObjectProperty
*prop
)
373 return strstart(prop
->type
, "child<", NULL
);
376 static void object_property_del_all(Object
*obj
)
378 ObjectProperty
*prop
;
385 g_hash_table_iter_init(&iter
, obj
->properties
);
386 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
389 prop
->release(obj
, prop
->name
, prop
->opaque
);
390 prop
->release
= NULL
;
394 g_hash_table_iter_remove(&iter
);
398 g_hash_table_unref(obj
->properties
);
401 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
403 ObjectProperty
*prop
;
407 g_hash_table_iter_init(&iter
, obj
->properties
);
408 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
410 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
412 prop
->release(obj
, prop
->name
, prop
->opaque
);
413 prop
->release
= NULL
;
418 g_hash_table_iter_init(&iter
, obj
->properties
);
419 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
421 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
422 g_hash_table_iter_remove(&iter
);
428 void object_unparent(Object
*obj
)
431 object_property_del_child(obj
->parent
, obj
, NULL
);
435 static void object_deinit(Object
*obj
, TypeImpl
*type
)
437 if (type
->instance_finalize
) {
438 type
->instance_finalize(obj
);
441 if (type_has_parent(type
)) {
442 object_deinit(obj
, type_get_parent(type
));
446 static void object_finalize(void *data
)
449 TypeImpl
*ti
= obj
->class->type
;
451 object_property_del_all(obj
);
452 object_deinit(obj
, ti
);
454 g_assert_cmpint(obj
->ref
, ==, 0);
460 Object
*object_new_with_type(Type type
)
464 g_assert(type
!= NULL
);
465 type_initialize(type
);
467 obj
= g_malloc(type
->instance_size
);
468 object_initialize_with_type(obj
, type
->instance_size
, type
);
474 Object
*object_new(const char *typename
)
476 TypeImpl
*ti
= type_get_by_name(typename
);
478 return object_new_with_type(ti
);
482 Object
*object_new_with_props(const char *typename
,
491 va_start(vargs
, errp
);
492 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
499 Object
*object_new_with_propv(const char *typename
,
507 Error
*local_err
= NULL
;
509 klass
= object_class_by_name(typename
);
511 error_setg(errp
, "invalid object type: %s", typename
);
515 if (object_class_is_abstract(klass
)) {
516 error_setg(errp
, "object type '%s' is abstract", typename
);
519 obj
= object_new(typename
);
521 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
525 object_property_add_child(parent
, id
, obj
, &local_err
);
530 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
531 user_creatable_complete(obj
, &local_err
);
533 object_unparent(obj
);
538 object_unref(OBJECT(obj
));
543 error_propagate(errp
, local_err
);
550 int object_set_props(Object
*obj
,
557 va_start(vargs
, errp
);
558 ret
= object_set_propv(obj
, errp
, vargs
);
565 int object_set_propv(Object
*obj
,
569 const char *propname
;
570 Error
*local_err
= NULL
;
572 propname
= va_arg(vargs
, char *);
573 while (propname
!= NULL
) {
574 const char *value
= va_arg(vargs
, char *);
576 g_assert(value
!= NULL
);
577 object_property_parse(obj
, value
, propname
, &local_err
);
579 error_propagate(errp
, local_err
);
582 propname
= va_arg(vargs
, char *);
589 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
591 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
598 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
599 const char *file
, int line
, const char *func
)
601 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
602 typename
, file
, line
, func
);
604 #ifdef CONFIG_QOM_CAST_DEBUG
608 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
609 if (obj
->class->object_cast_cache
[i
] == typename
) {
614 inst
= object_dynamic_cast(obj
, typename
);
617 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
618 file
, line
, func
, obj
, typename
);
624 if (obj
&& obj
== inst
) {
625 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
626 obj
->class->object_cast_cache
[i
- 1] =
627 obj
->class->object_cast_cache
[i
];
629 obj
->class->object_cast_cache
[i
- 1] = typename
;
637 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
638 const char *typename
)
640 ObjectClass
*ret
= NULL
;
641 TypeImpl
*target_type
;
648 /* A simple fast path that can trigger a lot for leaf classes. */
650 if (type
->name
== typename
) {
654 target_type
= type_get_by_name(typename
);
656 /* target class type unknown, so fail the cast */
660 if (type
->class->interfaces
&&
661 type_is_ancestor(target_type
, type_interface
)) {
665 for (i
= class->interfaces
; i
; i
= i
->next
) {
666 ObjectClass
*target_class
= i
->data
;
668 if (type_is_ancestor(target_class
->type
, target_type
)) {
674 /* The match was ambiguous, don't allow a cast */
678 } else if (type_is_ancestor(type
, target_type
)) {
685 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
686 const char *typename
,
687 const char *file
, int line
,
692 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
693 typename
, file
, line
, func
);
695 #ifdef CONFIG_QOM_CAST_DEBUG
698 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
699 if (class->class_cast_cache
[i
] == typename
) {
705 if (!class || !class->interfaces
) {
710 ret
= object_class_dynamic_cast(class, typename
);
712 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
713 file
, line
, func
, class, typename
);
717 #ifdef CONFIG_QOM_CAST_DEBUG
718 if (class && ret
== class) {
719 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
720 class->class_cast_cache
[i
- 1] = class->class_cast_cache
[i
];
722 class->class_cast_cache
[i
- 1] = typename
;
729 const char *object_get_typename(Object
*obj
)
731 return obj
->class->type
->name
;
734 ObjectClass
*object_get_class(Object
*obj
)
739 bool object_class_is_abstract(ObjectClass
*klass
)
741 return klass
->type
->abstract
;
744 const char *object_class_get_name(ObjectClass
*klass
)
746 return klass
->type
->name
;
749 ObjectClass
*object_class_by_name(const char *typename
)
751 TypeImpl
*type
= type_get_by_name(typename
);
757 type_initialize(type
);
762 ObjectClass
*object_class_get_parent(ObjectClass
*class)
764 TypeImpl
*type
= type_get_parent(class->type
);
770 type_initialize(type
);
775 typedef struct OCFData
777 void (*fn
)(ObjectClass
*klass
, void *opaque
);
778 const char *implements_type
;
779 bool include_abstract
;
783 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
786 OCFData
*data
= opaque
;
787 TypeImpl
*type
= value
;
790 type_initialize(type
);
793 if (!data
->include_abstract
&& type
->abstract
) {
797 if (data
->implements_type
&&
798 !object_class_dynamic_cast(k
, data
->implements_type
)) {
802 data
->fn(k
, data
->opaque
);
805 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
806 const char *implements_type
, bool include_abstract
,
809 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
811 enumerating_types
= true;
812 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
813 enumerating_types
= false;
816 static int do_object_child_foreach(Object
*obj
,
817 int (*fn
)(Object
*child
, void *opaque
),
818 void *opaque
, bool recurse
)
821 ObjectProperty
*prop
;
824 g_hash_table_iter_init(&iter
, obj
->properties
);
825 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
826 if (object_property_is_child(prop
)) {
827 Object
*child
= prop
->opaque
;
829 ret
= fn(child
, opaque
);
834 do_object_child_foreach(child
, fn
, opaque
, true);
841 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
844 return do_object_child_foreach(obj
, fn
, opaque
, false);
847 int object_child_foreach_recursive(Object
*obj
,
848 int (*fn
)(Object
*child
, void *opaque
),
851 return do_object_child_foreach(obj
, fn
, opaque
, true);
854 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
856 GSList
**list
= opaque
;
858 *list
= g_slist_prepend(*list
, klass
);
861 GSList
*object_class_get_list(const char *implements_type
,
862 bool include_abstract
)
866 object_class_foreach(object_class_get_list_tramp
,
867 implements_type
, include_abstract
, &list
);
871 void object_ref(Object
*obj
)
876 atomic_inc(&obj
->ref
);
879 void object_unref(Object
*obj
)
884 g_assert_cmpint(obj
->ref
, >, 0);
886 /* parent always holds a reference to its children */
887 if (atomic_fetch_dec(&obj
->ref
) == 1) {
888 object_finalize(obj
);
893 object_property_add(Object
*obj
, const char *name
, const char *type
,
894 ObjectPropertyAccessor
*get
,
895 ObjectPropertyAccessor
*set
,
896 ObjectPropertyRelease
*release
,
897 void *opaque
, Error
**errp
)
899 ObjectProperty
*prop
;
900 size_t name_len
= strlen(name
);
902 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
905 char *name_no_array
= g_strdup(name
);
907 name_no_array
[name_len
- 3] = '\0';
909 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
911 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
912 release
, opaque
, NULL
);
918 g_free(name_no_array
);
922 if (object_property_find(obj
, name
, NULL
) != NULL
) {
923 error_setg(errp
, "attempt to add duplicate property '%s'"
924 " to object (type '%s')", name
,
925 object_get_typename(obj
));
929 prop
= g_malloc0(sizeof(*prop
));
931 prop
->name
= g_strdup(name
);
932 prop
->type
= g_strdup(type
);
936 prop
->release
= release
;
937 prop
->opaque
= opaque
;
939 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
944 object_class_property_add(ObjectClass
*klass
,
947 ObjectPropertyAccessor
*get
,
948 ObjectPropertyAccessor
*set
,
949 ObjectPropertyRelease
*release
,
953 ObjectProperty
*prop
;
955 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
956 error_setg(errp
, "attempt to add duplicate property '%s'"
957 " to object (type '%s')", name
,
958 object_class_get_name(klass
));
962 prop
= g_malloc0(sizeof(*prop
));
964 prop
->name
= g_strdup(name
);
965 prop
->type
= g_strdup(type
);
969 prop
->release
= release
;
970 prop
->opaque
= opaque
;
972 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
977 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
980 ObjectProperty
*prop
;
981 ObjectClass
*klass
= object_get_class(obj
);
983 prop
= object_class_property_find(klass
, name
, NULL
);
988 prop
= g_hash_table_lookup(obj
->properties
, name
);
993 error_setg(errp
, "Property '.%s' not found", name
);
997 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1000 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1001 iter
->nextclass
= object_get_class(obj
);
1004 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1007 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1008 if (!iter
->nextclass
) {
1011 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1012 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1017 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1020 ObjectProperty
*prop
;
1021 ObjectClass
*parent_klass
;
1023 parent_klass
= object_class_get_parent(klass
);
1025 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1031 prop
= g_hash_table_lookup(klass
->properties
, name
);
1033 error_setg(errp
, "Property '.%s' not found", name
);
1038 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1040 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1043 error_setg(errp
, "Property '.%s' not found", name
);
1047 if (prop
->release
) {
1048 prop
->release(obj
, name
, prop
->opaque
);
1050 g_hash_table_remove(obj
->properties
, name
);
1053 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1056 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1062 error_setg(errp
, QERR_PERMISSION_DENIED
);
1064 prop
->get(obj
, v
, prop
->opaque
, name
, errp
);
1068 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1071 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1077 error_setg(errp
, QERR_PERMISSION_DENIED
);
1079 prop
->set(obj
, v
, prop
->opaque
, name
, errp
);
1083 void object_property_set_str(Object
*obj
, const char *value
,
1084 const char *name
, Error
**errp
)
1086 QString
*qstr
= qstring_from_str(value
);
1087 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1092 char *object_property_get_str(Object
*obj
, const char *name
,
1095 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1102 qstring
= qobject_to_qstring(ret
);
1104 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1107 retval
= g_strdup(qstring_get_str(qstring
));
1114 void object_property_set_link(Object
*obj
, Object
*value
,
1115 const char *name
, Error
**errp
)
1118 gchar
*path
= object_get_canonical_path(value
);
1119 object_property_set_str(obj
, path
, name
, errp
);
1122 object_property_set_str(obj
, "", name
, errp
);
1126 Object
*object_property_get_link(Object
*obj
, const char *name
,
1129 char *str
= object_property_get_str(obj
, name
, errp
);
1130 Object
*target
= NULL
;
1133 target
= object_resolve_path(str
, NULL
);
1135 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1136 "Device '%s' not found", str
);
1144 void object_property_set_bool(Object
*obj
, bool value
,
1145 const char *name
, Error
**errp
)
1147 QBool
*qbool
= qbool_from_bool(value
);
1148 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1153 bool object_property_get_bool(Object
*obj
, const char *name
,
1156 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1163 qbool
= qobject_to_qbool(ret
);
1165 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1168 retval
= qbool_get_bool(qbool
);
1175 void object_property_set_int(Object
*obj
, int64_t value
,
1176 const char *name
, Error
**errp
)
1178 QInt
*qint
= qint_from_int(value
);
1179 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
1184 int64_t object_property_get_int(Object
*obj
, const char *name
,
1187 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1194 qint
= qobject_to_qint(ret
);
1196 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1199 retval
= qint_get_int(qint
);
1206 typedef struct EnumProperty
{
1207 const char * const *strings
;
1208 int (*get
)(Object
*, Error
**);
1209 void (*set
)(Object
*, int, Error
**);
1212 int object_property_get_enum(Object
*obj
, const char *name
,
1213 const char *typename
, Error
**errp
)
1216 StringOutputVisitor
*sov
;
1217 StringInputVisitor
*siv
;
1220 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1221 EnumProperty
*enumprop
;
1227 if (!g_str_equal(prop
->type
, typename
)) {
1228 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1229 name
, object_class_get_name(
1230 object_get_class(obj
)), typename
);
1234 enumprop
= prop
->opaque
;
1236 sov
= string_output_visitor_new(false);
1237 object_property_get(obj
, string_output_get_visitor(sov
), name
, &err
);
1239 error_propagate(errp
, err
);
1240 string_output_visitor_cleanup(sov
);
1243 str
= string_output_get_string(sov
);
1244 siv
= string_input_visitor_new(str
);
1245 string_output_visitor_cleanup(sov
);
1246 visit_type_enum(string_input_get_visitor(siv
),
1247 &ret
, enumprop
->strings
, NULL
, name
, errp
);
1250 string_input_visitor_cleanup(siv
);
1255 void object_property_get_uint16List(Object
*obj
, const char *name
,
1256 uint16List
**list
, Error
**errp
)
1259 StringOutputVisitor
*ov
;
1260 StringInputVisitor
*iv
;
1263 ov
= string_output_visitor_new(false);
1264 object_property_get(obj
, string_output_get_visitor(ov
),
1267 error_propagate(errp
, err
);
1270 str
= string_output_get_string(ov
);
1271 iv
= string_input_visitor_new(str
);
1272 visit_type_uint16List(string_input_get_visitor(iv
),
1276 string_input_visitor_cleanup(iv
);
1278 string_output_visitor_cleanup(ov
);
1281 void object_property_parse(Object
*obj
, const char *string
,
1282 const char *name
, Error
**errp
)
1284 StringInputVisitor
*siv
;
1285 siv
= string_input_visitor_new(string
);
1286 object_property_set(obj
, string_input_get_visitor(siv
), name
, errp
);
1288 string_input_visitor_cleanup(siv
);
1291 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1294 StringOutputVisitor
*sov
;
1295 char *string
= NULL
;
1296 Error
*local_err
= NULL
;
1298 sov
= string_output_visitor_new(human
);
1299 object_property_get(obj
, string_output_get_visitor(sov
), name
, &local_err
);
1301 error_propagate(errp
, local_err
);
1305 string
= string_output_get_string(sov
);
1308 string_output_visitor_cleanup(sov
);
1312 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1314 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1322 Object
*object_get_root(void)
1324 static Object
*root
;
1327 root
= object_new("container");
1333 Object
*object_get_objects_root(void)
1335 return container_get(object_get_root(), "/objects");
1338 static void object_get_child_property(Object
*obj
, Visitor
*v
, void *opaque
,
1339 const char *name
, Error
**errp
)
1341 Object
*child
= opaque
;
1344 path
= object_get_canonical_path(child
);
1345 visit_type_str(v
, &path
, name
, errp
);
1349 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1354 static void object_finalize_child_property(Object
*obj
, const char *name
,
1357 Object
*child
= opaque
;
1359 if (child
->class->unparent
) {
1360 (child
->class->unparent
)(child
);
1362 child
->parent
= NULL
;
1363 object_unref(child
);
1366 void object_property_add_child(Object
*obj
, const char *name
,
1367 Object
*child
, Error
**errp
)
1369 Error
*local_err
= NULL
;
1373 if (child
->parent
!= NULL
) {
1374 error_setg(errp
, "child object is already parented");
1378 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1380 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1381 object_finalize_child_property
, child
, &local_err
);
1383 error_propagate(errp
, local_err
);
1387 op
->resolve
= object_resolve_child_property
;
1389 child
->parent
= obj
;
1395 void object_property_allow_set_link(Object
*obj
, const char *name
,
1396 Object
*val
, Error
**errp
)
1398 /* Allow the link to be set, always */
1403 void (*check
)(Object
*, const char *, Object
*, Error
**);
1404 ObjectPropertyLinkFlags flags
;
1407 static void object_get_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1408 const char *name
, Error
**errp
)
1410 LinkProperty
*lprop
= opaque
;
1411 Object
**child
= lprop
->child
;
1415 path
= object_get_canonical_path(*child
);
1416 visit_type_str(v
, &path
, name
, errp
);
1420 visit_type_str(v
, &path
, name
, errp
);
1425 * object_resolve_link:
1427 * Lookup an object and ensure its type matches the link property type. This
1428 * is similar to object_resolve_path() except type verification against the
1429 * link property is performed.
1431 * Returns: The matched object or NULL on path lookup failures.
1433 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1434 const char *path
, Error
**errp
)
1438 bool ambiguous
= false;
1441 /* Go from link<FOO> to FOO. */
1442 type
= object_property_get_type(obj
, name
, NULL
);
1443 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1444 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1447 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1449 } else if (!target
) {
1450 target
= object_resolve_path(path
, &ambiguous
);
1451 if (target
|| ambiguous
) {
1452 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1454 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1455 "Device '%s' not found", path
);
1459 g_free(target_type
);
1464 static void object_set_link_property(Object
*obj
, Visitor
*v
, void *opaque
,
1465 const char *name
, Error
**errp
)
1467 Error
*local_err
= NULL
;
1468 LinkProperty
*prop
= opaque
;
1469 Object
**child
= prop
->child
;
1470 Object
*old_target
= *child
;
1471 Object
*new_target
= NULL
;
1474 visit_type_str(v
, &path
, name
, &local_err
);
1476 if (!local_err
&& strcmp(path
, "") != 0) {
1477 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1482 error_propagate(errp
, local_err
);
1486 prop
->check(obj
, name
, new_target
, &local_err
);
1488 error_propagate(errp
, local_err
);
1492 object_ref(new_target
);
1493 *child
= new_target
;
1494 object_unref(old_target
);
1497 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1499 LinkProperty
*lprop
= opaque
;
1501 return *lprop
->child
;
1504 static void object_release_link_property(Object
*obj
, const char *name
,
1507 LinkProperty
*prop
= opaque
;
1509 if ((prop
->flags
& OBJ_PROP_LINK_UNREF_ON_RELEASE
) && *prop
->child
) {
1510 object_unref(*prop
->child
);
1515 void object_property_add_link(Object
*obj
, const char *name
,
1516 const char *type
, Object
**child
,
1517 void (*check
)(Object
*, const char *,
1518 Object
*, Error
**),
1519 ObjectPropertyLinkFlags flags
,
1522 Error
*local_err
= NULL
;
1523 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1527 prop
->child
= child
;
1528 prop
->check
= check
;
1529 prop
->flags
= flags
;
1531 full_type
= g_strdup_printf("link<%s>", type
);
1533 op
= object_property_add(obj
, name
, full_type
,
1534 object_get_link_property
,
1535 check
? object_set_link_property
: NULL
,
1536 object_release_link_property
,
1540 error_propagate(errp
, local_err
);
1545 op
->resolve
= object_resolve_link_property
;
1551 void object_property_add_const_link(Object
*obj
, const char *name
,
1552 Object
*target
, Error
**errp
)
1557 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1558 op
= object_property_add(obj
, name
, link_type
,
1559 object_get_child_property
, NULL
,
1560 NULL
, target
, errp
);
1562 op
->resolve
= object_resolve_child_property
;
1567 gchar
*object_get_canonical_path_component(Object
*obj
)
1569 ObjectProperty
*prop
= NULL
;
1570 GHashTableIter iter
;
1573 g_assert(obj
->parent
!= NULL
);
1575 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1576 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1577 if (!object_property_is_child(prop
)) {
1581 if (prop
->opaque
== obj
) {
1582 return g_strdup(prop
->name
);
1586 /* obj had a parent but was not a child, should never happen */
1587 g_assert_not_reached();
1591 gchar
*object_get_canonical_path(Object
*obj
)
1593 Object
*root
= object_get_root();
1594 char *newpath
, *path
= NULL
;
1596 while (obj
!= root
) {
1597 char *component
= object_get_canonical_path_component(obj
);
1600 newpath
= g_strdup_printf("%s/%s", component
, path
);
1611 newpath
= g_strdup_printf("/%s", path
? path
: "");
1617 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1619 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1624 if (prop
->resolve
) {
1625 return prop
->resolve(parent
, prop
->opaque
, part
);
1631 static Object
*object_resolve_abs_path(Object
*parent
,
1633 const char *typename
,
1638 if (parts
[index
] == NULL
) {
1639 return object_dynamic_cast(parent
, typename
);
1642 if (strcmp(parts
[index
], "") == 0) {
1643 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1646 child
= object_resolve_path_component(parent
, parts
[index
]);
1651 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1654 static Object
*object_resolve_partial_path(Object
*parent
,
1656 const char *typename
,
1660 GHashTableIter iter
;
1661 ObjectProperty
*prop
;
1663 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1665 g_hash_table_iter_init(&iter
, parent
->properties
);
1666 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1669 if (!object_property_is_child(prop
)) {
1673 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1674 typename
, ambiguous
);
1685 if (ambiguous
&& *ambiguous
) {
1693 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1699 parts
= g_strsplit(path
, "/", 0);
1702 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1706 obj
= object_resolve_partial_path(object_get_root(), parts
,
1707 typename
, ambiguous
);
1709 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1717 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1719 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1722 typedef struct StringProperty
1724 char *(*get
)(Object
*, Error
**);
1725 void (*set
)(Object
*, const char *, Error
**);
1728 static void property_get_str(Object
*obj
, Visitor
*v
, void *opaque
,
1729 const char *name
, Error
**errp
)
1731 StringProperty
*prop
= opaque
;
1735 value
= prop
->get(obj
, &err
);
1737 error_propagate(errp
, err
);
1741 visit_type_str(v
, &value
, name
, errp
);
1745 static void property_set_str(Object
*obj
, Visitor
*v
, void *opaque
,
1746 const char *name
, Error
**errp
)
1748 StringProperty
*prop
= opaque
;
1750 Error
*local_err
= NULL
;
1752 visit_type_str(v
, &value
, name
, &local_err
);
1754 error_propagate(errp
, local_err
);
1758 prop
->set(obj
, value
, errp
);
1762 static void property_release_str(Object
*obj
, const char *name
,
1765 StringProperty
*prop
= opaque
;
1769 void object_property_add_str(Object
*obj
, const char *name
,
1770 char *(*get
)(Object
*, Error
**),
1771 void (*set
)(Object
*, const char *, Error
**),
1774 Error
*local_err
= NULL
;
1775 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1780 object_property_add(obj
, name
, "string",
1781 get
? property_get_str
: NULL
,
1782 set
? property_set_str
: NULL
,
1783 property_release_str
,
1786 error_propagate(errp
, local_err
);
1791 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
1792 char *(*get
)(Object
*, Error
**),
1793 void (*set
)(Object
*, const char *,
1797 Error
*local_err
= NULL
;
1798 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1803 object_class_property_add(klass
, name
, "string",
1804 get
? property_get_str
: NULL
,
1805 set
? property_set_str
: NULL
,
1806 property_release_str
,
1809 error_propagate(errp
, local_err
);
1814 typedef struct BoolProperty
1816 bool (*get
)(Object
*, Error
**);
1817 void (*set
)(Object
*, bool, Error
**);
1820 static void property_get_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1821 const char *name
, Error
**errp
)
1823 BoolProperty
*prop
= opaque
;
1827 value
= prop
->get(obj
, &err
);
1829 error_propagate(errp
, err
);
1833 visit_type_bool(v
, &value
, name
, errp
);
1836 static void property_set_bool(Object
*obj
, Visitor
*v
, void *opaque
,
1837 const char *name
, Error
**errp
)
1839 BoolProperty
*prop
= opaque
;
1841 Error
*local_err
= NULL
;
1843 visit_type_bool(v
, &value
, name
, &local_err
);
1845 error_propagate(errp
, local_err
);
1849 prop
->set(obj
, value
, errp
);
1852 static void property_release_bool(Object
*obj
, const char *name
,
1855 BoolProperty
*prop
= opaque
;
1859 void object_property_add_bool(Object
*obj
, const char *name
,
1860 bool (*get
)(Object
*, Error
**),
1861 void (*set
)(Object
*, bool, Error
**),
1864 Error
*local_err
= NULL
;
1865 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1870 object_property_add(obj
, name
, "bool",
1871 get
? property_get_bool
: NULL
,
1872 set
? property_set_bool
: NULL
,
1873 property_release_bool
,
1876 error_propagate(errp
, local_err
);
1881 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
1882 bool (*get
)(Object
*, Error
**),
1883 void (*set
)(Object
*, bool, Error
**),
1886 Error
*local_err
= NULL
;
1887 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1892 object_class_property_add(klass
, name
, "bool",
1893 get
? property_get_bool
: NULL
,
1894 set
? property_set_bool
: NULL
,
1895 property_release_bool
,
1898 error_propagate(errp
, local_err
);
1903 static void property_get_enum(Object
*obj
, Visitor
*v
, void *opaque
,
1904 const char *name
, Error
**errp
)
1906 EnumProperty
*prop
= opaque
;
1910 value
= prop
->get(obj
, &err
);
1912 error_propagate(errp
, err
);
1916 visit_type_enum(v
, &value
, prop
->strings
, NULL
, name
, errp
);
1919 static void property_set_enum(Object
*obj
, Visitor
*v
, void *opaque
,
1920 const char *name
, Error
**errp
)
1922 EnumProperty
*prop
= opaque
;
1926 visit_type_enum(v
, &value
, prop
->strings
, NULL
, name
, &err
);
1928 error_propagate(errp
, err
);
1931 prop
->set(obj
, value
, errp
);
1934 static void property_release_enum(Object
*obj
, const char *name
,
1937 EnumProperty
*prop
= opaque
;
1941 void object_property_add_enum(Object
*obj
, const char *name
,
1942 const char *typename
,
1943 const char * const *strings
,
1944 int (*get
)(Object
*, Error
**),
1945 void (*set
)(Object
*, int, Error
**),
1948 Error
*local_err
= NULL
;
1949 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
1951 prop
->strings
= strings
;
1955 object_property_add(obj
, name
, typename
,
1956 get
? property_get_enum
: NULL
,
1957 set
? property_set_enum
: NULL
,
1958 property_release_enum
,
1961 error_propagate(errp
, local_err
);
1966 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
1967 const char *typename
,
1968 const char * const *strings
,
1969 int (*get
)(Object
*, Error
**),
1970 void (*set
)(Object
*, int, Error
**),
1973 Error
*local_err
= NULL
;
1974 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
1976 prop
->strings
= strings
;
1980 object_class_property_add(klass
, name
, typename
,
1981 get
? property_get_enum
: NULL
,
1982 set
? property_set_enum
: NULL
,
1983 property_release_enum
,
1986 error_propagate(errp
, local_err
);
1991 typedef struct TMProperty
{
1992 void (*get
)(Object
*, struct tm
*, Error
**);
1995 static void property_get_tm(Object
*obj
, Visitor
*v
, void *opaque
,
1996 const char *name
, Error
**errp
)
1998 TMProperty
*prop
= opaque
;
2002 prop
->get(obj
, &value
, &err
);
2007 visit_start_struct(v
, NULL
, "struct tm", name
, 0, &err
);
2011 visit_type_int32(v
, &value
.tm_year
, "tm_year", &err
);
2015 visit_type_int32(v
, &value
.tm_mon
, "tm_mon", &err
);
2019 visit_type_int32(v
, &value
.tm_mday
, "tm_mday", &err
);
2023 visit_type_int32(v
, &value
.tm_hour
, "tm_hour", &err
);
2027 visit_type_int32(v
, &value
.tm_min
, "tm_min", &err
);
2031 visit_type_int32(v
, &value
.tm_sec
, "tm_sec", &err
);
2036 error_propagate(errp
, err
);
2038 visit_end_struct(v
, errp
);
2040 error_propagate(errp
, err
);
2044 static void property_release_tm(Object
*obj
, const char *name
,
2047 TMProperty
*prop
= opaque
;
2051 void object_property_add_tm(Object
*obj
, const char *name
,
2052 void (*get
)(Object
*, struct tm
*, Error
**),
2055 Error
*local_err
= NULL
;
2056 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2060 object_property_add(obj
, name
, "struct tm",
2061 get
? property_get_tm
: NULL
, NULL
,
2062 property_release_tm
,
2065 error_propagate(errp
, local_err
);
2070 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2071 void (*get
)(Object
*, struct tm
*, Error
**),
2074 Error
*local_err
= NULL
;
2075 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2079 object_class_property_add(klass
, name
, "struct tm",
2080 get
? property_get_tm
: NULL
, NULL
,
2081 property_release_tm
,
2084 error_propagate(errp
, local_err
);
2089 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2091 return g_strdup(object_get_typename(obj
));
2094 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
,
2095 void *opaque
, const char *name
,
2098 uint8_t value
= *(uint8_t *)opaque
;
2099 visit_type_uint8(v
, &value
, name
, errp
);
2102 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
,
2103 void *opaque
, const char *name
,
2106 uint16_t value
= *(uint16_t *)opaque
;
2107 visit_type_uint16(v
, &value
, name
, errp
);
2110 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
,
2111 void *opaque
, const char *name
,
2114 uint32_t value
= *(uint32_t *)opaque
;
2115 visit_type_uint32(v
, &value
, name
, errp
);
2118 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
,
2119 void *opaque
, const char *name
,
2122 uint64_t value
= *(uint64_t *)opaque
;
2123 visit_type_uint64(v
, &value
, name
, 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
, struct Visitor
*v
, void *opaque
,
2188 const char *name
, 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
, struct Visitor
*v
, void *opaque
,
2196 const char *name
, 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
)