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 "qemu/memalign.h"
20 #include "qapi/visitor.h"
21 #include "qapi/string-input-visitor.h"
22 #include "qapi/string-output-visitor.h"
23 #include "qapi/qobject-input-visitor.h"
24 #include "qapi/forward-visitor.h"
25 #include "qapi/qapi-builtin-visit.h"
26 #include "qapi/qmp/qerror.h"
27 #include "qapi/qmp/qjson.h"
30 /* TODO: replace QObject with a simpler visitor to avoid a dependency
31 * of the QOM core on QObject? */
32 #include "qom/qom-qobject.h"
33 #include "qapi/qmp/qbool.h"
34 #include "qapi/qmp/qlist.h"
35 #include "qapi/qmp/qnum.h"
36 #include "qapi/qmp/qstring.h"
37 #include "qemu/error-report.h"
39 #define MAX_INTERFACES 32
41 typedef struct InterfaceImpl InterfaceImpl
;
42 typedef struct TypeImpl TypeImpl
;
56 size_t instance_align
;
58 void (*class_init
)(ObjectClass
*klass
, void *data
);
59 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
63 void (*instance_init
)(Object
*obj
);
64 void (*instance_post_init
)(Object
*obj
);
65 void (*instance_finalize
)(Object
*obj
);
70 TypeImpl
*parent_type
;
75 InterfaceImpl interfaces
[MAX_INTERFACES
];
78 static Type type_interface
;
80 static GHashTable
*type_table_get(void)
82 static GHashTable
*type_table
;
84 if (type_table
== NULL
) {
85 type_table
= g_hash_table_new(g_str_hash
, g_str_equal
);
91 static bool enumerating_types
;
93 static void type_table_add(TypeImpl
*ti
)
95 assert(!enumerating_types
);
96 g_hash_table_insert(type_table_get(), (void *)ti
->name
, ti
);
99 static TypeImpl
*type_table_lookup(const char *name
)
101 return g_hash_table_lookup(type_table_get(), name
);
104 static TypeImpl
*type_new(const TypeInfo
*info
)
106 TypeImpl
*ti
= g_malloc0(sizeof(*ti
));
109 g_assert(info
->name
!= NULL
);
111 if (type_table_lookup(info
->name
) != NULL
) {
112 fprintf(stderr
, "Registering `%s' which already exists\n", info
->name
);
116 ti
->name
= g_strdup(info
->name
);
117 ti
->parent
= g_strdup(info
->parent
);
119 ti
->class_size
= info
->class_size
;
120 ti
->instance_size
= info
->instance_size
;
121 ti
->instance_align
= info
->instance_align
;
123 ti
->class_init
= info
->class_init
;
124 ti
->class_base_init
= info
->class_base_init
;
125 ti
->class_data
= info
->class_data
;
127 ti
->instance_init
= info
->instance_init
;
128 ti
->instance_post_init
= info
->instance_post_init
;
129 ti
->instance_finalize
= info
->instance_finalize
;
131 ti
->abstract
= info
->abstract
;
133 for (i
= 0; info
->interfaces
&& info
->interfaces
[i
].type
; i
++) {
134 ti
->interfaces
[i
].typename
= g_strdup(info
->interfaces
[i
].type
);
136 ti
->num_interfaces
= i
;
141 static bool type_name_is_valid(const char *name
)
143 const int slen
= strlen(name
);
149 * Ideally, the name should start with a letter - however, we've got
150 * too many names starting with a digit already, so allow digits here,
151 * too (except '0' which is not used yet)
153 if (!g_ascii_isalnum(name
[0]) || name
[0] == '0') {
157 plen
= strspn(name
, "abcdefghijklmnopqrstuvwxyz"
158 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
161 /* Allow some legacy names with '+' in it for compatibility reasons */
162 if (name
[plen
] == '+') {
163 if (plen
== 6 && g_str_has_prefix(name
, "power")) {
164 /* Allow "power5+" and "power7+" CPU names*/
167 if (plen
>= 17 && g_str_has_prefix(name
, "Sun-UltraSparc-I")) {
168 /* Allow "Sun-UltraSparc-IV+" and "Sun-UltraSparc-IIIi+" */
176 static TypeImpl
*type_register_internal(const TypeInfo
*info
)
180 if (!type_name_is_valid(info
->name
)) {
181 fprintf(stderr
, "Registering '%s' with illegal type name\n", info
->name
);
191 TypeImpl
*type_register(const TypeInfo
*info
)
193 assert(info
->parent
);
194 return type_register_internal(info
);
197 TypeImpl
*type_register_static(const TypeInfo
*info
)
199 return type_register(info
);
202 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
)
206 for (i
= 0; i
< nr_infos
; i
++) {
207 type_register_static(&infos
[i
]);
211 static TypeImpl
*type_get_by_name(const char *name
)
217 return type_table_lookup(name
);
220 static TypeImpl
*type_get_parent(TypeImpl
*type
)
222 if (!type
->parent_type
&& type
->parent
) {
223 type
->parent_type
= type_get_by_name(type
->parent
);
224 if (!type
->parent_type
) {
225 fprintf(stderr
, "Type '%s' is missing its parent '%s'\n",
226 type
->name
, type
->parent
);
231 return type
->parent_type
;
234 static bool type_has_parent(TypeImpl
*type
)
236 return (type
->parent
!= NULL
);
239 static size_t type_class_get_size(TypeImpl
*ti
)
241 if (ti
->class_size
) {
242 return ti
->class_size
;
245 if (type_has_parent(ti
)) {
246 return type_class_get_size(type_get_parent(ti
));
249 return sizeof(ObjectClass
);
252 static size_t type_object_get_size(TypeImpl
*ti
)
254 if (ti
->instance_size
) {
255 return ti
->instance_size
;
258 if (type_has_parent(ti
)) {
259 return type_object_get_size(type_get_parent(ti
));
265 static size_t type_object_get_align(TypeImpl
*ti
)
267 if (ti
->instance_align
) {
268 return ti
->instance_align
;
271 if (type_has_parent(ti
)) {
272 return type_object_get_align(type_get_parent(ti
));
278 size_t object_type_get_instance_size(const char *typename
)
280 TypeImpl
*type
= type_get_by_name(typename
);
282 g_assert(type
!= NULL
);
283 return type_object_get_size(type
);
286 static bool type_is_ancestor(TypeImpl
*type
, TypeImpl
*target_type
)
290 /* Check if target_type is a direct ancestor of type */
292 if (type
== target_type
) {
296 type
= type_get_parent(type
);
302 static void type_initialize(TypeImpl
*ti
);
304 static void type_initialize_interface(TypeImpl
*ti
, TypeImpl
*interface_type
,
305 TypeImpl
*parent_type
)
307 InterfaceClass
*new_iface
;
309 TypeImpl
*iface_impl
;
311 info
.parent
= parent_type
->name
;
312 info
.name
= g_strdup_printf("%s::%s", ti
->name
, interface_type
->name
);
313 info
.abstract
= true;
315 iface_impl
= type_new(&info
);
316 iface_impl
->parent_type
= parent_type
;
317 type_initialize(iface_impl
);
318 g_free((char *)info
.name
);
320 new_iface
= (InterfaceClass
*)iface_impl
->class;
321 new_iface
->concrete_class
= ti
->class;
322 new_iface
->interface_type
= interface_type
;
324 ti
->class->interfaces
= g_slist_append(ti
->class->interfaces
, new_iface
);
327 static void object_property_free(gpointer data
)
329 ObjectProperty
*prop
= data
;
332 qobject_unref(prop
->defval
);
337 g_free(prop
->description
);
341 static void type_initialize(TypeImpl
*ti
)
349 ti
->class_size
= type_class_get_size(ti
);
350 ti
->instance_size
= type_object_get_size(ti
);
351 ti
->instance_align
= type_object_get_align(ti
);
352 /* Any type with zero instance_size is implicitly abstract.
353 * This means interface types are all abstract.
355 if (ti
->instance_size
== 0) {
358 if (type_is_ancestor(ti
, type_interface
)) {
359 assert(ti
->instance_size
== 0);
360 assert(ti
->abstract
);
361 assert(!ti
->instance_init
);
362 assert(!ti
->instance_post_init
);
363 assert(!ti
->instance_finalize
);
364 assert(!ti
->num_interfaces
);
366 ti
->class = g_malloc0(ti
->class_size
);
368 parent
= type_get_parent(ti
);
370 type_initialize(parent
);
374 g_assert(parent
->class_size
<= ti
->class_size
);
375 g_assert(parent
->instance_size
<= ti
->instance_size
);
376 memcpy(ti
->class, parent
->class, parent
->class_size
);
377 ti
->class->interfaces
= NULL
;
379 for (e
= parent
->class->interfaces
; e
; e
= e
->next
) {
380 InterfaceClass
*iface
= e
->data
;
381 ObjectClass
*klass
= OBJECT_CLASS(iface
);
383 type_initialize_interface(ti
, iface
->interface_type
, klass
->type
);
386 for (i
= 0; i
< ti
->num_interfaces
; i
++) {
387 TypeImpl
*t
= type_get_by_name(ti
->interfaces
[i
].typename
);
389 error_report("missing interface '%s' for object '%s'",
390 ti
->interfaces
[i
].typename
, parent
->name
);
393 for (e
= ti
->class->interfaces
; e
; e
= e
->next
) {
394 TypeImpl
*target_type
= OBJECT_CLASS(e
->data
)->type
;
396 if (type_is_ancestor(target_type
, t
)) {
405 type_initialize_interface(ti
, t
, t
);
409 ti
->class->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
, NULL
,
410 object_property_free
);
412 ti
->class->type
= ti
;
415 if (parent
->class_base_init
) {
416 parent
->class_base_init(ti
->class, ti
->class_data
);
418 parent
= type_get_parent(parent
);
421 if (ti
->class_init
) {
422 ti
->class_init(ti
->class, ti
->class_data
);
426 static void object_init_with_type(Object
*obj
, TypeImpl
*ti
)
428 if (type_has_parent(ti
)) {
429 object_init_with_type(obj
, type_get_parent(ti
));
432 if (ti
->instance_init
) {
433 ti
->instance_init(obj
);
437 static void object_post_init_with_type(Object
*obj
, TypeImpl
*ti
)
439 if (ti
->instance_post_init
) {
440 ti
->instance_post_init(obj
);
443 if (type_has_parent(ti
)) {
444 object_post_init_with_type(obj
, type_get_parent(ti
));
448 bool object_apply_global_props(Object
*obj
, const GPtrArray
*props
,
457 for (i
= 0; i
< props
->len
; i
++) {
458 GlobalProperty
*p
= g_ptr_array_index(props
, i
);
461 if (object_dynamic_cast(obj
, p
->driver
) == NULL
) {
464 if (p
->optional
&& !object_property_find(obj
, p
->property
)) {
468 if (!object_property_parse(obj
, p
->property
, p
->value
, &err
)) {
469 error_prepend(&err
, "can't apply global %s.%s=%s: ",
470 p
->driver
, p
->property
, p
->value
);
472 * If errp != NULL, propagate error and return.
473 * If errp == NULL, report a warning, but keep going
474 * with the remaining globals.
477 error_propagate(errp
, err
);
480 warn_report_err(err
);
489 * Global property defaults
490 * Slot 0: accelerator's global property defaults
491 * Slot 1: machine's global property defaults
492 * Slot 2: global properties from legacy command line option
493 * Each is a GPtrArray of of GlobalProperty.
494 * Applied in order, later entries override earlier ones.
496 static GPtrArray
*object_compat_props
[3];
499 * Retrieve @GPtrArray for global property defined with options
500 * other than "-global". These are generally used for syntactic
501 * sugar and legacy command line options.
503 void object_register_sugar_prop(const char *driver
, const char *prop
,
504 const char *value
, bool optional
)
507 if (!object_compat_props
[2]) {
508 object_compat_props
[2] = g_ptr_array_new();
510 g
= g_new0(GlobalProperty
, 1);
511 g
->driver
= g_strdup(driver
);
512 g
->property
= g_strdup(prop
);
513 g
->value
= g_strdup(value
);
514 g
->optional
= optional
;
515 g_ptr_array_add(object_compat_props
[2], g
);
519 * Set machine's global property defaults to @compat_props.
520 * May be called at most once.
522 void object_set_machine_compat_props(GPtrArray
*compat_props
)
524 assert(!object_compat_props
[1]);
525 object_compat_props
[1] = compat_props
;
529 * Set accelerator's global property defaults to @compat_props.
530 * May be called at most once.
532 void object_set_accelerator_compat_props(GPtrArray
*compat_props
)
534 assert(!object_compat_props
[0]);
535 object_compat_props
[0] = compat_props
;
538 void object_apply_compat_props(Object
*obj
)
542 for (i
= 0; i
< ARRAY_SIZE(object_compat_props
); i
++) {
543 object_apply_global_props(obj
, object_compat_props
[i
],
544 i
== 2 ? &error_fatal
: &error_abort
);
548 static void object_class_property_init_all(Object
*obj
)
550 ObjectPropertyIterator iter
;
551 ObjectProperty
*prop
;
553 object_class_property_iter_init(&iter
, object_get_class(obj
));
554 while ((prop
= object_property_iter_next(&iter
))) {
556 prop
->init(obj
, prop
);
561 static void object_initialize_with_type(Object
*obj
, size_t size
, TypeImpl
*type
)
563 type_initialize(type
);
565 g_assert(type
->instance_size
>= sizeof(Object
));
566 g_assert(type
->abstract
== false);
567 g_assert(size
>= type
->instance_size
);
569 memset(obj
, 0, type
->instance_size
);
570 obj
->class = type
->class;
572 object_class_property_init_all(obj
);
573 obj
->properties
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
574 NULL
, object_property_free
);
575 object_init_with_type(obj
, type
);
576 object_post_init_with_type(obj
, type
);
579 void object_initialize(void *data
, size_t size
, const char *typename
)
581 TypeImpl
*type
= type_get_by_name(typename
);
583 #ifdef CONFIG_MODULES
585 int rv
= module_load_qom(typename
, &error_fatal
);
587 type
= type_get_by_name(typename
);
589 error_report("missing object type '%s'", typename
);
595 error_report("missing object type '%s'", typename
);
599 object_initialize_with_type(data
, size
, type
);
602 bool object_initialize_child_with_props(Object
*parentobj
,
603 const char *propname
,
604 void *childobj
, size_t size
,
611 va_start(vargs
, errp
);
612 ok
= object_initialize_child_with_propsv(parentobj
, propname
,
613 childobj
, size
, type
, errp
,
619 bool object_initialize_child_with_propsv(Object
*parentobj
,
620 const char *propname
,
621 void *childobj
, size_t size
,
623 Error
**errp
, va_list vargs
)
629 object_initialize(childobj
, size
, type
);
630 obj
= OBJECT(childobj
);
632 if (!object_set_propv(obj
, errp
, vargs
)) {
636 object_property_add_child(parentobj
, propname
, obj
);
638 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
640 if (!user_creatable_complete(uc
, errp
)) {
641 object_unparent(obj
);
650 * We want @obj's reference to be 1 on success, 0 on failure.
651 * On success, it's 2: one taken by object_initialize(), and one
652 * by object_property_add_child().
653 * On failure in object_initialize() or earlier, it's 1.
654 * On failure afterwards, it's also 1: object_unparent() releases
655 * the reference taken by object_property_add_child().
661 void object_initialize_child_internal(Object
*parent
,
662 const char *propname
,
663 void *child
, size_t size
,
666 object_initialize_child_with_props(parent
, propname
, child
, size
, type
,
670 static inline bool object_property_is_child(ObjectProperty
*prop
)
672 return strstart(prop
->type
, "child<", NULL
);
675 static void object_property_del_all(Object
*obj
)
677 g_autoptr(GHashTable
) done
= g_hash_table_new(NULL
, NULL
);
678 ObjectProperty
*prop
;
679 ObjectPropertyIterator iter
;
684 object_property_iter_init(&iter
, obj
);
685 while ((prop
= object_property_iter_next(&iter
)) != NULL
) {
686 if (g_hash_table_add(done
, prop
)) {
688 prop
->release(obj
, prop
->name
, prop
->opaque
);
696 g_hash_table_unref(obj
->properties
);
699 static void object_property_del_child(Object
*obj
, Object
*child
)
701 ObjectProperty
*prop
;
705 g_hash_table_iter_init(&iter
, obj
->properties
);
706 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
708 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
710 prop
->release(obj
, prop
->name
, prop
->opaque
);
711 prop
->release
= NULL
;
716 g_hash_table_iter_init(&iter
, obj
->properties
);
717 while (g_hash_table_iter_next(&iter
, &key
, &value
)) {
719 if (object_property_is_child(prop
) && prop
->opaque
== child
) {
720 g_hash_table_iter_remove(&iter
);
726 void object_unparent(Object
*obj
)
729 object_property_del_child(obj
->parent
, obj
);
733 static void object_deinit(Object
*obj
, TypeImpl
*type
)
735 if (type
->instance_finalize
) {
736 type
->instance_finalize(obj
);
739 if (type_has_parent(type
)) {
740 object_deinit(obj
, type_get_parent(type
));
744 static void object_finalize(void *data
)
747 TypeImpl
*ti
= obj
->class->type
;
749 object_property_del_all(obj
);
750 object_deinit(obj
, ti
);
752 g_assert(obj
->ref
== 0);
753 g_assert(obj
->parent
== NULL
);
759 /* Find the minimum alignment guaranteed by the system malloc. */
760 #if __STDC_VERSION__ >= 201112L
761 typedef max_align_t qemu_max_align_t
;
771 static Object
*object_new_with_type(Type type
)
775 void (*obj_free
)(void *);
777 g_assert(type
!= NULL
);
778 type_initialize(type
);
780 size
= type
->instance_size
;
781 align
= type
->instance_align
;
784 * Do not use qemu_memalign unless required. Depending on the
785 * implementation, extra alignment implies extra overhead.
787 if (likely(align
<= __alignof__(qemu_max_align_t
))) {
788 obj
= g_malloc(size
);
791 obj
= qemu_memalign(align
, size
);
792 obj_free
= qemu_vfree
;
795 object_initialize_with_type(obj
, size
, type
);
796 obj
->free
= obj_free
;
801 Object
*object_new_with_class(ObjectClass
*klass
)
803 return object_new_with_type(klass
->type
);
806 Object
*object_new(const char *typename
)
808 TypeImpl
*ti
= type_get_by_name(typename
);
810 return object_new_with_type(ti
);
814 Object
*object_new_with_props(const char *typename
,
823 va_start(vargs
, errp
);
824 obj
= object_new_with_propv(typename
, parent
, id
, errp
, vargs
);
831 Object
*object_new_with_propv(const char *typename
,
841 klass
= object_class_by_name(typename
);
843 error_setg(errp
, "invalid object type: %s", typename
);
847 if (object_class_is_abstract(klass
)) {
848 error_setg(errp
, "object type '%s' is abstract", typename
);
851 obj
= object_new_with_type(klass
->type
);
853 if (!object_set_propv(obj
, errp
, vargs
)) {
858 object_property_add_child(parent
, id
, obj
);
861 uc
= (UserCreatable
*)object_dynamic_cast(obj
, TYPE_USER_CREATABLE
);
863 if (!user_creatable_complete(uc
, errp
)) {
865 object_unparent(obj
);
880 bool object_set_props(Object
*obj
,
887 va_start(vargs
, errp
);
888 ret
= object_set_propv(obj
, errp
, vargs
);
895 bool object_set_propv(Object
*obj
,
899 const char *propname
;
901 propname
= va_arg(vargs
, char *);
902 while (propname
!= NULL
) {
903 const char *value
= va_arg(vargs
, char *);
905 g_assert(value
!= NULL
);
906 if (!object_property_parse(obj
, propname
, value
, errp
)) {
909 propname
= va_arg(vargs
, char *);
916 Object
*object_dynamic_cast(Object
*obj
, const char *typename
)
918 if (obj
&& object_class_dynamic_cast(object_get_class(obj
), typename
)) {
925 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
926 const char *file
, int line
, const char *func
)
928 trace_object_dynamic_cast_assert(obj
? obj
->class->type
->name
: "(null)",
929 typename
, file
, line
, func
);
931 #ifdef CONFIG_QOM_CAST_DEBUG
935 for (i
= 0; obj
&& i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
936 if (qatomic_read(&obj
->class->object_cast_cache
[i
]) == typename
) {
941 inst
= object_dynamic_cast(obj
, typename
);
944 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
945 file
, line
, func
, obj
, typename
);
951 if (obj
&& obj
== inst
) {
952 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
953 qatomic_set(&obj
->class->object_cast_cache
[i
- 1],
954 qatomic_read(&obj
->class->object_cast_cache
[i
]));
956 qatomic_set(&obj
->class->object_cast_cache
[i
- 1], typename
);
964 ObjectClass
*object_class_dynamic_cast(ObjectClass
*class,
965 const char *typename
)
967 ObjectClass
*ret
= NULL
;
968 TypeImpl
*target_type
;
975 /* A simple fast path that can trigger a lot for leaf classes. */
977 if (type
->name
== typename
) {
981 target_type
= type_get_by_name(typename
);
983 /* target class type unknown, so fail the cast */
987 if (type
->class->interfaces
&&
988 type_is_ancestor(target_type
, type_interface
)) {
992 for (i
= class->interfaces
; i
; i
= i
->next
) {
993 ObjectClass
*target_class
= i
->data
;
995 if (type_is_ancestor(target_class
->type
, target_type
)) {
1001 /* The match was ambiguous, don't allow a cast */
1005 } else if (type_is_ancestor(type
, target_type
)) {
1012 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*class,
1013 const char *typename
,
1014 const char *file
, int line
,
1019 trace_object_class_dynamic_cast_assert(class ? class->type
->name
: "(null)",
1020 typename
, file
, line
, func
);
1022 #ifdef CONFIG_QOM_CAST_DEBUG
1025 for (i
= 0; class && i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
1026 if (qatomic_read(&class->class_cast_cache
[i
]) == typename
) {
1032 if (!class || !class->interfaces
) {
1037 ret
= object_class_dynamic_cast(class, typename
);
1038 if (!ret
&& class) {
1039 fprintf(stderr
, "%s:%d:%s: Object %p is not an instance of type %s\n",
1040 file
, line
, func
, class, typename
);
1044 #ifdef CONFIG_QOM_CAST_DEBUG
1045 if (class && ret
== class) {
1046 for (i
= 1; i
< OBJECT_CLASS_CAST_CACHE
; i
++) {
1047 qatomic_set(&class->class_cast_cache
[i
- 1],
1048 qatomic_read(&class->class_cast_cache
[i
]));
1050 qatomic_set(&class->class_cast_cache
[i
- 1], typename
);
1057 const char *object_get_typename(const Object
*obj
)
1059 return obj
->class->type
->name
;
1062 ObjectClass
*object_get_class(Object
*obj
)
1067 bool object_class_is_abstract(ObjectClass
*klass
)
1069 return klass
->type
->abstract
;
1072 const char *object_class_get_name(ObjectClass
*klass
)
1074 return klass
->type
->name
;
1077 ObjectClass
*object_class_by_name(const char *typename
)
1079 TypeImpl
*type
= type_get_by_name(typename
);
1085 type_initialize(type
);
1090 ObjectClass
*module_object_class_by_name(const char *typename
)
1094 oc
= object_class_by_name(typename
);
1095 #ifdef CONFIG_MODULES
1097 Error
*local_err
= NULL
;
1098 int rv
= module_load_qom(typename
, &local_err
);
1100 oc
= object_class_by_name(typename
);
1101 } else if (rv
< 0) {
1102 error_report_err(local_err
);
1109 ObjectClass
*object_class_get_parent(ObjectClass
*class)
1111 TypeImpl
*type
= type_get_parent(class->type
);
1117 type_initialize(type
);
1122 typedef struct OCFData
1124 void (*fn
)(ObjectClass
*klass
, void *opaque
);
1125 const char *implements_type
;
1126 bool include_abstract
;
1130 static void object_class_foreach_tramp(gpointer key
, gpointer value
,
1133 OCFData
*data
= opaque
;
1134 TypeImpl
*type
= value
;
1137 type_initialize(type
);
1140 if (!data
->include_abstract
&& type
->abstract
) {
1144 if (data
->implements_type
&&
1145 !object_class_dynamic_cast(k
, data
->implements_type
)) {
1149 data
->fn(k
, data
->opaque
);
1152 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1153 const char *implements_type
, bool include_abstract
,
1156 OCFData data
= { fn
, implements_type
, include_abstract
, opaque
};
1158 enumerating_types
= true;
1159 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp
, &data
);
1160 enumerating_types
= false;
1163 static int do_object_child_foreach(Object
*obj
,
1164 int (*fn
)(Object
*child
, void *opaque
),
1165 void *opaque
, bool recurse
)
1167 GHashTableIter iter
;
1168 ObjectProperty
*prop
;
1171 g_hash_table_iter_init(&iter
, obj
->properties
);
1172 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
1173 if (object_property_is_child(prop
)) {
1174 Object
*child
= prop
->opaque
;
1176 ret
= fn(child
, opaque
);
1181 ret
= do_object_child_foreach(child
, fn
, opaque
, true);
1191 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1194 return do_object_child_foreach(obj
, fn
, opaque
, false);
1197 int object_child_foreach_recursive(Object
*obj
,
1198 int (*fn
)(Object
*child
, void *opaque
),
1201 return do_object_child_foreach(obj
, fn
, opaque
, true);
1204 static void object_class_get_list_tramp(ObjectClass
*klass
, void *opaque
)
1206 GSList
**list
= opaque
;
1208 *list
= g_slist_prepend(*list
, klass
);
1211 GSList
*object_class_get_list(const char *implements_type
,
1212 bool include_abstract
)
1214 GSList
*list
= NULL
;
1216 object_class_foreach(object_class_get_list_tramp
,
1217 implements_type
, include_abstract
, &list
);
1221 static gint
object_class_cmp(gconstpointer a
, gconstpointer b
)
1223 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
1224 object_class_get_name((ObjectClass
*)b
));
1227 GSList
*object_class_get_list_sorted(const char *implements_type
,
1228 bool include_abstract
)
1230 return g_slist_sort(object_class_get_list(implements_type
, include_abstract
),
1234 Object
*object_ref(void *objptr
)
1236 Object
*obj
= OBJECT(objptr
);
1242 ref
= qatomic_fetch_inc(&obj
->ref
);
1243 /* Assert waaay before the integer overflows */
1244 g_assert(ref
< INT_MAX
);
1248 void object_unref(void *objptr
)
1250 Object
*obj
= OBJECT(objptr
);
1254 g_assert(obj
->ref
> 0);
1256 /* parent always holds a reference to its children */
1257 if (qatomic_fetch_dec(&obj
->ref
) == 1) {
1258 object_finalize(obj
);
1263 object_property_try_add(Object
*obj
, const char *name
, const char *type
,
1264 ObjectPropertyAccessor
*get
,
1265 ObjectPropertyAccessor
*set
,
1266 ObjectPropertyRelease
*release
,
1267 void *opaque
, Error
**errp
)
1269 ObjectProperty
*prop
;
1270 size_t name_len
= strlen(name
);
1272 if (name_len
>= 3 && !memcmp(name
+ name_len
- 3, "[*]", 4)) {
1274 ObjectProperty
*ret
= NULL
;
1275 char *name_no_array
= g_strdup(name
);
1277 name_no_array
[name_len
- 3] = '\0';
1278 for (i
= 0; i
< INT16_MAX
; ++i
) {
1279 char *full_name
= g_strdup_printf("%s[%d]", name_no_array
, i
);
1281 ret
= object_property_try_add(obj
, full_name
, type
, get
, set
,
1282 release
, opaque
, NULL
);
1288 g_free(name_no_array
);
1293 if (object_property_find(obj
, name
) != NULL
) {
1294 error_setg(errp
, "attempt to add duplicate property '%s' to object (type '%s')",
1295 name
, object_get_typename(obj
));
1299 prop
= g_malloc0(sizeof(*prop
));
1301 prop
->name
= g_strdup(name
);
1302 prop
->type
= g_strdup(type
);
1306 prop
->release
= release
;
1307 prop
->opaque
= opaque
;
1309 g_hash_table_insert(obj
->properties
, prop
->name
, prop
);
1314 object_property_add(Object
*obj
, const char *name
, const char *type
,
1315 ObjectPropertyAccessor
*get
,
1316 ObjectPropertyAccessor
*set
,
1317 ObjectPropertyRelease
*release
,
1320 return object_property_try_add(obj
, name
, type
, get
, set
, release
,
1321 opaque
, &error_abort
);
1325 object_class_property_add(ObjectClass
*klass
,
1328 ObjectPropertyAccessor
*get
,
1329 ObjectPropertyAccessor
*set
,
1330 ObjectPropertyRelease
*release
,
1333 ObjectProperty
*prop
;
1335 assert(!object_class_property_find(klass
, name
));
1337 prop
= g_malloc0(sizeof(*prop
));
1339 prop
->name
= g_strdup(name
);
1340 prop
->type
= g_strdup(type
);
1344 prop
->release
= release
;
1345 prop
->opaque
= opaque
;
1347 g_hash_table_insert(klass
->properties
, prop
->name
, prop
);
1352 ObjectProperty
*object_property_find(Object
*obj
, const char *name
)
1354 ObjectProperty
*prop
;
1355 ObjectClass
*klass
= object_get_class(obj
);
1357 prop
= object_class_property_find(klass
, name
);
1362 return g_hash_table_lookup(obj
->properties
, name
);
1365 ObjectProperty
*object_property_find_err(Object
*obj
, const char *name
,
1368 ObjectProperty
*prop
= object_property_find(obj
, name
);
1370 error_setg(errp
, "Property '%s.%s' not found",
1371 object_get_typename(obj
), name
);
1376 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1379 g_hash_table_iter_init(&iter
->iter
, obj
->properties
);
1380 iter
->nextclass
= object_get_class(obj
);
1383 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
)
1386 while (!g_hash_table_iter_next(&iter
->iter
, &key
, &val
)) {
1387 if (!iter
->nextclass
) {
1390 g_hash_table_iter_init(&iter
->iter
, iter
->nextclass
->properties
);
1391 iter
->nextclass
= object_class_get_parent(iter
->nextclass
);
1396 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1399 g_hash_table_iter_init(&iter
->iter
, klass
->properties
);
1400 iter
->nextclass
= object_class_get_parent(klass
);
1403 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
)
1405 ObjectClass
*parent_klass
;
1407 parent_klass
= object_class_get_parent(klass
);
1409 ObjectProperty
*prop
=
1410 object_class_property_find(parent_klass
, name
);
1416 return g_hash_table_lookup(klass
->properties
, name
);
1419 ObjectProperty
*object_class_property_find_err(ObjectClass
*klass
,
1423 ObjectProperty
*prop
= object_class_property_find(klass
, name
);
1425 error_setg(errp
, "Property '.%s' not found", name
);
1431 void object_property_del(Object
*obj
, const char *name
)
1433 ObjectProperty
*prop
= g_hash_table_lookup(obj
->properties
, name
);
1435 if (prop
->release
) {
1436 prop
->release(obj
, name
, prop
->opaque
);
1438 g_hash_table_remove(obj
->properties
, name
);
1441 bool object_property_get(Object
*obj
, const char *name
, Visitor
*v
,
1445 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1452 error_setg(errp
, "Property '%s.%s' is not readable",
1453 object_get_typename(obj
), name
);
1456 prop
->get(obj
, v
, name
, prop
->opaque
, &err
);
1457 error_propagate(errp
, err
);
1461 bool object_property_set(Object
*obj
, const char *name
, Visitor
*v
,
1465 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1472 error_setg(errp
, "Property '%s.%s' is not writable",
1473 object_get_typename(obj
), name
);
1476 prop
->set(obj
, v
, name
, prop
->opaque
, errp
);
1480 bool object_property_set_str(Object
*obj
, const char *name
,
1481 const char *value
, Error
**errp
)
1483 QString
*qstr
= qstring_from_str(value
);
1484 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qstr
), errp
);
1486 qobject_unref(qstr
);
1490 char *object_property_get_str(Object
*obj
, const char *name
,
1493 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1500 qstring
= qobject_to(QString
, ret
);
1502 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "string");
1505 retval
= g_strdup(qstring_get_str(qstring
));
1512 bool object_property_set_link(Object
*obj
, const char *name
,
1513 Object
*value
, Error
**errp
)
1515 g_autofree
char *path
= NULL
;
1518 path
= object_get_canonical_path(value
);
1520 return object_property_set_str(obj
, name
, path
?: "", errp
);
1523 Object
*object_property_get_link(Object
*obj
, const char *name
,
1526 char *str
= object_property_get_str(obj
, name
, errp
);
1527 Object
*target
= NULL
;
1530 target
= object_resolve_path(str
, NULL
);
1532 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1533 "Device '%s' not found", str
);
1541 bool object_property_set_bool(Object
*obj
, const char *name
,
1542 bool value
, Error
**errp
)
1544 QBool
*qbool
= qbool_from_bool(value
);
1545 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qbool
), errp
);
1547 qobject_unref(qbool
);
1551 bool object_property_get_bool(Object
*obj
, const char *name
,
1554 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1561 qbool
= qobject_to(QBool
, ret
);
1563 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "boolean");
1566 retval
= qbool_get_bool(qbool
);
1573 bool object_property_set_int(Object
*obj
, const char *name
,
1574 int64_t value
, Error
**errp
)
1576 QNum
*qnum
= qnum_from_int(value
);
1577 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1579 qobject_unref(qnum
);
1583 int64_t object_property_get_int(Object
*obj
, const char *name
,
1586 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1594 qnum
= qobject_to(QNum
, ret
);
1595 if (!qnum
|| !qnum_get_try_int(qnum
, &retval
)) {
1596 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "int");
1604 static void object_property_init_defval(Object
*obj
, ObjectProperty
*prop
)
1606 Visitor
*v
= qobject_input_visitor_new(prop
->defval
);
1608 assert(prop
->set
!= NULL
);
1609 prop
->set(obj
, v
, prop
->name
, prop
->opaque
, &error_abort
);
1614 static void object_property_set_default(ObjectProperty
*prop
, QObject
*defval
)
1616 assert(!prop
->defval
);
1617 assert(!prop
->init
);
1619 prop
->defval
= defval
;
1620 prop
->init
= object_property_init_defval
;
1623 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
)
1625 object_property_set_default(prop
, QOBJECT(qbool_from_bool(value
)));
1628 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
)
1630 object_property_set_default(prop
, QOBJECT(qstring_from_str(value
)));
1633 void object_property_set_default_list(ObjectProperty
*prop
)
1635 object_property_set_default(prop
, QOBJECT(qlist_new()));
1638 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
)
1640 object_property_set_default(prop
, QOBJECT(qnum_from_int(value
)));
1643 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
)
1645 object_property_set_default(prop
, QOBJECT(qnum_from_uint(value
)));
1648 bool object_property_set_uint(Object
*obj
, const char *name
,
1649 uint64_t value
, Error
**errp
)
1651 QNum
*qnum
= qnum_from_uint(value
);
1652 bool ok
= object_property_set_qobject(obj
, name
, QOBJECT(qnum
), errp
);
1654 qobject_unref(qnum
);
1658 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1661 QObject
*ret
= object_property_get_qobject(obj
, name
, errp
);
1668 qnum
= qobject_to(QNum
, ret
);
1669 if (!qnum
|| !qnum_get_try_uint(qnum
, &retval
)) {
1670 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, "uint");
1678 typedef struct EnumProperty
{
1679 const QEnumLookup
*lookup
;
1680 int (*get
)(Object
*, Error
**);
1681 void (*set
)(Object
*, int, Error
**);
1684 int object_property_get_enum(Object
*obj
, const char *name
,
1685 const char *typename
, Error
**errp
)
1689 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1690 EnumProperty
*enumprop
;
1696 if (!g_str_equal(prop
->type
, typename
)) {
1697 error_setg(errp
, "Property %s on %s is not '%s' enum type",
1698 name
, object_class_get_name(
1699 object_get_class(obj
)), typename
);
1703 enumprop
= prop
->opaque
;
1705 str
= object_property_get_str(obj
, name
, errp
);
1710 ret
= qapi_enum_parse(enumprop
->lookup
, str
, -1, errp
);
1716 bool object_property_parse(Object
*obj
, const char *name
,
1717 const char *string
, Error
**errp
)
1719 Visitor
*v
= string_input_visitor_new(string
);
1720 bool ok
= object_property_set(obj
, name
, v
, errp
);
1726 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1730 char *string
= NULL
;
1732 v
= string_output_visitor_new(human
, &string
);
1733 if (!object_property_get(obj
, name
, v
, errp
)) {
1737 visit_complete(v
, &string
);
1744 const char *object_property_get_type(Object
*obj
, const char *name
, Error
**errp
)
1746 ObjectProperty
*prop
= object_property_find_err(obj
, name
, errp
);
1754 Object
*object_get_root(void)
1756 static Object
*root
;
1759 root
= object_new("container");
1765 Object
*object_get_objects_root(void)
1767 return container_get(object_get_root(), "/objects");
1770 Object
*object_get_internal_root(void)
1772 static Object
*internal_root
;
1774 if (!internal_root
) {
1775 internal_root
= object_new("container");
1778 return internal_root
;
1781 static void object_get_child_property(Object
*obj
, Visitor
*v
,
1782 const char *name
, void *opaque
,
1785 Object
*child
= opaque
;
1788 path
= object_get_canonical_path(child
);
1789 visit_type_str(v
, name
, &path
, errp
);
1793 static Object
*object_resolve_child_property(Object
*parent
, void *opaque
,
1799 static void object_finalize_child_property(Object
*obj
, const char *name
,
1802 Object
*child
= opaque
;
1804 if (child
->class->unparent
) {
1805 (child
->class->unparent
)(child
);
1807 child
->parent
= NULL
;
1808 object_unref(child
);
1812 object_property_try_add_child(Object
*obj
, const char *name
,
1813 Object
*child
, Error
**errp
)
1815 g_autofree
char *type
= NULL
;
1818 assert(!child
->parent
);
1820 type
= g_strdup_printf("child<%s>", object_get_typename(child
));
1822 op
= object_property_try_add(obj
, name
, type
, object_get_child_property
,
1823 NULL
, object_finalize_child_property
,
1828 op
->resolve
= object_resolve_child_property
;
1830 child
->parent
= obj
;
1835 object_property_add_child(Object
*obj
, const char *name
,
1838 return object_property_try_add_child(obj
, name
, child
, &error_abort
);
1841 void object_property_allow_set_link(const Object
*obj
, const char *name
,
1842 Object
*val
, Error
**errp
)
1844 /* Allow the link to be set, always */
1850 Object
*target
; /* if OBJ_PROP_LINK_DIRECT, when holding the pointer */
1851 ptrdiff_t offset
; /* if OBJ_PROP_LINK_CLASS */
1853 void (*check
)(const Object
*, const char *, Object
*, Error
**);
1854 ObjectPropertyLinkFlags flags
;
1858 object_link_get_targetp(Object
*obj
, LinkProperty
*lprop
)
1860 if (lprop
->flags
& OBJ_PROP_LINK_DIRECT
) {
1861 return &lprop
->target
;
1862 } else if (lprop
->flags
& OBJ_PROP_LINK_CLASS
) {
1863 return (void *)obj
+ lprop
->offset
;
1865 return lprop
->targetp
;
1869 static void object_get_link_property(Object
*obj
, Visitor
*v
,
1870 const char *name
, void *opaque
,
1873 LinkProperty
*lprop
= opaque
;
1874 Object
**targetp
= object_link_get_targetp(obj
, lprop
);
1878 path
= object_get_canonical_path(*targetp
);
1879 visit_type_str(v
, name
, &path
, errp
);
1883 visit_type_str(v
, name
, &path
, errp
);
1888 * object_resolve_link:
1890 * Lookup an object and ensure its type matches the link property type. This
1891 * is similar to object_resolve_path() except type verification against the
1892 * link property is performed.
1894 * Returns: The matched object or NULL on path lookup failures.
1896 static Object
*object_resolve_link(Object
*obj
, const char *name
,
1897 const char *path
, Error
**errp
)
1901 bool ambiguous
= false;
1904 /* Go from link<FOO> to FOO. */
1905 type
= object_property_get_type(obj
, name
, NULL
);
1906 target_type
= g_strndup(&type
[5], strlen(type
) - 6);
1907 target
= object_resolve_path_type(path
, target_type
, &ambiguous
);
1910 error_setg(errp
, "Path '%s' does not uniquely identify an object",
1912 } else if (!target
) {
1913 target
= object_resolve_path(path
, &ambiguous
);
1914 if (target
|| ambiguous
) {
1915 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
, target_type
);
1917 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
1918 "Device '%s' not found", path
);
1922 g_free(target_type
);
1927 static void object_set_link_property(Object
*obj
, Visitor
*v
,
1928 const char *name
, void *opaque
,
1931 Error
*local_err
= NULL
;
1932 LinkProperty
*prop
= opaque
;
1933 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1934 Object
*old_target
= *targetp
;
1938 if (!visit_type_str(v
, name
, &path
, errp
)) {
1943 new_target
= object_resolve_link(obj
, name
, path
, errp
);
1954 prop
->check(obj
, name
, new_target
, &local_err
);
1956 error_propagate(errp
, local_err
);
1960 *targetp
= new_target
;
1961 if (prop
->flags
& OBJ_PROP_LINK_STRONG
) {
1962 object_ref(new_target
);
1963 object_unref(old_target
);
1967 static Object
*object_resolve_link_property(Object
*parent
, void *opaque
,
1970 LinkProperty
*lprop
= opaque
;
1972 return *object_link_get_targetp(parent
, lprop
);
1975 static void object_release_link_property(Object
*obj
, const char *name
,
1978 LinkProperty
*prop
= opaque
;
1979 Object
**targetp
= object_link_get_targetp(obj
, prop
);
1981 if ((prop
->flags
& OBJ_PROP_LINK_STRONG
) && *targetp
) {
1982 object_unref(*targetp
);
1984 if (!(prop
->flags
& OBJ_PROP_LINK_CLASS
)) {
1989 static ObjectProperty
*
1990 object_add_link_prop(Object
*obj
, const char *name
,
1991 const char *type
, void *ptr
,
1992 void (*check
)(const Object
*, const char *,
1993 Object
*, Error
**),
1994 ObjectPropertyLinkFlags flags
)
1996 LinkProperty
*prop
= g_malloc(sizeof(*prop
));
1997 g_autofree
char *full_type
= NULL
;
2000 if (flags
& OBJ_PROP_LINK_DIRECT
) {
2003 prop
->targetp
= ptr
;
2005 prop
->check
= check
;
2006 prop
->flags
= flags
;
2008 full_type
= g_strdup_printf("link<%s>", type
);
2010 op
= object_property_add(obj
, name
, full_type
,
2011 object_get_link_property
,
2012 check
? object_set_link_property
: NULL
,
2013 object_release_link_property
,
2015 op
->resolve
= object_resolve_link_property
;
2020 object_property_add_link(Object
*obj
, const char *name
,
2021 const char *type
, Object
**targetp
,
2022 void (*check
)(const Object
*, const char *,
2023 Object
*, Error
**),
2024 ObjectPropertyLinkFlags flags
)
2026 return object_add_link_prop(obj
, name
, type
, targetp
, check
, flags
);
2030 object_class_property_add_link(ObjectClass
*oc
,
2032 const char *type
, ptrdiff_t offset
,
2033 void (*check
)(const Object
*obj
, const char *name
,
2034 Object
*val
, Error
**errp
),
2035 ObjectPropertyLinkFlags flags
)
2037 LinkProperty
*prop
= g_new0(LinkProperty
, 1);
2041 prop
->offset
= offset
;
2042 prop
->check
= check
;
2043 prop
->flags
= flags
| OBJ_PROP_LINK_CLASS
;
2045 full_type
= g_strdup_printf("link<%s>", type
);
2047 op
= object_class_property_add(oc
, name
, full_type
,
2048 object_get_link_property
,
2049 check
? object_set_link_property
: NULL
,
2050 object_release_link_property
,
2053 op
->resolve
= object_resolve_link_property
;
2060 object_property_add_const_link(Object
*obj
, const char *name
,
2063 return object_add_link_prop(obj
, name
,
2064 object_get_typename(target
), target
,
2065 NULL
, OBJ_PROP_LINK_DIRECT
);
2068 const char *object_get_canonical_path_component(const Object
*obj
)
2070 ObjectProperty
*prop
= NULL
;
2071 GHashTableIter iter
;
2073 if (obj
->parent
== NULL
) {
2077 g_hash_table_iter_init(&iter
, obj
->parent
->properties
);
2078 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2079 if (!object_property_is_child(prop
)) {
2083 if (prop
->opaque
== obj
) {
2088 /* obj had a parent but was not a child, should never happen */
2089 g_assert_not_reached();
2093 char *object_get_canonical_path(const Object
*obj
)
2095 Object
*root
= object_get_root();
2096 char *newpath
, *path
= NULL
;
2099 return g_strdup("/");
2103 const char *component
= object_get_canonical_path_component(obj
);
2106 /* A canonical path must be complete, so discard what was
2113 newpath
= g_strdup_printf("/%s%s", component
, path
? path
: "");
2117 } while (obj
!= root
);
2122 Object
*object_resolve_path_component(Object
*parent
, const char *part
)
2124 ObjectProperty
*prop
= object_property_find(parent
, part
);
2129 if (prop
->resolve
) {
2130 return prop
->resolve(parent
, prop
->opaque
, part
);
2136 static Object
*object_resolve_abs_path(Object
*parent
,
2138 const char *typename
)
2142 if (*parts
== NULL
) {
2143 return object_dynamic_cast(parent
, typename
);
2146 if (strcmp(*parts
, "") == 0) {
2147 return object_resolve_abs_path(parent
, parts
+ 1, typename
);
2150 child
= object_resolve_path_component(parent
, *parts
);
2155 return object_resolve_abs_path(child
, parts
+ 1, typename
);
2158 static Object
*object_resolve_partial_path(Object
*parent
,
2160 const char *typename
,
2164 GHashTableIter iter
;
2165 ObjectProperty
*prop
;
2167 obj
= object_resolve_abs_path(parent
, parts
, typename
);
2169 g_hash_table_iter_init(&iter
, parent
->properties
);
2170 while (g_hash_table_iter_next(&iter
, NULL
, (gpointer
*)&prop
)) {
2173 if (!object_property_is_child(prop
)) {
2177 found
= object_resolve_partial_path(prop
->opaque
, parts
,
2178 typename
, ambiguous
);
2195 Object
*object_resolve_path_type(const char *path
, const char *typename
,
2201 parts
= g_strsplit(path
, "/", 0);
2204 if (parts
[0] == NULL
|| strcmp(parts
[0], "") != 0) {
2205 bool ambiguous
= false;
2206 obj
= object_resolve_partial_path(object_get_root(), parts
,
2207 typename
, &ambiguous
);
2209 *ambiguousp
= ambiguous
;
2212 obj
= object_resolve_abs_path(object_get_root(), parts
+ 1, typename
);
2220 Object
*object_resolve_path(const char *path
, bool *ambiguous
)
2222 return object_resolve_path_type(path
, TYPE_OBJECT
, ambiguous
);
2225 Object
*object_resolve_path_at(Object
*parent
, const char *path
)
2227 g_auto(GStrv
) parts
= g_strsplit(path
, "/", 0);
2230 return object_resolve_abs_path(object_get_root(), parts
+ 1,
2233 return object_resolve_abs_path(parent
, parts
, TYPE_OBJECT
);
2236 typedef struct StringProperty
2238 char *(*get
)(Object
*, Error
**);
2239 void (*set
)(Object
*, const char *, Error
**);
2242 static void property_get_str(Object
*obj
, Visitor
*v
, const char *name
,
2243 void *opaque
, Error
**errp
)
2245 StringProperty
*prop
= opaque
;
2249 value
= prop
->get(obj
, &err
);
2251 error_propagate(errp
, err
);
2255 visit_type_str(v
, name
, &value
, errp
);
2259 static void property_set_str(Object
*obj
, Visitor
*v
, const char *name
,
2260 void *opaque
, Error
**errp
)
2262 StringProperty
*prop
= opaque
;
2265 if (!visit_type_str(v
, name
, &value
, errp
)) {
2269 prop
->set(obj
, value
, errp
);
2273 static void property_release_data(Object
*obj
, const char *name
,
2280 object_property_add_str(Object
*obj
, const char *name
,
2281 char *(*get
)(Object
*, Error
**),
2282 void (*set
)(Object
*, const char *, Error
**))
2284 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2289 return object_property_add(obj
, name
, "string",
2290 get
? property_get_str
: NULL
,
2291 set
? property_set_str
: NULL
,
2292 property_release_data
,
2297 object_class_property_add_str(ObjectClass
*klass
, const char *name
,
2298 char *(*get
)(Object
*, Error
**),
2299 void (*set
)(Object
*, const char *,
2302 StringProperty
*prop
= g_malloc0(sizeof(*prop
));
2307 return object_class_property_add(klass
, name
, "string",
2308 get
? property_get_str
: NULL
,
2309 set
? property_set_str
: NULL
,
2314 typedef struct BoolProperty
2316 bool (*get
)(Object
*, Error
**);
2317 void (*set
)(Object
*, bool, Error
**);
2320 static void property_get_bool(Object
*obj
, Visitor
*v
, const char *name
,
2321 void *opaque
, Error
**errp
)
2323 BoolProperty
*prop
= opaque
;
2327 value
= prop
->get(obj
, &err
);
2329 error_propagate(errp
, err
);
2333 visit_type_bool(v
, name
, &value
, errp
);
2336 static void property_set_bool(Object
*obj
, Visitor
*v
, const char *name
,
2337 void *opaque
, Error
**errp
)
2339 BoolProperty
*prop
= opaque
;
2342 if (!visit_type_bool(v
, name
, &value
, errp
)) {
2346 prop
->set(obj
, value
, errp
);
2350 object_property_add_bool(Object
*obj
, const char *name
,
2351 bool (*get
)(Object
*, Error
**),
2352 void (*set
)(Object
*, bool, Error
**))
2354 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2359 return object_property_add(obj
, name
, "bool",
2360 get
? property_get_bool
: NULL
,
2361 set
? property_set_bool
: NULL
,
2362 property_release_data
,
2367 object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
2368 bool (*get
)(Object
*, Error
**),
2369 void (*set
)(Object
*, bool, Error
**))
2371 BoolProperty
*prop
= g_malloc0(sizeof(*prop
));
2376 return object_class_property_add(klass
, name
, "bool",
2377 get
? property_get_bool
: NULL
,
2378 set
? property_set_bool
: NULL
,
2383 static void property_get_enum(Object
*obj
, Visitor
*v
, const char *name
,
2384 void *opaque
, Error
**errp
)
2386 EnumProperty
*prop
= opaque
;
2390 value
= prop
->get(obj
, &err
);
2392 error_propagate(errp
, err
);
2396 visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
);
2399 static void property_set_enum(Object
*obj
, Visitor
*v
, const char *name
,
2400 void *opaque
, Error
**errp
)
2402 EnumProperty
*prop
= opaque
;
2405 if (!visit_type_enum(v
, name
, &value
, prop
->lookup
, errp
)) {
2408 prop
->set(obj
, value
, errp
);
2412 object_property_add_enum(Object
*obj
, const char *name
,
2413 const char *typename
,
2414 const QEnumLookup
*lookup
,
2415 int (*get
)(Object
*, Error
**),
2416 void (*set
)(Object
*, int, Error
**))
2418 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2420 prop
->lookup
= lookup
;
2424 return object_property_add(obj
, name
, typename
,
2425 get
? property_get_enum
: NULL
,
2426 set
? property_set_enum
: NULL
,
2427 property_release_data
,
2432 object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
2433 const char *typename
,
2434 const QEnumLookup
*lookup
,
2435 int (*get
)(Object
*, Error
**),
2436 void (*set
)(Object
*, int, Error
**))
2438 EnumProperty
*prop
= g_malloc(sizeof(*prop
));
2440 prop
->lookup
= lookup
;
2444 return object_class_property_add(klass
, name
, typename
,
2445 get
? property_get_enum
: NULL
,
2446 set
? property_set_enum
: NULL
,
2451 typedef struct TMProperty
{
2452 void (*get
)(Object
*, struct tm
*, Error
**);
2455 static void property_get_tm(Object
*obj
, Visitor
*v
, const char *name
,
2456 void *opaque
, Error
**errp
)
2458 TMProperty
*prop
= opaque
;
2462 prop
->get(obj
, &value
, &err
);
2464 error_propagate(errp
, err
);
2468 if (!visit_start_struct(v
, name
, NULL
, 0, errp
)) {
2471 if (!visit_type_int32(v
, "tm_year", &value
.tm_year
, errp
)) {
2474 if (!visit_type_int32(v
, "tm_mon", &value
.tm_mon
, errp
)) {
2477 if (!visit_type_int32(v
, "tm_mday", &value
.tm_mday
, errp
)) {
2480 if (!visit_type_int32(v
, "tm_hour", &value
.tm_hour
, errp
)) {
2483 if (!visit_type_int32(v
, "tm_min", &value
.tm_min
, errp
)) {
2486 if (!visit_type_int32(v
, "tm_sec", &value
.tm_sec
, errp
)) {
2489 visit_check_struct(v
, errp
);
2491 visit_end_struct(v
, NULL
);
2495 object_property_add_tm(Object
*obj
, const char *name
,
2496 void (*get
)(Object
*, struct tm
*, Error
**))
2498 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2502 return object_property_add(obj
, name
, "struct tm",
2503 get
? property_get_tm
: NULL
, NULL
,
2504 property_release_data
,
2509 object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
2510 void (*get
)(Object
*, struct tm
*, Error
**))
2512 TMProperty
*prop
= g_malloc0(sizeof(*prop
));
2516 return object_class_property_add(klass
, name
, "struct tm",
2517 get
? property_get_tm
: NULL
,
2521 static char *object_get_type(Object
*obj
, Error
**errp
)
2523 return g_strdup(object_get_typename(obj
));
2526 static void property_get_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2527 void *opaque
, Error
**errp
)
2529 uint8_t value
= *(uint8_t *)opaque
;
2530 visit_type_uint8(v
, name
, &value
, errp
);
2533 static void property_set_uint8_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2534 void *opaque
, Error
**errp
)
2536 uint8_t *field
= opaque
;
2539 if (!visit_type_uint8(v
, name
, &value
, errp
)) {
2546 static void property_get_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2547 void *opaque
, Error
**errp
)
2549 uint16_t value
= *(uint16_t *)opaque
;
2550 visit_type_uint16(v
, name
, &value
, errp
);
2553 static void property_set_uint16_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2554 void *opaque
, Error
**errp
)
2556 uint16_t *field
= opaque
;
2559 if (!visit_type_uint16(v
, name
, &value
, errp
)) {
2566 static void property_get_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2567 void *opaque
, Error
**errp
)
2569 uint32_t value
= *(uint32_t *)opaque
;
2570 visit_type_uint32(v
, name
, &value
, errp
);
2573 static void property_set_uint32_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2574 void *opaque
, Error
**errp
)
2576 uint32_t *field
= opaque
;
2579 if (!visit_type_uint32(v
, name
, &value
, errp
)) {
2586 static void property_get_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2587 void *opaque
, Error
**errp
)
2589 uint64_t value
= *(uint64_t *)opaque
;
2590 visit_type_uint64(v
, name
, &value
, errp
);
2593 static void property_set_uint64_ptr(Object
*obj
, Visitor
*v
, const char *name
,
2594 void *opaque
, Error
**errp
)
2596 uint64_t *field
= opaque
;
2599 if (!visit_type_uint64(v
, name
, &value
, errp
)) {
2607 object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2609 ObjectPropertyFlags flags
)
2611 ObjectPropertyAccessor
*getter
= NULL
;
2612 ObjectPropertyAccessor
*setter
= NULL
;
2614 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2615 getter
= property_get_uint8_ptr
;
2618 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2619 setter
= property_set_uint8_ptr
;
2622 return object_property_add(obj
, name
, "uint8",
2623 getter
, setter
, NULL
, (void *)v
);
2627 object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
2629 ObjectPropertyFlags flags
)
2631 ObjectPropertyAccessor
*getter
= NULL
;
2632 ObjectPropertyAccessor
*setter
= NULL
;
2634 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2635 getter
= property_get_uint8_ptr
;
2638 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2639 setter
= property_set_uint8_ptr
;
2642 return object_class_property_add(klass
, name
, "uint8",
2643 getter
, setter
, NULL
, (void *)v
);
2647 object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2649 ObjectPropertyFlags flags
)
2651 ObjectPropertyAccessor
*getter
= NULL
;
2652 ObjectPropertyAccessor
*setter
= NULL
;
2654 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2655 getter
= property_get_uint16_ptr
;
2658 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2659 setter
= property_set_uint16_ptr
;
2662 return object_property_add(obj
, name
, "uint16",
2663 getter
, setter
, NULL
, (void *)v
);
2667 object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
2669 ObjectPropertyFlags flags
)
2671 ObjectPropertyAccessor
*getter
= NULL
;
2672 ObjectPropertyAccessor
*setter
= NULL
;
2674 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2675 getter
= property_get_uint16_ptr
;
2678 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2679 setter
= property_set_uint16_ptr
;
2682 return object_class_property_add(klass
, name
, "uint16",
2683 getter
, setter
, NULL
, (void *)v
);
2687 object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2689 ObjectPropertyFlags flags
)
2691 ObjectPropertyAccessor
*getter
= NULL
;
2692 ObjectPropertyAccessor
*setter
= NULL
;
2694 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2695 getter
= property_get_uint32_ptr
;
2698 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2699 setter
= property_set_uint32_ptr
;
2702 return object_property_add(obj
, name
, "uint32",
2703 getter
, setter
, NULL
, (void *)v
);
2707 object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
2709 ObjectPropertyFlags flags
)
2711 ObjectPropertyAccessor
*getter
= NULL
;
2712 ObjectPropertyAccessor
*setter
= NULL
;
2714 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2715 getter
= property_get_uint32_ptr
;
2718 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2719 setter
= property_set_uint32_ptr
;
2722 return object_class_property_add(klass
, name
, "uint32",
2723 getter
, setter
, NULL
, (void *)v
);
2727 object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2729 ObjectPropertyFlags flags
)
2731 ObjectPropertyAccessor
*getter
= NULL
;
2732 ObjectPropertyAccessor
*setter
= NULL
;
2734 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2735 getter
= property_get_uint64_ptr
;
2738 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2739 setter
= property_set_uint64_ptr
;
2742 return object_property_add(obj
, name
, "uint64",
2743 getter
, setter
, NULL
, (void *)v
);
2747 object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
2749 ObjectPropertyFlags flags
)
2751 ObjectPropertyAccessor
*getter
= NULL
;
2752 ObjectPropertyAccessor
*setter
= NULL
;
2754 if ((flags
& OBJ_PROP_FLAG_READ
) == OBJ_PROP_FLAG_READ
) {
2755 getter
= property_get_uint64_ptr
;
2758 if ((flags
& OBJ_PROP_FLAG_WRITE
) == OBJ_PROP_FLAG_WRITE
) {
2759 setter
= property_set_uint64_ptr
;
2762 return object_class_property_add(klass
, name
, "uint64",
2763 getter
, setter
, NULL
, (void *)v
);
2771 static void property_get_alias(Object
*obj
, Visitor
*v
, const char *name
,
2772 void *opaque
, Error
**errp
)
2774 AliasProperty
*prop
= opaque
;
2775 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2777 object_property_get(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2778 visit_free(alias_v
);
2781 static void property_set_alias(Object
*obj
, Visitor
*v
, const char *name
,
2782 void *opaque
, Error
**errp
)
2784 AliasProperty
*prop
= opaque
;
2785 Visitor
*alias_v
= visitor_forward_field(v
, prop
->target_name
, name
);
2787 object_property_set(prop
->target_obj
, prop
->target_name
, alias_v
, errp
);
2788 visit_free(alias_v
);
2791 static Object
*property_resolve_alias(Object
*obj
, void *opaque
,
2794 AliasProperty
*prop
= opaque
;
2796 return object_resolve_path_component(prop
->target_obj
, prop
->target_name
);
2799 static void property_release_alias(Object
*obj
, const char *name
, void *opaque
)
2801 AliasProperty
*prop
= opaque
;
2803 g_free(prop
->target_name
);
2808 object_property_add_alias(Object
*obj
, const char *name
,
2809 Object
*target_obj
, const char *target_name
)
2811 AliasProperty
*prop
;
2813 ObjectProperty
*target_prop
;
2814 g_autofree
char *prop_type
= NULL
;
2816 target_prop
= object_property_find_err(target_obj
, target_name
,
2819 if (object_property_is_child(target_prop
)) {
2820 prop_type
= g_strdup_printf("link%s",
2821 target_prop
->type
+ strlen("child"));
2823 prop_type
= g_strdup(target_prop
->type
);
2826 prop
= g_malloc(sizeof(*prop
));
2827 prop
->target_obj
= target_obj
;
2828 prop
->target_name
= g_strdup(target_name
);
2830 op
= object_property_add(obj
, name
, prop_type
,
2833 property_release_alias
,
2835 op
->resolve
= property_resolve_alias
;
2836 if (target_prop
->defval
) {
2837 op
->defval
= qobject_ref(target_prop
->defval
);
2840 object_property_set_description(obj
, op
->name
,
2841 target_prop
->description
);
2845 void object_property_set_description(Object
*obj
, const char *name
,
2846 const char *description
)
2850 op
= object_property_find_err(obj
, name
, &error_abort
);
2851 g_free(op
->description
);
2852 op
->description
= g_strdup(description
);
2855 void object_class_property_set_description(ObjectClass
*klass
,
2857 const char *description
)
2861 op
= g_hash_table_lookup(klass
->properties
, name
);
2862 g_free(op
->description
);
2863 op
->description
= g_strdup(description
);
2866 static void object_class_init(ObjectClass
*klass
, void *data
)
2868 object_class_property_add_str(klass
, "type", object_get_type
,
2872 static void register_types(void)
2874 static const TypeInfo interface_info
= {
2875 .name
= TYPE_INTERFACE
,
2876 .class_size
= sizeof(InterfaceClass
),
2880 static const TypeInfo object_info
= {
2881 .name
= TYPE_OBJECT
,
2882 .instance_size
= sizeof(Object
),
2883 .class_init
= object_class_init
,
2887 type_interface
= type_register_internal(&interface_info
);
2888 type_register_internal(&object_info
);
2891 type_init(register_types
)