]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/qapi-visit-core.c
qapi: Make output visitor return qnull() instead of NULL
[mirror_qemu.git] / qapi / qapi-visit-core.c
CommitLineData
2345c77c
MR
1/*
2 * Core Definitions for QAPI Visitor Classes
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 *
12 */
13
79ee7df8 14#include "qemu-common.h"
69dd62df 15#include "qapi/qmp/qobject.h"
7b1b5d19
PB
16#include "qapi/qmp/qerror.h"
17#include "qapi/visitor.h"
18#include "qapi/visitor-impl.h"
2345c77c 19
2345c77c
MR
20void visit_start_struct(Visitor *v, void **obj, const char *kind,
21 const char *name, size_t size, Error **errp)
22{
297a3646 23 v->start_struct(v, obj, kind, name, size, errp);
2345c77c
MR
24}
25
26void visit_end_struct(Visitor *v, Error **errp)
27{
d195325b 28 v->end_struct(v, errp);
2345c77c
MR
29}
30
761d524d
KW
31void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
32 Error **errp)
33{
297a3646 34 if (v->start_implicit_struct) {
761d524d
KW
35 v->start_implicit_struct(v, obj, size, errp);
36 }
37}
38
39void visit_end_implicit_struct(Visitor *v, Error **errp)
40{
761d524d
KW
41 if (v->end_implicit_struct) {
42 v->end_implicit_struct(v, errp);
43 }
44}
45
2345c77c
MR
46void visit_start_list(Visitor *v, const char *name, Error **errp)
47{
297a3646 48 v->start_list(v, name, errp);
2345c77c
MR
49}
50
51GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
52{
297a3646 53 return v->next_list(v, list, errp);
2345c77c
MR
54}
55
56void visit_end_list(Visitor *v, Error **errp)
57{
d195325b 58 v->end_list(v, errp);
2345c77c
MR
59}
60
cee2dedb
MR
61bool visit_start_union(Visitor *v, bool data_present, Error **errp)
62{
63 if (v->start_union) {
64 return v->start_union(v, data_present, errp);
65 }
66 return true;
67}
68
69void visit_end_union(Visitor *v, bool data_present, Error **errp)
70{
71 if (v->end_union) {
72 v->end_union(v, data_present, errp);
73 }
74}
75
e2cd0f4f
MA
76void visit_optional(Visitor *v, bool *present, const char *name,
77 Error **errp)
2345c77c 78{
297a3646 79 if (v->optional) {
e2cd0f4f 80 v->optional(v, present, name, errp);
2345c77c
MR
81 }
82}
83
69dd62df
KW
84void visit_get_next_type(Visitor *v, int *obj, const int *qtypes,
85 const char *name, Error **errp)
86{
297a3646 87 if (v->get_next_type) {
69dd62df
KW
88 v->get_next_type(v, obj, qtypes, name, errp);
89 }
90}
91
2e4450ff 92void visit_type_enum(Visitor *v, int *obj, const char * const strings[],
2345c77c
MR
93 const char *kind, const char *name, Error **errp)
94{
297a3646 95 v->type_enum(v, obj, strings, kind, name, errp);
2345c77c
MR
96}
97
98void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
99{
297a3646 100 v->type_int(v, obj, name, errp);
2345c77c
MR
101}
102
4e27e819
MR
103void visit_type_uint8(Visitor *v, uint8_t *obj, const char *name, Error **errp)
104{
105 int64_t value;
297a3646
MA
106
107 if (v->type_uint8) {
108 v->type_uint8(v, obj, name, errp);
109 } else {
110 value = *obj;
111 v->type_int(v, &value, name, errp);
112 if (value < 0 || value > UINT8_MAX) {
c6bd8c70
MA
113 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
114 name ? name : "null", "uint8_t");
297a3646 115 return;
4e27e819 116 }
297a3646 117 *obj = value;
4e27e819
MR
118 }
119}
120
121void visit_type_uint16(Visitor *v, uint16_t *obj, const char *name, Error **errp)
122{
123 int64_t value;
297a3646
MA
124
125 if (v->type_uint16) {
126 v->type_uint16(v, obj, name, errp);
127 } else {
128 value = *obj;
129 v->type_int(v, &value, name, errp);
130 if (value < 0 || value > UINT16_MAX) {
c6bd8c70
MA
131 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
132 name ? name : "null", "uint16_t");
297a3646 133 return;
4e27e819 134 }
297a3646 135 *obj = value;
4e27e819
MR
136 }
137}
138
139void visit_type_uint32(Visitor *v, uint32_t *obj, const char *name, Error **errp)
140{
141 int64_t value;
297a3646
MA
142
143 if (v->type_uint32) {
144 v->type_uint32(v, obj, name, errp);
145 } else {
146 value = *obj;
147 v->type_int(v, &value, name, errp);
148 if (value < 0 || value > UINT32_MAX) {
c6bd8c70
MA
149 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
150 name ? name : "null", "uint32_t");
297a3646 151 return;
4e27e819 152 }
297a3646 153 *obj = value;
4e27e819
MR
154 }
155}
156
157void visit_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
158{
159 int64_t value;
297a3646
MA
160
161 if (v->type_uint64) {
162 v->type_uint64(v, obj, name, errp);
163 } else {
164 value = *obj;
165 v->type_int(v, &value, name, errp);
166 *obj = value;
4e27e819
MR
167 }
168}
169
170void visit_type_int8(Visitor *v, int8_t *obj, const char *name, Error **errp)
171{
172 int64_t value;
297a3646
MA
173
174 if (v->type_int8) {
175 v->type_int8(v, obj, name, errp);
176 } else {
177 value = *obj;
178 v->type_int(v, &value, name, errp);
179 if (value < INT8_MIN || value > INT8_MAX) {
c6bd8c70
MA
180 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
181 name ? name : "null", "int8_t");
297a3646 182 return;
4e27e819 183 }
297a3646 184 *obj = value;
4e27e819
MR
185 }
186}
187
188void visit_type_int16(Visitor *v, int16_t *obj, const char *name, Error **errp)
189{
190 int64_t value;
297a3646
MA
191
192 if (v->type_int16) {
193 v->type_int16(v, obj, name, errp);
194 } else {
195 value = *obj;
196 v->type_int(v, &value, name, errp);
197 if (value < INT16_MIN || value > INT16_MAX) {
c6bd8c70
MA
198 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
199 name ? name : "null", "int16_t");
297a3646 200 return;
4e27e819 201 }
297a3646 202 *obj = value;
4e27e819
MR
203 }
204}
205
206void visit_type_int32(Visitor *v, int32_t *obj, const char *name, Error **errp)
207{
208 int64_t value;
297a3646
MA
209
210 if (v->type_int32) {
211 v->type_int32(v, obj, name, errp);
212 } else {
213 value = *obj;
214 v->type_int(v, &value, name, errp);
215 if (value < INT32_MIN || value > INT32_MAX) {
c6bd8c70
MA
216 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
217 name ? name : "null", "int32_t");
297a3646 218 return;
4e27e819 219 }
297a3646 220 *obj = value;
4e27e819
MR
221 }
222}
223
224void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp)
225{
297a3646
MA
226 if (v->type_int64) {
227 v->type_int64(v, obj, name, errp);
228 } else {
229 v->type_int(v, obj, name, errp);
4e27e819
MR
230 }
231}
232
092705d4
LE
233void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
234{
b8877962 235 int64_t value;
297a3646
MA
236
237 if (v->type_size) {
238 v->type_size(v, obj, name, errp);
239 } else if (v->type_uint64) {
240 v->type_uint64(v, obj, name, errp);
241 } else {
242 value = *obj;
243 v->type_int(v, &value, name, errp);
244 *obj = value;
092705d4
LE
245 }
246}
247
2345c77c
MR
248void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
249{
297a3646 250 v->type_bool(v, obj, name, errp);
2345c77c
MR
251}
252
253void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
254{
297a3646 255 v->type_str(v, obj, name, errp);
2345c77c
MR
256}
257
258void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
259{
297a3646 260 v->type_number(v, obj, name, errp);
2345c77c 261}
0f71a1e0 262
2e4450ff 263void output_type_enum(Visitor *v, int *obj, const char * const strings[],
0f71a1e0
PB
264 const char *kind, const char *name,
265 Error **errp)
266{
267 int i = 0;
268 int value = *obj;
269 char *enum_str;
270
271 assert(strings);
272 while (strings[i++] != NULL);
273 if (value < 0 || value >= i - 1) {
c6bd8c70 274 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
0f71a1e0
PB
275 return;
276 }
277
278 enum_str = (char *)strings[value];
279 visit_type_str(v, &enum_str, name, errp);
280}
281
2e4450ff 282void input_type_enum(Visitor *v, int *obj, const char * const strings[],
0f71a1e0
PB
283 const char *kind, const char *name,
284 Error **errp)
285{
297a3646 286 Error *local_err = NULL;
0f71a1e0
PB
287 int64_t value = 0;
288 char *enum_str;
289
290 assert(strings);
291
297a3646
MA
292 visit_type_str(v, &enum_str, name, &local_err);
293 if (local_err) {
294 error_propagate(errp, local_err);
0f71a1e0
PB
295 return;
296 }
297
298 while (strings[value] != NULL) {
299 if (strcmp(strings[value], enum_str) == 0) {
300 break;
301 }
302 value++;
303 }
304
305 if (strings[value] == NULL) {
c6bd8c70 306 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
0f71a1e0
PB
307 g_free(enum_str);
308 return;
309 }
310
311 g_free(enum_str);
312 *obj = value;
313}