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) {
289 if (type_is_ancestor(ti
, type_interface
)) {
290 assert(ti
->instance_size
== 0);
291 assert(ti
->abstract
);
292 assert(!ti
->instance_init
);
293 assert(!ti
->instance_post_init
);
294 assert(!ti
->instance_finalize
);
295 assert(!ti
->num_interfaces
);
297 ti
->class = g_malloc0(ti
->class_size
);
299 parent
= type_get_parent(ti
);
301 type_initialize(parent
);
305 g_assert(parent
->class_size
<= ti
->class_size
);
306 memcpy(ti
->class, parent
->class, parent
->class_size
);
307 ti
->class->interfaces
= NULL
;
308 ti
->class->properties
= g_hash_table_new_full(
309 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
311 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
312 InterfaceClass
*iface
= e
->data
;
313 ObjectClass
*klass
= OBJECT_CLASS(iface
);
315 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
318 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
319 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
320 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
321 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
323 if (type_is_ancestor(target_type
, t
)) {
332 type_initialize_interface(ti
, t
, t
);
335 ti
->class->properties
= g_hash_table_new_full(
336 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
339 ti
->class->type
= ti
;
342 if (parent
->class_base_init
) {
343 parent
->class_base_init(ti
->class, ti
->class_data
);
345 parent
= type_get_parent(parent
);
348 if (ti
->class_init
) {
349 ti
->class_init(ti
->class, ti
->class_data
);
353 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
355 if (type_has_parent(ti
)) {
356 object_init_with_type(obj
, type_get_parent(ti
));
359 if (ti
->instance_init
) {
360 ti
->instance_init(obj
);
364 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
366 if (ti
->instance_post_init
) {
367 ti
->instance_post_init(obj
);
370 if (type_has_parent(ti
)) {
371 object_post_init_with_type(obj
, type_get_parent(ti
));
375 static void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
379 g_assert(type
!= NULL
);
380 type_initialize(type
);
382 g_assert(type
->instance_size
>= sizeof(Object
));
383 g_assert(type
->abstract
== false);
384 g_assert(size
>= type
->instance_size
);
386 memset(obj
, 0, type
->instance_size
);
387 obj
->class = type
->class;
389 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
390 NULL
, object_property_free
);
391 object_init_with_type(obj
, type
);
392 object_post_init_with_type(obj
, type
);
395 void object_initialize(void *data
, size_t size
, const char *typename
)
397 TypeImpl
*type
= type_get_by_name(typename
);
399 object_initialize_with_type(data
, size
, type
);
402 void object_initialize_child(Object
*parentobj
, const char *propname
,
403 void *childobj
, size_t size
, const char *type
,
408 va_start(vargs
, errp
);
409 object_initialize_childv(parentobj
, propname
, childobj
, size
, type
, errp
,
414 void object_initialize_childv(Object
*parentobj
, const char *propname
,
415 void *childobj
, size_t size
, const char *type
,
416 Error
**errp
, va_list vargs
)
418 Error
*local_err
= NULL
;
421 object_initialize(childobj
, size
, type
);
422 obj
= OBJECT(childobj
);
424 object_set_propv(obj
, &local_err
, vargs
);
429 object_property_add_child(parentobj
, propname
, obj
, &local_err
);
434 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
435 user_creatable_complete(obj
, &local_err
);
437 object_unparent(obj
);
443 * Since object_property_add_child added a reference to the child object,
444 * we can drop the reference added by object_initialize(), so the child
445 * property will own the only reference to the object.
451 error_propagate(errp
, local_err
);
456 static inline bool object_property_is_child(ObjectProperty
*prop
)
458 return strstart(prop
->type
, "child<", NULL
);
461 static void object_property_del_all(Object
*obj
)
463 ObjectProperty
*prop
;
470 g_hash_table_iter_init(&iter
, obj
->properties
);
471 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
474 prop
->release(obj
, prop
->name
, prop
->opaque
);
475 prop
->release
= NULL
;
479 g_hash_table_iter_remove(&iter
);
483 g_hash_table_unref(obj
->properties
);
486 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
488 ObjectProperty
*prop
;
492 g_hash_table_iter_init(&iter
, obj
->properties
);
493 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
495 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
497 prop
->release(obj
, prop
->name
, prop
->opaque
);
498 prop
->release
= NULL
;
503 g_hash_table_iter_init(&iter
, obj
->properties
);
504 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
506 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
507 g_hash_table_iter_remove(&iter
);
513 void object_unparent(Object
*obj
)
516 object_property_del_child(obj
->parent
, obj
, NULL
);
520 static void object_deinit(Object
*obj
, TypeImpl
*type
)
522 if (type
->instance_finalize
) {
523 type
->instance_finalize(obj
);
526 if (type_has_parent(type
)) {
527 object_deinit(obj
, type_get_parent(type
));
531 static void object_finalize(void *data
)
534 TypeImpl
*ti
= obj
->class->type
;
536 object_property_del_all(obj
);
537 object_deinit(obj
, ti
);
539 g_assert(obj
->ref
== 0);
545 static Object
*object_new_with_type(Type type
)
549 g_assert(type
!= NULL
);
550 type_initialize(type
);
552 obj
= g_malloc(type
->instance_size
);
553 object_initialize_with_type(obj
, type
->instance_size
, type
);
559 Object
*object_new(const char *typename
)
561 TypeImpl
*ti
= type_get_by_name(typename
);
563 return object_new_with_type(ti
);
567 Object
*object_new_with_props(const char *typename
,
576 va_start(vargs
, errp
);
577 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
584 Object
*object_new_with_propv(const char *typename
,
592 Error
*local_err
= NULL
;
594 klass
= object_class_by_name(typename
);
596 error_setg(errp
, "invalid object type: %s", typename
);
600 if (object_class_is_abstract(klass
)) {
601 error_setg(errp
, "object type '%s' is abstract", typename
);
604 obj
= object_new(typename
);
606 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
610 object_property_add_child(parent
, id
, obj
, &local_err
);
615 if (object_dynamic_cast(obj
, TYPE_USER_CREATABLE
)) {
616 user_creatable_complete(obj
, &local_err
);
618 object_unparent(obj
);
623 object_unref(OBJECT(obj
));
627 error_propagate(errp
, local_err
);
633 int object_set_props(Object
*obj
,
640 va_start(vargs
, errp
);
641 ret
= object_set_propv(obj
, errp
, vargs
);
648 int object_set_propv(Object
*obj
,
652 const char *propname
;
653 Error
*local_err
= NULL
;
655 propname
= va_arg(vargs
, char *);
656 while (propname
!= NULL
) {
657 const char *value
= va_arg(vargs
, char *);
659 g_assert(value
!= NULL
);
660 object_property_parse(obj
, value
, propname
, &local_err
);
662 error_propagate(errp
, local_err
);
665 propname
= va_arg(vargs
, char *);
672 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
674 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
681 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
682 const char *file
, int line
, const char *func
)
684 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
685 typename
, file
, line
, func
);
687 #ifdef CONFIG_QOM_CAST_DEBUG
691 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
692 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
697 inst
= object_dynamic_cast(obj
, typename
);
700 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
701 file
, line
, func
, obj
, typename
);
707 if (obj
&& obj
== inst
) {
708 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
709 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
710 atomic_read(&obj
->class->object_cast_cache
[i
]));
712 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
720 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
721 const char *typename
)
723 ObjectClass
*ret
= NULL
;
724 TypeImpl
*target_type
;
731 /* A simple fast path that can trigger a lot for leaf classes. */
733 if (type
->name
== typename
) {
737 target_type
= type_get_by_name(typename
);
739 /* target class type unknown, so fail the cast */
743 if (type
->class->interfaces
&&
744 type_is_ancestor(target_type
, type_interface
)) {
748 for (i
= class->interfaces
; i
; i
= i
->next
) {
749 ObjectClass
*target_class
= i
->data
;
751 if (type_is_ancestor(target_class
->type
, target_type
)) {
757 /* The match was ambiguous, don't allow a cast */
761 } else if (type_is_ancestor(type
, target_type
)) {
768 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
769 const char *typename
,
770 const char *file
, int line
,
775 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
776 typename
, file
, line
, func
);
778 #ifdef CONFIG_QOM_CAST_DEBUG
781 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
782 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
788 if (!class || !class->interfaces
) {
793 ret
= object_class_dynamic_cast(class, typename
);
795 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
796 file
, line
, func
, class, typename
);
800 #ifdef CONFIG_QOM_CAST_DEBUG
801 if (class && ret
== class) {
802 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
803 atomic_set(&class->class_cast_cache
[i
- 1],
804 atomic_read(&class->class_cast_cache
[i
]));
806 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
813 const char *object_get_typename(const Object
*obj
)
815 return obj
->class->type
->name
;
818 ObjectClass
*object_get_class(Object
*obj
)
823 bool object_class_is_abstract(ObjectClass
*klass
)
825 return klass
->type
->abstract
;
828 const char *object_class_get_name(ObjectClass
*klass
)
830 return klass
->type
->name
;
833 ObjectClass
*object_class_by_name(const char *typename
)
835 TypeImpl
*type
= type_get_by_name(typename
);
841 type_initialize(type
);
846 ObjectClass
*object_class_get_parent(ObjectClass
*class)
848 TypeImpl
*type
= type_get_parent(class->type
);
854 type_initialize(type
);
859 typedef struct OCFData
861 void (*fn
)(ObjectClass
*klass
, void *opaque
);
862 const char *implements_type
;
863 bool include_abstract
;
867 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
870 OCFData
*data
= opaque
;
871 TypeImpl
*type
= value
;
874 type_initialize(type
);
877 if (!data
->include_abstract
&& type
->abstract
) {
881 if (data
->implements_type
&&
882 !object_class_dynamic_cast(k
, data
->implements_type
)) {
886 data
->fn(k
, data
->opaque
);
889 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
890 const char *implements_type
, bool include_abstract
,
893 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
895 enumerating_types
= true;
896 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
897 enumerating_types
= false;
900 static int do_object_child_foreach(Object
*obj
,
901 int (*fn
)(Object
*child
, void *opaque
),
902 void *opaque
, bool recurse
)
905 ObjectProperty
*prop
;
908 g_hash_table_iter_init(&iter
, obj
->properties
);
909 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
910 if (object_property_is_child(prop
)) {
911 Object
*child
= prop
->opaque
;
913 ret
= fn(child
, opaque
);
918 do_object_child_foreach(child
, fn
, opaque
, true);
925 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
928 return do_object_child_foreach(obj
, fn
, opaque
, false);
931 int object_child_foreach_recursive(Object
*obj
,
932 int (*fn
)(Object
*child
, void *opaque
),
935 return do_object_child_foreach(obj
, fn
, opaque
, true);
938 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
940 GSList
**list
= opaque
;
942 *list
= g_slist_prepend(*list
, klass
);
945 GSList
*object_class_get_list(const char *implements_type
,
946 bool include_abstract
)
950 object_class_foreach(object_class_get_list_tramp
,
951 implements_type
, include_abstract
, &list
);
955 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
957 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
958 object_class_get_name((ObjectClass
*)b
));
961 GSList
*object_class_get_list_sorted(const char *implements_type
,
962 bool include_abstract
)
964 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
968 void object_ref(Object
*obj
)
973 atomic_inc(&obj
->ref
);
976 void object_unref(Object
*obj
)
981 g_assert(obj
->ref
> 0);
983 /* parent always holds a reference to its children */
984 if (atomic_fetch_dec(&obj
->ref
) == 1) {
985 object_finalize(obj
);
990 object_property_add(Object
*obj
, const char *name
, const char *type
,
991 ObjectPropertyAccessor
*get
,
992 ObjectPropertyAccessor
*set
,
993 ObjectPropertyRelease
*release
,
994 void *opaque
, Error
**errp
)
996 ObjectProperty
*prop
;
997 size_t name_len
= strlen(name
);
999 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1001 ObjectProperty
*ret
;
1002 char *name_no_array
= g_strdup(name
);
1004 name_no_array
[name_len
- 3] = '\0';
1005 for (i
= 0; ; ++i
) {
1006 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1008 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
1009 release
, opaque
, NULL
);
1015 g_free(name_no_array
);
1019 if (object_property_find(obj
, name
, NULL
) != NULL
) {
1020 error_setg(errp
, "attempt to add duplicate property '%s'"
1021 " to object (type '%s')", name
,
1022 object_get_typename(obj
));
1026 prop
= g_malloc0(sizeof(*prop
));
1028 prop
->name
= g_strdup(name
);
1029 prop
->type
= g_strdup(type
);
1033 prop
->release
= release
;
1034 prop
->opaque
= opaque
;
1036 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1041 object_class_property_add(ObjectClass
*klass
,
1044 ObjectPropertyAccessor
*get
,
1045 ObjectPropertyAccessor
*set
,
1046 ObjectPropertyRelease
*release
,
1050 ObjectProperty
*prop
;
1052 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
1053 error_setg(errp
, "attempt to add duplicate property '%s'"
1054 " to object (type '%s')", name
,
1055 object_class_get_name(klass
));
1059 prop
= g_malloc0(sizeof(*prop
));
1061 prop
->name
= g_strdup(name
);
1062 prop
->type
= g_strdup(type
);
1066 prop
->release
= release
;
1067 prop
->opaque
= opaque
;
1069 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
1074 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1077 ObjectProperty
*prop
;
1078 ObjectClass
*klass
= object_get_class(obj
);
1080 prop
= object_class_property_find(klass
, name
, NULL
);
1085 prop
= g_hash_table_lookup(obj
->properties
, name
);
1090 error_setg(errp
, "Property '.%s' not found", name
);
1094 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1097 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1098 iter
->nextclass
= object_get_class(obj
);
1101 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1104 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1105 if (!iter
->nextclass
) {
1108 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1109 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1114 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1117 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1118 iter
->nextclass
= object_class_get_parent(klass
);
1121 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1124 ObjectProperty
*prop
;
1125 ObjectClass
*parent_klass
;
1127 parent_klass
= object_class_get_parent(klass
);
1129 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1135 prop
= g_hash_table_lookup(klass
->properties
, name
);
1137 error_setg(errp
, "Property '.%s' not found", name
);
1142 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1144 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1147 error_setg(errp
, "Property '.%s' not found", name
);
1151 if (prop
->release
) {
1152 prop
->release(obj
, name
, prop
->opaque
);
1154 g_hash_table_remove(obj
->properties
, name
);
1157 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1160 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1166 error_setg(errp
, QERR_PERMISSION_DENIED
);
1168 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1172 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1175 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1181 error_setg(errp
, QERR_PERMISSION_DENIED
);
1183 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1187 void object_property_set_str(Object
*obj
, const char *value
,
1188 const char *name
, Error
**errp
)
1190 QString
*qstr
= qstring_from_str(value
);
1191 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1193 qobject_unref(qstr
);
1196 char *object_property_get_str(Object
*obj
, const char *name
,
1199 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1206 retval
= g_strdup(qobject_get_try_str(ret
));
1208 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1215 void object_property_set_link(Object
*obj
, Object
*value
,
1216 const char *name
, Error
**errp
)
1219 gchar
*path
= object_get_canonical_path(value
);
1220 object_property_set_str(obj
, path
, name
, errp
);
1223 object_property_set_str(obj
, "", name
, errp
);
1227 Object
*object_property_get_link(Object
*obj
, const char *name
,
1230 char *str
= object_property_get_str(obj
, name
, errp
);
1231 Object
*target
= NULL
;
1234 target
= object_resolve_path(str
, NULL
);
1236 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1237 "Device '%s' not found", str
);
1245 void object_property_set_bool(Object
*obj
, bool value
,
1246 const char *name
, Error
**errp
)
1248 QBool
*qbool
= qbool_from_bool(value
);
1249 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1251 qobject_unref(qbool
);
1254 bool object_property_get_bool(Object
*obj
, const char *name
,
1257 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1264 qbool
= qobject_to(QBool
, ret
);
1266 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1269 retval
= qbool_get_bool(qbool
);
1276 void object_property_set_int(Object
*obj
, int64_t value
,
1277 const char *name
, Error
**errp
)
1279 QNum
*qnum
= qnum_from_int(value
);
1280 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1282 qobject_unref(qnum
);
1285 int64_t object_property_get_int(Object
*obj
, const char *name
,
1288 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1296 qnum
= qobject_to(QNum
, ret
);
1297 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1298 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1306 void object_property_set_uint(Object
*obj
, uint64_t value
,
1307 const char *name
, Error
**errp
)
1309 QNum
*qnum
= qnum_from_uint(value
);
1311 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1312 qobject_unref(qnum
);
1315 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1318 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1325 qnum
= qobject_to(QNum
, ret
);
1326 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1327 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1335 typedef struct EnumProperty
{
1336 const QEnumLookup
*lookup
;
1337 int (*get
)(Object
*, Error
**);
1338 void (*set
)(Object
*, int, Error
**);
1341 int object_property_get_enum(Object
*obj
, const char *name
,
1342 const char *typename
, Error
**errp
)
1348 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1349 EnumProperty
*enumprop
;
1355 if (!g_str_equal(prop
->type
, typename
)) {
1356 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1357 name
, object_class_get_name(
1358 object_get_class(obj
)), typename
);
1362 enumprop
= prop
->opaque
;
1364 v
= string_output_visitor_new(false, &str
);
1365 object_property_get(obj
, v
, name
, &err
);
1367 error_propagate(errp
, err
);
1371 visit_complete(v
, &str
);
1373 v
= string_input_visitor_new(str
);
1374 visit_type_enum(v
, name
, &ret
, enumprop
->lookup
, errp
);
1382 void object_property_get_uint16List(Object
*obj
, const char *name
,
1383 uint16List
**list
, Error
**errp
)
1389 v
= string_output_visitor_new(false, &str
);
1390 object_property_get(obj
, v
, name
, &err
);
1392 error_propagate(errp
, err
);
1395 visit_complete(v
, &str
);
1397 v
= string_input_visitor_new(str
);
1398 visit_type_uint16List(v
, NULL
, list
, errp
);
1405 void object_property_parse(Object
*obj
, const char *string
,
1406 const char *name
, Error
**errp
)
1408 Visitor
*v
= string_input_visitor_new(string
);
1409 object_property_set(obj
, v
, name
, errp
);
1413 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1417 char *string
= NULL
;
1418 Error
*local_err
= NULL
;
1420 v
= string_output_visitor_new(human
, &string
);
1421 object_property_get(obj
, v
, name
, &local_err
);
1423 error_propagate(errp
, local_err
);
1427 visit_complete(v
, &string
);
1434 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1436 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1444 Object
*object_get_root(void)
1446 static Object
*root
;
1449 root
= object_new("container");
1455 Object
*object_get_objects_root(void)
1457 return container_get(object_get_root(), "/objects");
1460 Object
*object_get_internal_root(void)
1462 static Object
*internal_root
;
1464 if (!internal_root
) {
1465 internal_root
= object_new("container");
1468 return internal_root
;
1471 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1472 const char *name
, void *opaque
,
1475 Object
*child
= opaque
;
1478 path
= object_get_canonical_path(child
);
1479 visit_type_str(v
, name
, &path
, errp
);
1483 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1488 static void object_finalize_child_property(Object
*obj
, const char *name
,
1491 Object
*child
= opaque
;
1493 if (child
->class->unparent
) {
1494 (child
->class->unparent
)(child
);
1496 child
->parent
= NULL
;
1497 object_unref(child
);
1500 void object_property_add_child(Object
*obj
, const char *name
,
1501 Object
*child
, Error
**errp
)
1503 Error
*local_err
= NULL
;
1507 if (child
->parent
!= NULL
) {
1508 error_setg(errp
, "child object is already parented");
1512 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1514 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1515 object_finalize_child_property
, child
, &local_err
);
1517 error_propagate(errp
, local_err
);
1521 op
->resolve
= object_resolve_child_property
;
1523 child
->parent
= obj
;
1529 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1530 Object
*val
, Error
**errp
)
1532 /* Allow the link to be set, always */
1537 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1538 ObjectPropertyLinkFlags flags
;
1541 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1542 const char *name
, void *opaque
,
1545 LinkProperty
*lprop
= opaque
;
1546 Object
**child
= lprop
->child
;
1550 path
= object_get_canonical_path(*child
);
1551 visit_type_str(v
, name
, &path
, errp
);
1555 visit_type_str(v
, name
, &path
, errp
);
1560 * object_resolve_link:
1562 * Lookup an object and ensure its type matches the link property type. This
1563 * is similar to object_resolve_path() except type verification against the
1564 * link property is performed.
1566 * Returns: The matched object or NULL on path lookup failures.
1568 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1569 const char *path
, Error
**errp
)
1573 bool ambiguous
= false;
1576 /* Go from link<FOO> to FOO. */
1577 type
= object_property_get_type(obj
, name
, NULL
);
1578 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1579 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1582 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1584 } else if (!target
) {
1585 target
= object_resolve_path(path
, &ambiguous
);
1586 if (target
|| ambiguous
) {
1587 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1589 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1590 "Device '%s' not found", path
);
1594 g_free(target_type
);
1599 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1600 const char *name
, void *opaque
,
1603 Error
*local_err
= NULL
;
1604 LinkProperty
*prop
= opaque
;
1605 Object
**child
= prop
->child
;
1606 Object
*old_target
= *child
;
1607 Object
*new_target
= NULL
;
1610 visit_type_str(v
, name
, &path
, &local_err
);
1612 if (!local_err
&& strcmp(path
, "") != 0) {
1613 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1618 error_propagate(errp
, local_err
);
1622 prop
->check(obj
, name
, new_target
, &local_err
);
1624 error_propagate(errp
, local_err
);
1628 *child
= new_target
;
1629 if (prop
->flags
== OBJ_PROP_LINK_STRONG
) {
1630 object_ref(new_target
);
1631 object_unref(old_target
);
1635 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1637 LinkProperty
*lprop
= opaque
;
1639 return *lprop
->child
;
1642 static void object_release_link_property(Object
*obj
, const char *name
,
1645 LinkProperty
*prop
= opaque
;
1647 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *prop
->child
) {
1648 object_unref(*prop
->child
);
1653 void object_property_add_link(Object
*obj
, const char *name
,
1654 const char *type
, Object
**child
,
1655 void (*check
)(const Object
*, const char *,
1656 Object
*, Error
**),
1657 ObjectPropertyLinkFlags flags
,
1660 Error
*local_err
= NULL
;
1661 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1665 prop
->child
= child
;
1666 prop
->check
= check
;
1667 prop
->flags
= flags
;
1669 full_type
= g_strdup_printf("link<%s>", type
);
1671 op
= object_property_add(obj
, name
, full_type
,
1672 object_get_link_property
,
1673 check
? object_set_link_property
: NULL
,
1674 object_release_link_property
,
1678 error_propagate(errp
, local_err
);
1683 op
->resolve
= object_resolve_link_property
;
1689 void object_property_add_const_link(Object
*obj
, const char *name
,
1690 Object
*target
, Error
**errp
)
1695 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1696 op
= object_property_add(obj
, name
, link_type
,
1697 object_get_child_property
, NULL
,
1698 NULL
, target
, errp
);
1700 op
->resolve
= object_resolve_child_property
;
1705 gchar
*object_get_canonical_path_component(Object
*obj
)
1707 ObjectProperty
*prop
= NULL
;
1708 GHashTableIter iter
;
1710 if (obj
->parent
== NULL
) {
1714 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1715 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1716 if (!object_property_is_child(prop
)) {
1720 if (prop
->opaque
== obj
) {
1721 return g_strdup(prop
->name
);
1725 /* obj had a parent but was not a child, should never happen */
1726 g_assert_not_reached();
1730 gchar
*object_get_canonical_path(Object
*obj
)
1732 Object
*root
= object_get_root();
1733 char *newpath
, *path
= NULL
;
1736 return g_strdup("/");
1740 char *component
= object_get_canonical_path_component(obj
);
1743 /* A canonical path must be complete, so discard what was
1750 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
1755 } while (obj
!= root
);
1760 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1762 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1767 if (prop
->resolve
) {
1768 return prop
->resolve(parent
, prop
->opaque
, part
);
1774 static Object
*object_resolve_abs_path(Object
*parent
,
1776 const char *typename
,
1781 if (parts
[index
] == NULL
) {
1782 return object_dynamic_cast(parent
, typename
);
1785 if (strcmp(parts
[index
], "") == 0) {
1786 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1789 child
= object_resolve_path_component(parent
, parts
[index
]);
1794 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1797 static Object
*object_resolve_partial_path(Object
*parent
,
1799 const char *typename
,
1803 GHashTableIter iter
;
1804 ObjectProperty
*prop
;
1806 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1808 g_hash_table_iter_init(&iter
, parent
->properties
);
1809 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1812 if (!object_property_is_child(prop
)) {
1816 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1817 typename
, ambiguous
);
1834 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1840 parts
= g_strsplit(path
, "/", 0);
1843 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1844 bool ambiguous
= false;
1845 obj
= object_resolve_partial_path(object_get_root(), parts
,
1846 typename
, &ambiguous
);
1848 *ambiguousp
= ambiguous
;
1851 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1859 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1861 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1864 typedef struct StringProperty
1866 char *(*get
)(Object
*, Error
**);
1867 void (*set
)(Object
*, const char *, Error
**);
1870 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1871 void *opaque
, Error
**errp
)
1873 StringProperty
*prop
= opaque
;
1877 value
= prop
->get(obj
, &err
);
1879 error_propagate(errp
, err
);
1883 visit_type_str(v
, name
, &value
, errp
);
1887 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1888 void *opaque
, Error
**errp
)
1890 StringProperty
*prop
= opaque
;
1892 Error
*local_err
= NULL
;
1894 visit_type_str(v
, name
, &value
, &local_err
);
1896 error_propagate(errp
, local_err
);
1900 prop
->set(obj
, value
, errp
);
1904 static void property_release_str(Object
*obj
, const char *name
,
1907 StringProperty
*prop
= opaque
;
1911 void object_property_add_str(Object
*obj
, const char *name
,
1912 char *(*get
)(Object
*, Error
**),
1913 void (*set
)(Object
*, const char *, Error
**),
1916 Error
*local_err
= NULL
;
1917 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1922 object_property_add(obj
, name
, "string",
1923 get
? property_get_str
: NULL
,
1924 set
? property_set_str
: NULL
,
1925 property_release_str
,
1928 error_propagate(errp
, local_err
);
1933 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
1934 char *(*get
)(Object
*, Error
**),
1935 void (*set
)(Object
*, const char *,
1939 Error
*local_err
= NULL
;
1940 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
1945 object_class_property_add(klass
, name
, "string",
1946 get
? property_get_str
: NULL
,
1947 set
? property_set_str
: NULL
,
1948 property_release_str
,
1951 error_propagate(errp
, local_err
);
1956 typedef struct BoolProperty
1958 bool (*get
)(Object
*, Error
**);
1959 void (*set
)(Object
*, bool, Error
**);
1962 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
1963 void *opaque
, Error
**errp
)
1965 BoolProperty
*prop
= opaque
;
1969 value
= prop
->get(obj
, &err
);
1971 error_propagate(errp
, err
);
1975 visit_type_bool(v
, name
, &value
, errp
);
1978 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
1979 void *opaque
, Error
**errp
)
1981 BoolProperty
*prop
= opaque
;
1983 Error
*local_err
= NULL
;
1985 visit_type_bool(v
, name
, &value
, &local_err
);
1987 error_propagate(errp
, local_err
);
1991 prop
->set(obj
, value
, errp
);
1994 static void property_release_bool(Object
*obj
, const char *name
,
1997 BoolProperty
*prop
= opaque
;
2001 void object_property_add_bool(Object
*obj
, const char *name
,
2002 bool (*get
)(Object
*, Error
**),
2003 void (*set
)(Object
*, bool, Error
**),
2006 Error
*local_err
= NULL
;
2007 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2012 object_property_add(obj
, name
, "bool",
2013 get
? property_get_bool
: NULL
,
2014 set
? property_set_bool
: NULL
,
2015 property_release_bool
,
2018 error_propagate(errp
, local_err
);
2023 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2024 bool (*get
)(Object
*, Error
**),
2025 void (*set
)(Object
*, bool, Error
**),
2028 Error
*local_err
= NULL
;
2029 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2034 object_class_property_add(klass
, name
, "bool",
2035 get
? property_get_bool
: NULL
,
2036 set
? property_set_bool
: NULL
,
2037 property_release_bool
,
2040 error_propagate(errp
, local_err
);
2045 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2046 void *opaque
, Error
**errp
)
2048 EnumProperty
*prop
= opaque
;
2052 value
= prop
->get(obj
, &err
);
2054 error_propagate(errp
, err
);
2058 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2061 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2062 void *opaque
, Error
**errp
)
2064 EnumProperty
*prop
= opaque
;
2068 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
2070 error_propagate(errp
, err
);
2073 prop
->set(obj
, value
, errp
);
2076 static void property_release_enum(Object
*obj
, const char *name
,
2079 EnumProperty
*prop
= opaque
;
2083 void object_property_add_enum(Object
*obj
, const char *name
,
2084 const char *typename
,
2085 const QEnumLookup
*lookup
,
2086 int (*get
)(Object
*, Error
**),
2087 void (*set
)(Object
*, int, Error
**),
2090 Error
*local_err
= NULL
;
2091 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2093 prop
->lookup
= lookup
;
2097 object_property_add(obj
, name
, typename
,
2098 get
? property_get_enum
: NULL
,
2099 set
? property_set_enum
: NULL
,
2100 property_release_enum
,
2103 error_propagate(errp
, local_err
);
2108 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2109 const char *typename
,
2110 const QEnumLookup
*lookup
,
2111 int (*get
)(Object
*, Error
**),
2112 void (*set
)(Object
*, int, Error
**),
2115 Error
*local_err
= NULL
;
2116 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2118 prop
->lookup
= lookup
;
2122 object_class_property_add(klass
, name
, typename
,
2123 get
? property_get_enum
: NULL
,
2124 set
? property_set_enum
: NULL
,
2125 property_release_enum
,
2128 error_propagate(errp
, local_err
);
2133 typedef struct TMProperty
{
2134 void (*get
)(Object
*, struct tm
*, Error
**);
2137 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2138 void *opaque
, Error
**errp
)
2140 TMProperty
*prop
= opaque
;
2144 prop
->get(obj
, &value
, &err
);
2149 visit_start_struct(v
, name
, NULL
, 0, &err
);
2153 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2157 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2161 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2165 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2169 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2173 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2177 visit_check_struct(v
, &err
);
2179 visit_end_struct(v
, NULL
);
2181 error_propagate(errp
, err
);
2185 static void property_release_tm(Object
*obj
, const char *name
,
2188 TMProperty
*prop
= opaque
;
2192 void object_property_add_tm(Object
*obj
, const char *name
,
2193 void (*get
)(Object
*, struct tm
*, Error
**),
2196 Error
*local_err
= NULL
;
2197 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2201 object_property_add(obj
, name
, "struct tm",
2202 get
? property_get_tm
: NULL
, NULL
,
2203 property_release_tm
,
2206 error_propagate(errp
, local_err
);
2211 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2212 void (*get
)(Object
*, struct tm
*, Error
**),
2215 Error
*local_err
= NULL
;
2216 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2220 object_class_property_add(klass
, name
, "struct tm",
2221 get
? property_get_tm
: NULL
, NULL
,
2222 property_release_tm
,
2225 error_propagate(errp
, local_err
);
2230 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2232 return g_strdup(object_get_typename(obj
));
2235 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2236 void *opaque
, Error
**errp
)
2238 uint8_t value
= *(uint8_t *)opaque
;
2239 visit_type_uint8(v
, name
, &value
, errp
);
2242 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2243 void *opaque
, Error
**errp
)
2245 uint16_t value
= *(uint16_t *)opaque
;
2246 visit_type_uint16(v
, name
, &value
, errp
);
2249 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2250 void *opaque
, Error
**errp
)
2252 uint32_t value
= *(uint32_t *)opaque
;
2253 visit_type_uint32(v
, name
, &value
, errp
);
2256 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2257 void *opaque
, Error
**errp
)
2259 uint64_t value
= *(uint64_t *)opaque
;
2260 visit_type_uint64(v
, name
, &value
, errp
);
2263 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2264 const uint8_t *v
, Error
**errp
)
2266 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2267 NULL
, NULL
, (void *)v
, errp
);
2270 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2271 const uint8_t *v
, Error
**errp
)
2273 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2274 NULL
, NULL
, (void *)v
, errp
);
2277 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2278 const uint16_t *v
, Error
**errp
)
2280 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2281 NULL
, NULL
, (void *)v
, errp
);
2284 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2285 const uint16_t *v
, Error
**errp
)
2287 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2288 NULL
, NULL
, (void *)v
, errp
);
2291 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2292 const uint32_t *v
, Error
**errp
)
2294 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2295 NULL
, NULL
, (void *)v
, errp
);
2298 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2299 const uint32_t *v
, Error
**errp
)
2301 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2302 NULL
, NULL
, (void *)v
, errp
);
2305 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2306 const uint64_t *v
, Error
**errp
)
2308 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2309 NULL
, NULL
, (void *)v
, errp
);
2312 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2313 const uint64_t *v
, Error
**errp
)
2315 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2316 NULL
, NULL
, (void *)v
, errp
);
2324 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2325 void *opaque
, Error
**errp
)
2327 AliasProperty
*prop
= opaque
;
2329 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2332 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2333 void *opaque
, Error
**errp
)
2335 AliasProperty
*prop
= opaque
;
2337 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2340 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2343 AliasProperty
*prop
= opaque
;
2345 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2348 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2350 AliasProperty
*prop
= opaque
;
2352 g_free(prop
->target_name
);
2356 void object_property_add_alias(Object
*obj
, const char *name
,
2357 Object
*target_obj
, const char *target_name
,
2360 AliasProperty
*prop
;
2362 ObjectProperty
*target_prop
;
2364 Error
*local_err
= NULL
;
2366 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2371 if (object_property_is_child(target_prop
)) {
2372 prop_type
= g_strdup_printf("link%s",
2373 target_prop
->type
+ strlen("child"));
2375 prop_type
= g_strdup(target_prop
->type
);
2378 prop
= g_malloc(sizeof(*prop
));
2379 prop
->target_obj
= target_obj
;
2380 prop
->target_name
= g_strdup(target_name
);
2382 op
= object_property_add(obj
, name
, prop_type
,
2385 property_release_alias
,
2388 error_propagate(errp
, local_err
);
2392 op
->resolve
= property_resolve_alias
;
2394 object_property_set_description(obj
, op
->name
,
2395 target_prop
->description
,
2402 void object_property_set_description(Object
*obj
, const char *name
,
2403 const char *description
, Error
**errp
)
2407 op
= object_property_find(obj
, name
, errp
);
2412 g_free(op
->description
);
2413 op
->description
= g_strdup(description
);
2416 void object_class_property_set_description(ObjectClass
*klass
,
2418 const char *description
,
2423 op
= g_hash_table_lookup(klass
->properties
, name
);
2425 error_setg(errp
, "Property '.%s' not found", name
);
2429 g_free(op
->description
);
2430 op
->description
= g_strdup(description
);
2433 static void object_class_init(ObjectClass
*klass
, void *data
)
2435 object_class_property_add_str(klass
, "type", qdev_get_type
,
2436 NULL
, &error_abort
);
2439 static void register_types(void)
2441 static TypeInfo interface_info
= {
2442 .name
= TYPE_INTERFACE
,
2443 .class_size
= sizeof(InterfaceClass
),
2447 static TypeInfo object_info
= {
2448 .name
= TYPE_OBJECT
,
2449 .instance_size
= sizeof(Object
),
2450 .class_init
= object_class_init
,
2454 type_interface
= type_register_internal(&interface_info
);
2455 type_register_internal(&object_info
);
2458 type_init(register_types
)