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 "qom/object.h"
15 #include "qom/object_interfaces.h"
16 #include "qemu-common.h"
17 #include "qapi/visitor.h"
18 #include "qapi-visit.h"
19 #include "qapi/string-input-visitor.h"
20 #include "qapi/string-output-visitor.h"
21 #include "qapi/qmp/qerror.h"
24 /* TODO: replace QObject with a simpler visitor to avoid a dependency
25 * of the QOM core on QObject? */
26 #include "qom/qom-qobject.h"
27 #include "qapi/qmp/qobject.h"
28 #include "qapi/qmp/qbool.h"
29 #include "qapi/qmp/qint.h"
30 #include "qapi/qmp/qstring.h"
32 #define MAX_INTERFACES 32
34 typedef struct InterfaceImpl InterfaceImpl
;
35 typedef struct TypeImpl TypeImpl
;
50 void (*class_init
)(ObjectClass
*klass
, void *data
);
51 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
52 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
56 void (*instance_init
)(Object
*obj
);
57 void (*instance_post_init
)(Object
*obj
);
58 void (*instance_finalize
)(Object
*obj
);
63 TypeImpl
*parent_type
;
68 InterfaceImpl interfaces
[MAX_INTERFACES
];
71 static Type type_interface
;
73 static GHashTable
*type_table_get(void)
75 static GHashTable
*type_table
;
77 if (type_table
== NULL
) {
78 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
84 static bool enumerating_types
;
86 static void type_table_add(TypeImpl
*ti
)
88 assert(!enumerating_types
);
89 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
92 static TypeImpl
*type_table_lookup(const char *name
)
94 return g_hash_table_lookup(type_table_get(), name
);
97 static TypeImpl
*type_new(const TypeInfo
*info
)
99 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_post_init
= info
->instance_post_init
;
122 ti
->instance_finalize
= info
->instance_finalize
;
124 ti
->abstract
= info
->abstract
;
126 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
127 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
129 ti
->num_interfaces
= i
;
134 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
143 TypeImpl
*type_register(const TypeInfo
*info
)
145 assert(info
->parent
);
146 return type_register_internal(info
);
149 TypeImpl
*type_register_static(const TypeInfo
*info
)
151 return type_register(info
);
154 static TypeImpl
*type_get_by_name(const char *name
)
160 return type_table_lookup(name
);
163 static TypeImpl
*type_get_parent(TypeImpl
*type
)
165 if (!type
->parent_type
&& type
->parent
) {
166 type
->parent_type
= type_get_by_name(type
->parent
);
167 g_assert(type
->parent_type
!= NULL
);
170 return type
->parent_type
;
173 static bool type_has_parent(TypeImpl
*type
)
175 return (type
->parent
!= NULL
);
178 static size_t type_class_get_size(TypeImpl
*ti
)
180 if (ti
->class_size
) {
181 return ti
->class_size
;
184 if (type_has_parent(ti
)) {
185 return type_class_get_size(type_get_parent(ti
));
188 return sizeof(ObjectClass
);
191 static size_t type_object_get_size(TypeImpl
*ti
)
193 if (ti
->instance_size
) {
194 return ti
->instance_size
;
197 if (type_has_parent(ti
)) {
198 return type_object_get_size(type_get_parent(ti
));
204 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
208 /* Check if target_type is a direct ancestor of type */
210 if (type
== target_type
) {
214 type
= type_get_parent(type
);
220 static void type_initialize(TypeImpl
*ti
);
222 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
223 TypeImpl
*parent_type
)
225 InterfaceClass
*new_iface
;
227 TypeImpl
*iface_impl
;
229 info
.parent
= parent_type
->name
;
230 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
231 info
.abstract
= true;
233 iface_impl
= type_new(&info
);
234 iface_impl
->parent_type
= parent_type
;
235 type_initialize(iface_impl
);
236 g_free((char *)info
.name
);
238 new_iface
= (InterfaceClass
*)iface_impl
->class;
239 new_iface
->concrete_class
= ti
->class;
240 new_iface
->interface_type
= interface_type
;
242 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
246 static void object_property_free(gpointer data
)
248 ObjectProperty
*prop
= data
;
252 g_free(prop
->description
);
256 static void type_initialize(TypeImpl
*ti
)
264 ti
->class_size
= type_class_get_size(ti
);
265 ti
->instance_size
= type_object_get_size(ti
);
267 ti
->class = g_malloc0(ti
->class_size
);
269 parent
= type_get_parent(ti
);
271 type_initialize(parent
);
275 g_assert_cmpint(parent
->class_size
, <=, ti
->class_size
);
276 memcpy(ti
->class, parent
->class, parent
->class_size
);
277 ti
->class->interfaces
= NULL
;
278 ti
->class->properties
= g_hash_table_new_full(
279 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
281 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
282 InterfaceClass
*iface
= e
->data
;
283 ObjectClass
*klass
= OBJECT_CLASS(iface
);
285 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
288 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
289 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
290 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
291 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
293 if (type_is_ancestor(target_type
, t
)) {
302 type_initialize_interface(ti
, t
, t
);
305 ti
->class->properties
= g_hash_table_new_full(
306 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
309 ti
->class->type
= ti
;
312 if (parent
->class_base_init
) {
313 parent
->class_base_init(ti
->class, ti
->class_data
);
315 parent
= type_get_parent(parent
);
318 if (ti
->class_init
) {
319 ti
->class_init(ti
->class, ti
->class_data
);
323 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
325 if (type_has_parent(ti
)) {
326 object_init_with_type(obj
, type_get_parent(ti
));
329 if (ti
->instance_init
) {
330 ti
->instance_init(obj
);
334 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
336 if (ti
->instance_post_init
) {
337 ti
->instance_post_init(obj
);
340 if (type_has_parent(ti
)) {
341 object_post_init_with_type(obj
, type_get_parent(ti
));
345 void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
349 g_assert(type
!= NULL
);
350 type_initialize(type
);
352 g_assert_cmpint(type
->instance_size
, >=, sizeof(Object
));
353 g_assert(type
->abstract
== false);
354 g_assert_cmpint(size
, >=, type
->instance_size
);
356 memset(obj
, 0, type
->instance_size
);
357 obj
->class = type
->class;
359 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
360 NULL
, object_property_free
);
361 object_init_with_type(obj
, type
);
362 object_post_init_with_type(obj
, type
);
365 void object_initialize(void *data
, size_t size
, const char *typename
)
367 TypeImpl
*type
= type_get_by_name(typename
);
369 object_initialize_with_type(data
, size
, type
);
372 static inline bool object_property_is_child(ObjectProperty
*prop
)
374 return strstart(prop
->type
, "child<", NULL
);
377 static void object_property_del_all(Object
*obj
)
379 ObjectProperty
*prop
;
386 g_hash_table_iter_init(&iter
, obj
->properties
);
387 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
390 prop
->release(obj
, prop
->name
, prop
->opaque
);
391 prop
->release
= NULL
;
395 g_hash_table_iter_remove(&iter
);
399 g_hash_table_unref(obj
->properties
);
402 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
404 ObjectProperty
*prop
;
408 g_hash_table_iter_init(&iter
, obj
->properties
);
409 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
411 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
413 prop
->release(obj
, prop
->name
, prop
->opaque
);
414 prop
->release
= NULL
;
419 g_hash_table_iter_init(&iter
, obj
->properties
);
420 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
422 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
423 g_hash_table_iter_remove(&iter
);
429 void object_unparent(Object
*obj
)
432 object_property_del_child(obj
->parent
, obj
, NULL
);
436 static void object_deinit(Object
*obj
, TypeImpl
*type
)
438 if (type
->instance_finalize
) {
439 type
->instance_finalize(obj
);
442 if (type_has_parent(type
)) {
443 object_deinit(obj
, type_get_parent(type
));
447 static void object_finalize(void *data
)
450 TypeImpl
*ti
= obj
->class->type
;
452 object_property_del_all(obj
);
453 object_deinit(obj
, ti
);
455 g_assert_cmpint(obj
->ref
, ==, 0);
461 Object
*object_new_with_type(Type type
)
465 g_assert(type
!= NULL
);
466 type_initialize(type
);
468 obj
= g_malloc(type
->instance_size
);
469 object_initialize_with_type(obj
, type
->instance_size
, type
);
475 Object
*object_new(const char *typename
)
477 TypeImpl
*ti
= type_get_by_name(typename
);
479 return object_new_with_type(ti
);
483 Object
*object_new_with_props(const char *typename
,
492 va_start(vargs
, errp
);
493 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
500 Object
*object_new_with_propv(const char *typename
,
508 Error
*local_err
= NULL
;
510 klass
= object_class_by_name(typename
);
512 error_setg(errp
, "invalid object type: %s", typename
);
516 if (object_class_is_abstract(klass
)) {
517 error_setg(errp
, "object type '%s' is abstract", typename
);
520 obj
= object_new(typename
);
522 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
526 object_property_add_child(parent
, id
, obj
, &local_err
);
531 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
532 user_creatable_complete(obj
, &local_err
);
534 object_unparent(obj
);
539 object_unref(OBJECT(obj
));
544 error_propagate(errp
, local_err
);
551 int object_set_props(Object
*obj
,
558 va_start(vargs
, errp
);
559 ret
= object_set_propv(obj
, errp
, vargs
);
566 int object_set_propv(Object
*obj
,
570 const char *propname
;
571 Error
*local_err
= NULL
;
573 propname
= va_arg(vargs
, char *);
574 while (propname
!= NULL
) {
575 const char *value
= va_arg(vargs
, char *);
577 g_assert(value
!= NULL
);
578 object_property_parse(obj
, value
, propname
, &local_err
);
580 error_propagate(errp
, local_err
);
583 propname
= va_arg(vargs
, char *);
590 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
592 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
599 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
600 const char *file
, int line
, const char *func
)
602 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
603 typename
, file
, line
, func
);
605 #ifdef CONFIG_QOM_CAST_DEBUG
609 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
610 if (obj
->class->object_cast_cache
[i
] == typename
) {
615 inst
= object_dynamic_cast(obj
, typename
);
618 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
619 file
, line
, func
, obj
, typename
);
625 if (obj
&& obj
== inst
) {
626 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
627 obj
->class->object_cast_cache
[i
- 1] =
628 obj
->class->object_cast_cache
[i
];
630 obj
->class->object_cast_cache
[i
- 1] = typename
;
638 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
639 const char *typename
)
641 ObjectClass
*ret
= NULL
;
642 TypeImpl
*target_type
;
649 /* A simple fast path that can trigger a lot for leaf classes. */
651 if (type
->name
== typename
) {
655 target_type
= type_get_by_name(typename
);
657 /* target class type unknown, so fail the cast */
661 if (type
->class->interfaces
&&
662 type_is_ancestor(target_type
, type_interface
)) {
666 for (i
= class->interfaces
; i
; i
= i
->next
) {
667 ObjectClass
*target_class
= i
->data
;
669 if (type_is_ancestor(target_class
->type
, target_type
)) {
675 /* The match was ambiguous, don't allow a cast */
679 } else if (type_is_ancestor(type
, target_type
)) {
686 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
687 const char *typename
,
688 const char *file
, int line
,
693 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
694 typename
, file
, line
, func
);
696 #ifdef CONFIG_QOM_CAST_DEBUG
699 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
700 if (class->class_cast_cache
[i
] == typename
) {
706 if (!class || !class->interfaces
) {
711 ret
= object_class_dynamic_cast(class, typename
);
713 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
714 file
, line
, func
, class, typename
);
718 #ifdef CONFIG_QOM_CAST_DEBUG
719 if (class && ret
== class) {
720 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
721 class->class_cast_cache
[i
- 1] = class->class_cast_cache
[i
];
723 class->class_cast_cache
[i
- 1] = typename
;
730 const char *object_get_typename(Object
*obj
)
732 return obj
->class->type
->name
;
735 ObjectClass
*object_get_class(Object
*obj
)
740 bool object_class_is_abstract(ObjectClass
*klass
)
742 return klass
->type
->abstract
;
745 const char *object_class_get_name(ObjectClass
*klass
)
747 return klass
->type
->name
;
750 ObjectClass
*object_class_by_name(const char *typename
)
752 TypeImpl
*type
= type_get_by_name(typename
);
758 type_initialize(type
);
763 ObjectClass
*object_class_get_parent(ObjectClass
*class)
765 TypeImpl
*type
= type_get_parent(class->type
);
771 type_initialize(type
);
776 typedef struct OCFData
778 void (*fn
)(ObjectClass
*klass
, void *opaque
);
779 const char *implements_type
;
780 bool include_abstract
;
784 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
787 OCFData
*data
= opaque
;
788 TypeImpl
*type
= value
;
791 type_initialize(type
);
794 if (!data
->include_abstract
&& type
->abstract
) {
798 if (data
->implements_type
&&
799 !object_class_dynamic_cast(k
, data
->implements_type
)) {
803 data
->fn(k
, data
->opaque
);
806 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
807 const char *implements_type
, bool include_abstract
,
810 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
812 enumerating_types
= true;
813 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
814 enumerating_types
= false;
817 static int do_object_child_foreach(Object
*obj
,
818 int (*fn
)(Object
*child
, void *opaque
),
819 void *opaque
, bool recurse
)
822 ObjectProperty
*prop
;
825 g_hash_table_iter_init(&iter
, obj
->properties
);
826 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
827 if (object_property_is_child(prop
)) {
828 Object
*child
= prop
->opaque
;
830 ret
= fn(child
, opaque
);
835 do_object_child_foreach(child
, fn
, opaque
, true);
842 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
845 return do_object_child_foreach(obj
, fn
, opaque
, false);
848 int object_child_foreach_recursive(Object
*obj
,
849 int (*fn
)(Object
*child
, void *opaque
),
852 return do_object_child_foreach(obj
, fn
, opaque
, true);
855 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
857 GSList
**list
= opaque
;
859 *list
= g_slist_prepend(*list
, klass
);
862 GSList
*object_class_get_list(const char *implements_type
,
863 bool include_abstract
)
867 object_class_foreach(object_class_get_list_tramp
,
868 implements_type
, include_abstract
, &list
);
872 void object_ref(Object
*obj
)
877 atomic_inc(&obj
->ref
);
880 void object_unref(Object
*obj
)
885 g_assert_cmpint(obj
->ref
, >, 0);
887 /* parent always holds a reference to its children */
888 if (atomic_fetch_dec(&obj
->ref
) == 1) {
889 object_finalize(obj
);
894 object_property_add(Object
*obj
, const char *name
, const char *type
,
895 ObjectPropertyAccessor
*get
,
896 ObjectPropertyAccessor
*set
,
897 ObjectPropertyRelease
*release
,
898 void *opaque
, Error
**errp
)
900 ObjectProperty
*prop
;
901 size_t name_len
= strlen(name
);
903 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
906 char *name_no_array
= g_strdup(name
);
908 name_no_array
[name_len
- 3] = '\0';
910 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
912 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
913 release
, opaque
, NULL
);
919 g_free(name_no_array
);
923 if (object_property_find(obj
, name
, NULL
) != NULL
) {
924 error_setg(errp
, "attempt to add duplicate property '%s'"
925 " to object (type '%s')", name
,
926 object_get_typename(obj
));
930 prop
= g_malloc0(sizeof(*prop
));
932 prop
->name
= g_strdup(name
);
933 prop
->type
= g_strdup(type
);
937 prop
->release
= release
;
938 prop
->opaque
= opaque
;
940 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
945 object_class_property_add(ObjectClass
*klass
,
948 ObjectPropertyAccessor
*get
,
949 ObjectPropertyAccessor
*set
,
950 ObjectPropertyRelease
*release
,
954 ObjectProperty
*prop
;
956 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
957 error_setg(errp
, "attempt to add duplicate property '%s'"
958 " to object (type '%s')", name
,
959 object_class_get_name(klass
));
963 prop
= g_malloc0(sizeof(*prop
));
965 prop
->name
= g_strdup(name
);
966 prop
->type
= g_strdup(type
);
970 prop
->release
= release
;
971 prop
->opaque
= opaque
;
973 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
978 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
981 ObjectProperty
*prop
;
982 ObjectClass
*klass
= object_get_class(obj
);
984 prop
= object_class_property_find(klass
, name
, NULL
);
989 prop
= g_hash_table_lookup(obj
->properties
, name
);
994 error_setg(errp
, "Property '.%s' not found", name
);
998 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1001 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1002 iter
->nextclass
= object_get_class(obj
);
1005 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1008 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1009 if (!iter
->nextclass
) {
1012 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1013 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1018 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1021 ObjectProperty
*prop
;
1022 ObjectClass
*parent_klass
;
1024 parent_klass
= object_class_get_parent(klass
);
1026 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1032 prop
= g_hash_table_lookup(klass
->properties
, name
);
1034 error_setg(errp
, "Property '.%s' not found", name
);
1039 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1041 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1044 error_setg(errp
, "Property '.%s' not found", name
);
1048 if (prop
->release
) {
1049 prop
->release(obj
, name
, prop
->opaque
);
1051 g_hash_table_remove(obj
->properties
, name
);
1054 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1057 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1063 error_setg(errp
, QERR_PERMISSION_DENIED
);
1065 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1069 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1072 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1078 error_setg(errp
, QERR_PERMISSION_DENIED
);
1080 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1084 void object_property_set_str(Object
*obj
, const char *value
,
1085 const char *name
, Error
**errp
)
1087 QString
*qstr
= qstring_from_str(value
);
1088 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1093 char *object_property_get_str(Object
*obj
, const char *name
,
1096 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1103 qstring
= qobject_to_qstring(ret
);
1105 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1108 retval
= g_strdup(qstring_get_str(qstring
));
1115 void object_property_set_link(Object
*obj
, Object
*value
,
1116 const char *name
, Error
**errp
)
1119 gchar
*path
= object_get_canonical_path(value
);
1120 object_property_set_str(obj
, path
, name
, errp
);
1123 object_property_set_str(obj
, "", name
, errp
);
1127 Object
*object_property_get_link(Object
*obj
, const char *name
,
1130 char *str
= object_property_get_str(obj
, name
, errp
);
1131 Object
*target
= NULL
;
1134 target
= object_resolve_path(str
, NULL
);
1136 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1137 "Device '%s' not found", str
);
1145 void object_property_set_bool(Object
*obj
, bool value
,
1146 const char *name
, Error
**errp
)
1148 QBool
*qbool
= qbool_from_bool(value
);
1149 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1154 bool object_property_get_bool(Object
*obj
, const char *name
,
1157 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1164 qbool
= qobject_to_qbool(ret
);
1166 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1169 retval
= qbool_get_bool(qbool
);
1176 void object_property_set_int(Object
*obj
, int64_t value
,
1177 const char *name
, Error
**errp
)
1179 QInt
*qint
= qint_from_int(value
);
1180 object_property_set_qobject(obj
, QOBJECT(qint
), name
, errp
);
1185 int64_t object_property_get_int(Object
*obj
, const char *name
,
1188 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1195 qint
= qobject_to_qint(ret
);
1197 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1200 retval
= qint_get_int(qint
);
1207 typedef struct EnumProperty
{
1208 const char * const *strings
;
1209 int (*get
)(Object
*, Error
**);
1210 void (*set
)(Object
*, int, Error
**);
1213 int object_property_get_enum(Object
*obj
, const char *name
,
1214 const char *typename
, Error
**errp
)
1217 StringOutputVisitor
*sov
;
1218 StringInputVisitor
*siv
;
1221 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1222 EnumProperty
*enumprop
;
1228 if (!g_str_equal(prop
->type
, typename
)) {
1229 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1230 name
, object_class_get_name(
1231 object_get_class(obj
)), typename
);
1235 enumprop
= prop
->opaque
;
1237 sov
= string_output_visitor_new(false);
1238 object_property_get(obj
, string_output_get_visitor(sov
), name
, &err
);
1240 error_propagate(errp
, err
);
1241 string_output_visitor_cleanup(sov
);
1244 str
= string_output_get_string(sov
);
1245 siv
= string_input_visitor_new(str
);
1246 string_output_visitor_cleanup(sov
);
1247 visit_type_enum(string_input_get_visitor(siv
), name
, &ret
,
1248 enumprop
->strings
, errp
);
1251 string_input_visitor_cleanup(siv
);
1256 void object_property_get_uint16List(Object
*obj
, const char *name
,
1257 uint16List
**list
, Error
**errp
)
1260 StringOutputVisitor
*ov
;
1261 StringInputVisitor
*iv
;
1264 ov
= string_output_visitor_new(false);
1265 object_property_get(obj
, string_output_get_visitor(ov
),
1268 error_propagate(errp
, err
);
1271 str
= string_output_get_string(ov
);
1272 iv
= string_input_visitor_new(str
);
1273 visit_type_uint16List(string_input_get_visitor(iv
), NULL
, list
, errp
);
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
,
1339 const char *name
, void *opaque
,
1342 Object
*child
= opaque
;
1345 path
= object_get_canonical_path(child
);
1346 visit_type_str(v
, name
, &path
, errp
);
1350 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1355 static void object_finalize_child_property(Object
*obj
, const char *name
,
1358 Object
*child
= opaque
;
1360 if (child
->class->unparent
) {
1361 (child
->class->unparent
)(child
);
1363 child
->parent
= NULL
;
1364 object_unref(child
);
1367 void object_property_add_child(Object
*obj
, const char *name
,
1368 Object
*child
, Error
**errp
)
1370 Error
*local_err
= NULL
;
1374 if (child
->parent
!= NULL
) {
1375 error_setg(errp
, "child object is already parented");
1379 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1381 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1382 object_finalize_child_property
, child
, &local_err
);
1384 error_propagate(errp
, local_err
);
1388 op
->resolve
= object_resolve_child_property
;
1390 child
->parent
= obj
;
1396 void object_property_allow_set_link(Object
*obj
, const char *name
,
1397 Object
*val
, Error
**errp
)
1399 /* Allow the link to be set, always */
1404 void (*check
)(Object
*, const char *, Object
*, Error
**);
1405 ObjectPropertyLinkFlags flags
;
1408 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1409 const char *name
, void *opaque
,
1412 LinkProperty
*lprop
= opaque
;
1413 Object
**child
= lprop
->child
;
1417 path
= object_get_canonical_path(*child
);
1418 visit_type_str(v
, name
, &path
, errp
);
1422 visit_type_str(v
, name
, &path
, errp
);
1427 * object_resolve_link:
1429 * Lookup an object and ensure its type matches the link property type. This
1430 * is similar to object_resolve_path() except type verification against the
1431 * link property is performed.
1433 * Returns: The matched object or NULL on path lookup failures.
1435 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1436 const char *path
, Error
**errp
)
1440 bool ambiguous
= false;
1443 /* Go from link<FOO> to FOO. */
1444 type
= object_property_get_type(obj
, name
, NULL
);
1445 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1446 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1449 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1451 } else if (!target
) {
1452 target
= object_resolve_path(path
, &ambiguous
);
1453 if (target
|| ambiguous
) {
1454 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1456 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1457 "Device '%s' not found", path
);
1461 g_free(target_type
);
1466 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1467 const char *name
, void *opaque
,
1470 Error
*local_err
= NULL
;
1471 LinkProperty
*prop
= opaque
;
1472 Object
**child
= prop
->child
;
1473 Object
*old_target
= *child
;
1474 Object
*new_target
= NULL
;
1477 visit_type_str(v
, name
, &path
, &local_err
);
1479 if (!local_err
&& strcmp(path
, "") != 0) {
1480 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1485 error_propagate(errp
, local_err
);
1489 prop
->check(obj
, name
, new_target
, &local_err
);
1491 error_propagate(errp
, local_err
);
1495 object_ref(new_target
);
1496 *child
= new_target
;
1497 object_unref(old_target
);
1500 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1502 LinkProperty
*lprop
= opaque
;
1504 return *lprop
->child
;
1507 static void object_release_link_property(Object
*obj
, const char *name
,
1510 LinkProperty
*prop
= opaque
;
1512 if ((prop
->flags
& OBJ_PROP_LINK_UNREF_ON_RELEASE
) && *prop
->child
) {
1513 object_unref(*prop
->child
);
1518 void object_property_add_link(Object
*obj
, const char *name
,
1519 const char *type
, Object
**child
,
1520 void (*check
)(Object
*, const char *,
1521 Object
*, Error
**),
1522 ObjectPropertyLinkFlags flags
,
1525 Error
*local_err
= NULL
;
1526 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1530 prop
->child
= child
;
1531 prop
->check
= check
;
1532 prop
->flags
= flags
;
1534 full_type
= g_strdup_printf("link<%s>", type
);
1536 op
= object_property_add(obj
, name
, full_type
,
1537 object_get_link_property
,
1538 check
? object_set_link_property
: NULL
,
1539 object_release_link_property
,
1543 error_propagate(errp
, local_err
);
1548 op
->resolve
= object_resolve_link_property
;
1554 void object_property_add_const_link(Object
*obj
, const char *name
,
1555 Object
*target
, Error
**errp
)
1560 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1561 op
= object_property_add(obj
, name
, link_type
,
1562 object_get_child_property
, NULL
,
1563 NULL
, target
, errp
);
1565 op
->resolve
= object_resolve_child_property
;
1570 gchar
*object_get_canonical_path_component(Object
*obj
)
1572 ObjectProperty
*prop
= NULL
;
1573 GHashTableIter iter
;
1576 g_assert(obj
->parent
!= NULL
);
1578 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1579 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1580 if (!object_property_is_child(prop
)) {
1584 if (prop
->opaque
== obj
) {
1585 return g_strdup(prop
->name
);
1589 /* obj had a parent but was not a child, should never happen */
1590 g_assert_not_reached();
1594 gchar
*object_get_canonical_path(Object
*obj
)
1596 Object
*root
= object_get_root();
1597 char *newpath
, *path
= NULL
;
1599 while (obj
!= root
) {
1600 char *component
= object_get_canonical_path_component(obj
);
1603 newpath
= g_strdup_printf("%s/%s", component
, path
);
1614 newpath
= g_strdup_printf("/%s", path
? path
: "");
1620 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1622 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1627 if (prop
->resolve
) {
1628 return prop
->resolve(parent
, prop
->opaque
, part
);
1634 static Object
*object_resolve_abs_path(Object
*parent
,
1636 const char *typename
,
1641 if (parts
[index
] == NULL
) {
1642 return object_dynamic_cast(parent
, typename
);
1645 if (strcmp(parts
[index
], "") == 0) {
1646 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1649 child
= object_resolve_path_component(parent
, parts
[index
]);
1654 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1657 static Object
*object_resolve_partial_path(Object
*parent
,
1659 const char *typename
,
1663 GHashTableIter iter
;
1664 ObjectProperty
*prop
;
1666 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1668 g_hash_table_iter_init(&iter
, parent
->properties
);
1669 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1672 if (!object_property_is_child(prop
)) {
1676 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1677 typename
, ambiguous
);
1688 if (ambiguous
&& *ambiguous
) {
1696 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1702 parts
= g_strsplit(path
, "/", 0);
1705 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1709 obj
= object_resolve_partial_path(object_get_root(), parts
,
1710 typename
, ambiguous
);
1712 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1720 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1722 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1725 typedef struct StringProperty
1727 char *(*get
)(Object
*, Error
**);
1728 void (*set
)(Object
*, const char *, Error
**);
1731 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1732 void *opaque
, Error
**errp
)
1734 StringProperty
*prop
= opaque
;
1738 value
= prop
->get(obj
, &err
);
1740 error_propagate(errp
, err
);
1744 visit_type_str(v
, name
, &value
, errp
);
1748 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1749 void *opaque
, Error
**errp
)
1751 StringProperty
*prop
= opaque
;
1753 Error
*local_err
= NULL
;
1755 visit_type_str(v
, name
, &value
, &local_err
);
1757 error_propagate(errp
, local_err
);
1761 prop
->set(obj
, value
, errp
);
1765 static void property_release_str(Object
*obj
, const char *name
,
1768 StringProperty
*prop
= opaque
;
1772 void object_property_add_str(Object
*obj
, const char *name
,
1773 char *(*get
)(Object
*, Error
**),
1774 void (*set
)(Object
*, const char *, Error
**),
1777 Error
*local_err
= NULL
;
1778 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1783 object_property_add(obj
, name
, "string",
1784 get
? property_get_str
: NULL
,
1785 set
? property_set_str
: NULL
,
1786 property_release_str
,
1789 error_propagate(errp
, local_err
);
1794 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
1795 char *(*get
)(Object
*, Error
**),
1796 void (*set
)(Object
*, const char *,
1800 Error
*local_err
= NULL
;
1801 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1806 object_class_property_add(klass
, name
, "string",
1807 get
? property_get_str
: NULL
,
1808 set
? property_set_str
: NULL
,
1809 property_release_str
,
1812 error_propagate(errp
, local_err
);
1817 typedef struct BoolProperty
1819 bool (*get
)(Object
*, Error
**);
1820 void (*set
)(Object
*, bool, Error
**);
1823 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
1824 void *opaque
, Error
**errp
)
1826 BoolProperty
*prop
= opaque
;
1830 value
= prop
->get(obj
, &err
);
1832 error_propagate(errp
, err
);
1836 visit_type_bool(v
, name
, &value
, errp
);
1839 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
1840 void *opaque
, Error
**errp
)
1842 BoolProperty
*prop
= opaque
;
1844 Error
*local_err
= NULL
;
1846 visit_type_bool(v
, name
, &value
, &local_err
);
1848 error_propagate(errp
, local_err
);
1852 prop
->set(obj
, value
, errp
);
1855 static void property_release_bool(Object
*obj
, const char *name
,
1858 BoolProperty
*prop
= opaque
;
1862 void object_property_add_bool(Object
*obj
, const char *name
,
1863 bool (*get
)(Object
*, Error
**),
1864 void (*set
)(Object
*, bool, Error
**),
1867 Error
*local_err
= NULL
;
1868 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1873 object_property_add(obj
, name
, "bool",
1874 get
? property_get_bool
: NULL
,
1875 set
? property_set_bool
: NULL
,
1876 property_release_bool
,
1879 error_propagate(errp
, local_err
);
1884 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
1885 bool (*get
)(Object
*, Error
**),
1886 void (*set
)(Object
*, bool, Error
**),
1889 Error
*local_err
= NULL
;
1890 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1895 object_class_property_add(klass
, name
, "bool",
1896 get
? property_get_bool
: NULL
,
1897 set
? property_set_bool
: NULL
,
1898 property_release_bool
,
1901 error_propagate(errp
, local_err
);
1906 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
1907 void *opaque
, Error
**errp
)
1909 EnumProperty
*prop
= opaque
;
1913 value
= prop
->get(obj
, &err
);
1915 error_propagate(errp
, err
);
1919 visit_type_enum(v
, name
, &value
, prop
->strings
, errp
);
1922 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
1923 void *opaque
, Error
**errp
)
1925 EnumProperty
*prop
= opaque
;
1929 visit_type_enum(v
, name
, &value
, prop
->strings
, &err
);
1931 error_propagate(errp
, err
);
1934 prop
->set(obj
, value
, errp
);
1937 static void property_release_enum(Object
*obj
, const char *name
,
1940 EnumProperty
*prop
= opaque
;
1944 void object_property_add_enum(Object
*obj
, const char *name
,
1945 const char *typename
,
1946 const char * const *strings
,
1947 int (*get
)(Object
*, Error
**),
1948 void (*set
)(Object
*, int, Error
**),
1951 Error
*local_err
= NULL
;
1952 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
1954 prop
->strings
= strings
;
1958 object_property_add(obj
, name
, typename
,
1959 get
? property_get_enum
: NULL
,
1960 set
? property_set_enum
: NULL
,
1961 property_release_enum
,
1964 error_propagate(errp
, local_err
);
1969 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
1970 const char *typename
,
1971 const char * const *strings
,
1972 int (*get
)(Object
*, Error
**),
1973 void (*set
)(Object
*, int, Error
**),
1976 Error
*local_err
= NULL
;
1977 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
1979 prop
->strings
= strings
;
1983 object_class_property_add(klass
, name
, typename
,
1984 get
? property_get_enum
: NULL
,
1985 set
? property_set_enum
: NULL
,
1986 property_release_enum
,
1989 error_propagate(errp
, local_err
);
1994 typedef struct TMProperty
{
1995 void (*get
)(Object
*, struct tm
*, Error
**);
1998 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
1999 void *opaque
, Error
**errp
)
2001 TMProperty
*prop
= opaque
;
2005 prop
->get(obj
, &value
, &err
);
2010 visit_start_struct(v
, name
, NULL
, 0, &err
);
2014 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2018 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2022 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2026 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2030 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2034 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2039 error_propagate(errp
, err
);
2041 visit_end_struct(v
, errp
);
2043 error_propagate(errp
, err
);
2047 static void property_release_tm(Object
*obj
, const char *name
,
2050 TMProperty
*prop
= opaque
;
2054 void object_property_add_tm(Object
*obj
, const char *name
,
2055 void (*get
)(Object
*, struct tm
*, Error
**),
2058 Error
*local_err
= NULL
;
2059 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2063 object_property_add(obj
, name
, "struct tm",
2064 get
? property_get_tm
: NULL
, NULL
,
2065 property_release_tm
,
2068 error_propagate(errp
, local_err
);
2073 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2074 void (*get
)(Object
*, struct tm
*, Error
**),
2077 Error
*local_err
= NULL
;
2078 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2082 object_class_property_add(klass
, name
, "struct tm",
2083 get
? property_get_tm
: NULL
, NULL
,
2084 property_release_tm
,
2087 error_propagate(errp
, local_err
);
2092 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2094 return g_strdup(object_get_typename(obj
));
2097 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2098 void *opaque
, Error
**errp
)
2100 uint8_t value
= *(uint8_t *)opaque
;
2101 visit_type_uint8(v
, name
, &value
, errp
);
2104 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2105 void *opaque
, Error
**errp
)
2107 uint16_t value
= *(uint16_t *)opaque
;
2108 visit_type_uint16(v
, name
, &value
, errp
);
2111 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2112 void *opaque
, Error
**errp
)
2114 uint32_t value
= *(uint32_t *)opaque
;
2115 visit_type_uint32(v
, name
, &value
, errp
);
2118 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2119 void *opaque
, Error
**errp
)
2121 uint64_t value
= *(uint64_t *)opaque
;
2122 visit_type_uint64(v
, name
, &value
, errp
);
2125 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2126 const uint8_t *v
, Error
**errp
)
2128 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2129 NULL
, NULL
, (void *)v
, errp
);
2132 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2133 const uint8_t *v
, Error
**errp
)
2135 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2136 NULL
, NULL
, (void *)v
, errp
);
2139 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2140 const uint16_t *v
, Error
**errp
)
2142 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2143 NULL
, NULL
, (void *)v
, errp
);
2146 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2147 const uint16_t *v
, Error
**errp
)
2149 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2150 NULL
, NULL
, (void *)v
, errp
);
2153 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2154 const uint32_t *v
, Error
**errp
)
2156 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2157 NULL
, NULL
, (void *)v
, errp
);
2160 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2161 const uint32_t *v
, Error
**errp
)
2163 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2164 NULL
, NULL
, (void *)v
, errp
);
2167 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2168 const uint64_t *v
, Error
**errp
)
2170 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2171 NULL
, NULL
, (void *)v
, errp
);
2174 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2175 const uint64_t *v
, Error
**errp
)
2177 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2178 NULL
, NULL
, (void *)v
, errp
);
2186 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2187 void *opaque
, Error
**errp
)
2189 AliasProperty
*prop
= opaque
;
2191 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2194 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2195 void *opaque
, Error
**errp
)
2197 AliasProperty
*prop
= opaque
;
2199 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2202 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2205 AliasProperty
*prop
= opaque
;
2207 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2210 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2212 AliasProperty
*prop
= opaque
;
2214 g_free(prop
->target_name
);
2218 void object_property_add_alias(Object
*obj
, const char *name
,
2219 Object
*target_obj
, const char *target_name
,
2222 AliasProperty
*prop
;
2224 ObjectProperty
*target_prop
;
2226 Error
*local_err
= NULL
;
2228 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2233 if (object_property_is_child(target_prop
)) {
2234 prop_type
= g_strdup_printf("link%s",
2235 target_prop
->type
+ strlen("child"));
2237 prop_type
= g_strdup(target_prop
->type
);
2240 prop
= g_malloc(sizeof(*prop
));
2241 prop
->target_obj
= target_obj
;
2242 prop
->target_name
= g_strdup(target_name
);
2244 op
= object_property_add(obj
, name
, prop_type
,
2247 property_release_alias
,
2250 error_propagate(errp
, local_err
);
2254 op
->resolve
= property_resolve_alias
;
2256 object_property_set_description(obj
, op
->name
,
2257 target_prop
->description
,
2264 void object_property_set_description(Object
*obj
, const char *name
,
2265 const char *description
, Error
**errp
)
2269 op
= object_property_find(obj
, name
, errp
);
2274 g_free(op
->description
);
2275 op
->description
= g_strdup(description
);
2278 void object_class_property_set_description(ObjectClass
*klass
,
2280 const char *description
,
2285 op
= g_hash_table_lookup(klass
->properties
, name
);
2287 error_setg(errp
, "Property '.%s' not found", name
);
2291 g_free(op
->description
);
2292 op
->description
= g_strdup(description
);
2295 static void object_instance_init(Object
*obj
)
2297 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
2300 static void register_types(void)
2302 static TypeInfo interface_info
= {
2303 .name
= TYPE_INTERFACE
,
2304 .class_size
= sizeof(InterfaceClass
),
2308 static TypeInfo object_info
= {
2309 .name
= TYPE_OBJECT
,
2310 .instance_size
= sizeof(Object
),
2311 .instance_init
= object_instance_init
,
2315 type_interface
= type_register_internal(&interface_info
);
2316 type_register_internal(&object_info
);
2319 type_init(register_types
)