Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' into staging
[qemu/ar7.git] / qom / object_interfaces.c
blob2f76e1f36d55631f68fb0d4b9c7ecf16dd685b1c
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(Object *obj, Error **errp)
14 UserCreatableClass *ucc;
15 UserCreatable *uc =
16 (UserCreatable *)object_dynamic_cast(obj, TYPE_USER_CREATABLE);
18 if (!uc) {
19 return;
22 ucc = USER_CREATABLE_GET_CLASS(uc);
23 if (ucc->complete) {
24 ucc->complete(uc, errp);
28 bool user_creatable_can_be_deleted(UserCreatable *uc)
31 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
33 if (ucc->can_be_deleted) {
34 return ucc->can_be_deleted(uc);
35 } else {
36 return true;
40 Object *user_creatable_add_type(const char *type, const char *id,
41 const QDict *qdict,
42 Visitor *v, Error **errp)
44 Object *obj;
45 ObjectClass *klass;
46 const QDictEntry *e;
47 Error *local_err = NULL;
49 klass = object_class_by_name(type);
50 if (!klass) {
51 error_setg(errp, "invalid object type: %s", type);
52 return NULL;
55 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
56 error_setg(errp, "object type '%s' isn't supported by object-add",
57 type);
58 return NULL;
61 if (object_class_is_abstract(klass)) {
62 error_setg(errp, "object type '%s' is abstract", type);
63 return NULL;
66 assert(qdict);
67 obj = object_new(type);
68 if (object_property_find(obj, "id", NULL)) {
69 object_property_set_str(obj, id, "id", &local_err);
70 if (local_err) {
71 goto out;
74 visit_start_struct(v, NULL, NULL, 0, &local_err);
75 if (local_err) {
76 goto out;
78 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
79 object_property_set(obj, v, e->key, &local_err);
80 if (local_err) {
81 break;
84 if (!local_err) {
85 visit_check_struct(v, &local_err);
87 visit_end_struct(v, NULL);
88 if (local_err) {
89 goto out;
92 object_property_add_child(object_get_objects_root(),
93 id, obj, &local_err);
94 if (local_err) {
95 goto out;
98 user_creatable_complete(obj, &local_err);
99 if (local_err) {
100 object_property_del(object_get_objects_root(),
101 id, &error_abort);
102 goto out;
104 out:
105 if (local_err) {
106 error_propagate(errp, local_err);
107 object_unref(obj);
108 return NULL;
110 return obj;
114 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
116 Visitor *v;
117 QDict *pdict;
118 Object *obj;
119 const char *id = qemu_opts_id(opts);
120 char *type = qemu_opt_get_del(opts, "qom-type");
122 if (!type) {
123 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
124 return NULL;
126 if (!id) {
127 error_setg(errp, QERR_MISSING_PARAMETER, "id");
128 qemu_opt_set(opts, "qom-type", type, &error_abort);
129 g_free(type);
130 return NULL;
133 qemu_opts_set_id(opts, NULL);
134 pdict = qemu_opts_to_qdict(opts, NULL);
136 v = opts_visitor_new(opts);
137 obj = user_creatable_add_type(type, id, pdict, v, errp);
138 visit_free(v);
140 qemu_opts_set_id(opts, (char *) id);
141 qemu_opt_set(opts, "qom-type", type, &error_abort);
142 g_free(type);
143 QDECREF(pdict);
144 return obj;
148 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
150 bool (*type_predicate)(const char *) = opaque;
151 Object *obj = NULL;
152 Error *err = NULL;
153 const char *type;
155 type = qemu_opt_get(opts, "qom-type");
156 if (type && type_predicate &&
157 !type_predicate(type)) {
158 return 0;
161 obj = user_creatable_add_opts(opts, &err);
162 if (!obj) {
163 error_report_err(err);
164 return -1;
166 object_unref(obj);
167 return 0;
171 void user_creatable_del(const char *id, Error **errp)
173 Object *container;
174 Object *obj;
176 container = object_get_objects_root();
177 obj = object_resolve_path_component(container, id);
178 if (!obj) {
179 error_setg(errp, "object '%s' not found", id);
180 return;
183 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
184 error_setg(errp, "object '%s' is in use, can not be deleted", id);
185 return;
189 * if object was defined on the command-line, remove its corresponding
190 * option group entry
192 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort),
193 id));
195 object_unparent(obj);
198 void user_creatable_cleanup(void)
200 object_unparent(object_get_objects_root());
203 static void register_types(void)
205 static const TypeInfo uc_interface_info = {
206 .name = TYPE_USER_CREATABLE,
207 .parent = TYPE_INTERFACE,
208 .class_size = sizeof(UserCreatableClass),
211 type_register_static(&uc_interface_info);
214 type_init(register_types)