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