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 * Each is a GPtrArray of of GlobalProperty.
418 * Applied in order, later entries override earlier ones.
420 static GPtrArray
*object_compat_props
[2];
423 * Set machine's global property defaults to @compat_props.
424 * May be called at most once.
426 void object_set_machine_compat_props(GPtrArray
*compat_props
)
428 assert(!object_compat_props
[1]);
429 object_compat_props
[1] = compat_props
;
433 * Set accelerator's global property defaults to @compat_props.
434 * May be called at most once.
436 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
438 assert(!object_compat_props
[0]);
439 object_compat_props
[0] = compat_props
;
442 void object_apply_compat_props(Object
*obj
)
446 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
447 object_apply_global_props(obj
, object_compat_props
[i
],
452 static void object_initialize_with_type(void *data
, size_t size
, TypeImpl
*type
)
456 type_initialize(type
);
458 g_assert(type
->instance_size
>= sizeof(Object
));
459 g_assert(type
->abstract
== false);
460 g_assert(size
>= type
->instance_size
);
462 memset(obj
, 0, type
->instance_size
);
463 obj
->class = type
->class;
465 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
466 NULL
, object_property_free
);
467 object_init_with_type(obj
, type
);
468 object_post_init_with_type(obj
, type
);
471 void object_initialize(void *data
, size_t size
, const char *typename
)
473 TypeImpl
*type
= type_get_by_name(typename
);
476 error_report("missing object type '%s'", typename
);
480 object_initialize_with_type(data
, size
, type
);
483 void object_initialize_child(Object
*parentobj
, const char *propname
,
484 void *childobj
, size_t size
, const char *type
,
489 va_start(vargs
, errp
);
490 object_initialize_childv(parentobj
, propname
, childobj
, size
, type
, errp
,
495 void object_initialize_childv(Object
*parentobj
, const char *propname
,
496 void *childobj
, size_t size
, const char *type
,
497 Error
**errp
, va_list vargs
)
499 Error
*local_err
= NULL
;
503 object_initialize(childobj
, size
, type
);
504 obj
= OBJECT(childobj
);
506 object_set_propv(obj
, &local_err
, vargs
);
511 object_property_add_child(parentobj
, propname
, obj
, &local_err
);
516 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
518 user_creatable_complete(uc
, &local_err
);
520 object_unparent(obj
);
526 * Since object_property_add_child added a reference to the child object,
527 * we can drop the reference added by object_initialize(), so the child
528 * property will own the only reference to the object.
534 error_propagate(errp
, local_err
);
539 static inline bool object_property_is_child(ObjectProperty
*prop
)
541 return strstart(prop
->type
, "child<", NULL
);
544 static void object_property_del_all(Object
*obj
)
546 ObjectProperty
*prop
;
553 g_hash_table_iter_init(&iter
, obj
->properties
);
554 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
557 prop
->release(obj
, prop
->name
, prop
->opaque
);
558 prop
->release
= NULL
;
562 g_hash_table_iter_remove(&iter
);
566 g_hash_table_unref(obj
->properties
);
569 static void object_property_del_child(Object
*obj
, Object
*child
, Error
**errp
)
571 ObjectProperty
*prop
;
575 g_hash_table_iter_init(&iter
, obj
->properties
);
576 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
578 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
580 prop
->release(obj
, prop
->name
, prop
->opaque
);
581 prop
->release
= NULL
;
586 g_hash_table_iter_init(&iter
, obj
->properties
);
587 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
589 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
590 g_hash_table_iter_remove(&iter
);
596 void object_unparent(Object
*obj
)
599 object_property_del_child(obj
->parent
, obj
, NULL
);
603 static void object_deinit(Object
*obj
, TypeImpl
*type
)
605 if (type
->instance_finalize
) {
606 type
->instance_finalize(obj
);
609 if (type_has_parent(type
)) {
610 object_deinit(obj
, type_get_parent(type
));
614 static void object_finalize(void *data
)
617 TypeImpl
*ti
= obj
->class->type
;
619 object_property_del_all(obj
);
620 object_deinit(obj
, ti
);
622 g_assert(obj
->ref
== 0);
628 static Object
*object_new_with_type(Type type
)
632 g_assert(type
!= NULL
);
633 type_initialize(type
);
635 obj
= g_malloc(type
->instance_size
);
636 object_initialize_with_type(obj
, type
->instance_size
, type
);
642 Object
*object_new(const char *typename
)
644 TypeImpl
*ti
= type_get_by_name(typename
);
646 return object_new_with_type(ti
);
650 Object
*object_new_with_props(const char *typename
,
659 va_start(vargs
, errp
);
660 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
667 Object
*object_new_with_propv(const char *typename
,
675 Error
*local_err
= NULL
;
678 klass
= object_class_by_name(typename
);
680 error_setg(errp
, "invalid object type: %s", typename
);
684 if (object_class_is_abstract(klass
)) {
685 error_setg(errp
, "object type '%s' is abstract", typename
);
688 obj
= object_new_with_type(klass
->type
);
690 if (object_set_propv(obj
, &local_err
, vargs
) < 0) {
695 object_property_add_child(parent
, id
, obj
, &local_err
);
701 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
703 user_creatable_complete(uc
, &local_err
);
706 object_unparent(obj
);
712 object_unref(OBJECT(obj
));
716 error_propagate(errp
, local_err
);
722 int object_set_props(Object
*obj
,
729 va_start(vargs
, errp
);
730 ret
= object_set_propv(obj
, errp
, vargs
);
737 int object_set_propv(Object
*obj
,
741 const char *propname
;
742 Error
*local_err
= NULL
;
744 propname
= va_arg(vargs
, char *);
745 while (propname
!= NULL
) {
746 const char *value
= va_arg(vargs
, char *);
748 g_assert(value
!= NULL
);
749 object_property_parse(obj
, value
, propname
, &local_err
);
751 error_propagate(errp
, local_err
);
754 propname
= va_arg(vargs
, char *);
761 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
763 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
770 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
771 const char *file
, int line
, const char *func
)
773 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
774 typename
, file
, line
, func
);
776 #ifdef CONFIG_QOM_CAST_DEBUG
780 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
781 if (atomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
786 inst
= object_dynamic_cast(obj
, typename
);
789 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
790 file
, line
, func
, obj
, typename
);
796 if (obj
&& obj
== inst
) {
797 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
798 atomic_set(&obj
->class->object_cast_cache
[i
- 1],
799 atomic_read(&obj
->class->object_cast_cache
[i
]));
801 atomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
809 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
810 const char *typename
)
812 ObjectClass
*ret
= NULL
;
813 TypeImpl
*target_type
;
820 /* A simple fast path that can trigger a lot for leaf classes. */
822 if (type
->name
== typename
) {
826 target_type
= type_get_by_name(typename
);
828 /* target class type unknown, so fail the cast */
832 if (type
->class->interfaces
&&
833 type_is_ancestor(target_type
, type_interface
)) {
837 for (i
= class->interfaces
; i
; i
= i
->next
) {
838 ObjectClass
*target_class
= i
->data
;
840 if (type_is_ancestor(target_class
->type
, target_type
)) {
846 /* The match was ambiguous, don't allow a cast */
850 } else if (type_is_ancestor(type
, target_type
)) {
857 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
858 const char *typename
,
859 const char *file
, int line
,
864 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
865 typename
, file
, line
, func
);
867 #ifdef CONFIG_QOM_CAST_DEBUG
870 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
871 if (atomic_read(&class->class_cast_cache
[i
]) == typename
) {
877 if (!class || !class->interfaces
) {
882 ret
= object_class_dynamic_cast(class, typename
);
884 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
885 file
, line
, func
, class, typename
);
889 #ifdef CONFIG_QOM_CAST_DEBUG
890 if (class && ret
== class) {
891 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
892 atomic_set(&class->class_cast_cache
[i
- 1],
893 atomic_read(&class->class_cast_cache
[i
]));
895 atomic_set(&class->class_cast_cache
[i
- 1], typename
);
902 const char *object_get_typename(const Object
*obj
)
904 return obj
->class->type
->name
;
907 ObjectClass
*object_get_class(Object
*obj
)
912 bool object_class_is_abstract(ObjectClass
*klass
)
914 return klass
->type
->abstract
;
917 const char *object_class_get_name(ObjectClass
*klass
)
919 return klass
->type
->name
;
922 ObjectClass
*object_class_by_name(const char *typename
)
924 TypeImpl
*type
= type_get_by_name(typename
);
930 type_initialize(type
);
935 ObjectClass
*object_class_get_parent(ObjectClass
*class)
937 TypeImpl
*type
= type_get_parent(class->type
);
943 type_initialize(type
);
948 typedef struct OCFData
950 void (*fn
)(ObjectClass
*klass
, void *opaque
);
951 const char *implements_type
;
952 bool include_abstract
;
956 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
959 OCFData
*data
= opaque
;
960 TypeImpl
*type
= value
;
963 type_initialize(type
);
966 if (!data
->include_abstract
&& type
->abstract
) {
970 if (data
->implements_type
&&
971 !object_class_dynamic_cast(k
, data
->implements_type
)) {
975 data
->fn(k
, data
->opaque
);
978 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
979 const char *implements_type
, bool include_abstract
,
982 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
984 enumerating_types
= true;
985 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
986 enumerating_types
= false;
989 static int do_object_child_foreach(Object
*obj
,
990 int (*fn
)(Object
*child
, void *opaque
),
991 void *opaque
, bool recurse
)
994 ObjectProperty
*prop
;
997 g_hash_table_iter_init(&iter
, obj
->properties
);
998 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
999 if (object_property_is_child(prop
)) {
1000 Object
*child
= prop
->opaque
;
1002 ret
= fn(child
, opaque
);
1007 do_object_child_foreach(child
, fn
, opaque
, true);
1014 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1017 return do_object_child_foreach(obj
, fn
, opaque
, false);
1020 int object_child_foreach_recursive(Object
*obj
,
1021 int (*fn
)(Object
*child
, void *opaque
),
1024 return do_object_child_foreach(obj
, fn
, opaque
, true);
1027 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1029 GSList
**list
= opaque
;
1031 *list
= g_slist_prepend(*list
, klass
);
1034 GSList
*object_class_get_list(const char *implements_type
,
1035 bool include_abstract
)
1037 GSList
*list
= NULL
;
1039 object_class_foreach(object_class_get_list_tramp
,
1040 implements_type
, include_abstract
, &list
);
1044 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1046 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1047 object_class_get_name((ObjectClass
*)b
));
1050 GSList
*object_class_get_list_sorted(const char *implements_type
,
1051 bool include_abstract
)
1053 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1057 void object_ref(Object
*obj
)
1062 atomic_inc(&obj
->ref
);
1065 void object_unref(Object
*obj
)
1070 g_assert(obj
->ref
> 0);
1072 /* parent always holds a reference to its children */
1073 if (atomic_fetch_dec(&obj
->ref
) == 1) {
1074 object_finalize(obj
);
1079 object_property_add(Object
*obj
, const char *name
, const char *type
,
1080 ObjectPropertyAccessor
*get
,
1081 ObjectPropertyAccessor
*set
,
1082 ObjectPropertyRelease
*release
,
1083 void *opaque
, Error
**errp
)
1085 ObjectProperty
*prop
;
1086 size_t name_len
= strlen(name
);
1088 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1090 ObjectProperty
*ret
;
1091 char *name_no_array
= g_strdup(name
);
1093 name_no_array
[name_len
- 3] = '\0';
1094 for (i
= 0; ; ++i
) {
1095 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1097 ret
= object_property_add(obj
, full_name
, type
, get
, set
,
1098 release
, opaque
, NULL
);
1104 g_free(name_no_array
);
1108 if (object_property_find(obj
, name
, NULL
) != NULL
) {
1109 error_setg(errp
, "attempt to add duplicate property '%s'"
1110 " to object (type '%s')", name
,
1111 object_get_typename(obj
));
1115 prop
= g_malloc0(sizeof(*prop
));
1117 prop
->name
= g_strdup(name
);
1118 prop
->type
= g_strdup(type
);
1122 prop
->release
= release
;
1123 prop
->opaque
= opaque
;
1125 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1130 object_class_property_add(ObjectClass
*klass
,
1133 ObjectPropertyAccessor
*get
,
1134 ObjectPropertyAccessor
*set
,
1135 ObjectPropertyRelease
*release
,
1139 ObjectProperty
*prop
;
1141 if (object_class_property_find(klass
, name
, NULL
) != NULL
) {
1142 error_setg(errp
, "attempt to add duplicate property '%s'"
1143 " to object (type '%s')", name
,
1144 object_class_get_name(klass
));
1148 prop
= g_malloc0(sizeof(*prop
));
1150 prop
->name
= g_strdup(name
);
1151 prop
->type
= g_strdup(type
);
1155 prop
->release
= release
;
1156 prop
->opaque
= opaque
;
1158 g_hash_table_insert(klass
->properties
, g_strdup(name
), prop
);
1163 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1166 ObjectProperty
*prop
;
1167 ObjectClass
*klass
= object_get_class(obj
);
1169 prop
= object_class_property_find(klass
, name
, NULL
);
1174 prop
= g_hash_table_lookup(obj
->properties
, name
);
1179 error_setg(errp
, "Property '.%s' not found", name
);
1183 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1186 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1187 iter
->nextclass
= object_get_class(obj
);
1190 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1193 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1194 if (!iter
->nextclass
) {
1197 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1198 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1203 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1206 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1207 iter
->nextclass
= object_class_get_parent(klass
);
1210 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1213 ObjectProperty
*prop
;
1214 ObjectClass
*parent_klass
;
1216 parent_klass
= object_class_get_parent(klass
);
1218 prop
= object_class_property_find(parent_klass
, name
, NULL
);
1224 prop
= g_hash_table_lookup(klass
->properties
, name
);
1226 error_setg(errp
, "Property '.%s' not found", name
);
1231 void object_property_del(Object
*obj
, const char *name
, Error
**errp
)
1233 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1236 error_setg(errp
, "Property '.%s' not found", name
);
1240 if (prop
->release
) {
1241 prop
->release(obj
, name
, prop
->opaque
);
1243 g_hash_table_remove(obj
->properties
, name
);
1246 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1249 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1255 error_setg(errp
, QERR_PERMISSION_DENIED
);
1257 prop
->get(obj
, v
, name
, prop
->opaque
, errp
);
1261 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1264 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1270 error_setg(errp
, QERR_PERMISSION_DENIED
);
1272 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1276 void object_property_set_str(Object
*obj
, const char *value
,
1277 const char *name
, Error
**errp
)
1279 QString
*qstr
= qstring_from_str(value
);
1280 object_property_set_qobject(obj
, QOBJECT(qstr
), name
, errp
);
1282 qobject_unref(qstr
);
1285 char *object_property_get_str(Object
*obj
, const char *name
,
1288 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1295 retval
= g_strdup(qobject_get_try_str(ret
));
1297 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1304 void object_property_set_link(Object
*obj
, Object
*value
,
1305 const char *name
, Error
**errp
)
1308 gchar
*path
= object_get_canonical_path(value
);
1309 object_property_set_str(obj
, path
, name
, errp
);
1312 object_property_set_str(obj
, "", name
, errp
);
1316 Object
*object_property_get_link(Object
*obj
, const char *name
,
1319 char *str
= object_property_get_str(obj
, name
, errp
);
1320 Object
*target
= NULL
;
1323 target
= object_resolve_path(str
, NULL
);
1325 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1326 "Device '%s' not found", str
);
1334 void object_property_set_bool(Object
*obj
, bool value
,
1335 const char *name
, Error
**errp
)
1337 QBool
*qbool
= qbool_from_bool(value
);
1338 object_property_set_qobject(obj
, QOBJECT(qbool
), name
, errp
);
1340 qobject_unref(qbool
);
1343 bool object_property_get_bool(Object
*obj
, const char *name
,
1346 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1353 qbool
= qobject_to(QBool
, ret
);
1355 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1358 retval
= qbool_get_bool(qbool
);
1365 void object_property_set_int(Object
*obj
, int64_t value
,
1366 const char *name
, Error
**errp
)
1368 QNum
*qnum
= qnum_from_int(value
);
1369 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1371 qobject_unref(qnum
);
1374 int64_t object_property_get_int(Object
*obj
, const char *name
,
1377 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1385 qnum
= qobject_to(QNum
, ret
);
1386 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1387 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1395 void object_property_set_uint(Object
*obj
, uint64_t value
,
1396 const char *name
, Error
**errp
)
1398 QNum
*qnum
= qnum_from_uint(value
);
1400 object_property_set_qobject(obj
, QOBJECT(qnum
), name
, errp
);
1401 qobject_unref(qnum
);
1404 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1407 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1414 qnum
= qobject_to(QNum
, ret
);
1415 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1416 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1424 typedef struct EnumProperty
{
1425 const QEnumLookup
*lookup
;
1426 int (*get
)(Object
*, Error
**);
1427 void (*set
)(Object
*, int, Error
**);
1430 int object_property_get_enum(Object
*obj
, const char *name
,
1431 const char *typename
, Error
**errp
)
1437 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1438 EnumProperty
*enumprop
;
1444 if (!g_str_equal(prop
->type
, typename
)) {
1445 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1446 name
, object_class_get_name(
1447 object_get_class(obj
)), typename
);
1451 enumprop
= prop
->opaque
;
1453 v
= string_output_visitor_new(false, &str
);
1454 object_property_get(obj
, v
, name
, &err
);
1456 error_propagate(errp
, err
);
1460 visit_complete(v
, &str
);
1462 v
= string_input_visitor_new(str
);
1463 visit_type_enum(v
, name
, &ret
, enumprop
->lookup
, errp
);
1471 void object_property_get_uint16List(Object
*obj
, const char *name
,
1472 uint16List
**list
, Error
**errp
)
1478 v
= string_output_visitor_new(false, &str
);
1479 object_property_get(obj
, v
, name
, &err
);
1481 error_propagate(errp
, err
);
1484 visit_complete(v
, &str
);
1486 v
= string_input_visitor_new(str
);
1487 visit_type_uint16List(v
, NULL
, list
, errp
);
1494 void object_property_parse(Object
*obj
, const char *string
,
1495 const char *name
, Error
**errp
)
1497 Visitor
*v
= string_input_visitor_new(string
);
1498 object_property_set(obj
, v
, name
, errp
);
1502 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1506 char *string
= NULL
;
1507 Error
*local_err
= NULL
;
1509 v
= string_output_visitor_new(human
, &string
);
1510 object_property_get(obj
, v
, name
, &local_err
);
1512 error_propagate(errp
, local_err
);
1516 visit_complete(v
, &string
);
1523 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1525 ObjectProperty
*prop
= object_property_find(obj
, name
, errp
);
1533 Object
*object_get_root(void)
1535 static Object
*root
;
1538 root
= object_new("container");
1544 Object
*object_get_objects_root(void)
1546 return container_get(object_get_root(), "/objects");
1549 Object
*object_get_internal_root(void)
1551 static Object
*internal_root
;
1553 if (!internal_root
) {
1554 internal_root
= object_new("container");
1557 return internal_root
;
1560 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1561 const char *name
, void *opaque
,
1564 Object
*child
= opaque
;
1567 path
= object_get_canonical_path(child
);
1568 visit_type_str(v
, name
, &path
, errp
);
1572 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
, const gchar
*part
)
1577 static void object_finalize_child_property(Object
*obj
, const char *name
,
1580 Object
*child
= opaque
;
1582 if (child
->class->unparent
) {
1583 (child
->class->unparent
)(child
);
1585 child
->parent
= NULL
;
1586 object_unref(child
);
1589 void object_property_add_child(Object
*obj
, const char *name
,
1590 Object
*child
, Error
**errp
)
1592 Error
*local_err
= NULL
;
1596 if (child
->parent
!= NULL
) {
1597 error_setg(errp
, "child object is already parented");
1601 type
= g_strdup_printf("child<%s>", object_get_typename(OBJECT(child
)));
1603 op
= object_property_add(obj
, name
, type
, object_get_child_property
, NULL
,
1604 object_finalize_child_property
, child
, &local_err
);
1606 error_propagate(errp
, local_err
);
1610 op
->resolve
= object_resolve_child_property
;
1612 child
->parent
= obj
;
1618 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1619 Object
*val
, Error
**errp
)
1621 /* Allow the link to be set, always */
1626 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1627 ObjectPropertyLinkFlags flags
;
1630 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1631 const char *name
, void *opaque
,
1634 LinkProperty
*lprop
= opaque
;
1635 Object
**child
= lprop
->child
;
1639 path
= object_get_canonical_path(*child
);
1640 visit_type_str(v
, name
, &path
, errp
);
1644 visit_type_str(v
, name
, &path
, errp
);
1649 * object_resolve_link:
1651 * Lookup an object and ensure its type matches the link property type. This
1652 * is similar to object_resolve_path() except type verification against the
1653 * link property is performed.
1655 * Returns: The matched object or NULL on path lookup failures.
1657 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1658 const char *path
, Error
**errp
)
1662 bool ambiguous
= false;
1665 /* Go from link<FOO> to FOO. */
1666 type
= object_property_get_type(obj
, name
, NULL
);
1667 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1668 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1671 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1673 } else if (!target
) {
1674 target
= object_resolve_path(path
, &ambiguous
);
1675 if (target
|| ambiguous
) {
1676 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1678 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1679 "Device '%s' not found", path
);
1683 g_free(target_type
);
1688 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1689 const char *name
, void *opaque
,
1692 Error
*local_err
= NULL
;
1693 LinkProperty
*prop
= opaque
;
1694 Object
**child
= prop
->child
;
1695 Object
*old_target
= *child
;
1696 Object
*new_target
= NULL
;
1699 visit_type_str(v
, name
, &path
, &local_err
);
1701 if (!local_err
&& strcmp(path
, "") != 0) {
1702 new_target
= object_resolve_link(obj
, name
, path
, &local_err
);
1707 error_propagate(errp
, local_err
);
1711 prop
->check(obj
, name
, new_target
, &local_err
);
1713 error_propagate(errp
, local_err
);
1717 *child
= new_target
;
1718 if (prop
->flags
== OBJ_PROP_LINK_STRONG
) {
1719 object_ref(new_target
);
1720 object_unref(old_target
);
1724 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
, const gchar
*part
)
1726 LinkProperty
*lprop
= opaque
;
1728 return *lprop
->child
;
1731 static void object_release_link_property(Object
*obj
, const char *name
,
1734 LinkProperty
*prop
= opaque
;
1736 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *prop
->child
) {
1737 object_unref(*prop
->child
);
1742 void object_property_add_link(Object
*obj
, const char *name
,
1743 const char *type
, Object
**child
,
1744 void (*check
)(const Object
*, const char *,
1745 Object
*, Error
**),
1746 ObjectPropertyLinkFlags flags
,
1749 Error
*local_err
= NULL
;
1750 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1754 prop
->child
= child
;
1755 prop
->check
= check
;
1756 prop
->flags
= flags
;
1758 full_type
= g_strdup_printf("link<%s>", type
);
1760 op
= object_property_add(obj
, name
, full_type
,
1761 object_get_link_property
,
1762 check
? object_set_link_property
: NULL
,
1763 object_release_link_property
,
1767 error_propagate(errp
, local_err
);
1772 op
->resolve
= object_resolve_link_property
;
1778 void object_property_add_const_link(Object
*obj
, const char *name
,
1779 Object
*target
, Error
**errp
)
1784 link_type
= g_strdup_printf("link<%s>", object_get_typename(target
));
1785 op
= object_property_add(obj
, name
, link_type
,
1786 object_get_child_property
, NULL
,
1787 NULL
, target
, errp
);
1789 op
->resolve
= object_resolve_child_property
;
1794 gchar
*object_get_canonical_path_component(Object
*obj
)
1796 ObjectProperty
*prop
= NULL
;
1797 GHashTableIter iter
;
1799 if (obj
->parent
== NULL
) {
1803 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
1804 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1805 if (!object_property_is_child(prop
)) {
1809 if (prop
->opaque
== obj
) {
1810 return g_strdup(prop
->name
);
1814 /* obj had a parent but was not a child, should never happen */
1815 g_assert_not_reached();
1819 gchar
*object_get_canonical_path(Object
*obj
)
1821 Object
*root
= object_get_root();
1822 char *newpath
, *path
= NULL
;
1825 return g_strdup("/");
1829 char *component
= object_get_canonical_path_component(obj
);
1832 /* A canonical path must be complete, so discard what was
1839 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
1844 } while (obj
!= root
);
1849 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
)
1851 ObjectProperty
*prop
= object_property_find(parent
, part
, NULL
);
1856 if (prop
->resolve
) {
1857 return prop
->resolve(parent
, prop
->opaque
, part
);
1863 static Object
*object_resolve_abs_path(Object
*parent
,
1865 const char *typename
,
1870 if (parts
[index
] == NULL
) {
1871 return object_dynamic_cast(parent
, typename
);
1874 if (strcmp(parts
[index
], "") == 0) {
1875 return object_resolve_abs_path(parent
, parts
, typename
, index
+ 1);
1878 child
= object_resolve_path_component(parent
, parts
[index
]);
1883 return object_resolve_abs_path(child
, parts
, typename
, index
+ 1);
1886 static Object
*object_resolve_partial_path(Object
*parent
,
1888 const char *typename
,
1892 GHashTableIter iter
;
1893 ObjectProperty
*prop
;
1895 obj
= object_resolve_abs_path(parent
, parts
, typename
, 0);
1897 g_hash_table_iter_init(&iter
, parent
->properties
);
1898 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1901 if (!object_property_is_child(prop
)) {
1905 found
= object_resolve_partial_path(prop
->opaque
, parts
,
1906 typename
, ambiguous
);
1923 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1929 parts
= g_strsplit(path
, "/", 0);
1932 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
1933 bool ambiguous
= false;
1934 obj
= object_resolve_partial_path(object_get_root(), parts
,
1935 typename
, &ambiguous
);
1937 *ambiguousp
= ambiguous
;
1940 obj
= object_resolve_abs_path(object_get_root(), parts
, typename
, 1);
1948 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
1950 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
1953 typedef struct StringProperty
1955 char *(*get
)(Object
*, Error
**);
1956 void (*set
)(Object
*, const char *, Error
**);
1959 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
1960 void *opaque
, Error
**errp
)
1962 StringProperty
*prop
= opaque
;
1966 value
= prop
->get(obj
, &err
);
1968 error_propagate(errp
, err
);
1972 visit_type_str(v
, name
, &value
, errp
);
1976 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
1977 void *opaque
, Error
**errp
)
1979 StringProperty
*prop
= opaque
;
1981 Error
*local_err
= NULL
;
1983 visit_type_str(v
, name
, &value
, &local_err
);
1985 error_propagate(errp
, local_err
);
1989 prop
->set(obj
, value
, errp
);
1993 static void property_release_str(Object
*obj
, const char *name
,
1996 StringProperty
*prop
= opaque
;
2000 void object_property_add_str(Object
*obj
, const char *name
,
2001 char *(*get
)(Object
*, Error
**),
2002 void (*set
)(Object
*, const char *, Error
**),
2005 Error
*local_err
= NULL
;
2006 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2011 object_property_add(obj
, name
, "string",
2012 get
? property_get_str
: NULL
,
2013 set
? property_set_str
: NULL
,
2014 property_release_str
,
2017 error_propagate(errp
, local_err
);
2022 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2023 char *(*get
)(Object
*, Error
**),
2024 void (*set
)(Object
*, const char *,
2028 Error
*local_err
= NULL
;
2029 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2034 object_class_property_add(klass
, name
, "string",
2035 get
? property_get_str
: NULL
,
2036 set
? property_set_str
: NULL
,
2037 property_release_str
,
2040 error_propagate(errp
, local_err
);
2045 typedef struct BoolProperty
2047 bool (*get
)(Object
*, Error
**);
2048 void (*set
)(Object
*, bool, Error
**);
2051 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2052 void *opaque
, Error
**errp
)
2054 BoolProperty
*prop
= opaque
;
2058 value
= prop
->get(obj
, &err
);
2060 error_propagate(errp
, err
);
2064 visit_type_bool(v
, name
, &value
, errp
);
2067 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2068 void *opaque
, Error
**errp
)
2070 BoolProperty
*prop
= opaque
;
2072 Error
*local_err
= NULL
;
2074 visit_type_bool(v
, name
, &value
, &local_err
);
2076 error_propagate(errp
, local_err
);
2080 prop
->set(obj
, value
, errp
);
2083 static void property_release_bool(Object
*obj
, const char *name
,
2086 BoolProperty
*prop
= opaque
;
2090 void object_property_add_bool(Object
*obj
, const char *name
,
2091 bool (*get
)(Object
*, Error
**),
2092 void (*set
)(Object
*, bool, Error
**),
2095 Error
*local_err
= NULL
;
2096 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2101 object_property_add(obj
, name
, "bool",
2102 get
? property_get_bool
: NULL
,
2103 set
? property_set_bool
: NULL
,
2104 property_release_bool
,
2107 error_propagate(errp
, local_err
);
2112 void object_class_property_add_bool(ObjectClass
*klass
, 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_class_property_add(klass
, 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 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2135 void *opaque
, Error
**errp
)
2137 EnumProperty
*prop
= opaque
;
2141 value
= prop
->get(obj
, &err
);
2143 error_propagate(errp
, err
);
2147 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2150 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2151 void *opaque
, Error
**errp
)
2153 EnumProperty
*prop
= opaque
;
2157 visit_type_enum(v
, name
, &value
, prop
->lookup
, &err
);
2159 error_propagate(errp
, err
);
2162 prop
->set(obj
, value
, errp
);
2165 static void property_release_enum(Object
*obj
, const char *name
,
2168 EnumProperty
*prop
= opaque
;
2172 void object_property_add_enum(Object
*obj
, const char *name
,
2173 const char *typename
,
2174 const QEnumLookup
*lookup
,
2175 int (*get
)(Object
*, Error
**),
2176 void (*set
)(Object
*, int, Error
**),
2179 Error
*local_err
= NULL
;
2180 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2182 prop
->lookup
= lookup
;
2186 object_property_add(obj
, name
, typename
,
2187 get
? property_get_enum
: NULL
,
2188 set
? property_set_enum
: NULL
,
2189 property_release_enum
,
2192 error_propagate(errp
, local_err
);
2197 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2198 const char *typename
,
2199 const QEnumLookup
*lookup
,
2200 int (*get
)(Object
*, Error
**),
2201 void (*set
)(Object
*, int, Error
**),
2204 Error
*local_err
= NULL
;
2205 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2207 prop
->lookup
= lookup
;
2211 object_class_property_add(klass
, name
, typename
,
2212 get
? property_get_enum
: NULL
,
2213 set
? property_set_enum
: NULL
,
2214 property_release_enum
,
2217 error_propagate(errp
, local_err
);
2222 typedef struct TMProperty
{
2223 void (*get
)(Object
*, struct tm
*, Error
**);
2226 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2227 void *opaque
, Error
**errp
)
2229 TMProperty
*prop
= opaque
;
2233 prop
->get(obj
, &value
, &err
);
2238 visit_start_struct(v
, name
, NULL
, 0, &err
);
2242 visit_type_int32(v
, "tm_year", &value
.tm_year
, &err
);
2246 visit_type_int32(v
, "tm_mon", &value
.tm_mon
, &err
);
2250 visit_type_int32(v
, "tm_mday", &value
.tm_mday
, &err
);
2254 visit_type_int32(v
, "tm_hour", &value
.tm_hour
, &err
);
2258 visit_type_int32(v
, "tm_min", &value
.tm_min
, &err
);
2262 visit_type_int32(v
, "tm_sec", &value
.tm_sec
, &err
);
2266 visit_check_struct(v
, &err
);
2268 visit_end_struct(v
, NULL
);
2270 error_propagate(errp
, err
);
2274 static void property_release_tm(Object
*obj
, const char *name
,
2277 TMProperty
*prop
= opaque
;
2281 void object_property_add_tm(Object
*obj
, const char *name
,
2282 void (*get
)(Object
*, struct tm
*, Error
**),
2285 Error
*local_err
= NULL
;
2286 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2290 object_property_add(obj
, name
, "struct tm",
2291 get
? property_get_tm
: NULL
, NULL
,
2292 property_release_tm
,
2295 error_propagate(errp
, local_err
);
2300 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2301 void (*get
)(Object
*, struct tm
*, Error
**),
2304 Error
*local_err
= NULL
;
2305 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2309 object_class_property_add(klass
, name
, "struct tm",
2310 get
? property_get_tm
: NULL
, NULL
,
2311 property_release_tm
,
2314 error_propagate(errp
, local_err
);
2319 static char *qdev_get_type(Object
*obj
, Error
**errp
)
2321 return g_strdup(object_get_typename(obj
));
2324 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2325 void *opaque
, Error
**errp
)
2327 uint8_t value
= *(uint8_t *)opaque
;
2328 visit_type_uint8(v
, name
, &value
, errp
);
2331 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2332 void *opaque
, Error
**errp
)
2334 uint16_t value
= *(uint16_t *)opaque
;
2335 visit_type_uint16(v
, name
, &value
, errp
);
2338 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2339 void *opaque
, Error
**errp
)
2341 uint32_t value
= *(uint32_t *)opaque
;
2342 visit_type_uint32(v
, name
, &value
, errp
);
2345 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2346 void *opaque
, Error
**errp
)
2348 uint64_t value
= *(uint64_t *)opaque
;
2349 visit_type_uint64(v
, name
, &value
, errp
);
2352 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2353 const uint8_t *v
, Error
**errp
)
2355 object_property_add(obj
, name
, "uint8", property_get_uint8_ptr
,
2356 NULL
, NULL
, (void *)v
, errp
);
2359 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2360 const uint8_t *v
, Error
**errp
)
2362 object_class_property_add(klass
, name
, "uint8", property_get_uint8_ptr
,
2363 NULL
, NULL
, (void *)v
, errp
);
2366 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2367 const uint16_t *v
, Error
**errp
)
2369 object_property_add(obj
, name
, "uint16", property_get_uint16_ptr
,
2370 NULL
, NULL
, (void *)v
, errp
);
2373 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2374 const uint16_t *v
, Error
**errp
)
2376 object_class_property_add(klass
, name
, "uint16", property_get_uint16_ptr
,
2377 NULL
, NULL
, (void *)v
, errp
);
2380 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2381 const uint32_t *v
, Error
**errp
)
2383 object_property_add(obj
, name
, "uint32", property_get_uint32_ptr
,
2384 NULL
, NULL
, (void *)v
, errp
);
2387 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2388 const uint32_t *v
, Error
**errp
)
2390 object_class_property_add(klass
, name
, "uint32", property_get_uint32_ptr
,
2391 NULL
, NULL
, (void *)v
, errp
);
2394 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2395 const uint64_t *v
, Error
**errp
)
2397 object_property_add(obj
, name
, "uint64", property_get_uint64_ptr
,
2398 NULL
, NULL
, (void *)v
, errp
);
2401 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2402 const uint64_t *v
, Error
**errp
)
2404 object_class_property_add(klass
, name
, "uint64", property_get_uint64_ptr
,
2405 NULL
, NULL
, (void *)v
, errp
);
2413 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2414 void *opaque
, Error
**errp
)
2416 AliasProperty
*prop
= opaque
;
2418 object_property_get(prop
->target_obj
, v
, prop
->target_name
, errp
);
2421 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2422 void *opaque
, Error
**errp
)
2424 AliasProperty
*prop
= opaque
;
2426 object_property_set(prop
->target_obj
, v
, prop
->target_name
, errp
);
2429 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2432 AliasProperty
*prop
= opaque
;
2434 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2437 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2439 AliasProperty
*prop
= opaque
;
2441 g_free(prop
->target_name
);
2445 void object_property_add_alias(Object
*obj
, const char *name
,
2446 Object
*target_obj
, const char *target_name
,
2449 AliasProperty
*prop
;
2451 ObjectProperty
*target_prop
;
2453 Error
*local_err
= NULL
;
2455 target_prop
= object_property_find(target_obj
, target_name
, errp
);
2460 if (object_property_is_child(target_prop
)) {
2461 prop_type
= g_strdup_printf("link%s",
2462 target_prop
->type
+ strlen("child"));
2464 prop_type
= g_strdup(target_prop
->type
);
2467 prop
= g_malloc(sizeof(*prop
));
2468 prop
->target_obj
= target_obj
;
2469 prop
->target_name
= g_strdup(target_name
);
2471 op
= object_property_add(obj
, name
, prop_type
,
2474 property_release_alias
,
2477 error_propagate(errp
, local_err
);
2481 op
->resolve
= property_resolve_alias
;
2483 object_property_set_description(obj
, op
->name
,
2484 target_prop
->description
,
2491 void object_property_set_description(Object
*obj
, const char *name
,
2492 const char *description
, Error
**errp
)
2496 op
= object_property_find(obj
, name
, errp
);
2501 g_free(op
->description
);
2502 op
->description
= g_strdup(description
);
2505 void object_class_property_set_description(ObjectClass
*klass
,
2507 const char *description
,
2512 op
= g_hash_table_lookup(klass
->properties
, name
);
2514 error_setg(errp
, "Property '.%s' not found", name
);
2518 g_free(op
->description
);
2519 op
->description
= g_strdup(description
);
2522 static void object_class_init(ObjectClass
*klass
, void *data
)
2524 object_class_property_add_str(klass
, "type", qdev_get_type
,
2525 NULL
, &error_abort
);
2528 static void register_types(void)
2530 static TypeInfo interface_info
= {
2531 .name
= TYPE_INTERFACE
,
2532 .class_size
= sizeof(InterfaceClass
),
2536 static TypeInfo object_info
= {
2537 .name
= TYPE_OBJECT
,
2538 .instance_size
= sizeof(Object
),
2539 .class_init
= object_class_init
,
2543 type_interface
= type_register_internal(&interface_info
);
2544 type_register_internal(&object_info
);
2547 type_init(register_types
)