libqos: add MSI-X callbacks to QVirtioPCIDevice
[qemu/ar7.git] / qom / object_interfaces.c
blob46cd6eab5c6b61b783760d189d239b68d958ce62
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 "qom/object_interfaces.h"
8 #include "qemu/help_option.h"
9 #include "qemu/module.h"
10 #include "qemu/option.h"
11 #include "qapi/opts-visitor.h"
12 #include "qemu/config-file.h"
14 void user_creatable_complete(UserCreatable *uc, Error **errp)
16 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
18 if (ucc->complete) {
19 ucc->complete(uc, errp);
23 bool user_creatable_can_be_deleted(UserCreatable *uc)
26 UserCreatableClass *ucc = USER_CREATABLE_GET_CLASS(uc);
28 if (ucc->can_be_deleted) {
29 return ucc->can_be_deleted(uc);
30 } else {
31 return true;
35 Object *user_creatable_add_type(const char *type, const char *id,
36 const QDict *qdict,
37 Visitor *v, Error **errp)
39 Object *obj;
40 ObjectClass *klass;
41 const QDictEntry *e;
42 Error *local_err = NULL;
44 klass = object_class_by_name(type);
45 if (!klass) {
46 error_setg(errp, "invalid object type: %s", type);
47 return NULL;
50 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
51 error_setg(errp, "object type '%s' isn't supported by object-add",
52 type);
53 return NULL;
56 if (object_class_is_abstract(klass)) {
57 error_setg(errp, "object type '%s' is abstract", type);
58 return NULL;
61 assert(qdict);
62 obj = object_new(type);
63 visit_start_struct(v, NULL, NULL, 0, &local_err);
64 if (local_err) {
65 goto out;
67 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
68 object_property_set(obj, v, e->key, &local_err);
69 if (local_err) {
70 break;
73 if (!local_err) {
74 visit_check_struct(v, &local_err);
76 visit_end_struct(v, NULL);
77 if (local_err) {
78 goto out;
81 if (id != NULL) {
82 object_property_add_child(object_get_objects_root(),
83 id, obj, &local_err);
84 if (local_err) {
85 goto out;
89 user_creatable_complete(USER_CREATABLE(obj), &local_err);
90 if (local_err) {
91 if (id != NULL) {
92 object_property_del(object_get_objects_root(),
93 id, &error_abort);
95 goto out;
97 out:
98 if (local_err) {
99 error_propagate(errp, local_err);
100 object_unref(obj);
101 return NULL;
103 return obj;
107 Object *user_creatable_add_opts(QemuOpts *opts, Error **errp)
109 Visitor *v;
110 QDict *pdict;
111 Object *obj;
112 const char *id = qemu_opts_id(opts);
113 char *type = qemu_opt_get_del(opts, "qom-type");
115 if (!type) {
116 error_setg(errp, QERR_MISSING_PARAMETER, "qom-type");
117 return NULL;
119 if (!id) {
120 error_setg(errp, QERR_MISSING_PARAMETER, "id");
121 qemu_opt_set(opts, "qom-type", type, &error_abort);
122 g_free(type);
123 return NULL;
126 qemu_opts_set_id(opts, NULL);
127 pdict = qemu_opts_to_qdict(opts, NULL);
129 v = opts_visitor_new(opts);
130 obj = user_creatable_add_type(type, id, pdict, v, errp);
131 visit_free(v);
133 qemu_opts_set_id(opts, (char *) id);
134 qemu_opt_set(opts, "qom-type", type, &error_abort);
135 g_free(type);
136 qobject_unref(pdict);
137 return obj;
141 int user_creatable_add_opts_foreach(void *opaque, QemuOpts *opts, Error **errp)
143 bool (*type_opt_predicate)(const char *, QemuOpts *) = opaque;
144 Object *obj = NULL;
145 const char *type;
147 type = qemu_opt_get(opts, "qom-type");
148 if (type && type_opt_predicate &&
149 !type_opt_predicate(type, opts)) {
150 return 0;
153 obj = user_creatable_add_opts(opts, errp);
154 if (!obj) {
155 return -1;
157 object_unref(obj);
158 return 0;
161 bool user_creatable_print_help(const char *type, QemuOpts *opts)
163 ObjectClass *klass;
165 if (is_help_option(type)) {
166 GSList *l, *list;
168 printf("List of user creatable objects:\n");
169 list = object_class_get_list_sorted(TYPE_USER_CREATABLE, false);
170 for (l = list; l != NULL; l = l->next) {
171 ObjectClass *oc = OBJECT_CLASS(l->data);
172 printf(" %s\n", object_class_get_name(oc));
174 g_slist_free(list);
175 return true;
178 klass = object_class_by_name(type);
179 if (klass && qemu_opt_has_help_opt(opts)) {
180 ObjectPropertyIterator iter;
181 ObjectProperty *prop;
182 GPtrArray *array = g_ptr_array_new();
183 int i;
185 object_class_property_iter_init(&iter, klass);
186 while ((prop = object_property_iter_next(&iter))) {
187 GString *str;
189 if (!prop->set) {
190 continue;
193 str = g_string_new(NULL);
194 g_string_append_printf(str, " %s=<%s>", prop->name, prop->type);
195 if (prop->description) {
196 if (str->len < 24) {
197 g_string_append_printf(str, "%*s", 24 - (int)str->len, "");
199 g_string_append_printf(str, " - %s", prop->description);
201 g_ptr_array_add(array, g_string_free(str, false));
203 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
204 if (array->len > 0) {
205 printf("%s options:\n", type);
206 } else {
207 printf("There are no options for %s.\n", type);
209 for (i = 0; i < array->len; i++) {
210 printf("%s\n", (char *)array->pdata[i]);
212 g_ptr_array_set_free_func(array, g_free);
213 g_ptr_array_free(array, true);
214 return true;
217 return false;
220 void user_creatable_del(const char *id, Error **errp)
222 Object *container;
223 Object *obj;
225 container = object_get_objects_root();
226 obj = object_resolve_path_component(container, id);
227 if (!obj) {
228 error_setg(errp, "object '%s' not found", id);
229 return;
232 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj))) {
233 error_setg(errp, "object '%s' is in use, can not be deleted", id);
234 return;
238 * if object was defined on the command-line, remove its corresponding
239 * option group entry
241 qemu_opts_del(qemu_opts_find(qemu_find_opts_err("object", &error_abort),
242 id));
244 object_unparent(obj);
247 void user_creatable_cleanup(void)
249 object_unparent(object_get_objects_root());
252 static void register_types(void)
254 static const TypeInfo uc_interface_info = {
255 .name = TYPE_USER_CREATABLE,
256 .parent = TYPE_INTERFACE,
257 .class_size = sizeof(UserCreatableClass),
260 type_register_static(&uc_interface_info);
263 type_init(register_types)