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