]> Git Repo - qemu.git/blame - qapi/qapi-visit-core.c
target-ppc: Optimize rlwnm MB=0 ME=31
[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 <[email protected]>
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
e2cd0f4f
MA
61void visit_optional(Visitor *v, bool *present, const char *name,
62 Error **errp)
2345c77c 63{
297a3646 64 if (v->optional) {
e2cd0f4f 65 v->optional(v, present, name, errp);
2345c77c
MR
66 }
67}
68
69dd62df
KW
69void visit_get_next_type(Visitor *v, int *obj, const int *qtypes,
70 const char *name, Error **errp)
71{
297a3646 72 if (v->get_next_type) {
69dd62df
KW
73 v->get_next_type(v, obj, qtypes, name, errp);
74 }
75}
76
2345c77c
MR
77void visit_type_enum(Visitor *v, int *obj, const char *strings[],
78 const char *kind, const char *name, Error **errp)
79{
297a3646 80 v->type_enum(v, obj, strings, kind, name, errp);
2345c77c
MR
81}
82
83void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
84{
297a3646 85 v->type_int(v, obj, name, errp);
2345c77c
MR
86}
87
4e27e819
MR
88void visit_type_uint8(Visitor *v, uint8_t *obj, const char *name, Error **errp)
89{
90 int64_t value;
297a3646
MA
91
92 if (v->type_uint8) {
93 v->type_uint8(v, obj, name, errp);
94 } else {
95 value = *obj;
96 v->type_int(v, &value, name, errp);
97 if (value < 0 || value > UINT8_MAX) {
98 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
99 "uint8_t");
100 return;
4e27e819 101 }
297a3646 102 *obj = value;
4e27e819
MR
103 }
104}
105
106void visit_type_uint16(Visitor *v, uint16_t *obj, const char *name, Error **errp)
107{
108 int64_t value;
297a3646
MA
109
110 if (v->type_uint16) {
111 v->type_uint16(v, obj, name, errp);
112 } else {
113 value = *obj;
114 v->type_int(v, &value, name, errp);
115 if (value < 0 || value > UINT16_MAX) {
116 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
117 "uint16_t");
118 return;
4e27e819 119 }
297a3646 120 *obj = value;
4e27e819
MR
121 }
122}
123
124void visit_type_uint32(Visitor *v, uint32_t *obj, const char *name, Error **errp)
125{
126 int64_t value;
297a3646
MA
127
128 if (v->type_uint32) {
129 v->type_uint32(v, obj, name, errp);
130 } else {
131 value = *obj;
132 v->type_int(v, &value, name, errp);
133 if (value < 0 || value > UINT32_MAX) {
134 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
135 "uint32_t");
136 return;
4e27e819 137 }
297a3646 138 *obj = value;
4e27e819
MR
139 }
140}
141
142void visit_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
143{
144 int64_t value;
297a3646
MA
145
146 if (v->type_uint64) {
147 v->type_uint64(v, obj, name, errp);
148 } else {
149 value = *obj;
150 v->type_int(v, &value, name, errp);
151 *obj = value;
4e27e819
MR
152 }
153}
154
155void visit_type_int8(Visitor *v, int8_t *obj, const char *name, Error **errp)
156{
157 int64_t value;
297a3646
MA
158
159 if (v->type_int8) {
160 v->type_int8(v, obj, name, errp);
161 } else {
162 value = *obj;
163 v->type_int(v, &value, name, errp);
164 if (value < INT8_MIN || value > INT8_MAX) {
165 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
166 "int8_t");
167 return;
4e27e819 168 }
297a3646 169 *obj = value;
4e27e819
MR
170 }
171}
172
173void visit_type_int16(Visitor *v, int16_t *obj, const char *name, Error **errp)
174{
175 int64_t value;
297a3646
MA
176
177 if (v->type_int16) {
178 v->type_int16(v, obj, name, errp);
179 } else {
180 value = *obj;
181 v->type_int(v, &value, name, errp);
182 if (value < INT16_MIN || value > INT16_MAX) {
183 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
184 "int16_t");
185 return;
4e27e819 186 }
297a3646 187 *obj = value;
4e27e819
MR
188 }
189}
190
191void visit_type_int32(Visitor *v, int32_t *obj, const char *name, Error **errp)
192{
193 int64_t value;
297a3646
MA
194
195 if (v->type_int32) {
196 v->type_int32(v, obj, name, errp);
197 } else {
198 value = *obj;
199 v->type_int(v, &value, name, errp);
200 if (value < INT32_MIN || value > INT32_MAX) {
201 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
202 "int32_t");
203 return;
4e27e819 204 }
297a3646 205 *obj = value;
4e27e819
MR
206 }
207}
208
209void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp)
210{
297a3646
MA
211 if (v->type_int64) {
212 v->type_int64(v, obj, name, errp);
213 } else {
214 v->type_int(v, obj, name, errp);
4e27e819
MR
215 }
216}
217
092705d4
LE
218void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
219{
b8877962 220 int64_t value;
297a3646
MA
221
222 if (v->type_size) {
223 v->type_size(v, obj, name, errp);
224 } else if (v->type_uint64) {
225 v->type_uint64(v, obj, name, errp);
226 } else {
227 value = *obj;
228 v->type_int(v, &value, name, errp);
229 *obj = value;
092705d4
LE
230 }
231}
232
2345c77c
MR
233void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
234{
297a3646 235 v->type_bool(v, obj, name, errp);
2345c77c
MR
236}
237
238void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
239{
297a3646 240 v->type_str(v, obj, name, errp);
2345c77c
MR
241}
242
243void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
244{
297a3646 245 v->type_number(v, obj, name, errp);
2345c77c 246}
0f71a1e0
PB
247
248void output_type_enum(Visitor *v, int *obj, const char *strings[],
249 const char *kind, const char *name,
250 Error **errp)
251{
252 int i = 0;
253 int value = *obj;
254 char *enum_str;
255
256 assert(strings);
257 while (strings[i++] != NULL);
258 if (value < 0 || value >= i - 1) {
259 error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
260 return;
261 }
262
263 enum_str = (char *)strings[value];
264 visit_type_str(v, &enum_str, name, errp);
265}
266
267void input_type_enum(Visitor *v, int *obj, const char *strings[],
268 const char *kind, const char *name,
269 Error **errp)
270{
297a3646 271 Error *local_err = NULL;
0f71a1e0
PB
272 int64_t value = 0;
273 char *enum_str;
274
275 assert(strings);
276
297a3646
MA
277 visit_type_str(v, &enum_str, name, &local_err);
278 if (local_err) {
279 error_propagate(errp, local_err);
0f71a1e0
PB
280 return;
281 }
282
283 while (strings[value] != NULL) {
284 if (strcmp(strings[value], enum_str) == 0) {
285 break;
286 }
287 value++;
288 }
289
290 if (strings[value] == NULL) {
94c3db85 291 error_set(errp, QERR_INVALID_PARAMETER, enum_str);
0f71a1e0
PB
292 g_free(enum_str);
293 return;
294 }
295
296 g_free(enum_str);
297 *obj = value;
298}
This page took 0.303265 seconds and 4 git commands to generate.