]> Git Repo - qemu.git/blame - qom/object.c
qom: add a fast path to object_class_dynamic_cast
[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"
b2cd7dee
PB
16#include "qapi/string-input-visitor.h"
17#include "qapi/string-output-visitor.h"
7b1b5d19 18#include "qapi/qmp/qerror.h"
2f28d2ff 19
7b7b7d18
PB
20/* TODO: replace QObject with a simpler visitor to avoid a dependency
21 * of the QOM core on QObject? */
14cccb61 22#include "qom/qom-qobject.h"
7b1b5d19
PB
23#include "qapi/qmp/qobject.h"
24#include "qapi/qmp/qbool.h"
25#include "qapi/qmp/qint.h"
26#include "qapi/qmp/qstring.h"
7b7b7d18 27
2f28d2ff
AL
28#define MAX_INTERFACES 32
29
30typedef struct InterfaceImpl InterfaceImpl;
31typedef struct TypeImpl TypeImpl;
32
33struct InterfaceImpl
34{
33e95c63 35 const char *typename;
2f28d2ff
AL
36};
37
38struct TypeImpl
39{
40 const char *name;
41
42 size_t class_size;
43
44 size_t instance_size;
45
46 void (*class_init)(ObjectClass *klass, void *data);
3b50e311 47 void (*class_base_init)(ObjectClass *klass, void *data);
2f28d2ff
AL
48 void (*class_finalize)(ObjectClass *klass, void *data);
49
50 void *class_data;
51
52 void (*instance_init)(Object *obj);
53 void (*instance_finalize)(Object *obj);
54
55 bool abstract;
56
57 const char *parent;
58 TypeImpl *parent_type;
59
60 ObjectClass *class;
61
62 int num_interfaces;
63 InterfaceImpl interfaces[MAX_INTERFACES];
64};
65
9970bd88
PB
66static Type type_interface;
67
2f28d2ff
AL
68static GHashTable *type_table_get(void)
69{
70 static GHashTable *type_table;
71
72 if (type_table == NULL) {
73 type_table = g_hash_table_new(g_str_hash, g_str_equal);
74 }
75
76 return type_table;
77}
78
79static void type_table_add(TypeImpl *ti)
80{
81 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
82}
83
84static TypeImpl *type_table_lookup(const char *name)
85{
86 return g_hash_table_lookup(type_table_get(), name);
87}
88
049cb3cf 89static TypeImpl *type_register_internal(const TypeInfo *info)
2f28d2ff
AL
90{
91 TypeImpl *ti = g_malloc0(sizeof(*ti));
33e95c63 92 int i;
2f28d2ff
AL
93
94 g_assert(info->name != NULL);
95
73093354
AL
96 if (type_table_lookup(info->name) != NULL) {
97 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
98 abort();
99 }
100
2f28d2ff
AL
101 ti->name = g_strdup(info->name);
102 ti->parent = g_strdup(info->parent);
103
104 ti->class_size = info->class_size;
105 ti->instance_size = info->instance_size;
106
107 ti->class_init = info->class_init;
3b50e311 108 ti->class_base_init = info->class_base_init;
2f28d2ff
AL
109 ti->class_finalize = info->class_finalize;
110 ti->class_data = info->class_data;
111
112 ti->instance_init = info->instance_init;
113 ti->instance_finalize = info->instance_finalize;
114
115 ti->abstract = info->abstract;
116
33e95c63
AL
117 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
118 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
2f28d2ff 119 }
33e95c63 120 ti->num_interfaces = i;
2f28d2ff
AL
121
122 type_table_add(ti);
123
124 return ti;
125}
126
049cb3cf
PB
127TypeImpl *type_register(const TypeInfo *info)
128{
129 assert(info->parent);
130 return type_register_internal(info);
131}
132
2f28d2ff
AL
133TypeImpl *type_register_static(const TypeInfo *info)
134{
135 return type_register(info);
136}
137
138static TypeImpl *type_get_by_name(const char *name)
139{
140 if (name == NULL) {
141 return NULL;
142 }
143
144 return type_table_lookup(name);
145}
146
147static TypeImpl *type_get_parent(TypeImpl *type)
148{
149 if (!type->parent_type && type->parent) {
150 type->parent_type = type_get_by_name(type->parent);
151 g_assert(type->parent_type != NULL);
152 }
153
154 return type->parent_type;
155}
156
157static bool type_has_parent(TypeImpl *type)
158{
159 return (type->parent != NULL);
160}
161
162static size_t type_class_get_size(TypeImpl *ti)
163{
164 if (ti->class_size) {
165 return ti->class_size;
166 }
167
168 if (type_has_parent(ti)) {
169 return type_class_get_size(type_get_parent(ti));
170 }
171
172 return sizeof(ObjectClass);
173}
174
aca59af6
IM
175static size_t type_object_get_size(TypeImpl *ti)
176{
177 if (ti->instance_size) {
178 return ti->instance_size;
179 }
180
181 if (type_has_parent(ti)) {
182 return type_object_get_size(type_get_parent(ti));
183 }
184
185 return 0;
186}
187
33e95c63 188static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
2f28d2ff 189{
33e95c63
AL
190 assert(target_type);
191
192 /* Check if typename is a direct ancestor of type */
193 while (type) {
194 if (type == target_type) {
195 return true;
196 }
2f28d2ff 197
33e95c63
AL
198 type = type_get_parent(type);
199 }
200
201 return false;
202}
203
204static void type_initialize(TypeImpl *ti);
205
206static void type_initialize_interface(TypeImpl *ti, const char *parent)
207{
208 InterfaceClass *new_iface;
209 TypeInfo info = { };
210 TypeImpl *iface_impl;
211
212 info.parent = parent;
213 info.name = g_strdup_printf("%s::%s", ti->name, info.parent);
214 info.abstract = true;
215
216 iface_impl = type_register(&info);
217 type_initialize(iface_impl);
218 g_free((char *)info.name);
219
220 new_iface = (InterfaceClass *)iface_impl->class;
221 new_iface->concrete_class = ti->class;
222
223 ti->class->interfaces = g_slist_append(ti->class->interfaces,
224 iface_impl->class);
2f28d2ff
AL
225}
226
ac451033 227static void type_initialize(TypeImpl *ti)
2f28d2ff 228{
745549c8 229 TypeImpl *parent;
2f28d2ff
AL
230
231 if (ti->class) {
232 return;
233 }
234
235 ti->class_size = type_class_get_size(ti);
aca59af6 236 ti->instance_size = type_object_get_size(ti);
2f28d2ff
AL
237
238 ti->class = g_malloc0(ti->class_size);
2f28d2ff 239
745549c8
PB
240 parent = type_get_parent(ti);
241 if (parent) {
ac451033 242 type_initialize(parent);
33e95c63
AL
243 GSList *e;
244 int i;
2f28d2ff 245
2f28d2ff 246 g_assert(parent->class_size <= ti->class_size);
745549c8 247 memcpy(ti->class, parent->class, parent->class_size);
3e407de4 248 ti->class->interfaces = NULL;
33e95c63
AL
249
250 for (e = parent->class->interfaces; e; e = e->next) {
251 ObjectClass *iface = e->data;
252 type_initialize_interface(ti, object_class_get_name(iface));
253 }
254
255 for (i = 0; i < ti->num_interfaces; i++) {
256 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
257 for (e = ti->class->interfaces; e; e = e->next) {
258 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
259
260 if (type_is_ancestor(target_type, t)) {
261 break;
262 }
263 }
264
265 if (e) {
266 continue;
267 }
268
269 type_initialize_interface(ti, ti->interfaces[i].typename);
270 }
745549c8 271 }
2f28d2ff 272
745549c8 273 ti->class->type = ti;
3b50e311 274
745549c8
PB
275 while (parent) {
276 if (parent->class_base_init) {
277 parent->class_base_init(ti->class, ti->class_data);
3b50e311 278 }
745549c8 279 parent = type_get_parent(parent);
2f28d2ff
AL
280 }
281
2f28d2ff
AL
282 if (ti->class_init) {
283 ti->class_init(ti->class, ti->class_data);
284 }
2f28d2ff 285
2f28d2ff 286
2f28d2ff
AL
287}
288
289static void object_init_with_type(Object *obj, TypeImpl *ti)
290{
2f28d2ff
AL
291 if (type_has_parent(ti)) {
292 object_init_with_type(obj, type_get_parent(ti));
293 }
294
2f28d2ff
AL
295 if (ti->instance_init) {
296 ti->instance_init(obj);
297 }
298}
299
300void object_initialize_with_type(void *data, TypeImpl *type)
301{
302 Object *obj = data;
303
304 g_assert(type != NULL);
ac451033 305 type_initialize(type);
aca59af6
IM
306
307 g_assert(type->instance_size >= sizeof(Object));
2f28d2ff
AL
308 g_assert(type->abstract == false);
309
310 memset(obj, 0, type->instance_size);
311 obj->class = type->class;
764b6312 312 object_ref(obj);
57c9fafe 313 QTAILQ_INIT(&obj->properties);
2f28d2ff
AL
314 object_init_with_type(obj, type);
315}
316
317void object_initialize(void *data, const char *typename)
318{
319 TypeImpl *type = type_get_by_name(typename);
320
321 object_initialize_with_type(data, type);
322}
323
5d9d3f47
AF
324static inline bool object_property_is_child(ObjectProperty *prop)
325{
326 return strstart(prop->type, "child<", NULL);
327}
328
329static inline bool object_property_is_link(ObjectProperty *prop)
330{
331 return strstart(prop->type, "link<", NULL);
332}
333
57c9fafe
AL
334static void object_property_del_all(Object *obj)
335{
336 while (!QTAILQ_EMPTY(&obj->properties)) {
337 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
338
339 QTAILQ_REMOVE(&obj->properties, prop, node);
340
341 if (prop->release) {
342 prop->release(obj, prop->name, prop->opaque);
343 }
344
345 g_free(prop->name);
346 g_free(prop->type);
347 g_free(prop);
348 }
349}
350
351static void object_property_del_child(Object *obj, Object *child, Error **errp)
352{
353 ObjectProperty *prop;
354
355 QTAILQ_FOREACH(prop, &obj->properties, node) {
5d9d3f47 356 if (object_property_is_child(prop) && prop->opaque == child) {
57c9fafe 357 object_property_del(obj, prop->name, errp);
6c1fdcf9 358 break;
57c9fafe
AL
359 }
360 }
361}
362
363void object_unparent(Object *obj)
364{
e0a83fc2
PB
365 if (!obj->parent) {
366 return;
367 }
368
52e636cd 369 object_ref(obj);
667d22d1
PB
370 if (obj->class->unparent) {
371 (obj->class->unparent)(obj);
372 }
e998fa8d
MT
373 if (obj->parent) {
374 object_property_del_child(obj->parent, obj, NULL);
375 }
52e636cd 376 object_unref(obj);
57c9fafe
AL
377}
378
2f28d2ff
AL
379static void object_deinit(Object *obj, TypeImpl *type)
380{
381 if (type->instance_finalize) {
382 type->instance_finalize(obj);
383 }
384
2f28d2ff
AL
385 if (type_has_parent(type)) {
386 object_deinit(obj, type_get_parent(type));
387 }
388}
389
339c2708 390static void object_finalize(void *data)
2f28d2ff
AL
391{
392 Object *obj = data;
393 TypeImpl *ti = obj->class->type;
394
395 object_deinit(obj, ti);
57c9fafe 396 object_property_del_all(obj);
db85b575
AL
397
398 g_assert(obj->ref == 0);
fde9bf44
PB
399 if (obj->free) {
400 obj->free(obj);
401 }
2f28d2ff
AL
402}
403
404Object *object_new_with_type(Type type)
405{
406 Object *obj;
407
408 g_assert(type != NULL);
ac451033 409 type_initialize(type);
2f28d2ff
AL
410
411 obj = g_malloc(type->instance_size);
412 object_initialize_with_type(obj, type);
fde9bf44 413 obj->free = g_free;
2f28d2ff
AL
414
415 return obj;
416}
417
418Object *object_new(const char *typename)
419{
420 TypeImpl *ti = type_get_by_name(typename);
421
422 return object_new_with_type(ti);
423}
424
2f28d2ff
AL
425Object *object_dynamic_cast(Object *obj, const char *typename)
426{
b7f43fe4 427 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
acc4af3f
PB
428 return obj;
429 }
430
2f28d2ff
AL
431 return NULL;
432}
433
2f28d2ff
AL
434Object *object_dynamic_cast_assert(Object *obj, const char *typename)
435{
436 Object *inst;
437
438 inst = object_dynamic_cast(obj, typename);
439
b7f43fe4 440 if (!inst && obj) {
2f28d2ff
AL
441 fprintf(stderr, "Object %p is not an instance of type %s\n",
442 obj, typename);
443 abort();
444 }
445
446 return inst;
447}
448
449ObjectClass *object_class_dynamic_cast(ObjectClass *class,
450 const char *typename)
451{
33e95c63 452 ObjectClass *ret = NULL;
bf0fda34
PB
453 TypeImpl *target_type;
454 TypeImpl *type;
2f28d2ff 455
bf0fda34
PB
456 if (!class) {
457 return NULL;
458 }
459
793c96b5 460 /* A simple fast path that can trigger a lot for leaf classes. */
bf0fda34 461 type = class->type;
793c96b5
PB
462 if (type->name == typename) {
463 return class;
464 }
465
bf0fda34 466 target_type = type_get_by_name(typename);
9ab880b3
AG
467 if (!target_type) {
468 /* target class type unknown, so fail the cast */
469 return NULL;
470 }
471
00e2ceae
PC
472 if (type->class->interfaces &&
473 type_is_ancestor(target_type, type_interface)) {
33e95c63
AL
474 int found = 0;
475 GSList *i;
2f28d2ff 476
33e95c63
AL
477 for (i = class->interfaces; i; i = i->next) {
478 ObjectClass *target_class = i->data;
479
480 if (type_is_ancestor(target_class->type, target_type)) {
481 ret = target_class;
482 found++;
483 }
484 }
485
486 /* The match was ambiguous, don't allow a cast */
487 if (found > 1) {
488 ret = NULL;
489 }
490 } else if (type_is_ancestor(type, target_type)) {
491 ret = class;
2f28d2ff
AL
492 }
493
33e95c63 494 return ret;
2f28d2ff
AL
495}
496
497ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
498 const char *typename)
499{
500 ObjectClass *ret = object_class_dynamic_cast(class, typename);
501
bf0fda34 502 if (!ret && class) {
2f28d2ff
AL
503 fprintf(stderr, "Object %p is not an instance of type %s\n",
504 class, typename);
505 abort();
506 }
507
508 return ret;
509}
510
511const char *object_get_typename(Object *obj)
512{
513 return obj->class->type->name;
514}
515
516ObjectClass *object_get_class(Object *obj)
517{
518 return obj->class;
519}
520
17862378
AF
521bool object_class_is_abstract(ObjectClass *klass)
522{
523 return klass->type->abstract;
524}
525
2f28d2ff
AL
526const char *object_class_get_name(ObjectClass *klass)
527{
528 return klass->type->name;
529}
530
531ObjectClass *object_class_by_name(const char *typename)
532{
533 TypeImpl *type = type_get_by_name(typename);
534
535 if (!type) {
536 return NULL;
537 }
538
ac451033 539 type_initialize(type);
2f28d2ff
AL
540
541 return type->class;
542}
543
e7cce67f
PB
544ObjectClass *object_class_get_parent(ObjectClass *class)
545{
546 TypeImpl *type = type_get_parent(class->type);
547
548 if (!type) {
549 return NULL;
550 }
551
552 type_initialize(type);
553
554 return type->class;
555}
556
2f28d2ff
AL
557typedef struct OCFData
558{
559 void (*fn)(ObjectClass *klass, void *opaque);
93c511a1
AL
560 const char *implements_type;
561 bool include_abstract;
2f28d2ff
AL
562 void *opaque;
563} OCFData;
564
565static void object_class_foreach_tramp(gpointer key, gpointer value,
566 gpointer opaque)
567{
568 OCFData *data = opaque;
569 TypeImpl *type = value;
93c511a1 570 ObjectClass *k;
2f28d2ff 571
ac451033 572 type_initialize(type);
93c511a1 573 k = type->class;
2f28d2ff 574
93c511a1
AL
575 if (!data->include_abstract && type->abstract) {
576 return;
577 }
578
579 if (data->implements_type &&
580 !object_class_dynamic_cast(k, data->implements_type)) {
581 return;
582 }
583
584 data->fn(k, data->opaque);
2f28d2ff
AL
585}
586
587void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 588 const char *implements_type, bool include_abstract,
2f28d2ff
AL
589 void *opaque)
590{
93c511a1 591 OCFData data = { fn, implements_type, include_abstract, opaque };
2f28d2ff
AL
592
593 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
594}
57c9fafe 595
32efc535
PB
596int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
597 void *opaque)
598{
599 ObjectProperty *prop;
600 int ret = 0;
601
602 QTAILQ_FOREACH(prop, &obj->properties, node) {
603 if (object_property_is_child(prop)) {
604 ret = fn(prop->opaque, opaque);
605 if (ret != 0) {
606 break;
607 }
608 }
609 }
610 return ret;
611}
612
418ba9e5
AF
613static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
614{
615 GSList **list = opaque;
616
617 *list = g_slist_prepend(*list, klass);
618}
619
620GSList *object_class_get_list(const char *implements_type,
621 bool include_abstract)
622{
623 GSList *list = NULL;
624
625 object_class_foreach(object_class_get_list_tramp,
626 implements_type, include_abstract, &list);
627 return list;
628}
629
57c9fafe
AL
630void object_ref(Object *obj)
631{
632 obj->ref++;
633}
634
635void object_unref(Object *obj)
636{
637 g_assert(obj->ref > 0);
638 obj->ref--;
639
640 /* parent always holds a reference to its children */
641 if (obj->ref == 0) {
642 object_finalize(obj);
643 }
644}
645
646void object_property_add(Object *obj, const char *name, const char *type,
647 ObjectPropertyAccessor *get,
648 ObjectPropertyAccessor *set,
649 ObjectPropertyRelease *release,
650 void *opaque, Error **errp)
651{
54852b03
PM
652 ObjectProperty *prop;
653
654 QTAILQ_FOREACH(prop, &obj->properties, node) {
655 if (strcmp(prop->name, name) == 0) {
656 error_setg(errp, "attempt to add duplicate property '%s'"
657 " to object (type '%s')", name,
658 object_get_typename(obj));
659 return;
660 }
661 }
662
663 prop = g_malloc0(sizeof(*prop));
57c9fafe
AL
664
665 prop->name = g_strdup(name);
666 prop->type = g_strdup(type);
667
668 prop->get = get;
669 prop->set = set;
670 prop->release = release;
671 prop->opaque = opaque;
672
673 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
674}
675
89bfe000
PB
676ObjectProperty *object_property_find(Object *obj, const char *name,
677 Error **errp)
57c9fafe
AL
678{
679 ObjectProperty *prop;
680
681 QTAILQ_FOREACH(prop, &obj->properties, node) {
682 if (strcmp(prop->name, name) == 0) {
683 return prop;
684 }
685 }
686
89bfe000 687 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
57c9fafe
AL
688 return NULL;
689}
690
691void object_property_del(Object *obj, const char *name, Error **errp)
692{
89bfe000 693 ObjectProperty *prop = object_property_find(obj, name, errp);
0866aca1 694 if (prop == NULL) {
0866aca1
AL
695 return;
696 }
57c9fafe 697
0866aca1
AL
698 if (prop->release) {
699 prop->release(obj, name, prop->opaque);
700 }
701
702 QTAILQ_REMOVE(&obj->properties, prop, node);
57c9fafe
AL
703
704 g_free(prop->name);
705 g_free(prop->type);
706 g_free(prop);
707}
708
709void object_property_get(Object *obj, Visitor *v, const char *name,
710 Error **errp)
711{
89bfe000 712 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 713 if (prop == NULL) {
57c9fafe
AL
714 return;
715 }
716
717 if (!prop->get) {
718 error_set(errp, QERR_PERMISSION_DENIED);
719 } else {
720 prop->get(obj, v, prop->opaque, name, errp);
721 }
722}
723
724void object_property_set(Object *obj, Visitor *v, const char *name,
725 Error **errp)
726{
89bfe000 727 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 728 if (prop == NULL) {
57c9fafe
AL
729 return;
730 }
731
732 if (!prop->set) {
733 error_set(errp, QERR_PERMISSION_DENIED);
734 } else {
735 prop->set(obj, v, prop->opaque, name, errp);
736 }
737}
738
7b7b7d18
PB
739void object_property_set_str(Object *obj, const char *value,
740 const char *name, Error **errp)
741{
742 QString *qstr = qstring_from_str(value);
743 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
744
745 QDECREF(qstr);
746}
747
748char *object_property_get_str(Object *obj, const char *name,
749 Error **errp)
750{
751 QObject *ret = object_property_get_qobject(obj, name, errp);
752 QString *qstring;
753 char *retval;
754
755 if (!ret) {
756 return NULL;
757 }
758 qstring = qobject_to_qstring(ret);
759 if (!qstring) {
760 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
761 retval = NULL;
762 } else {
763 retval = g_strdup(qstring_get_str(qstring));
764 }
765
766 QDECREF(qstring);
767 return retval;
768}
769
1d9c5a12
PB
770void object_property_set_link(Object *obj, Object *value,
771 const char *name, Error **errp)
772{
773 object_property_set_str(obj, object_get_canonical_path(value),
774 name, errp);
775}
776
777Object *object_property_get_link(Object *obj, const char *name,
778 Error **errp)
779{
780 char *str = object_property_get_str(obj, name, errp);
781 Object *target = NULL;
782
783 if (str && *str) {
784 target = object_resolve_path(str, NULL);
785 if (!target) {
786 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
787 }
788 }
789
790 g_free(str);
791 return target;
792}
793
7b7b7d18
PB
794void object_property_set_bool(Object *obj, bool value,
795 const char *name, Error **errp)
796{
797 QBool *qbool = qbool_from_int(value);
798 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
799
800 QDECREF(qbool);
801}
802
803bool object_property_get_bool(Object *obj, const char *name,
804 Error **errp)
805{
806 QObject *ret = object_property_get_qobject(obj, name, errp);
807 QBool *qbool;
808 bool retval;
809
810 if (!ret) {
811 return false;
812 }
813 qbool = qobject_to_qbool(ret);
814 if (!qbool) {
815 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
816 retval = false;
817 } else {
818 retval = qbool_get_int(qbool);
819 }
820
821 QDECREF(qbool);
822 return retval;
823}
824
825void object_property_set_int(Object *obj, int64_t value,
826 const char *name, Error **errp)
827{
828 QInt *qint = qint_from_int(value);
829 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
830
831 QDECREF(qint);
832}
833
834int64_t object_property_get_int(Object *obj, const char *name,
835 Error **errp)
836{
837 QObject *ret = object_property_get_qobject(obj, name, errp);
838 QInt *qint;
839 int64_t retval;
840
841 if (!ret) {
842 return -1;
843 }
844 qint = qobject_to_qint(ret);
845 if (!qint) {
846 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
847 retval = -1;
848 } else {
849 retval = qint_get_int(qint);
850 }
851
852 QDECREF(qint);
853 return retval;
b2cd7dee
PB
854}
855
856void object_property_parse(Object *obj, const char *string,
857 const char *name, Error **errp)
858{
859 StringInputVisitor *mi;
860 mi = string_input_visitor_new(string);
861 object_property_set(obj, string_input_get_visitor(mi), name, errp);
862
863 string_input_visitor_cleanup(mi);
864}
865
866char *object_property_print(Object *obj, const char *name,
867 Error **errp)
868{
869 StringOutputVisitor *mo;
870 char *string;
871
872 mo = string_output_visitor_new();
8185bfc1 873 object_property_get(obj, string_output_get_visitor(mo), name, errp);
b2cd7dee
PB
874 string = string_output_get_string(mo);
875 string_output_visitor_cleanup(mo);
876 return string;
7b7b7d18
PB
877}
878
57c9fafe
AL
879const char *object_property_get_type(Object *obj, const char *name, Error **errp)
880{
89bfe000 881 ObjectProperty *prop = object_property_find(obj, name, errp);
57c9fafe 882 if (prop == NULL) {
57c9fafe
AL
883 return NULL;
884 }
885
886 return prop->type;
887}
888
889Object *object_get_root(void)
890{
8b45d447 891 static Object *root;
57c9fafe 892
8b45d447
AL
893 if (!root) {
894 root = object_new("container");
57c9fafe
AL
895 }
896
8b45d447 897 return root;
57c9fafe
AL
898}
899
900static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
901 const char *name, Error **errp)
902{
903 Object *child = opaque;
904 gchar *path;
905
906 path = object_get_canonical_path(child);
907 visit_type_str(v, &path, name, errp);
908 g_free(path);
909}
910
db85b575
AL
911static void object_finalize_child_property(Object *obj, const char *name,
912 void *opaque)
913{
914 Object *child = opaque;
915
916 object_unref(child);
917}
918
57c9fafe
AL
919void object_property_add_child(Object *obj, const char *name,
920 Object *child, Error **errp)
921{
922 gchar *type;
923
924 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
925
926 object_property_add(obj, name, type, object_get_child_property,
db85b575 927 NULL, object_finalize_child_property, child, errp);
57c9fafe
AL
928
929 object_ref(child);
930 g_assert(child->parent == NULL);
931 child->parent = obj;
932
933 g_free(type);
934}
935
936static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
937 const char *name, Error **errp)
938{
939 Object **child = opaque;
940 gchar *path;
941
942 if (*child) {
943 path = object_get_canonical_path(*child);
944 visit_type_str(v, &path, name, errp);
945 g_free(path);
946 } else {
947 path = (gchar *)"";
948 visit_type_str(v, &path, name, errp);
949 }
950}
951
952static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
953 const char *name, Error **errp)
954{
955 Object **child = opaque;
f0cdc966 956 Object *old_target;
57c9fafe
AL
957 bool ambiguous = false;
958 const char *type;
959 char *path;
11e35bfd 960 gchar *target_type;
57c9fafe
AL
961
962 type = object_property_get_type(obj, name, NULL);
963
964 visit_type_str(v, &path, name, errp);
965
f0cdc966
AB
966 old_target = *child;
967 *child = NULL;
57c9fafe
AL
968
969 if (strcmp(path, "") != 0) {
970 Object *target;
971
11e35bfd
PB
972 /* Go from link<FOO> to FOO. */
973 target_type = g_strndup(&type[5], strlen(type) - 6);
974 target = object_resolve_path_type(path, target_type, &ambiguous);
57c9fafe 975
11e35bfd
PB
976 if (ambiguous) {
977 error_set(errp, QERR_AMBIGUOUS_PATH, path);
978 } else if (target) {
979 object_ref(target);
980 *child = target;
57c9fafe 981 } else {
11e35bfd
PB
982 target = object_resolve_path(path, &ambiguous);
983 if (target || ambiguous) {
984 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
985 } else {
986 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
987 }
57c9fafe 988 }
11e35bfd 989 g_free(target_type);
57c9fafe
AL
990 }
991
992 g_free(path);
f0cdc966
AB
993
994 if (old_target != NULL) {
995 object_unref(old_target);
996 }
57c9fafe
AL
997}
998
999void object_property_add_link(Object *obj, const char *name,
1000 const char *type, Object **child,
1001 Error **errp)
1002{
1003 gchar *full_type;
1004
1005 full_type = g_strdup_printf("link<%s>", type);
1006
1007 object_property_add(obj, name, full_type,
1008 object_get_link_property,
1009 object_set_link_property,
1010 NULL, child, errp);
1011
1012 g_free(full_type);
1013}
1014
1015gchar *object_get_canonical_path(Object *obj)
1016{
1017 Object *root = object_get_root();
1018 char *newpath = NULL, *path = NULL;
1019
1020 while (obj != root) {
1021 ObjectProperty *prop = NULL;
1022
1023 g_assert(obj->parent != NULL);
1024
1025 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
5d9d3f47 1026 if (!object_property_is_child(prop)) {
57c9fafe
AL
1027 continue;
1028 }
1029
1030 if (prop->opaque == obj) {
1031 if (path) {
1032 newpath = g_strdup_printf("%s/%s", prop->name, path);
1033 g_free(path);
1034 path = newpath;
1035 } else {
1036 path = g_strdup(prop->name);
1037 }
1038 break;
1039 }
1040 }
1041
1042 g_assert(prop != NULL);
1043
1044 obj = obj->parent;
1045 }
1046
1047 newpath = g_strdup_printf("/%s", path);
1048 g_free(path);
1049
1050 return newpath;
1051}
1052
3e84b483 1053Object *object_resolve_path_component(Object *parent, const gchar *part)
a612b2a6 1054{
89bfe000 1055 ObjectProperty *prop = object_property_find(parent, part, NULL);
a612b2a6
PB
1056 if (prop == NULL) {
1057 return NULL;
1058 }
1059
5d9d3f47 1060 if (object_property_is_link(prop)) {
a612b2a6 1061 return *(Object **)prop->opaque;
5d9d3f47 1062 } else if (object_property_is_child(prop)) {
a612b2a6
PB
1063 return prop->opaque;
1064 } else {
1065 return NULL;
1066 }
1067}
1068
57c9fafe
AL
1069static Object *object_resolve_abs_path(Object *parent,
1070 gchar **parts,
02fe2db6 1071 const char *typename,
57c9fafe
AL
1072 int index)
1073{
57c9fafe
AL
1074 Object *child;
1075
1076 if (parts[index] == NULL) {
02fe2db6 1077 return object_dynamic_cast(parent, typename);
57c9fafe
AL
1078 }
1079
1080 if (strcmp(parts[index], "") == 0) {
02fe2db6 1081 return object_resolve_abs_path(parent, parts, typename, index + 1);
57c9fafe
AL
1082 }
1083
a612b2a6 1084 child = object_resolve_path_component(parent, parts[index]);
57c9fafe
AL
1085 if (!child) {
1086 return NULL;
1087 }
1088
02fe2db6 1089 return object_resolve_abs_path(child, parts, typename, index + 1);
57c9fafe
AL
1090}
1091
1092static Object *object_resolve_partial_path(Object *parent,
1093 gchar **parts,
02fe2db6 1094 const char *typename,
57c9fafe
AL
1095 bool *ambiguous)
1096{
1097 Object *obj;
1098 ObjectProperty *prop;
1099
02fe2db6 1100 obj = object_resolve_abs_path(parent, parts, typename, 0);
57c9fafe
AL
1101
1102 QTAILQ_FOREACH(prop, &parent->properties, node) {
1103 Object *found;
1104
5d9d3f47 1105 if (!object_property_is_child(prop)) {
57c9fafe
AL
1106 continue;
1107 }
1108
02fe2db6
PB
1109 found = object_resolve_partial_path(prop->opaque, parts,
1110 typename, ambiguous);
57c9fafe
AL
1111 if (found) {
1112 if (obj) {
1113 if (ambiguous) {
1114 *ambiguous = true;
1115 }
1116 return NULL;
1117 }
1118 obj = found;
1119 }
1120
1121 if (ambiguous && *ambiguous) {
1122 return NULL;
1123 }
1124 }
1125
1126 return obj;
1127}
1128
02fe2db6
PB
1129Object *object_resolve_path_type(const char *path, const char *typename,
1130 bool *ambiguous)
57c9fafe 1131{
57c9fafe
AL
1132 Object *obj;
1133 gchar **parts;
1134
1135 parts = g_strsplit(path, "/", 0);
2e1103f6 1136 assert(parts);
57c9fafe 1137
2e1103f6 1138 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
57c9fafe
AL
1139 if (ambiguous) {
1140 *ambiguous = false;
1141 }
02fe2db6
PB
1142 obj = object_resolve_partial_path(object_get_root(), parts,
1143 typename, ambiguous);
57c9fafe 1144 } else {
02fe2db6 1145 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
57c9fafe
AL
1146 }
1147
1148 g_strfreev(parts);
1149
1150 return obj;
1151}
1152
02fe2db6
PB
1153Object *object_resolve_path(const char *path, bool *ambiguous)
1154{
1155 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1156}
1157
57c9fafe
AL
1158typedef struct StringProperty
1159{
1160 char *(*get)(Object *, Error **);
1161 void (*set)(Object *, const char *, Error **);
1162} StringProperty;
1163
7b7b7d18
PB
1164static void property_get_str(Object *obj, Visitor *v, void *opaque,
1165 const char *name, Error **errp)
57c9fafe
AL
1166{
1167 StringProperty *prop = opaque;
1168 char *value;
1169
1170 value = prop->get(obj, errp);
1171 if (value) {
1172 visit_type_str(v, &value, name, errp);
1173 g_free(value);
1174 }
1175}
1176
7b7b7d18
PB
1177static void property_set_str(Object *obj, Visitor *v, void *opaque,
1178 const char *name, Error **errp)
57c9fafe
AL
1179{
1180 StringProperty *prop = opaque;
1181 char *value;
1182 Error *local_err = NULL;
1183
1184 visit_type_str(v, &value, name, &local_err);
1185 if (local_err) {
1186 error_propagate(errp, local_err);
1187 return;
1188 }
1189
1190 prop->set(obj, value, errp);
1191 g_free(value);
1192}
1193
7b7b7d18
PB
1194static void property_release_str(Object *obj, const char *name,
1195 void *opaque)
57c9fafe
AL
1196{
1197 StringProperty *prop = opaque;
1198 g_free(prop);
1199}
1200
1201void object_property_add_str(Object *obj, const char *name,
1202 char *(*get)(Object *, Error **),
1203 void (*set)(Object *, const char *, Error **),
1204 Error **errp)
1205{
1206 StringProperty *prop = g_malloc0(sizeof(*prop));
1207
1208 prop->get = get;
1209 prop->set = set;
1210
1211 object_property_add(obj, name, "string",
7b7b7d18
PB
1212 get ? property_get_str : NULL,
1213 set ? property_set_str : NULL,
1214 property_release_str,
57c9fafe
AL
1215 prop, errp);
1216}
745549c8 1217
0e558843
AL
1218typedef struct BoolProperty
1219{
1220 bool (*get)(Object *, Error **);
1221 void (*set)(Object *, bool, Error **);
1222} BoolProperty;
1223
1224static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1225 const char *name, Error **errp)
1226{
1227 BoolProperty *prop = opaque;
1228 bool value;
1229
1230 value = prop->get(obj, errp);
1231 visit_type_bool(v, &value, name, errp);
1232}
1233
1234static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1235 const char *name, Error **errp)
1236{
1237 BoolProperty *prop = opaque;
1238 bool value;
1239 Error *local_err = NULL;
1240
1241 visit_type_bool(v, &value, name, &local_err);
1242 if (local_err) {
1243 error_propagate(errp, local_err);
1244 return;
1245 }
1246
1247 prop->set(obj, value, errp);
1248}
1249
1250static void property_release_bool(Object *obj, const char *name,
1251 void *opaque)
1252{
1253 BoolProperty *prop = opaque;
1254 g_free(prop);
1255}
1256
1257void object_property_add_bool(Object *obj, const char *name,
1258 bool (*get)(Object *, Error **),
1259 void (*set)(Object *, bool, Error **),
1260 Error **errp)
1261{
1262 BoolProperty *prop = g_malloc0(sizeof(*prop));
1263
1264 prop->get = get;
1265 prop->set = set;
1266
1267 object_property_add(obj, name, "bool",
1268 get ? property_get_bool : NULL,
1269 set ? property_set_bool : NULL,
1270 property_release_bool,
1271 prop, errp);
1272}
1273
2f262e06
PB
1274static char *qdev_get_type(Object *obj, Error **errp)
1275{
1276 return g_strdup(object_get_typename(obj));
1277}
1278
1279static void object_instance_init(Object *obj)
1280{
1281 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1282}
1283
745549c8
PB
1284static void register_types(void)
1285{
1286 static TypeInfo interface_info = {
1287 .name = TYPE_INTERFACE,
33e95c63 1288 .class_size = sizeof(InterfaceClass),
745549c8
PB
1289 .abstract = true,
1290 };
1291
1292 static TypeInfo object_info = {
1293 .name = TYPE_OBJECT,
1294 .instance_size = sizeof(Object),
2f262e06 1295 .instance_init = object_instance_init,
745549c8
PB
1296 .abstract = true,
1297 };
1298
049cb3cf
PB
1299 type_interface = type_register_internal(&interface_info);
1300 type_register_internal(&object_info);
745549c8
PB
1301}
1302
1303type_init(register_types)
This page took 0.382282 seconds and 4 git commands to generate.