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"
31 #include "qemu/error-report.h"
33 #define MAX_INTERFACES 32
35 typedef struct InterfaceImpl InterfaceImpl
;
36 typedef struct TypeImpl TypeImpl
;
51 void (*class_init
)(ObjectClass
*klass
, void *data
);
52 void (*class_base_init
)(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_data
= info
->class_data
;
119 ti
->instance_init
= info
->instance_init
;
120 ti
->instance_post_init
= info
->instance_post_init
;
121 ti
->instance_finalize
= info
->instance_finalize
;
123 ti
->abstract
= info
->abstract
;
125 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
126 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
128 ti
->num_interfaces
= i
;
133 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
142 TypeImpl
*type_register(const TypeInfo
*info
)
144 assert(info
->parent
);
145 return type_register_internal(info
);
148 TypeImpl
*type_register_static(const TypeInfo
*info
)
150 return type_register(info
);
153 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
157 for (i
= 0; i
< nr_infos
; i
++) {
158 type_register_static(&infos
[i
]);
162 static TypeImpl
*type_get_by_name(const char *name
)
168 return type_table_lookup(name
);
171 static TypeImpl
*type_get_parent(TypeImpl
*type
)
173 if (!type
->parent_type
&& type
->parent
) {
174 type
->parent_type
= type_get_by_name(type
->parent
);
175 g_assert(type
->parent_type
!= NULL
);
178 return type
->parent_type
;
181 static bool type_has_parent(TypeImpl
*type
)
183 return (type
->parent
!= NULL
);
186 static size_t type_class_get_size(TypeImpl
*ti
)
188 if (ti
->class_size
) {
189 return ti
->class_size
;
192 if (type_has_parent(ti
)) {
193 return type_class_get_size(type_get_parent(ti
));
196 return sizeof(ObjectClass
);
199 static size_t type_object_get_size(TypeImpl
*ti
)
201 if (ti
->instance_size
) {
202 return ti
->instance_size
;
205 if (type_has_parent(ti
)) {
206 return type_object_get_size(type_get_parent(ti
));
212 size_t object_type_get_instance_size(const char *typename
)
214 TypeImpl
*type
= type_get_by_name(typename
);
216 g_assert(type
!= NULL
);
217 return type_object_get_size(type
);
220 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
224 /* Check if target_type is a direct ancestor of type */
226 if (type
== target_type
) {
230 type
= type_get_parent(type
);
236 static void type_initialize(TypeImpl
*ti
);
238 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
239 TypeImpl
*parent_type
)
241 InterfaceClass
*new_iface
;
243 TypeImpl
*iface_impl
;
245 info
.parent
= parent_type
->name
;
246 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
247 info
.abstract
= true;
249 iface_impl
= type_new(&info
);
250 iface_impl
->parent_type
= parent_type
;
251 type_initialize(iface_impl
);
252 g_free((char *)info
.name
);
254 new_iface
= (InterfaceClass
*)iface_impl
->class;
255 new_iface
->concrete_class
= ti
->class;
256 new_iface
->interface_type
= interface_type
;
258 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
262 static void object_property_free(gpointer data
)
264 ObjectProperty
*prop
= data
;
268 g_free(prop
->description
);
272 static void type_initialize(TypeImpl
*ti
)
280 ti
->class_size
= type_class_get_size(ti
);
281 ti
->instance_size
= type_object_get_size(ti
);
282 /* Any type with zero instance_size is implicitly abstract.
283 * This means interface types are all abstract.
285 if (ti
->instance_size
== 0) {
288 if (type_is_ancestor(ti
, type_interface
)) {
289 assert(ti
->instance_size
== 0);
290 assert(ti
->abstract
);
291 assert(!ti
->instance_init
);
292 assert(!ti
->instance_post_init
);
293 assert(!ti
->instance_finalize
);
294 assert(!ti
->num_interfaces
);
296 ti
->class = g_malloc0(ti
->class_size
);
298 parent
= type_get_parent(ti
);
300 type_initialize(parent
);
304 g_assert(parent
->class_size
<= ti
->class_size
);
305 memcpy(ti
->class, parent
->class, parent
->class_size
);
306 ti
->class->interfaces
= NULL
;
307 ti
->class->properties
= g_hash_table_new_full(
308 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
310 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
311 InterfaceClass
*iface
= e
->data
;
312 ObjectClass
*klass
= OBJECT_CLASS(iface
);
314 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
317 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
318 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
319 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
320 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
322 if (type_is_ancestor(target_type
, t
)) {
331 type_initialize_interface(ti
, t
, t
);
334 ti
->class->properties
= g_hash_table_new_full(
335 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
338 ti
->class->type
= ti
;
341 if (parent
->class_base_init
) {
342 parent
->class_base_init(ti
->class, ti
->class_data
);
344 parent
= type_get_parent(parent
);
347 if (ti
->class_init
) {
348 ti
->class_init(ti
->class, ti
->class_data
);
352 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
354 if (type_has_parent(ti
)) {
355 object_init_with_type(obj
, type_get_parent(ti
));
358 if (ti
->instance_init
) {
359 ti
->instance_init(obj
);
363 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
365 if (ti
->instance_post_init
) {
366 ti
->instance_post_init(obj
);
369 if (type_has_parent(ti
)) {
370 object_post_init_with_type(obj
, type_get_parent(ti
));
374 void object_apply_global_props(Object
*obj
, const GPtrArray
*props
, Error
**errp
)
382 for (i
= 0; i
< props
->len
; i
++) {
383 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
386 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
390 object_property_parse(obj
, p
->value
, p
->property
, &err
);
392 error_prepend(&err
, "can't apply global %s.%s=%s: ",
393 p
->driver
, p
->property
, p
->value
);
395 * If errp != NULL, propagate error and return.
396 * If errp == NULL, report a warning, but keep going
397 * with the remaining globals.
400 error_propagate(errp
, err
);
403 warn_report_err(err
);
410 * Global property defaults
411 * Slot 0: accelerator's global property defaults
412 * Slot 1: machine's global property defaults
413 * Each is a GPtrArray of of GlobalProperty.
414 * Applied in order, later entries override earlier ones.
416 static GPtrArray
*object_compat_props
[2];
419 * Set machine's global property defaults to @compat_props.
420 * May be called at most once.
422 void object_set_machine_compat_props(GPtrArray
*compat_props
)
424 assert(!object_compat_props
[1]);
425 object_compat_props
[1] = compat_props
;
429 * Set accelerator's global property defaults to @compat_props.
430 * May be called at most once.
432 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
434 assert(!object_compat_props
[0]);
435 object_compat_props
[0] = compat_props
;
438 void object_apply_compat_props(Object
*obj
)
442 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
443 object_apply_global_props(obj
, object_compat_props
[i
],
448 static void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
452 type_initialize(type
);
454 g_assert(type
->instance_size
>= sizeof(Object
));
455 g_assert(type
->abstract
== false);
456 g_assert(size
>= type
->instance_size
);
458 memset(obj
, 0, type
->instance_size
);
459 obj
->class = type
->class;
461 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
462 NULL
, object_property_free
);
463 object_init_with_type(obj
, type
);
464 object_post_init_with_type(obj
, type
);
467 void object_initialize(void *data
, size_t size
, const char *typename
)
469 TypeImpl
*type
= type_get_by_name(typename
);
472 error_report("missing object type '%s'", typename
);
476 object_initialize_with_type(data
, size
, type
);
479 void object_initialize_child(Object
*parentobj
, const char *propname
,
480 void *childobj
, size_t size
, const char *type
,
485 va_start(vargs
, errp
);
486 object_initialize_childv(parentobj
, propname
, childobj
, size
, type
, errp
,
491 void object_initialize_childv(Object
*parentobj
, const char *propname
,
492 void *childobj
, size_t size
, const char *type
,
493 Error
**errp
, va_list vargs
)
495 Error
*local_err
= NULL
;
499 object_initialize(childobj
, size
, type
);
500 obj
= OBJECT(childobj
);
502 object_set_propv(obj
, &local_err
, vargs
);
507 object_property_add_child(parentobj
, propname
, obj
, &local_err
);
512 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
514 user_creatable_complete(uc
, &local_err
);
516 object_unparent(obj
);
522 * Since object_property_add_child added a reference to the child object,
523 * we can drop the reference added by object_initialize(), so the child
524 * property will own the only reference to the object.
530 error_propagate(errp
, local_err
);
535 static inline bool object_property_is_child(ObjectProperty
*prop
)
537 return strstart(prop
->type
, "child<", NULL
);
540 static void object_property_del_all(Object
*obj
)
542 ObjectProperty
*prop
;
549 g_hash_table_iter_init(&iter
, obj
->properties
);
550 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
553 prop
->release(obj
, prop
->name
, prop
->opaque
);
554 prop
->release
= NULL
;
558 g_hash_table_iter_remove(&iter
);
562 g_hash_table_unref(obj
->properties
);
565 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
567 ObjectProperty
*prop
;
571 g_hash_table_iter_init(&iter
, obj
->properties
);
572 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
574 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
576 prop
->release(obj
, prop
->name
, prop
->opaque
);
577 prop
->release
= NULL
;
582 g_hash_table_iter_init(&iter
, obj
->properties
);
583 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
585 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
586 g_hash_table_iter_remove(&iter
);
592 void object_unparent(Object
*obj
)
595 object_property_del_child(obj
->parent
, obj
, NULL
);
599 static void object_deinit(Object
*obj
, TypeImpl
*type
)
601 if (type
->instance_finalize
) {
602 type
->instance_finalize(obj
);
605 if (type_has_parent(type
)) {
606 object_deinit(obj
, type_get_parent(type
));
610 static void object_finalize(void *data
)
613 TypeImpl
*ti
= obj
->class->type
;
615 object_property_del_all(obj
);
616 object_deinit(obj
, ti
);
618 g_assert(obj
->ref
== 0);
624 static Object
*object_new_with_type(Type type
)
628 g_assert(type
!= NULL
);
629 type_initialize(type
);
631 obj
= g_malloc(type
->instance_size
);
632 object_initialize_with_type(obj
, type
->instance_size
, type
);
638 Object
*object_new(const char *typename
)
640 TypeImpl
*ti
= type_get_by_name(typename
);
642 return object_new_with_type(ti
);
646 Object
*object_new_with_props(const char *typename
,
655 va_start(vargs
, errp
);
656 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
663 Object
*object_new_with_propv(const char *typename
,
671 Error
*local_err
= NULL
;
674 klass
= object_class_by_name(typename
);
676 error_setg(errp
, "invalid object type: %s", typename
);
680 if (object_class_is_abstract(klass
)) {
681 error_setg(errp
, "object type '%s' is abstract", typename
);
684 obj
= object_new_with_type(klass
->type
);
686 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
691 object_property_add_child(parent
, id
, obj
, &local_err
);
697 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
699 user_creatable_complete(uc
, &local_err
);
702 object_unparent(obj
);
708 object_unref(OBJECT(obj
));
712 error_propagate(errp
, local_err
);
718 int object_set_props(Object
*obj
,
725 va_start(vargs
, errp
);
726 ret
= object_set_propv(obj
, errp
, vargs
);
733 int object_set_propv(Object
*obj
,
737 const char *propname
;
738 Error
*local_err
= NULL
;
740 propname
= va_arg(vargs
, char *);
741 while (propname
!= NULL
) {
742 const char *value
= va_arg(vargs
, char *);
744 g_assert(value
!= NULL
);
745 object_property_parse(obj
, value
, propname
, &local_err
);
747 error_propagate(errp
, local_err
);
750 propname
= va_arg(vargs
, char *);
757 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
759 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
766 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
767 const char *file
, int line
, const char *func
)
769 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
770 typename
, file
, line
, func
);
772 #ifdef CONFIG_QOM_CAST_DEBUG
776 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
777 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
782 inst
= object_dynamic_cast(obj
, typename
);
785 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
786 file
, line
, func
, obj
, typename
);
792 if (obj
&& obj
== inst
) {
793 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
794 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
795 atomic_read(&obj
->class->object_cast_cache
[i
]));
797 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
805 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
806 const char *typename
)
808 ObjectClass
*ret
= NULL
;
809 TypeImpl
*target_type
;
816 /* A simple fast path that can trigger a lot for leaf classes. */
818 if (type
->name
== typename
) {
822 target_type
= type_get_by_name(typename
);
824 /* target class type unknown, so fail the cast */
828 if (type
->class->interfaces
&&
829 type_is_ancestor(target_type
, type_interface
)) {
833 for (i
= class->interfaces
; i
; i
= i
->next
) {
834 ObjectClass
*target_class
= i
->data
;
836 if (type_is_ancestor(target_class
->type
, target_type
)) {
842 /* The match was ambiguous, don't allow a cast */
846 } else if (type_is_ancestor(type
, target_type
)) {
853 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
854 const char *typename
,
855 const char *file
, int line
,
860 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
861 typename
, file
, line
, func
);
863 #ifdef CONFIG_QOM_CAST_DEBUG
866 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
867 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
873 if (!class || !class->interfaces
) {
878 ret
= object_class_dynamic_cast(class, typename
);
880 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
881 file
, line
, func
, class, typename
);
885 #ifdef CONFIG_QOM_CAST_DEBUG
886 if (class && ret
== class) {
887 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
888 atomic_set(&class->class_cast_cache
[i
- 1],
889 atomic_read(&class->class_cast_cache
[i
]));
891 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
898 const char *object_get_typename(const Object
*obj
)
900 return obj
->class->type
->name
;
903 ObjectClass
*object_get_class(Object
*obj
)
908 bool object_class_is_abstract(ObjectClass
*klass
)
910 return klass
->type
->abstract
;
913 const char *object_class_get_name(ObjectClass
*klass
)
915 return klass
->type
->name
;
918 ObjectClass
*object_class_by_name(const char *typename
)
920 TypeImpl
*type
= type_get_by_name(typename
);
926 type_initialize(type
);
931 ObjectClass
*object_class_get_parent(ObjectClass
*class)
933 TypeImpl
*type
= type_get_parent(class->type
);
939 type_initialize(type
);
944 typedef struct OCFData
946 void (*fn
)(ObjectClass
*klass
, void *opaque
);
947 const char *implements_type
;
948 bool include_abstract
;
952 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
955 OCFData
*data
= opaque
;
956 TypeImpl
*type
= value
;
959 type_initialize(type
);
962 if (!data
->include_abstract
&& type
->abstract
) {
966 if (data
->implements_type
&&
967 !object_class_dynamic_cast(k
, data
->implements_type
)) {
971 data
->fn(k
, data
->opaque
);
974 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
975 const char *implements_type
, bool include_abstract
,
978 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
980 enumerating_types
= true;
981 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
982 enumerating_types
= false;
985 static int do_object_child_foreach(Object
*obj
,
986 int (*fn
)(Object
*child
, void *opaque
),
987 void *opaque
, bool recurse
)
990 ObjectProperty
*prop
;
993 g_hash_table_iter_init(&iter
, obj
->properties
);
994 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
995 if (object_property_is_child(prop
)) {
996 Object
*child
= prop
->opaque
;
998 ret
= fn(child
, opaque
);
1003 do_object_child_foreach(child
, fn
, opaque
, true);
1010 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1013 return do_object_child_foreach(obj
, fn
, opaque
, false);
1016 int object_child_foreach_recursive(Object
*obj
,
1017 int (*fn
)(Object
*child
, void *opaque
),
1020 return do_object_child_foreach(obj
, fn
, opaque
, true);
1023 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1025 GSList
**list
= opaque
;
1027 *list
= g_slist_prepend(*list
, klass
);
1030 GSList
*object_class_get_list(const char *implements_type
,
1031 bool include_abstract
)
1033 GSList
*list
= NULL
;
1035 object_class_foreach(object_class_get_list_tramp
,
1036 implements_type
, include_abstract
, &list
);
1040 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1042 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1043 object_class_get_name((ObjectClass
*)b
));
1046 GSList
*object_class_get_list_sorted(const char *implements_type
,
1047 bool include_abstract
)
1049 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1053 void object_ref(Object
*obj
)
1058 atomic_inc(&obj
->ref
);
1061 void object_unref(Object
*obj
)
1066 g_assert(obj
->ref
> 0);
1068 /* parent always holds a reference to its children */
1069 if (atomic_fetch_dec(&obj
->ref
) == 1) {
1070 object_finalize(obj
);
1075 object_property_add(Object
*obj
, const char *name
, const char *type
,
1076 ObjectPropertyAccessor
*get
,
1077 ObjectPropertyAccessor
*set
,
1078 ObjectPropertyRelease
*release
,
1079 void *opaque
, Error
**errp
)
1081 ObjectProperty
*prop
;
1082 size_t name_len
= strlen(name
);
1084 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1086 ObjectProperty
*ret
;
1087 char *name_no_array
= g_strdup(name
);
1089 name_no_array
[name_len
- 3] = '\0';
1090 for (i
= 0; ; ++i
) {
1091 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1093 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
1094 release
, opaque
, NULL
);
1100 g_free(name_no_array
);
1104 if (object_property_find(obj
, name
, NULL
) != NULL
) {
1105 error_setg(errp
, "attempt to add duplicate property '%s'"
1106 " to object (type '%s')", name
,
1107 object_get_typename(obj
));
1111 prop
= g_malloc0(sizeof(*prop
));
1113 prop
->name
= g_strdup(name
);
1114 prop
->type
= g_strdup(type
);
1118 prop
->release
= release
;
1119 prop
->opaque
= opaque
;
1121 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1126 object_class_property_add(ObjectClass
*klass
,
1129 ObjectPropertyAccessor
*get
,
1130 ObjectPropertyAccessor
*set
,
1131 ObjectPropertyRelease
*release
,
1135 ObjectProperty
*prop
;
1137 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
1138 error_setg(errp
, "attempt to add duplicate property '%s'"
1139 " to object (type '%s')", name
,
1140 object_class_get_name(klass
));
1144 prop
= g_malloc0(sizeof(*prop
));
1146 prop
->name
= g_strdup(name
);
1147 prop
->type
= g_strdup(type
);
1151 prop
->release
= release
;
1152 prop
->opaque
= opaque
;
1154 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
1159 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1162 ObjectProperty
*prop
;
1163 ObjectClass
*klass
= object_get_class(obj
);
1165 prop
= object_class_property_find(klass
, name
, NULL
);
1170 prop
= g_hash_table_lookup(obj
->properties
, name
);
1175 error_setg(errp
, "Property '.%s' not found", name
);
1179 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1182 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1183 iter
->nextclass
= object_get_class(obj
);
1186 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1189 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1190 if (!iter
->nextclass
) {
1193 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1194 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1199 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1202 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1203 iter
->nextclass
= object_class_get_parent(klass
);
1206 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1209 ObjectProperty
*prop
;
1210 ObjectClass
*parent_klass
;
1212 parent_klass
= object_class_get_parent(klass
);
1214 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1220 prop
= g_hash_table_lookup(klass
->properties
, name
);
1222 error_setg(errp
, "Property '.%s' not found", name
);
1227 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1229 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1232 error_setg(errp
, "Property '.%s' not found", name
);
1236 if (prop
->release
) {
1237 prop
->release(obj
, name
, prop
->opaque
);
1239 g_hash_table_remove(obj
->properties
, name
);
1242 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1245 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1251 error_setg(errp
, QERR_PERMISSION_DENIED
);
1253 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1257 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1260 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1266 error_setg(errp
, QERR_PERMISSION_DENIED
);
1268 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1272 void object_property_set_str(Object
*obj
, const char *value
,
1273 const char *name
, Error
**errp
)
1275 QString
*qstr
= qstring_from_str(value
);
1276 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1278 qobject_unref(qstr
);
1281 char *object_property_get_str(Object
*obj
, const char *name
,
1284 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1291 retval
= g_strdup(qobject_get_try_str(ret
));
1293 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1300 void object_property_set_link(Object
*obj
, Object
*value
,
1301 const char *name
, Error
**errp
)
1304 gchar
*path
= object_get_canonical_path(value
);
1305 object_property_set_str(obj
, path
, name
, errp
);
1308 object_property_set_str(obj
, "", name
, errp
);
1312 Object
*object_property_get_link(Object
*obj
, const char *name
,
1315 char *str
= object_property_get_str(obj
, name
, errp
);
1316 Object
*target
= NULL
;
1319 target
= object_resolve_path(str
, NULL
);
1321 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1322 "Device '%s' not found", str
);
1330 void object_property_set_bool(Object
*obj
, bool value
,
1331 const char *name
, Error
**errp
)
1333 QBool
*qbool
= qbool_from_bool(value
);
1334 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1336 qobject_unref(qbool
);
1339 bool object_property_get_bool(Object
*obj
, const char *name
,
1342 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1349 qbool
= qobject_to(QBool
, ret
);
1351 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1354 retval
= qbool_get_bool(qbool
);
1361 void object_property_set_int(Object
*obj
, int64_t value
,
1362 const char *name
, Error
**errp
)
1364 QNum
*qnum
= qnum_from_int(value
);
1365 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1367 qobject_unref(qnum
);
1370 int64_t object_property_get_int(Object
*obj
, const char *name
,
1373 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1381 qnum
= qobject_to(QNum
, ret
);
1382 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1383 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1391 void object_property_set_uint(Object
*obj
, uint64_t value
,
1392 const char *name
, Error
**errp
)
1394 QNum
*qnum
= qnum_from_uint(value
);
1396 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1397 qobject_unref(qnum
);
1400 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1403 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1410 qnum
= qobject_to(QNum
, ret
);
1411 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1412 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1420 typedef struct EnumProperty
{
1421 const QEnumLookup
*lookup
;
1422 int (*get
)(Object
*, Error
**);
1423 void (*set
)(Object
*, int, Error
**);
1426 int object_property_get_enum(Object
*obj
, const char *name
,
1427 const char *typename
, Error
**errp
)
1433 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1434 EnumProperty
*enumprop
;
1440 if (!g_str_equal(prop
->type
, typename
)) {
1441 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1442 name
, object_class_get_name(
1443 object_get_class(obj
)), typename
);
1447 enumprop
= prop
->opaque
;
1449 v
= string_output_visitor_new(false, &str
);
1450 object_property_get(obj
, v
, name
, &err
);
1452 error_propagate(errp
, err
);
1456 visit_complete(v
, &str
);
1458 v
= string_input_visitor_new(str
);
1459 visit_type_enum(v
, name
, &ret
, enumprop
->lookup
, errp
);
1467 void object_property_get_uint16List(Object
*obj
, const char *name
,
1468 uint16List
**list
, Error
**errp
)
1474 v
= string_output_visitor_new(false, &str
);
1475 object_property_get(obj
, v
, name
, &err
);
1477 error_propagate(errp
, err
);
1480 visit_complete(v
, &str
);
1482 v
= string_input_visitor_new(str
);
1483 visit_type_uint16List(v
, NULL
, list
, errp
);
1490 void object_property_parse(Object
*obj
, const char *string
,
1491 const char *name
, Error
**errp
)
1493 Visitor
*v
= string_input_visitor_new(string
);
1494 object_property_set(obj
, v
, name
, errp
);
1498 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1502 char *string
= NULL
;
1503 Error
*local_err
= NULL
;
1505 v
= string_output_visitor_new(human
, &string
);
1506 object_property_get(obj
, v
, name
, &local_err
);
1508 error_propagate(errp
, local_err
);
1512 visit_complete(v
, &string
);
1519 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1521 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1529 Object
*object_get_root(void)
1531 static Object
*root
;
1534 root
= object_new("container");
1540 Object
*object_get_objects_root(void)
1542 return container_get(object_get_root(), "/objects");
1545 Object
*object_get_internal_root(void)
1547 static Object
*internal_root
;
1549 if (!internal_root
) {
1550 internal_root
= object_new("container");
1553 return internal_root
;
1556 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1557 const char *name
, void *opaque
,
1560 Object
*child
= opaque
;
1563 path
= object_get_canonical_path(child
);
1564 visit_type_str(v
, name
, &path
, errp
);
1568 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1573 static void object_finalize_child_property(Object
*obj
, const char *name
,
1576 Object
*child
= opaque
;
1578 if (child
->class->unparent
) {
1579 (child
->class->unparent
)(child
);
1581 child
->parent
= NULL
;
1582 object_unref(child
);
1585 void object_property_add_child(Object
*obj
, const char *name
,
1586 Object
*child
, Error
**errp
)
1588 Error
*local_err
= NULL
;
1592 if (child
->parent
!= NULL
) {
1593 error_setg(errp
, "child object is already parented");
1597 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1599 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1600 object_finalize_child_property
, child
, &local_err
);
1602 error_propagate(errp
, local_err
);
1606 op
->resolve
= object_resolve_child_property
;
1608 child
->parent
= obj
;
1614 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1615 Object
*val
, Error
**errp
)
1617 /* Allow the link to be set, always */
1622 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1623 ObjectPropertyLinkFlags flags
;
1626 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1627 const char *name
, void *opaque
,
1630 LinkProperty
*lprop
= opaque
;
1631 Object
**child
= lprop
->child
;
1635 path
= object_get_canonical_path(*child
);
1636 visit_type_str(v
, name
, &path
, errp
);
1640 visit_type_str(v
, name
, &path
, errp
);
1645 * object_resolve_link:
1647 * Lookup an object and ensure its type matches the link property type. This
1648 * is similar to object_resolve_path() except type verification against the
1649 * link property is performed.
1651 * Returns: The matched object or NULL on path lookup failures.
1653 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1654 const char *path
, Error
**errp
)
1658 bool ambiguous
= false;
1661 /* Go from link<FOO> to FOO. */
1662 type
= object_property_get_type(obj
, name
, NULL
);
1663 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1664 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1667 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1669 } else if (!target
) {
1670 target
= object_resolve_path(path
, &ambiguous
);
1671 if (target
|| ambiguous
) {
1672 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1674 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1675 "Device '%s' not found", path
);
1679 g_free(target_type
);
1684 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1685 const char *name
, void *opaque
,
1688 Error
*local_err
= NULL
;
1689 LinkProperty
*prop
= opaque
;
1690 Object
**child
= prop
->child
;
1691 Object
*old_target
= *child
;
1692 Object
*new_target
= NULL
;
1695 visit_type_str(v
, name
, &path
, &local_err
);
1697 if (!local_err
&& strcmp(path
, "") != 0) {
1698 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1703 error_propagate(errp
, local_err
);
1707 prop
->check(obj
, name
, new_target
, &local_err
);
1709 error_propagate(errp
, local_err
);
1713 *child
= new_target
;
1714 if (prop
->flags
== OBJ_PROP_LINK_STRONG
) {
1715 object_ref(new_target
);
1716 object_unref(old_target
);
1720 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1722 LinkProperty
*lprop
= opaque
;
1724 return *lprop
->child
;
1727 static void object_release_link_property(Object
*obj
, const char *name
,
1730 LinkProperty
*prop
= opaque
;
1732 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *prop
->child
) {
1733 object_unref(*prop
->child
);
1738 void object_property_add_link(Object
*obj
, const char *name
,
1739 const char *type
, Object
**child
,
1740 void (*check
)(const Object
*, const char *,
1741 Object
*, Error
**),
1742 ObjectPropertyLinkFlags flags
,
1745 Error
*local_err
= NULL
;
1746 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1750 prop
->child
= child
;
1751 prop
->check
= check
;
1752 prop
->flags
= flags
;
1754 full_type
= g_strdup_printf("link<%s>", type
);
1756 op
= object_property_add(obj
, name
, full_type
,
1757 object_get_link_property
,
1758 check
? object_set_link_property
: NULL
,
1759 object_release_link_property
,
1763 error_propagate(errp
, local_err
);
1768 op
->resolve
= object_resolve_link_property
;
1774 void object_property_add_const_link(Object
*obj
, const char *name
,
1775 Object
*target
, Error
**errp
)
1780 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1781 op
= object_property_add(obj
, name
, link_type
,
1782 object_get_child_property
, NULL
,
1783 NULL
, target
, errp
);
1785 op
->resolve
= object_resolve_child_property
;
1790 gchar
*object_get_canonical_path_component(Object
*obj
)
1792 ObjectProperty
*prop
= NULL
;
1793 GHashTableIter iter
;
1795 if (obj
->parent
== NULL
) {
1799 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1800 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1801 if (!object_property_is_child(prop
)) {
1805 if (prop
->opaque
== obj
) {
1806 return g_strdup(prop
->name
);
1810 /* obj had a parent but was not a child, should never happen */
1811 g_assert_not_reached();
1815 gchar
*object_get_canonical_path(Object
*obj
)
1817 Object
*root
= object_get_root();
1818 char *newpath
, *path
= NULL
;
1821 return g_strdup("/");
1825 char *component
= object_get_canonical_path_component(obj
);
1828 /* A canonical path must be complete, so discard what was
1835 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
1840 } while (obj
!= root
);
1845 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1847 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1852 if (prop
->resolve
) {
1853 return prop
->resolve(parent
, prop
->opaque
, part
);
1859 static Object
*object_resolve_abs_path(Object
*parent
,
1861 const char *typename
,
1866 if (parts
[index
] == NULL
) {
1867 return object_dynamic_cast(parent
, typename
);
1870 if (strcmp(parts
[index
], "") == 0) {
1871 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1874 child
= object_resolve_path_component(parent
, parts
[index
]);
1879 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1882 static Object
*object_resolve_partial_path(Object
*parent
,
1884 const char *typename
,
1888 GHashTableIter iter
;
1889 ObjectProperty
*prop
;
1891 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1893 g_hash_table_iter_init(&iter
, parent
->properties
);
1894 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1897 if (!object_property_is_child(prop
)) {
1901 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1902 typename
, ambiguous
);
1919 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1925 parts
= g_strsplit(path
, "/", 0);
1928 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1929 bool ambiguous
= false;
1930 obj
= object_resolve_partial_path(object_get_root(), parts
,
1931 typename
, &ambiguous
);
1933 *ambiguousp
= ambiguous
;
1936 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1944 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1946 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1949 typedef struct StringProperty
1951 char *(*get
)(Object
*, Error
**);
1952 void (*set
)(Object
*, const char *, Error
**);
1955 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1956 void *opaque
, Error
**errp
)
1958 StringProperty
*prop
= opaque
;
1962 value
= prop
->get(obj
, &err
);
1964 error_propagate(errp
, err
);
1968 visit_type_str(v
, name
, &value
, errp
);
1972 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1973 void *opaque
, Error
**errp
)
1975 StringProperty
*prop
= opaque
;
1977 Error
*local_err
= NULL
;
1979 visit_type_str(v
, name
, &value
, &local_err
);
1981 error_propagate(errp
, local_err
);
1985 prop
->set(obj
, value
, errp
);
1989 static void property_release_str(Object
*obj
, const char *name
,
1992 StringProperty
*prop
= opaque
;
1996 void object_property_add_str(Object
*obj
, const char *name
,
1997 char *(*get
)(Object
*, Error
**),
1998 void (*set
)(Object
*, const char *, Error
**),
2001 Error
*local_err
= NULL
;
2002 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2007 object_property_add(obj
, name
, "string",
2008 get
? property_get_str
: NULL
,
2009 set
? property_set_str
: NULL
,
2010 property_release_str
,
2013 error_propagate(errp
, local_err
);
2018 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2019 char *(*get
)(Object
*, Error
**),
2020 void (*set
)(Object
*, const char *,
2024 Error
*local_err
= NULL
;
2025 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2030 object_class_property_add(klass
, name
, "string",
2031 get
? property_get_str
: NULL
,
2032 set
? property_set_str
: NULL
,
2033 property_release_str
,
2036 error_propagate(errp
, local_err
);
2041 typedef struct BoolProperty
2043 bool (*get
)(Object
*, Error
**);
2044 void (*set
)(Object
*, bool, Error
**);
2047 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2048 void *opaque
, Error
**errp
)
2050 BoolProperty
*prop
= opaque
;
2054 value
= prop
->get(obj
, &err
);
2056 error_propagate(errp
, err
);
2060 visit_type_bool(v
, name
, &value
, errp
);
2063 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2064 void *opaque
, Error
**errp
)
2066 BoolProperty
*prop
= opaque
;
2068 Error
*local_err
= NULL
;
2070 visit_type_bool(v
, name
, &value
, &local_err
);
2072 error_propagate(errp
, local_err
);
2076 prop
->set(obj
, value
, errp
);
2079 static void property_release_bool(Object
*obj
, const char *name
,
2082 BoolProperty
*prop
= opaque
;
2086 void object_property_add_bool(Object
*obj
, const char *name
,
2087 bool (*get
)(Object
*, Error
**),
2088 void (*set
)(Object
*, bool, Error
**),
2091 Error
*local_err
= NULL
;
2092 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2097 object_property_add(obj
, name
, "bool",
2098 get
? property_get_bool
: NULL
,
2099 set
? property_set_bool
: NULL
,
2100 property_release_bool
,
2103 error_propagate(errp
, local_err
);
2108 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2109 bool (*get
)(Object
*, Error
**),
2110 void (*set
)(Object
*, bool, Error
**),
2113 Error
*local_err
= NULL
;
2114 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2119 object_class_property_add(klass
, name
, "bool",
2120 get
? property_get_bool
: NULL
,
2121 set
? property_set_bool
: NULL
,
2122 property_release_bool
,
2125 error_propagate(errp
, local_err
);
2130 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2131 void *opaque
, Error
**errp
)
2133 EnumProperty
*prop
= opaque
;
2137 value
= prop
->get(obj
, &err
);
2139 error_propagate(errp
, err
);
2143 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2146 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2147 void *opaque
, Error
**errp
)
2149 EnumProperty
*prop
= opaque
;
2153 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
2155 error_propagate(errp
, err
);
2158 prop
->set(obj
, value
, errp
);
2161 static void property_release_enum(Object
*obj
, const char *name
,
2164 EnumProperty
*prop
= opaque
;
2168 void object_property_add_enum(Object
*obj
, const char *name
,
2169 const char *typename
,
2170 const QEnumLookup
*lookup
,
2171 int (*get
)(Object
*, Error
**),
2172 void (*set
)(Object
*, int, Error
**),
2175 Error
*local_err
= NULL
;
2176 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2178 prop
->lookup
= lookup
;
2182 object_property_add(obj
, name
, typename
,
2183 get
? property_get_enum
: NULL
,
2184 set
? property_set_enum
: NULL
,
2185 property_release_enum
,
2188 error_propagate(errp
, local_err
);
2193 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2194 const char *typename
,
2195 const QEnumLookup
*lookup
,
2196 int (*get
)(Object
*, Error
**),
2197 void (*set
)(Object
*, int, Error
**),
2200 Error
*local_err
= NULL
;
2201 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2203 prop
->lookup
= lookup
;
2207 object_class_property_add(klass
, name
, typename
,
2208 get
? property_get_enum
: NULL
,
2209 set
? property_set_enum
: NULL
,
2210 property_release_enum
,
2213 error_propagate(errp
, local_err
);
2218 typedef struct TMProperty
{
2219 void (*get
)(Object
*, struct tm
*, Error
**);
2222 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2223 void *opaque
, Error
**errp
)
2225 TMProperty
*prop
= opaque
;
2229 prop
->get(obj
, &value
, &err
);
2234 visit_start_struct(v
, name
, NULL
, 0, &err
);
2238 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2242 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2246 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2250 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2254 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2258 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2262 visit_check_struct(v
, &err
);
2264 visit_end_struct(v
, NULL
);
2266 error_propagate(errp
, err
);
2270 static void property_release_tm(Object
*obj
, const char *name
,
2273 TMProperty
*prop
= opaque
;
2277 void object_property_add_tm(Object
*obj
, const char *name
,
2278 void (*get
)(Object
*, struct tm
*, Error
**),
2281 Error
*local_err
= NULL
;
2282 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2286 object_property_add(obj
, name
, "struct tm",
2287 get
? property_get_tm
: NULL
, NULL
,
2288 property_release_tm
,
2291 error_propagate(errp
, local_err
);
2296 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2297 void (*get
)(Object
*, struct tm
*, Error
**),
2300 Error
*local_err
= NULL
;
2301 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2305 object_class_property_add(klass
, name
, "struct tm",
2306 get
? property_get_tm
: NULL
, NULL
,
2307 property_release_tm
,
2310 error_propagate(errp
, local_err
);
2315 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2317 return g_strdup(object_get_typename(obj
));
2320 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2321 void *opaque
, Error
**errp
)
2323 uint8_t value
= *(uint8_t *)opaque
;
2324 visit_type_uint8(v
, name
, &value
, errp
);
2327 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2328 void *opaque
, Error
**errp
)
2330 uint16_t value
= *(uint16_t *)opaque
;
2331 visit_type_uint16(v
, name
, &value
, errp
);
2334 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2335 void *opaque
, Error
**errp
)
2337 uint32_t value
= *(uint32_t *)opaque
;
2338 visit_type_uint32(v
, name
, &value
, errp
);
2341 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2342 void *opaque
, Error
**errp
)
2344 uint64_t value
= *(uint64_t *)opaque
;
2345 visit_type_uint64(v
, name
, &value
, errp
);
2348 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2349 const uint8_t *v
, Error
**errp
)
2351 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2352 NULL
, NULL
, (void *)v
, errp
);
2355 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2356 const uint8_t *v
, Error
**errp
)
2358 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2359 NULL
, NULL
, (void *)v
, errp
);
2362 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2363 const uint16_t *v
, Error
**errp
)
2365 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2366 NULL
, NULL
, (void *)v
, errp
);
2369 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2370 const uint16_t *v
, Error
**errp
)
2372 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2373 NULL
, NULL
, (void *)v
, errp
);
2376 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2377 const uint32_t *v
, Error
**errp
)
2379 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2380 NULL
, NULL
, (void *)v
, errp
);
2383 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2384 const uint32_t *v
, Error
**errp
)
2386 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2387 NULL
, NULL
, (void *)v
, errp
);
2390 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2391 const uint64_t *v
, Error
**errp
)
2393 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2394 NULL
, NULL
, (void *)v
, errp
);
2397 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2398 const uint64_t *v
, Error
**errp
)
2400 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2401 NULL
, NULL
, (void *)v
, errp
);
2409 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2410 void *opaque
, Error
**errp
)
2412 AliasProperty
*prop
= opaque
;
2414 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2417 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2418 void *opaque
, Error
**errp
)
2420 AliasProperty
*prop
= opaque
;
2422 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2425 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2428 AliasProperty
*prop
= opaque
;
2430 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2433 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2435 AliasProperty
*prop
= opaque
;
2437 g_free(prop
->target_name
);
2441 void object_property_add_alias(Object
*obj
, const char *name
,
2442 Object
*target_obj
, const char *target_name
,
2445 AliasProperty
*prop
;
2447 ObjectProperty
*target_prop
;
2449 Error
*local_err
= NULL
;
2451 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2456 if (object_property_is_child(target_prop
)) {
2457 prop_type
= g_strdup_printf("link%s",
2458 target_prop
->type
+ strlen("child"));
2460 prop_type
= g_strdup(target_prop
->type
);
2463 prop
= g_malloc(sizeof(*prop
));
2464 prop
->target_obj
= target_obj
;
2465 prop
->target_name
= g_strdup(target_name
);
2467 op
= object_property_add(obj
, name
, prop_type
,
2470 property_release_alias
,
2473 error_propagate(errp
, local_err
);
2477 op
->resolve
= property_resolve_alias
;
2479 object_property_set_description(obj
, op
->name
,
2480 target_prop
->description
,
2487 void object_property_set_description(Object
*obj
, const char *name
,
2488 const char *description
, Error
**errp
)
2492 op
= object_property_find(obj
, name
, errp
);
2497 g_free(op
->description
);
2498 op
->description
= g_strdup(description
);
2501 void object_class_property_set_description(ObjectClass
*klass
,
2503 const char *description
,
2508 op
= g_hash_table_lookup(klass
->properties
, name
);
2510 error_setg(errp
, "Property '.%s' not found", name
);
2514 g_free(op
->description
);
2515 op
->description
= g_strdup(description
);
2518 static void object_class_init(ObjectClass
*klass
, void *data
)
2520 object_class_property_add_str(klass
, "type", qdev_get_type
,
2521 NULL
, &error_abort
);
2524 static void register_types(void)
2526 static TypeInfo interface_info
= {
2527 .name
= TYPE_INTERFACE
,
2528 .class_size
= sizeof(InterfaceClass
),
2532 static TypeInfo object_info
= {
2533 .name
= TYPE_OBJECT
,
2534 .instance_size
= sizeof(Object
),
2535 .class_init
= object_class_init
,
2539 type_interface
= type_register_internal(&interface_info
);
2540 type_register_internal(&object_info
);
2543 type_init(register_types
)