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 "hw/qdev-core.h"
15 #include "qapi/error.h"
16 #include "qom/object.h"
17 #include "qom/object_interfaces.h"
18 #include "qemu/cutils.h"
19 #include "qapi/visitor.h"
20 #include "qapi/string-input-visitor.h"
21 #include "qapi/string-output-visitor.h"
22 #include "qapi/qapi-builtin-visit.h"
23 #include "qapi/qmp/qerror.h"
26 /* TODO: replace QObject with a simpler visitor to avoid a dependency
27 * of the QOM core on QObject? */
28 #include "qom/qom-qobject.h"
29 #include "qapi/qmp/qbool.h"
30 #include "qapi/qmp/qnum.h"
31 #include "qapi/qmp/qstring.h"
32 #include "qemu/error-report.h"
34 #define MAX_INTERFACES 32
36 typedef struct InterfaceImpl InterfaceImpl
;
37 typedef struct TypeImpl TypeImpl
;
52 void (*class_init
)(ObjectClass
*klass
, void *data
);
53 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
57 void (*instance_init
)(Object
*obj
);
58 void (*instance_post_init
)(Object
*obj
);
59 void (*instance_finalize
)(Object
*obj
);
64 TypeImpl
*parent_type
;
69 InterfaceImpl interfaces
[MAX_INTERFACES
];
72 static Type type_interface
;
74 static GHashTable
*type_table_get(void)
76 static GHashTable
*type_table
;
78 if (type_table
== NULL
) {
79 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
85 static bool enumerating_types
;
87 static void type_table_add(TypeImpl
*ti
)
89 assert(!enumerating_types
);
90 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
93 static TypeImpl
*type_table_lookup(const char *name
)
95 return g_hash_table_lookup(type_table_get(), name
);
98 static TypeImpl
*type_new(const TypeInfo
*info
)
100 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
103 g_assert(info
->name
!= NULL
);
105 if (type_table_lookup(info
->name
) != NULL
) {
106 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
110 ti
->name
= g_strdup(info
->name
);
111 ti
->parent
= g_strdup(info
->parent
);
113 ti
->class_size
= info
->class_size
;
114 ti
->instance_size
= info
->instance_size
;
116 ti
->class_init
= info
->class_init
;
117 ti
->class_base_init
= info
->class_base_init
;
118 ti
->class_data
= info
->class_data
;
120 ti
->instance_init
= info
->instance_init
;
121 ti
->instance_post_init
= info
->instance_post_init
;
122 ti
->instance_finalize
= info
->instance_finalize
;
124 ti
->abstract
= info
->abstract
;
126 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
127 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
129 ti
->num_interfaces
= i
;
134 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
143 TypeImpl
*type_register(const TypeInfo
*info
)
145 assert(info
->parent
);
146 return type_register_internal(info
);
149 TypeImpl
*type_register_static(const TypeInfo
*info
)
151 return type_register(info
);
154 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
158 for (i
= 0; i
< nr_infos
; i
++) {
159 type_register_static(&infos
[i
]);
163 static TypeImpl
*type_get_by_name(const char *name
)
169 return type_table_lookup(name
);
172 static TypeImpl
*type_get_parent(TypeImpl
*type
)
174 if (!type
->parent_type
&& type
->parent
) {
175 type
->parent_type
= type_get_by_name(type
->parent
);
176 g_assert(type
->parent_type
!= NULL
);
179 return type
->parent_type
;
182 static bool type_has_parent(TypeImpl
*type
)
184 return (type
->parent
!= NULL
);
187 static size_t type_class_get_size(TypeImpl
*ti
)
189 if (ti
->class_size
) {
190 return ti
->class_size
;
193 if (type_has_parent(ti
)) {
194 return type_class_get_size(type_get_parent(ti
));
197 return sizeof(ObjectClass
);
200 static size_t type_object_get_size(TypeImpl
*ti
)
202 if (ti
->instance_size
) {
203 return ti
->instance_size
;
206 if (type_has_parent(ti
)) {
207 return type_object_get_size(type_get_parent(ti
));
213 size_t object_type_get_instance_size(const char *typename
)
215 TypeImpl
*type
= type_get_by_name(typename
);
217 g_assert(type
!= NULL
);
218 return type_object_get_size(type
);
221 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
225 /* Check if target_type is a direct ancestor of type */
227 if (type
== target_type
) {
231 type
= type_get_parent(type
);
237 static void type_initialize(TypeImpl
*ti
);
239 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
240 TypeImpl
*parent_type
)
242 InterfaceClass
*new_iface
;
244 TypeImpl
*iface_impl
;
246 info
.parent
= parent_type
->name
;
247 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
248 info
.abstract
= true;
250 iface_impl
= type_new(&info
);
251 iface_impl
->parent_type
= parent_type
;
252 type_initialize(iface_impl
);
253 g_free((char *)info
.name
);
255 new_iface
= (InterfaceClass
*)iface_impl
->class;
256 new_iface
->concrete_class
= ti
->class;
257 new_iface
->interface_type
= interface_type
;
259 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
,
263 static void object_property_free(gpointer data
)
265 ObjectProperty
*prop
= data
;
269 g_free(prop
->description
);
273 static void type_initialize(TypeImpl
*ti
)
281 ti
->class_size
= type_class_get_size(ti
);
282 ti
->instance_size
= type_object_get_size(ti
);
283 /* Any type with zero instance_size is implicitly abstract.
284 * This means interface types are all abstract.
286 if (ti
->instance_size
== 0) {
289 if (type_is_ancestor(ti
, type_interface
)) {
290 assert(ti
->instance_size
== 0);
291 assert(ti
->abstract
);
292 assert(!ti
->instance_init
);
293 assert(!ti
->instance_post_init
);
294 assert(!ti
->instance_finalize
);
295 assert(!ti
->num_interfaces
);
297 ti
->class = g_malloc0(ti
->class_size
);
299 parent
= type_get_parent(ti
);
301 type_initialize(parent
);
305 g_assert(parent
->class_size
<= ti
->class_size
);
306 memcpy(ti
->class, parent
->class, parent
->class_size
);
307 ti
->class->interfaces
= NULL
;
308 ti
->class->properties
= g_hash_table_new_full(
309 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
311 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
312 InterfaceClass
*iface
= e
->data
;
313 ObjectClass
*klass
= OBJECT_CLASS(iface
);
315 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
318 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
319 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
320 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
321 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
323 if (type_is_ancestor(target_type
, t
)) {
332 type_initialize_interface(ti
, t
, t
);
335 ti
->class->properties
= g_hash_table_new_full(
336 g_str_hash
, g_str_equal
, g_free
, object_property_free
);
339 ti
->class->type
= ti
;
342 if (parent
->class_base_init
) {
343 parent
->class_base_init(ti
->class, ti
->class_data
);
345 parent
= type_get_parent(parent
);
348 if (ti
->class_init
) {
349 ti
->class_init(ti
->class, ti
->class_data
);
353 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
355 if (type_has_parent(ti
)) {
356 object_init_with_type(obj
, type_get_parent(ti
));
359 if (ti
->instance_init
) {
360 ti
->instance_init(obj
);
364 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
366 if (ti
->instance_post_init
) {
367 ti
->instance_post_init(obj
);
370 if (type_has_parent(ti
)) {
371 object_post_init_with_type(obj
, type_get_parent(ti
));
375 void object_apply_global_props(Object
*obj
, const GPtrArray
*props
, Error
**errp
)
383 for (i
= 0; i
< props
->len
; i
++) {
384 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
387 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
390 if (p
->optional
&& !object_property_find(obj
, p
->property
, NULL
)) {
394 object_property_parse(obj
, p
->value
, p
->property
, &err
);
396 error_prepend(&err
, "can't apply global %s.%s=%s: ",
397 p
->driver
, p
->property
, p
->value
);
399 * If errp != NULL, propagate error and return.
400 * If errp == NULL, report a warning, but keep going
401 * with the remaining globals.
404 error_propagate(errp
, err
);
407 warn_report_err(err
);
414 * Global property defaults
415 * Slot 0: accelerator's global property defaults
416 * Slot 1: machine's global property defaults
417 * Slot 2: global properties from legacy command line option
418 * Each is a GPtrArray of of GlobalProperty.
419 * Applied in order, later entries override earlier ones.
421 static GPtrArray
*object_compat_props
[3];
424 * Retrieve @GPtrArray for global property defined with options
425 * other than "-global". These are generally used for syntactic
426 * sugar and legacy command line options.
428 void object_register_sugar_prop(const char *driver
, const char *prop
, const char *value
)
431 if (!object_compat_props
[2]) {
432 object_compat_props
[2] = g_ptr_array_new();
434 g
= g_new0(GlobalProperty
, 1);
435 g
->driver
= g_strdup(driver
);
436 g
->property
= g_strdup(prop
);
437 g
->value
= g_strdup(value
);
438 g_ptr_array_add(object_compat_props
[2], g
);
442 * Set machine's global property defaults to @compat_props.
443 * May be called at most once.
445 void object_set_machine_compat_props(GPtrArray
*compat_props
)
447 assert(!object_compat_props
[1]);
448 object_compat_props
[1] = compat_props
;
452 * Set accelerator's global property defaults to @compat_props.
453 * May be called at most once.
455 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
457 assert(!object_compat_props
[0]);
458 object_compat_props
[0] = compat_props
;
461 void object_apply_compat_props(Object
*obj
)
465 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
466 object_apply_global_props(obj
, object_compat_props
[i
],
467 i
== 2 ? &error_fatal
: &error_abort
);
471 static void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
475 type_initialize(type
);
477 g_assert(type
->instance_size
>= sizeof(Object
));
478 g_assert(type
->abstract
== false);
479 g_assert(size
>= type
->instance_size
);
481 memset(obj
, 0, type
->instance_size
);
482 obj
->class = type
->class;
484 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
485 NULL
, object_property_free
);
486 object_init_with_type(obj
, type
);
487 object_post_init_with_type(obj
, type
);
490 void object_initialize(void *data
, size_t size
, const char *typename
)
492 TypeImpl
*type
= type_get_by_name(typename
);
495 error_report("missing object type '%s'", typename
);
499 object_initialize_with_type(data
, size
, type
);
502 void object_initialize_child(Object
*parentobj
, const char *propname
,
503 void *childobj
, size_t size
, const char *type
,
508 va_start(vargs
, errp
);
509 object_initialize_childv(parentobj
, propname
, childobj
, size
, type
, errp
,
514 void object_initialize_childv(Object
*parentobj
, const char *propname
,
515 void *childobj
, size_t size
, const char *type
,
516 Error
**errp
, va_list vargs
)
518 Error
*local_err
= NULL
;
522 object_initialize(childobj
, size
, type
);
523 obj
= OBJECT(childobj
);
525 object_set_propv(obj
, &local_err
, vargs
);
530 object_property_add_child(parentobj
, propname
, obj
, &local_err
);
535 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
537 user_creatable_complete(uc
, &local_err
);
539 object_unparent(obj
);
545 * Since object_property_add_child added a reference to the child object,
546 * we can drop the reference added by object_initialize(), so the child
547 * property will own the only reference to the object.
553 error_propagate(errp
, local_err
);
558 static inline bool object_property_is_child(ObjectProperty
*prop
)
560 return strstart(prop
->type
, "child<", NULL
);
563 static void object_property_del_all(Object
*obj
)
565 ObjectProperty
*prop
;
572 g_hash_table_iter_init(&iter
, obj
->properties
);
573 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
576 prop
->release(obj
, prop
->name
, prop
->opaque
);
577 prop
->release
= NULL
;
581 g_hash_table_iter_remove(&iter
);
585 g_hash_table_unref(obj
->properties
);
588 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
590 ObjectProperty
*prop
;
594 g_hash_table_iter_init(&iter
, obj
->properties
);
595 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
597 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
599 prop
->release(obj
, prop
->name
, prop
->opaque
);
600 prop
->release
= NULL
;
605 g_hash_table_iter_init(&iter
, obj
->properties
);
606 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
608 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
609 g_hash_table_iter_remove(&iter
);
615 void object_unparent(Object
*obj
)
618 object_property_del_child(obj
->parent
, obj
, NULL
);
622 static void object_deinit(Object
*obj
, TypeImpl
*type
)
624 if (type
->instance_finalize
) {
625 type
->instance_finalize(obj
);
628 if (type_has_parent(type
)) {
629 object_deinit(obj
, type_get_parent(type
));
633 static void object_finalize(void *data
)
636 TypeImpl
*ti
= obj
->class->type
;
638 object_property_del_all(obj
);
639 object_deinit(obj
, ti
);
641 g_assert(obj
->ref
== 0);
647 static Object
*object_new_with_type(Type type
)
651 g_assert(type
!= NULL
);
652 type_initialize(type
);
654 obj
= g_malloc(type
->instance_size
);
655 object_initialize_with_type(obj
, type
->instance_size
, type
);
661 Object
*object_new_with_class(ObjectClass
*klass
)
663 return object_new_with_type(klass
->type
);
666 Object
*object_new(const char *typename
)
668 TypeImpl
*ti
= type_get_by_name(typename
);
670 return object_new_with_type(ti
);
674 Object
*object_new_with_props(const char *typename
,
683 va_start(vargs
, errp
);
684 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
691 Object
*object_new_with_propv(const char *typename
,
699 Error
*local_err
= NULL
;
702 klass
= object_class_by_name(typename
);
704 error_setg(errp
, "invalid object type: %s", typename
);
708 if (object_class_is_abstract(klass
)) {
709 error_setg(errp
, "object type '%s' is abstract", typename
);
712 obj
= object_new_with_type(klass
->type
);
714 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
719 object_property_add_child(parent
, id
, obj
, &local_err
);
725 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
727 user_creatable_complete(uc
, &local_err
);
730 object_unparent(obj
);
736 object_unref(OBJECT(obj
));
740 error_propagate(errp
, local_err
);
746 int object_set_props(Object
*obj
,
753 va_start(vargs
, errp
);
754 ret
= object_set_propv(obj
, errp
, vargs
);
761 int object_set_propv(Object
*obj
,
765 const char *propname
;
766 Error
*local_err
= NULL
;
768 propname
= va_arg(vargs
, char *);
769 while (propname
!= NULL
) {
770 const char *value
= va_arg(vargs
, char *);
772 g_assert(value
!= NULL
);
773 object_property_parse(obj
, value
, propname
, &local_err
);
775 error_propagate(errp
, local_err
);
778 propname
= va_arg(vargs
, char *);
785 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
787 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
794 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
795 const char *file
, int line
, const char *func
)
797 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
798 typename
, file
, line
, func
);
800 #ifdef CONFIG_QOM_CAST_DEBUG
804 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
805 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
810 inst
= object_dynamic_cast(obj
, typename
);
813 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
814 file
, line
, func
, obj
, typename
);
820 if (obj
&& obj
== inst
) {
821 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
822 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
823 atomic_read(&obj
->class->object_cast_cache
[i
]));
825 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
833 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
834 const char *typename
)
836 ObjectClass
*ret
= NULL
;
837 TypeImpl
*target_type
;
844 /* A simple fast path that can trigger a lot for leaf classes. */
846 if (type
->name
== typename
) {
850 target_type
= type_get_by_name(typename
);
852 /* target class type unknown, so fail the cast */
856 if (type
->class->interfaces
&&
857 type_is_ancestor(target_type
, type_interface
)) {
861 for (i
= class->interfaces
; i
; i
= i
->next
) {
862 ObjectClass
*target_class
= i
->data
;
864 if (type_is_ancestor(target_class
->type
, target_type
)) {
870 /* The match was ambiguous, don't allow a cast */
874 } else if (type_is_ancestor(type
, target_type
)) {
881 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
882 const char *typename
,
883 const char *file
, int line
,
888 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
889 typename
, file
, line
, func
);
891 #ifdef CONFIG_QOM_CAST_DEBUG
894 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
895 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
901 if (!class || !class->interfaces
) {
906 ret
= object_class_dynamic_cast(class, typename
);
908 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
909 file
, line
, func
, class, typename
);
913 #ifdef CONFIG_QOM_CAST_DEBUG
914 if (class && ret
== class) {
915 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
916 atomic_set(&class->class_cast_cache
[i
- 1],
917 atomic_read(&class->class_cast_cache
[i
]));
919 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
926 const char *object_get_typename(const Object
*obj
)
928 return obj
->class->type
->name
;
931 ObjectClass
*object_get_class(Object
*obj
)
936 bool object_class_is_abstract(ObjectClass
*klass
)
938 return klass
->type
->abstract
;
941 const char *object_class_get_name(ObjectClass
*klass
)
943 return klass
->type
->name
;
946 ObjectClass
*object_class_by_name(const char *typename
)
948 TypeImpl
*type
= type_get_by_name(typename
);
954 type_initialize(type
);
959 ObjectClass
*object_class_get_parent(ObjectClass
*class)
961 TypeImpl
*type
= type_get_parent(class->type
);
967 type_initialize(type
);
972 typedef struct OCFData
974 void (*fn
)(ObjectClass
*klass
, void *opaque
);
975 const char *implements_type
;
976 bool include_abstract
;
980 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
983 OCFData
*data
= opaque
;
984 TypeImpl
*type
= value
;
987 type_initialize(type
);
990 if (!data
->include_abstract
&& type
->abstract
) {
994 if (data
->implements_type
&&
995 !object_class_dynamic_cast(k
, data
->implements_type
)) {
999 data
->fn(k
, data
->opaque
);
1002 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1003 const char *implements_type
, bool include_abstract
,
1006 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1008 enumerating_types
= true;
1009 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1010 enumerating_types
= false;
1013 static int do_object_child_foreach(Object
*obj
,
1014 int (*fn
)(Object
*child
, void *opaque
),
1015 void *opaque
, bool recurse
)
1017 GHashTableIter iter
;
1018 ObjectProperty
*prop
;
1021 g_hash_table_iter_init(&iter
, obj
->properties
);
1022 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1023 if (object_property_is_child(prop
)) {
1024 Object
*child
= prop
->opaque
;
1026 ret
= fn(child
, opaque
);
1031 do_object_child_foreach(child
, fn
, opaque
, true);
1038 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1041 return do_object_child_foreach(obj
, fn
, opaque
, false);
1044 int object_child_foreach_recursive(Object
*obj
,
1045 int (*fn
)(Object
*child
, void *opaque
),
1048 return do_object_child_foreach(obj
, fn
, opaque
, true);
1051 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1053 GSList
**list
= opaque
;
1055 *list
= g_slist_prepend(*list
, klass
);
1058 GSList
*object_class_get_list(const char *implements_type
,
1059 bool include_abstract
)
1061 GSList
*list
= NULL
;
1063 object_class_foreach(object_class_get_list_tramp
,
1064 implements_type
, include_abstract
, &list
);
1068 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1070 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1071 object_class_get_name((ObjectClass
*)b
));
1074 GSList
*object_class_get_list_sorted(const char *implements_type
,
1075 bool include_abstract
)
1077 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1081 void object_ref(Object
*obj
)
1086 atomic_inc(&obj
->ref
);
1089 void object_unref(Object
*obj
)
1094 g_assert(obj
->ref
> 0);
1096 /* parent always holds a reference to its children */
1097 if (atomic_fetch_dec(&obj
->ref
) == 1) {
1098 object_finalize(obj
);
1103 object_property_add(Object
*obj
, const char *name
, const char *type
,
1104 ObjectPropertyAccessor
*get
,
1105 ObjectPropertyAccessor
*set
,
1106 ObjectPropertyRelease
*release
,
1107 void *opaque
, Error
**errp
)
1109 ObjectProperty
*prop
;
1110 size_t name_len
= strlen(name
);
1112 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1114 ObjectProperty
*ret
;
1115 char *name_no_array
= g_strdup(name
);
1117 name_no_array
[name_len
- 3] = '\0';
1118 for (i
= 0; ; ++i
) {
1119 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1121 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
1122 release
, opaque
, NULL
);
1128 g_free(name_no_array
);
1132 if (object_property_find(obj
, name
, NULL
) != NULL
) {
1133 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1134 name
, object_get_typename(obj
));
1138 prop
= g_malloc0(sizeof(*prop
));
1140 prop
->name
= g_strdup(name
);
1141 prop
->type
= g_strdup(type
);
1145 prop
->release
= release
;
1146 prop
->opaque
= opaque
;
1148 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1153 object_class_property_add(ObjectClass
*klass
,
1156 ObjectPropertyAccessor
*get
,
1157 ObjectPropertyAccessor
*set
,
1158 ObjectPropertyRelease
*release
,
1162 ObjectProperty
*prop
;
1164 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
1165 error_setg(errp
, "attempt to add duplicate property '%s' to class (type '%s')",
1166 name
, object_class_get_name(klass
));
1170 prop
= g_malloc0(sizeof(*prop
));
1172 prop
->name
= g_strdup(name
);
1173 prop
->type
= g_strdup(type
);
1177 prop
->release
= release
;
1178 prop
->opaque
= opaque
;
1180 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
1185 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1188 ObjectProperty
*prop
;
1189 ObjectClass
*klass
= object_get_class(obj
);
1191 prop
= object_class_property_find(klass
, name
, NULL
);
1196 prop
= g_hash_table_lookup(obj
->properties
, name
);
1201 error_setg(errp
, "Property '.%s' not found", name
);
1205 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1208 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1209 iter
->nextclass
= object_get_class(obj
);
1212 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1215 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1216 if (!iter
->nextclass
) {
1219 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1220 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1225 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1228 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1229 iter
->nextclass
= object_class_get_parent(klass
);
1232 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1235 ObjectProperty
*prop
;
1236 ObjectClass
*parent_klass
;
1238 parent_klass
= object_class_get_parent(klass
);
1240 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1246 prop
= g_hash_table_lookup(klass
->properties
, name
);
1248 error_setg(errp
, "Property '.%s' not found", name
);
1253 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1255 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1258 error_setg(errp
, "Property '.%s' not found", name
);
1262 if (prop
->release
) {
1263 prop
->release(obj
, name
, prop
->opaque
);
1265 g_hash_table_remove(obj
->properties
, name
);
1268 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1271 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1277 error_setg(errp
, QERR_PERMISSION_DENIED
);
1279 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1283 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1286 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1292 error_setg(errp
, QERR_PERMISSION_DENIED
);
1294 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1298 void object_property_set_str(Object
*obj
, const char *value
,
1299 const char *name
, Error
**errp
)
1301 QString
*qstr
= qstring_from_str(value
);
1302 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1304 qobject_unref(qstr
);
1307 char *object_property_get_str(Object
*obj
, const char *name
,
1310 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1317 retval
= g_strdup(qobject_get_try_str(ret
));
1319 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1326 void object_property_set_link(Object
*obj
, Object
*value
,
1327 const char *name
, Error
**errp
)
1330 gchar
*path
= object_get_canonical_path(value
);
1331 object_property_set_str(obj
, path
, name
, errp
);
1334 object_property_set_str(obj
, "", name
, errp
);
1338 Object
*object_property_get_link(Object
*obj
, const char *name
,
1341 char *str
= object_property_get_str(obj
, name
, errp
);
1342 Object
*target
= NULL
;
1345 target
= object_resolve_path(str
, NULL
);
1347 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1348 "Device '%s' not found", str
);
1356 void object_property_set_bool(Object
*obj
, bool value
,
1357 const char *name
, Error
**errp
)
1359 QBool
*qbool
= qbool_from_bool(value
);
1360 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1362 qobject_unref(qbool
);
1365 bool object_property_get_bool(Object
*obj
, const char *name
,
1368 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1375 qbool
= qobject_to(QBool
, ret
);
1377 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1380 retval
= qbool_get_bool(qbool
);
1387 void object_property_set_int(Object
*obj
, int64_t value
,
1388 const char *name
, Error
**errp
)
1390 QNum
*qnum
= qnum_from_int(value
);
1391 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1393 qobject_unref(qnum
);
1396 int64_t object_property_get_int(Object
*obj
, const char *name
,
1399 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1407 qnum
= qobject_to(QNum
, ret
);
1408 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1409 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1417 void object_property_set_uint(Object
*obj
, uint64_t value
,
1418 const char *name
, Error
**errp
)
1420 QNum
*qnum
= qnum_from_uint(value
);
1422 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1423 qobject_unref(qnum
);
1426 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1429 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1436 qnum
= qobject_to(QNum
, ret
);
1437 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1438 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1446 typedef struct EnumProperty
{
1447 const QEnumLookup
*lookup
;
1448 int (*get
)(Object
*, Error
**);
1449 void (*set
)(Object
*, int, Error
**);
1452 int object_property_get_enum(Object
*obj
, const char *name
,
1453 const char *typename
, Error
**errp
)
1459 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1460 EnumProperty
*enumprop
;
1466 if (!g_str_equal(prop
->type
, typename
)) {
1467 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1468 name
, object_class_get_name(
1469 object_get_class(obj
)), typename
);
1473 enumprop
= prop
->opaque
;
1475 v
= string_output_visitor_new(false, &str
);
1476 object_property_get(obj
, v
, name
, &err
);
1478 error_propagate(errp
, err
);
1482 visit_complete(v
, &str
);
1484 v
= string_input_visitor_new(str
);
1485 visit_type_enum(v
, name
, &ret
, enumprop
->lookup
, errp
);
1493 void object_property_get_uint16List(Object
*obj
, const char *name
,
1494 uint16List
**list
, Error
**errp
)
1500 v
= string_output_visitor_new(false, &str
);
1501 object_property_get(obj
, v
, name
, &err
);
1503 error_propagate(errp
, err
);
1506 visit_complete(v
, &str
);
1508 v
= string_input_visitor_new(str
);
1509 visit_type_uint16List(v
, NULL
, list
, errp
);
1516 void object_property_parse(Object
*obj
, const char *string
,
1517 const char *name
, Error
**errp
)
1519 Visitor
*v
= string_input_visitor_new(string
);
1520 object_property_set(obj
, v
, name
, errp
);
1524 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1528 char *string
= NULL
;
1529 Error
*local_err
= NULL
;
1531 v
= string_output_visitor_new(human
, &string
);
1532 object_property_get(obj
, v
, name
, &local_err
);
1534 error_propagate(errp
, local_err
);
1538 visit_complete(v
, &string
);
1545 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1547 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1555 Object
*object_get_root(void)
1557 static Object
*root
;
1560 root
= object_new("container");
1566 Object
*object_get_objects_root(void)
1568 return container_get(object_get_root(), "/objects");
1571 Object
*object_get_internal_root(void)
1573 static Object
*internal_root
;
1575 if (!internal_root
) {
1576 internal_root
= object_new("container");
1579 return internal_root
;
1582 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1583 const char *name
, void *opaque
,
1586 Object
*child
= opaque
;
1589 path
= object_get_canonical_path(child
);
1590 visit_type_str(v
, name
, &path
, errp
);
1594 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1599 static void object_finalize_child_property(Object
*obj
, const char *name
,
1602 Object
*child
= opaque
;
1604 if (child
->class->unparent
) {
1605 (child
->class->unparent
)(child
);
1607 child
->parent
= NULL
;
1608 object_unref(child
);
1611 void object_property_add_child(Object
*obj
, const char *name
,
1612 Object
*child
, Error
**errp
)
1614 Error
*local_err
= NULL
;
1618 if (child
->parent
!= NULL
) {
1619 error_setg(errp
, "child object is already parented");
1623 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1625 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1626 object_finalize_child_property
, child
, &local_err
);
1628 error_propagate(errp
, local_err
);
1632 op
->resolve
= object_resolve_child_property
;
1634 child
->parent
= obj
;
1640 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1641 Object
*val
, Error
**errp
)
1643 /* Allow the link to be set, always */
1648 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1649 ObjectPropertyLinkFlags flags
;
1652 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1653 const char *name
, void *opaque
,
1656 LinkProperty
*lprop
= opaque
;
1657 Object
**child
= lprop
->child
;
1661 path
= object_get_canonical_path(*child
);
1662 visit_type_str(v
, name
, &path
, errp
);
1666 visit_type_str(v
, name
, &path
, errp
);
1671 * object_resolve_link:
1673 * Lookup an object and ensure its type matches the link property type. This
1674 * is similar to object_resolve_path() except type verification against the
1675 * link property is performed.
1677 * Returns: The matched object or NULL on path lookup failures.
1679 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1680 const char *path
, Error
**errp
)
1684 bool ambiguous
= false;
1687 /* Go from link<FOO> to FOO. */
1688 type
= object_property_get_type(obj
, name
, NULL
);
1689 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1690 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1693 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1695 } else if (!target
) {
1696 target
= object_resolve_path(path
, &ambiguous
);
1697 if (target
|| ambiguous
) {
1698 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1700 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1701 "Device '%s' not found", path
);
1705 g_free(target_type
);
1710 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1711 const char *name
, void *opaque
,
1714 Error
*local_err
= NULL
;
1715 LinkProperty
*prop
= opaque
;
1716 Object
**child
= prop
->child
;
1717 Object
*old_target
= *child
;
1718 Object
*new_target
= NULL
;
1721 visit_type_str(v
, name
, &path
, &local_err
);
1723 if (!local_err
&& strcmp(path
, "") != 0) {
1724 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1729 error_propagate(errp
, local_err
);
1733 prop
->check(obj
, name
, new_target
, &local_err
);
1735 error_propagate(errp
, local_err
);
1739 *child
= new_target
;
1740 if (prop
->flags
== OBJ_PROP_LINK_STRONG
) {
1741 object_ref(new_target
);
1742 object_unref(old_target
);
1746 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1748 LinkProperty
*lprop
= opaque
;
1750 return *lprop
->child
;
1753 static void object_release_link_property(Object
*obj
, const char *name
,
1756 LinkProperty
*prop
= opaque
;
1758 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *prop
->child
) {
1759 object_unref(*prop
->child
);
1764 void object_property_add_link(Object
*obj
, const char *name
,
1765 const char *type
, Object
**child
,
1766 void (*check
)(const Object
*, const char *,
1767 Object
*, Error
**),
1768 ObjectPropertyLinkFlags flags
,
1771 Error
*local_err
= NULL
;
1772 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1776 prop
->child
= child
;
1777 prop
->check
= check
;
1778 prop
->flags
= flags
;
1780 full_type
= g_strdup_printf("link<%s>", type
);
1782 op
= object_property_add(obj
, name
, full_type
,
1783 object_get_link_property
,
1784 check
? object_set_link_property
: NULL
,
1785 object_release_link_property
,
1789 error_propagate(errp
, local_err
);
1794 op
->resolve
= object_resolve_link_property
;
1800 void object_property_add_const_link(Object
*obj
, const char *name
,
1801 Object
*target
, Error
**errp
)
1806 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1807 op
= object_property_add(obj
, name
, link_type
,
1808 object_get_child_property
, NULL
,
1809 NULL
, target
, errp
);
1811 op
->resolve
= object_resolve_child_property
;
1816 gchar
*object_get_canonical_path_component(Object
*obj
)
1818 ObjectProperty
*prop
= NULL
;
1819 GHashTableIter iter
;
1821 if (obj
->parent
== NULL
) {
1825 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1826 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1827 if (!object_property_is_child(prop
)) {
1831 if (prop
->opaque
== obj
) {
1832 return g_strdup(prop
->name
);
1836 /* obj had a parent but was not a child, should never happen */
1837 g_assert_not_reached();
1841 gchar
*object_get_canonical_path(Object
*obj
)
1843 Object
*root
= object_get_root();
1844 char *newpath
, *path
= NULL
;
1847 return g_strdup("/");
1851 char *component
= object_get_canonical_path_component(obj
);
1854 /* A canonical path must be complete, so discard what was
1861 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
1866 } while (obj
!= root
);
1871 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1873 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1878 if (prop
->resolve
) {
1879 return prop
->resolve(parent
, prop
->opaque
, part
);
1885 static Object
*object_resolve_abs_path(Object
*parent
,
1887 const char *typename
,
1892 if (parts
[index
] == NULL
) {
1893 return object_dynamic_cast(parent
, typename
);
1896 if (strcmp(parts
[index
], "") == 0) {
1897 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1900 child
= object_resolve_path_component(parent
, parts
[index
]);
1905 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1908 static Object
*object_resolve_partial_path(Object
*parent
,
1910 const char *typename
,
1914 GHashTableIter iter
;
1915 ObjectProperty
*prop
;
1917 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1919 g_hash_table_iter_init(&iter
, parent
->properties
);
1920 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1923 if (!object_property_is_child(prop
)) {
1927 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1928 typename
, ambiguous
);
1945 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1951 parts
= g_strsplit(path
, "/", 0);
1954 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1955 bool ambiguous
= false;
1956 obj
= object_resolve_partial_path(object_get_root(), parts
,
1957 typename
, &ambiguous
);
1959 *ambiguousp
= ambiguous
;
1962 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1970 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1972 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1975 typedef struct StringProperty
1977 char *(*get
)(Object
*, Error
**);
1978 void (*set
)(Object
*, const char *, Error
**);
1981 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1982 void *opaque
, Error
**errp
)
1984 StringProperty
*prop
= opaque
;
1988 value
= prop
->get(obj
, &err
);
1990 error_propagate(errp
, err
);
1994 visit_type_str(v
, name
, &value
, errp
);
1998 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1999 void *opaque
, Error
**errp
)
2001 StringProperty
*prop
= opaque
;
2003 Error
*local_err
= NULL
;
2005 visit_type_str(v
, name
, &value
, &local_err
);
2007 error_propagate(errp
, local_err
);
2011 prop
->set(obj
, value
, errp
);
2015 static void property_release_str(Object
*obj
, const char *name
,
2018 StringProperty
*prop
= opaque
;
2022 void object_property_add_str(Object
*obj
, const char *name
,
2023 char *(*get
)(Object
*, Error
**),
2024 void (*set
)(Object
*, const char *, Error
**),
2027 Error
*local_err
= NULL
;
2028 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2033 object_property_add(obj
, name
, "string",
2034 get
? property_get_str
: NULL
,
2035 set
? property_set_str
: NULL
,
2036 property_release_str
,
2039 error_propagate(errp
, local_err
);
2044 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2045 char *(*get
)(Object
*, Error
**),
2046 void (*set
)(Object
*, const char *,
2050 Error
*local_err
= NULL
;
2051 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2056 object_class_property_add(klass
, name
, "string",
2057 get
? property_get_str
: NULL
,
2058 set
? property_set_str
: NULL
,
2059 property_release_str
,
2062 error_propagate(errp
, local_err
);
2067 typedef struct BoolProperty
2069 bool (*get
)(Object
*, Error
**);
2070 void (*set
)(Object
*, bool, Error
**);
2073 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2074 void *opaque
, Error
**errp
)
2076 BoolProperty
*prop
= opaque
;
2080 value
= prop
->get(obj
, &err
);
2082 error_propagate(errp
, err
);
2086 visit_type_bool(v
, name
, &value
, errp
);
2089 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2090 void *opaque
, Error
**errp
)
2092 BoolProperty
*prop
= opaque
;
2094 Error
*local_err
= NULL
;
2096 visit_type_bool(v
, name
, &value
, &local_err
);
2098 error_propagate(errp
, local_err
);
2102 prop
->set(obj
, value
, errp
);
2105 static void property_release_bool(Object
*obj
, const char *name
,
2108 BoolProperty
*prop
= opaque
;
2112 void object_property_add_bool(Object
*obj
, const char *name
,
2113 bool (*get
)(Object
*, Error
**),
2114 void (*set
)(Object
*, bool, Error
**),
2117 Error
*local_err
= NULL
;
2118 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2123 object_property_add(obj
, name
, "bool",
2124 get
? property_get_bool
: NULL
,
2125 set
? property_set_bool
: NULL
,
2126 property_release_bool
,
2129 error_propagate(errp
, local_err
);
2134 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2135 bool (*get
)(Object
*, Error
**),
2136 void (*set
)(Object
*, bool, Error
**),
2139 Error
*local_err
= NULL
;
2140 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2145 object_class_property_add(klass
, name
, "bool",
2146 get
? property_get_bool
: NULL
,
2147 set
? property_set_bool
: NULL
,
2148 property_release_bool
,
2151 error_propagate(errp
, local_err
);
2156 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2157 void *opaque
, Error
**errp
)
2159 EnumProperty
*prop
= opaque
;
2163 value
= prop
->get(obj
, &err
);
2165 error_propagate(errp
, err
);
2169 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2172 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2173 void *opaque
, Error
**errp
)
2175 EnumProperty
*prop
= opaque
;
2179 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
2181 error_propagate(errp
, err
);
2184 prop
->set(obj
, value
, errp
);
2187 static void property_release_enum(Object
*obj
, const char *name
,
2190 EnumProperty
*prop
= opaque
;
2194 void object_property_add_enum(Object
*obj
, const char *name
,
2195 const char *typename
,
2196 const QEnumLookup
*lookup
,
2197 int (*get
)(Object
*, Error
**),
2198 void (*set
)(Object
*, int, Error
**),
2201 Error
*local_err
= NULL
;
2202 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2204 prop
->lookup
= lookup
;
2208 object_property_add(obj
, name
, typename
,
2209 get
? property_get_enum
: NULL
,
2210 set
? property_set_enum
: NULL
,
2211 property_release_enum
,
2214 error_propagate(errp
, local_err
);
2219 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2220 const char *typename
,
2221 const QEnumLookup
*lookup
,
2222 int (*get
)(Object
*, Error
**),
2223 void (*set
)(Object
*, int, Error
**),
2226 Error
*local_err
= NULL
;
2227 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2229 prop
->lookup
= lookup
;
2233 object_class_property_add(klass
, name
, typename
,
2234 get
? property_get_enum
: NULL
,
2235 set
? property_set_enum
: NULL
,
2236 property_release_enum
,
2239 error_propagate(errp
, local_err
);
2244 typedef struct TMProperty
{
2245 void (*get
)(Object
*, struct tm
*, Error
**);
2248 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2249 void *opaque
, Error
**errp
)
2251 TMProperty
*prop
= opaque
;
2255 prop
->get(obj
, &value
, &err
);
2260 visit_start_struct(v
, name
, NULL
, 0, &err
);
2264 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2268 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2272 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2276 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2280 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2284 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2288 visit_check_struct(v
, &err
);
2290 visit_end_struct(v
, NULL
);
2292 error_propagate(errp
, err
);
2296 static void property_release_tm(Object
*obj
, const char *name
,
2299 TMProperty
*prop
= opaque
;
2303 void object_property_add_tm(Object
*obj
, const char *name
,
2304 void (*get
)(Object
*, struct tm
*, Error
**),
2307 Error
*local_err
= NULL
;
2308 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2312 object_property_add(obj
, name
, "struct tm",
2313 get
? property_get_tm
: NULL
, NULL
,
2314 property_release_tm
,
2317 error_propagate(errp
, local_err
);
2322 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2323 void (*get
)(Object
*, struct tm
*, Error
**),
2326 Error
*local_err
= NULL
;
2327 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2331 object_class_property_add(klass
, name
, "struct tm",
2332 get
? property_get_tm
: NULL
, NULL
,
2333 property_release_tm
,
2336 error_propagate(errp
, local_err
);
2341 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2343 return g_strdup(object_get_typename(obj
));
2346 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2347 void *opaque
, Error
**errp
)
2349 uint8_t value
= *(uint8_t *)opaque
;
2350 visit_type_uint8(v
, name
, &value
, errp
);
2353 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2354 void *opaque
, Error
**errp
)
2356 uint16_t value
= *(uint16_t *)opaque
;
2357 visit_type_uint16(v
, name
, &value
, errp
);
2360 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2361 void *opaque
, Error
**errp
)
2363 uint32_t value
= *(uint32_t *)opaque
;
2364 visit_type_uint32(v
, name
, &value
, errp
);
2367 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2368 void *opaque
, Error
**errp
)
2370 uint64_t value
= *(uint64_t *)opaque
;
2371 visit_type_uint64(v
, name
, &value
, errp
);
2374 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2375 const uint8_t *v
, Error
**errp
)
2377 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2378 NULL
, NULL
, (void *)v
, errp
);
2381 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2382 const uint8_t *v
, Error
**errp
)
2384 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2385 NULL
, NULL
, (void *)v
, errp
);
2388 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2389 const uint16_t *v
, Error
**errp
)
2391 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2392 NULL
, NULL
, (void *)v
, errp
);
2395 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2396 const uint16_t *v
, Error
**errp
)
2398 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2399 NULL
, NULL
, (void *)v
, errp
);
2402 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2403 const uint32_t *v
, Error
**errp
)
2405 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2406 NULL
, NULL
, (void *)v
, errp
);
2409 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2410 const uint32_t *v
, Error
**errp
)
2412 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2413 NULL
, NULL
, (void *)v
, errp
);
2416 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2417 const uint64_t *v
, Error
**errp
)
2419 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2420 NULL
, NULL
, (void *)v
, errp
);
2423 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2424 const uint64_t *v
, Error
**errp
)
2426 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2427 NULL
, NULL
, (void *)v
, errp
);
2435 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2436 void *opaque
, Error
**errp
)
2438 AliasProperty
*prop
= opaque
;
2440 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2443 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2444 void *opaque
, Error
**errp
)
2446 AliasProperty
*prop
= opaque
;
2448 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2451 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2454 AliasProperty
*prop
= opaque
;
2456 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2459 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2461 AliasProperty
*prop
= opaque
;
2463 g_free(prop
->target_name
);
2467 void object_property_add_alias(Object
*obj
, const char *name
,
2468 Object
*target_obj
, const char *target_name
,
2471 AliasProperty
*prop
;
2473 ObjectProperty
*target_prop
;
2475 Error
*local_err
= NULL
;
2477 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2482 if (object_property_is_child(target_prop
)) {
2483 prop_type
= g_strdup_printf("link%s",
2484 target_prop
->type
+ strlen("child"));
2486 prop_type
= g_strdup(target_prop
->type
);
2489 prop
= g_malloc(sizeof(*prop
));
2490 prop
->target_obj
= target_obj
;
2491 prop
->target_name
= g_strdup(target_name
);
2493 op
= object_property_add(obj
, name
, prop_type
,
2496 property_release_alias
,
2499 error_propagate(errp
, local_err
);
2503 op
->resolve
= property_resolve_alias
;
2505 object_property_set_description(obj
, op
->name
,
2506 target_prop
->description
,
2513 void object_property_set_description(Object
*obj
, const char *name
,
2514 const char *description
, Error
**errp
)
2518 op
= object_property_find(obj
, name
, errp
);
2523 g_free(op
->description
);
2524 op
->description
= g_strdup(description
);
2527 void object_class_property_set_description(ObjectClass
*klass
,
2529 const char *description
,
2534 op
= g_hash_table_lookup(klass
->properties
, name
);
2536 error_setg(errp
, "Property '.%s' not found", name
);
2540 g_free(op
->description
);
2541 op
->description
= g_strdup(description
);
2544 static void object_class_init(ObjectClass
*klass
, void *data
)
2546 object_class_property_add_str(klass
, "type", qdev_get_type
,
2547 NULL
, &error_abort
);
2550 static void register_types(void)
2552 static TypeInfo interface_info
= {
2553 .name
= TYPE_INTERFACE
,
2554 .class_size
= sizeof(InterfaceClass
),
2558 static TypeInfo object_info
= {
2559 .name
= TYPE_OBJECT
,
2560 .instance_size
= sizeof(Object
),
2561 .class_init
= object_class_init
,
2565 type_interface
= type_register_internal(&interface_info
);
2566 type_register_internal(&object_info
);
2569 type_init(register_types
)