]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/qobject-output-visitor.c
audio: fix audio timer rate conversion bug
[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"
6b673957 20#include "qapi/qmp/qbool.h"
452fcdbc 21#include "qapi/qmp/qdict.h"
47e6b297 22#include "qapi/qmp/qlist.h"
15280c36
MA
23#include "qapi/qmp/qnull.h"
24#include "qapi/qmp/qnum.h"
6b673957 25#include "qapi/qmp/qstring.h"
e4e6aa14 26
7d5e199a 27typedef struct QStackEntry {
e4e6aa14 28 QObject *value;
1158bb2a 29 void *qapi; /* sanity check that caller uses same pointer */
fc76ae8b 30 QSLIST_ENTRY(QStackEntry) node;
e4e6aa14
MR
31} QStackEntry;
32
7d5e199a 33struct QObjectOutputVisitor {
e4e6aa14 34 Visitor visitor;
fc76ae8b 35 QSLIST_HEAD(, QStackEntry) stack; /* Stack of unfinished containers */
455ba08a 36 QObject *root; /* Root of the output visit */
3b098d56 37 QObject **result; /* User's storage location for result */
e4e6aa14
MR
38};
39
7d5e199a
DB
40#define qobject_output_add(qov, name, value) \
41 qobject_output_add_obj(qov, name, QOBJECT(value))
42#define qobject_output_push(qov, value, qapi) \
43 qobject_output_push_obj(qov, QOBJECT(value), qapi)
e4e6aa14 44
7d5e199a 45static QObjectOutputVisitor *to_qov(Visitor *v)
e4e6aa14 46{
7d5e199a 47 return container_of(v, QObjectOutputVisitor, visitor);
e4e6aa14
MR
48}
49
a8615640 50/* Push @value onto the stack of current QObjects being built */
7d5e199a
DB
51static void qobject_output_push_obj(QObjectOutputVisitor *qov, QObject *value,
52 void *qapi)
e4e6aa14 53{
7267c094 54 QStackEntry *e = g_malloc0(sizeof(*e));
e4e6aa14 55
455ba08a 56 assert(qov->root);
a8615640 57 assert(value);
e4e6aa14 58 e->value = value;
1158bb2a 59 e->qapi = qapi;
fc76ae8b 60 QSLIST_INSERT_HEAD(&qov->stack, e, node);
e4e6aa14
MR
61}
62
a8615640 63/* Pop a value off the stack of QObjects being built, and return it. */
7d5e199a 64static QObject *qobject_output_pop(QObjectOutputVisitor *qov, void *qapi)
e4e6aa14 65{
fc76ae8b 66 QStackEntry *e = QSLIST_FIRST(&qov->stack);
e4e6aa14 67 QObject *value;
a8615640
EB
68
69 assert(e);
1158bb2a 70 assert(e->qapi == qapi);
fc76ae8b 71 QSLIST_REMOVE_HEAD(&qov->stack, node);
e4e6aa14 72 value = e->value;
a8615640 73 assert(value);
7267c094 74 g_free(e);
e4e6aa14
MR
75 return value;
76}
77
a8615640
EB
78/* Add @value to the current QObject being built.
79 * If the stack is visiting a dictionary or list, @value is now owned
80 * by that container. Otherwise, @value is now the root. */
7d5e199a
DB
81static void qobject_output_add_obj(QObjectOutputVisitor *qov, const char *name,
82 QObject *value)
e4e6aa14 83{
fc76ae8b 84 QStackEntry *e = QSLIST_FIRST(&qov->stack);
455ba08a 85 QObject *cur = e ? e->value : NULL;
e4e6aa14 86
455ba08a 87 if (!cur) {
56a6f02b
EB
88 /* Don't allow reuse of visitor on more than one root */
89 assert(!qov->root);
455ba08a
EB
90 qov->root = value;
91 } else {
92 switch (qobject_type(cur)) {
93 case QTYPE_QDICT:
94 assert(name);
7dc847eb 95 qdict_put_obj(qobject_to(QDict, cur), name, value);
455ba08a
EB
96 break;
97 case QTYPE_QLIST:
56a6f02b 98 assert(!name);
7dc847eb 99 qlist_append_obj(qobject_to(QList, cur), value);
455ba08a
EB
100 break;
101 default:
102 g_assert_not_reached();
103 }
e4e6aa14
MR
104 }
105}
106
7d5e199a
DB
107static void qobject_output_start_struct(Visitor *v, const char *name,
108 void **obj, size_t unused, Error **errp)
e4e6aa14 109{
7d5e199a 110 QObjectOutputVisitor *qov = to_qov(v);
e4e6aa14
MR
111 QDict *dict = qdict_new();
112
7d5e199a
DB
113 qobject_output_add(qov, name, dict);
114 qobject_output_push(qov, dict, obj);
e4e6aa14
MR
115}
116
7d5e199a 117static void qobject_output_end_struct(Visitor *v, void **obj)
e4e6aa14 118{
7d5e199a
DB
119 QObjectOutputVisitor *qov = to_qov(v);
120 QObject *value = qobject_output_pop(qov, obj);
56a6f02b 121 assert(qobject_type(value) == QTYPE_QDICT);
e4e6aa14
MR
122}
123
7d5e199a
DB
124static void qobject_output_start_list(Visitor *v, const char *name,
125 GenericList **listp, size_t size,
126 Error **errp)
e4e6aa14 127{
7d5e199a 128 QObjectOutputVisitor *qov = to_qov(v);
e4e6aa14
MR
129 QList *list = qlist_new();
130
7d5e199a
DB
131 qobject_output_add(qov, name, list);
132 qobject_output_push(qov, list, listp);
e4e6aa14
MR
133}
134
7d5e199a
DB
135static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail,
136 size_t size)
e4e6aa14 137{
d9f62dde 138 return tail->next;
e4e6aa14
MR
139}
140
7d5e199a 141static void qobject_output_end_list(Visitor *v, void **obj)
e4e6aa14 142{
7d5e199a
DB
143 QObjectOutputVisitor *qov = to_qov(v);
144 QObject *value = qobject_output_pop(qov, obj);
56a6f02b 145 assert(qobject_type(value) == QTYPE_QLIST);
e4e6aa14
MR
146}
147
7d5e199a
DB
148static void qobject_output_type_int64(Visitor *v, const char *name,
149 int64_t *obj, Error **errp)
e4e6aa14 150{
7d5e199a 151 QObjectOutputVisitor *qov = to_qov(v);
01b2ffce 152 qobject_output_add(qov, name, qnum_from_int(*obj));
e4e6aa14
MR
153}
154
7d5e199a
DB
155static void qobject_output_type_uint64(Visitor *v, const char *name,
156 uint64_t *obj, Error **errp)
f755dea7 157{
7d5e199a 158 QObjectOutputVisitor *qov = to_qov(v);
5923f85f 159 qobject_output_add(qov, name, qnum_from_uint(*obj));
f755dea7
EB
160}
161
7d5e199a
DB
162static void qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
163 Error **errp)
e4e6aa14 164{
7d5e199a
DB
165 QObjectOutputVisitor *qov = to_qov(v);
166 qobject_output_add(qov, name, qbool_from_bool(*obj));
e4e6aa14
MR
167}
168
7d5e199a
DB
169static void qobject_output_type_str(Visitor *v, const char *name, char **obj,
170 Error **errp)
e4e6aa14 171{
7d5e199a 172 QObjectOutputVisitor *qov = to_qov(v);
e4e6aa14 173 if (*obj) {
7d5e199a 174 qobject_output_add(qov, name, qstring_from_str(*obj));
e4e6aa14 175 } else {
7d5e199a 176 qobject_output_add(qov, name, qstring_from_str(""));
e4e6aa14
MR
177 }
178}
179
7d5e199a
DB
180static void qobject_output_type_number(Visitor *v, const char *name,
181 double *obj, Error **errp)
e4e6aa14 182{
7d5e199a 183 QObjectOutputVisitor *qov = to_qov(v);
01b2ffce 184 qobject_output_add(qov, name, qnum_from_double(*obj));
e4e6aa14
MR
185}
186
7d5e199a
DB
187static void qobject_output_type_any(Visitor *v, const char *name,
188 QObject **obj, Error **errp)
28770e05 189{
7d5e199a 190 QObjectOutputVisitor *qov = to_qov(v);
f5a74a5a
MAL
191
192 qobject_output_add_obj(qov, name, qobject_ref(*obj));
28770e05
MA
193}
194
d2f95f4d
MA
195static void qobject_output_type_null(Visitor *v, const char *name,
196 QNull **obj, Error **errp)
3bc97fd5 197{
7d5e199a 198 QObjectOutputVisitor *qov = to_qov(v);
006ca09f 199 qobject_output_add(qov, name, qnull());
3bc97fd5
EB
200}
201
56a6f02b
EB
202/* Finish building, and return the root object.
203 * The root object is never null. The caller becomes the object's
cb3e7f08 204 * owner, and should use qobject_unref() when done with it. */
7d5e199a 205static void qobject_output_complete(Visitor *v, void *opaque)
e4e6aa14 206{
7d5e199a 207 QObjectOutputVisitor *qov = to_qov(v);
3b098d56 208
56a6f02b 209 /* A visit must have occurred, with each start paired with end. */
fc76ae8b 210 assert(qov->root && QSLIST_EMPTY(&qov->stack));
3b098d56 211 assert(opaque == qov->result);
56a6f02b 212
f5a74a5a 213 *qov->result = qobject_ref(qov->root);
3b098d56 214 qov->result = NULL;
e4e6aa14
MR
215}
216
7d5e199a 217static void qobject_output_free(Visitor *v)
2c0ef9f4 218{
7d5e199a 219 QObjectOutputVisitor *qov = to_qov(v);
fc76ae8b 220 QStackEntry *e;
e4e6aa14 221
fc76ae8b
PB
222 while (!QSLIST_EMPTY(&qov->stack)) {
223 e = QSLIST_FIRST(&qov->stack);
224 QSLIST_REMOVE_HEAD(&qov->stack, node);
7267c094 225 g_free(e);
e4e6aa14
MR
226 }
227
cb3e7f08 228 qobject_unref(qov->root);
1830f22a 229 g_free(qov);
e4e6aa14
MR
230}
231
7d5e199a 232Visitor *qobject_output_visitor_new(QObject **result)
e4e6aa14 233{
7d5e199a 234 QObjectOutputVisitor *v;
e4e6aa14 235
7267c094 236 v = g_malloc0(sizeof(*v));
e4e6aa14 237
983f52d4 238 v->visitor.type = VISITOR_OUTPUT;
7d5e199a
DB
239 v->visitor.start_struct = qobject_output_start_struct;
240 v->visitor.end_struct = qobject_output_end_struct;
241 v->visitor.start_list = qobject_output_start_list;
242 v->visitor.next_list = qobject_output_next_list;
243 v->visitor.end_list = qobject_output_end_list;
244 v->visitor.type_int64 = qobject_output_type_int64;
245 v->visitor.type_uint64 = qobject_output_type_uint64;
246 v->visitor.type_bool = qobject_output_type_bool;
247 v->visitor.type_str = qobject_output_type_str;
248 v->visitor.type_number = qobject_output_type_number;
249 v->visitor.type_any = qobject_output_type_any;
250 v->visitor.type_null = qobject_output_type_null;
251 v->visitor.complete = qobject_output_complete;
252 v->visitor.free = qobject_output_free;
e4e6aa14 253
3b098d56
EB
254 *result = NULL;
255 v->result = result;
e4e6aa14 256
3b098d56 257 return &v->visitor;
e4e6aa14 258}