hw/arm/musca: Wire up PL031 RTC
[qemu/ar7.git] / qom / object_interfaces.c
blobdb85d1eb75a05e39425820049a87800b0cbbe950
1 #include "qemu/osdep.h"
2 #include "qapi/error.h"
3 #include "qapi/qmp/qdict.h"
4 #include "qapi/qmp/qerror.h"
5 #include "qom/object_interfaces.h"
6 #include "qemu/module.h"
7 #include "qemu/option.h"
8 #include "qapi/opts-visitor.h"
9 #include "qemu/config-file.h"
11 void user_creatable_complete(UserCreatable *uc, Error **errp)
13 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
15 if (ucc->complete) {
16 ucc->complete(uc, errp);
20 bool user_creatable_can_be_deleted(UserCreatable *uc)
23 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
25 if (ucc->can_be_deleted) {
26 return ucc->can_be_deleted(uc);
27 } else {
28 return true;
32 Object *user_creatable_add_type(const char *type, const char *id,
33 const QDict *qdict,
34 Visitor *v, Error **errp)
36 Object *obj;
37 ObjectClass *klass;
38 const QDictEntry *e;
39 Error *local_err = NULL;
41 klass = object_class_by_name(type);
42 if (!klass) {
43 error_setg(errp, "invalid object type: %s", type);
44 return NULL;
47 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
48 error_setg(errp, "object type '%s' isn't supported by object-add",
49 type);
50 return NULL;
53 if (object_class_is_abstract(klass)) {
54 error_setg(errp, "object type '%s' is abstract", type);
55 return NULL;
58 assert(qdict);
59 obj = object_new(type);
60 visit_start_struct(v, NULL, NULL, 0, &local_err);
61 if (local_err) {
62 goto out;
64 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
65 object_property_set(obj, v, e->key, &local_err);
66 if (local_err) {
67 break;
70 if (!local_err) {
71 visit_check_struct(v, &local_err);
73 visit_end_struct(v, NULL);
74 if (local_err) {
75 goto out;
78 object_property_add_child(object_get_objects_root(),
79 id, obj, &local_err);
80 if (local_err) {
81 goto out;
84 user_creatable_complete(USER_CREATABLE(obj), &local_err);
85 if (local_err) {
86 object_property_del(object_get_objects_root(),
87 id, &error_abort);
88 goto out;
90 out:
91 if (local_err) {
92 error_propagate(errp, local_err);
93 object_unref(obj);
94 return NULL;
96 return obj;
100 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
102 Visitor *v;
103 QDict *pdict;
104 Object *obj;
105 const char *id = qemu_opts_id(opts);
106 char *type = qemu_opt_get_del(opts, "qom-type");
108 if (!type) {
109 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
110 return NULL;
112 if (!id) {
113 error_setg(errp, QERR_MISSING_PARAMETER, "id");
114 qemu_opt_set(opts, "qom-type", type, &error_abort);
115 g_free(type);
116 return NULL;
119 qemu_opts_set_id(opts, NULL);
120 pdict = qemu_opts_to_qdict(opts, NULL);
122 v = opts_visitor_new(opts);
123 obj = user_creatable_add_type(type, id, pdict, v, errp);
124 visit_free(v);
126 qemu_opts_set_id(opts, (char *) id);
127 qemu_opt_set(opts, "qom-type", type, &error_abort);
128 g_free(type);
129 qobject_unref(pdict);
130 return obj;
134 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
136 bool (*type_opt_predicate)(const char *, QemuOpts *) = opaque;
137 Object *obj = NULL;
138 const char *type;
140 type = qemu_opt_get(opts, "qom-type");
141 if (type && type_opt_predicate &&
142 !type_opt_predicate(type, opts)) {
143 return 0;
146 obj = user_creatable_add_opts(opts, errp);
147 if (!obj) {
148 return -1;
150 object_unref(obj);
151 return 0;
155 void user_creatable_del(const char *id, Error **errp)
157 Object *container;
158 Object *obj;
160 container = object_get_objects_root();
161 obj = object_resolve_path_component(container, id);
162 if (!obj) {
163 error_setg(errp, "object '%s' not found", id);
164 return;
167 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
168 error_setg(errp, "object '%s' is in use, can not be deleted", id);
169 return;
173 * if object was defined on the command-line, remove its corresponding
174 * option group entry
176 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort),
177 id));
179 object_unparent(obj);
182 void user_creatable_cleanup(void)
184 object_unparent(object_get_objects_root());
187 static void register_types(void)
189 static const TypeInfo uc_interface_info = {
190 .name = TYPE_USER_CREATABLE,
191 .parent = TYPE_INTERFACE,
192 .class_size = sizeof(UserCreatableClass),
195 type_register_static(&uc_interface_info);
198 type_init(register_types)