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/string-input-visitor.h"
20 #include "qapi/string-output-visitor.h"
21 #include "qapi/qapi-builtin-visit.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(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(type
->instance_size
>= sizeof(Object
));
376 g_assert(type
->abstract
== false);
377 g_assert(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 void object_initialize_child(Object
*parentobj
, const char *propname
,
396 void *childobj
, size_t size
, const char *type
,
401 va_start(vargs
, errp
);
402 object_initialize_childv(parentobj
, propname
, childobj
, size
, type
, errp
,
407 void object_initialize_childv(Object
*parentobj
, const char *propname
,
408 void *childobj
, size_t size
, const char *type
,
409 Error
**errp
, va_list vargs
)
411 Error
*local_err
= NULL
;
414 object_initialize(childobj
, size
, type
);
415 obj
= OBJECT(childobj
);
417 object_set_propv(obj
, &local_err
, vargs
);
422 object_property_add_child(parentobj
, propname
, obj
, &local_err
);
427 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
428 user_creatable_complete(obj
, &local_err
);
430 object_unparent(obj
);
436 * Since object_property_add_child added a reference to the child object,
437 * we can drop the reference added by object_initialize(), so the child
438 * property will own the only reference to the object.
444 error_propagate(errp
, local_err
);
449 static inline bool object_property_is_child(ObjectProperty
*prop
)
451 return strstart(prop
->type
, "child<", NULL
);
454 static void object_property_del_all(Object
*obj
)
456 ObjectProperty
*prop
;
463 g_hash_table_iter_init(&iter
, obj
->properties
);
464 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
467 prop
->release(obj
, prop
->name
, prop
->opaque
);
468 prop
->release
= NULL
;
472 g_hash_table_iter_remove(&iter
);
476 g_hash_table_unref(obj
->properties
);
479 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
481 ObjectProperty
*prop
;
485 g_hash_table_iter_init(&iter
, obj
->properties
);
486 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
488 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
490 prop
->release(obj
, prop
->name
, prop
->opaque
);
491 prop
->release
= NULL
;
496 g_hash_table_iter_init(&iter
, obj
->properties
);
497 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
499 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
500 g_hash_table_iter_remove(&iter
);
506 void object_unparent(Object
*obj
)
509 object_property_del_child(obj
->parent
, obj
, NULL
);
513 static void object_deinit(Object
*obj
, TypeImpl
*type
)
515 if (type
->instance_finalize
) {
516 type
->instance_finalize(obj
);
519 if (type_has_parent(type
)) {
520 object_deinit(obj
, type_get_parent(type
));
524 static void object_finalize(void *data
)
527 TypeImpl
*ti
= obj
->class->type
;
529 object_property_del_all(obj
);
530 object_deinit(obj
, ti
);
532 g_assert(obj
->ref
== 0);
538 static Object
*object_new_with_type(Type type
)
542 g_assert(type
!= NULL
);
543 type_initialize(type
);
545 obj
= g_malloc(type
->instance_size
);
546 object_initialize_with_type(obj
, type
->instance_size
, type
);
552 Object
*object_new(const char *typename
)
554 TypeImpl
*ti
= type_get_by_name(typename
);
556 return object_new_with_type(ti
);
560 Object
*object_new_with_props(const char *typename
,
569 va_start(vargs
, errp
);
570 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
577 Object
*object_new_with_propv(const char *typename
,
585 Error
*local_err
= NULL
;
587 klass
= object_class_by_name(typename
);
589 error_setg(errp
, "invalid object type: %s", typename
);
593 if (object_class_is_abstract(klass
)) {
594 error_setg(errp
, "object type '%s' is abstract", typename
);
597 obj
= object_new(typename
);
599 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
603 object_property_add_child(parent
, id
, obj
, &local_err
);
608 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
609 user_creatable_complete(obj
, &local_err
);
611 object_unparent(obj
);
616 object_unref(OBJECT(obj
));
620 error_propagate(errp
, local_err
);
626 int object_set_props(Object
*obj
,
633 va_start(vargs
, errp
);
634 ret
= object_set_propv(obj
, errp
, vargs
);
641 int object_set_propv(Object
*obj
,
645 const char *propname
;
646 Error
*local_err
= NULL
;
648 propname
= va_arg(vargs
, char *);
649 while (propname
!= NULL
) {
650 const char *value
= va_arg(vargs
, char *);
652 g_assert(value
!= NULL
);
653 object_property_parse(obj
, value
, propname
, &local_err
);
655 error_propagate(errp
, local_err
);
658 propname
= va_arg(vargs
, char *);
665 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
667 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
674 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
675 const char *file
, int line
, const char *func
)
677 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
678 typename
, file
, line
, func
);
680 #ifdef CONFIG_QOM_CAST_DEBUG
684 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
685 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
690 inst
= object_dynamic_cast(obj
, typename
);
693 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
694 file
, line
, func
, obj
, typename
);
700 if (obj
&& obj
== inst
) {
701 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
702 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
703 atomic_read(&obj
->class->object_cast_cache
[i
]));
705 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
713 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
714 const char *typename
)
716 ObjectClass
*ret
= NULL
;
717 TypeImpl
*target_type
;
724 /* A simple fast path that can trigger a lot for leaf classes. */
726 if (type
->name
== typename
) {
730 target_type
= type_get_by_name(typename
);
732 /* target class type unknown, so fail the cast */
736 if (type
->class->interfaces
&&
737 type_is_ancestor(target_type
, type_interface
)) {
741 for (i
= class->interfaces
; i
; i
= i
->next
) {
742 ObjectClass
*target_class
= i
->data
;
744 if (type_is_ancestor(target_class
->type
, target_type
)) {
750 /* The match was ambiguous, don't allow a cast */
754 } else if (type_is_ancestor(type
, target_type
)) {
761 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
762 const char *typename
,
763 const char *file
, int line
,
768 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
769 typename
, file
, line
, func
);
771 #ifdef CONFIG_QOM_CAST_DEBUG
774 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
775 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
781 if (!class || !class->interfaces
) {
786 ret
= object_class_dynamic_cast(class, typename
);
788 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
789 file
, line
, func
, class, typename
);
793 #ifdef CONFIG_QOM_CAST_DEBUG
794 if (class && ret
== class) {
795 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
796 atomic_set(&class->class_cast_cache
[i
- 1],
797 atomic_read(&class->class_cast_cache
[i
]));
799 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
806 const char *object_get_typename(const Object
*obj
)
808 return obj
->class->type
->name
;
811 ObjectClass
*object_get_class(Object
*obj
)
816 bool object_class_is_abstract(ObjectClass
*klass
)
818 return klass
->type
->abstract
;
821 const char *object_class_get_name(ObjectClass
*klass
)
823 return klass
->type
->name
;
826 ObjectClass
*object_class_by_name(const char *typename
)
828 TypeImpl
*type
= type_get_by_name(typename
);
834 type_initialize(type
);
839 ObjectClass
*object_class_get_parent(ObjectClass
*class)
841 TypeImpl
*type
= type_get_parent(class->type
);
847 type_initialize(type
);
852 typedef struct OCFData
854 void (*fn
)(ObjectClass
*klass
, void *opaque
);
855 const char *implements_type
;
856 bool include_abstract
;
860 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
863 OCFData
*data
= opaque
;
864 TypeImpl
*type
= value
;
867 type_initialize(type
);
870 if (!data
->include_abstract
&& type
->abstract
) {
874 if (data
->implements_type
&&
875 !object_class_dynamic_cast(k
, data
->implements_type
)) {
879 data
->fn(k
, data
->opaque
);
882 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
883 const char *implements_type
, bool include_abstract
,
886 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
888 enumerating_types
= true;
889 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
890 enumerating_types
= false;
893 static int do_object_child_foreach(Object
*obj
,
894 int (*fn
)(Object
*child
, void *opaque
),
895 void *opaque
, bool recurse
)
898 ObjectProperty
*prop
;
901 g_hash_table_iter_init(&iter
, obj
->properties
);
902 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
903 if (object_property_is_child(prop
)) {
904 Object
*child
= prop
->opaque
;
906 ret
= fn(child
, opaque
);
911 do_object_child_foreach(child
, fn
, opaque
, true);
918 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
921 return do_object_child_foreach(obj
, fn
, opaque
, false);
924 int object_child_foreach_recursive(Object
*obj
,
925 int (*fn
)(Object
*child
, void *opaque
),
928 return do_object_child_foreach(obj
, fn
, opaque
, true);
931 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
933 GSList
**list
= opaque
;
935 *list
= g_slist_prepend(*list
, klass
);
938 GSList
*object_class_get_list(const char *implements_type
,
939 bool include_abstract
)
943 object_class_foreach(object_class_get_list_tramp
,
944 implements_type
, include_abstract
, &list
);
948 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
950 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
951 object_class_get_name((ObjectClass
*)b
));
954 GSList
*object_class_get_list_sorted(const char *implements_type
,
955 bool include_abstract
)
957 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
961 void object_ref(Object
*obj
)
966 atomic_inc(&obj
->ref
);
969 void object_unref(Object
*obj
)
974 g_assert(obj
->ref
> 0);
976 /* parent always holds a reference to its children */
977 if (atomic_fetch_dec(&obj
->ref
) == 1) {
978 object_finalize(obj
);
983 object_property_add(Object
*obj
, const char *name
, const char *type
,
984 ObjectPropertyAccessor
*get
,
985 ObjectPropertyAccessor
*set
,
986 ObjectPropertyRelease
*release
,
987 void *opaque
, Error
**errp
)
989 ObjectProperty
*prop
;
990 size_t name_len
= strlen(name
);
992 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
995 char *name_no_array
= g_strdup(name
);
997 name_no_array
[name_len
- 3] = '\0';
999 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1001 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
1002 release
, opaque
, NULL
);
1008 g_free(name_no_array
);
1012 if (object_property_find(obj
, name
, NULL
) != NULL
) {
1013 error_setg(errp
, "attempt to add duplicate property '%s'"
1014 " to object (type '%s')", name
,
1015 object_get_typename(obj
));
1019 prop
= g_malloc0(sizeof(*prop
));
1021 prop
->name
= g_strdup(name
);
1022 prop
->type
= g_strdup(type
);
1026 prop
->release
= release
;
1027 prop
->opaque
= opaque
;
1029 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1034 object_class_property_add(ObjectClass
*klass
,
1037 ObjectPropertyAccessor
*get
,
1038 ObjectPropertyAccessor
*set
,
1039 ObjectPropertyRelease
*release
,
1043 ObjectProperty
*prop
;
1045 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
1046 error_setg(errp
, "attempt to add duplicate property '%s'"
1047 " to object (type '%s')", name
,
1048 object_class_get_name(klass
));
1052 prop
= g_malloc0(sizeof(*prop
));
1054 prop
->name
= g_strdup(name
);
1055 prop
->type
= g_strdup(type
);
1059 prop
->release
= release
;
1060 prop
->opaque
= opaque
;
1062 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
1067 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1070 ObjectProperty
*prop
;
1071 ObjectClass
*klass
= object_get_class(obj
);
1073 prop
= object_class_property_find(klass
, name
, NULL
);
1078 prop
= g_hash_table_lookup(obj
->properties
, name
);
1083 error_setg(errp
, "Property '.%s' not found", name
);
1087 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1090 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1091 iter
->nextclass
= object_get_class(obj
);
1094 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1097 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1098 if (!iter
->nextclass
) {
1101 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1102 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1107 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1110 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1111 iter
->nextclass
= klass
;
1114 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1117 ObjectProperty
*prop
;
1118 ObjectClass
*parent_klass
;
1120 parent_klass
= object_class_get_parent(klass
);
1122 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1128 prop
= g_hash_table_lookup(klass
->properties
, name
);
1130 error_setg(errp
, "Property '.%s' not found", name
);
1135 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1137 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1140 error_setg(errp
, "Property '.%s' not found", name
);
1144 if (prop
->release
) {
1145 prop
->release(obj
, name
, prop
->opaque
);
1147 g_hash_table_remove(obj
->properties
, name
);
1150 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1153 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1159 error_setg(errp
, QERR_PERMISSION_DENIED
);
1161 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1165 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1168 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1174 error_setg(errp
, QERR_PERMISSION_DENIED
);
1176 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1180 void object_property_set_str(Object
*obj
, const char *value
,
1181 const char *name
, Error
**errp
)
1183 QString
*qstr
= qstring_from_str(value
);
1184 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1186 qobject_unref(qstr
);
1189 char *object_property_get_str(Object
*obj
, const char *name
,
1192 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1199 retval
= g_strdup(qobject_get_try_str(ret
));
1201 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1208 void object_property_set_link(Object
*obj
, Object
*value
,
1209 const char *name
, Error
**errp
)
1212 gchar
*path
= object_get_canonical_path(value
);
1213 object_property_set_str(obj
, path
, name
, errp
);
1216 object_property_set_str(obj
, "", name
, errp
);
1220 Object
*object_property_get_link(Object
*obj
, const char *name
,
1223 char *str
= object_property_get_str(obj
, name
, errp
);
1224 Object
*target
= NULL
;
1227 target
= object_resolve_path(str
, NULL
);
1229 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1230 "Device '%s' not found", str
);
1238 void object_property_set_bool(Object
*obj
, bool value
,
1239 const char *name
, Error
**errp
)
1241 QBool
*qbool
= qbool_from_bool(value
);
1242 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1244 qobject_unref(qbool
);
1247 bool object_property_get_bool(Object
*obj
, const char *name
,
1250 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1257 qbool
= qobject_to(QBool
, ret
);
1259 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1262 retval
= qbool_get_bool(qbool
);
1269 void object_property_set_int(Object
*obj
, int64_t value
,
1270 const char *name
, Error
**errp
)
1272 QNum
*qnum
= qnum_from_int(value
);
1273 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1275 qobject_unref(qnum
);
1278 int64_t object_property_get_int(Object
*obj
, const char *name
,
1281 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1289 qnum
= qobject_to(QNum
, ret
);
1290 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1291 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1299 void object_property_set_uint(Object
*obj
, uint64_t value
,
1300 const char *name
, Error
**errp
)
1302 QNum
*qnum
= qnum_from_uint(value
);
1304 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1305 qobject_unref(qnum
);
1308 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1311 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1318 qnum
= qobject_to(QNum
, ret
);
1319 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1320 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1328 typedef struct EnumProperty
{
1329 const QEnumLookup
*lookup
;
1330 int (*get
)(Object
*, Error
**);
1331 void (*set
)(Object
*, int, Error
**);
1334 int object_property_get_enum(Object
*obj
, const char *name
,
1335 const char *typename
, Error
**errp
)
1341 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1342 EnumProperty
*enumprop
;
1348 if (!g_str_equal(prop
->type
, typename
)) {
1349 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1350 name
, object_class_get_name(
1351 object_get_class(obj
)), typename
);
1355 enumprop
= prop
->opaque
;
1357 v
= string_output_visitor_new(false, &str
);
1358 object_property_get(obj
, v
, name
, &err
);
1360 error_propagate(errp
, err
);
1364 visit_complete(v
, &str
);
1366 v
= string_input_visitor_new(str
);
1367 visit_type_enum(v
, name
, &ret
, enumprop
->lookup
, errp
);
1375 void object_property_get_uint16List(Object
*obj
, const char *name
,
1376 uint16List
**list
, Error
**errp
)
1382 v
= string_output_visitor_new(false, &str
);
1383 object_property_get(obj
, v
, name
, &err
);
1385 error_propagate(errp
, err
);
1388 visit_complete(v
, &str
);
1390 v
= string_input_visitor_new(str
);
1391 visit_type_uint16List(v
, NULL
, list
, errp
);
1398 void object_property_parse(Object
*obj
, const char *string
,
1399 const char *name
, Error
**errp
)
1401 Visitor
*v
= string_input_visitor_new(string
);
1402 object_property_set(obj
, v
, name
, errp
);
1406 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1410 char *string
= NULL
;
1411 Error
*local_err
= NULL
;
1413 v
= string_output_visitor_new(human
, &string
);
1414 object_property_get(obj
, v
, name
, &local_err
);
1416 error_propagate(errp
, local_err
);
1420 visit_complete(v
, &string
);
1427 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1429 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1437 Object
*object_get_root(void)
1439 static Object
*root
;
1442 root
= object_new("container");
1448 Object
*object_get_objects_root(void)
1450 return container_get(object_get_root(), "/objects");
1453 Object
*object_get_internal_root(void)
1455 static Object
*internal_root
;
1457 if (!internal_root
) {
1458 internal_root
= object_new("container");
1461 return internal_root
;
1464 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1465 const char *name
, void *opaque
,
1468 Object
*child
= opaque
;
1471 path
= object_get_canonical_path(child
);
1472 visit_type_str(v
, name
, &path
, errp
);
1476 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1481 static void object_finalize_child_property(Object
*obj
, const char *name
,
1484 Object
*child
= opaque
;
1486 if (child
->class->unparent
) {
1487 (child
->class->unparent
)(child
);
1489 child
->parent
= NULL
;
1490 object_unref(child
);
1493 void object_property_add_child(Object
*obj
, const char *name
,
1494 Object
*child
, Error
**errp
)
1496 Error
*local_err
= NULL
;
1500 if (child
->parent
!= NULL
) {
1501 error_setg(errp
, "child object is already parented");
1505 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1507 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1508 object_finalize_child_property
, child
, &local_err
);
1510 error_propagate(errp
, local_err
);
1514 op
->resolve
= object_resolve_child_property
;
1516 child
->parent
= obj
;
1522 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1523 Object
*val
, Error
**errp
)
1525 /* Allow the link to be set, always */
1530 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1531 ObjectPropertyLinkFlags flags
;
1534 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1535 const char *name
, void *opaque
,
1538 LinkProperty
*lprop
= opaque
;
1539 Object
**child
= lprop
->child
;
1543 path
= object_get_canonical_path(*child
);
1544 visit_type_str(v
, name
, &path
, errp
);
1548 visit_type_str(v
, name
, &path
, errp
);
1553 * object_resolve_link:
1555 * Lookup an object and ensure its type matches the link property type. This
1556 * is similar to object_resolve_path() except type verification against the
1557 * link property is performed.
1559 * Returns: The matched object or NULL on path lookup failures.
1561 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1562 const char *path
, Error
**errp
)
1566 bool ambiguous
= false;
1569 /* Go from link<FOO> to FOO. */
1570 type
= object_property_get_type(obj
, name
, NULL
);
1571 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1572 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1575 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1577 } else if (!target
) {
1578 target
= object_resolve_path(path
, &ambiguous
);
1579 if (target
|| ambiguous
) {
1580 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1582 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1583 "Device '%s' not found", path
);
1587 g_free(target_type
);
1592 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1593 const char *name
, void *opaque
,
1596 Error
*local_err
= NULL
;
1597 LinkProperty
*prop
= opaque
;
1598 Object
**child
= prop
->child
;
1599 Object
*old_target
= *child
;
1600 Object
*new_target
= NULL
;
1603 visit_type_str(v
, name
, &path
, &local_err
);
1605 if (!local_err
&& strcmp(path
, "") != 0) {
1606 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1611 error_propagate(errp
, local_err
);
1615 prop
->check(obj
, name
, new_target
, &local_err
);
1617 error_propagate(errp
, local_err
);
1621 *child
= new_target
;
1622 if (prop
->flags
== OBJ_PROP_LINK_STRONG
) {
1623 object_ref(new_target
);
1624 object_unref(old_target
);
1628 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1630 LinkProperty
*lprop
= opaque
;
1632 return *lprop
->child
;
1635 static void object_release_link_property(Object
*obj
, const char *name
,
1638 LinkProperty
*prop
= opaque
;
1640 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *prop
->child
) {
1641 object_unref(*prop
->child
);
1646 void object_property_add_link(Object
*obj
, const char *name
,
1647 const char *type
, Object
**child
,
1648 void (*check
)(const Object
*, const char *,
1649 Object
*, Error
**),
1650 ObjectPropertyLinkFlags flags
,
1653 Error
*local_err
= NULL
;
1654 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1658 prop
->child
= child
;
1659 prop
->check
= check
;
1660 prop
->flags
= flags
;
1662 full_type
= g_strdup_printf("link<%s>", type
);
1664 op
= object_property_add(obj
, name
, full_type
,
1665 object_get_link_property
,
1666 check
? object_set_link_property
: NULL
,
1667 object_release_link_property
,
1671 error_propagate(errp
, local_err
);
1676 op
->resolve
= object_resolve_link_property
;
1682 void object_property_add_const_link(Object
*obj
, const char *name
,
1683 Object
*target
, Error
**errp
)
1688 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1689 op
= object_property_add(obj
, name
, link_type
,
1690 object_get_child_property
, NULL
,
1691 NULL
, target
, errp
);
1693 op
->resolve
= object_resolve_child_property
;
1698 gchar
*object_get_canonical_path_component(Object
*obj
)
1700 ObjectProperty
*prop
= NULL
;
1701 GHashTableIter iter
;
1703 if (obj
->parent
== NULL
) {
1707 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1708 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1709 if (!object_property_is_child(prop
)) {
1713 if (prop
->opaque
== obj
) {
1714 return g_strdup(prop
->name
);
1718 /* obj had a parent but was not a child, should never happen */
1719 g_assert_not_reached();
1723 gchar
*object_get_canonical_path(Object
*obj
)
1725 Object
*root
= object_get_root();
1726 char *newpath
, *path
= NULL
;
1729 return g_strdup("/");
1733 char *component
= object_get_canonical_path_component(obj
);
1736 /* A canonical path must be complete, so discard what was
1743 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
1748 } while (obj
!= root
);
1753 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1755 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1760 if (prop
->resolve
) {
1761 return prop
->resolve(parent
, prop
->opaque
, part
);
1767 static Object
*object_resolve_abs_path(Object
*parent
,
1769 const char *typename
,
1774 if (parts
[index
] == NULL
) {
1775 return object_dynamic_cast(parent
, typename
);
1778 if (strcmp(parts
[index
], "") == 0) {
1779 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1782 child
= object_resolve_path_component(parent
, parts
[index
]);
1787 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1790 static Object
*object_resolve_partial_path(Object
*parent
,
1792 const char *typename
,
1796 GHashTableIter iter
;
1797 ObjectProperty
*prop
;
1799 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1801 g_hash_table_iter_init(&iter
, parent
->properties
);
1802 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1805 if (!object_property_is_child(prop
)) {
1809 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1810 typename
, ambiguous
);
1827 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1833 parts
= g_strsplit(path
, "/", 0);
1836 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1837 bool ambiguous
= false;
1838 obj
= object_resolve_partial_path(object_get_root(), parts
,
1839 typename
, &ambiguous
);
1841 *ambiguousp
= ambiguous
;
1844 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1852 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1854 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1857 typedef struct StringProperty
1859 char *(*get
)(Object
*, Error
**);
1860 void (*set
)(Object
*, const char *, Error
**);
1863 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1864 void *opaque
, Error
**errp
)
1866 StringProperty
*prop
= opaque
;
1870 value
= prop
->get(obj
, &err
);
1872 error_propagate(errp
, err
);
1876 visit_type_str(v
, name
, &value
, errp
);
1880 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1881 void *opaque
, Error
**errp
)
1883 StringProperty
*prop
= opaque
;
1885 Error
*local_err
= NULL
;
1887 visit_type_str(v
, name
, &value
, &local_err
);
1889 error_propagate(errp
, local_err
);
1893 prop
->set(obj
, value
, errp
);
1897 static void property_release_str(Object
*obj
, const char *name
,
1900 StringProperty
*prop
= opaque
;
1904 void object_property_add_str(Object
*obj
, const char *name
,
1905 char *(*get
)(Object
*, Error
**),
1906 void (*set
)(Object
*, const char *, Error
**),
1909 Error
*local_err
= NULL
;
1910 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1915 object_property_add(obj
, name
, "string",
1916 get
? property_get_str
: NULL
,
1917 set
? property_set_str
: NULL
,
1918 property_release_str
,
1921 error_propagate(errp
, local_err
);
1926 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
1927 char *(*get
)(Object
*, Error
**),
1928 void (*set
)(Object
*, const char *,
1932 Error
*local_err
= NULL
;
1933 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1938 object_class_property_add(klass
, name
, "string",
1939 get
? property_get_str
: NULL
,
1940 set
? property_set_str
: NULL
,
1941 property_release_str
,
1944 error_propagate(errp
, local_err
);
1949 typedef struct BoolProperty
1951 bool (*get
)(Object
*, Error
**);
1952 void (*set
)(Object
*, bool, Error
**);
1955 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
1956 void *opaque
, Error
**errp
)
1958 BoolProperty
*prop
= opaque
;
1962 value
= prop
->get(obj
, &err
);
1964 error_propagate(errp
, err
);
1968 visit_type_bool(v
, name
, &value
, errp
);
1971 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
1972 void *opaque
, Error
**errp
)
1974 BoolProperty
*prop
= opaque
;
1976 Error
*local_err
= NULL
;
1978 visit_type_bool(v
, name
, &value
, &local_err
);
1980 error_propagate(errp
, local_err
);
1984 prop
->set(obj
, value
, errp
);
1987 static void property_release_bool(Object
*obj
, const char *name
,
1990 BoolProperty
*prop
= opaque
;
1994 void object_property_add_bool(Object
*obj
, const char *name
,
1995 bool (*get
)(Object
*, Error
**),
1996 void (*set
)(Object
*, bool, Error
**),
1999 Error
*local_err
= NULL
;
2000 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2005 object_property_add(obj
, name
, "bool",
2006 get
? property_get_bool
: NULL
,
2007 set
? property_set_bool
: NULL
,
2008 property_release_bool
,
2011 error_propagate(errp
, local_err
);
2016 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2017 bool (*get
)(Object
*, Error
**),
2018 void (*set
)(Object
*, bool, Error
**),
2021 Error
*local_err
= NULL
;
2022 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2027 object_class_property_add(klass
, name
, "bool",
2028 get
? property_get_bool
: NULL
,
2029 set
? property_set_bool
: NULL
,
2030 property_release_bool
,
2033 error_propagate(errp
, local_err
);
2038 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2039 void *opaque
, Error
**errp
)
2041 EnumProperty
*prop
= opaque
;
2045 value
= prop
->get(obj
, &err
);
2047 error_propagate(errp
, err
);
2051 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2054 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2055 void *opaque
, Error
**errp
)
2057 EnumProperty
*prop
= opaque
;
2061 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
2063 error_propagate(errp
, err
);
2066 prop
->set(obj
, value
, errp
);
2069 static void property_release_enum(Object
*obj
, const char *name
,
2072 EnumProperty
*prop
= opaque
;
2076 void object_property_add_enum(Object
*obj
, const char *name
,
2077 const char *typename
,
2078 const QEnumLookup
*lookup
,
2079 int (*get
)(Object
*, Error
**),
2080 void (*set
)(Object
*, int, Error
**),
2083 Error
*local_err
= NULL
;
2084 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2086 prop
->lookup
= lookup
;
2090 object_property_add(obj
, name
, typename
,
2091 get
? property_get_enum
: NULL
,
2092 set
? property_set_enum
: NULL
,
2093 property_release_enum
,
2096 error_propagate(errp
, local_err
);
2101 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2102 const char *typename
,
2103 const QEnumLookup
*lookup
,
2104 int (*get
)(Object
*, Error
**),
2105 void (*set
)(Object
*, int, Error
**),
2108 Error
*local_err
= NULL
;
2109 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2111 prop
->lookup
= lookup
;
2115 object_class_property_add(klass
, name
, typename
,
2116 get
? property_get_enum
: NULL
,
2117 set
? property_set_enum
: NULL
,
2118 property_release_enum
,
2121 error_propagate(errp
, local_err
);
2126 typedef struct TMProperty
{
2127 void (*get
)(Object
*, struct tm
*, Error
**);
2130 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2131 void *opaque
, Error
**errp
)
2133 TMProperty
*prop
= opaque
;
2137 prop
->get(obj
, &value
, &err
);
2142 visit_start_struct(v
, name
, NULL
, 0, &err
);
2146 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2150 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2154 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2158 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2162 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2166 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2170 visit_check_struct(v
, &err
);
2172 visit_end_struct(v
, NULL
);
2174 error_propagate(errp
, err
);
2178 static void property_release_tm(Object
*obj
, const char *name
,
2181 TMProperty
*prop
= opaque
;
2185 void object_property_add_tm(Object
*obj
, const char *name
,
2186 void (*get
)(Object
*, struct tm
*, Error
**),
2189 Error
*local_err
= NULL
;
2190 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2194 object_property_add(obj
, name
, "struct tm",
2195 get
? property_get_tm
: NULL
, NULL
,
2196 property_release_tm
,
2199 error_propagate(errp
, local_err
);
2204 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2205 void (*get
)(Object
*, struct tm
*, Error
**),
2208 Error
*local_err
= NULL
;
2209 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2213 object_class_property_add(klass
, name
, "struct tm",
2214 get
? property_get_tm
: NULL
, NULL
,
2215 property_release_tm
,
2218 error_propagate(errp
, local_err
);
2223 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2225 return g_strdup(object_get_typename(obj
));
2228 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2229 void *opaque
, Error
**errp
)
2231 uint8_t value
= *(uint8_t *)opaque
;
2232 visit_type_uint8(v
, name
, &value
, errp
);
2235 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2236 void *opaque
, Error
**errp
)
2238 uint16_t value
= *(uint16_t *)opaque
;
2239 visit_type_uint16(v
, name
, &value
, errp
);
2242 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2243 void *opaque
, Error
**errp
)
2245 uint32_t value
= *(uint32_t *)opaque
;
2246 visit_type_uint32(v
, name
, &value
, errp
);
2249 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2250 void *opaque
, Error
**errp
)
2252 uint64_t value
= *(uint64_t *)opaque
;
2253 visit_type_uint64(v
, name
, &value
, errp
);
2256 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2257 const uint8_t *v
, Error
**errp
)
2259 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2260 NULL
, NULL
, (void *)v
, errp
);
2263 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2264 const uint8_t *v
, Error
**errp
)
2266 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2267 NULL
, NULL
, (void *)v
, errp
);
2270 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2271 const uint16_t *v
, Error
**errp
)
2273 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2274 NULL
, NULL
, (void *)v
, errp
);
2277 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2278 const uint16_t *v
, Error
**errp
)
2280 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2281 NULL
, NULL
, (void *)v
, errp
);
2284 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2285 const uint32_t *v
, Error
**errp
)
2287 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2288 NULL
, NULL
, (void *)v
, errp
);
2291 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2292 const uint32_t *v
, Error
**errp
)
2294 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2295 NULL
, NULL
, (void *)v
, errp
);
2298 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2299 const uint64_t *v
, Error
**errp
)
2301 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2302 NULL
, NULL
, (void *)v
, errp
);
2305 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2306 const uint64_t *v
, Error
**errp
)
2308 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2309 NULL
, NULL
, (void *)v
, errp
);
2317 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2318 void *opaque
, Error
**errp
)
2320 AliasProperty
*prop
= opaque
;
2322 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2325 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2326 void *opaque
, Error
**errp
)
2328 AliasProperty
*prop
= opaque
;
2330 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2333 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2336 AliasProperty
*prop
= opaque
;
2338 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2341 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2343 AliasProperty
*prop
= opaque
;
2345 g_free(prop
->target_name
);
2349 void object_property_add_alias(Object
*obj
, const char *name
,
2350 Object
*target_obj
, const char *target_name
,
2353 AliasProperty
*prop
;
2355 ObjectProperty
*target_prop
;
2357 Error
*local_err
= NULL
;
2359 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2364 if (object_property_is_child(target_prop
)) {
2365 prop_type
= g_strdup_printf("link%s",
2366 target_prop
->type
+ strlen("child"));
2368 prop_type
= g_strdup(target_prop
->type
);
2371 prop
= g_malloc(sizeof(*prop
));
2372 prop
->target_obj
= target_obj
;
2373 prop
->target_name
= g_strdup(target_name
);
2375 op
= object_property_add(obj
, name
, prop_type
,
2378 property_release_alias
,
2381 error_propagate(errp
, local_err
);
2385 op
->resolve
= property_resolve_alias
;
2387 object_property_set_description(obj
, op
->name
,
2388 target_prop
->description
,
2395 void object_property_set_description(Object
*obj
, const char *name
,
2396 const char *description
, Error
**errp
)
2400 op
= object_property_find(obj
, name
, errp
);
2405 g_free(op
->description
);
2406 op
->description
= g_strdup(description
);
2409 void object_class_property_set_description(ObjectClass
*klass
,
2411 const char *description
,
2416 op
= g_hash_table_lookup(klass
->properties
, name
);
2418 error_setg(errp
, "Property '.%s' not found", name
);
2422 g_free(op
->description
);
2423 op
->description
= g_strdup(description
);
2426 static void object_instance_init(Object
*obj
)
2428 object_property_add_str(obj
, "type", qdev_get_type
, NULL
, NULL
);
2431 static void register_types(void)
2433 static TypeInfo interface_info
= {
2434 .name
= TYPE_INTERFACE
,
2435 .class_size
= sizeof(InterfaceClass
),
2439 static TypeInfo object_info
= {
2440 .name
= TYPE_OBJECT
,
2441 .instance_size
= sizeof(Object
),
2442 .instance_init
= object_instance_init
,
2446 type_interface
= type_register_internal(&interface_info
);
2447 type_register_internal(&object_info
);
2450 type_init(register_types
)