4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qom/object.h"
16 #include "qom/object_interfaces.h"
17 #include "qemu/cutils.h"
18 #include "qapi/visitor.h"
19 #include "qapi-visit.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qmp/qerror.h"
25 /* TODO: replace QObject with a simpler visitor to avoid a dependency
26 * of the QOM core on QObject? */
27 #include "qom/qom-qobject.h"
28 #include "qapi/qmp/qobject.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/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 size_t object_type_get_instance_size(const char *typename
)
206 TypeImpl
*type
= type_get_by_name(typename
);
208 g_assert(type
!= NULL
);
209 return type_object_get_size(type
);
212 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
216 /* Check if target_type is a direct ancestor of type */
218 if (type
== target_type
) {
222 type
= type_get_parent(type
);
228 static void type_initialize(TypeImpl
*ti
);
230 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
231 TypeImpl
*parent_type
)
233 InterfaceClass
*new_iface
;
235 TypeImpl
*iface_impl
;
237 info
.parent
= parent_type
->name
;
238 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
239 info
.abstract
= true;
241 iface_impl
= type_new(&info
);
242 iface_impl
->parent_type
= parent_type
;
243 type_initialize(iface_impl
);
244 g_free((char *)info
.name
);
246 new_iface
= (InterfaceClass
*)iface_impl
->class;
247 new_iface
->concrete_class
= ti
->class;
248 new_iface
->interface_type
= interface_type
;
250 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
254 static void object_property_free(gpointer data
)
256 ObjectProperty
*prop
= data
;
260 g_free(prop
->description
);
264 static void type_initialize(TypeImpl
*ti
)
272 ti
->class_size
= type_class_get_size(ti
);
273 ti
->instance_size
= type_object_get_size(ti
);
274 /* Any type with zero instance_size is implicitly abstract.
275 * This means interface types are all abstract.
277 if (ti
->instance_size
== 0) {
281 ti
->class = g_malloc0(ti
->class_size
);
283 parent
= type_get_parent(ti
);
285 type_initialize(parent
);
289 g_assert_cmpint(parent
->class_size
, <=, ti
->class_size
);
290 memcpy(ti
->class, parent
->class, parent
->class_size
);
291 ti
->class->interfaces
= NULL
;
292 ti
->class->properties
= g_hash_table_new_full(
293 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
295 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
296 InterfaceClass
*iface
= e
->data
;
297 ObjectClass
*klass
= OBJECT_CLASS(iface
);
299 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
302 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
303 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
304 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
305 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
307 if (type_is_ancestor(target_type
, t
)) {
316 type_initialize_interface(ti
, t
, t
);
319 ti
->class->properties
= g_hash_table_new_full(
320 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
323 ti
->class->type
= ti
;
326 if (parent
->class_base_init
) {
327 parent
->class_base_init(ti
->class, ti
->class_data
);
329 parent
= type_get_parent(parent
);
332 if (ti
->class_init
) {
333 ti
->class_init(ti
->class, ti
->class_data
);
337 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
339 if (type_has_parent(ti
)) {
340 object_init_with_type(obj
, type_get_parent(ti
));
343 if (ti
->instance_init
) {
344 ti
->instance_init(obj
);
348 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
350 if (ti
->instance_post_init
) {
351 ti
->instance_post_init(obj
);
354 if (type_has_parent(ti
)) {
355 object_post_init_with_type(obj
, type_get_parent(ti
));
359 static void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
363 g_assert(type
!= NULL
);
364 type_initialize(type
);
366 g_assert_cmpint(type
->instance_size
, >=, sizeof(Object
));
367 g_assert(type
->abstract
== false);
368 g_assert_cmpint(size
, >=, type
->instance_size
);
370 memset(obj
, 0, type
->instance_size
);
371 obj
->class = type
->class;
373 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
374 NULL
, object_property_free
);
375 object_init_with_type(obj
, type
);
376 object_post_init_with_type(obj
, type
);
379 void object_initialize(void *data
, size_t size
, const char *typename
)
381 TypeImpl
*type
= type_get_by_name(typename
);
383 object_initialize_with_type(data
, size
, type
);
386 static inline bool object_property_is_child(ObjectProperty
*prop
)
388 return strstart(prop
->type
, "child<", NULL
);
391 static void object_property_del_all(Object
*obj
)
393 ObjectProperty
*prop
;
400 g_hash_table_iter_init(&iter
, obj
->properties
);
401 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
404 prop
->release(obj
, prop
->name
, prop
->opaque
);
405 prop
->release
= NULL
;
409 g_hash_table_iter_remove(&iter
);
413 g_hash_table_unref(obj
->properties
);
416 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
418 ObjectProperty
*prop
;
422 g_hash_table_iter_init(&iter
, obj
->properties
);
423 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
425 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
427 prop
->release(obj
, prop
->name
, prop
->opaque
);
428 prop
->release
= NULL
;
433 g_hash_table_iter_init(&iter
, obj
->properties
);
434 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
436 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
437 g_hash_table_iter_remove(&iter
);
443 void object_unparent(Object
*obj
)
446 object_property_del_child(obj
->parent
, obj
, NULL
);
450 static void object_deinit(Object
*obj
, TypeImpl
*type
)
452 if (type
->instance_finalize
) {
453 type
->instance_finalize(obj
);
456 if (type_has_parent(type
)) {
457 object_deinit(obj
, type_get_parent(type
));
461 static void object_finalize(void *data
)
464 TypeImpl
*ti
= obj
->class->type
;
466 object_property_del_all(obj
);
467 object_deinit(obj
, ti
);
469 g_assert_cmpint(obj
->ref
, ==, 0);
475 static Object
*object_new_with_type(Type type
)
479 g_assert(type
!= NULL
);
480 type_initialize(type
);
482 obj
= g_malloc(type
->instance_size
);
483 object_initialize_with_type(obj
, type
->instance_size
, type
);
489 Object
*object_new(const char *typename
)
491 TypeImpl
*ti
= type_get_by_name(typename
);
493 return object_new_with_type(ti
);
497 Object
*object_new_with_props(const char *typename
,
506 va_start(vargs
, errp
);
507 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
514 Object
*object_new_with_propv(const char *typename
,
522 Error
*local_err
= NULL
;
524 klass
= object_class_by_name(typename
);
526 error_setg(errp
, "invalid object type: %s", typename
);
530 if (object_class_is_abstract(klass
)) {
531 error_setg(errp
, "object type '%s' is abstract", typename
);
534 obj
= object_new(typename
);
536 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
540 object_property_add_child(parent
, id
, obj
, &local_err
);
545 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
546 user_creatable_complete(obj
, &local_err
);
548 object_unparent(obj
);
553 object_unref(OBJECT(obj
));
557 error_propagate(errp
, local_err
);
563 int object_set_props(Object
*obj
,
570 va_start(vargs
, errp
);
571 ret
= object_set_propv(obj
, errp
, vargs
);
578 int object_set_propv(Object
*obj
,
582 const char *propname
;
583 Error
*local_err
= NULL
;
585 propname
= va_arg(vargs
, char *);
586 while (propname
!= NULL
) {
587 const char *value
= va_arg(vargs
, char *);
589 g_assert(value
!= NULL
);
590 object_property_parse(obj
, value
, propname
, &local_err
);
592 error_propagate(errp
, local_err
);
595 propname
= va_arg(vargs
, char *);
602 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
604 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
611 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
612 const char *file
, int line
, const char *func
)
614 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
615 typename
, file
, line
, func
);
617 #ifdef CONFIG_QOM_CAST_DEBUG
621 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
622 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
627 inst
= object_dynamic_cast(obj
, typename
);
630 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
631 file
, line
, func
, obj
, typename
);
637 if (obj
&& obj
== inst
) {
638 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
639 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
640 atomic_read(&obj
->class->object_cast_cache
[i
]));
642 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
650 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
651 const char *typename
)
653 ObjectClass
*ret
= NULL
;
654 TypeImpl
*target_type
;
661 /* A simple fast path that can trigger a lot for leaf classes. */
663 if (type
->name
== typename
) {
667 target_type
= type_get_by_name(typename
);
669 /* target class type unknown, so fail the cast */
673 if (type
->class->interfaces
&&
674 type_is_ancestor(target_type
, type_interface
)) {
678 for (i
= class->interfaces
; i
; i
= i
->next
) {
679 ObjectClass
*target_class
= i
->data
;
681 if (type_is_ancestor(target_class
->type
, target_type
)) {
687 /* The match was ambiguous, don't allow a cast */
691 } else if (type_is_ancestor(type
, target_type
)) {
698 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
699 const char *typename
,
700 const char *file
, int line
,
705 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
706 typename
, file
, line
, func
);
708 #ifdef CONFIG_QOM_CAST_DEBUG
711 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
712 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
718 if (!class || !class->interfaces
) {
723 ret
= object_class_dynamic_cast(class, typename
);
725 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
726 file
, line
, func
, class, typename
);
730 #ifdef CONFIG_QOM_CAST_DEBUG
731 if (class && ret
== class) {
732 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
733 atomic_set(&class->class_cast_cache
[i
- 1],
734 atomic_read(&class->class_cast_cache
[i
]));
736 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
743 const char *object_get_typename(Object
*obj
)
745 return obj
->class->type
->name
;
748 ObjectClass
*object_get_class(Object
*obj
)
753 bool object_class_is_abstract(ObjectClass
*klass
)
755 return klass
->type
->abstract
;
758 const char *object_class_get_name(ObjectClass
*klass
)
760 return klass
->type
->name
;
763 ObjectClass
*object_class_by_name(const char *typename
)
765 TypeImpl
*type
= type_get_by_name(typename
);
771 type_initialize(type
);
776 ObjectClass
*object_class_get_parent(ObjectClass
*class)
778 TypeImpl
*type
= type_get_parent(class->type
);
784 type_initialize(type
);
789 typedef struct OCFData
791 void (*fn
)(ObjectClass
*klass
, void *opaque
);
792 const char *implements_type
;
793 bool include_abstract
;
797 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
800 OCFData
*data
= opaque
;
801 TypeImpl
*type
= value
;
804 type_initialize(type
);
807 if (!data
->include_abstract
&& type
->abstract
) {
811 if (data
->implements_type
&&
812 !object_class_dynamic_cast(k
, data
->implements_type
)) {
816 data
->fn(k
, data
->opaque
);
819 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
820 const char *implements_type
, bool include_abstract
,
823 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
825 enumerating_types
= true;
826 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
827 enumerating_types
= false;
830 static int do_object_child_foreach(Object
*obj
,
831 int (*fn
)(Object
*child
, void *opaque
),
832 void *opaque
, bool recurse
)
835 ObjectProperty
*prop
;
838 g_hash_table_iter_init(&iter
, obj
->properties
);
839 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
840 if (object_property_is_child(prop
)) {
841 Object
*child
= prop
->opaque
;
843 ret
= fn(child
, opaque
);
848 do_object_child_foreach(child
, fn
, opaque
, true);
855 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
858 return do_object_child_foreach(obj
, fn
, opaque
, false);
861 int object_child_foreach_recursive(Object
*obj
,
862 int (*fn
)(Object
*child
, void *opaque
),
865 return do_object_child_foreach(obj
, fn
, opaque
, true);
868 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
870 GSList
**list
= opaque
;
872 *list
= g_slist_prepend(*list
, klass
);
875 GSList
*object_class_get_list(const char *implements_type
,
876 bool include_abstract
)
880 object_class_foreach(object_class_get_list_tramp
,
881 implements_type
, include_abstract
, &list
);
885 void object_ref(Object
*obj
)
890 atomic_inc(&obj
->ref
);
893 void object_unref(Object
*obj
)
898 g_assert_cmpint(obj
->ref
, >, 0);
900 /* parent always holds a reference to its children */
901 if (atomic_fetch_dec(&obj
->ref
) == 1) {
902 object_finalize(obj
);
907 object_property_add(Object
*obj
, const char *name
, const char *type
,
908 ObjectPropertyAccessor
*get
,
909 ObjectPropertyAccessor
*set
,
910 ObjectPropertyRelease
*release
,
911 void *opaque
, Error
**errp
)
913 ObjectProperty
*prop
;
914 size_t name_len
= strlen(name
);
916 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
919 char *name_no_array
= g_strdup(name
);
921 name_no_array
[name_len
- 3] = '\0';
923 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
925 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
926 release
, opaque
, NULL
);
932 g_free(name_no_array
);
936 if (object_property_find(obj
, name
, NULL
) != NULL
) {
937 error_setg(errp
, "attempt to add duplicate property '%s'"
938 " to object (type '%s')", name
,
939 object_get_typename(obj
));
943 prop
= g_malloc0(sizeof(*prop
));
945 prop
->name
= g_strdup(name
);
946 prop
->type
= g_strdup(type
);
950 prop
->release
= release
;
951 prop
->opaque
= opaque
;
953 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
958 object_class_property_add(ObjectClass
*klass
,
961 ObjectPropertyAccessor
*get
,
962 ObjectPropertyAccessor
*set
,
963 ObjectPropertyRelease
*release
,
967 ObjectProperty
*prop
;
969 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
970 error_setg(errp
, "attempt to add duplicate property '%s'"
971 " to object (type '%s')", name
,
972 object_class_get_name(klass
));
976 prop
= g_malloc0(sizeof(*prop
));
978 prop
->name
= g_strdup(name
);
979 prop
->type
= g_strdup(type
);
983 prop
->release
= release
;
984 prop
->opaque
= opaque
;
986 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
991 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
994 ObjectProperty
*prop
;
995 ObjectClass
*klass
= object_get_class(obj
);
997 prop
= object_class_property_find(klass
, name
, NULL
);
1002 prop
= g_hash_table_lookup(obj
->properties
, name
);
1007 error_setg(errp
, "Property '.%s' not found", name
);
1011 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1014 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1015 iter
->nextclass
= object_get_class(obj
);
1018 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1021 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1022 if (!iter
->nextclass
) {
1025 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1026 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1031 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1034 ObjectProperty
*prop
;
1035 ObjectClass
*parent_klass
;
1037 parent_klass
= object_class_get_parent(klass
);
1039 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1045 prop
= g_hash_table_lookup(klass
->properties
, name
);
1047 error_setg(errp
, "Property '.%s' not found", name
);
1052 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1054 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1057 error_setg(errp
, "Property '.%s' not found", name
);
1061 if (prop
->release
) {
1062 prop
->release(obj
, name
, prop
->opaque
);
1064 g_hash_table_remove(obj
->properties
, name
);
1067 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1070 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1076 error_setg(errp
, QERR_PERMISSION_DENIED
);
1078 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1082 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1085 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1091 error_setg(errp
, QERR_PERMISSION_DENIED
);
1093 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1097 void object_property_set_str(Object
*obj
, const char *value
,
1098 const char *name
, Error
**errp
)
1100 QString
*qstr
= qstring_from_str(value
);
1101 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1106 char *object_property_get_str(Object
*obj
, const char *name
,
1109 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1116 qstring
= qobject_to_qstring(ret
);
1118 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1121 retval
= g_strdup(qstring_get_str(qstring
));
1124 qobject_decref(ret
);
1128 void object_property_set_link(Object
*obj
, Object
*value
,
1129 const char *name
, Error
**errp
)
1132 gchar
*path
= object_get_canonical_path(value
);
1133 object_property_set_str(obj
, path
, name
, errp
);
1136 object_property_set_str(obj
, "", name
, errp
);
1140 Object
*object_property_get_link(Object
*obj
, const char *name
,
1143 char *str
= object_property_get_str(obj
, name
, errp
);
1144 Object
*target
= NULL
;
1147 target
= object_resolve_path(str
, NULL
);
1149 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1150 "Device '%s' not found", str
);
1158 void object_property_set_bool(Object
*obj
, bool value
,
1159 const char *name
, Error
**errp
)
1161 QBool
*qbool
= qbool_from_bool(value
);
1162 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1167 bool object_property_get_bool(Object
*obj
, const char *name
,
1170 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1177 qbool
= qobject_to_qbool(ret
);
1179 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1182 retval
= qbool_get_bool(qbool
);
1185 qobject_decref(ret
);
1189 void object_property_set_int(Object
*obj
, int64_t value
,
1190 const char *name
, Error
**errp
)
1192 QNum
*qnum
= qnum_from_int(value
);
1193 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1198 int64_t object_property_get_int(Object
*obj
, const char *name
,
1201 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1209 qnum
= qobject_to_qnum(ret
);
1210 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1211 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1215 qobject_decref(ret
);
1219 void object_property_set_uint(Object
*obj
, uint64_t value
,
1220 const char *name
, Error
**errp
)
1222 QNum
*qnum
= qnum_from_uint(value
);
1224 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1228 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1231 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1238 qnum
= qobject_to_qnum(ret
);
1239 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1240 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1244 qobject_decref(ret
);
1248 typedef struct EnumProperty
{
1249 const char * const *strings
;
1250 int (*get
)(Object
*, Error
**);
1251 void (*set
)(Object
*, int, Error
**);
1254 int object_property_get_enum(Object
*obj
, const char *name
,
1255 const char *typename
, Error
**errp
)
1261 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1262 EnumProperty
*enumprop
;
1268 if (!g_str_equal(prop
->type
, typename
)) {
1269 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1270 name
, object_class_get_name(
1271 object_get_class(obj
)), typename
);
1275 enumprop
= prop
->opaque
;
1277 v
= string_output_visitor_new(false, &str
);
1278 object_property_get(obj
, v
, name
, &err
);
1280 error_propagate(errp
, err
);
1284 visit_complete(v
, &str
);
1286 v
= string_input_visitor_new(str
);
1287 visit_type_enum(v
, name
, &ret
, enumprop
->strings
, errp
);
1295 void object_property_get_uint16List(Object
*obj
, const char *name
,
1296 uint16List
**list
, Error
**errp
)
1302 v
= string_output_visitor_new(false, &str
);
1303 object_property_get(obj
, v
, name
, &err
);
1305 error_propagate(errp
, err
);
1308 visit_complete(v
, &str
);
1310 v
= string_input_visitor_new(str
);
1311 visit_type_uint16List(v
, NULL
, list
, errp
);
1318 void object_property_parse(Object
*obj
, const char *string
,
1319 const char *name
, Error
**errp
)
1321 Visitor
*v
= string_input_visitor_new(string
);
1322 object_property_set(obj
, v
, name
, errp
);
1326 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1330 char *string
= NULL
;
1331 Error
*local_err
= NULL
;
1333 v
= string_output_visitor_new(human
, &string
);
1334 object_property_get(obj
, v
, name
, &local_err
);
1336 error_propagate(errp
, local_err
);
1340 visit_complete(v
, &string
);
1347 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1349 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1357 Object
*object_get_root(void)
1359 static Object
*root
;
1362 root
= object_new("container");
1368 Object
*object_get_objects_root(void)
1370 return container_get(object_get_root(), "/objects");
1373 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1374 const char *name
, void *opaque
,
1377 Object
*child
= opaque
;
1380 path
= object_get_canonical_path(child
);
1381 visit_type_str(v
, name
, &path
, errp
);
1385 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1390 static void object_finalize_child_property(Object
*obj
, const char *name
,
1393 Object
*child
= opaque
;
1395 if (child
->class->unparent
) {
1396 (child
->class->unparent
)(child
);
1398 child
->parent
= NULL
;
1399 object_unref(child
);
1402 void object_property_add_child(Object
*obj
, const char *name
,
1403 Object
*child
, Error
**errp
)
1405 Error
*local_err
= NULL
;
1409 if (child
->parent
!= NULL
) {
1410 error_setg(errp
, "child object is already parented");
1414 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1416 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1417 object_finalize_child_property
, child
, &local_err
);
1419 error_propagate(errp
, local_err
);
1423 op
->resolve
= object_resolve_child_property
;
1425 child
->parent
= obj
;
1431 void object_property_allow_set_link(Object
*obj
, const char *name
,
1432 Object
*val
, Error
**errp
)
1434 /* Allow the link to be set, always */
1439 void (*check
)(Object
*, const char *, Object
*, Error
**);
1440 ObjectPropertyLinkFlags flags
;
1443 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1444 const char *name
, void *opaque
,
1447 LinkProperty
*lprop
= opaque
;
1448 Object
**child
= lprop
->child
;
1452 path
= object_get_canonical_path(*child
);
1453 visit_type_str(v
, name
, &path
, errp
);
1457 visit_type_str(v
, name
, &path
, errp
);
1462 * object_resolve_link:
1464 * Lookup an object and ensure its type matches the link property type. This
1465 * is similar to object_resolve_path() except type verification against the
1466 * link property is performed.
1468 * Returns: The matched object or NULL on path lookup failures.
1470 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1471 const char *path
, Error
**errp
)
1475 bool ambiguous
= false;
1478 /* Go from link<FOO> to FOO. */
1479 type
= object_property_get_type(obj
, name
, NULL
);
1480 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1481 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1484 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1486 } else if (!target
) {
1487 target
= object_resolve_path(path
, &ambiguous
);
1488 if (target
|| ambiguous
) {
1489 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1491 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1492 "Device '%s' not found", path
);
1496 g_free(target_type
);
1501 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1502 const char *name
, void *opaque
,
1505 Error
*local_err
= NULL
;
1506 LinkProperty
*prop
= opaque
;
1507 Object
**child
= prop
->child
;
1508 Object
*old_target
= *child
;
1509 Object
*new_target
= NULL
;
1512 visit_type_str(v
, name
, &path
, &local_err
);
1514 if (!local_err
&& strcmp(path
, "") != 0) {
1515 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1520 error_propagate(errp
, local_err
);
1524 prop
->check(obj
, name
, new_target
, &local_err
);
1526 error_propagate(errp
, local_err
);
1530 object_ref(new_target
);
1531 *child
= new_target
;
1532 object_unref(old_target
);
1535 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1537 LinkProperty
*lprop
= opaque
;
1539 return *lprop
->child
;
1542 static void object_release_link_property(Object
*obj
, const char *name
,
1545 LinkProperty
*prop
= opaque
;
1547 if ((prop
->flags
& OBJ_PROP_LINK_UNREF_ON_RELEASE
) && *prop
->child
) {
1548 object_unref(*prop
->child
);
1553 void object_property_add_link(Object
*obj
, const char *name
,
1554 const char *type
, Object
**child
,
1555 void (*check
)(Object
*, const char *,
1556 Object
*, Error
**),
1557 ObjectPropertyLinkFlags flags
,
1560 Error
*local_err
= NULL
;
1561 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1565 prop
->child
= child
;
1566 prop
->check
= check
;
1567 prop
->flags
= flags
;
1569 full_type
= g_strdup_printf("link<%s>", type
);
1571 op
= object_property_add(obj
, name
, full_type
,
1572 object_get_link_property
,
1573 check
? object_set_link_property
: NULL
,
1574 object_release_link_property
,
1578 error_propagate(errp
, local_err
);
1583 op
->resolve
= object_resolve_link_property
;
1589 void object_property_add_const_link(Object
*obj
, const char *name
,
1590 Object
*target
, Error
**errp
)
1595 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1596 op
= object_property_add(obj
, name
, link_type
,
1597 object_get_child_property
, NULL
,
1598 NULL
, target
, errp
);
1600 op
->resolve
= object_resolve_child_property
;
1605 gchar
*object_get_canonical_path_component(Object
*obj
)
1607 ObjectProperty
*prop
= NULL
;
1608 GHashTableIter iter
;
1611 g_assert(obj
->parent
!= NULL
);
1613 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1614 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1615 if (!object_property_is_child(prop
)) {
1619 if (prop
->opaque
== obj
) {
1620 return g_strdup(prop
->name
);
1624 /* obj had a parent but was not a child, should never happen */
1625 g_assert_not_reached();
1629 gchar
*object_get_canonical_path(Object
*obj
)
1631 Object
*root
= object_get_root();
1632 char *newpath
, *path
= NULL
;
1634 while (obj
!= root
) {
1635 char *component
= object_get_canonical_path_component(obj
);
1638 newpath
= g_strdup_printf("%s/%s", component
, path
);
1649 newpath
= g_strdup_printf("/%s", path
? path
: "");
1655 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1657 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1662 if (prop
->resolve
) {
1663 return prop
->resolve(parent
, prop
->opaque
, part
);
1669 static Object
*object_resolve_abs_path(Object
*parent
,
1671 const char *typename
,
1676 if (parts
[index
] == NULL
) {
1677 return object_dynamic_cast(parent
, typename
);
1680 if (strcmp(parts
[index
], "") == 0) {
1681 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1684 child
= object_resolve_path_component(parent
, parts
[index
]);
1689 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1692 static Object
*object_resolve_partial_path(Object
*parent
,
1694 const char *typename
,
1698 GHashTableIter iter
;
1699 ObjectProperty
*prop
;
1701 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1703 g_hash_table_iter_init(&iter
, parent
->properties
);
1704 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1707 if (!object_property_is_child(prop
)) {
1711 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1712 typename
, ambiguous
);
1723 if (ambiguous
&& *ambiguous
) {
1731 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1737 parts
= g_strsplit(path
, "/", 0);
1740 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1744 obj
= object_resolve_partial_path(object_get_root(), parts
,
1745 typename
, ambiguous
);
1747 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1755 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1757 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1760 typedef struct StringProperty
1762 char *(*get
)(Object
*, Error
**);
1763 void (*set
)(Object
*, const char *, Error
**);
1766 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1767 void *opaque
, Error
**errp
)
1769 StringProperty
*prop
= opaque
;
1773 value
= prop
->get(obj
, &err
);
1775 error_propagate(errp
, err
);
1779 visit_type_str(v
, name
, &value
, errp
);
1783 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1784 void *opaque
, Error
**errp
)
1786 StringProperty
*prop
= opaque
;
1788 Error
*local_err
= NULL
;
1790 visit_type_str(v
, name
, &value
, &local_err
);
1792 error_propagate(errp
, local_err
);
1796 prop
->set(obj
, value
, errp
);
1800 static void property_release_str(Object
*obj
, const char *name
,
1803 StringProperty
*prop
= opaque
;
1807 void object_property_add_str(Object
*obj
, const char *name
,
1808 char *(*get
)(Object
*, Error
**),
1809 void (*set
)(Object
*, const char *, Error
**),
1812 Error
*local_err
= NULL
;
1813 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1818 object_property_add(obj
, name
, "string",
1819 get
? property_get_str
: NULL
,
1820 set
? property_set_str
: NULL
,
1821 property_release_str
,
1824 error_propagate(errp
, local_err
);
1829 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
1830 char *(*get
)(Object
*, Error
**),
1831 void (*set
)(Object
*, const char *,
1835 Error
*local_err
= NULL
;
1836 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1841 object_class_property_add(klass
, name
, "string",
1842 get
? property_get_str
: NULL
,
1843 set
? property_set_str
: NULL
,
1844 property_release_str
,
1847 error_propagate(errp
, local_err
);
1852 typedef struct BoolProperty
1854 bool (*get
)(Object
*, Error
**);
1855 void (*set
)(Object
*, bool, Error
**);
1858 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
1859 void *opaque
, Error
**errp
)
1861 BoolProperty
*prop
= opaque
;
1865 value
= prop
->get(obj
, &err
);
1867 error_propagate(errp
, err
);
1871 visit_type_bool(v
, name
, &value
, errp
);
1874 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
1875 void *opaque
, Error
**errp
)
1877 BoolProperty
*prop
= opaque
;
1879 Error
*local_err
= NULL
;
1881 visit_type_bool(v
, name
, &value
, &local_err
);
1883 error_propagate(errp
, local_err
);
1887 prop
->set(obj
, value
, errp
);
1890 static void property_release_bool(Object
*obj
, const char *name
,
1893 BoolProperty
*prop
= opaque
;
1897 void object_property_add_bool(Object
*obj
, const char *name
,
1898 bool (*get
)(Object
*, Error
**),
1899 void (*set
)(Object
*, bool, Error
**),
1902 Error
*local_err
= NULL
;
1903 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1908 object_property_add(obj
, name
, "bool",
1909 get
? property_get_bool
: NULL
,
1910 set
? property_set_bool
: NULL
,
1911 property_release_bool
,
1914 error_propagate(errp
, local_err
);
1919 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
1920 bool (*get
)(Object
*, Error
**),
1921 void (*set
)(Object
*, bool, Error
**),
1924 Error
*local_err
= NULL
;
1925 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
1930 object_class_property_add(klass
, name
, "bool",
1931 get
? property_get_bool
: NULL
,
1932 set
? property_set_bool
: NULL
,
1933 property_release_bool
,
1936 error_propagate(errp
, local_err
);
1941 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
1942 void *opaque
, Error
**errp
)
1944 EnumProperty
*prop
= opaque
;
1948 value
= prop
->get(obj
, &err
);
1950 error_propagate(errp
, err
);
1954 visit_type_enum(v
, name
, &value
, prop
->strings
, errp
);
1957 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
1958 void *opaque
, Error
**errp
)
1960 EnumProperty
*prop
= opaque
;
1964 visit_type_enum(v
, name
, &value
, prop
->strings
, &err
);
1966 error_propagate(errp
, err
);
1969 prop
->set(obj
, value
, errp
);
1972 static void property_release_enum(Object
*obj
, const char *name
,
1975 EnumProperty
*prop
= opaque
;
1979 void object_property_add_enum(Object
*obj
, const char *name
,
1980 const char *typename
,
1981 const char * const *strings
,
1982 int (*get
)(Object
*, Error
**),
1983 void (*set
)(Object
*, int, Error
**),
1986 Error
*local_err
= NULL
;
1987 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
1989 prop
->strings
= strings
;
1993 object_property_add(obj
, name
, typename
,
1994 get
? property_get_enum
: NULL
,
1995 set
? property_set_enum
: NULL
,
1996 property_release_enum
,
1999 error_propagate(errp
, local_err
);
2004 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2005 const char *typename
,
2006 const char * const *strings
,
2007 int (*get
)(Object
*, Error
**),
2008 void (*set
)(Object
*, int, Error
**),
2011 Error
*local_err
= NULL
;
2012 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2014 prop
->strings
= strings
;
2018 object_class_property_add(klass
, name
, typename
,
2019 get
? property_get_enum
: NULL
,
2020 set
? property_set_enum
: NULL
,
2021 property_release_enum
,
2024 error_propagate(errp
, local_err
);
2029 typedef struct TMProperty
{
2030 void (*get
)(Object
*, struct tm
*, Error
**);
2033 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2034 void *opaque
, Error
**errp
)
2036 TMProperty
*prop
= opaque
;
2040 prop
->get(obj
, &value
, &err
);
2045 visit_start_struct(v
, name
, NULL
, 0, &err
);
2049 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2053 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2057 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2061 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2065 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2069 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2073 visit_check_struct(v
, &err
);
2075 visit_end_struct(v
, NULL
);
2077 error_propagate(errp
, err
);
2081 static void property_release_tm(Object
*obj
, const char *name
,
2084 TMProperty
*prop
= opaque
;
2088 void object_property_add_tm(Object
*obj
, const char *name
,
2089 void (*get
)(Object
*, struct tm
*, Error
**),
2092 Error
*local_err
= NULL
;
2093 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2097 object_property_add(obj
, name
, "struct tm",
2098 get
? property_get_tm
: NULL
, NULL
,
2099 property_release_tm
,
2102 error_propagate(errp
, local_err
);
2107 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2108 void (*get
)(Object
*, struct tm
*, Error
**),
2111 Error
*local_err
= NULL
;
2112 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2116 object_class_property_add(klass
, name
, "struct tm",
2117 get
? property_get_tm
: NULL
, NULL
,
2118 property_release_tm
,
2121 error_propagate(errp
, local_err
);
2126 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2128 return g_strdup(object_get_typename(obj
));
2131 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2132 void *opaque
, Error
**errp
)
2134 uint8_t value
= *(uint8_t *)opaque
;
2135 visit_type_uint8(v
, name
, &value
, errp
);
2138 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2139 void *opaque
, Error
**errp
)
2141 uint16_t value
= *(uint16_t *)opaque
;
2142 visit_type_uint16(v
, name
, &value
, errp
);
2145 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2146 void *opaque
, Error
**errp
)
2148 uint32_t value
= *(uint32_t *)opaque
;
2149 visit_type_uint32(v
, name
, &value
, errp
);
2152 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2153 void *opaque
, Error
**errp
)
2155 uint64_t value
= *(uint64_t *)opaque
;
2156 visit_type_uint64(v
, name
, &value
, errp
);
2159 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2160 const uint8_t *v
, Error
**errp
)
2162 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2163 NULL
, NULL
, (void *)v
, errp
);
2166 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2167 const uint8_t *v
, Error
**errp
)
2169 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2170 NULL
, NULL
, (void *)v
, errp
);
2173 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2174 const uint16_t *v
, Error
**errp
)
2176 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2177 NULL
, NULL
, (void *)v
, errp
);
2180 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2181 const uint16_t *v
, Error
**errp
)
2183 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2184 NULL
, NULL
, (void *)v
, errp
);
2187 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2188 const uint32_t *v
, Error
**errp
)
2190 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2191 NULL
, NULL
, (void *)v
, errp
);
2194 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2195 const uint32_t *v
, Error
**errp
)
2197 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2198 NULL
, NULL
, (void *)v
, errp
);
2201 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2202 const uint64_t *v
, Error
**errp
)
2204 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2205 NULL
, NULL
, (void *)v
, errp
);
2208 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2209 const uint64_t *v
, Error
**errp
)
2211 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2212 NULL
, NULL
, (void *)v
, errp
);
2220 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2221 void *opaque
, Error
**errp
)
2223 AliasProperty
*prop
= opaque
;
2225 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2228 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2229 void *opaque
, Error
**errp
)
2231 AliasProperty
*prop
= opaque
;
2233 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2236 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2239 AliasProperty
*prop
= opaque
;
2241 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2244 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2246 AliasProperty
*prop
= opaque
;
2248 g_free(prop
->target_name
);
2252 void object_property_add_alias(Object
*obj
, const char *name
,
2253 Object
*target_obj
, const char *target_name
,
2256 AliasProperty
*prop
;
2258 ObjectProperty
*target_prop
;
2260 Error
*local_err
= NULL
;
2262 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2267 if (object_property_is_child(target_prop
)) {
2268 prop_type
= g_strdup_printf("link%s",
2269 target_prop
->type
+ strlen("child"));
2271 prop_type
= g_strdup(target_prop
->type
);
2274 prop
= g_malloc(sizeof(*prop
));
2275 prop
->target_obj
= target_obj
;
2276 prop
->target_name
= g_strdup(target_name
);
2278 op
= object_property_add(obj
, name
, prop_type
,
2281 property_release_alias
,
2284 error_propagate(errp
, local_err
);
2288 op
->resolve
= property_resolve_alias
;
2290 object_property_set_description(obj
, op
->name
,
2291 target_prop
->description
,
2298 void object_property_set_description(Object
*obj
, const char *name
,
2299 const char *description
, Error
**errp
)
2303 op
= object_property_find(obj
, name
, errp
);
2308 g_free(op
->description
);
2309 op
->description
= g_strdup(description
);
2312 void object_class_property_set_description(ObjectClass
*klass
,
2314 const char *description
,
2319 op
= g_hash_table_lookup(klass
->properties
, name
);
2321 error_setg(errp
, "Property '.%s' not found", name
);
2325 g_free(op
->description
);
2326 op
->description
= g_strdup(description
);
2329 static void object_instance_init(Object
*obj
)
2331 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
2334 static void register_types(void)
2336 static TypeInfo interface_info
= {
2337 .name
= TYPE_INTERFACE
,
2338 .class_size
= sizeof(InterfaceClass
),
2342 static TypeInfo object_info
= {
2343 .name
= TYPE_OBJECT
,
2344 .instance_size
= sizeof(Object
),
2345 .instance_init
= object_instance_init
,
2349 type_interface
= type_register_internal(&interface_info
);
2350 type_register_internal(&object_info
);
2353 type_init(register_types
)