]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/qobject-output-visitor.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[mirror_qemu.git] / qapi / qobject-output-visitor.c
CommitLineData
e4e6aa14
MR
1/*
2 * Core Definitions for QAPI/QMP Command Registry
3 *
08f9541d 4 * Copyright (C) 2012-2016 Red Hat, Inc.
e4e6aa14
MR
5 * Copyright IBM, Corp. 2011
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
cbf21151 15#include "qemu/osdep.h"
b3db211f 16#include "qapi/qobject-output-visitor.h"
7b1b5d19 17#include "qapi/visitor-impl.h"
1de7afc9 18#include "qemu/queue.h"
e4e6aa14 19#include "qemu-common.h"
7b1b5d19 20#include "qapi/qmp/types.h"
e4e6aa14 21
7d5e199a 22typedef struct QStackEntry {
e4e6aa14 23 QObject *value;
1158bb2a 24 void *qapi; /* sanity check that caller uses same pointer */
fc76ae8b 25 QSLIST_ENTRY(QStackEntry) node;
e4e6aa14
MR
26} QStackEntry;
27
7d5e199a 28struct QObjectOutputVisitor {
e4e6aa14 29 Visitor visitor;
fc76ae8b 30 QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
455ba08a 31 QObject *root; /* Root of the output visit */
3b098d56 32 QObject **result; /* User's storage location for result */
e4e6aa14
MR
33};
34
7d5e199a
DB
35#define qobject_output_add(qov, name, value) \
36 qobject_output_add_obj(qov, name, QOBJECT(value))
37#define qobject_output_push(qov, value, qapi) \
38 qobject_output_push_obj(qov, QOBJECT(value), qapi)
e4e6aa14 39
7d5e199a 40static QObjectOutputVisitor *to_qov(Visitor *v)
e4e6aa14 41{
7d5e199a 42 return container_of(v, QObjectOutputVisitor, visitor);
e4e6aa14
MR
43}
44
a8615640 45/* Push @value onto the stack of current QObjects being built */
7d5e199a
DB
46static void qobject_output_push_obj(QObjectOutputVisitor *qov, QObject *value,
47 void *qapi)
e4e6aa14 48{
7267c094 49 QStackEntry *e = g_malloc0(sizeof(*e));
e4e6aa14 50
455ba08a 51 assert(qov->root);
a8615640 52 assert(value);
e4e6aa14 53 e->value = value;
1158bb2a 54 e->qapi = qapi;
fc76ae8b 55 QSLIST_INSERT_HEAD(&qov->stack, e, node);
e4e6aa14
MR
56}
57
a8615640 58/* Pop a value off the stack of QObjects being built, and return it. */
7d5e199a 59static QObject *qobject_output_pop(QObjectOutputVisitor *qov, void *qapi)
e4e6aa14 60{
fc76ae8b 61 QStackEntry *e = QSLIST_FIRST(&qov->stack);
e4e6aa14 62 QObject *value;
a8615640
EB
63
64 assert(e);
1158bb2a 65 assert(e->qapi == qapi);
fc76ae8b 66 QSLIST_REMOVE_HEAD(&qov->stack, node);
e4e6aa14 67 value = e->value;
a8615640 68 assert(value);
7267c094 69 g_free(e);
e4e6aa14
MR
70 return value;
71}
72
a8615640
EB
73/* Add @value to the current QObject being built.
74 * If the stack is visiting a dictionary or list, @value is now owned
75 * by that container. Otherwise, @value is now the root. */
7d5e199a
DB
76static void qobject_output_add_obj(QObjectOutputVisitor *qov, const char *name,
77 QObject *value)
e4e6aa14 78{
fc76ae8b 79 QStackEntry *e = QSLIST_FIRST(&qov->stack);
455ba08a 80 QObject *cur = e ? e->value : NULL;
e4e6aa14 81
455ba08a 82 if (!cur) {
56a6f02b
EB
83 /* Don't allow reuse of visitor on more than one root */
84 assert(!qov->root);
455ba08a
EB
85 qov->root = value;
86 } else {
87 switch (qobject_type(cur)) {
88 case QTYPE_QDICT:
89 assert(name);
90 qdict_put_obj(qobject_to_qdict(cur), name, value);
91 break;
92 case QTYPE_QLIST:
56a6f02b 93 assert(!name);
455ba08a
EB
94 qlist_append_obj(qobject_to_qlist(cur), value);
95 break;
96 default:
97 g_assert_not_reached();
98 }
e4e6aa14
MR
99 }
100}
101
7d5e199a
DB
102static void qobject_output_start_struct(Visitor *v, const char *name,
103 void **obj, size_t unused, Error **errp)
e4e6aa14 104{
7d5e199a 105 QObjectOutputVisitor *qov = to_qov(v);
e4e6aa14
MR
106 QDict *dict = qdict_new();
107
7d5e199a
DB
108 qobject_output_add(qov, name, dict);
109 qobject_output_push(qov, dict, obj);
e4e6aa14
MR
110}
111
7d5e199a 112static void qobject_output_end_struct(Visitor *v, void **obj)
e4e6aa14 113{
7d5e199a
DB
114 QObjectOutputVisitor *qov = to_qov(v);
115 QObject *value = qobject_output_pop(qov, obj);
56a6f02b 116 assert(qobject_type(value) == QTYPE_QDICT);
e4e6aa14
MR
117}
118
7d5e199a
DB
119static void qobject_output_start_list(Visitor *v, const char *name,
120 GenericList **listp, size_t size,
121 Error **errp)
e4e6aa14 122{
7d5e199a 123 QObjectOutputVisitor *qov = to_qov(v);
e4e6aa14
MR
124 QList *list = qlist_new();
125
7d5e199a
DB
126 qobject_output_add(qov, name, list);
127 qobject_output_push(qov, list, listp);
e4e6aa14
MR
128}
129
7d5e199a
DB
130static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail,
131 size_t size)
e4e6aa14 132{
d9f62dde 133 return tail->next;
e4e6aa14
MR
134}
135
7d5e199a 136static void qobject_output_end_list(Visitor *v, void **obj)
e4e6aa14 137{
7d5e199a
DB
138 QObjectOutputVisitor *qov = to_qov(v);
139 QObject *value = qobject_output_pop(qov, obj);
56a6f02b 140 assert(qobject_type(value) == QTYPE_QLIST);
e4e6aa14
MR
141}
142
7d5e199a
DB
143static void qobject_output_type_int64(Visitor *v, const char *name,
144 int64_t *obj, Error **errp)
e4e6aa14 145{
7d5e199a
DB
146 QObjectOutputVisitor *qov = to_qov(v);
147 qobject_output_add(qov, name, qint_from_int(*obj));
e4e6aa14
MR
148}
149
7d5e199a
DB
150static void qobject_output_type_uint64(Visitor *v, const char *name,
151 uint64_t *obj, Error **errp)
f755dea7 152{
b3db211f 153 /* FIXME values larger than INT64_MAX become negative */
7d5e199a
DB
154 QObjectOutputVisitor *qov = to_qov(v);
155 qobject_output_add(qov, name, qint_from_int(*obj));
f755dea7
EB
156}
157
7d5e199a
DB
158static void qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
159 Error **errp)
e4e6aa14 160{
7d5e199a
DB
161 QObjectOutputVisitor *qov = to_qov(v);
162 qobject_output_add(qov, name, qbool_from_bool(*obj));
e4e6aa14
MR
163}
164
7d5e199a
DB
165static void qobject_output_type_str(Visitor *v, const char *name, char **obj,
166 Error **errp)
e4e6aa14 167{
7d5e199a 168 QObjectOutputVisitor *qov = to_qov(v);
e4e6aa14 169 if (*obj) {
7d5e199a 170 qobject_output_add(qov, name, qstring_from_str(*obj));
e4e6aa14 171 } else {
7d5e199a 172 qobject_output_add(qov, name, qstring_from_str(""));
e4e6aa14
MR
173 }
174}
175
7d5e199a
DB
176static void qobject_output_type_number(Visitor *v, const char *name,
177 double *obj, Error **errp)
e4e6aa14 178{
7d5e199a
DB
179 QObjectOutputVisitor *qov = to_qov(v);
180 qobject_output_add(qov, name, qfloat_from_double(*obj));
e4e6aa14
MR
181}
182
7d5e199a
DB
183static void qobject_output_type_any(Visitor *v, const char *name,
184 QObject **obj, Error **errp)
28770e05 185{
7d5e199a 186 QObjectOutputVisitor *qov = to_qov(v);
28770e05 187 qobject_incref(*obj);
7d5e199a 188 qobject_output_add_obj(qov, name, *obj);
28770e05
MA
189}
190
7d5e199a 191static void qobject_output_type_null(Visitor *v, const char *name, Error **errp)
3bc97fd5 192{
7d5e199a
DB
193 QObjectOutputVisitor *qov = to_qov(v);
194 qobject_output_add_obj(qov, name, qnull());
3bc97fd5
EB
195}
196
56a6f02b
EB
197/* Finish building, and return the root object.
198 * The root object is never null. The caller becomes the object's
199 * owner, and should use qobject_decref() when done with it. */
7d5e199a 200static void qobject_output_complete(Visitor *v, void *opaque)
e4e6aa14 201{
7d5e199a 202 QObjectOutputVisitor *qov = to_qov(v);
3b098d56 203
56a6f02b 204 /* A visit must have occurred, with each start paired with end. */
fc76ae8b 205 assert(qov->root && QSLIST_EMPTY(&qov->stack));
3b098d56 206 assert(opaque == qov->result);
56a6f02b
EB
207
208 qobject_incref(qov->root);
3b098d56
EB
209 *qov->result = qov->root;
210 qov->result = NULL;
e4e6aa14
MR
211}
212
7d5e199a 213static void qobject_output_free(Visitor *v)
2c0ef9f4 214{
7d5e199a 215 QObjectOutputVisitor *qov = to_qov(v);
fc76ae8b 216 QStackEntry *e;
e4e6aa14 217
fc76ae8b
PB
218 while (!QSLIST_EMPTY(&qov->stack)) {
219 e = QSLIST_FIRST(&qov->stack);
220 QSLIST_REMOVE_HEAD(&qov->stack, node);
7267c094 221 g_free(e);
e4e6aa14
MR
222 }
223
1830f22a
EB
224 qobject_decref(qov->root);
225 g_free(qov);
e4e6aa14
MR
226}
227
7d5e199a 228Visitor *qobject_output_visitor_new(QObject **result)
e4e6aa14 229{
7d5e199a 230 QObjectOutputVisitor *v;
e4e6aa14 231
7267c094 232 v = g_malloc0(sizeof(*v));
e4e6aa14 233
983f52d4 234 v->visitor.type = VISITOR_OUTPUT;
7d5e199a
DB
235 v->visitor.start_struct = qobject_output_start_struct;
236 v->visitor.end_struct = qobject_output_end_struct;
237 v->visitor.start_list = qobject_output_start_list;
238 v->visitor.next_list = qobject_output_next_list;
239 v->visitor.end_list = qobject_output_end_list;
240 v->visitor.type_int64 = qobject_output_type_int64;
241 v->visitor.type_uint64 = qobject_output_type_uint64;
242 v->visitor.type_bool = qobject_output_type_bool;
243 v->visitor.type_str = qobject_output_type_str;
244 v->visitor.type_number = qobject_output_type_number;
245 v->visitor.type_any = qobject_output_type_any;
246 v->visitor.type_null = qobject_output_type_null;
247 v->visitor.complete = qobject_output_complete;
248 v->visitor.free = qobject_output_free;
e4e6aa14 249
3b098d56
EB
250 *result = NULL;
251 v->result = result;
e4e6aa14 252
3b098d56 253 return &v->visitor;
e4e6aa14 254}