block/block-copy: rename in-flight requests to tasks
[qemu/ar7.git] / qom / object_interfaces.c
blobbc36f96e47883c01dcb8e198139c43f525493735
1 #include "qemu/osdep.h"
3 #include "qemu/cutils.h"
4 #include "qapi/error.h"
5 #include "qapi/qmp/qdict.h"
6 #include "qapi/qmp/qerror.h"
7 #include "qapi/qmp/qjson.h"
8 #include "qapi/qmp/qstring.h"
9 #include "qapi/qobject-input-visitor.h"
10 #include "qom/object_interfaces.h"
11 #include "qemu/help_option.h"
12 #include "qemu/module.h"
13 #include "qemu/option.h"
14 #include "qapi/opts-visitor.h"
15 #include "qemu/config-file.h"
17 void user_creatable_complete(UserCreatable *uc, Error **errp)
19 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
21 if (ucc->complete) {
22 ucc->complete(uc, errp);
26 bool user_creatable_can_be_deleted(UserCreatable *uc)
29 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
31 if (ucc->can_be_deleted) {
32 return ucc->can_be_deleted(uc);
33 } else {
34 return true;
38 Object *user_creatable_add_type(const char *type, const char *id,
39 const QDict *qdict,
40 Visitor *v, Error **errp)
42 Object *obj;
43 ObjectClass *klass;
44 const QDictEntry *e;
45 Error *local_err = NULL;
47 klass = object_class_by_name(type);
48 if (!klass) {
49 error_setg(errp, "invalid object type: %s", type);
50 return NULL;
53 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
54 error_setg(errp, "object type '%s' isn't supported by object-add",
55 type);
56 return NULL;
59 if (object_class_is_abstract(klass)) {
60 error_setg(errp, "object type '%s' is abstract", type);
61 return NULL;
64 assert(qdict);
65 obj = object_new(type);
66 visit_start_struct(v, NULL, NULL, 0, &local_err);
67 if (local_err) {
68 goto out;
70 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
71 object_property_set(obj, v, e->key, &local_err);
72 if (local_err) {
73 break;
76 if (!local_err) {
77 visit_check_struct(v, &local_err);
79 visit_end_struct(v, NULL);
80 if (local_err) {
81 goto out;
84 if (id != NULL) {
85 object_property_add_child(object_get_objects_root(),
86 id, obj, &local_err);
87 if (local_err) {
88 goto out;
92 user_creatable_complete(USER_CREATABLE(obj), &local_err);
93 if (local_err) {
94 if (id != NULL) {
95 object_property_del(object_get_objects_root(),
96 id, &error_abort);
98 goto out;
100 out:
101 if (local_err) {
102 error_propagate(errp, local_err);
103 object_unref(obj);
104 return NULL;
106 return obj;
109 void user_creatable_add_dict(QDict *qdict, bool keyval, Error **errp)
111 Visitor *v;
112 Object *obj;
113 g_autofree char *type = NULL;
114 g_autofree char *id = NULL;
116 type = g_strdup(qdict_get_try_str(qdict, "qom-type"));
117 if (!type) {
118 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
119 return;
121 qdict_del(qdict, "qom-type");
123 id = g_strdup(qdict_get_try_str(qdict, "id"));
124 if (!id) {
125 error_setg(errp, QERR_MISSING_PARAMETER, "id");
126 return;
128 qdict_del(qdict, "id");
130 if (keyval) {
131 v = qobject_input_visitor_new_keyval(QOBJECT(qdict));
132 } else {
133 v = qobject_input_visitor_new(QOBJECT(qdict));
135 obj = user_creatable_add_type(type, id, qdict, v, errp);
136 visit_free(v);
137 object_unref(obj);
140 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
142 Visitor *v;
143 QDict *pdict;
144 Object *obj;
145 const char *id = qemu_opts_id(opts);
146 char *type = qemu_opt_get_del(opts, "qom-type");
148 if (!type) {
149 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
150 return NULL;
152 if (!id) {
153 error_setg(errp, QERR_MISSING_PARAMETER, "id");
154 qemu_opt_set(opts, "qom-type", type, &error_abort);
155 g_free(type);
156 return NULL;
159 qemu_opts_set_id(opts, NULL);
160 pdict = qemu_opts_to_qdict(opts, NULL);
162 v = opts_visitor_new(opts);
163 obj = user_creatable_add_type(type, id, pdict, v, errp);
164 visit_free(v);
166 qemu_opts_set_id(opts, (char *) id);
167 qemu_opt_set(opts, "qom-type", type, &error_abort);
168 g_free(type);
169 qobject_unref(pdict);
170 return obj;
174 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
176 bool (*type_opt_predicate)(const char *, QemuOpts *) = opaque;
177 Object *obj = NULL;
178 const char *type;
180 type = qemu_opt_get(opts, "qom-type");
181 if (type && type_opt_predicate &&
182 !type_opt_predicate(type, opts)) {
183 return 0;
186 obj = user_creatable_add_opts(opts, errp);
187 if (!obj) {
188 return -1;
190 object_unref(obj);
191 return 0;
194 char *object_property_help(const char *name, const char *type,
195 QObject *defval, const char *description)
197 GString *str = g_string_new(NULL);
199 g_string_append_printf(str, " %s=<%s>", name, type);
200 if (description || defval) {
201 if (str->len < 24) {
202 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
204 g_string_append(str, " - ");
206 if (description) {
207 g_string_append(str, description);
209 if (defval) {
210 g_autofree char *def_json = qstring_free(qobject_to_json(defval), TRUE);
211 g_string_append_printf(str, " (default: %s)", def_json);
214 return g_string_free(str, false);
217 bool user_creatable_print_help(const char *type, QemuOpts *opts)
219 ObjectClass *klass;
221 if (is_help_option(type)) {
222 GSList *l, *list;
224 printf("List of user creatable objects:\n");
225 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
226 for (l = list; l != NULL; l = l->next) {
227 ObjectClass *oc = OBJECT_CLASS(l->data);
228 printf(" %s\n", object_class_get_name(oc));
230 g_slist_free(list);
231 return true;
234 klass = object_class_by_name(type);
235 if (klass && qemu_opt_has_help_opt(opts)) {
236 ObjectPropertyIterator iter;
237 ObjectProperty *prop;
238 GPtrArray *array = g_ptr_array_new();
239 int i;
241 object_class_property_iter_init(&iter, klass);
242 while ((prop = object_property_iter_next(&iter))) {
243 if (!prop->set) {
244 continue;
247 g_ptr_array_add(array,
248 object_property_help(prop->name, prop->type,
249 prop->defval, prop->description));
251 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
252 if (array->len > 0) {
253 printf("%s options:\n", type);
254 } else {
255 printf("There are no options for %s.\n", type);
257 for (i = 0; i < array->len; i++) {
258 printf("%s\n", (char *)array->pdata[i]);
260 g_ptr_array_set_free_func(array, g_free);
261 g_ptr_array_free(array, true);
262 return true;
265 return false;
268 void user_creatable_del(const char *id, Error **errp)
270 Object *container;
271 Object *obj;
273 container = object_get_objects_root();
274 obj = object_resolve_path_component(container, id);
275 if (!obj) {
276 error_setg(errp, "object '%s' not found", id);
277 return;
280 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
281 error_setg(errp, "object '%s' is in use, can not be deleted", id);
282 return;
286 * if object was defined on the command-line, remove its corresponding
287 * option group entry
289 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort),
290 id));
292 object_unparent(obj);
295 void user_creatable_cleanup(void)
297 object_unparent(object_get_objects_root());
300 static void register_types(void)
302 static const TypeInfo uc_interface_info = {
303 .name = TYPE_USER_CREATABLE,
304 .parent = TYPE_INTERFACE,
305 .class_size = sizeof(UserCreatableClass),
308 type_register_static(&uc_interface_info);
311 type_init(register_types)