]> Git Repo - qemu.git/blame - qapi/qobject-output-visitor.c
Merge remote-tracking branch 'remotes/cschoenebeck/tags/pull-9p-20211027' into staging
[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"
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;
91fa93e5 34
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
012d4c96 107static bool qobject_output_start_struct(Visitor *v, const char *name,
7d5e199a 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);
012d4c96 115 return true;
e4e6aa14
MR
116}
117
7d5e199a 118static void qobject_output_end_struct(Visitor *v, void **obj)
e4e6aa14 119{
7d5e199a
DB
120 QObjectOutputVisitor *qov = to_qov(v);
121 QObject *value = qobject_output_pop(qov, obj);
56a6f02b 122 assert(qobject_type(value) == QTYPE_QDICT);
e4e6aa14
MR
123}
124
012d4c96 125static bool qobject_output_start_list(Visitor *v, const char *name,
7d5e199a
DB
126 GenericList **listp, size_t size,
127 Error **errp)
e4e6aa14 128{
7d5e199a 129 QObjectOutputVisitor *qov = to_qov(v);
e4e6aa14
MR
130 QList *list = qlist_new();
131
7d5e199a
DB
132 qobject_output_add(qov, name, list);
133 qobject_output_push(qov, list, listp);
012d4c96 134 return true;
e4e6aa14
MR
135}
136
7d5e199a
DB
137static GenericList *qobject_output_next_list(Visitor *v, GenericList *tail,
138 size_t size)
e4e6aa14 139{
d9f62dde 140 return tail->next;
e4e6aa14
MR
141}
142
7d5e199a 143static void qobject_output_end_list(Visitor *v, void **obj)
e4e6aa14 144{
7d5e199a
DB
145 QObjectOutputVisitor *qov = to_qov(v);
146 QObject *value = qobject_output_pop(qov, obj);
56a6f02b 147 assert(qobject_type(value) == QTYPE_QLIST);
e4e6aa14
MR
148}
149
012d4c96 150static bool qobject_output_type_int64(Visitor *v, const char *name,
7d5e199a 151 int64_t *obj, Error **errp)
e4e6aa14 152{
7d5e199a 153 QObjectOutputVisitor *qov = to_qov(v);
01b2ffce 154 qobject_output_add(qov, name, qnum_from_int(*obj));
012d4c96 155 return true;
e4e6aa14
MR
156}
157
012d4c96 158static bool qobject_output_type_uint64(Visitor *v, const char *name,
7d5e199a 159 uint64_t *obj, Error **errp)
f755dea7 160{
7d5e199a 161 QObjectOutputVisitor *qov = to_qov(v);
5923f85f 162 qobject_output_add(qov, name, qnum_from_uint(*obj));
012d4c96 163 return true;
f755dea7
EB
164}
165
012d4c96 166static bool qobject_output_type_bool(Visitor *v, const char *name, bool *obj,
7d5e199a 167 Error **errp)
e4e6aa14 168{
7d5e199a
DB
169 QObjectOutputVisitor *qov = to_qov(v);
170 qobject_output_add(qov, name, qbool_from_bool(*obj));
012d4c96 171 return true;
e4e6aa14
MR
172}
173
012d4c96 174static bool qobject_output_type_str(Visitor *v, const char *name, char **obj,
7d5e199a 175 Error **errp)
e4e6aa14 176{
7d5e199a 177 QObjectOutputVisitor *qov = to_qov(v);
e4e6aa14 178 if (*obj) {
7d5e199a 179 qobject_output_add(qov, name, qstring_from_str(*obj));
e4e6aa14 180 } else {
7d5e199a 181 qobject_output_add(qov, name, qstring_from_str(""));
e4e6aa14 182 }
012d4c96 183 return true;
e4e6aa14
MR
184}
185
012d4c96 186static bool qobject_output_type_number(Visitor *v, const char *name,
7d5e199a 187 double *obj, Error **errp)
e4e6aa14 188{
7d5e199a 189 QObjectOutputVisitor *qov = to_qov(v);
01b2ffce 190 qobject_output_add(qov, name, qnum_from_double(*obj));
012d4c96 191 return true;
e4e6aa14
MR
192}
193
012d4c96 194static bool qobject_output_type_any(Visitor *v, const char *name,
7d5e199a 195 QObject **obj, Error **errp)
28770e05 196{
7d5e199a 197 QObjectOutputVisitor *qov = to_qov(v);
f5a74a5a
MAL
198
199 qobject_output_add_obj(qov, name, qobject_ref(*obj));
012d4c96 200 return true;
28770e05
MA
201}
202
012d4c96 203static bool qobject_output_type_null(Visitor *v, const char *name,
d2f95f4d 204 QNull **obj, Error **errp)
3bc97fd5 205{
7d5e199a 206 QObjectOutputVisitor *qov = to_qov(v);
006ca09f 207 qobject_output_add(qov, name, qnull());
012d4c96 208 return true;
3bc97fd5
EB
209}
210
91fa93e5
MA
211static bool qobject_output_deprecated(Visitor *v, const char *name)
212{
ed29bb28 213 return v->compat_policy.deprecated_output != COMPAT_POLICY_OUTPUT_HIDE;
91fa93e5
MA
214}
215
56a6f02b
EB
216/* Finish building, and return the root object.
217 * The root object is never null. The caller becomes the object's
cb3e7f08 218 * owner, and should use qobject_unref() when done with it. */
7d5e199a 219static void qobject_output_complete(Visitor *v, void *opaque)
e4e6aa14 220{
7d5e199a 221 QObjectOutputVisitor *qov = to_qov(v);
3b098d56 222
56a6f02b 223 /* A visit must have occurred, with each start paired with end. */
fc76ae8b 224 assert(qov->root && QSLIST_EMPTY(&qov->stack));
3b098d56 225 assert(opaque == qov->result);
56a6f02b 226
f5a74a5a 227 *qov->result = qobject_ref(qov->root);
3b098d56 228 qov->result = NULL;
e4e6aa14
MR
229}
230
7d5e199a 231static void qobject_output_free(Visitor *v)
2c0ef9f4 232{
7d5e199a 233 QObjectOutputVisitor *qov = to_qov(v);
fc76ae8b 234 QStackEntry *e;
e4e6aa14 235
fc76ae8b
PB
236 while (!QSLIST_EMPTY(&qov->stack)) {
237 e = QSLIST_FIRST(&qov->stack);
238 QSLIST_REMOVE_HEAD(&qov->stack, node);
7267c094 239 g_free(e);
e4e6aa14
MR
240 }
241
cb3e7f08 242 qobject_unref(qov->root);
1830f22a 243 g_free(qov);
e4e6aa14
MR
244}
245
7d5e199a 246Visitor *qobject_output_visitor_new(QObject **result)
e4e6aa14 247{
7d5e199a 248 QObjectOutputVisitor *v;
e4e6aa14 249
7267c094 250 v = g_malloc0(sizeof(*v));
e4e6aa14 251
983f52d4 252 v->visitor.type = VISITOR_OUTPUT;
7d5e199a
DB
253 v->visitor.start_struct = qobject_output_start_struct;
254 v->visitor.end_struct = qobject_output_end_struct;
255 v->visitor.start_list = qobject_output_start_list;
256 v->visitor.next_list = qobject_output_next_list;
257 v->visitor.end_list = qobject_output_end_list;
258 v->visitor.type_int64 = qobject_output_type_int64;
259 v->visitor.type_uint64 = qobject_output_type_uint64;
260 v->visitor.type_bool = qobject_output_type_bool;
261 v->visitor.type_str = qobject_output_type_str;
262 v->visitor.type_number = qobject_output_type_number;
263 v->visitor.type_any = qobject_output_type_any;
264 v->visitor.type_null = qobject_output_type_null;
91fa93e5 265 v->visitor.deprecated = qobject_output_deprecated;
7d5e199a
DB
266 v->visitor.complete = qobject_output_complete;
267 v->visitor.free = qobject_output_free;
e4e6aa14 268
3b098d56
EB
269 *result = NULL;
270 v->result = result;
e4e6aa14 271
3b098d56 272 return &v->visitor;
e4e6aa14 273}
This page took 0.493854 seconds and 4 git commands to generate.