s390x: add function to deliver restart irqs
[qemu.git] / qapi / qmp-output-visitor.c
blob96b338463ed8f16e385921a1ca056cd4d9625cf4
1 /*
2 * Core Definitions for QAPI/QMP Command Registry
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 "qapi/qmp-output-visitor.h"
15 #include "qapi/visitor-impl.h"
16 #include "qemu/queue.h"
17 #include "qemu-common.h"
18 #include "qapi/qmp/types.h"
19 #include "qapi/qmp/qerror.h"
21 typedef struct QStackEntry
23 QObject *value;
24 bool is_list_head;
25 QTAILQ_ENTRY(QStackEntry) node;
26 } QStackEntry;
28 typedef QTAILQ_HEAD(QStack, QStackEntry) QStack;
30 struct QmpOutputVisitor
32 Visitor visitor;
33 QStack stack;
36 #define qmp_output_add(qov, name, value) \
37 qmp_output_add_obj(qov, name, QOBJECT(value))
38 #define qmp_output_push(qov, value) qmp_output_push_obj(qov, QOBJECT(value))
40 static QmpOutputVisitor *to_qov(Visitor *v)
42 return container_of(v, QmpOutputVisitor, visitor);
45 static void qmp_output_push_obj(QmpOutputVisitor *qov, QObject *value)
47 QStackEntry *e = g_malloc0(sizeof(*e));
49 e->value = value;
50 if (qobject_type(e->value) == QTYPE_QLIST) {
51 e->is_list_head = true;
53 QTAILQ_INSERT_HEAD(&qov->stack, e, node);
56 static QObject *qmp_output_pop(QmpOutputVisitor *qov)
58 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
59 QObject *value;
60 QTAILQ_REMOVE(&qov->stack, e, node);
61 value = e->value;
62 g_free(e);
63 return value;
66 static QObject *qmp_output_first(QmpOutputVisitor *qov)
68 QStackEntry *e = QTAILQ_LAST(&qov->stack, QStack);
70 /* FIXME - find a better way to deal with NULL values */
71 if (!e) {
72 return NULL;
75 return e->value;
78 static QObject *qmp_output_last(QmpOutputVisitor *qov)
80 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
81 return e->value;
84 static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
85 QObject *value)
87 QObject *cur;
89 if (QTAILQ_EMPTY(&qov->stack)) {
90 qmp_output_push_obj(qov, value);
91 return;
94 cur = qmp_output_last(qov);
96 switch (qobject_type(cur)) {
97 case QTYPE_QDICT:
98 qdict_put_obj(qobject_to_qdict(cur), name, value);
99 break;
100 case QTYPE_QLIST:
101 qlist_append_obj(qobject_to_qlist(cur), value);
102 break;
103 default:
104 qobject_decref(qmp_output_pop(qov));
105 qmp_output_push_obj(qov, value);
106 break;
110 static void qmp_output_start_struct(Visitor *v, void **obj, const char *kind,
111 const char *name, size_t unused,
112 Error **errp)
114 QmpOutputVisitor *qov = to_qov(v);
115 QDict *dict = qdict_new();
117 qmp_output_add(qov, name, dict);
118 qmp_output_push(qov, dict);
121 static void qmp_output_end_struct(Visitor *v, Error **errp)
123 QmpOutputVisitor *qov = to_qov(v);
124 qmp_output_pop(qov);
127 static void qmp_output_start_list(Visitor *v, const char *name, Error **errp)
129 QmpOutputVisitor *qov = to_qov(v);
130 QList *list = qlist_new();
132 qmp_output_add(qov, name, list);
133 qmp_output_push(qov, list);
136 static GenericList *qmp_output_next_list(Visitor *v, GenericList **listp,
137 Error **errp)
139 GenericList *list = *listp;
140 QmpOutputVisitor *qov = to_qov(v);
141 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
143 assert(e);
144 if (e->is_list_head) {
145 e->is_list_head = false;
146 return list;
149 return list ? list->next : NULL;
152 static void qmp_output_end_list(Visitor *v, Error **errp)
154 QmpOutputVisitor *qov = to_qov(v);
155 qmp_output_pop(qov);
158 static void qmp_output_type_int(Visitor *v, int64_t *obj, const char *name,
159 Error **errp)
161 QmpOutputVisitor *qov = to_qov(v);
162 qmp_output_add(qov, name, qint_from_int(*obj));
165 static void qmp_output_type_bool(Visitor *v, bool *obj, const char *name,
166 Error **errp)
168 QmpOutputVisitor *qov = to_qov(v);
169 qmp_output_add(qov, name, qbool_from_int(*obj));
172 static void qmp_output_type_str(Visitor *v, char **obj, const char *name,
173 Error **errp)
175 QmpOutputVisitor *qov = to_qov(v);
176 if (*obj) {
177 qmp_output_add(qov, name, qstring_from_str(*obj));
178 } else {
179 qmp_output_add(qov, name, qstring_from_str(""));
183 static void qmp_output_type_number(Visitor *v, double *obj, const char *name,
184 Error **errp)
186 QmpOutputVisitor *qov = to_qov(v);
187 qmp_output_add(qov, name, qfloat_from_double(*obj));
190 QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
192 QObject *obj = qmp_output_first(qov);
193 if (obj) {
194 qobject_incref(obj);
196 return obj;
199 Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)
201 return &v->visitor;
204 void qmp_output_visitor_cleanup(QmpOutputVisitor *v)
206 QStackEntry *e, *tmp;
208 /* The bottom QStackEntry, if any, owns the root QObject. See the
209 * qmp_output_push_obj() invocations in qmp_output_add_obj(). */
210 QObject *root = QTAILQ_EMPTY(&v->stack) ? NULL : qmp_output_first(v);
212 QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) {
213 QTAILQ_REMOVE(&v->stack, e, node);
214 g_free(e);
217 qobject_decref(root);
218 g_free(v);
221 QmpOutputVisitor *qmp_output_visitor_new(void)
223 QmpOutputVisitor *v;
225 v = g_malloc0(sizeof(*v));
227 v->visitor.start_struct = qmp_output_start_struct;
228 v->visitor.end_struct = qmp_output_end_struct;
229 v->visitor.start_list = qmp_output_start_list;
230 v->visitor.next_list = qmp_output_next_list;
231 v->visitor.end_list = qmp_output_end_list;
232 v->visitor.type_enum = output_type_enum;
233 v->visitor.type_int = qmp_output_type_int;
234 v->visitor.type_bool = qmp_output_type_bool;
235 v->visitor.type_str = qmp_output_type_str;
236 v->visitor.type_number = qmp_output_type_number;
238 QTAILQ_INIT(&v->stack);
240 return v;