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