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