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