nvme: remove superfluous check
[qemu-kvm.git] / qapi / qapi-visit-core.c
blobb66b93ae2b0113c8080dfddfbe9bebfb8028bda9
1 /*
2 * Core Definitions for QAPI Visitor Classes
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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.
14 #include "qemu-common.h"
15 #include "qapi/qmp/qobject.h"
16 #include "qapi/qmp/qerror.h"
17 #include "qapi/visitor.h"
18 #include "qapi/visitor-impl.h"
20 void visit_start_struct(Visitor *v, void **obj, const char *kind,
21 const char *name, size_t size, Error **errp)
23 v->start_struct(v, obj, kind, name, size, errp);
26 void visit_end_struct(Visitor *v, Error **errp)
28 v->end_struct(v, errp);
31 void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
32 Error **errp)
34 if (v->start_implicit_struct) {
35 v->start_implicit_struct(v, obj, size, errp);
39 void visit_end_implicit_struct(Visitor *v, Error **errp)
41 if (v->end_implicit_struct) {
42 v->end_implicit_struct(v, errp);
46 void visit_start_list(Visitor *v, const char *name, Error **errp)
48 v->start_list(v, name, errp);
51 GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
53 return v->next_list(v, list, errp);
56 void visit_end_list(Visitor *v, Error **errp)
58 v->end_list(v, errp);
61 bool visit_start_union(Visitor *v, bool data_present, Error **errp)
63 if (v->start_union) {
64 return v->start_union(v, data_present, errp);
66 return true;
69 void visit_end_union(Visitor *v, bool data_present, Error **errp)
71 if (v->end_union) {
72 v->end_union(v, data_present, errp);
76 void visit_optional(Visitor *v, bool *present, const char *name,
77 Error **errp)
79 if (v->optional) {
80 v->optional(v, present, name, errp);
84 void visit_get_next_type(Visitor *v, int *obj, const int *qtypes,
85 const char *name, Error **errp)
87 if (v->get_next_type) {
88 v->get_next_type(v, obj, qtypes, name, errp);
92 void visit_type_enum(Visitor *v, int *obj, const char *strings[],
93 const char *kind, const char *name, Error **errp)
95 v->type_enum(v, obj, strings, kind, name, errp);
98 void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
100 v->type_int(v, obj, name, errp);
103 void visit_type_uint8(Visitor *v, uint8_t *obj, const char *name, Error **errp)
105 int64_t value;
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) {
113 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
114 "uint8_t");
115 return;
117 *obj = value;
121 void visit_type_uint16(Visitor *v, uint16_t *obj, const char *name, Error **errp)
123 int64_t value;
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) {
131 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
132 "uint16_t");
133 return;
135 *obj = value;
139 void visit_type_uint32(Visitor *v, uint32_t *obj, const char *name, Error **errp)
141 int64_t value;
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) {
149 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
150 "uint32_t");
151 return;
153 *obj = value;
157 void visit_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
159 int64_t value;
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;
170 void visit_type_int8(Visitor *v, int8_t *obj, const char *name, Error **errp)
172 int64_t value;
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) {
180 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
181 "int8_t");
182 return;
184 *obj = value;
188 void visit_type_int16(Visitor *v, int16_t *obj, const char *name, Error **errp)
190 int64_t value;
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) {
198 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
199 "int16_t");
200 return;
202 *obj = value;
206 void visit_type_int32(Visitor *v, int32_t *obj, const char *name, Error **errp)
208 int64_t value;
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) {
216 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
217 "int32_t");
218 return;
220 *obj = value;
224 void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp)
226 if (v->type_int64) {
227 v->type_int64(v, obj, name, errp);
228 } else {
229 v->type_int(v, obj, name, errp);
233 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
235 int64_t value;
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;
248 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
250 v->type_bool(v, obj, name, errp);
253 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
255 v->type_str(v, obj, name, errp);
258 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
260 v->type_number(v, obj, name, errp);
263 void output_type_enum(Visitor *v, int *obj, const char *strings[],
264 const char *kind, const char *name,
265 Error **errp)
267 int i = 0;
268 int value = *obj;
269 char *enum_str;
271 assert(strings);
272 while (strings[i++] != NULL);
273 if (value < 0 || value >= i - 1) {
274 error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
275 return;
278 enum_str = (char *)strings[value];
279 visit_type_str(v, &enum_str, name, errp);
282 void input_type_enum(Visitor *v, int *obj, const char *strings[],
283 const char *kind, const char *name,
284 Error **errp)
286 Error *local_err = NULL;
287 int64_t value = 0;
288 char *enum_str;
290 assert(strings);
292 visit_type_str(v, &enum_str, name, &local_err);
293 if (local_err) {
294 error_propagate(errp, local_err);
295 return;
298 while (strings[value] != NULL) {
299 if (strcmp(strings[value], enum_str) == 0) {
300 break;
302 value++;
305 if (strings[value] == NULL) {
306 error_set(errp, QERR_INVALID_PARAMETER, enum_str);
307 g_free(enum_str);
308 return;
311 g_free(enum_str);
312 *obj = value;