create new function: qemu_opt_set_number
[qemu.git] / qapi / qapi-visit-core.c
blob7a82b63766410ce8c78e66fd8e4c633d113d7f62
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 "qapi/qapi-visit-core.h"
15 #include "qapi/qapi-visit-impl.h"
17 void visit_start_handle(Visitor *v, void **obj, const char *kind,
18 const char *name, Error **errp)
20 if (!error_is_set(errp) && v->start_handle) {
21 v->start_handle(v, obj, kind, name, errp);
25 void visit_end_handle(Visitor *v, Error **errp)
27 if (!error_is_set(errp) && v->end_handle) {
28 v->end_handle(v, errp);
32 void visit_start_struct(Visitor *v, void **obj, const char *kind,
33 const char *name, size_t size, Error **errp)
35 if (!error_is_set(errp)) {
36 v->start_struct(v, obj, kind, name, size, errp);
40 void visit_end_struct(Visitor *v, Error **errp)
42 assert(!error_is_set(errp));
43 v->end_struct(v, errp);
46 void visit_start_list(Visitor *v, const char *name, Error **errp)
48 if (!error_is_set(errp)) {
49 v->start_list(v, name, errp);
53 GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
55 if (!error_is_set(errp)) {
56 return v->next_list(v, list, errp);
59 return 0;
62 void visit_end_list(Visitor *v, Error **errp)
64 assert(!error_is_set(errp));
65 v->end_list(v, errp);
68 void visit_start_optional(Visitor *v, bool *present, const char *name,
69 Error **errp)
71 if (!error_is_set(errp) && v->start_optional) {
72 v->start_optional(v, present, name, errp);
76 void visit_end_optional(Visitor *v, Error **errp)
78 if (!error_is_set(errp) && v->end_optional) {
79 v->end_optional(v, errp);
83 void visit_type_enum(Visitor *v, int *obj, const char *strings[],
84 const char *kind, const char *name, Error **errp)
86 if (!error_is_set(errp)) {
87 v->type_enum(v, obj, strings, kind, name, errp);
91 void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
93 if (!error_is_set(errp)) {
94 v->type_int(v, obj, name, errp);
98 void visit_type_uint8(Visitor *v, uint8_t *obj, const char *name, Error **errp)
100 int64_t value;
101 if (!error_is_set(errp)) {
102 if (v->type_uint8) {
103 v->type_uint8(v, obj, name, errp);
104 } else {
105 value = *obj;
106 v->type_int(v, &value, name, errp);
107 if (value < 0 || value > UINT8_MAX) {
108 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
109 "uint8_t");
110 return;
112 *obj = value;
117 void visit_type_uint16(Visitor *v, uint16_t *obj, const char *name, Error **errp)
119 int64_t value;
120 if (!error_is_set(errp)) {
121 if (v->type_uint16) {
122 v->type_uint16(v, obj, name, errp);
123 } else {
124 value = *obj;
125 v->type_int(v, &value, name, errp);
126 if (value < 0 || value > UINT16_MAX) {
127 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
128 "uint16_t");
129 return;
131 *obj = value;
136 void visit_type_uint32(Visitor *v, uint32_t *obj, const char *name, Error **errp)
138 int64_t value;
139 if (!error_is_set(errp)) {
140 if (v->type_uint32) {
141 v->type_uint32(v, obj, name, errp);
142 } else {
143 value = *obj;
144 v->type_int(v, &value, name, errp);
145 if (value < 0 || value > UINT32_MAX) {
146 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
147 "uint32_t");
148 return;
150 *obj = value;
155 void visit_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
157 int64_t value;
158 if (!error_is_set(errp)) {
159 if (v->type_uint64) {
160 v->type_uint64(v, obj, name, errp);
161 } else {
162 value = *obj;
163 v->type_int(v, &value, name, errp);
164 *obj = value;
169 void visit_type_int8(Visitor *v, int8_t *obj, const char *name, Error **errp)
171 int64_t value;
172 if (!error_is_set(errp)) {
173 if (v->type_int8) {
174 v->type_int8(v, obj, name, errp);
175 } else {
176 value = *obj;
177 v->type_int(v, &value, name, errp);
178 if (value < INT8_MIN || value > INT8_MAX) {
179 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
180 "int8_t");
181 return;
183 *obj = value;
188 void visit_type_int16(Visitor *v, int16_t *obj, const char *name, Error **errp)
190 int64_t value;
191 if (!error_is_set(errp)) {
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;
207 void visit_type_int32(Visitor *v, int32_t *obj, const char *name, Error **errp)
209 int64_t value;
210 if (!error_is_set(errp)) {
211 if (v->type_int32) {
212 v->type_int32(v, obj, name, errp);
213 } else {
214 value = *obj;
215 v->type_int(v, &value, name, errp);
216 if (value < INT32_MIN || value > INT32_MAX) {
217 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
218 "int32_t");
219 return;
221 *obj = value;
226 void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp)
228 if (!error_is_set(errp)) {
229 if (v->type_int64) {
230 v->type_int64(v, obj, name, errp);
231 } else {
232 v->type_int(v, obj, name, errp);
237 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
239 if (!error_is_set(errp)) {
240 (v->type_size ? v->type_size : v->type_uint64)(v, obj, name, errp);
244 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
246 if (!error_is_set(errp)) {
247 v->type_bool(v, obj, name, errp);
251 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
253 if (!error_is_set(errp)) {
254 v->type_str(v, obj, name, errp);
258 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
260 if (!error_is_set(errp)) {
261 v->type_number(v, obj, name, errp);
265 void output_type_enum(Visitor *v, int *obj, const char *strings[],
266 const char *kind, const char *name,
267 Error **errp)
269 int i = 0;
270 int value = *obj;
271 char *enum_str;
273 assert(strings);
274 while (strings[i++] != NULL);
275 if (value < 0 || value >= i - 1) {
276 error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
277 return;
280 enum_str = (char *)strings[value];
281 visit_type_str(v, &enum_str, name, errp);
284 void input_type_enum(Visitor *v, int *obj, const char *strings[],
285 const char *kind, const char *name,
286 Error **errp)
288 int64_t value = 0;
289 char *enum_str;
291 assert(strings);
293 visit_type_str(v, &enum_str, name, errp);
294 if (error_is_set(errp)) {
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;