]> Git Repo - qemu.git/blame - qom/object.c
target/arm/kvm: Use CPUState::kvm_state in kvm_arm_pmu_supported()
[qemu.git] / qom / object.c
CommitLineData
2f28d2ff
AL
1/*
2 * QEMU Object Model
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <[email protected]>
8 *
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.
11 */
12
9bbc853b 13#include "qemu/osdep.h"
13d4ff07 14#include "hw/qdev-core.h"
da34e65c 15#include "qapi/error.h"
14cccb61 16#include "qom/object.h"
a31bdae5 17#include "qom/object_interfaces.h"
f348b6d1 18#include "qemu/cutils.h"
7b1b5d19 19#include "qapi/visitor.h"
b2cd7dee
PB
20#include "qapi/string-input-visitor.h"
21#include "qapi/string-output-visitor.h"
eb815e24 22#include "qapi/qapi-builtin-visit.h"
7b1b5d19 23#include "qapi/qmp/qerror.h"
fa131d94 24#include "trace.h"
2f28d2ff 25
7b7b7d18
PB
26/* TODO: replace QObject with a simpler visitor to avoid a dependency
27 * of the QOM core on QObject? */
14cccb61 28#include "qom/qom-qobject.h"
7b1b5d19 29#include "qapi/qmp/qbool.h"
15280c36 30#include "qapi/qmp/qnum.h"
7b1b5d19 31#include "qapi/qmp/qstring.h"
e02bdf1c 32#include "qemu/error-report.h"
7b7b7d18 33
2f28d2ff
AL
34#define MAX_INTERFACES 32
35
36typedef struct InterfaceImpl InterfaceImpl;
37typedef struct TypeImpl TypeImpl;
38
39struct InterfaceImpl
40{
33e95c63 41 const char *typename;
2f28d2ff
AL
42};
43
44struct TypeImpl
45{
46 const char *name;
47
48 size_t class_size;
49
50 size_t instance_size;
51
52 void (*class_init)(ObjectClass *klass, void *data);
3b50e311 53 void (*class_base_init)(ObjectClass *klass, void *data);
2f28d2ff
AL
54
55 void *class_data;
56
57 void (*instance_init)(Object *obj);
8231c2dd 58 void (*instance_post_init)(Object *obj);
2f28d2ff
AL
59 void (*instance_finalize)(Object *obj);
60
61 bool abstract;
62
63 const char *parent;
64 TypeImpl *parent_type;
65
66 ObjectClass *class;
67
68 int num_interfaces;
69 InterfaceImpl interfaces[MAX_INTERFACES];
70};
71
9970bd88
PB
72static Type type_interface;
73
2f28d2ff
AL
74static GHashTable *type_table_get(void)
75{
76 static GHashTable *type_table;
77
78 if (type_table == NULL) {
79 type_table = g_hash_table_new(g_str_hash, g_str_equal);
80 }
81
82 return type_table;
83}
84
f54c19ca
HP
85static bool enumerating_types;
86
2f28d2ff
AL
87static void type_table_add(TypeImpl *ti)
88{
f54c19ca 89 assert(!enumerating_types);
2f28d2ff
AL
90 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
91}
92
93static TypeImpl *type_table_lookup(const char *name)
94{
95 return g_hash_table_lookup(type_table_get(), name);
96}
97
b061dc41 98static TypeImpl *type_new(const TypeInfo *info)
2f28d2ff
AL
99{
100 TypeImpl *ti = g_malloc0(sizeof(*ti));
33e95c63 101 int i;
2f28d2ff
AL
102
103 g_assert(info->name != NULL);
104
73093354
AL
105 if (type_table_lookup(info->name) != NULL) {
106 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
107 abort();
108 }
109
2f28d2ff
AL
110 ti->name = g_strdup(info->name);
111 ti->parent = g_strdup(info->parent);
112
113 ti->class_size = info->class_size;
114 ti->instance_size = info->instance_size;
115
116 ti->class_init = info->class_init;
3b50e311 117 ti->class_base_init = info->class_base_init;
2f28d2ff
AL
118 ti->class_data = info->class_data;
119
120 ti->instance_init = info->instance_init;
8231c2dd 121 ti->instance_post_init = info->instance_post_init;
2f28d2ff
AL
122 ti->instance_finalize = info->instance_finalize;
123
124 ti->abstract = info->abstract;
125
33e95c63
AL
126 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
127 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
2f28d2ff 128 }
33e95c63 129 ti->num_interfaces = i;
2f28d2ff 130
b061dc41
PB
131 return ti;
132}
2f28d2ff 133
b061dc41
PB
134static TypeImpl *type_register_internal(const TypeInfo *info)
135{
136 TypeImpl *ti;
137 ti = type_new(info);
138
139 type_table_add(ti);
2f28d2ff
AL
140 return ti;
141}
142
049cb3cf
PB
143TypeImpl *type_register(const TypeInfo *info)
144{
145 assert(info->parent);
146 return type_register_internal(info);
147}
148
2f28d2ff
AL
149TypeImpl *type_register_static(const TypeInfo *info)
150{
151 return type_register(info);
152}
153
aa04c9d2
IM
154void type_register_static_array(const TypeInfo *infos, int nr_infos)
155{
156 int i;
157
158 for (i = 0; i < nr_infos; i++) {
159 type_register_static(&infos[i]);
160 }
161}
162
2f28d2ff
AL
163static TypeImpl *type_get_by_name(const char *name)
164{
165 if (name == NULL) {
166 return NULL;
167 }
168
169 return type_table_lookup(name);
170}
171
172static TypeImpl *type_get_parent(TypeImpl *type)
173{
174 if (!type->parent_type && type->parent) {
175 type->parent_type = type_get_by_name(type->parent);
176 g_assert(type->parent_type != NULL);
177 }
178
179 return type->parent_type;
180}
181
182static bool type_has_parent(TypeImpl *type)
183{
184 return (type->parent != NULL);
185}
186
187static size_t type_class_get_size(TypeImpl *ti)
188{
189 if (ti->class_size) {
190 return ti->class_size;
191 }
192
193 if (type_has_parent(ti)) {
194 return type_class_get_size(type_get_parent(ti));
195 }
196
197 return sizeof(ObjectClass);
198}
199
aca59af6
IM
200static size_t type_object_get_size(TypeImpl *ti)
201{
202 if (ti->instance_size) {
203 return ti->instance_size;
204 }
205
206 if (type_has_parent(ti)) {
207 return type_object_get_size(type_get_parent(ti));
208 }
209
210 return 0;
211}
212
3f97b53a
BR
213size_t object_type_get_instance_size(const char *typename)
214{
215 TypeImpl *type = type_get_by_name(typename);
216
217 g_assert(type != NULL);
218 return type_object_get_size(type);
219}
220
33e95c63 221static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
2f28d2ff 222{
33e95c63
AL
223 assert(target_type);
224
b30d8054 225 /* Check if target_type is a direct ancestor of type */
33e95c63
AL
226 while (type) {
227 if (type == target_type) {
228 return true;
229 }
2f28d2ff 230
33e95c63
AL
231 type = type_get_parent(type);
232 }
233
234 return false;
235}
236
237static void type_initialize(TypeImpl *ti);
238
b061dc41
PB
239static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
240 TypeImpl *parent_type)
33e95c63
AL
241{
242 InterfaceClass *new_iface;
243 TypeInfo info = { };
244 TypeImpl *iface_impl;
245
b061dc41
PB
246 info.parent = parent_type->name;
247 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
33e95c63
AL
248 info.abstract = true;
249
b061dc41
PB
250 iface_impl = type_new(&info);
251 iface_impl->parent_type = parent_type;
33e95c63
AL
252 type_initialize(iface_impl);
253 g_free((char *)info.name);
254
255 new_iface = (InterfaceClass *)iface_impl->class;
256 new_iface->concrete_class = ti->class;
b061dc41 257 new_iface->interface_type = interface_type;
33e95c63
AL
258
259 ti->class->interfaces = g_slist_append(ti->class->interfaces,
260 iface_impl->class);
2f28d2ff
AL
261}
262
16bf7f52
DB
263static void object_property_free(gpointer data)
264{
265 ObjectProperty *prop = data;
266
267 g_free(prop->name);
268 g_free(prop->type);
269 g_free(prop->description);
270 g_free(prop);
271}
272
ac451033 273static void type_initialize(TypeImpl *ti)
2f28d2ff 274{
745549c8 275 TypeImpl *parent;
2f28d2ff
AL
276
277 if (ti->class) {
278 return;
279 }
280
281 ti->class_size = type_class_get_size(ti);
aca59af6 282 ti->instance_size = type_object_get_size(ti);
1c6d75d5
EH
283 /* Any type with zero instance_size is implicitly abstract.
284 * This means interface types are all abstract.
285 */
286 if (ti->instance_size == 0) {
287 ti->abstract = true;
288 }
422ca143
MAL
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);
296 }
2f28d2ff 297 ti->class = g_malloc0(ti->class_size);
2f28d2ff 298
745549c8
PB
299 parent = type_get_parent(ti);
300 if (parent) {
ac451033 301 type_initialize(parent);
33e95c63
AL
302 GSList *e;
303 int i;
2f28d2ff 304
719a3077 305 g_assert(parent->class_size <= ti->class_size);
745549c8 306 memcpy(ti->class, parent->class, parent->class_size);
3e407de4 307 ti->class->interfaces = NULL;
16bf7f52
DB
308 ti->class->properties = g_hash_table_new_full(
309 g_str_hash, g_str_equal, g_free, object_property_free);
33e95c63
AL
310
311 for (e = parent->class->interfaces; e; e = e->next) {
b061dc41
PB
312 InterfaceClass *iface = e->data;
313 ObjectClass *klass = OBJECT_CLASS(iface);
314
315 type_initialize_interface(ti, iface->interface_type, klass->type);
33e95c63
AL
316 }
317
318 for (i = 0; i < ti->num_interfaces; i++) {
319 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
a9ee3a9e
PMD
320 if (!t) {
321 error_report("missing interface '%s' for object '%s'",
322 ti->interfaces[i].typename, parent->name);
323 abort();
324 }
33e95c63
AL
325 for (e = ti->class->interfaces; e; e = e->next) {
326 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
327
328 if (type_is_ancestor(target_type, t)) {
329 break;
330 }
331 }
332
333 if (e) {
334 continue;
335 }
336
b061dc41 337 type_initialize_interface(ti, t, t);
33e95c63 338 }
16bf7f52
DB
339 } else {
340 ti->class->properties = g_hash_table_new_full(
341 g_str_hash, g_str_equal, g_free, object_property_free);
745549c8 342 }
2f28d2ff 343
745549c8 344 ti->class->type = ti;
3b50e311 345
745549c8
PB
346 while (parent) {
347 if (parent->class_base_init) {
348 parent->class_base_init(ti->class, ti->class_data);
3b50e311 349 }
745549c8 350 parent = type_get_parent(parent);
2f28d2ff
AL
351 }
352
2f28d2ff
AL
353 if (ti->class_init) {
354 ti->class_init(ti->class, ti->class_data);
355 }
2f28d2ff
AL
356}
357
358static void object_init_with_type(Object *obj, TypeImpl *ti)
359{
2f28d2ff
AL
360 if (type_has_parent(ti)) {
361 object_init_with_type(obj, type_get_parent(ti));
362 }
363
2f28d2ff
AL
364 if (ti->instance_init) {
365 ti->instance_init(obj);
366 }
367}
368
8231c2dd
EH
369static void object_post_init_with_type(Object *obj, TypeImpl *ti)
370{
371 if (ti->instance_post_init) {
372 ti->instance_post_init(obj);
373 }
374
375 if (type_has_parent(ti)) {
376 object_post_init_with_type(obj, type_get_parent(ti));
377 }
378}
379
ea9ce893
MAL
380void object_apply_global_props(Object *obj, const GPtrArray *props, Error **errp)
381{
ea9ce893
MAL
382 int i;
383
384 if (!props) {
385 return;
386 }
387
388 for (i = 0; i < props->len; i++) {
389 GlobalProperty *p = g_ptr_array_index(props, i);
d769f0df 390 Error *err = NULL;
ea9ce893
MAL
391
392 if (object_dynamic_cast(obj, p->driver) == NULL) {
393 continue;
394 }
92fd453c
DDAG
395 if (p->optional && !object_property_find(obj, p->property, NULL)) {
396 continue;
397 }
ea9ce893
MAL
398 p->used = true;
399 object_property_parse(obj, p->value, p->property, &err);
400 if (err != NULL) {
401 error_prepend(&err, "can't apply global %s.%s=%s: ",
402 p->driver, p->property, p->value);
50545b2c
MAL
403 /*
404 * If errp != NULL, propagate error and return.
405 * If errp == NULL, report a warning, but keep going
406 * with the remaining globals.
407 */
408 if (errp) {
409 error_propagate(errp, err);
410 return;
411 } else {
412 warn_report_err(err);
413 }
ea9ce893
MAL
414 }
415 }
416}
417
617902af
MA
418/*
419 * Global property defaults
420 * Slot 0: accelerator's global property defaults
421 * Slot 1: machine's global property defaults
1fff3c20 422 * Slot 2: global properties from legacy command line option
617902af
MA
423 * Each is a GPtrArray of of GlobalProperty.
424 * Applied in order, later entries override earlier ones.
425 */
1fff3c20
PB
426static GPtrArray *object_compat_props[3];
427
428/*
429 * Retrieve @GPtrArray for global property defined with options
430 * other than "-global". These are generally used for syntactic
431 * sugar and legacy command line options.
432 */
433void object_register_sugar_prop(const char *driver, const char *prop, const char *value)
434{
435 GlobalProperty *g;
436 if (!object_compat_props[2]) {
437 object_compat_props[2] = g_ptr_array_new();
438 }
439 g = g_new0(GlobalProperty, 1);
440 g->driver = g_strdup(driver);
441 g->property = g_strdup(prop);
442 g->value = g_strdup(value);
443 g_ptr_array_add(object_compat_props[2], g);
444}
617902af
MA
445
446/*
447 * Set machine's global property defaults to @compat_props.
448 * May be called at most once.
449 */
450void object_set_machine_compat_props(GPtrArray *compat_props)
451{
452 assert(!object_compat_props[1]);
453 object_compat_props[1] = compat_props;
454}
455
456/*
457 * Set accelerator's global property defaults to @compat_props.
458 * May be called at most once.
459 */
460void object_set_accelerator_compat_props(GPtrArray *compat_props)
461{
462 assert(!object_compat_props[0]);
463 object_compat_props[0] = compat_props;
464}
465
466void object_apply_compat_props(Object *obj)
467{
468 int i;
469
470 for (i = 0; i < ARRAY_SIZE(object_compat_props); i++) {
471 object_apply_global_props(obj, object_compat_props[i],
1fff3c20 472 i == 2 ? &error_fatal : &error_abort);
617902af
MA
473 }
474}
475
63f7b10b 476static void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
2f28d2ff
AL
477{
478 Object *obj = data;
479
ac451033 480 type_initialize(type);
aca59af6 481
719a3077 482 g_assert(type->instance_size >= sizeof(Object));
2f28d2ff 483 g_assert(type->abstract == false);
719a3077 484 g_assert(size >= type->instance_size);
2f28d2ff
AL
485
486 memset(obj, 0, type->instance_size);
487 obj->class = type->class;
764b6312 488 object_ref(obj);
b604a854
PF
489 obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal,
490 NULL, object_property_free);
2f28d2ff 491 object_init_with_type(obj, type);
8231c2dd 492 object_post_init_with_type(obj, type);
2f28d2ff
AL
493}
494
213f0c4f 495void object_initialize(void *data, size_t size, const char *typename)
2f28d2ff
AL
496{
497 TypeImpl *type = type_get_by_name(typename);
498
e02bdf1c
PMD
499 if (!type) {
500 error_report("missing object type '%s'", typename);
501 abort();
502 }
503
5b9237f6 504 object_initialize_with_type(data, size, type);
2f28d2ff
AL
505}
506
0210b39d
TH
507void object_initialize_child(Object *parentobj, const char *propname,
508 void *childobj, size_t size, const char *type,
509 Error **errp, ...)
510{
511 va_list vargs;
512
513 va_start(vargs, errp);
514 object_initialize_childv(parentobj, propname, childobj, size, type, errp,
515 vargs);
516 va_end(vargs);
517}
518
519void object_initialize_childv(Object *parentobj, const char *propname,
520 void *childobj, size_t size, const char *type,
521 Error **errp, va_list vargs)
522{
523 Error *local_err = NULL;
524 Object *obj;
3650b2de 525 UserCreatable *uc;
0210b39d
TH
526
527 object_initialize(childobj, size, type);
528 obj = OBJECT(childobj);
529
530 object_set_propv(obj, &local_err, vargs);
531 if (local_err) {
532 goto out;
533 }
534
535 object_property_add_child(parentobj, propname, obj, &local_err);
536 if (local_err) {
537 goto out;
538 }
539
3650b2de
MAL
540 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
541 if (uc) {
542 user_creatable_complete(uc, &local_err);
0210b39d
TH
543 if (local_err) {
544 object_unparent(obj);
545 goto out;
546 }
547 }
548
549 /*
550 * Since object_property_add_child added a reference to the child object,
551 * we can drop the reference added by object_initialize(), so the child
552 * property will own the only reference to the object.
553 */
554 object_unref(obj);
555
556out:
557 if (local_err) {
558 error_propagate(errp, local_err);
559 object_unref(obj);
560 }
561}
562
5d9d3f47
AF
563static inline bool object_property_is_child(ObjectProperty *prop)
564{
565 return strstart(prop->type, "child<", NULL);
566}
567
57c9fafe
AL
568static void object_property_del_all(Object *obj)
569{
b604a854
PF
570 ObjectProperty *prop;
571 GHashTableIter iter;
572 gpointer key, value;
573 bool released;
574
575 do {
576 released = false;
577 g_hash_table_iter_init(&iter, obj->properties);
578 while (g_hash_table_iter_next(&iter, &key, &value)) {
579 prop = value;
580 if (prop->release) {
581 prop->release(obj, prop->name, prop->opaque);
582 prop->release = NULL;
583 released = true;
584 break;
585 }
586 g_hash_table_iter_remove(&iter);
57c9fafe 587 }
b604a854 588 } while (released);
57c9fafe 589
b604a854 590 g_hash_table_unref(obj->properties);
57c9fafe
AL
591}
592
593static void object_property_del_child(Object *obj, Object *child, Error **errp)
594{
595 ObjectProperty *prop;
b604a854
PF
596 GHashTableIter iter;
597 gpointer key, value;
57c9fafe 598
b604a854
PF
599 g_hash_table_iter_init(&iter, obj->properties);
600 while (g_hash_table_iter_next(&iter, &key, &value)) {
601 prop = value;
602 if (object_property_is_child(prop) && prop->opaque == child) {
603 if (prop->release) {
604 prop->release(obj, prop->name, prop->opaque);
605 prop->release = NULL;
606 }
607 break;
608 }
609 }
610 g_hash_table_iter_init(&iter, obj->properties);
611 while (g_hash_table_iter_next(&iter, &key, &value)) {
612 prop = value;
5d9d3f47 613 if (object_property_is_child(prop) && prop->opaque == child) {
b604a854 614 g_hash_table_iter_remove(&iter);
6c1fdcf9 615 break;
57c9fafe
AL
616 }
617 }
618}
619
620void object_unparent(Object *obj)
621{
e998fa8d
MT
622 if (obj->parent) {
623 object_property_del_child(obj->parent, obj, NULL);
624 }
57c9fafe
AL
625}
626
2f28d2ff
AL
627static void object_deinit(Object *obj, TypeImpl *type)
628{
629 if (type->instance_finalize) {
630 type->instance_finalize(obj);
631 }
632
2f28d2ff
AL
633 if (type_has_parent(type)) {
634 object_deinit(obj, type_get_parent(type));
635 }
636}
637
339c2708 638static void object_finalize(void *data)
2f28d2ff
AL
639{
640 Object *obj = data;
641 TypeImpl *ti = obj->class->type;
642
57c9fafe 643 object_property_del_all(obj);
76a6e1cc 644 object_deinit(obj, ti);
db85b575 645
719a3077 646 g_assert(obj->ref == 0);
fde9bf44
PB
647 if (obj->free) {
648 obj->free(obj);
649 }
2f28d2ff
AL
650}
651
63f7b10b 652static Object *object_new_with_type(Type type)
2f28d2ff
AL
653{
654 Object *obj;
655
656 g_assert(type != NULL);
ac451033 657 type_initialize(type);
2f28d2ff
AL
658
659 obj = g_malloc(type->instance_size);
5b9237f6 660 object_initialize_with_type(obj, type->instance_size, type);
fde9bf44 661 obj->free = g_free;
2f28d2ff
AL
662
663 return obj;
664}
665
3c75e12e
PB
666Object *object_new_with_class(ObjectClass *klass)
667{
668 return object_new_with_type(klass->type);
669}
670
2f28d2ff
AL
671Object *object_new(const char *typename)
672{
673 TypeImpl *ti = type_get_by_name(typename);
674
675 return object_new_with_type(ti);
676}
677
a31bdae5
DB
678
679Object *object_new_with_props(const char *typename,
680 Object *parent,
681 const char *id,
682 Error **errp,
683 ...)
684{
685 va_list vargs;
686 Object *obj;
687
688 va_start(vargs, errp);
689 obj = object_new_with_propv(typename, parent, id, errp, vargs);
690 va_end(vargs);
691
692 return obj;
693}
694
695
696Object *object_new_with_propv(const char *typename,
697 Object *parent,
698 const char *id,
699 Error **errp,
700 va_list vargs)
701{
702 Object *obj;
703 ObjectClass *klass;
704 Error *local_err = NULL;
3650b2de 705 UserCreatable *uc;
a31bdae5
DB
706
707 klass = object_class_by_name(typename);
708 if (!klass) {
709 error_setg(errp, "invalid object type: %s", typename);
710 return NULL;
711 }
712
713 if (object_class_is_abstract(klass)) {
714 error_setg(errp, "object type '%s' is abstract", typename);
715 return NULL;
716 }
66e1155a 717 obj = object_new_with_type(klass->type);
a31bdae5
DB
718
719 if (object_set_propv(obj, &local_err, vargs) < 0) {
720 goto error;
721 }
722
6134d752
DB
723 if (id != NULL) {
724 object_property_add_child(parent, id, obj, &local_err);
725 if (local_err) {
726 goto error;
727 }
a31bdae5
DB
728 }
729
3650b2de
MAL
730 uc = (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
731 if (uc) {
732 user_creatable_complete(uc, &local_err);
a31bdae5 733 if (local_err) {
6134d752
DB
734 if (id != NULL) {
735 object_unparent(obj);
736 }
a31bdae5
DB
737 goto error;
738 }
739 }
740
741 object_unref(OBJECT(obj));
742 return obj;
743
744 error:
621ff94d 745 error_propagate(errp, local_err);
a31bdae5
DB
746 object_unref(obj);
747 return NULL;
748}
749
750
751int object_set_props(Object *obj,
752 Error **errp,
753 ...)
754{
755 va_list vargs;
756 int ret;
757
758 va_start(vargs, errp);
759 ret = object_set_propv(obj, errp, vargs);
760 va_end(vargs);
761
762 return ret;
763}
764
765
766int object_set_propv(Object *obj,
767 Error **errp,
768 va_list vargs)
769{
770 const char *propname;
771 Error *local_err = NULL;
772
773 propname = va_arg(vargs, char *);
774 while (propname != NULL) {
775 const char *value = va_arg(vargs, char *);
776
777 g_assert(value != NULL);
778 object_property_parse(obj, value, propname, &local_err);
779 if (local_err) {
780 error_propagate(errp, local_err);
781 return -1;
782 }
783 propname = va_arg(vargs, char *);
784 }
785
786 return 0;
787}
788
789
2f28d2ff
AL
790Object *object_dynamic_cast(Object *obj, const char *typename)
791{
b7f43fe4 792 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
acc4af3f
PB
793 return obj;
794 }
795
2f28d2ff
AL
796 return NULL;
797}
798
be17f18b
PB
799Object *object_dynamic_cast_assert(Object *obj, const char *typename,
800 const char *file, int line, const char *func)
2f28d2ff 801{
fa131d94
PB
802 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
803 typename, file, line, func);
804
3556c233 805#ifdef CONFIG_QOM_CAST_DEBUG
03587328
AL
806 int i;
807 Object *inst;
808
95916abc 809 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
b6b3ccfd 810 if (atomic_read(&obj->class->object_cast_cache[i]) == typename) {
03587328
AL
811 goto out;
812 }
813 }
814
815 inst = object_dynamic_cast(obj, typename);
2f28d2ff 816
b7f43fe4 817 if (!inst && obj) {
be17f18b
PB
818 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
819 file, line, func, obj, typename);
2f28d2ff
AL
820 abort();
821 }
822
3556c233 823 assert(obj == inst);
03587328 824
95916abc 825 if (obj && obj == inst) {
03587328 826 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
b6b3ccfd
AB
827 atomic_set(&obj->class->object_cast_cache[i - 1],
828 atomic_read(&obj->class->object_cast_cache[i]));
03587328 829 }
b6b3ccfd 830 atomic_set(&obj->class->object_cast_cache[i - 1], typename);
03587328
AL
831 }
832
833out:
3556c233
PB
834#endif
835 return obj;
2f28d2ff
AL
836}
837
838ObjectClass *object_class_dynamic_cast(ObjectClass *class,
839 const char *typename)
840{
33e95c63 841 ObjectClass *ret = NULL;
bf0fda34
PB
842 TypeImpl *target_type;
843 TypeImpl *type;
2f28d2ff 844
bf0fda34
PB
845 if (!class) {
846 return NULL;
847 }
848
793c96b5 849 /* A simple fast path that can trigger a lot for leaf classes. */
bf0fda34 850 type = class->type;
793c96b5
PB
851 if (type->name == typename) {
852 return class;
853 }
854
bf0fda34 855 target_type = type_get_by_name(typename);
9ab880b3
AG
856 if (!target_type) {
857 /* target class type unknown, so fail the cast */
858 return NULL;
859 }
860
00e2ceae
PC
861 if (type->class->interfaces &&
862 type_is_ancestor(target_type, type_interface)) {
33e95c63
AL
863 int found = 0;
864 GSList *i;
2f28d2ff 865
33e95c63
AL
866 for (i = class->interfaces; i; i = i->next) {
867 ObjectClass *target_class = i->data;
868
869 if (type_is_ancestor(target_class->type, target_type)) {
870 ret = target_class;
871 found++;
872 }
873 }
874
875 /* The match was ambiguous, don't allow a cast */
876 if (found > 1) {
877 ret = NULL;
878 }
879 } else if (type_is_ancestor(type, target_type)) {
880 ret = class;
2f28d2ff
AL
881 }
882
33e95c63 883 return ret;
2f28d2ff
AL
884}
885
886ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
be17f18b
PB
887 const char *typename,
888 const char *file, int line,
889 const char *func)
2f28d2ff 890{
fa131d94
PB
891 ObjectClass *ret;
892
893 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
894 typename, file, line, func);
2f28d2ff 895
03587328
AL
896#ifdef CONFIG_QOM_CAST_DEBUG
897 int i;
898
9d6a3d58 899 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
b6b3ccfd 900 if (atomic_read(&class->class_cast_cache[i]) == typename) {
03587328
AL
901 ret = class;
902 goto out;
903 }
904 }
905#else
9d6a3d58 906 if (!class || !class->interfaces) {
3556c233
PB
907 return class;
908 }
909#endif
910
fa131d94 911 ret = object_class_dynamic_cast(class, typename);
bf0fda34 912 if (!ret && class) {
be17f18b
PB
913 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
914 file, line, func, class, typename);
2f28d2ff
AL
915 abort();
916 }
917
03587328 918#ifdef CONFIG_QOM_CAST_DEBUG
9d6a3d58 919 if (class && ret == class) {
03587328 920 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
b6b3ccfd
AB
921 atomic_set(&class->class_cast_cache[i - 1],
922 atomic_read(&class->class_cast_cache[i]));
03587328 923 }
b6b3ccfd 924 atomic_set(&class->class_cast_cache[i - 1], typename);
03587328
AL
925 }
926out:
927#endif
2f28d2ff
AL
928 return ret;
929}
930
8f5d58ef 931const char *object_get_typename(const Object *obj)
2f28d2ff
AL
932{
933 return obj->class->type->name;
934}
935
936ObjectClass *object_get_class(Object *obj)
937{
938 return obj->class;
939}
940
17862378
AF
941bool object_class_is_abstract(ObjectClass *klass)
942{
943 return klass->type->abstract;
944}
945
2f28d2ff
AL
946const char *object_class_get_name(ObjectClass *klass)
947{
948 return klass->type->name;
949}
950
951ObjectClass *object_class_by_name(const char *typename)
952{
953 TypeImpl *type = type_get_by_name(typename);
954
955 if (!type) {
956 return NULL;
957 }
958
ac451033 959 type_initialize(type);
2f28d2ff
AL
960
961 return type->class;
962}
963
e7cce67f
PB
964ObjectClass *object_class_get_parent(ObjectClass *class)
965{
966 TypeImpl *type = type_get_parent(class->type);
967
968 if (!type) {
969 return NULL;
970 }
971
972 type_initialize(type);
973
974 return type->class;
975}
976
2f28d2ff
AL
977typedef struct OCFData
978{
979 void (*fn)(ObjectClass *klass, void *opaque);
93c511a1
AL
980 const char *implements_type;
981 bool include_abstract;
2f28d2ff
AL
982 void *opaque;
983} OCFData;
984
985static void object_class_foreach_tramp(gpointer key, gpointer value,
986 gpointer opaque)
987{
988 OCFData *data = opaque;
989 TypeImpl *type = value;
93c511a1 990 ObjectClass *k;
2f28d2ff 991
ac451033 992 type_initialize(type);
93c511a1 993 k = type->class;
2f28d2ff 994
93c511a1
AL
995 if (!data->include_abstract && type->abstract) {
996 return;
997 }
998
999 if (data->implements_type &&
1000 !object_class_dynamic_cast(k, data->implements_type)) {
1001 return;
1002 }
1003
1004 data->fn(k, data->opaque);
2f28d2ff
AL
1005}
1006
1007void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 1008 const char *implements_type, bool include_abstract,
2f28d2ff
AL
1009 void *opaque)
1010{
93c511a1 1011 OCFData data = { fn, implements_type, include_abstract, opaque };
2f28d2ff 1012
f54c19ca 1013 enumerating_types = true;
2f28d2ff 1014 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
f54c19ca 1015 enumerating_types = false;
2f28d2ff 1016}
57c9fafe 1017
d714b8de
PC
1018static int do_object_child_foreach(Object *obj,
1019 int (*fn)(Object *child, void *opaque),
1020 void *opaque, bool recurse)
32efc535 1021{
b604a854
PF
1022 GHashTableIter iter;
1023 ObjectProperty *prop;
32efc535
PB
1024 int ret = 0;
1025
b604a854
PF
1026 g_hash_table_iter_init(&iter, obj->properties);
1027 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
32efc535 1028 if (object_property_is_child(prop)) {
d714b8de
PC
1029 Object *child = prop->opaque;
1030
1031 ret = fn(child, opaque);
32efc535
PB
1032 if (ret != 0) {
1033 break;
1034 }
d714b8de
PC
1035 if (recurse) {
1036 do_object_child_foreach(child, fn, opaque, true);
1037 }
32efc535
PB
1038 }
1039 }
1040 return ret;
1041}
1042
d714b8de
PC
1043int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1044 void *opaque)
1045{
1046 return do_object_child_foreach(obj, fn, opaque, false);
1047}
1048
1049int object_child_foreach_recursive(Object *obj,
1050 int (*fn)(Object *child, void *opaque),
1051 void *opaque)
1052{
1053 return do_object_child_foreach(obj, fn, opaque, true);
1054}
1055
418ba9e5
AF
1056static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
1057{
1058 GSList **list = opaque;
1059
1060 *list = g_slist_prepend(*list, klass);
1061}
1062
1063GSList *object_class_get_list(const char *implements_type,
1064 bool include_abstract)
1065{
1066 GSList *list = NULL;
1067
1068 object_class_foreach(object_class_get_list_tramp,
1069 implements_type, include_abstract, &list);
1070 return list;
1071}
1072
47c66009
PB
1073static gint object_class_cmp(gconstpointer a, gconstpointer b)
1074{
1075 return strcasecmp(object_class_get_name((ObjectClass *)a),
1076 object_class_get_name((ObjectClass *)b));
1077}
1078
1079GSList *object_class_get_list_sorted(const char *implements_type,
1080 bool include_abstract)
1081{
1082 return g_slist_sort(object_class_get_list(implements_type, include_abstract),
1083 object_class_cmp);
1084}
1085
57c9fafe
AL
1086void object_ref(Object *obj)
1087{
8ffad850
PC
1088 if (!obj) {
1089 return;
1090 }
8438a135 1091 atomic_inc(&obj->ref);
57c9fafe
AL
1092}
1093
1094void object_unref(Object *obj)
1095{
8ffad850
PC
1096 if (!obj) {
1097 return;
1098 }
719a3077 1099 g_assert(obj->ref > 0);
57c9fafe
AL
1100
1101 /* parent always holds a reference to its children */
f08c03f3 1102 if (atomic_fetch_dec(&obj->ref) == 1) {
57c9fafe
AL
1103 object_finalize(obj);
1104 }
1105}
1106
64607d08
PB
1107ObjectProperty *
1108object_property_add(Object *obj, const char *name, const char *type,
1109 ObjectPropertyAccessor *get,
1110 ObjectPropertyAccessor *set,
1111 ObjectPropertyRelease *release,
1112 void *opaque, Error **errp)
57c9fafe 1113{
54852b03 1114 ObjectProperty *prop;
33965904
PC
1115 size_t name_len = strlen(name);
1116
1117 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
1118 int i;
1119 ObjectProperty *ret;
1120 char *name_no_array = g_strdup(name);
1121
1122 name_no_array[name_len - 3] = '\0';
1123 for (i = 0; ; ++i) {
1124 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
1125
1126 ret = object_property_add(obj, full_name, type, get, set,
1127 release, opaque, NULL);
1128 g_free(full_name);
1129 if (ret) {
1130 break;
1131 }
1132 }
1133 g_free(name_no_array);
1134 return ret;
1135 }
54852b03 1136
16bf7f52 1137 if (object_property_find(obj, name, NULL) != NULL) {
d55e937d
GK
1138 error_setg(errp, "attempt to add duplicate property '%s' to object (type '%s')",
1139 name, object_get_typename(obj));
b604a854 1140 return NULL;
54852b03
PM
1141 }
1142
1143 prop = g_malloc0(sizeof(*prop));
57c9fafe
AL
1144
1145 prop->name = g_strdup(name);
1146 prop->type = g_strdup(type);
1147
1148 prop->get = get;
1149 prop->set = set;
1150 prop->release = release;
1151 prop->opaque = opaque;
1152
b604a854 1153 g_hash_table_insert(obj->properties, prop->name, prop);
64607d08 1154 return prop;
57c9fafe
AL
1155}
1156
16bf7f52
DB
1157ObjectProperty *
1158object_class_property_add(ObjectClass *klass,
1159 const char *name,
1160 const char *type,
1161 ObjectPropertyAccessor *get,
1162 ObjectPropertyAccessor *set,
1163 ObjectPropertyRelease *release,
1164 void *opaque,
1165 Error **errp)
1166{
1167 ObjectProperty *prop;
1168
1169 if (object_class_property_find(klass, name, NULL) != NULL) {
d55e937d
GK
1170 error_setg(errp, "attempt to add duplicate property '%s' to class (type '%s')",
1171 name, object_class_get_name(klass));
16bf7f52
DB
1172 return NULL;
1173 }
1174
1175 prop = g_malloc0(sizeof(*prop));
1176
1177 prop->name = g_strdup(name);
1178 prop->type = g_strdup(type);
1179
1180 prop->get = get;
1181 prop->set = set;
1182 prop->release = release;
1183 prop->opaque = opaque;
1184
1185 g_hash_table_insert(klass->properties, g_strdup(name), prop);
1186
1187 return prop;
1188}
1189
89bfe000
PB
1190ObjectProperty *object_property_find(Object *obj, const char *name,
1191 Error **errp)
57c9fafe
AL
1192{
1193 ObjectProperty *prop;
16bf7f52
DB
1194 ObjectClass *klass = object_get_class(obj);
1195
1196 prop = object_class_property_find(klass, name, NULL);
1197 if (prop) {
1198 return prop;
1199 }
57c9fafe 1200
b604a854
PF
1201 prop = g_hash_table_lookup(obj->properties, name);
1202 if (prop) {
1203 return prop;
57c9fafe
AL
1204 }
1205
f231b88d 1206 error_setg(errp, "Property '.%s' not found", name);
57c9fafe
AL
1207 return NULL;
1208}
1209
7746abd8
DB
1210void object_property_iter_init(ObjectPropertyIterator *iter,
1211 Object *obj)
a00c9482 1212{
7746abd8
DB
1213 g_hash_table_iter_init(&iter->iter, obj->properties);
1214 iter->nextclass = object_get_class(obj);
a00c9482
DB
1215}
1216
1217ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter)
1218{
b604a854 1219 gpointer key, val;
16bf7f52
DB
1220 while (!g_hash_table_iter_next(&iter->iter, &key, &val)) {
1221 if (!iter->nextclass) {
1222 return NULL;
1223 }
1224 g_hash_table_iter_init(&iter->iter, iter->nextclass->properties);
1225 iter->nextclass = object_class_get_parent(iter->nextclass);
a00c9482 1226 }
b604a854 1227 return val;
a00c9482
DB
1228}
1229
961c47bb
AK
1230void object_class_property_iter_init(ObjectPropertyIterator *iter,
1231 ObjectClass *klass)
1232{
1233 g_hash_table_iter_init(&iter->iter, klass->properties);
684546d8 1234 iter->nextclass = object_class_get_parent(klass);
961c47bb
AK
1235}
1236
16bf7f52
DB
1237ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1238 Error **errp)
1239{
1240 ObjectProperty *prop;
1241 ObjectClass *parent_klass;
1242
1243 parent_klass = object_class_get_parent(klass);
1244 if (parent_klass) {
1245 prop = object_class_property_find(parent_klass, name, NULL);
1246 if (prop) {
1247 return prop;
1248 }
1249 }
1250
1251 prop = g_hash_table_lookup(klass->properties, name);
1252 if (!prop) {
1253 error_setg(errp, "Property '.%s' not found", name);
1254 }
1255 return prop;
1256}
1257
57c9fafe
AL
1258void object_property_del(Object *obj, const char *name, Error **errp)
1259{
b604a854
PF
1260 ObjectProperty *prop = g_hash_table_lookup(obj->properties, name);
1261
1262 if (!prop) {
1263 error_setg(errp, "Property '.%s' not found", name);
0866aca1
AL
1264 return;
1265 }
57c9fafe 1266
0866aca1
AL
1267 if (prop->release) {
1268 prop->release(obj, name, prop->opaque);
1269 }
b604a854 1270 g_hash_table_remove(obj->properties, name);
57c9fafe
AL
1271}
1272
1273void object_property_get(Object *obj, Visitor *v, const char *name,
1274 Error **errp)
1275{
89bfe000 1276 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 1277 if (prop == NULL) {
57c9fafe
AL
1278 return;
1279 }
1280
1281 if (!prop->get) {
c6bd8c70 1282 error_setg(errp, QERR_PERMISSION_DENIED);
57c9fafe 1283 } else {
d7bce999 1284 prop->get(obj, v, name, prop->opaque, errp);
57c9fafe
AL
1285 }
1286}
1287
1288void object_property_set(Object *obj, Visitor *v, const char *name,
1289 Error **errp)
1290{
89bfe000 1291 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 1292 if (prop == NULL) {
57c9fafe
AL
1293 return;
1294 }
1295
1296 if (!prop->set) {
c6bd8c70 1297 error_setg(errp, QERR_PERMISSION_DENIED);
57c9fafe 1298 } else {
d7bce999 1299 prop->set(obj, v, name, prop->opaque, errp);
57c9fafe
AL
1300 }
1301}
1302
7b7b7d18
PB
1303void object_property_set_str(Object *obj, const char *value,
1304 const char *name, Error **errp)
1305{
1306 QString *qstr = qstring_from_str(value);
1307 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
1308
cb3e7f08 1309 qobject_unref(qstr);
7b7b7d18
PB
1310}
1311
1312char *object_property_get_str(Object *obj, const char *name,
1313 Error **errp)
1314{
1315 QObject *ret = object_property_get_qobject(obj, name, errp);
7b7b7d18
PB
1316 char *retval;
1317
1318 if (!ret) {
1319 return NULL;
1320 }
aafb21a0
PX
1321
1322 retval = g_strdup(qobject_get_try_str(ret));
1323 if (!retval) {
c6bd8c70 1324 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
7b7b7d18
PB
1325 }
1326
cb3e7f08 1327 qobject_unref(ret);
7b7b7d18
PB
1328 return retval;
1329}
1330
1d9c5a12
PB
1331void object_property_set_link(Object *obj, Object *value,
1332 const char *name, Error **errp)
1333{
d3c49316
PC
1334 if (value) {
1335 gchar *path = object_get_canonical_path(value);
1336 object_property_set_str(obj, path, name, errp);
1337 g_free(path);
1338 } else {
1339 object_property_set_str(obj, "", name, errp);
1340 }
1d9c5a12
PB
1341}
1342
1343Object *object_property_get_link(Object *obj, const char *name,
1344 Error **errp)
1345{
1346 char *str = object_property_get_str(obj, name, errp);
1347 Object *target = NULL;
1348
1349 if (str && *str) {
1350 target = object_resolve_path(str, NULL);
1351 if (!target) {
75158ebb
MA
1352 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1353 "Device '%s' not found", str);
1d9c5a12
PB
1354 }
1355 }
1356
1357 g_free(str);
1358 return target;
1359}
1360
7b7b7d18
PB
1361void object_property_set_bool(Object *obj, bool value,
1362 const char *name, Error **errp)
1363{
fc48ffc3 1364 QBool *qbool = qbool_from_bool(value);
7b7b7d18
PB
1365 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
1366
cb3e7f08 1367 qobject_unref(qbool);
7b7b7d18
PB
1368}
1369
1370bool object_property_get_bool(Object *obj, const char *name,
1371 Error **errp)
1372{
1373 QObject *ret = object_property_get_qobject(obj, name, errp);
1374 QBool *qbool;
1375 bool retval;
1376
1377 if (!ret) {
1378 return false;
1379 }
7dc847eb 1380 qbool = qobject_to(QBool, ret);
7b7b7d18 1381 if (!qbool) {
c6bd8c70 1382 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
7b7b7d18
PB
1383 retval = false;
1384 } else {
fc48ffc3 1385 retval = qbool_get_bool(qbool);
7b7b7d18
PB
1386 }
1387
cb3e7f08 1388 qobject_unref(ret);
7b7b7d18
PB
1389 return retval;
1390}
1391
1392void object_property_set_int(Object *obj, int64_t value,
1393 const char *name, Error **errp)
1394{
01b2ffce
MAL
1395 QNum *qnum = qnum_from_int(value);
1396 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
7b7b7d18 1397
cb3e7f08 1398 qobject_unref(qnum);
7b7b7d18
PB
1399}
1400
1401int64_t object_property_get_int(Object *obj, const char *name,
1402 Error **errp)
1403{
1404 QObject *ret = object_property_get_qobject(obj, name, errp);
01b2ffce 1405 QNum *qnum;
7b7b7d18
PB
1406 int64_t retval;
1407
1408 if (!ret) {
1409 return -1;
1410 }
01b2ffce 1411
7dc847eb 1412 qnum = qobject_to(QNum, ret);
01b2ffce 1413 if (!qnum || !qnum_get_try_int(qnum, &retval)) {
c6bd8c70 1414 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
7b7b7d18 1415 retval = -1;
7b7b7d18
PB
1416 }
1417
cb3e7f08 1418 qobject_unref(ret);
7b7b7d18 1419 return retval;
b2cd7dee
PB
1420}
1421
3152779c
MAL
1422void object_property_set_uint(Object *obj, uint64_t value,
1423 const char *name, Error **errp)
1424{
1425 QNum *qnum = qnum_from_uint(value);
1426
1427 object_property_set_qobject(obj, QOBJECT(qnum), name, errp);
cb3e7f08 1428 qobject_unref(qnum);
3152779c
MAL
1429}
1430
1431uint64_t object_property_get_uint(Object *obj, const char *name,
1432 Error **errp)
1433{
1434 QObject *ret = object_property_get_qobject(obj, name, errp);
1435 QNum *qnum;
1436 uint64_t retval;
1437
1438 if (!ret) {
1439 return 0;
1440 }
7dc847eb 1441 qnum = qobject_to(QNum, ret);
3152779c
MAL
1442 if (!qnum || !qnum_get_try_uint(qnum, &retval)) {
1443 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "uint");
1444 retval = 0;
1445 }
1446
cb3e7f08 1447 qobject_unref(ret);
3152779c
MAL
1448 return retval;
1449}
1450
a8e3fbed 1451typedef struct EnumProperty {
f7abe0ec 1452 const QEnumLookup *lookup;
a8e3fbed
DB
1453 int (*get)(Object *, Error **);
1454 void (*set)(Object *, int, Error **);
1455} EnumProperty;
1456
1f21772d 1457int object_property_get_enum(Object *obj, const char *name,
a3590dac 1458 const char *typename, Error **errp)
1f21772d 1459{
4715d42e 1460 Error *err = NULL;
7a0525c7 1461 Visitor *v;
976620ac 1462 char *str;
1f21772d 1463 int ret;
a3590dac
DB
1464 ObjectProperty *prop = object_property_find(obj, name, errp);
1465 EnumProperty *enumprop;
1466
1467 if (prop == NULL) {
1468 return 0;
1469 }
1470
1471 if (!g_str_equal(prop->type, typename)) {
1472 error_setg(errp, "Property %s on %s is not '%s' enum type",
1473 name, object_class_get_name(
1474 object_get_class(obj)), typename);
1475 return 0;
1476 }
1477
1478 enumprop = prop->opaque;
1f21772d 1479
3b098d56 1480 v = string_output_visitor_new(false, &str);
e7ca5656 1481 object_property_get(obj, v, name, &err);
4715d42e
MA
1482 if (err) {
1483 error_propagate(errp, err);
e7ca5656 1484 visit_free(v);
4715d42e
MA
1485 return 0;
1486 }
3b098d56 1487 visit_complete(v, &str);
e7ca5656 1488 visit_free(v);
7a0525c7 1489 v = string_input_visitor_new(str);
f7abe0ec 1490 visit_type_enum(v, name, &ret, enumprop->lookup, errp);
976620ac
CF
1491
1492 g_free(str);
7a0525c7 1493 visit_free(v);
1f21772d
HT
1494
1495 return ret;
1496}
1497
1498void object_property_get_uint16List(Object *obj, const char *name,
1499 uint16List **list, Error **errp)
1500{
4715d42e 1501 Error *err = NULL;
7a0525c7 1502 Visitor *v;
976620ac 1503 char *str;
1f21772d 1504
3b098d56
EB
1505 v = string_output_visitor_new(false, &str);
1506 object_property_get(obj, v, name, &err);
4715d42e
MA
1507 if (err) {
1508 error_propagate(errp, err);
1509 goto out;
1510 }
3b098d56
EB
1511 visit_complete(v, &str);
1512 visit_free(v);
7a0525c7
EB
1513 v = string_input_visitor_new(str);
1514 visit_type_uint16List(v, NULL, list, errp);
976620ac
CF
1515
1516 g_free(str);
4715d42e 1517out:
3b098d56 1518 visit_free(v);
1f21772d
HT
1519}
1520
b2cd7dee
PB
1521void object_property_parse(Object *obj, const char *string,
1522 const char *name, Error **errp)
1523{
7a0525c7
EB
1524 Visitor *v = string_input_visitor_new(string);
1525 object_property_set(obj, v, name, errp);
1526 visit_free(v);
b2cd7dee
PB
1527}
1528
0b7593e0 1529char *object_property_print(Object *obj, const char *name, bool human,
b2cd7dee
PB
1530 Error **errp)
1531{
3b098d56 1532 Visitor *v;
3a53009f
GA
1533 char *string = NULL;
1534 Error *local_err = NULL;
b2cd7dee 1535
3b098d56
EB
1536 v = string_output_visitor_new(human, &string);
1537 object_property_get(obj, v, name, &local_err);
3a53009f
GA
1538 if (local_err) {
1539 error_propagate(errp, local_err);
1540 goto out;
1541 }
1542
3b098d56 1543 visit_complete(v, &string);
3a53009f
GA
1544
1545out:
3b098d56 1546 visit_free(v);
b2cd7dee 1547 return string;
7b7b7d18
PB
1548}
1549
57c9fafe
AL
1550const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1551{
89bfe000 1552 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 1553 if (prop == NULL) {
57c9fafe
AL
1554 return NULL;
1555 }
1556
1557 return prop->type;
1558}
1559
1560Object *object_get_root(void)
1561{
8b45d447 1562 static Object *root;
57c9fafe 1563
8b45d447
AL
1564 if (!root) {
1565 root = object_new("container");
57c9fafe
AL
1566 }
1567
8b45d447 1568 return root;
57c9fafe
AL
1569}
1570
bc2256c4
DB
1571Object *object_get_objects_root(void)
1572{
1573 return container_get(object_get_root(), "/objects");
1574}
1575
7c47c4ea
PX
1576Object *object_get_internal_root(void)
1577{
1578 static Object *internal_root;
1579
1580 if (!internal_root) {
1581 internal_root = object_new("container");
1582 }
1583
1584 return internal_root;
1585}
1586
d7bce999
EB
1587static void object_get_child_property(Object *obj, Visitor *v,
1588 const char *name, void *opaque,
1589 Error **errp)
57c9fafe
AL
1590{
1591 Object *child = opaque;
1592 gchar *path;
1593
1594 path = object_get_canonical_path(child);
51e72bc1 1595 visit_type_str(v, name, &path, errp);
57c9fafe
AL
1596 g_free(path);
1597}
1598
64607d08
PB
1599static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1600{
1601 return opaque;
1602}
1603
db85b575
AL
1604static void object_finalize_child_property(Object *obj, const char *name,
1605 void *opaque)
1606{
1607 Object *child = opaque;
1608
bffc687d
PB
1609 if (child->class->unparent) {
1610 (child->class->unparent)(child);
1611 }
1612 child->parent = NULL;
db85b575
AL
1613 object_unref(child);
1614}
1615
57c9fafe
AL
1616void object_property_add_child(Object *obj, const char *name,
1617 Object *child, Error **errp)
1618{
b0ed5e9f 1619 Error *local_err = NULL;
57c9fafe 1620 gchar *type;
64607d08 1621 ObjectProperty *op;
57c9fafe 1622
8faa2f85
PC
1623 if (child->parent != NULL) {
1624 error_setg(errp, "child object is already parented");
1625 return;
1626 }
1627
57c9fafe
AL
1628 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1629
64607d08
PB
1630 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1631 object_finalize_child_property, child, &local_err);
b0ed5e9f
PB
1632 if (local_err) {
1633 error_propagate(errp, local_err);
1634 goto out;
1635 }
64607d08
PB
1636
1637 op->resolve = object_resolve_child_property;
57c9fafe 1638 object_ref(child);
57c9fafe
AL
1639 child->parent = obj;
1640
b0ed5e9f 1641out:
57c9fafe
AL
1642 g_free(type);
1643}
1644
8f5d58ef 1645void object_property_allow_set_link(const Object *obj, const char *name,
39f72ef9
SH
1646 Object *val, Error **errp)
1647{
1648 /* Allow the link to be set, always */
1649}
1650
9561fda8
SH
1651typedef struct {
1652 Object **child;
8f5d58ef 1653 void (*check)(const Object *, const char *, Object *, Error **);
9561fda8
SH
1654 ObjectPropertyLinkFlags flags;
1655} LinkProperty;
1656
d7bce999
EB
1657static void object_get_link_property(Object *obj, Visitor *v,
1658 const char *name, void *opaque,
1659 Error **errp)
57c9fafe 1660{
9561fda8
SH
1661 LinkProperty *lprop = opaque;
1662 Object **child = lprop->child;
57c9fafe
AL
1663 gchar *path;
1664
1665 if (*child) {
1666 path = object_get_canonical_path(*child);
51e72bc1 1667 visit_type_str(v, name, &path, errp);
57c9fafe
AL
1668 g_free(path);
1669 } else {
1670 path = (gchar *)"";
51e72bc1 1671 visit_type_str(v, name, &path, errp);
57c9fafe
AL
1672 }
1673}
1674
f5ec6704
SH
1675/*
1676 * object_resolve_link:
1677 *
1678 * Lookup an object and ensure its type matches the link property type. This
1679 * is similar to object_resolve_path() except type verification against the
1680 * link property is performed.
1681 *
1682 * Returns: The matched object or NULL on path lookup failures.
1683 */
1684static Object *object_resolve_link(Object *obj, const char *name,
1685 const char *path, Error **errp)
1686{
1687 const char *type;
1688 gchar *target_type;
1689 bool ambiguous = false;
1690 Object *target;
1691
1692 /* Go from link<FOO> to FOO. */
1693 type = object_property_get_type(obj, name, NULL);
1694 target_type = g_strndup(&type[5], strlen(type) - 6);
1695 target = object_resolve_path_type(path, target_type, &ambiguous);
1696
1697 if (ambiguous) {
455b0fde
EB
1698 error_setg(errp, "Path '%s' does not uniquely identify an object",
1699 path);
f5ec6704
SH
1700 } else if (!target) {
1701 target = object_resolve_path(path, &ambiguous);
1702 if (target || ambiguous) {
c6bd8c70 1703 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
f5ec6704 1704 } else {
75158ebb
MA
1705 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
1706 "Device '%s' not found", path);
f5ec6704
SH
1707 }
1708 target = NULL;
1709 }
1710 g_free(target_type);
1711
1712 return target;
1713}
1714
d7bce999
EB
1715static void object_set_link_property(Object *obj, Visitor *v,
1716 const char *name, void *opaque,
1717 Error **errp)
57c9fafe 1718{
c6aed983 1719 Error *local_err = NULL;
9561fda8
SH
1720 LinkProperty *prop = opaque;
1721 Object **child = prop->child;
c6aed983
SH
1722 Object *old_target = *child;
1723 Object *new_target = NULL;
1724 char *path = NULL;
57c9fafe 1725
51e72bc1 1726 visit_type_str(v, name, &path, &local_err);
57c9fafe 1727
c6aed983
SH
1728 if (!local_err && strcmp(path, "") != 0) {
1729 new_target = object_resolve_link(obj, name, path, &local_err);
57c9fafe
AL
1730 }
1731
1732 g_free(path);
c6aed983
SH
1733 if (local_err) {
1734 error_propagate(errp, local_err);
1735 return;
1736 }
f0cdc966 1737
39f72ef9
SH
1738 prop->check(obj, name, new_target, &local_err);
1739 if (local_err) {
1740 error_propagate(errp, local_err);
1741 return;
1742 }
1743
c6aed983 1744 *child = new_target;
265b578c
MAL
1745 if (prop->flags == OBJ_PROP_LINK_STRONG) {
1746 object_ref(new_target);
1747 object_unref(old_target);
1748 }
57c9fafe
AL
1749}
1750
64607d08
PB
1751static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1752{
1753 LinkProperty *lprop = opaque;
1754
1755 return *lprop->child;
1756}
1757
9561fda8
SH
1758static void object_release_link_property(Object *obj, const char *name,
1759 void *opaque)
1760{
1761 LinkProperty *prop = opaque;
1762
265b578c 1763 if ((prop->flags & OBJ_PROP_LINK_STRONG) && *prop->child) {
9561fda8
SH
1764 object_unref(*prop->child);
1765 }
1766 g_free(prop);
1767}
1768
57c9fafe
AL
1769void object_property_add_link(Object *obj, const char *name,
1770 const char *type, Object **child,
8f5d58ef 1771 void (*check)(const Object *, const char *,
39f72ef9 1772 Object *, Error **),
9561fda8 1773 ObjectPropertyLinkFlags flags,
57c9fafe
AL
1774 Error **errp)
1775{
9561fda8
SH
1776 Error *local_err = NULL;
1777 LinkProperty *prop = g_malloc(sizeof(*prop));
57c9fafe 1778 gchar *full_type;
64607d08 1779 ObjectProperty *op;
57c9fafe 1780
9561fda8 1781 prop->child = child;
39f72ef9 1782 prop->check = check;
9561fda8
SH
1783 prop->flags = flags;
1784
57c9fafe
AL
1785 full_type = g_strdup_printf("link<%s>", type);
1786
64607d08
PB
1787 op = object_property_add(obj, name, full_type,
1788 object_get_link_property,
1789 check ? object_set_link_property : NULL,
1790 object_release_link_property,
1791 prop,
1792 &local_err);
9561fda8
SH
1793 if (local_err) {
1794 error_propagate(errp, local_err);
1795 g_free(prop);
64607d08 1796 goto out;
9561fda8 1797 }
57c9fafe 1798
64607d08
PB
1799 op->resolve = object_resolve_link_property;
1800
1801out:
57c9fafe
AL
1802 g_free(full_type);
1803}
1804
fb9e7e33
PB
1805void object_property_add_const_link(Object *obj, const char *name,
1806 Object *target, Error **errp)
1807{
1808 char *link_type;
1809 ObjectProperty *op;
1810
1811 link_type = g_strdup_printf("link<%s>", object_get_typename(target));
1812 op = object_property_add(obj, name, link_type,
1813 object_get_child_property, NULL,
1814 NULL, target, errp);
1815 if (op != NULL) {
1816 op->resolve = object_resolve_child_property;
1817 }
1818 g_free(link_type);
1819}
1820
11f590b1
SH
1821gchar *object_get_canonical_path_component(Object *obj)
1822{
1823 ObjectProperty *prop = NULL;
b604a854 1824 GHashTableIter iter;
11f590b1 1825
770dec26
PB
1826 if (obj->parent == NULL) {
1827 return NULL;
1828 }
11f590b1 1829
b604a854
PF
1830 g_hash_table_iter_init(&iter, obj->parent->properties);
1831 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
11f590b1
SH
1832 if (!object_property_is_child(prop)) {
1833 continue;
1834 }
1835
1836 if (prop->opaque == obj) {
1837 return g_strdup(prop->name);
1838 }
1839 }
1840
1841 /* obj had a parent but was not a child, should never happen */
1842 g_assert_not_reached();
1843 return NULL;
1844}
1845
57c9fafe
AL
1846gchar *object_get_canonical_path(Object *obj)
1847{
1848 Object *root = object_get_root();
11f590b1 1849 char *newpath, *path = NULL;
57c9fafe 1850
e40077fd
PB
1851 if (obj == root) {
1852 return g_strdup("/");
1853 }
1854
1855 do {
11f590b1 1856 char *component = object_get_canonical_path_component(obj);
57c9fafe 1857
e40077fd
PB
1858 if (!component) {
1859 /* A canonical path must be complete, so discard what was
1860 * collected so far.
1861 */
11f590b1 1862 g_free(path);
e40077fd 1863 return NULL;
57c9fafe
AL
1864 }
1865
e40077fd
PB
1866 newpath = g_strdup_printf("/%s%s", component, path ? path : "");
1867 g_free(path);
1868 g_free(component);
1869 path = newpath;
57c9fafe 1870 obj = obj->parent;
e40077fd 1871 } while (obj != root);
57c9fafe 1872
e40077fd 1873 return path;
57c9fafe
AL
1874}
1875
3e84b483 1876Object *object_resolve_path_component(Object *parent, const gchar *part)
a612b2a6 1877{
89bfe000 1878 ObjectProperty *prop = object_property_find(parent, part, NULL);
a612b2a6
PB
1879 if (prop == NULL) {
1880 return NULL;
1881 }
1882
64607d08
PB
1883 if (prop->resolve) {
1884 return prop->resolve(parent, prop->opaque, part);
a612b2a6
PB
1885 } else {
1886 return NULL;
1887 }
1888}
1889
57c9fafe
AL
1890static Object *object_resolve_abs_path(Object *parent,
1891 gchar **parts,
02fe2db6 1892 const char *typename,
57c9fafe
AL
1893 int index)
1894{
57c9fafe
AL
1895 Object *child;
1896
1897 if (parts[index] == NULL) {
02fe2db6 1898 return object_dynamic_cast(parent, typename);
57c9fafe
AL
1899 }
1900
1901 if (strcmp(parts[index], "") == 0) {
02fe2db6 1902 return object_resolve_abs_path(parent, parts, typename, index + 1);
57c9fafe
AL
1903 }
1904
a612b2a6 1905 child = object_resolve_path_component(parent, parts[index]);
57c9fafe
AL
1906 if (!child) {
1907 return NULL;
1908 }
1909
02fe2db6 1910 return object_resolve_abs_path(child, parts, typename, index + 1);
57c9fafe
AL
1911}
1912
1913static Object *object_resolve_partial_path(Object *parent,
1914 gchar **parts,
02fe2db6 1915 const char *typename,
57c9fafe
AL
1916 bool *ambiguous)
1917{
1918 Object *obj;
b604a854 1919 GHashTableIter iter;
57c9fafe
AL
1920 ObjectProperty *prop;
1921
02fe2db6 1922 obj = object_resolve_abs_path(parent, parts, typename, 0);
57c9fafe 1923
b604a854
PF
1924 g_hash_table_iter_init(&iter, parent->properties);
1925 while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) {
57c9fafe
AL
1926 Object *found;
1927
5d9d3f47 1928 if (!object_property_is_child(prop)) {
57c9fafe
AL
1929 continue;
1930 }
1931
02fe2db6
PB
1932 found = object_resolve_partial_path(prop->opaque, parts,
1933 typename, ambiguous);
57c9fafe
AL
1934 if (found) {
1935 if (obj) {
ebcc479e 1936 *ambiguous = true;
57c9fafe
AL
1937 return NULL;
1938 }
1939 obj = found;
1940 }
1941
ebcc479e 1942 if (*ambiguous) {
57c9fafe
AL
1943 return NULL;
1944 }
1945 }
1946
1947 return obj;
1948}
1949
02fe2db6 1950Object *object_resolve_path_type(const char *path, const char *typename,
ebcc479e 1951 bool *ambiguousp)
57c9fafe 1952{
57c9fafe
AL
1953 Object *obj;
1954 gchar **parts;
1955
1956 parts = g_strsplit(path, "/", 0);
2e1103f6 1957 assert(parts);
57c9fafe 1958
2e1103f6 1959 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
ebcc479e 1960 bool ambiguous = false;
02fe2db6 1961 obj = object_resolve_partial_path(object_get_root(), parts,
ebcc479e
EH
1962 typename, &ambiguous);
1963 if (ambiguousp) {
1964 *ambiguousp = ambiguous;
1965 }
57c9fafe 1966 } else {
02fe2db6 1967 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
57c9fafe
AL
1968 }
1969
1970 g_strfreev(parts);
1971
1972 return obj;
1973}
1974
02fe2db6
PB
1975Object *object_resolve_path(const char *path, bool *ambiguous)
1976{
1977 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1978}
1979
57c9fafe
AL
1980typedef struct StringProperty
1981{
1982 char *(*get)(Object *, Error **);
1983 void (*set)(Object *, const char *, Error **);
1984} StringProperty;
1985
d7bce999
EB
1986static void property_get_str(Object *obj, Visitor *v, const char *name,
1987 void *opaque, Error **errp)
57c9fafe
AL
1988{
1989 StringProperty *prop = opaque;
1990 char *value;
e1c8237d 1991 Error *err = NULL;
57c9fafe 1992
e1c8237d
MA
1993 value = prop->get(obj, &err);
1994 if (err) {
1995 error_propagate(errp, err);
1996 return;
57c9fafe 1997 }
e1c8237d 1998
51e72bc1 1999 visit_type_str(v, name, &value, errp);
e1c8237d 2000 g_free(value);
57c9fafe
AL
2001}
2002
d7bce999
EB
2003static void property_set_str(Object *obj, Visitor *v, const char *name,
2004 void *opaque, Error **errp)
57c9fafe
AL
2005{
2006 StringProperty *prop = opaque;
2007 char *value;
2008 Error *local_err = NULL;
2009
51e72bc1 2010 visit_type_str(v, name, &value, &local_err);
57c9fafe
AL
2011 if (local_err) {
2012 error_propagate(errp, local_err);
2013 return;
2014 }
2015
2016 prop->set(obj, value, errp);
2017 g_free(value);
2018}
2019
7b7b7d18
PB
2020static void property_release_str(Object *obj, const char *name,
2021 void *opaque)
57c9fafe
AL
2022{
2023 StringProperty *prop = opaque;
2024 g_free(prop);
2025}
2026
2027void object_property_add_str(Object *obj, const char *name,
2028 char *(*get)(Object *, Error **),
2029 void (*set)(Object *, const char *, Error **),
2030 Error **errp)
2031{
a01aedc8 2032 Error *local_err = NULL;
57c9fafe
AL
2033 StringProperty *prop = g_malloc0(sizeof(*prop));
2034
2035 prop->get = get;
2036 prop->set = set;
2037
2038 object_property_add(obj, name, "string",
7b7b7d18
PB
2039 get ? property_get_str : NULL,
2040 set ? property_set_str : NULL,
2041 property_release_str,
a01aedc8
SH
2042 prop, &local_err);
2043 if (local_err) {
2044 error_propagate(errp, local_err);
2045 g_free(prop);
2046 }
57c9fafe 2047}
745549c8 2048
16bf7f52
DB
2049void object_class_property_add_str(ObjectClass *klass, const char *name,
2050 char *(*get)(Object *, Error **),
2051 void (*set)(Object *, const char *,
2052 Error **),
2053 Error **errp)
2054{
2055 Error *local_err = NULL;
2056 StringProperty *prop = g_malloc0(sizeof(*prop));
2057
2058 prop->get = get;
2059 prop->set = set;
2060
2061 object_class_property_add(klass, name, "string",
2062 get ? property_get_str : NULL,
2063 set ? property_set_str : NULL,
2064 property_release_str,
2065 prop, &local_err);
2066 if (local_err) {
2067 error_propagate(errp, local_err);
2068 g_free(prop);
2069 }
2070}
2071
0e558843
AL
2072typedef struct BoolProperty
2073{
2074 bool (*get)(Object *, Error **);
2075 void (*set)(Object *, bool, Error **);
2076} BoolProperty;
2077
d7bce999
EB
2078static void property_get_bool(Object *obj, Visitor *v, const char *name,
2079 void *opaque, Error **errp)
0e558843
AL
2080{
2081 BoolProperty *prop = opaque;
2082 bool value;
4715d42e
MA
2083 Error *err = NULL;
2084
2085 value = prop->get(obj, &err);
2086 if (err) {
2087 error_propagate(errp, err);
2088 return;
2089 }
0e558843 2090
51e72bc1 2091 visit_type_bool(v, name, &value, errp);
0e558843
AL
2092}
2093
d7bce999
EB
2094static void property_set_bool(Object *obj, Visitor *v, const char *name,
2095 void *opaque, Error **errp)
0e558843
AL
2096{
2097 BoolProperty *prop = opaque;
2098 bool value;
2099 Error *local_err = NULL;
2100
51e72bc1 2101 visit_type_bool(v, name, &value, &local_err);
0e558843
AL
2102 if (local_err) {
2103 error_propagate(errp, local_err);
2104 return;
2105 }
2106
2107 prop->set(obj, value, errp);
2108}
2109
2110static void property_release_bool(Object *obj, const char *name,
2111 void *opaque)
2112{
2113 BoolProperty *prop = opaque;
2114 g_free(prop);
2115}
2116
2117void object_property_add_bool(Object *obj, const char *name,
2118 bool (*get)(Object *, Error **),
2119 void (*set)(Object *, bool, Error **),
2120 Error **errp)
2121{
a01aedc8 2122 Error *local_err = NULL;
0e558843
AL
2123 BoolProperty *prop = g_malloc0(sizeof(*prop));
2124
2125 prop->get = get;
2126 prop->set = set;
2127
2128 object_property_add(obj, name, "bool",
2129 get ? property_get_bool : NULL,
2130 set ? property_set_bool : NULL,
2131 property_release_bool,
a01aedc8
SH
2132 prop, &local_err);
2133 if (local_err) {
2134 error_propagate(errp, local_err);
2135 g_free(prop);
2136 }
0e558843
AL
2137}
2138
16bf7f52
DB
2139void object_class_property_add_bool(ObjectClass *klass, const char *name,
2140 bool (*get)(Object *, Error **),
2141 void (*set)(Object *, bool, Error **),
2142 Error **errp)
2143{
2144 Error *local_err = NULL;
2145 BoolProperty *prop = g_malloc0(sizeof(*prop));
2146
2147 prop->get = get;
2148 prop->set = set;
2149
2150 object_class_property_add(klass, name, "bool",
2151 get ? property_get_bool : NULL,
2152 set ? property_set_bool : NULL,
2153 property_release_bool,
2154 prop, &local_err);
2155 if (local_err) {
2156 error_propagate(errp, local_err);
2157 g_free(prop);
2158 }
2159}
2160
d7bce999
EB
2161static void property_get_enum(Object *obj, Visitor *v, const char *name,
2162 void *opaque, Error **errp)
a8e3fbed
DB
2163{
2164 EnumProperty *prop = opaque;
2165 int value;
4715d42e
MA
2166 Error *err = NULL;
2167
2168 value = prop->get(obj, &err);
2169 if (err) {
2170 error_propagate(errp, err);
2171 return;
2172 }
a8e3fbed 2173
f7abe0ec 2174 visit_type_enum(v, name, &value, prop->lookup, errp);
a8e3fbed
DB
2175}
2176
d7bce999
EB
2177static void property_set_enum(Object *obj, Visitor *v, const char *name,
2178 void *opaque, Error **errp)
a8e3fbed
DB
2179{
2180 EnumProperty *prop = opaque;
2181 int value;
4715d42e 2182 Error *err = NULL;
a8e3fbed 2183
f7abe0ec 2184 visit_type_enum(v, name, &value, prop->lookup, &err);
4715d42e
MA
2185 if (err) {
2186 error_propagate(errp, err);
2187 return;
2188 }
a8e3fbed
DB
2189 prop->set(obj, value, errp);
2190}
2191
2192static void property_release_enum(Object *obj, const char *name,
2193 void *opaque)
2194{
2195 EnumProperty *prop = opaque;
2196 g_free(prop);
2197}
2198
2199void object_property_add_enum(Object *obj, const char *name,
2200 const char *typename,
f7abe0ec 2201 const QEnumLookup *lookup,
a8e3fbed
DB
2202 int (*get)(Object *, Error **),
2203 void (*set)(Object *, int, Error **),
2204 Error **errp)
2205{
2206 Error *local_err = NULL;
2207 EnumProperty *prop = g_malloc(sizeof(*prop));
2208
f7abe0ec 2209 prop->lookup = lookup;
a8e3fbed
DB
2210 prop->get = get;
2211 prop->set = set;
2212
2213 object_property_add(obj, name, typename,
2214 get ? property_get_enum : NULL,
2215 set ? property_set_enum : NULL,
2216 property_release_enum,
2217 prop, &local_err);
2218 if (local_err) {
2219 error_propagate(errp, local_err);
2220 g_free(prop);
2221 }
2222}
2223
16bf7f52
DB
2224void object_class_property_add_enum(ObjectClass *klass, const char *name,
2225 const char *typename,
f7abe0ec 2226 const QEnumLookup *lookup,
16bf7f52
DB
2227 int (*get)(Object *, Error **),
2228 void (*set)(Object *, int, Error **),
2229 Error **errp)
2230{
2231 Error *local_err = NULL;
2232 EnumProperty *prop = g_malloc(sizeof(*prop));
2233
f7abe0ec 2234 prop->lookup = lookup;
16bf7f52
DB
2235 prop->get = get;
2236 prop->set = set;
2237
2238 object_class_property_add(klass, name, typename,
2239 get ? property_get_enum : NULL,
2240 set ? property_set_enum : NULL,
2241 property_release_enum,
2242 prop, &local_err);
2243 if (local_err) {
2244 error_propagate(errp, local_err);
2245 g_free(prop);
2246 }
2247}
2248
8e099d14
DG
2249typedef struct TMProperty {
2250 void (*get)(Object *, struct tm *, Error **);
2251} TMProperty;
2252
d7bce999
EB
2253static void property_get_tm(Object *obj, Visitor *v, const char *name,
2254 void *opaque, Error **errp)
8e099d14
DG
2255{
2256 TMProperty *prop = opaque;
2257 Error *err = NULL;
2258 struct tm value;
2259
2260 prop->get(obj, &value, &err);
2261 if (err) {
2262 goto out;
2263 }
2264
337283df 2265 visit_start_struct(v, name, NULL, 0, &err);
8e099d14
DG
2266 if (err) {
2267 goto out;
2268 }
51e72bc1 2269 visit_type_int32(v, "tm_year", &value.tm_year, &err);
8e099d14
DG
2270 if (err) {
2271 goto out_end;
2272 }
51e72bc1 2273 visit_type_int32(v, "tm_mon", &value.tm_mon, &err);
8e099d14
DG
2274 if (err) {
2275 goto out_end;
2276 }
51e72bc1 2277 visit_type_int32(v, "tm_mday", &value.tm_mday, &err);
8e099d14
DG
2278 if (err) {
2279 goto out_end;
2280 }
51e72bc1 2281 visit_type_int32(v, "tm_hour", &value.tm_hour, &err);
8e099d14
DG
2282 if (err) {
2283 goto out_end;
2284 }
51e72bc1 2285 visit_type_int32(v, "tm_min", &value.tm_min, &err);
8e099d14
DG
2286 if (err) {
2287 goto out_end;
2288 }
51e72bc1 2289 visit_type_int32(v, "tm_sec", &value.tm_sec, &err);
8e099d14
DG
2290 if (err) {
2291 goto out_end;
2292 }
15c2f669 2293 visit_check_struct(v, &err);
8e099d14 2294out_end:
1158bb2a 2295 visit_end_struct(v, NULL);
8e099d14
DG
2296out:
2297 error_propagate(errp, err);
2298
2299}
2300
2301static void property_release_tm(Object *obj, const char *name,
2302 void *opaque)
2303{
2304 TMProperty *prop = opaque;
2305 g_free(prop);
2306}
2307
2308void object_property_add_tm(Object *obj, const char *name,
2309 void (*get)(Object *, struct tm *, Error **),
2310 Error **errp)
2311{
2312 Error *local_err = NULL;
2313 TMProperty *prop = g_malloc0(sizeof(*prop));
2314
2315 prop->get = get;
2316
2317 object_property_add(obj, name, "struct tm",
2318 get ? property_get_tm : NULL, NULL,
2319 property_release_tm,
2320 prop, &local_err);
2321 if (local_err) {
2322 error_propagate(errp, local_err);
2323 g_free(prop);
2324 }
2325}
2326
16bf7f52
DB
2327void object_class_property_add_tm(ObjectClass *klass, const char *name,
2328 void (*get)(Object *, struct tm *, Error **),
2329 Error **errp)
2330{
2331 Error *local_err = NULL;
2332 TMProperty *prop = g_malloc0(sizeof(*prop));
2333
2334 prop->get = get;
2335
2336 object_class_property_add(klass, name, "struct tm",
2337 get ? property_get_tm : NULL, NULL,
2338 property_release_tm,
2339 prop, &local_err);
2340 if (local_err) {
2341 error_propagate(errp, local_err);
2342 g_free(prop);
2343 }
2344}
2345
2f262e06
PB
2346static char *qdev_get_type(Object *obj, Error **errp)
2347{
2348 return g_strdup(object_get_typename(obj));
2349}
2350
d7bce999
EB
2351static void property_get_uint8_ptr(Object *obj, Visitor *v, const char *name,
2352 void *opaque, Error **errp)
e732ea63
MT
2353{
2354 uint8_t value = *(uint8_t *)opaque;
51e72bc1 2355 visit_type_uint8(v, name, &value, errp);
e732ea63
MT
2356}
2357
d7bce999
EB
2358static void property_get_uint16_ptr(Object *obj, Visitor *v, const char *name,
2359 void *opaque, Error **errp)
e732ea63
MT
2360{
2361 uint16_t value = *(uint16_t *)opaque;
51e72bc1 2362 visit_type_uint16(v, name, &value, errp);
e732ea63
MT
2363}
2364
d7bce999
EB
2365static void property_get_uint32_ptr(Object *obj, Visitor *v, const char *name,
2366 void *opaque, Error **errp)
e732ea63
MT
2367{
2368 uint32_t value = *(uint32_t *)opaque;
51e72bc1 2369 visit_type_uint32(v, name, &value, errp);
e732ea63
MT
2370}
2371
d7bce999
EB
2372static void property_get_uint64_ptr(Object *obj, Visitor *v, const char *name,
2373 void *opaque, Error **errp)
e732ea63
MT
2374{
2375 uint64_t value = *(uint64_t *)opaque;
51e72bc1 2376 visit_type_uint64(v, name, &value, errp);
e732ea63
MT
2377}
2378
2379void object_property_add_uint8_ptr(Object *obj, const char *name,
2380 const uint8_t *v, Error **errp)
2381{
2382 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
2383 NULL, NULL, (void *)v, errp);
2384}
2385
16bf7f52
DB
2386void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
2387 const uint8_t *v, Error **errp)
2388{
2389 object_class_property_add(klass, name, "uint8", property_get_uint8_ptr,
2390 NULL, NULL, (void *)v, errp);
2391}
2392
e732ea63
MT
2393void object_property_add_uint16_ptr(Object *obj, const char *name,
2394 const uint16_t *v, Error **errp)
2395{
2396 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
2397 NULL, NULL, (void *)v, errp);
2398}
2399
16bf7f52
DB
2400void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
2401 const uint16_t *v, Error **errp)
2402{
2403 object_class_property_add(klass, name, "uint16", property_get_uint16_ptr,
2404 NULL, NULL, (void *)v, errp);
2405}
2406
e732ea63
MT
2407void object_property_add_uint32_ptr(Object *obj, const char *name,
2408 const uint32_t *v, Error **errp)
2409{
2410 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
2411 NULL, NULL, (void *)v, errp);
2412}
2413
16bf7f52
DB
2414void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
2415 const uint32_t *v, Error **errp)
2416{
2417 object_class_property_add(klass, name, "uint32", property_get_uint32_ptr,
2418 NULL, NULL, (void *)v, errp);
2419}
2420
e732ea63
MT
2421void object_property_add_uint64_ptr(Object *obj, const char *name,
2422 const uint64_t *v, Error **errp)
2423{
2424 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
2425 NULL, NULL, (void *)v, errp);
2426}
2427
16bf7f52
DB
2428void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
2429 const uint64_t *v, Error **errp)
2430{
2431 object_class_property_add(klass, name, "uint64", property_get_uint64_ptr,
2432 NULL, NULL, (void *)v, errp);
2433}
2434
ef7c7ff6
SH
2435typedef struct {
2436 Object *target_obj;
1590d266 2437 char *target_name;
ef7c7ff6
SH
2438} AliasProperty;
2439
d7bce999
EB
2440static void property_get_alias(Object *obj, Visitor *v, const char *name,
2441 void *opaque, Error **errp)
ef7c7ff6
SH
2442{
2443 AliasProperty *prop = opaque;
2444
2445 object_property_get(prop->target_obj, v, prop->target_name, errp);
2446}
2447
d7bce999
EB
2448static void property_set_alias(Object *obj, Visitor *v, const char *name,
2449 void *opaque, Error **errp)
ef7c7ff6
SH
2450{
2451 AliasProperty *prop = opaque;
2452
2453 object_property_set(prop->target_obj, v, prop->target_name, errp);
2454}
2455
64607d08
PB
2456static Object *property_resolve_alias(Object *obj, void *opaque,
2457 const gchar *part)
2458{
2459 AliasProperty *prop = opaque;
2460
2461 return object_resolve_path_component(prop->target_obj, prop->target_name);
2462}
2463
ef7c7ff6
SH
2464static void property_release_alias(Object *obj, const char *name, void *opaque)
2465{
2466 AliasProperty *prop = opaque;
2467
1590d266 2468 g_free(prop->target_name);
ef7c7ff6
SH
2469 g_free(prop);
2470}
2471
2472void object_property_add_alias(Object *obj, const char *name,
2473 Object *target_obj, const char *target_name,
2474 Error **errp)
2475{
2476 AliasProperty *prop;
64607d08 2477 ObjectProperty *op;
ef7c7ff6 2478 ObjectProperty *target_prop;
d190698e 2479 gchar *prop_type;
8ae9a9ef 2480 Error *local_err = NULL;
ef7c7ff6
SH
2481
2482 target_prop = object_property_find(target_obj, target_name, errp);
2483 if (!target_prop) {
2484 return;
2485 }
2486
d190698e
PB
2487 if (object_property_is_child(target_prop)) {
2488 prop_type = g_strdup_printf("link%s",
2489 target_prop->type + strlen("child"));
2490 } else {
2491 prop_type = g_strdup(target_prop->type);
2492 }
2493
ef7c7ff6
SH
2494 prop = g_malloc(sizeof(*prop));
2495 prop->target_obj = target_obj;
1590d266 2496 prop->target_name = g_strdup(target_name);
ef7c7ff6 2497
d190698e 2498 op = object_property_add(obj, name, prop_type,
64607d08
PB
2499 property_get_alias,
2500 property_set_alias,
2501 property_release_alias,
8ae9a9ef
GA
2502 prop, &local_err);
2503 if (local_err) {
2504 error_propagate(errp, local_err);
2505 g_free(prop);
2506 goto out;
2507 }
64607d08 2508 op->resolve = property_resolve_alias;
d190698e 2509
a18bb417 2510 object_property_set_description(obj, op->name,
80742642
GA
2511 target_prop->description,
2512 &error_abort);
2513
8ae9a9ef 2514out:
d190698e 2515 g_free(prop_type);
ef7c7ff6
SH
2516}
2517
80742642
GA
2518void object_property_set_description(Object *obj, const char *name,
2519 const char *description, Error **errp)
2520{
2521 ObjectProperty *op;
2522
2523 op = object_property_find(obj, name, errp);
2524 if (!op) {
2525 return;
2526 }
2527
2528 g_free(op->description);
2529 op->description = g_strdup(description);
2530}
2531
16bf7f52
DB
2532void object_class_property_set_description(ObjectClass *klass,
2533 const char *name,
2534 const char *description,
2535 Error **errp)
2536{
2537 ObjectProperty *op;
2538
2539 op = g_hash_table_lookup(klass->properties, name);
2540 if (!op) {
2541 error_setg(errp, "Property '.%s' not found", name);
2542 return;
2543 }
2544
2545 g_free(op->description);
2546 op->description = g_strdup(description);
2547}
2548
7439a036 2549static void object_class_init(ObjectClass *klass, void *data)
2f262e06 2550{
7439a036
MAL
2551 object_class_property_add_str(klass, "type", qdev_get_type,
2552 NULL, &error_abort);
2f262e06
PB
2553}
2554
745549c8
PB
2555static void register_types(void)
2556{
2557 static TypeInfo interface_info = {
2558 .name = TYPE_INTERFACE,
33e95c63 2559 .class_size = sizeof(InterfaceClass),
745549c8
PB
2560 .abstract = true,
2561 };
2562
2563 static TypeInfo object_info = {
2564 .name = TYPE_OBJECT,
2565 .instance_size = sizeof(Object),
7439a036 2566 .class_init = object_class_init,
745549c8
PB
2567 .abstract = true,
2568 };
2569
049cb3cf
PB
2570 type_interface = type_register_internal(&interface_info);
2571 type_register_internal(&object_info);
745549c8
PB
2572}
2573
2574type_init(register_types)
This page took 0.825261 seconds and 4 git commands to generate.