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/qbool.h"
29 #include "qapi/qmp/qnum.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 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
158 for (i
= 0; i
< nr_infos
; i
++) {
159 type_register_static(&infos
[i
]);
163 static TypeImpl
*type_get_by_name(const char *name
)
169 return type_table_lookup(name
);
172 static TypeImpl
*type_get_parent(TypeImpl
*type
)
174 if (!type
->parent_type
&& type
->parent
) {
175 type
->parent_type
= type_get_by_name(type
->parent
);
176 g_assert(type
->parent_type
!= NULL
);
179 return type
->parent_type
;
182 static bool type_has_parent(TypeImpl
*type
)
184 return (type
->parent
!= NULL
);
187 static size_t type_class_get_size(TypeImpl
*ti
)
189 if (ti
->class_size
) {
190 return ti
->class_size
;
193 if (type_has_parent(ti
)) {
194 return type_class_get_size(type_get_parent(ti
));
197 return sizeof(ObjectClass
);
200 static size_t type_object_get_size(TypeImpl
*ti
)
202 if (ti
->instance_size
) {
203 return ti
->instance_size
;
206 if (type_has_parent(ti
)) {
207 return type_object_get_size(type_get_parent(ti
));
213 size_t object_type_get_instance_size(const char *typename
)
215 TypeImpl
*type
= type_get_by_name(typename
);
217 g_assert(type
!= NULL
);
218 return type_object_get_size(type
);
221 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
225 /* Check if target_type is a direct ancestor of type */
227 if (type
== target_type
) {
231 type
= type_get_parent(type
);
237 static void type_initialize(TypeImpl
*ti
);
239 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
240 TypeImpl
*parent_type
)
242 InterfaceClass
*new_iface
;
244 TypeImpl
*iface_impl
;
246 info
.parent
= parent_type
->name
;
247 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
248 info
.abstract
= true;
250 iface_impl
= type_new(&info
);
251 iface_impl
->parent_type
= parent_type
;
252 type_initialize(iface_impl
);
253 g_free((char *)info
.name
);
255 new_iface
= (InterfaceClass
*)iface_impl
->class;
256 new_iface
->concrete_class
= ti
->class;
257 new_iface
->interface_type
= interface_type
;
259 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
263 static void object_property_free(gpointer data
)
265 ObjectProperty
*prop
= data
;
269 g_free(prop
->description
);
273 static void type_initialize(TypeImpl
*ti
)
281 ti
->class_size
= type_class_get_size(ti
);
282 ti
->instance_size
= type_object_get_size(ti
);
283 /* Any type with zero instance_size is implicitly abstract.
284 * This means interface types are all abstract.
286 if (ti
->instance_size
== 0) {
290 ti
->class = g_malloc0(ti
->class_size
);
292 parent
= type_get_parent(ti
);
294 type_initialize(parent
);
298 g_assert_cmpint(parent
->class_size
, <=, ti
->class_size
);
299 memcpy(ti
->class, parent
->class, parent
->class_size
);
300 ti
->class->interfaces
= NULL
;
301 ti
->class->properties
= g_hash_table_new_full(
302 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
304 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
305 InterfaceClass
*iface
= e
->data
;
306 ObjectClass
*klass
= OBJECT_CLASS(iface
);
308 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
311 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
312 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
313 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
314 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
316 if (type_is_ancestor(target_type
, t
)) {
325 type_initialize_interface(ti
, t
, t
);
328 ti
->class->properties
= g_hash_table_new_full(
329 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
332 ti
->class->type
= ti
;
335 if (parent
->class_base_init
) {
336 parent
->class_base_init(ti
->class, ti
->class_data
);
338 parent
= type_get_parent(parent
);
341 if (ti
->class_init
) {
342 ti
->class_init(ti
->class, ti
->class_data
);
346 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
348 if (type_has_parent(ti
)) {
349 object_init_with_type(obj
, type_get_parent(ti
));
352 if (ti
->instance_init
) {
353 ti
->instance_init(obj
);
357 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
359 if (ti
->instance_post_init
) {
360 ti
->instance_post_init(obj
);
363 if (type_has_parent(ti
)) {
364 object_post_init_with_type(obj
, type_get_parent(ti
));
368 static void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
372 g_assert(type
!= NULL
);
373 type_initialize(type
);
375 g_assert_cmpint(type
->instance_size
, >=, sizeof(Object
));
376 g_assert(type
->abstract
== false);
377 g_assert_cmpint(size
, >=, type
->instance_size
);
379 memset(obj
, 0, type
->instance_size
);
380 obj
->class = type
->class;
382 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
383 NULL
, object_property_free
);
384 object_init_with_type(obj
, type
);
385 object_post_init_with_type(obj
, type
);
388 void object_initialize(void *data
, size_t size
, const char *typename
)
390 TypeImpl
*type
= type_get_by_name(typename
);
392 object_initialize_with_type(data
, size
, type
);
395 static inline bool object_property_is_child(ObjectProperty
*prop
)
397 return strstart(prop
->type
, "child<", NULL
);
400 static void object_property_del_all(Object
*obj
)
402 ObjectProperty
*prop
;
409 g_hash_table_iter_init(&iter
, obj
->properties
);
410 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
413 prop
->release(obj
, prop
->name
, prop
->opaque
);
414 prop
->release
= NULL
;
418 g_hash_table_iter_remove(&iter
);
422 g_hash_table_unref(obj
->properties
);
425 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
427 ObjectProperty
*prop
;
431 g_hash_table_iter_init(&iter
, obj
->properties
);
432 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
434 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
436 prop
->release(obj
, prop
->name
, prop
->opaque
);
437 prop
->release
= NULL
;
442 g_hash_table_iter_init(&iter
, obj
->properties
);
443 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
445 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
446 g_hash_table_iter_remove(&iter
);
452 void object_unparent(Object
*obj
)
455 object_property_del_child(obj
->parent
, obj
, NULL
);
459 static void object_deinit(Object
*obj
, TypeImpl
*type
)
461 if (type
->instance_finalize
) {
462 type
->instance_finalize(obj
);
465 if (type_has_parent(type
)) {
466 object_deinit(obj
, type_get_parent(type
));
470 static void object_finalize(void *data
)
473 TypeImpl
*ti
= obj
->class->type
;
475 object_property_del_all(obj
);
476 object_deinit(obj
, ti
);
478 g_assert_cmpint(obj
->ref
, ==, 0);
484 static Object
*object_new_with_type(Type type
)
488 g_assert(type
!= NULL
);
489 type_initialize(type
);
491 obj
= g_malloc(type
->instance_size
);
492 object_initialize_with_type(obj
, type
->instance_size
, type
);
498 Object
*object_new(const char *typename
)
500 TypeImpl
*ti
= type_get_by_name(typename
);
502 return object_new_with_type(ti
);
506 Object
*object_new_with_props(const char *typename
,
515 va_start(vargs
, errp
);
516 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
523 Object
*object_new_with_propv(const char *typename
,
531 Error
*local_err
= NULL
;
533 klass
= object_class_by_name(typename
);
535 error_setg(errp
, "invalid object type: %s", typename
);
539 if (object_class_is_abstract(klass
)) {
540 error_setg(errp
, "object type '%s' is abstract", typename
);
543 obj
= object_new(typename
);
545 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
549 object_property_add_child(parent
, id
, obj
, &local_err
);
554 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
555 user_creatable_complete(obj
, &local_err
);
557 object_unparent(obj
);
562 object_unref(OBJECT(obj
));
566 error_propagate(errp
, local_err
);
572 int object_set_props(Object
*obj
,
579 va_start(vargs
, errp
);
580 ret
= object_set_propv(obj
, errp
, vargs
);
587 int object_set_propv(Object
*obj
,
591 const char *propname
;
592 Error
*local_err
= NULL
;
594 propname
= va_arg(vargs
, char *);
595 while (propname
!= NULL
) {
596 const char *value
= va_arg(vargs
, char *);
598 g_assert(value
!= NULL
);
599 object_property_parse(obj
, value
, propname
, &local_err
);
601 error_propagate(errp
, local_err
);
604 propname
= va_arg(vargs
, char *);
611 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
613 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
620 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
621 const char *file
, int line
, const char *func
)
623 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
624 typename
, file
, line
, func
);
626 #ifdef CONFIG_QOM_CAST_DEBUG
630 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
631 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
636 inst
= object_dynamic_cast(obj
, typename
);
639 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
640 file
, line
, func
, obj
, typename
);
646 if (obj
&& obj
== inst
) {
647 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
648 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
649 atomic_read(&obj
->class->object_cast_cache
[i
]));
651 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
659 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
660 const char *typename
)
662 ObjectClass
*ret
= NULL
;
663 TypeImpl
*target_type
;
670 /* A simple fast path that can trigger a lot for leaf classes. */
672 if (type
->name
== typename
) {
676 target_type
= type_get_by_name(typename
);
678 /* target class type unknown, so fail the cast */
682 if (type
->class->interfaces
&&
683 type_is_ancestor(target_type
, type_interface
)) {
687 for (i
= class->interfaces
; i
; i
= i
->next
) {
688 ObjectClass
*target_class
= i
->data
;
690 if (type_is_ancestor(target_class
->type
, target_type
)) {
696 /* The match was ambiguous, don't allow a cast */
700 } else if (type_is_ancestor(type
, target_type
)) {
707 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
708 const char *typename
,
709 const char *file
, int line
,
714 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
715 typename
, file
, line
, func
);
717 #ifdef CONFIG_QOM_CAST_DEBUG
720 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
721 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
727 if (!class || !class->interfaces
) {
732 ret
= object_class_dynamic_cast(class, typename
);
734 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
735 file
, line
, func
, class, typename
);
739 #ifdef CONFIG_QOM_CAST_DEBUG
740 if (class && ret
== class) {
741 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
742 atomic_set(&class->class_cast_cache
[i
- 1],
743 atomic_read(&class->class_cast_cache
[i
]));
745 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
752 const char *object_get_typename(const Object
*obj
)
754 return obj
->class->type
->name
;
757 ObjectClass
*object_get_class(Object
*obj
)
762 bool object_class_is_abstract(ObjectClass
*klass
)
764 return klass
->type
->abstract
;
767 const char *object_class_get_name(ObjectClass
*klass
)
769 return klass
->type
->name
;
772 ObjectClass
*object_class_by_name(const char *typename
)
774 TypeImpl
*type
= type_get_by_name(typename
);
780 type_initialize(type
);
785 ObjectClass
*object_class_get_parent(ObjectClass
*class)
787 TypeImpl
*type
= type_get_parent(class->type
);
793 type_initialize(type
);
798 typedef struct OCFData
800 void (*fn
)(ObjectClass
*klass
, void *opaque
);
801 const char *implements_type
;
802 bool include_abstract
;
806 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
809 OCFData
*data
= opaque
;
810 TypeImpl
*type
= value
;
813 type_initialize(type
);
816 if (!data
->include_abstract
&& type
->abstract
) {
820 if (data
->implements_type
&&
821 !object_class_dynamic_cast(k
, data
->implements_type
)) {
825 data
->fn(k
, data
->opaque
);
828 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
829 const char *implements_type
, bool include_abstract
,
832 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
834 enumerating_types
= true;
835 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
836 enumerating_types
= false;
839 static int do_object_child_foreach(Object
*obj
,
840 int (*fn
)(Object
*child
, void *opaque
),
841 void *opaque
, bool recurse
)
844 ObjectProperty
*prop
;
847 g_hash_table_iter_init(&iter
, obj
->properties
);
848 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
849 if (object_property_is_child(prop
)) {
850 Object
*child
= prop
->opaque
;
852 ret
= fn(child
, opaque
);
857 do_object_child_foreach(child
, fn
, opaque
, true);
864 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
867 return do_object_child_foreach(obj
, fn
, opaque
, false);
870 int object_child_foreach_recursive(Object
*obj
,
871 int (*fn
)(Object
*child
, void *opaque
),
874 return do_object_child_foreach(obj
, fn
, opaque
, true);
877 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
879 GSList
**list
= opaque
;
881 *list
= g_slist_prepend(*list
, klass
);
884 GSList
*object_class_get_list(const char *implements_type
,
885 bool include_abstract
)
889 object_class_foreach(object_class_get_list_tramp
,
890 implements_type
, include_abstract
, &list
);
894 void object_ref(Object
*obj
)
899 atomic_inc(&obj
->ref
);
902 void object_unref(Object
*obj
)
907 g_assert_cmpint(obj
->ref
, >, 0);
909 /* parent always holds a reference to its children */
910 if (atomic_fetch_dec(&obj
->ref
) == 1) {
911 object_finalize(obj
);
916 object_property_add(Object
*obj
, const char *name
, const char *type
,
917 ObjectPropertyAccessor
*get
,
918 ObjectPropertyAccessor
*set
,
919 ObjectPropertyRelease
*release
,
920 void *opaque
, Error
**errp
)
922 ObjectProperty
*prop
;
923 size_t name_len
= strlen(name
);
925 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
928 char *name_no_array
= g_strdup(name
);
930 name_no_array
[name_len
- 3] = '\0';
932 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
934 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
935 release
, opaque
, NULL
);
941 g_free(name_no_array
);
945 if (object_property_find(obj
, name
, NULL
) != NULL
) {
946 error_setg(errp
, "attempt to add duplicate property '%s'"
947 " to object (type '%s')", name
,
948 object_get_typename(obj
));
952 prop
= g_malloc0(sizeof(*prop
));
954 prop
->name
= g_strdup(name
);
955 prop
->type
= g_strdup(type
);
959 prop
->release
= release
;
960 prop
->opaque
= opaque
;
962 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
967 object_class_property_add(ObjectClass
*klass
,
970 ObjectPropertyAccessor
*get
,
971 ObjectPropertyAccessor
*set
,
972 ObjectPropertyRelease
*release
,
976 ObjectProperty
*prop
;
978 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
979 error_setg(errp
, "attempt to add duplicate property '%s'"
980 " to object (type '%s')", name
,
981 object_class_get_name(klass
));
985 prop
= g_malloc0(sizeof(*prop
));
987 prop
->name
= g_strdup(name
);
988 prop
->type
= g_strdup(type
);
992 prop
->release
= release
;
993 prop
->opaque
= opaque
;
995 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
1000 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1003 ObjectProperty
*prop
;
1004 ObjectClass
*klass
= object_get_class(obj
);
1006 prop
= object_class_property_find(klass
, name
, NULL
);
1011 prop
= g_hash_table_lookup(obj
->properties
, name
);
1016 error_setg(errp
, "Property '.%s' not found", name
);
1020 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1023 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1024 iter
->nextclass
= object_get_class(obj
);
1027 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1030 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1031 if (!iter
->nextclass
) {
1034 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1035 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1040 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1043 ObjectProperty
*prop
;
1044 ObjectClass
*parent_klass
;
1046 parent_klass
= object_class_get_parent(klass
);
1048 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1054 prop
= g_hash_table_lookup(klass
->properties
, name
);
1056 error_setg(errp
, "Property '.%s' not found", name
);
1061 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1063 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1066 error_setg(errp
, "Property '.%s' not found", name
);
1070 if (prop
->release
) {
1071 prop
->release(obj
, name
, prop
->opaque
);
1073 g_hash_table_remove(obj
->properties
, name
);
1076 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1079 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1085 error_setg(errp
, QERR_PERMISSION_DENIED
);
1087 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1091 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1094 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1100 error_setg(errp
, QERR_PERMISSION_DENIED
);
1102 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1106 void object_property_set_str(Object
*obj
, const char *value
,
1107 const char *name
, Error
**errp
)
1109 QString
*qstr
= qstring_from_str(value
);
1110 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1115 char *object_property_get_str(Object
*obj
, const char *name
,
1118 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1125 qstring
= qobject_to_qstring(ret
);
1127 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1130 retval
= g_strdup(qstring_get_str(qstring
));
1133 qobject_decref(ret
);
1137 void object_property_set_link(Object
*obj
, Object
*value
,
1138 const char *name
, Error
**errp
)
1141 gchar
*path
= object_get_canonical_path(value
);
1142 object_property_set_str(obj
, path
, name
, errp
);
1145 object_property_set_str(obj
, "", name
, errp
);
1149 Object
*object_property_get_link(Object
*obj
, const char *name
,
1152 char *str
= object_property_get_str(obj
, name
, errp
);
1153 Object
*target
= NULL
;
1156 target
= object_resolve_path(str
, NULL
);
1158 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1159 "Device '%s' not found", str
);
1167 void object_property_set_bool(Object
*obj
, bool value
,
1168 const char *name
, Error
**errp
)
1170 QBool
*qbool
= qbool_from_bool(value
);
1171 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1176 bool object_property_get_bool(Object
*obj
, const char *name
,
1179 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1186 qbool
= qobject_to_qbool(ret
);
1188 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1191 retval
= qbool_get_bool(qbool
);
1194 qobject_decref(ret
);
1198 void object_property_set_int(Object
*obj
, int64_t value
,
1199 const char *name
, Error
**errp
)
1201 QNum
*qnum
= qnum_from_int(value
);
1202 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1207 int64_t object_property_get_int(Object
*obj
, const char *name
,
1210 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1218 qnum
= qobject_to_qnum(ret
);
1219 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1220 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1224 qobject_decref(ret
);
1228 void object_property_set_uint(Object
*obj
, uint64_t value
,
1229 const char *name
, Error
**errp
)
1231 QNum
*qnum
= qnum_from_uint(value
);
1233 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1237 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1240 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1247 qnum
= qobject_to_qnum(ret
);
1248 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1249 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1253 qobject_decref(ret
);
1257 typedef struct EnumProperty
{
1258 const QEnumLookup
*lookup
;
1259 int (*get
)(Object
*, Error
**);
1260 void (*set
)(Object
*, int, Error
**);
1263 int object_property_get_enum(Object
*obj
, const char *name
,
1264 const char *typename
, Error
**errp
)
1270 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1271 EnumProperty
*enumprop
;
1277 if (!g_str_equal(prop
->type
, typename
)) {
1278 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1279 name
, object_class_get_name(
1280 object_get_class(obj
)), typename
);
1284 enumprop
= prop
->opaque
;
1286 v
= string_output_visitor_new(false, &str
);
1287 object_property_get(obj
, v
, name
, &err
);
1289 error_propagate(errp
, err
);
1293 visit_complete(v
, &str
);
1295 v
= string_input_visitor_new(str
);
1296 visit_type_enum(v
, name
, &ret
, enumprop
->lookup
, errp
);
1304 void object_property_get_uint16List(Object
*obj
, const char *name
,
1305 uint16List
**list
, Error
**errp
)
1311 v
= string_output_visitor_new(false, &str
);
1312 object_property_get(obj
, v
, name
, &err
);
1314 error_propagate(errp
, err
);
1317 visit_complete(v
, &str
);
1319 v
= string_input_visitor_new(str
);
1320 visit_type_uint16List(v
, NULL
, list
, errp
);
1327 void object_property_parse(Object
*obj
, const char *string
,
1328 const char *name
, Error
**errp
)
1330 Visitor
*v
= string_input_visitor_new(string
);
1331 object_property_set(obj
, v
, name
, errp
);
1335 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1339 char *string
= NULL
;
1340 Error
*local_err
= NULL
;
1342 v
= string_output_visitor_new(human
, &string
);
1343 object_property_get(obj
, v
, name
, &local_err
);
1345 error_propagate(errp
, local_err
);
1349 visit_complete(v
, &string
);
1356 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1358 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1366 Object
*object_get_root(void)
1368 static Object
*root
;
1371 root
= object_new("container");
1377 Object
*object_get_objects_root(void)
1379 return container_get(object_get_root(), "/objects");
1382 Object
*object_get_internal_root(void)
1384 static Object
*internal_root
;
1386 if (!internal_root
) {
1387 internal_root
= object_new("container");
1390 return internal_root
;
1393 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1394 const char *name
, void *opaque
,
1397 Object
*child
= opaque
;
1400 path
= object_get_canonical_path(child
);
1401 visit_type_str(v
, name
, &path
, errp
);
1405 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1410 static void object_finalize_child_property(Object
*obj
, const char *name
,
1413 Object
*child
= opaque
;
1415 if (child
->class->unparent
) {
1416 (child
->class->unparent
)(child
);
1418 child
->parent
= NULL
;
1419 object_unref(child
);
1422 void object_property_add_child(Object
*obj
, const char *name
,
1423 Object
*child
, Error
**errp
)
1425 Error
*local_err
= NULL
;
1429 if (child
->parent
!= NULL
) {
1430 error_setg(errp
, "child object is already parented");
1434 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1436 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1437 object_finalize_child_property
, child
, &local_err
);
1439 error_propagate(errp
, local_err
);
1443 op
->resolve
= object_resolve_child_property
;
1445 child
->parent
= obj
;
1451 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1452 Object
*val
, Error
**errp
)
1454 /* Allow the link to be set, always */
1459 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1460 ObjectPropertyLinkFlags flags
;
1463 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1464 const char *name
, void *opaque
,
1467 LinkProperty
*lprop
= opaque
;
1468 Object
**child
= lprop
->child
;
1472 path
= object_get_canonical_path(*child
);
1473 visit_type_str(v
, name
, &path
, errp
);
1477 visit_type_str(v
, name
, &path
, errp
);
1482 * object_resolve_link:
1484 * Lookup an object and ensure its type matches the link property type. This
1485 * is similar to object_resolve_path() except type verification against the
1486 * link property is performed.
1488 * Returns: The matched object or NULL on path lookup failures.
1490 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1491 const char *path
, Error
**errp
)
1495 bool ambiguous
= false;
1498 /* Go from link<FOO> to FOO. */
1499 type
= object_property_get_type(obj
, name
, NULL
);
1500 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1501 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1504 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1506 } else if (!target
) {
1507 target
= object_resolve_path(path
, &ambiguous
);
1508 if (target
|| ambiguous
) {
1509 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1511 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1512 "Device '%s' not found", path
);
1516 g_free(target_type
);
1521 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1522 const char *name
, void *opaque
,
1525 Error
*local_err
= NULL
;
1526 LinkProperty
*prop
= opaque
;
1527 Object
**child
= prop
->child
;
1528 Object
*old_target
= *child
;
1529 Object
*new_target
= NULL
;
1532 visit_type_str(v
, name
, &path
, &local_err
);
1534 if (!local_err
&& strcmp(path
, "") != 0) {
1535 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1540 error_propagate(errp
, local_err
);
1544 prop
->check(obj
, name
, new_target
, &local_err
);
1546 error_propagate(errp
, local_err
);
1550 object_ref(new_target
);
1551 *child
= new_target
;
1552 object_unref(old_target
);
1555 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1557 LinkProperty
*lprop
= opaque
;
1559 return *lprop
->child
;
1562 static void object_release_link_property(Object
*obj
, const char *name
,
1565 LinkProperty
*prop
= opaque
;
1567 if ((prop
->flags
& OBJ_PROP_LINK_UNREF_ON_RELEASE
) && *prop
->child
) {
1568 object_unref(*prop
->child
);
1573 void object_property_add_link(Object
*obj
, const char *name
,
1574 const char *type
, Object
**child
,
1575 void (*check
)(const Object
*, const char *,
1576 Object
*, Error
**),
1577 ObjectPropertyLinkFlags flags
,
1580 Error
*local_err
= NULL
;
1581 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1585 prop
->child
= child
;
1586 prop
->check
= check
;
1587 prop
->flags
= flags
;
1589 full_type
= g_strdup_printf("link<%s>", type
);
1591 op
= object_property_add(obj
, name
, full_type
,
1592 object_get_link_property
,
1593 check
? object_set_link_property
: NULL
,
1594 object_release_link_property
,
1598 error_propagate(errp
, local_err
);
1603 op
->resolve
= object_resolve_link_property
;
1609 void object_property_add_const_link(Object
*obj
, const char *name
,
1610 Object
*target
, Error
**errp
)
1615 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1616 op
= object_property_add(obj
, name
, link_type
,
1617 object_get_child_property
, NULL
,
1618 NULL
, target
, errp
);
1620 op
->resolve
= object_resolve_child_property
;
1625 gchar
*object_get_canonical_path_component(Object
*obj
)
1627 ObjectProperty
*prop
= NULL
;
1628 GHashTableIter iter
;
1631 g_assert(obj
->parent
!= NULL
);
1633 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1634 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1635 if (!object_property_is_child(prop
)) {
1639 if (prop
->opaque
== obj
) {
1640 return g_strdup(prop
->name
);
1644 /* obj had a parent but was not a child, should never happen */
1645 g_assert_not_reached();
1649 gchar
*object_get_canonical_path(Object
*obj
)
1651 Object
*root
= object_get_root();
1652 char *newpath
, *path
= NULL
;
1654 while (obj
!= root
) {
1655 char *component
= object_get_canonical_path_component(obj
);
1658 newpath
= g_strdup_printf("%s/%s", component
, path
);
1669 newpath
= g_strdup_printf("/%s", path
? path
: "");
1675 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1677 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1682 if (prop
->resolve
) {
1683 return prop
->resolve(parent
, prop
->opaque
, part
);
1689 static Object
*object_resolve_abs_path(Object
*parent
,
1691 const char *typename
,
1696 if (parts
[index
] == NULL
) {
1697 return object_dynamic_cast(parent
, typename
);
1700 if (strcmp(parts
[index
], "") == 0) {
1701 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1704 child
= object_resolve_path_component(parent
, parts
[index
]);
1709 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1712 static Object
*object_resolve_partial_path(Object
*parent
,
1714 const char *typename
,
1718 GHashTableIter iter
;
1719 ObjectProperty
*prop
;
1721 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1723 g_hash_table_iter_init(&iter
, parent
->properties
);
1724 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1727 if (!object_property_is_child(prop
)) {
1731 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1732 typename
, ambiguous
);
1749 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1755 parts
= g_strsplit(path
, "/", 0);
1758 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1759 bool ambiguous
= false;
1760 obj
= object_resolve_partial_path(object_get_root(), parts
,
1761 typename
, &ambiguous
);
1763 *ambiguousp
= ambiguous
;
1766 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1774 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1776 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1779 typedef struct StringProperty
1781 char *(*get
)(Object
*, Error
**);
1782 void (*set
)(Object
*, const char *, Error
**);
1785 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1786 void *opaque
, Error
**errp
)
1788 StringProperty
*prop
= opaque
;
1792 value
= prop
->get(obj
, &err
);
1794 error_propagate(errp
, err
);
1798 visit_type_str(v
, name
, &value
, errp
);
1802 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1803 void *opaque
, Error
**errp
)
1805 StringProperty
*prop
= opaque
;
1807 Error
*local_err
= NULL
;
1809 visit_type_str(v
, name
, &value
, &local_err
);
1811 error_propagate(errp
, local_err
);
1815 prop
->set(obj
, value
, errp
);
1819 static void property_release_str(Object
*obj
, const char *name
,
1822 StringProperty
*prop
= opaque
;
1826 void object_property_add_str(Object
*obj
, const char *name
,
1827 char *(*get
)(Object
*, Error
**),
1828 void (*set
)(Object
*, const char *, Error
**),
1831 Error
*local_err
= NULL
;
1832 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1837 object_property_add(obj
, name
, "string",
1838 get
? property_get_str
: NULL
,
1839 set
? property_set_str
: NULL
,
1840 property_release_str
,
1843 error_propagate(errp
, local_err
);
1848 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
1849 char *(*get
)(Object
*, Error
**),
1850 void (*set
)(Object
*, const char *,
1854 Error
*local_err
= NULL
;
1855 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1860 object_class_property_add(klass
, name
, "string",
1861 get
? property_get_str
: NULL
,
1862 set
? property_set_str
: NULL
,
1863 property_release_str
,
1866 error_propagate(errp
, local_err
);
1871 typedef struct BoolProperty
1873 bool (*get
)(Object
*, Error
**);
1874 void (*set
)(Object
*, bool, Error
**);
1877 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
1878 void *opaque
, Error
**errp
)
1880 BoolProperty
*prop
= opaque
;
1884 value
= prop
->get(obj
, &err
);
1886 error_propagate(errp
, err
);
1890 visit_type_bool(v
, name
, &value
, errp
);
1893 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
1894 void *opaque
, Error
**errp
)
1896 BoolProperty
*prop
= opaque
;
1898 Error
*local_err
= NULL
;
1900 visit_type_bool(v
, name
, &value
, &local_err
);
1902 error_propagate(errp
, local_err
);
1906 prop
->set(obj
, value
, errp
);
1909 static void property_release_bool(Object
*obj
, const char *name
,
1912 BoolProperty
*prop
= opaque
;
1916 void object_property_add_bool(Object
*obj
, const char *name
,
1917 bool (*get
)(Object
*, Error
**),
1918 void (*set
)(Object
*, bool, Error
**),
1921 Error
*local_err
= NULL
;
1922 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1927 object_property_add(obj
, name
, "bool",
1928 get
? property_get_bool
: NULL
,
1929 set
? property_set_bool
: NULL
,
1930 property_release_bool
,
1933 error_propagate(errp
, local_err
);
1938 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
1939 bool (*get
)(Object
*, Error
**),
1940 void (*set
)(Object
*, bool, Error
**),
1943 Error
*local_err
= NULL
;
1944 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1949 object_class_property_add(klass
, name
, "bool",
1950 get
? property_get_bool
: NULL
,
1951 set
? property_set_bool
: NULL
,
1952 property_release_bool
,
1955 error_propagate(errp
, local_err
);
1960 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
1961 void *opaque
, Error
**errp
)
1963 EnumProperty
*prop
= opaque
;
1967 value
= prop
->get(obj
, &err
);
1969 error_propagate(errp
, err
);
1973 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
1976 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
1977 void *opaque
, Error
**errp
)
1979 EnumProperty
*prop
= opaque
;
1983 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
1985 error_propagate(errp
, err
);
1988 prop
->set(obj
, value
, errp
);
1991 static void property_release_enum(Object
*obj
, const char *name
,
1994 EnumProperty
*prop
= opaque
;
1998 void object_property_add_enum(Object
*obj
, const char *name
,
1999 const char *typename
,
2000 const QEnumLookup
*lookup
,
2001 int (*get
)(Object
*, Error
**),
2002 void (*set
)(Object
*, int, Error
**),
2005 Error
*local_err
= NULL
;
2006 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2008 prop
->lookup
= lookup
;
2012 object_property_add(obj
, name
, typename
,
2013 get
? property_get_enum
: NULL
,
2014 set
? property_set_enum
: NULL
,
2015 property_release_enum
,
2018 error_propagate(errp
, local_err
);
2023 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2024 const char *typename
,
2025 const QEnumLookup
*lookup
,
2026 int (*get
)(Object
*, Error
**),
2027 void (*set
)(Object
*, int, Error
**),
2030 Error
*local_err
= NULL
;
2031 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2033 prop
->lookup
= lookup
;
2037 object_class_property_add(klass
, name
, typename
,
2038 get
? property_get_enum
: NULL
,
2039 set
? property_set_enum
: NULL
,
2040 property_release_enum
,
2043 error_propagate(errp
, local_err
);
2048 typedef struct TMProperty
{
2049 void (*get
)(Object
*, struct tm
*, Error
**);
2052 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2053 void *opaque
, Error
**errp
)
2055 TMProperty
*prop
= opaque
;
2059 prop
->get(obj
, &value
, &err
);
2064 visit_start_struct(v
, name
, NULL
, 0, &err
);
2068 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2072 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2076 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2080 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2084 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2088 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2092 visit_check_struct(v
, &err
);
2094 visit_end_struct(v
, NULL
);
2096 error_propagate(errp
, err
);
2100 static void property_release_tm(Object
*obj
, const char *name
,
2103 TMProperty
*prop
= opaque
;
2107 void object_property_add_tm(Object
*obj
, const char *name
,
2108 void (*get
)(Object
*, struct tm
*, Error
**),
2111 Error
*local_err
= NULL
;
2112 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2116 object_property_add(obj
, name
, "struct tm",
2117 get
? property_get_tm
: NULL
, NULL
,
2118 property_release_tm
,
2121 error_propagate(errp
, local_err
);
2126 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2127 void (*get
)(Object
*, struct tm
*, Error
**),
2130 Error
*local_err
= NULL
;
2131 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2135 object_class_property_add(klass
, name
, "struct tm",
2136 get
? property_get_tm
: NULL
, NULL
,
2137 property_release_tm
,
2140 error_propagate(errp
, local_err
);
2145 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2147 return g_strdup(object_get_typename(obj
));
2150 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2151 void *opaque
, Error
**errp
)
2153 uint8_t value
= *(uint8_t *)opaque
;
2154 visit_type_uint8(v
, name
, &value
, errp
);
2157 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2158 void *opaque
, Error
**errp
)
2160 uint16_t value
= *(uint16_t *)opaque
;
2161 visit_type_uint16(v
, name
, &value
, errp
);
2164 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2165 void *opaque
, Error
**errp
)
2167 uint32_t value
= *(uint32_t *)opaque
;
2168 visit_type_uint32(v
, name
, &value
, errp
);
2171 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2172 void *opaque
, Error
**errp
)
2174 uint64_t value
= *(uint64_t *)opaque
;
2175 visit_type_uint64(v
, name
, &value
, errp
);
2178 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2179 const uint8_t *v
, Error
**errp
)
2181 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2182 NULL
, NULL
, (void *)v
, errp
);
2185 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2186 const uint8_t *v
, Error
**errp
)
2188 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2189 NULL
, NULL
, (void *)v
, errp
);
2192 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2193 const uint16_t *v
, Error
**errp
)
2195 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2196 NULL
, NULL
, (void *)v
, errp
);
2199 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2200 const uint16_t *v
, Error
**errp
)
2202 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2203 NULL
, NULL
, (void *)v
, errp
);
2206 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2207 const uint32_t *v
, Error
**errp
)
2209 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2210 NULL
, NULL
, (void *)v
, errp
);
2213 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2214 const uint32_t *v
, Error
**errp
)
2216 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2217 NULL
, NULL
, (void *)v
, errp
);
2220 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2221 const uint64_t *v
, Error
**errp
)
2223 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2224 NULL
, NULL
, (void *)v
, errp
);
2227 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2228 const uint64_t *v
, Error
**errp
)
2230 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2231 NULL
, NULL
, (void *)v
, errp
);
2239 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2240 void *opaque
, Error
**errp
)
2242 AliasProperty
*prop
= opaque
;
2244 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2247 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2248 void *opaque
, Error
**errp
)
2250 AliasProperty
*prop
= opaque
;
2252 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2255 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2258 AliasProperty
*prop
= opaque
;
2260 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2263 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2265 AliasProperty
*prop
= opaque
;
2267 g_free(prop
->target_name
);
2271 void object_property_add_alias(Object
*obj
, const char *name
,
2272 Object
*target_obj
, const char *target_name
,
2275 AliasProperty
*prop
;
2277 ObjectProperty
*target_prop
;
2279 Error
*local_err
= NULL
;
2281 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2286 if (object_property_is_child(target_prop
)) {
2287 prop_type
= g_strdup_printf("link%s",
2288 target_prop
->type
+ strlen("child"));
2290 prop_type
= g_strdup(target_prop
->type
);
2293 prop
= g_malloc(sizeof(*prop
));
2294 prop
->target_obj
= target_obj
;
2295 prop
->target_name
= g_strdup(target_name
);
2297 op
= object_property_add(obj
, name
, prop_type
,
2300 property_release_alias
,
2303 error_propagate(errp
, local_err
);
2307 op
->resolve
= property_resolve_alias
;
2309 object_property_set_description(obj
, op
->name
,
2310 target_prop
->description
,
2317 void object_property_set_description(Object
*obj
, const char *name
,
2318 const char *description
, Error
**errp
)
2322 op
= object_property_find(obj
, name
, errp
);
2327 g_free(op
->description
);
2328 op
->description
= g_strdup(description
);
2331 void object_class_property_set_description(ObjectClass
*klass
,
2333 const char *description
,
2338 op
= g_hash_table_lookup(klass
->properties
, name
);
2340 error_setg(errp
, "Property '.%s' not found", name
);
2344 g_free(op
->description
);
2345 op
->description
= g_strdup(description
);
2348 static void object_instance_init(Object
*obj
)
2350 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
2353 static void register_types(void)
2355 static TypeInfo interface_info
= {
2356 .name
= TYPE_INTERFACE
,
2357 .class_size
= sizeof(InterfaceClass
),
2361 static TypeInfo object_info
= {
2362 .name
= TYPE_OBJECT
,
2363 .instance_size
= sizeof(Object
),
2364 .instance_init
= object_instance_init
,
2368 type_interface
= type_register_internal(&interface_info
);
2369 type_register_internal(&object_info
);
2372 type_init(register_types
)