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
);
55 void (*instance_init
)(Object
*obj
);
56 void (*instance_post_init
)(Object
*obj
);
57 void (*instance_finalize
)(Object
*obj
);
62 TypeImpl
*parent_type
;
67 InterfaceImpl interfaces
[MAX_INTERFACES
];
70 static Type type_interface
;
72 static GHashTable
*type_table_get(void)
74 static GHashTable
*type_table
;
76 if (type_table
== NULL
) {
77 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
83 static bool enumerating_types
;
85 static void type_table_add(TypeImpl
*ti
)
87 assert(!enumerating_types
);
88 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
91 static TypeImpl
*type_table_lookup(const char *name
)
93 return g_hash_table_lookup(type_table_get(), name
);
96 static TypeImpl
*type_new(const TypeInfo
*info
)
98 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
101 g_assert(info
->name
!= NULL
);
103 if (type_table_lookup(info
->name
) != NULL
) {
104 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
108 ti
->name
= g_strdup(info
->name
);
109 ti
->parent
= g_strdup(info
->parent
);
111 ti
->class_size
= info
->class_size
;
112 ti
->instance_size
= info
->instance_size
;
114 ti
->class_init
= info
->class_init
;
115 ti
->class_base_init
= info
->class_base_init
;
116 ti
->class_data
= info
->class_data
;
118 ti
->instance_init
= info
->instance_init
;
119 ti
->instance_post_init
= info
->instance_post_init
;
120 ti
->instance_finalize
= info
->instance_finalize
;
122 ti
->abstract
= info
->abstract
;
124 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
125 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
127 ti
->num_interfaces
= i
;
132 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
141 TypeImpl
*type_register(const TypeInfo
*info
)
143 assert(info
->parent
);
144 return type_register_internal(info
);
147 TypeImpl
*type_register_static(const TypeInfo
*info
)
149 return type_register(info
);
152 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
156 for (i
= 0; i
< nr_infos
; i
++) {
157 type_register_static(&infos
[i
]);
161 static TypeImpl
*type_get_by_name(const char *name
)
167 return type_table_lookup(name
);
170 static TypeImpl
*type_get_parent(TypeImpl
*type
)
172 if (!type
->parent_type
&& type
->parent
) {
173 type
->parent_type
= type_get_by_name(type
->parent
);
174 g_assert(type
->parent_type
!= NULL
);
177 return type
->parent_type
;
180 static bool type_has_parent(TypeImpl
*type
)
182 return (type
->parent
!= NULL
);
185 static size_t type_class_get_size(TypeImpl
*ti
)
187 if (ti
->class_size
) {
188 return ti
->class_size
;
191 if (type_has_parent(ti
)) {
192 return type_class_get_size(type_get_parent(ti
));
195 return sizeof(ObjectClass
);
198 static size_t type_object_get_size(TypeImpl
*ti
)
200 if (ti
->instance_size
) {
201 return ti
->instance_size
;
204 if (type_has_parent(ti
)) {
205 return type_object_get_size(type_get_parent(ti
));
211 size_t object_type_get_instance_size(const char *typename
)
213 TypeImpl
*type
= type_get_by_name(typename
);
215 g_assert(type
!= NULL
);
216 return type_object_get_size(type
);
219 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
223 /* Check if target_type is a direct ancestor of type */
225 if (type
== target_type
) {
229 type
= type_get_parent(type
);
235 static void type_initialize(TypeImpl
*ti
);
237 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
238 TypeImpl
*parent_type
)
240 InterfaceClass
*new_iface
;
242 TypeImpl
*iface_impl
;
244 info
.parent
= parent_type
->name
;
245 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
246 info
.abstract
= true;
248 iface_impl
= type_new(&info
);
249 iface_impl
->parent_type
= parent_type
;
250 type_initialize(iface_impl
);
251 g_free((char *)info
.name
);
253 new_iface
= (InterfaceClass
*)iface_impl
->class;
254 new_iface
->concrete_class
= ti
->class;
255 new_iface
->interface_type
= interface_type
;
257 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
261 static void object_property_free(gpointer data
)
263 ObjectProperty
*prop
= data
;
267 g_free(prop
->description
);
271 static void type_initialize(TypeImpl
*ti
)
279 ti
->class_size
= type_class_get_size(ti
);
280 ti
->instance_size
= type_object_get_size(ti
);
281 /* Any type with zero instance_size is implicitly abstract.
282 * This means interface types are all abstract.
284 if (ti
->instance_size
== 0) {
287 if (type_is_ancestor(ti
, type_interface
)) {
288 assert(ti
->instance_size
== 0);
289 assert(ti
->abstract
);
290 assert(!ti
->instance_init
);
291 assert(!ti
->instance_post_init
);
292 assert(!ti
->instance_finalize
);
293 assert(!ti
->num_interfaces
);
295 ti
->class = g_malloc0(ti
->class_size
);
297 parent
= type_get_parent(ti
);
299 type_initialize(parent
);
303 g_assert(parent
->class_size
<= ti
->class_size
);
304 memcpy(ti
->class, parent
->class, parent
->class_size
);
305 ti
->class->interfaces
= NULL
;
306 ti
->class->properties
= g_hash_table_new_full(
307 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
309 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
310 InterfaceClass
*iface
= e
->data
;
311 ObjectClass
*klass
= OBJECT_CLASS(iface
);
313 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
316 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
317 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
318 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
319 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
321 if (type_is_ancestor(target_type
, t
)) {
330 type_initialize_interface(ti
, t
, t
);
333 ti
->class->properties
= g_hash_table_new_full(
334 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
337 ti
->class->type
= ti
;
340 if (parent
->class_base_init
) {
341 parent
->class_base_init(ti
->class, ti
->class_data
);
343 parent
= type_get_parent(parent
);
346 if (ti
->class_init
) {
347 ti
->class_init(ti
->class, ti
->class_data
);
351 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
353 if (type_has_parent(ti
)) {
354 object_init_with_type(obj
, type_get_parent(ti
));
357 if (ti
->instance_init
) {
358 ti
->instance_init(obj
);
362 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
364 if (ti
->instance_post_init
) {
365 ti
->instance_post_init(obj
);
368 if (type_has_parent(ti
)) {
369 object_post_init_with_type(obj
, type_get_parent(ti
));
373 void object_apply_global_props(Object
*obj
, const GPtrArray
*props
, Error
**errp
)
381 for (i
= 0; i
< props
->len
; i
++) {
382 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
385 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
388 if (p
->optional
&& !object_property_find(obj
, p
->property
, NULL
)) {
392 object_property_parse(obj
, p
->value
, p
->property
, &err
);
394 error_prepend(&err
, "can't apply global %s.%s=%s: ",
395 p
->driver
, p
->property
, p
->value
);
397 * If errp != NULL, propagate error and return.
398 * If errp == NULL, report a warning, but keep going
399 * with the remaining globals.
402 error_propagate(errp
, err
);
405 warn_report_err(err
);
412 * Global property defaults
413 * Slot 0: accelerator's global property defaults
414 * Slot 1: machine's global property defaults
415 * Each is a GPtrArray of of GlobalProperty.
416 * Applied in order, later entries override earlier ones.
418 static GPtrArray
*object_compat_props
[2];
421 * Set machine's global property defaults to @compat_props.
422 * May be called at most once.
424 void object_set_machine_compat_props(GPtrArray
*compat_props
)
426 assert(!object_compat_props
[1]);
427 object_compat_props
[1] = compat_props
;
431 * Set accelerator's global property defaults to @compat_props.
432 * May be called at most once.
434 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
436 assert(!object_compat_props
[0]);
437 object_compat_props
[0] = compat_props
;
440 void object_apply_compat_props(Object
*obj
)
444 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
445 object_apply_global_props(obj
, object_compat_props
[i
],
450 static void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
454 g_assert(type
!= NULL
);
455 type_initialize(type
);
457 g_assert(type
->instance_size
>= sizeof(Object
));
458 g_assert(type
->abstract
== false);
459 g_assert(size
>= type
->instance_size
);
461 memset(obj
, 0, type
->instance_size
);
462 obj
->class = type
->class;
464 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
465 NULL
, object_property_free
);
466 object_init_with_type(obj
, type
);
467 object_post_init_with_type(obj
, type
);
470 void object_initialize(void *data
, size_t size
, const char *typename
)
472 TypeImpl
*type
= type_get_by_name(typename
);
474 object_initialize_with_type(data
, size
, type
);
477 void object_initialize_child(Object
*parentobj
, const char *propname
,
478 void *childobj
, size_t size
, const char *type
,
483 va_start(vargs
, errp
);
484 object_initialize_childv(parentobj
, propname
, childobj
, size
, type
, errp
,
489 void object_initialize_childv(Object
*parentobj
, const char *propname
,
490 void *childobj
, size_t size
, const char *type
,
491 Error
**errp
, va_list vargs
)
493 Error
*local_err
= NULL
;
497 object_initialize(childobj
, size
, type
);
498 obj
= OBJECT(childobj
);
500 object_set_propv(obj
, &local_err
, vargs
);
505 object_property_add_child(parentobj
, propname
, obj
, &local_err
);
510 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
512 user_creatable_complete(uc
, &local_err
);
514 object_unparent(obj
);
520 * Since object_property_add_child added a reference to the child object,
521 * we can drop the reference added by object_initialize(), so the child
522 * property will own the only reference to the object.
528 error_propagate(errp
, local_err
);
533 static inline bool object_property_is_child(ObjectProperty
*prop
)
535 return strstart(prop
->type
, "child<", NULL
);
538 static void object_property_del_all(Object
*obj
)
540 ObjectProperty
*prop
;
547 g_hash_table_iter_init(&iter
, obj
->properties
);
548 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
551 prop
->release(obj
, prop
->name
, prop
->opaque
);
552 prop
->release
= NULL
;
556 g_hash_table_iter_remove(&iter
);
560 g_hash_table_unref(obj
->properties
);
563 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
565 ObjectProperty
*prop
;
569 g_hash_table_iter_init(&iter
, obj
->properties
);
570 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
572 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
574 prop
->release(obj
, prop
->name
, prop
->opaque
);
575 prop
->release
= NULL
;
580 g_hash_table_iter_init(&iter
, obj
->properties
);
581 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
583 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
584 g_hash_table_iter_remove(&iter
);
590 void object_unparent(Object
*obj
)
593 object_property_del_child(obj
->parent
, obj
, NULL
);
597 static void object_deinit(Object
*obj
, TypeImpl
*type
)
599 if (type
->instance_finalize
) {
600 type
->instance_finalize(obj
);
603 if (type_has_parent(type
)) {
604 object_deinit(obj
, type_get_parent(type
));
608 static void object_finalize(void *data
)
611 TypeImpl
*ti
= obj
->class->type
;
613 object_property_del_all(obj
);
614 object_deinit(obj
, ti
);
616 g_assert(obj
->ref
== 0);
622 static Object
*object_new_with_type(Type type
)
626 g_assert(type
!= NULL
);
627 type_initialize(type
);
629 obj
= g_malloc(type
->instance_size
);
630 object_initialize_with_type(obj
, type
->instance_size
, type
);
636 Object
*object_new(const char *typename
)
638 TypeImpl
*ti
= type_get_by_name(typename
);
640 return object_new_with_type(ti
);
644 Object
*object_new_with_props(const char *typename
,
653 va_start(vargs
, errp
);
654 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
661 Object
*object_new_with_propv(const char *typename
,
669 Error
*local_err
= NULL
;
672 klass
= object_class_by_name(typename
);
674 error_setg(errp
, "invalid object type: %s", typename
);
678 if (object_class_is_abstract(klass
)) {
679 error_setg(errp
, "object type '%s' is abstract", typename
);
682 obj
= object_new_with_type(klass
->type
);
684 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
689 object_property_add_child(parent
, id
, obj
, &local_err
);
695 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
697 user_creatable_complete(uc
, &local_err
);
700 object_unparent(obj
);
706 object_unref(OBJECT(obj
));
710 error_propagate(errp
, local_err
);
716 int object_set_props(Object
*obj
,
723 va_start(vargs
, errp
);
724 ret
= object_set_propv(obj
, errp
, vargs
);
731 int object_set_propv(Object
*obj
,
735 const char *propname
;
736 Error
*local_err
= NULL
;
738 propname
= va_arg(vargs
, char *);
739 while (propname
!= NULL
) {
740 const char *value
= va_arg(vargs
, char *);
742 g_assert(value
!= NULL
);
743 object_property_parse(obj
, value
, propname
, &local_err
);
745 error_propagate(errp
, local_err
);
748 propname
= va_arg(vargs
, char *);
755 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
757 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
764 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
765 const char *file
, int line
, const char *func
)
767 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
768 typename
, file
, line
, func
);
770 #ifdef CONFIG_QOM_CAST_DEBUG
774 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
775 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
780 inst
= object_dynamic_cast(obj
, typename
);
783 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
784 file
, line
, func
, obj
, typename
);
790 if (obj
&& obj
== inst
) {
791 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
792 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
793 atomic_read(&obj
->class->object_cast_cache
[i
]));
795 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
803 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
804 const char *typename
)
806 ObjectClass
*ret
= NULL
;
807 TypeImpl
*target_type
;
814 /* A simple fast path that can trigger a lot for leaf classes. */
816 if (type
->name
== typename
) {
820 target_type
= type_get_by_name(typename
);
822 /* target class type unknown, so fail the cast */
826 if (type
->class->interfaces
&&
827 type_is_ancestor(target_type
, type_interface
)) {
831 for (i
= class->interfaces
; i
; i
= i
->next
) {
832 ObjectClass
*target_class
= i
->data
;
834 if (type_is_ancestor(target_class
->type
, target_type
)) {
840 /* The match was ambiguous, don't allow a cast */
844 } else if (type_is_ancestor(type
, target_type
)) {
851 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
852 const char *typename
,
853 const char *file
, int line
,
858 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
859 typename
, file
, line
, func
);
861 #ifdef CONFIG_QOM_CAST_DEBUG
864 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
865 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
871 if (!class || !class->interfaces
) {
876 ret
= object_class_dynamic_cast(class, typename
);
878 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
879 file
, line
, func
, class, typename
);
883 #ifdef CONFIG_QOM_CAST_DEBUG
884 if (class && ret
== class) {
885 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
886 atomic_set(&class->class_cast_cache
[i
- 1],
887 atomic_read(&class->class_cast_cache
[i
]));
889 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
896 const char *object_get_typename(const Object
*obj
)
898 return obj
->class->type
->name
;
901 ObjectClass
*object_get_class(Object
*obj
)
906 bool object_class_is_abstract(ObjectClass
*klass
)
908 return klass
->type
->abstract
;
911 const char *object_class_get_name(ObjectClass
*klass
)
913 return klass
->type
->name
;
916 ObjectClass
*object_class_by_name(const char *typename
)
918 TypeImpl
*type
= type_get_by_name(typename
);
924 type_initialize(type
);
929 ObjectClass
*object_class_get_parent(ObjectClass
*class)
931 TypeImpl
*type
= type_get_parent(class->type
);
937 type_initialize(type
);
942 typedef struct OCFData
944 void (*fn
)(ObjectClass
*klass
, void *opaque
);
945 const char *implements_type
;
946 bool include_abstract
;
950 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
953 OCFData
*data
= opaque
;
954 TypeImpl
*type
= value
;
957 type_initialize(type
);
960 if (!data
->include_abstract
&& type
->abstract
) {
964 if (data
->implements_type
&&
965 !object_class_dynamic_cast(k
, data
->implements_type
)) {
969 data
->fn(k
, data
->opaque
);
972 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
973 const char *implements_type
, bool include_abstract
,
976 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
978 enumerating_types
= true;
979 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
980 enumerating_types
= false;
983 static int do_object_child_foreach(Object
*obj
,
984 int (*fn
)(Object
*child
, void *opaque
),
985 void *opaque
, bool recurse
)
988 ObjectProperty
*prop
;
991 g_hash_table_iter_init(&iter
, obj
->properties
);
992 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
993 if (object_property_is_child(prop
)) {
994 Object
*child
= prop
->opaque
;
996 ret
= fn(child
, opaque
);
1001 do_object_child_foreach(child
, fn
, opaque
, true);
1008 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1011 return do_object_child_foreach(obj
, fn
, opaque
, false);
1014 int object_child_foreach_recursive(Object
*obj
,
1015 int (*fn
)(Object
*child
, void *opaque
),
1018 return do_object_child_foreach(obj
, fn
, opaque
, true);
1021 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1023 GSList
**list
= opaque
;
1025 *list
= g_slist_prepend(*list
, klass
);
1028 GSList
*object_class_get_list(const char *implements_type
,
1029 bool include_abstract
)
1031 GSList
*list
= NULL
;
1033 object_class_foreach(object_class_get_list_tramp
,
1034 implements_type
, include_abstract
, &list
);
1038 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1040 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1041 object_class_get_name((ObjectClass
*)b
));
1044 GSList
*object_class_get_list_sorted(const char *implements_type
,
1045 bool include_abstract
)
1047 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1051 void object_ref(Object
*obj
)
1056 atomic_inc(&obj
->ref
);
1059 void object_unref(Object
*obj
)
1064 g_assert(obj
->ref
> 0);
1066 /* parent always holds a reference to its children */
1067 if (atomic_fetch_dec(&obj
->ref
) == 1) {
1068 object_finalize(obj
);
1073 object_property_add(Object
*obj
, const char *name
, const char *type
,
1074 ObjectPropertyAccessor
*get
,
1075 ObjectPropertyAccessor
*set
,
1076 ObjectPropertyRelease
*release
,
1077 void *opaque
, Error
**errp
)
1079 ObjectProperty
*prop
;
1080 size_t name_len
= strlen(name
);
1082 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1084 ObjectProperty
*ret
;
1085 char *name_no_array
= g_strdup(name
);
1087 name_no_array
[name_len
- 3] = '\0';
1088 for (i
= 0; ; ++i
) {
1089 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1091 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
1092 release
, opaque
, NULL
);
1098 g_free(name_no_array
);
1102 if (object_property_find(obj
, name
, NULL
) != NULL
) {
1103 error_setg(errp
, "attempt to add duplicate property '%s'"
1104 " to object (type '%s')", name
,
1105 object_get_typename(obj
));
1109 prop
= g_malloc0(sizeof(*prop
));
1111 prop
->name
= g_strdup(name
);
1112 prop
->type
= g_strdup(type
);
1116 prop
->release
= release
;
1117 prop
->opaque
= opaque
;
1119 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1124 object_class_property_add(ObjectClass
*klass
,
1127 ObjectPropertyAccessor
*get
,
1128 ObjectPropertyAccessor
*set
,
1129 ObjectPropertyRelease
*release
,
1133 ObjectProperty
*prop
;
1135 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
1136 error_setg(errp
, "attempt to add duplicate property '%s'"
1137 " to object (type '%s')", name
,
1138 object_class_get_name(klass
));
1142 prop
= g_malloc0(sizeof(*prop
));
1144 prop
->name
= g_strdup(name
);
1145 prop
->type
= g_strdup(type
);
1149 prop
->release
= release
;
1150 prop
->opaque
= opaque
;
1152 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
1157 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1160 ObjectProperty
*prop
;
1161 ObjectClass
*klass
= object_get_class(obj
);
1163 prop
= object_class_property_find(klass
, name
, NULL
);
1168 prop
= g_hash_table_lookup(obj
->properties
, name
);
1173 error_setg(errp
, "Property '.%s' not found", name
);
1177 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1180 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1181 iter
->nextclass
= object_get_class(obj
);
1184 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1187 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1188 if (!iter
->nextclass
) {
1191 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1192 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1197 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1200 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1201 iter
->nextclass
= object_class_get_parent(klass
);
1204 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1207 ObjectProperty
*prop
;
1208 ObjectClass
*parent_klass
;
1210 parent_klass
= object_class_get_parent(klass
);
1212 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1218 prop
= g_hash_table_lookup(klass
->properties
, name
);
1220 error_setg(errp
, "Property '.%s' not found", name
);
1225 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1227 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1230 error_setg(errp
, "Property '.%s' not found", name
);
1234 if (prop
->release
) {
1235 prop
->release(obj
, name
, prop
->opaque
);
1237 g_hash_table_remove(obj
->properties
, name
);
1240 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1243 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1249 error_setg(errp
, QERR_PERMISSION_DENIED
);
1251 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1255 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1258 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1264 error_setg(errp
, QERR_PERMISSION_DENIED
);
1266 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1270 void object_property_set_str(Object
*obj
, const char *value
,
1271 const char *name
, Error
**errp
)
1273 QString
*qstr
= qstring_from_str(value
);
1274 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1276 qobject_unref(qstr
);
1279 char *object_property_get_str(Object
*obj
, const char *name
,
1282 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1289 retval
= g_strdup(qobject_get_try_str(ret
));
1291 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1298 void object_property_set_link(Object
*obj
, Object
*value
,
1299 const char *name
, Error
**errp
)
1302 gchar
*path
= object_get_canonical_path(value
);
1303 object_property_set_str(obj
, path
, name
, errp
);
1306 object_property_set_str(obj
, "", name
, errp
);
1310 Object
*object_property_get_link(Object
*obj
, const char *name
,
1313 char *str
= object_property_get_str(obj
, name
, errp
);
1314 Object
*target
= NULL
;
1317 target
= object_resolve_path(str
, NULL
);
1319 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1320 "Device '%s' not found", str
);
1328 void object_property_set_bool(Object
*obj
, bool value
,
1329 const char *name
, Error
**errp
)
1331 QBool
*qbool
= qbool_from_bool(value
);
1332 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1334 qobject_unref(qbool
);
1337 bool object_property_get_bool(Object
*obj
, const char *name
,
1340 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1347 qbool
= qobject_to(QBool
, ret
);
1349 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1352 retval
= qbool_get_bool(qbool
);
1359 void object_property_set_int(Object
*obj
, int64_t value
,
1360 const char *name
, Error
**errp
)
1362 QNum
*qnum
= qnum_from_int(value
);
1363 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1365 qobject_unref(qnum
);
1368 int64_t object_property_get_int(Object
*obj
, const char *name
,
1371 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1379 qnum
= qobject_to(QNum
, ret
);
1380 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1381 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1389 void object_property_set_uint(Object
*obj
, uint64_t value
,
1390 const char *name
, Error
**errp
)
1392 QNum
*qnum
= qnum_from_uint(value
);
1394 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1395 qobject_unref(qnum
);
1398 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1401 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1408 qnum
= qobject_to(QNum
, ret
);
1409 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1410 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1418 typedef struct EnumProperty
{
1419 const QEnumLookup
*lookup
;
1420 int (*get
)(Object
*, Error
**);
1421 void (*set
)(Object
*, int, Error
**);
1424 int object_property_get_enum(Object
*obj
, const char *name
,
1425 const char *typename
, Error
**errp
)
1431 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1432 EnumProperty
*enumprop
;
1438 if (!g_str_equal(prop
->type
, typename
)) {
1439 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1440 name
, object_class_get_name(
1441 object_get_class(obj
)), typename
);
1445 enumprop
= prop
->opaque
;
1447 v
= string_output_visitor_new(false, &str
);
1448 object_property_get(obj
, v
, name
, &err
);
1450 error_propagate(errp
, err
);
1454 visit_complete(v
, &str
);
1456 v
= string_input_visitor_new(str
);
1457 visit_type_enum(v
, name
, &ret
, enumprop
->lookup
, errp
);
1465 void object_property_get_uint16List(Object
*obj
, const char *name
,
1466 uint16List
**list
, Error
**errp
)
1472 v
= string_output_visitor_new(false, &str
);
1473 object_property_get(obj
, v
, name
, &err
);
1475 error_propagate(errp
, err
);
1478 visit_complete(v
, &str
);
1480 v
= string_input_visitor_new(str
);
1481 visit_type_uint16List(v
, NULL
, list
, errp
);
1488 void object_property_parse(Object
*obj
, const char *string
,
1489 const char *name
, Error
**errp
)
1491 Visitor
*v
= string_input_visitor_new(string
);
1492 object_property_set(obj
, v
, name
, errp
);
1496 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1500 char *string
= NULL
;
1501 Error
*local_err
= NULL
;
1503 v
= string_output_visitor_new(human
, &string
);
1504 object_property_get(obj
, v
, name
, &local_err
);
1506 error_propagate(errp
, local_err
);
1510 visit_complete(v
, &string
);
1517 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1519 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1527 Object
*object_get_root(void)
1529 static Object
*root
;
1532 root
= object_new("container");
1538 Object
*object_get_objects_root(void)
1540 return container_get(object_get_root(), "/objects");
1543 Object
*object_get_internal_root(void)
1545 static Object
*internal_root
;
1547 if (!internal_root
) {
1548 internal_root
= object_new("container");
1551 return internal_root
;
1554 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1555 const char *name
, void *opaque
,
1558 Object
*child
= opaque
;
1561 path
= object_get_canonical_path(child
);
1562 visit_type_str(v
, name
, &path
, errp
);
1566 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1571 static void object_finalize_child_property(Object
*obj
, const char *name
,
1574 Object
*child
= opaque
;
1576 if (child
->class->unparent
) {
1577 (child
->class->unparent
)(child
);
1579 child
->parent
= NULL
;
1580 object_unref(child
);
1583 void object_property_add_child(Object
*obj
, const char *name
,
1584 Object
*child
, Error
**errp
)
1586 Error
*local_err
= NULL
;
1590 if (child
->parent
!= NULL
) {
1591 error_setg(errp
, "child object is already parented");
1595 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1597 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1598 object_finalize_child_property
, child
, &local_err
);
1600 error_propagate(errp
, local_err
);
1604 op
->resolve
= object_resolve_child_property
;
1606 child
->parent
= obj
;
1612 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1613 Object
*val
, Error
**errp
)
1615 /* Allow the link to be set, always */
1620 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1621 ObjectPropertyLinkFlags flags
;
1624 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1625 const char *name
, void *opaque
,
1628 LinkProperty
*lprop
= opaque
;
1629 Object
**child
= lprop
->child
;
1633 path
= object_get_canonical_path(*child
);
1634 visit_type_str(v
, name
, &path
, errp
);
1638 visit_type_str(v
, name
, &path
, errp
);
1643 * object_resolve_link:
1645 * Lookup an object and ensure its type matches the link property type. This
1646 * is similar to object_resolve_path() except type verification against the
1647 * link property is performed.
1649 * Returns: The matched object or NULL on path lookup failures.
1651 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1652 const char *path
, Error
**errp
)
1656 bool ambiguous
= false;
1659 /* Go from link<FOO> to FOO. */
1660 type
= object_property_get_type(obj
, name
, NULL
);
1661 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1662 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1665 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1667 } else if (!target
) {
1668 target
= object_resolve_path(path
, &ambiguous
);
1669 if (target
|| ambiguous
) {
1670 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1672 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1673 "Device '%s' not found", path
);
1677 g_free(target_type
);
1682 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1683 const char *name
, void *opaque
,
1686 Error
*local_err
= NULL
;
1687 LinkProperty
*prop
= opaque
;
1688 Object
**child
= prop
->child
;
1689 Object
*old_target
= *child
;
1690 Object
*new_target
= NULL
;
1693 visit_type_str(v
, name
, &path
, &local_err
);
1695 if (!local_err
&& strcmp(path
, "") != 0) {
1696 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1701 error_propagate(errp
, local_err
);
1705 prop
->check(obj
, name
, new_target
, &local_err
);
1707 error_propagate(errp
, local_err
);
1711 *child
= new_target
;
1712 if (prop
->flags
== OBJ_PROP_LINK_STRONG
) {
1713 object_ref(new_target
);
1714 object_unref(old_target
);
1718 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1720 LinkProperty
*lprop
= opaque
;
1722 return *lprop
->child
;
1725 static void object_release_link_property(Object
*obj
, const char *name
,
1728 LinkProperty
*prop
= opaque
;
1730 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *prop
->child
) {
1731 object_unref(*prop
->child
);
1736 void object_property_add_link(Object
*obj
, const char *name
,
1737 const char *type
, Object
**child
,
1738 void (*check
)(const Object
*, const char *,
1739 Object
*, Error
**),
1740 ObjectPropertyLinkFlags flags
,
1743 Error
*local_err
= NULL
;
1744 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1748 prop
->child
= child
;
1749 prop
->check
= check
;
1750 prop
->flags
= flags
;
1752 full_type
= g_strdup_printf("link<%s>", type
);
1754 op
= object_property_add(obj
, name
, full_type
,
1755 object_get_link_property
,
1756 check
? object_set_link_property
: NULL
,
1757 object_release_link_property
,
1761 error_propagate(errp
, local_err
);
1766 op
->resolve
= object_resolve_link_property
;
1772 void object_property_add_const_link(Object
*obj
, const char *name
,
1773 Object
*target
, Error
**errp
)
1778 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1779 op
= object_property_add(obj
, name
, link_type
,
1780 object_get_child_property
, NULL
,
1781 NULL
, target
, errp
);
1783 op
->resolve
= object_resolve_child_property
;
1788 gchar
*object_get_canonical_path_component(Object
*obj
)
1790 ObjectProperty
*prop
= NULL
;
1791 GHashTableIter iter
;
1793 if (obj
->parent
== NULL
) {
1797 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1798 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1799 if (!object_property_is_child(prop
)) {
1803 if (prop
->opaque
== obj
) {
1804 return g_strdup(prop
->name
);
1808 /* obj had a parent but was not a child, should never happen */
1809 g_assert_not_reached();
1813 gchar
*object_get_canonical_path(Object
*obj
)
1815 Object
*root
= object_get_root();
1816 char *newpath
, *path
= NULL
;
1819 return g_strdup("/");
1823 char *component
= object_get_canonical_path_component(obj
);
1826 /* A canonical path must be complete, so discard what was
1833 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
1838 } while (obj
!= root
);
1843 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1845 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1850 if (prop
->resolve
) {
1851 return prop
->resolve(parent
, prop
->opaque
, part
);
1857 static Object
*object_resolve_abs_path(Object
*parent
,
1859 const char *typename
,
1864 if (parts
[index
] == NULL
) {
1865 return object_dynamic_cast(parent
, typename
);
1868 if (strcmp(parts
[index
], "") == 0) {
1869 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1872 child
= object_resolve_path_component(parent
, parts
[index
]);
1877 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1880 static Object
*object_resolve_partial_path(Object
*parent
,
1882 const char *typename
,
1886 GHashTableIter iter
;
1887 ObjectProperty
*prop
;
1889 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1891 g_hash_table_iter_init(&iter
, parent
->properties
);
1892 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1895 if (!object_property_is_child(prop
)) {
1899 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1900 typename
, ambiguous
);
1917 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1923 parts
= g_strsplit(path
, "/", 0);
1926 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1927 bool ambiguous
= false;
1928 obj
= object_resolve_partial_path(object_get_root(), parts
,
1929 typename
, &ambiguous
);
1931 *ambiguousp
= ambiguous
;
1934 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1942 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1944 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1947 typedef struct StringProperty
1949 char *(*get
)(Object
*, Error
**);
1950 void (*set
)(Object
*, const char *, Error
**);
1953 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1954 void *opaque
, Error
**errp
)
1956 StringProperty
*prop
= opaque
;
1960 value
= prop
->get(obj
, &err
);
1962 error_propagate(errp
, err
);
1966 visit_type_str(v
, name
, &value
, errp
);
1970 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1971 void *opaque
, Error
**errp
)
1973 StringProperty
*prop
= opaque
;
1975 Error
*local_err
= NULL
;
1977 visit_type_str(v
, name
, &value
, &local_err
);
1979 error_propagate(errp
, local_err
);
1983 prop
->set(obj
, value
, errp
);
1987 static void property_release_str(Object
*obj
, const char *name
,
1990 StringProperty
*prop
= opaque
;
1994 void object_property_add_str(Object
*obj
, const char *name
,
1995 char *(*get
)(Object
*, Error
**),
1996 void (*set
)(Object
*, const char *, Error
**),
1999 Error
*local_err
= NULL
;
2000 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2005 object_property_add(obj
, name
, "string",
2006 get
? property_get_str
: NULL
,
2007 set
? property_set_str
: NULL
,
2008 property_release_str
,
2011 error_propagate(errp
, local_err
);
2016 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2017 char *(*get
)(Object
*, Error
**),
2018 void (*set
)(Object
*, const char *,
2022 Error
*local_err
= NULL
;
2023 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2028 object_class_property_add(klass
, name
, "string",
2029 get
? property_get_str
: NULL
,
2030 set
? property_set_str
: NULL
,
2031 property_release_str
,
2034 error_propagate(errp
, local_err
);
2039 typedef struct BoolProperty
2041 bool (*get
)(Object
*, Error
**);
2042 void (*set
)(Object
*, bool, Error
**);
2045 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2046 void *opaque
, Error
**errp
)
2048 BoolProperty
*prop
= opaque
;
2052 value
= prop
->get(obj
, &err
);
2054 error_propagate(errp
, err
);
2058 visit_type_bool(v
, name
, &value
, errp
);
2061 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2062 void *opaque
, Error
**errp
)
2064 BoolProperty
*prop
= opaque
;
2066 Error
*local_err
= NULL
;
2068 visit_type_bool(v
, name
, &value
, &local_err
);
2070 error_propagate(errp
, local_err
);
2074 prop
->set(obj
, value
, errp
);
2077 static void property_release_bool(Object
*obj
, const char *name
,
2080 BoolProperty
*prop
= opaque
;
2084 void object_property_add_bool(Object
*obj
, const char *name
,
2085 bool (*get
)(Object
*, Error
**),
2086 void (*set
)(Object
*, bool, Error
**),
2089 Error
*local_err
= NULL
;
2090 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2095 object_property_add(obj
, name
, "bool",
2096 get
? property_get_bool
: NULL
,
2097 set
? property_set_bool
: NULL
,
2098 property_release_bool
,
2101 error_propagate(errp
, local_err
);
2106 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2107 bool (*get
)(Object
*, Error
**),
2108 void (*set
)(Object
*, bool, Error
**),
2111 Error
*local_err
= NULL
;
2112 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2117 object_class_property_add(klass
, name
, "bool",
2118 get
? property_get_bool
: NULL
,
2119 set
? property_set_bool
: NULL
,
2120 property_release_bool
,
2123 error_propagate(errp
, local_err
);
2128 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2129 void *opaque
, Error
**errp
)
2131 EnumProperty
*prop
= opaque
;
2135 value
= prop
->get(obj
, &err
);
2137 error_propagate(errp
, err
);
2141 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2144 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2145 void *opaque
, Error
**errp
)
2147 EnumProperty
*prop
= opaque
;
2151 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
2153 error_propagate(errp
, err
);
2156 prop
->set(obj
, value
, errp
);
2159 static void property_release_enum(Object
*obj
, const char *name
,
2162 EnumProperty
*prop
= opaque
;
2166 void object_property_add_enum(Object
*obj
, const char *name
,
2167 const char *typename
,
2168 const QEnumLookup
*lookup
,
2169 int (*get
)(Object
*, Error
**),
2170 void (*set
)(Object
*, int, Error
**),
2173 Error
*local_err
= NULL
;
2174 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2176 prop
->lookup
= lookup
;
2180 object_property_add(obj
, name
, typename
,
2181 get
? property_get_enum
: NULL
,
2182 set
? property_set_enum
: NULL
,
2183 property_release_enum
,
2186 error_propagate(errp
, local_err
);
2191 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2192 const char *typename
,
2193 const QEnumLookup
*lookup
,
2194 int (*get
)(Object
*, Error
**),
2195 void (*set
)(Object
*, int, Error
**),
2198 Error
*local_err
= NULL
;
2199 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2201 prop
->lookup
= lookup
;
2205 object_class_property_add(klass
, name
, typename
,
2206 get
? property_get_enum
: NULL
,
2207 set
? property_set_enum
: NULL
,
2208 property_release_enum
,
2211 error_propagate(errp
, local_err
);
2216 typedef struct TMProperty
{
2217 void (*get
)(Object
*, struct tm
*, Error
**);
2220 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2221 void *opaque
, Error
**errp
)
2223 TMProperty
*prop
= opaque
;
2227 prop
->get(obj
, &value
, &err
);
2232 visit_start_struct(v
, name
, NULL
, 0, &err
);
2236 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2240 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2244 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2248 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2252 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2256 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2260 visit_check_struct(v
, &err
);
2262 visit_end_struct(v
, NULL
);
2264 error_propagate(errp
, err
);
2268 static void property_release_tm(Object
*obj
, const char *name
,
2271 TMProperty
*prop
= opaque
;
2275 void object_property_add_tm(Object
*obj
, const char *name
,
2276 void (*get
)(Object
*, struct tm
*, Error
**),
2279 Error
*local_err
= NULL
;
2280 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2284 object_property_add(obj
, name
, "struct tm",
2285 get
? property_get_tm
: NULL
, NULL
,
2286 property_release_tm
,
2289 error_propagate(errp
, local_err
);
2294 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2295 void (*get
)(Object
*, struct tm
*, Error
**),
2298 Error
*local_err
= NULL
;
2299 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2303 object_class_property_add(klass
, name
, "struct tm",
2304 get
? property_get_tm
: NULL
, NULL
,
2305 property_release_tm
,
2308 error_propagate(errp
, local_err
);
2313 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2315 return g_strdup(object_get_typename(obj
));
2318 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2319 void *opaque
, Error
**errp
)
2321 uint8_t value
= *(uint8_t *)opaque
;
2322 visit_type_uint8(v
, name
, &value
, errp
);
2325 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2326 void *opaque
, Error
**errp
)
2328 uint16_t value
= *(uint16_t *)opaque
;
2329 visit_type_uint16(v
, name
, &value
, errp
);
2332 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2333 void *opaque
, Error
**errp
)
2335 uint32_t value
= *(uint32_t *)opaque
;
2336 visit_type_uint32(v
, name
, &value
, errp
);
2339 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2340 void *opaque
, Error
**errp
)
2342 uint64_t value
= *(uint64_t *)opaque
;
2343 visit_type_uint64(v
, name
, &value
, errp
);
2346 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2347 const uint8_t *v
, Error
**errp
)
2349 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2350 NULL
, NULL
, (void *)v
, errp
);
2353 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2354 const uint8_t *v
, Error
**errp
)
2356 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2357 NULL
, NULL
, (void *)v
, errp
);
2360 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2361 const uint16_t *v
, Error
**errp
)
2363 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2364 NULL
, NULL
, (void *)v
, errp
);
2367 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2368 const uint16_t *v
, Error
**errp
)
2370 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2371 NULL
, NULL
, (void *)v
, errp
);
2374 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2375 const uint32_t *v
, Error
**errp
)
2377 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2378 NULL
, NULL
, (void *)v
, errp
);
2381 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2382 const uint32_t *v
, Error
**errp
)
2384 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2385 NULL
, NULL
, (void *)v
, errp
);
2388 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2389 const uint64_t *v
, Error
**errp
)
2391 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2392 NULL
, NULL
, (void *)v
, errp
);
2395 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2396 const uint64_t *v
, Error
**errp
)
2398 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2399 NULL
, NULL
, (void *)v
, errp
);
2407 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2408 void *opaque
, Error
**errp
)
2410 AliasProperty
*prop
= opaque
;
2412 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2415 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2416 void *opaque
, Error
**errp
)
2418 AliasProperty
*prop
= opaque
;
2420 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2423 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2426 AliasProperty
*prop
= opaque
;
2428 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2431 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2433 AliasProperty
*prop
= opaque
;
2435 g_free(prop
->target_name
);
2439 void object_property_add_alias(Object
*obj
, const char *name
,
2440 Object
*target_obj
, const char *target_name
,
2443 AliasProperty
*prop
;
2445 ObjectProperty
*target_prop
;
2447 Error
*local_err
= NULL
;
2449 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2454 if (object_property_is_child(target_prop
)) {
2455 prop_type
= g_strdup_printf("link%s",
2456 target_prop
->type
+ strlen("child"));
2458 prop_type
= g_strdup(target_prop
->type
);
2461 prop
= g_malloc(sizeof(*prop
));
2462 prop
->target_obj
= target_obj
;
2463 prop
->target_name
= g_strdup(target_name
);
2465 op
= object_property_add(obj
, name
, prop_type
,
2468 property_release_alias
,
2471 error_propagate(errp
, local_err
);
2475 op
->resolve
= property_resolve_alias
;
2477 object_property_set_description(obj
, op
->name
,
2478 target_prop
->description
,
2485 void object_property_set_description(Object
*obj
, const char *name
,
2486 const char *description
, Error
**errp
)
2490 op
= object_property_find(obj
, name
, errp
);
2495 g_free(op
->description
);
2496 op
->description
= g_strdup(description
);
2499 void object_class_property_set_description(ObjectClass
*klass
,
2501 const char *description
,
2506 op
= g_hash_table_lookup(klass
->properties
, name
);
2508 error_setg(errp
, "Property '.%s' not found", name
);
2512 g_free(op
->description
);
2513 op
->description
= g_strdup(description
);
2516 static void object_class_init(ObjectClass
*klass
, void *data
)
2518 object_class_property_add_str(klass
, "type", qdev_get_type
,
2519 NULL
, &error_abort
);
2522 static void register_types(void)
2524 static TypeInfo interface_info
= {
2525 .name
= TYPE_INTERFACE
,
2526 .class_size
= sizeof(InterfaceClass
),
2530 static TypeInfo object_info
= {
2531 .name
= TYPE_OBJECT
,
2532 .instance_size
= sizeof(Object
),
2533 .class_init
= object_class_init
,
2537 type_interface
= type_register_internal(&interface_info
);
2538 type_register_internal(&object_info
);
2541 type_init(register_types
)