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