w64: Fix data types in softmmu*.h
[qemu/rayw.git] / qapi / qmp-input-visitor.c
blob74386b9b1b80ff8ad0acd790f669cc84239698fd
1 /*
2 * Input Visitor
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qmp-input-visitor.h"
15 #include "qapi/qapi-visit-impl.h"
16 #include "qemu-queue.h"
17 #include "qemu-common.h"
18 #include "qemu-objects.h"
19 #include "qerror.h"
21 #define QIV_STACK_SIZE 1024
23 typedef struct StackObject
25 QObject *obj;
26 const QListEntry *entry;
27 GHashTable *h;
28 } StackObject;
30 struct QmpInputVisitor
32 Visitor visitor;
33 StackObject stack[QIV_STACK_SIZE];
34 int nb_stack;
35 bool strict;
38 static QmpInputVisitor *to_qiv(Visitor *v)
40 return container_of(v, QmpInputVisitor, visitor);
43 static QObject *qmp_input_get_object(QmpInputVisitor *qiv,
44 const char *name)
46 QObject *qobj = qiv->stack[qiv->nb_stack - 1].obj;
48 if (qobj) {
49 if (name && qobject_type(qobj) == QTYPE_QDICT) {
50 if (qiv->stack[qiv->nb_stack - 1].h) {
51 g_hash_table_remove(qiv->stack[qiv->nb_stack - 1].h, name);
53 return qdict_get(qobject_to_qdict(qobj), name);
54 } else if (qiv->stack[qiv->nb_stack - 1].entry) {
55 return qlist_entry_obj(qiv->stack[qiv->nb_stack - 1].entry);
59 return qobj;
62 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
64 GHashTable *h = opaque;
65 g_hash_table_insert(h, (gpointer) key, NULL);
68 static void qmp_input_push(QmpInputVisitor *qiv, QObject *obj, Error **errp)
70 GHashTable *h;
72 if (qiv->nb_stack >= QIV_STACK_SIZE) {
73 error_set(errp, QERR_BUFFER_OVERRUN);
74 return;
77 qiv->stack[qiv->nb_stack].obj = obj;
78 qiv->stack[qiv->nb_stack].entry = NULL;
79 qiv->stack[qiv->nb_stack].h = NULL;
81 if (qiv->strict && qobject_type(obj) == QTYPE_QDICT) {
82 h = g_hash_table_new(g_str_hash, g_str_equal);
83 qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
84 qiv->stack[qiv->nb_stack].h = h;
87 qiv->nb_stack++;
90 static void qmp_input_pop(QmpInputVisitor *qiv, Error **errp)
92 GHashTableIter iter;
93 gpointer key;
95 if (qiv->strict && qiv->stack[qiv->nb_stack - 1].h) {
96 g_hash_table_iter_init(&iter, qiv->stack[qiv->nb_stack - 1].h);
97 if (g_hash_table_iter_next(&iter, &key, NULL)) {
98 error_set(errp, QERR_QMP_EXTRA_MEMBER, (char *) key);
100 g_hash_table_unref(qiv->stack[qiv->nb_stack - 1].h);
103 assert(qiv->nb_stack > 0);
104 qiv->nb_stack--;
107 static void qmp_input_start_struct(Visitor *v, void **obj, const char *kind,
108 const char *name, size_t size, Error **errp)
110 QmpInputVisitor *qiv = to_qiv(v);
111 QObject *qobj = qmp_input_get_object(qiv, name);
112 Error *err = NULL;
114 if (!qobj || qobject_type(qobj) != QTYPE_QDICT) {
115 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
116 "QDict");
117 return;
120 qmp_input_push(qiv, qobj, &err);
121 if (err) {
122 error_propagate(errp, err);
123 return;
126 if (obj) {
127 *obj = g_malloc0(size);
131 static void qmp_input_end_struct(Visitor *v, Error **errp)
133 QmpInputVisitor *qiv = to_qiv(v);
135 qmp_input_pop(qiv, errp);
138 static void qmp_input_start_list(Visitor *v, const char *name, Error **errp)
140 QmpInputVisitor *qiv = to_qiv(v);
141 QObject *qobj = qmp_input_get_object(qiv, name);
143 if (!qobj || qobject_type(qobj) != QTYPE_QLIST) {
144 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
145 "list");
146 return;
149 qmp_input_push(qiv, qobj, errp);
152 static GenericList *qmp_input_next_list(Visitor *v, GenericList **list,
153 Error **errp)
155 QmpInputVisitor *qiv = to_qiv(v);
156 GenericList *entry;
157 StackObject *so = &qiv->stack[qiv->nb_stack - 1];
158 bool first;
160 if (so->entry == NULL) {
161 so->entry = qlist_first(qobject_to_qlist(so->obj));
162 first = true;
163 } else {
164 so->entry = qlist_next(so->entry);
165 first = false;
168 if (so->entry == NULL) {
169 return NULL;
172 entry = g_malloc0(sizeof(*entry));
173 if (first) {
174 *list = entry;
175 } else {
176 (*list)->next = entry;
179 return entry;
182 static void qmp_input_end_list(Visitor *v, Error **errp)
184 QmpInputVisitor *qiv = to_qiv(v);
186 qmp_input_pop(qiv, errp);
189 static void qmp_input_type_int(Visitor *v, int64_t *obj, const char *name,
190 Error **errp)
192 QmpInputVisitor *qiv = to_qiv(v);
193 QObject *qobj = qmp_input_get_object(qiv, name);
195 if (!qobj || qobject_type(qobj) != QTYPE_QINT) {
196 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
197 "integer");
198 return;
201 *obj = qint_get_int(qobject_to_qint(qobj));
204 static void qmp_input_type_bool(Visitor *v, bool *obj, const char *name,
205 Error **errp)
207 QmpInputVisitor *qiv = to_qiv(v);
208 QObject *qobj = qmp_input_get_object(qiv, name);
210 if (!qobj || qobject_type(qobj) != QTYPE_QBOOL) {
211 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
212 "boolean");
213 return;
216 *obj = qbool_get_int(qobject_to_qbool(qobj));
219 static void qmp_input_type_str(Visitor *v, char **obj, const char *name,
220 Error **errp)
222 QmpInputVisitor *qiv = to_qiv(v);
223 QObject *qobj = qmp_input_get_object(qiv, name);
225 if (!qobj || qobject_type(qobj) != QTYPE_QSTRING) {
226 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
227 "string");
228 return;
231 *obj = g_strdup(qstring_get_str(qobject_to_qstring(qobj)));
234 static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
235 Error **errp)
237 QmpInputVisitor *qiv = to_qiv(v);
238 QObject *qobj = qmp_input_get_object(qiv, name);
240 if (!qobj || qobject_type(qobj) != QTYPE_QFLOAT) {
241 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null",
242 "double");
243 return;
246 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
249 static void qmp_input_start_optional(Visitor *v, bool *present,
250 const char *name, Error **errp)
252 QmpInputVisitor *qiv = to_qiv(v);
253 QObject *qobj = qmp_input_get_object(qiv, name);
255 if (!qobj) {
256 *present = false;
257 return;
260 *present = true;
263 Visitor *qmp_input_get_visitor(QmpInputVisitor *v)
265 return &v->visitor;
268 void qmp_input_visitor_cleanup(QmpInputVisitor *v)
270 qobject_decref(v->stack[0].obj);
271 g_free(v);
274 QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
276 QmpInputVisitor *v;
278 v = g_malloc0(sizeof(*v));
280 v->visitor.start_struct = qmp_input_start_struct;
281 v->visitor.end_struct = qmp_input_end_struct;
282 v->visitor.start_list = qmp_input_start_list;
283 v->visitor.next_list = qmp_input_next_list;
284 v->visitor.end_list = qmp_input_end_list;
285 v->visitor.type_enum = input_type_enum;
286 v->visitor.type_int = qmp_input_type_int;
287 v->visitor.type_bool = qmp_input_type_bool;
288 v->visitor.type_str = qmp_input_type_str;
289 v->visitor.type_number = qmp_input_type_number;
290 v->visitor.start_optional = qmp_input_start_optional;
292 qmp_input_push(v, obj, NULL);
293 qobject_incref(obj);
295 return v;
298 QmpInputVisitor *qmp_input_visitor_new_strict(QObject *obj)
300 QmpInputVisitor *v;
302 v = qmp_input_visitor_new(obj);
303 v->strict = true;
305 return v;