]> Git Repo - qemu.git/blame - qom/object.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[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
14cccb61 13#include "qom/object.h"
2f28d2ff 14#include "qemu-common.h"
7b1b5d19 15#include "qapi/visitor.h"
1f21772d 16#include "qapi-visit.h"
b2cd7dee
PB
17#include "qapi/string-input-visitor.h"
18#include "qapi/string-output-visitor.h"
7b1b5d19 19#include "qapi/qmp/qerror.h"
fa131d94 20#include "trace.h"
2f28d2ff 21
7b7b7d18
PB
22/* TODO: replace QObject with a simpler visitor to avoid a dependency
23 * of the QOM core on QObject? */
14cccb61 24#include "qom/qom-qobject.h"
7b1b5d19
PB
25#include "qapi/qmp/qobject.h"
26#include "qapi/qmp/qbool.h"
27#include "qapi/qmp/qint.h"
28#include "qapi/qmp/qstring.h"
7b7b7d18 29
2f28d2ff
AL
30#define MAX_INTERFACES 32
31
32typedef struct InterfaceImpl InterfaceImpl;
33typedef struct TypeImpl TypeImpl;
34
35struct InterfaceImpl
36{
33e95c63 37 const char *typename;
2f28d2ff
AL
38};
39
40struct TypeImpl
41{
42 const char *name;
43
44 size_t class_size;
45
46 size_t instance_size;
47
48 void (*class_init)(ObjectClass *klass, void *data);
3b50e311 49 void (*class_base_init)(ObjectClass *klass, void *data);
2f28d2ff
AL
50 void (*class_finalize)(ObjectClass *klass, void *data);
51
52 void *class_data;
53
54 void (*instance_init)(Object *obj);
8231c2dd 55 void (*instance_post_init)(Object *obj);
2f28d2ff
AL
56 void (*instance_finalize)(Object *obj);
57
58 bool abstract;
59
60 const char *parent;
61 TypeImpl *parent_type;
62
63 ObjectClass *class;
64
65 int num_interfaces;
66 InterfaceImpl interfaces[MAX_INTERFACES];
67};
68
9970bd88
PB
69static Type type_interface;
70
2f28d2ff
AL
71static GHashTable *type_table_get(void)
72{
73 static GHashTable *type_table;
74
75 if (type_table == NULL) {
76 type_table = g_hash_table_new(g_str_hash, g_str_equal);
77 }
78
79 return type_table;
80}
81
f54c19ca
HP
82static bool enumerating_types;
83
2f28d2ff
AL
84static void type_table_add(TypeImpl *ti)
85{
f54c19ca 86 assert(!enumerating_types);
2f28d2ff
AL
87 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
88}
89
90static TypeImpl *type_table_lookup(const char *name)
91{
92 return g_hash_table_lookup(type_table_get(), name);
93}
94
b061dc41 95static TypeImpl *type_new(const TypeInfo *info)
2f28d2ff
AL
96{
97 TypeImpl *ti = g_malloc0(sizeof(*ti));
33e95c63 98 int i;
2f28d2ff
AL
99
100 g_assert(info->name != NULL);
101
73093354
AL
102 if (type_table_lookup(info->name) != NULL) {
103 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
104 abort();
105 }
106
2f28d2ff
AL
107 ti->name = g_strdup(info->name);
108 ti->parent = g_strdup(info->parent);
109
110 ti->class_size = info->class_size;
111 ti->instance_size = info->instance_size;
112
113 ti->class_init = info->class_init;
3b50e311 114 ti->class_base_init = info->class_base_init;
2f28d2ff
AL
115 ti->class_finalize = info->class_finalize;
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
152static TypeImpl *type_get_by_name(const char *name)
153{
154 if (name == NULL) {
155 return NULL;
156 }
157
158 return type_table_lookup(name);
159}
160
161static TypeImpl *type_get_parent(TypeImpl *type)
162{
163 if (!type->parent_type && type->parent) {
164 type->parent_type = type_get_by_name(type->parent);
165 g_assert(type->parent_type != NULL);
166 }
167
168 return type->parent_type;
169}
170
171static bool type_has_parent(TypeImpl *type)
172{
173 return (type->parent != NULL);
174}
175
176static size_t type_class_get_size(TypeImpl *ti)
177{
178 if (ti->class_size) {
179 return ti->class_size;
180 }
181
182 if (type_has_parent(ti)) {
183 return type_class_get_size(type_get_parent(ti));
184 }
185
186 return sizeof(ObjectClass);
187}
188
aca59af6
IM
189static size_t type_object_get_size(TypeImpl *ti)
190{
191 if (ti->instance_size) {
192 return ti->instance_size;
193 }
194
195 if (type_has_parent(ti)) {
196 return type_object_get_size(type_get_parent(ti));
197 }
198
199 return 0;
200}
201
33e95c63 202static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
2f28d2ff 203{
33e95c63
AL
204 assert(target_type);
205
206 /* Check if typename is a direct ancestor of type */
207 while (type) {
208 if (type == target_type) {
209 return true;
210 }
2f28d2ff 211
33e95c63
AL
212 type = type_get_parent(type);
213 }
214
215 return false;
216}
217
218static void type_initialize(TypeImpl *ti);
219
b061dc41
PB
220static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
221 TypeImpl *parent_type)
33e95c63
AL
222{
223 InterfaceClass *new_iface;
224 TypeInfo info = { };
225 TypeImpl *iface_impl;
226
b061dc41
PB
227 info.parent = parent_type->name;
228 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
33e95c63
AL
229 info.abstract = true;
230
b061dc41
PB
231 iface_impl = type_new(&info);
232 iface_impl->parent_type = parent_type;
33e95c63
AL
233 type_initialize(iface_impl);
234 g_free((char *)info.name);
235
236 new_iface = (InterfaceClass *)iface_impl->class;
237 new_iface->concrete_class = ti->class;
b061dc41 238 new_iface->interface_type = interface_type;
33e95c63
AL
239
240 ti->class->interfaces = g_slist_append(ti->class->interfaces,
241 iface_impl->class);
2f28d2ff
AL
242}
243
ac451033 244static void type_initialize(TypeImpl *ti)
2f28d2ff 245{
745549c8 246 TypeImpl *parent;
2f28d2ff
AL
247
248 if (ti->class) {
249 return;
250 }
251
252 ti->class_size = type_class_get_size(ti);
aca59af6 253 ti->instance_size = type_object_get_size(ti);
2f28d2ff
AL
254
255 ti->class = g_malloc0(ti->class_size);
2f28d2ff 256
745549c8
PB
257 parent = type_get_parent(ti);
258 if (parent) {
ac451033 259 type_initialize(parent);
33e95c63
AL
260 GSList *e;
261 int i;
2f28d2ff 262
2f28d2ff 263 g_assert(parent->class_size <= ti->class_size);
745549c8 264 memcpy(ti->class, parent->class, parent->class_size);
3e407de4 265 ti->class->interfaces = NULL;
33e95c63
AL
266
267 for (e = parent->class->interfaces; e; e = e->next) {
b061dc41
PB
268 InterfaceClass *iface = e->data;
269 ObjectClass *klass = OBJECT_CLASS(iface);
270
271 type_initialize_interface(ti, iface->interface_type, klass->type);
33e95c63
AL
272 }
273
274 for (i = 0; i < ti->num_interfaces; i++) {
275 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
276 for (e = ti->class->interfaces; e; e = e->next) {
277 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
278
279 if (type_is_ancestor(target_type, t)) {
280 break;
281 }
282 }
283
284 if (e) {
285 continue;
286 }
287
b061dc41 288 type_initialize_interface(ti, t, t);
33e95c63 289 }
745549c8 290 }
2f28d2ff 291
745549c8 292 ti->class->type = ti;
3b50e311 293
745549c8
PB
294 while (parent) {
295 if (parent->class_base_init) {
296 parent->class_base_init(ti->class, ti->class_data);
3b50e311 297 }
745549c8 298 parent = type_get_parent(parent);
2f28d2ff
AL
299 }
300
2f28d2ff
AL
301 if (ti->class_init) {
302 ti->class_init(ti->class, ti->class_data);
303 }
2f28d2ff
AL
304}
305
306static void object_init_with_type(Object *obj, TypeImpl *ti)
307{
2f28d2ff
AL
308 if (type_has_parent(ti)) {
309 object_init_with_type(obj, type_get_parent(ti));
310 }
311
2f28d2ff
AL
312 if (ti->instance_init) {
313 ti->instance_init(obj);
314 }
315}
316
8231c2dd
EH
317static void object_post_init_with_type(Object *obj, TypeImpl *ti)
318{
319 if (ti->instance_post_init) {
320 ti->instance_post_init(obj);
321 }
322
323 if (type_has_parent(ti)) {
324 object_post_init_with_type(obj, type_get_parent(ti));
325 }
326}
327
5b9237f6 328void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
2f28d2ff
AL
329{
330 Object *obj = data;
331
332 g_assert(type != NULL);
ac451033 333 type_initialize(type);
aca59af6
IM
334
335 g_assert(type->instance_size >= sizeof(Object));
2f28d2ff 336 g_assert(type->abstract == false);
5b9237f6 337 g_assert(size >= type->instance_size);
2f28d2ff
AL
338
339 memset(obj, 0, type->instance_size);
340 obj->class = type->class;
764b6312 341 object_ref(obj);
57c9fafe 342 QTAILQ_INIT(&obj->properties);
2f28d2ff 343 object_init_with_type(obj, type);
8231c2dd 344 object_post_init_with_type(obj, type);
2f28d2ff
AL
345}
346
213f0c4f 347void object_initialize(void *data, size_t size, const char *typename)
2f28d2ff
AL
348{
349 TypeImpl *type = type_get_by_name(typename);
350
5b9237f6 351 object_initialize_with_type(data, size, type);
2f28d2ff
AL
352}
353
5d9d3f47
AF
354static inline bool object_property_is_child(ObjectProperty *prop)
355{
356 return strstart(prop->type, "child<", NULL);
357}
358
57c9fafe
AL
359static void object_property_del_all(Object *obj)
360{
361 while (!QTAILQ_EMPTY(&obj->properties)) {
362 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
363
364 QTAILQ_REMOVE(&obj->properties, prop, node);
365
366 if (prop->release) {
367 prop->release(obj, prop->name, prop->opaque);
368 }
369
370 g_free(prop->name);
371 g_free(prop->type);
372 g_free(prop);
373 }
374}
375
376static void object_property_del_child(Object *obj, Object *child, Error **errp)
377{
378 ObjectProperty *prop;
379
380 QTAILQ_FOREACH(prop, &obj->properties, node) {
5d9d3f47 381 if (object_property_is_child(prop) && prop->opaque == child) {
57c9fafe 382 object_property_del(obj, prop->name, errp);
6c1fdcf9 383 break;
57c9fafe
AL
384 }
385 }
386}
387
388void object_unparent(Object *obj)
389{
e998fa8d
MT
390 if (obj->parent) {
391 object_property_del_child(obj->parent, obj, NULL);
392 }
57c9fafe
AL
393}
394
2f28d2ff
AL
395static void object_deinit(Object *obj, TypeImpl *type)
396{
397 if (type->instance_finalize) {
398 type->instance_finalize(obj);
399 }
400
2f28d2ff
AL
401 if (type_has_parent(type)) {
402 object_deinit(obj, type_get_parent(type));
403 }
404}
405
339c2708 406static void object_finalize(void *data)
2f28d2ff
AL
407{
408 Object *obj = data;
409 TypeImpl *ti = obj->class->type;
410
57c9fafe 411 object_property_del_all(obj);
76a6e1cc 412 object_deinit(obj, ti);
db85b575
AL
413
414 g_assert(obj->ref == 0);
fde9bf44
PB
415 if (obj->free) {
416 obj->free(obj);
417 }
2f28d2ff
AL
418}
419
420Object *object_new_with_type(Type type)
421{
422 Object *obj;
423
424 g_assert(type != NULL);
ac451033 425 type_initialize(type);
2f28d2ff
AL
426
427 obj = g_malloc(type->instance_size);
5b9237f6 428 object_initialize_with_type(obj, type->instance_size, type);
fde9bf44 429 obj->free = g_free;
2f28d2ff
AL
430
431 return obj;
432}
433
434Object *object_new(const char *typename)
435{
436 TypeImpl *ti = type_get_by_name(typename);
437
438 return object_new_with_type(ti);
439}
440
2f28d2ff
AL
441Object *object_dynamic_cast(Object *obj, const char *typename)
442{
b7f43fe4 443 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
acc4af3f
PB
444 return obj;
445 }
446
2f28d2ff
AL
447 return NULL;
448}
449
be17f18b
PB
450Object *object_dynamic_cast_assert(Object *obj, const char *typename,
451 const char *file, int line, const char *func)
2f28d2ff 452{
fa131d94
PB
453 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
454 typename, file, line, func);
455
3556c233 456#ifdef CONFIG_QOM_CAST_DEBUG
03587328
AL
457 int i;
458 Object *inst;
459
95916abc 460 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
0ab4c94c 461 if (obj->class->object_cast_cache[i] == typename) {
03587328
AL
462 goto out;
463 }
464 }
465
466 inst = object_dynamic_cast(obj, typename);
2f28d2ff 467
b7f43fe4 468 if (!inst && obj) {
be17f18b
PB
469 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
470 file, line, func, obj, typename);
2f28d2ff
AL
471 abort();
472 }
473
3556c233 474 assert(obj == inst);
03587328 475
95916abc 476 if (obj && obj == inst) {
03587328 477 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
0ab4c94c
PC
478 obj->class->object_cast_cache[i - 1] =
479 obj->class->object_cast_cache[i];
03587328 480 }
0ab4c94c 481 obj->class->object_cast_cache[i - 1] = typename;
03587328
AL
482 }
483
484out:
3556c233
PB
485#endif
486 return obj;
2f28d2ff
AL
487}
488
489ObjectClass *object_class_dynamic_cast(ObjectClass *class,
490 const char *typename)
491{
33e95c63 492 ObjectClass *ret = NULL;
bf0fda34
PB
493 TypeImpl *target_type;
494 TypeImpl *type;
2f28d2ff 495
bf0fda34
PB
496 if (!class) {
497 return NULL;
498 }
499
793c96b5 500 /* A simple fast path that can trigger a lot for leaf classes. */
bf0fda34 501 type = class->type;
793c96b5
PB
502 if (type->name == typename) {
503 return class;
504 }
505
bf0fda34 506 target_type = type_get_by_name(typename);
9ab880b3
AG
507 if (!target_type) {
508 /* target class type unknown, so fail the cast */
509 return NULL;
510 }
511
00e2ceae
PC
512 if (type->class->interfaces &&
513 type_is_ancestor(target_type, type_interface)) {
33e95c63
AL
514 int found = 0;
515 GSList *i;
2f28d2ff 516
33e95c63
AL
517 for (i = class->interfaces; i; i = i->next) {
518 ObjectClass *target_class = i->data;
519
520 if (type_is_ancestor(target_class->type, target_type)) {
521 ret = target_class;
522 found++;
523 }
524 }
525
526 /* The match was ambiguous, don't allow a cast */
527 if (found > 1) {
528 ret = NULL;
529 }
530 } else if (type_is_ancestor(type, target_type)) {
531 ret = class;
2f28d2ff
AL
532 }
533
33e95c63 534 return ret;
2f28d2ff
AL
535}
536
537ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
be17f18b
PB
538 const char *typename,
539 const char *file, int line,
540 const char *func)
2f28d2ff 541{
fa131d94
PB
542 ObjectClass *ret;
543
544 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
545 typename, file, line, func);
2f28d2ff 546
03587328
AL
547#ifdef CONFIG_QOM_CAST_DEBUG
548 int i;
549
9d6a3d58 550 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
0ab4c94c 551 if (class->class_cast_cache[i] == typename) {
03587328
AL
552 ret = class;
553 goto out;
554 }
555 }
556#else
9d6a3d58 557 if (!class || !class->interfaces) {
3556c233
PB
558 return class;
559 }
560#endif
561
fa131d94 562 ret = object_class_dynamic_cast(class, typename);
bf0fda34 563 if (!ret && class) {
be17f18b
PB
564 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
565 file, line, func, class, typename);
2f28d2ff
AL
566 abort();
567 }
568
03587328 569#ifdef CONFIG_QOM_CAST_DEBUG
9d6a3d58 570 if (class && ret == class) {
03587328 571 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
0ab4c94c 572 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
03587328 573 }
0ab4c94c 574 class->class_cast_cache[i - 1] = typename;
03587328
AL
575 }
576out:
577#endif
2f28d2ff
AL
578 return ret;
579}
580
581const char *object_get_typename(Object *obj)
582{
583 return obj->class->type->name;
584}
585
586ObjectClass *object_get_class(Object *obj)
587{
588 return obj->class;
589}
590
17862378
AF
591bool object_class_is_abstract(ObjectClass *klass)
592{
593 return klass->type->abstract;
594}
595
2f28d2ff
AL
596const char *object_class_get_name(ObjectClass *klass)
597{
598 return klass->type->name;
599}
600
601ObjectClass *object_class_by_name(const char *typename)
602{
603 TypeImpl *type = type_get_by_name(typename);
604
605 if (!type) {
606 return NULL;
607 }
608
ac451033 609 type_initialize(type);
2f28d2ff
AL
610
611 return type->class;
612}
613
e7cce67f
PB
614ObjectClass *object_class_get_parent(ObjectClass *class)
615{
616 TypeImpl *type = type_get_parent(class->type);
617
618 if (!type) {
619 return NULL;
620 }
621
622 type_initialize(type);
623
624 return type->class;
625}
626
2f28d2ff
AL
627typedef struct OCFData
628{
629 void (*fn)(ObjectClass *klass, void *opaque);
93c511a1
AL
630 const char *implements_type;
631 bool include_abstract;
2f28d2ff
AL
632 void *opaque;
633} OCFData;
634
635static void object_class_foreach_tramp(gpointer key, gpointer value,
636 gpointer opaque)
637{
638 OCFData *data = opaque;
639 TypeImpl *type = value;
93c511a1 640 ObjectClass *k;
2f28d2ff 641
ac451033 642 type_initialize(type);
93c511a1 643 k = type->class;
2f28d2ff 644
93c511a1
AL
645 if (!data->include_abstract && type->abstract) {
646 return;
647 }
648
649 if (data->implements_type &&
650 !object_class_dynamic_cast(k, data->implements_type)) {
651 return;
652 }
653
654 data->fn(k, data->opaque);
2f28d2ff
AL
655}
656
657void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 658 const char *implements_type, bool include_abstract,
2f28d2ff
AL
659 void *opaque)
660{
93c511a1 661 OCFData data = { fn, implements_type, include_abstract, opaque };
2f28d2ff 662
f54c19ca 663 enumerating_types = true;
2f28d2ff 664 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
f54c19ca 665 enumerating_types = false;
2f28d2ff 666}
57c9fafe 667
32efc535
PB
668int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
669 void *opaque)
670{
8af734ca 671 ObjectProperty *prop, *next;
32efc535
PB
672 int ret = 0;
673
8af734ca 674 QTAILQ_FOREACH_SAFE(prop, &obj->properties, node, next) {
32efc535
PB
675 if (object_property_is_child(prop)) {
676 ret = fn(prop->opaque, opaque);
677 if (ret != 0) {
678 break;
679 }
680 }
681 }
682 return ret;
683}
684
418ba9e5
AF
685static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
686{
687 GSList **list = opaque;
688
689 *list = g_slist_prepend(*list, klass);
690}
691
692GSList *object_class_get_list(const char *implements_type,
693 bool include_abstract)
694{
695 GSList *list = NULL;
696
697 object_class_foreach(object_class_get_list_tramp,
698 implements_type, include_abstract, &list);
699 return list;
700}
701
57c9fafe
AL
702void object_ref(Object *obj)
703{
8ffad850
PC
704 if (!obj) {
705 return;
706 }
f08c03f3 707 atomic_inc(&obj->ref);
57c9fafe
AL
708}
709
710void object_unref(Object *obj)
711{
8ffad850
PC
712 if (!obj) {
713 return;
714 }
57c9fafe 715 g_assert(obj->ref > 0);
57c9fafe
AL
716
717 /* parent always holds a reference to its children */
f08c03f3 718 if (atomic_fetch_dec(&obj->ref) == 1) {
57c9fafe
AL
719 object_finalize(obj);
720 }
721}
722
64607d08
PB
723ObjectProperty *
724object_property_add(Object *obj, const char *name, const char *type,
725 ObjectPropertyAccessor *get,
726 ObjectPropertyAccessor *set,
727 ObjectPropertyRelease *release,
728 void *opaque, Error **errp)
57c9fafe 729{
54852b03 730 ObjectProperty *prop;
33965904
PC
731 size_t name_len = strlen(name);
732
733 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
734 int i;
735 ObjectProperty *ret;
736 char *name_no_array = g_strdup(name);
737
738 name_no_array[name_len - 3] = '\0';
739 for (i = 0; ; ++i) {
740 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
741
742 ret = object_property_add(obj, full_name, type, get, set,
743 release, opaque, NULL);
744 g_free(full_name);
745 if (ret) {
746 break;
747 }
748 }
749 g_free(name_no_array);
750 return ret;
751 }
54852b03
PM
752
753 QTAILQ_FOREACH(prop, &obj->properties, node) {
754 if (strcmp(prop->name, name) == 0) {
755 error_setg(errp, "attempt to add duplicate property '%s'"
756 " to object (type '%s')", name,
757 object_get_typename(obj));
64607d08 758 return NULL;
54852b03
PM
759 }
760 }
761
762 prop = g_malloc0(sizeof(*prop));
57c9fafe
AL
763
764 prop->name = g_strdup(name);
765 prop->type = g_strdup(type);
766
767 prop->get = get;
768 prop->set = set;
769 prop->release = release;
770 prop->opaque = opaque;
771
772 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
64607d08 773 return prop;
57c9fafe
AL
774}
775
89bfe000
PB
776ObjectProperty *object_property_find(Object *obj, const char *name,
777 Error **errp)
57c9fafe
AL
778{
779 ObjectProperty *prop;
780
781 QTAILQ_FOREACH(prop, &obj->properties, node) {
782 if (strcmp(prop->name, name) == 0) {
783 return prop;
784 }
785 }
786
f231b88d 787 error_setg(errp, "Property '.%s' not found", name);
57c9fafe
AL
788 return NULL;
789}
790
791void object_property_del(Object *obj, const char *name, Error **errp)
792{
89bfe000 793 ObjectProperty *prop = object_property_find(obj, name, errp);
0866aca1 794 if (prop == NULL) {
0866aca1
AL
795 return;
796 }
57c9fafe 797
0866aca1
AL
798 if (prop->release) {
799 prop->release(obj, name, prop->opaque);
800 }
801
802 QTAILQ_REMOVE(&obj->properties, prop, node);
57c9fafe
AL
803
804 g_free(prop->name);
805 g_free(prop->type);
806 g_free(prop);
807}
808
809void object_property_get(Object *obj, Visitor *v, const char *name,
810 Error **errp)
811{
89bfe000 812 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 813 if (prop == NULL) {
57c9fafe
AL
814 return;
815 }
816
817 if (!prop->get) {
818 error_set(errp, QERR_PERMISSION_DENIED);
819 } else {
820 prop->get(obj, v, prop->opaque, name, errp);
821 }
822}
823
824void object_property_set(Object *obj, Visitor *v, const char *name,
825 Error **errp)
826{
89bfe000 827 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 828 if (prop == NULL) {
57c9fafe
AL
829 return;
830 }
831
832 if (!prop->set) {
833 error_set(errp, QERR_PERMISSION_DENIED);
834 } else {
835 prop->set(obj, v, prop->opaque, name, errp);
836 }
837}
838
7b7b7d18
PB
839void object_property_set_str(Object *obj, const char *value,
840 const char *name, Error **errp)
841{
842 QString *qstr = qstring_from_str(value);
843 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
844
845 QDECREF(qstr);
846}
847
848char *object_property_get_str(Object *obj, const char *name,
849 Error **errp)
850{
851 QObject *ret = object_property_get_qobject(obj, name, errp);
852 QString *qstring;
853 char *retval;
854
855 if (!ret) {
856 return NULL;
857 }
858 qstring = qobject_to_qstring(ret);
859 if (!qstring) {
860 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
861 retval = NULL;
862 } else {
863 retval = g_strdup(qstring_get_str(qstring));
864 }
865
866 QDECREF(qstring);
867 return retval;
868}
869
1d9c5a12
PB
870void object_property_set_link(Object *obj, Object *value,
871 const char *name, Error **errp)
872{
2d3aa28c
VY
873 gchar *path = object_get_canonical_path(value);
874 object_property_set_str(obj, path, name, errp);
875 g_free(path);
1d9c5a12
PB
876}
877
878Object *object_property_get_link(Object *obj, const char *name,
879 Error **errp)
880{
881 char *str = object_property_get_str(obj, name, errp);
882 Object *target = NULL;
883
884 if (str && *str) {
885 target = object_resolve_path(str, NULL);
886 if (!target) {
887 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
888 }
889 }
890
891 g_free(str);
892 return target;
893}
894
7b7b7d18
PB
895void object_property_set_bool(Object *obj, bool value,
896 const char *name, Error **errp)
897{
898 QBool *qbool = qbool_from_int(value);
899 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
900
901 QDECREF(qbool);
902}
903
904bool object_property_get_bool(Object *obj, const char *name,
905 Error **errp)
906{
907 QObject *ret = object_property_get_qobject(obj, name, errp);
908 QBool *qbool;
909 bool retval;
910
911 if (!ret) {
912 return false;
913 }
914 qbool = qobject_to_qbool(ret);
915 if (!qbool) {
916 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
917 retval = false;
918 } else {
919 retval = qbool_get_int(qbool);
920 }
921
922 QDECREF(qbool);
923 return retval;
924}
925
926void object_property_set_int(Object *obj, int64_t value,
927 const char *name, Error **errp)
928{
929 QInt *qint = qint_from_int(value);
930 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
931
932 QDECREF(qint);
933}
934
935int64_t object_property_get_int(Object *obj, const char *name,
936 Error **errp)
937{
938 QObject *ret = object_property_get_qobject(obj, name, errp);
939 QInt *qint;
940 int64_t retval;
941
942 if (!ret) {
943 return -1;
944 }
945 qint = qobject_to_qint(ret);
946 if (!qint) {
947 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
948 retval = -1;
949 } else {
950 retval = qint_get_int(qint);
951 }
952
953 QDECREF(qint);
954 return retval;
b2cd7dee
PB
955}
956
1f21772d
HT
957int object_property_get_enum(Object *obj, const char *name,
958 const char *strings[], Error **errp)
959{
960 StringOutputVisitor *sov;
961 StringInputVisitor *siv;
976620ac 962 char *str;
1f21772d
HT
963 int ret;
964
965 sov = string_output_visitor_new(false);
966 object_property_get(obj, string_output_get_visitor(sov), name, errp);
976620ac
CF
967 str = string_output_get_string(sov);
968 siv = string_input_visitor_new(str);
1f21772d
HT
969 string_output_visitor_cleanup(sov);
970 visit_type_enum(string_input_get_visitor(siv),
971 &ret, strings, NULL, name, errp);
976620ac
CF
972
973 g_free(str);
1f21772d
HT
974 string_input_visitor_cleanup(siv);
975
976 return ret;
977}
978
979void object_property_get_uint16List(Object *obj, const char *name,
980 uint16List **list, Error **errp)
981{
982 StringOutputVisitor *ov;
983 StringInputVisitor *iv;
976620ac 984 char *str;
1f21772d
HT
985
986 ov = string_output_visitor_new(false);
987 object_property_get(obj, string_output_get_visitor(ov),
988 name, errp);
976620ac
CF
989 str = string_output_get_string(ov);
990 iv = string_input_visitor_new(str);
1f21772d
HT
991 visit_type_uint16List(string_input_get_visitor(iv),
992 list, NULL, errp);
976620ac
CF
993
994 g_free(str);
1f21772d
HT
995 string_output_visitor_cleanup(ov);
996 string_input_visitor_cleanup(iv);
997}
998
b2cd7dee
PB
999void object_property_parse(Object *obj, const char *string,
1000 const char *name, Error **errp)
1001{
1002 StringInputVisitor *mi;
1003 mi = string_input_visitor_new(string);
1004 object_property_set(obj, string_input_get_visitor(mi), name, errp);
1005
1006 string_input_visitor_cleanup(mi);
1007}
1008
0b7593e0 1009char *object_property_print(Object *obj, const char *name, bool human,
b2cd7dee
PB
1010 Error **errp)
1011{
1012 StringOutputVisitor *mo;
1013 char *string;
1014
0b7593e0 1015 mo = string_output_visitor_new(human);
8185bfc1 1016 object_property_get(obj, string_output_get_visitor(mo), name, errp);
b2cd7dee
PB
1017 string = string_output_get_string(mo);
1018 string_output_visitor_cleanup(mo);
1019 return string;
7b7b7d18
PB
1020}
1021
57c9fafe
AL
1022const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1023{
89bfe000 1024 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 1025 if (prop == NULL) {
57c9fafe
AL
1026 return NULL;
1027 }
1028
1029 return prop->type;
1030}
1031
1032Object *object_get_root(void)
1033{
8b45d447 1034 static Object *root;
57c9fafe 1035
8b45d447
AL
1036 if (!root) {
1037 root = object_new("container");
57c9fafe
AL
1038 }
1039
8b45d447 1040 return root;
57c9fafe
AL
1041}
1042
1043static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
1044 const char *name, Error **errp)
1045{
1046 Object *child = opaque;
1047 gchar *path;
1048
1049 path = object_get_canonical_path(child);
1050 visit_type_str(v, &path, name, errp);
1051 g_free(path);
1052}
1053
64607d08
PB
1054static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1055{
1056 return opaque;
1057}
1058
db85b575
AL
1059static void object_finalize_child_property(Object *obj, const char *name,
1060 void *opaque)
1061{
1062 Object *child = opaque;
1063
bffc687d
PB
1064 if (child->class->unparent) {
1065 (child->class->unparent)(child);
1066 }
1067 child->parent = NULL;
db85b575
AL
1068 object_unref(child);
1069}
1070
57c9fafe
AL
1071void object_property_add_child(Object *obj, const char *name,
1072 Object *child, Error **errp)
1073{
b0ed5e9f 1074 Error *local_err = NULL;
57c9fafe 1075 gchar *type;
64607d08 1076 ObjectProperty *op;
57c9fafe
AL
1077
1078 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1079
64607d08
PB
1080 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1081 object_finalize_child_property, child, &local_err);
b0ed5e9f
PB
1082 if (local_err) {
1083 error_propagate(errp, local_err);
1084 goto out;
1085 }
64607d08
PB
1086
1087 op->resolve = object_resolve_child_property;
57c9fafe
AL
1088 object_ref(child);
1089 g_assert(child->parent == NULL);
1090 child->parent = obj;
1091
b0ed5e9f 1092out:
57c9fafe
AL
1093 g_free(type);
1094}
1095
39f72ef9
SH
1096void object_property_allow_set_link(Object *obj, const char *name,
1097 Object *val, Error **errp)
1098{
1099 /* Allow the link to be set, always */
1100}
1101
9561fda8
SH
1102typedef struct {
1103 Object **child;
39f72ef9 1104 void (*check)(Object *, const char *, Object *, Error **);
9561fda8
SH
1105 ObjectPropertyLinkFlags flags;
1106} LinkProperty;
1107
57c9fafe
AL
1108static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1109 const char *name, Error **errp)
1110{
9561fda8
SH
1111 LinkProperty *lprop = opaque;
1112 Object **child = lprop->child;
57c9fafe
AL
1113 gchar *path;
1114
1115 if (*child) {
1116 path = object_get_canonical_path(*child);
1117 visit_type_str(v, &path, name, errp);
1118 g_free(path);
1119 } else {
1120 path = (gchar *)"";
1121 visit_type_str(v, &path, name, errp);
1122 }
1123}
1124
f5ec6704
SH
1125/*
1126 * object_resolve_link:
1127 *
1128 * Lookup an object and ensure its type matches the link property type. This
1129 * is similar to object_resolve_path() except type verification against the
1130 * link property is performed.
1131 *
1132 * Returns: The matched object or NULL on path lookup failures.
1133 */
1134static Object *object_resolve_link(Object *obj, const char *name,
1135 const char *path, Error **errp)
1136{
1137 const char *type;
1138 gchar *target_type;
1139 bool ambiguous = false;
1140 Object *target;
1141
1142 /* Go from link<FOO> to FOO. */
1143 type = object_property_get_type(obj, name, NULL);
1144 target_type = g_strndup(&type[5], strlen(type) - 6);
1145 target = object_resolve_path_type(path, target_type, &ambiguous);
1146
1147 if (ambiguous) {
f231b88d
CR
1148 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
1149 "Path '%s' does not uniquely identify an object", path);
f5ec6704
SH
1150 } else if (!target) {
1151 target = object_resolve_path(path, &ambiguous);
1152 if (target || ambiguous) {
1153 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1154 } else {
1155 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1156 }
1157 target = NULL;
1158 }
1159 g_free(target_type);
1160
1161 return target;
1162}
1163
57c9fafe
AL
1164static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1165 const char *name, Error **errp)
1166{
c6aed983 1167 Error *local_err = NULL;
9561fda8
SH
1168 LinkProperty *prop = opaque;
1169 Object **child = prop->child;
c6aed983
SH
1170 Object *old_target = *child;
1171 Object *new_target = NULL;
1172 char *path = NULL;
57c9fafe 1173
c6aed983 1174 visit_type_str(v, &path, name, &local_err);
57c9fafe 1175
c6aed983
SH
1176 if (!local_err && strcmp(path, "") != 0) {
1177 new_target = object_resolve_link(obj, name, path, &local_err);
57c9fafe
AL
1178 }
1179
1180 g_free(path);
c6aed983
SH
1181 if (local_err) {
1182 error_propagate(errp, local_err);
1183 return;
1184 }
f0cdc966 1185
39f72ef9
SH
1186 prop->check(obj, name, new_target, &local_err);
1187 if (local_err) {
1188 error_propagate(errp, local_err);
1189 return;
1190 }
1191
8ffad850 1192 object_ref(new_target);
c6aed983 1193 *child = new_target;
8ffad850 1194 object_unref(old_target);
57c9fafe
AL
1195}
1196
64607d08
PB
1197static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1198{
1199 LinkProperty *lprop = opaque;
1200
1201 return *lprop->child;
1202}
1203
9561fda8
SH
1204static void object_release_link_property(Object *obj, const char *name,
1205 void *opaque)
1206{
1207 LinkProperty *prop = opaque;
1208
1209 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1210 object_unref(*prop->child);
1211 }
1212 g_free(prop);
1213}
1214
57c9fafe
AL
1215void object_property_add_link(Object *obj, const char *name,
1216 const char *type, Object **child,
39f72ef9
SH
1217 void (*check)(Object *, const char *,
1218 Object *, Error **),
9561fda8 1219 ObjectPropertyLinkFlags flags,
57c9fafe
AL
1220 Error **errp)
1221{
9561fda8
SH
1222 Error *local_err = NULL;
1223 LinkProperty *prop = g_malloc(sizeof(*prop));
57c9fafe 1224 gchar *full_type;
64607d08 1225 ObjectProperty *op;
57c9fafe 1226
9561fda8 1227 prop->child = child;
39f72ef9 1228 prop->check = check;
9561fda8
SH
1229 prop->flags = flags;
1230
57c9fafe
AL
1231 full_type = g_strdup_printf("link<%s>", type);
1232
64607d08
PB
1233 op = object_property_add(obj, name, full_type,
1234 object_get_link_property,
1235 check ? object_set_link_property : NULL,
1236 object_release_link_property,
1237 prop,
1238 &local_err);
9561fda8
SH
1239 if (local_err) {
1240 error_propagate(errp, local_err);
1241 g_free(prop);
64607d08 1242 goto out;
9561fda8 1243 }
57c9fafe 1244
64607d08
PB
1245 op->resolve = object_resolve_link_property;
1246
1247out:
57c9fafe
AL
1248 g_free(full_type);
1249}
1250
11f590b1
SH
1251gchar *object_get_canonical_path_component(Object *obj)
1252{
1253 ObjectProperty *prop = NULL;
1254
1255 g_assert(obj);
1256 g_assert(obj->parent != NULL);
1257
1258 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1259 if (!object_property_is_child(prop)) {
1260 continue;
1261 }
1262
1263 if (prop->opaque == obj) {
1264 return g_strdup(prop->name);
1265 }
1266 }
1267
1268 /* obj had a parent but was not a child, should never happen */
1269 g_assert_not_reached();
1270 return NULL;
1271}
1272
57c9fafe
AL
1273gchar *object_get_canonical_path(Object *obj)
1274{
1275 Object *root = object_get_root();
11f590b1 1276 char *newpath, *path = NULL;
57c9fafe
AL
1277
1278 while (obj != root) {
11f590b1 1279 char *component = object_get_canonical_path_component(obj);
57c9fafe 1280
11f590b1
SH
1281 if (path) {
1282 newpath = g_strdup_printf("%s/%s", component, path);
1283 g_free(component);
1284 g_free(path);
1285 path = newpath;
1286 } else {
1287 path = component;
57c9fafe
AL
1288 }
1289
57c9fafe
AL
1290 obj = obj->parent;
1291 }
1292
11f590b1 1293 newpath = g_strdup_printf("/%s", path ? path : "");
57c9fafe
AL
1294 g_free(path);
1295
1296 return newpath;
1297}
1298
3e84b483 1299Object *object_resolve_path_component(Object *parent, const gchar *part)
a612b2a6 1300{
89bfe000 1301 ObjectProperty *prop = object_property_find(parent, part, NULL);
a612b2a6
PB
1302 if (prop == NULL) {
1303 return NULL;
1304 }
1305
64607d08
PB
1306 if (prop->resolve) {
1307 return prop->resolve(parent, prop->opaque, part);
a612b2a6
PB
1308 } else {
1309 return NULL;
1310 }
1311}
1312
57c9fafe
AL
1313static Object *object_resolve_abs_path(Object *parent,
1314 gchar **parts,
02fe2db6 1315 const char *typename,
57c9fafe
AL
1316 int index)
1317{
57c9fafe
AL
1318 Object *child;
1319
1320 if (parts[index] == NULL) {
02fe2db6 1321 return object_dynamic_cast(parent, typename);
57c9fafe
AL
1322 }
1323
1324 if (strcmp(parts[index], "") == 0) {
02fe2db6 1325 return object_resolve_abs_path(parent, parts, typename, index + 1);
57c9fafe
AL
1326 }
1327
a612b2a6 1328 child = object_resolve_path_component(parent, parts[index]);
57c9fafe
AL
1329 if (!child) {
1330 return NULL;
1331 }
1332
02fe2db6 1333 return object_resolve_abs_path(child, parts, typename, index + 1);
57c9fafe
AL
1334}
1335
1336static Object *object_resolve_partial_path(Object *parent,
1337 gchar **parts,
02fe2db6 1338 const char *typename,
57c9fafe
AL
1339 bool *ambiguous)
1340{
1341 Object *obj;
1342 ObjectProperty *prop;
1343
02fe2db6 1344 obj = object_resolve_abs_path(parent, parts, typename, 0);
57c9fafe
AL
1345
1346 QTAILQ_FOREACH(prop, &parent->properties, node) {
1347 Object *found;
1348
5d9d3f47 1349 if (!object_property_is_child(prop)) {
57c9fafe
AL
1350 continue;
1351 }
1352
02fe2db6
PB
1353 found = object_resolve_partial_path(prop->opaque, parts,
1354 typename, ambiguous);
57c9fafe
AL
1355 if (found) {
1356 if (obj) {
1357 if (ambiguous) {
1358 *ambiguous = true;
1359 }
1360 return NULL;
1361 }
1362 obj = found;
1363 }
1364
1365 if (ambiguous && *ambiguous) {
1366 return NULL;
1367 }
1368 }
1369
1370 return obj;
1371}
1372
02fe2db6
PB
1373Object *object_resolve_path_type(const char *path, const char *typename,
1374 bool *ambiguous)
57c9fafe 1375{
57c9fafe
AL
1376 Object *obj;
1377 gchar **parts;
1378
1379 parts = g_strsplit(path, "/", 0);
2e1103f6 1380 assert(parts);
57c9fafe 1381
2e1103f6 1382 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
57c9fafe
AL
1383 if (ambiguous) {
1384 *ambiguous = false;
1385 }
02fe2db6
PB
1386 obj = object_resolve_partial_path(object_get_root(), parts,
1387 typename, ambiguous);
57c9fafe 1388 } else {
02fe2db6 1389 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
57c9fafe
AL
1390 }
1391
1392 g_strfreev(parts);
1393
1394 return obj;
1395}
1396
02fe2db6
PB
1397Object *object_resolve_path(const char *path, bool *ambiguous)
1398{
1399 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1400}
1401
57c9fafe
AL
1402typedef struct StringProperty
1403{
1404 char *(*get)(Object *, Error **);
1405 void (*set)(Object *, const char *, Error **);
1406} StringProperty;
1407
7b7b7d18
PB
1408static void property_get_str(Object *obj, Visitor *v, void *opaque,
1409 const char *name, Error **errp)
57c9fafe
AL
1410{
1411 StringProperty *prop = opaque;
1412 char *value;
1413
1414 value = prop->get(obj, errp);
1415 if (value) {
1416 visit_type_str(v, &value, name, errp);
1417 g_free(value);
1418 }
1419}
1420
7b7b7d18
PB
1421static void property_set_str(Object *obj, Visitor *v, void *opaque,
1422 const char *name, Error **errp)
57c9fafe
AL
1423{
1424 StringProperty *prop = opaque;
1425 char *value;
1426 Error *local_err = NULL;
1427
1428 visit_type_str(v, &value, name, &local_err);
1429 if (local_err) {
1430 error_propagate(errp, local_err);
1431 return;
1432 }
1433
1434 prop->set(obj, value, errp);
1435 g_free(value);
1436}
1437
7b7b7d18
PB
1438static void property_release_str(Object *obj, const char *name,
1439 void *opaque)
57c9fafe
AL
1440{
1441 StringProperty *prop = opaque;
1442 g_free(prop);
1443}
1444
1445void object_property_add_str(Object *obj, const char *name,
1446 char *(*get)(Object *, Error **),
1447 void (*set)(Object *, const char *, Error **),
1448 Error **errp)
1449{
a01aedc8 1450 Error *local_err = NULL;
57c9fafe
AL
1451 StringProperty *prop = g_malloc0(sizeof(*prop));
1452
1453 prop->get = get;
1454 prop->set = set;
1455
1456 object_property_add(obj, name, "string",
7b7b7d18
PB
1457 get ? property_get_str : NULL,
1458 set ? property_set_str : NULL,
1459 property_release_str,
a01aedc8
SH
1460 prop, &local_err);
1461 if (local_err) {
1462 error_propagate(errp, local_err);
1463 g_free(prop);
1464 }
57c9fafe 1465}
745549c8 1466
0e558843
AL
1467typedef struct BoolProperty
1468{
1469 bool (*get)(Object *, Error **);
1470 void (*set)(Object *, bool, Error **);
1471} BoolProperty;
1472
1473static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1474 const char *name, Error **errp)
1475{
1476 BoolProperty *prop = opaque;
1477 bool value;
1478
1479 value = prop->get(obj, errp);
1480 visit_type_bool(v, &value, name, errp);
1481}
1482
1483static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1484 const char *name, Error **errp)
1485{
1486 BoolProperty *prop = opaque;
1487 bool value;
1488 Error *local_err = NULL;
1489
1490 visit_type_bool(v, &value, name, &local_err);
1491 if (local_err) {
1492 error_propagate(errp, local_err);
1493 return;
1494 }
1495
1496 prop->set(obj, value, errp);
1497}
1498
1499static void property_release_bool(Object *obj, const char *name,
1500 void *opaque)
1501{
1502 BoolProperty *prop = opaque;
1503 g_free(prop);
1504}
1505
1506void object_property_add_bool(Object *obj, const char *name,
1507 bool (*get)(Object *, Error **),
1508 void (*set)(Object *, bool, Error **),
1509 Error **errp)
1510{
a01aedc8 1511 Error *local_err = NULL;
0e558843
AL
1512 BoolProperty *prop = g_malloc0(sizeof(*prop));
1513
1514 prop->get = get;
1515 prop->set = set;
1516
1517 object_property_add(obj, name, "bool",
1518 get ? property_get_bool : NULL,
1519 set ? property_set_bool : NULL,
1520 property_release_bool,
a01aedc8
SH
1521 prop, &local_err);
1522 if (local_err) {
1523 error_propagate(errp, local_err);
1524 g_free(prop);
1525 }
0e558843
AL
1526}
1527
2f262e06
PB
1528static char *qdev_get_type(Object *obj, Error **errp)
1529{
1530 return g_strdup(object_get_typename(obj));
1531}
1532
e732ea63
MT
1533static void property_get_uint8_ptr(Object *obj, Visitor *v,
1534 void *opaque, const char *name,
1535 Error **errp)
1536{
1537 uint8_t value = *(uint8_t *)opaque;
1538 visit_type_uint8(v, &value, name, errp);
1539}
1540
1541static void property_get_uint16_ptr(Object *obj, Visitor *v,
1542 void *opaque, const char *name,
1543 Error **errp)
1544{
1545 uint16_t value = *(uint16_t *)opaque;
1546 visit_type_uint16(v, &value, name, errp);
1547}
1548
1549static void property_get_uint32_ptr(Object *obj, Visitor *v,
1550 void *opaque, const char *name,
1551 Error **errp)
1552{
1553 uint32_t value = *(uint32_t *)opaque;
1554 visit_type_uint32(v, &value, name, errp);
1555}
1556
1557static void property_get_uint64_ptr(Object *obj, Visitor *v,
1558 void *opaque, const char *name,
1559 Error **errp)
1560{
1561 uint64_t value = *(uint64_t *)opaque;
1562 visit_type_uint64(v, &value, name, errp);
1563}
1564
1565void object_property_add_uint8_ptr(Object *obj, const char *name,
1566 const uint8_t *v, Error **errp)
1567{
1568 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1569 NULL, NULL, (void *)v, errp);
1570}
1571
1572void object_property_add_uint16_ptr(Object *obj, const char *name,
1573 const uint16_t *v, Error **errp)
1574{
1575 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1576 NULL, NULL, (void *)v, errp);
1577}
1578
1579void object_property_add_uint32_ptr(Object *obj, const char *name,
1580 const uint32_t *v, Error **errp)
1581{
1582 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1583 NULL, NULL, (void *)v, errp);
1584}
1585
1586void object_property_add_uint64_ptr(Object *obj, const char *name,
1587 const uint64_t *v, Error **errp)
1588{
1589 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1590 NULL, NULL, (void *)v, errp);
1591}
1592
ef7c7ff6
SH
1593typedef struct {
1594 Object *target_obj;
1595 const char *target_name;
1596} AliasProperty;
1597
1598static void property_get_alias(Object *obj, struct Visitor *v, void *opaque,
1599 const char *name, Error **errp)
1600{
1601 AliasProperty *prop = opaque;
1602
1603 object_property_get(prop->target_obj, v, prop->target_name, errp);
1604}
1605
1606static void property_set_alias(Object *obj, struct Visitor *v, void *opaque,
1607 const char *name, Error **errp)
1608{
1609 AliasProperty *prop = opaque;
1610
1611 object_property_set(prop->target_obj, v, prop->target_name, errp);
1612}
1613
64607d08
PB
1614static Object *property_resolve_alias(Object *obj, void *opaque,
1615 const gchar *part)
1616{
1617 AliasProperty *prop = opaque;
1618
1619 return object_resolve_path_component(prop->target_obj, prop->target_name);
1620}
1621
ef7c7ff6
SH
1622static void property_release_alias(Object *obj, const char *name, void *opaque)
1623{
1624 AliasProperty *prop = opaque;
1625
1626 g_free(prop);
1627}
1628
1629void object_property_add_alias(Object *obj, const char *name,
1630 Object *target_obj, const char *target_name,
1631 Error **errp)
1632{
1633 AliasProperty *prop;
64607d08 1634 ObjectProperty *op;
ef7c7ff6 1635 ObjectProperty *target_prop;
d190698e 1636 gchar *prop_type;
ef7c7ff6
SH
1637
1638 target_prop = object_property_find(target_obj, target_name, errp);
1639 if (!target_prop) {
1640 return;
1641 }
1642
d190698e
PB
1643 if (object_property_is_child(target_prop)) {
1644 prop_type = g_strdup_printf("link%s",
1645 target_prop->type + strlen("child"));
1646 } else {
1647 prop_type = g_strdup(target_prop->type);
1648 }
1649
ef7c7ff6
SH
1650 prop = g_malloc(sizeof(*prop));
1651 prop->target_obj = target_obj;
1652 prop->target_name = target_name;
1653
d190698e 1654 op = object_property_add(obj, name, prop_type,
64607d08
PB
1655 property_get_alias,
1656 property_set_alias,
1657 property_release_alias,
1658 prop, errp);
1659 op->resolve = property_resolve_alias;
d190698e
PB
1660
1661 g_free(prop_type);
ef7c7ff6
SH
1662}
1663
2f262e06
PB
1664static void object_instance_init(Object *obj)
1665{
1666 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1667}
1668
745549c8
PB
1669static void register_types(void)
1670{
1671 static TypeInfo interface_info = {
1672 .name = TYPE_INTERFACE,
33e95c63 1673 .class_size = sizeof(InterfaceClass),
745549c8
PB
1674 .abstract = true,
1675 };
1676
1677 static TypeInfo object_info = {
1678 .name = TYPE_OBJECT,
1679 .instance_size = sizeof(Object),
2f262e06 1680 .instance_init = object_instance_init,
745549c8
PB
1681 .abstract = true,
1682 };
1683
049cb3cf
PB
1684 type_interface = type_register_internal(&interface_info);
1685 type_register_internal(&object_info);
745549c8
PB
1686}
1687
1688type_init(register_types)
This page took 0.478921 seconds and 4 git commands to generate.