fw_cfg: remove offset argument from callback prototype
[qemu/rayw.git] / qapi / qapi-visit-core.c
blob59ed5067faffee9488515cb0f2f6fc6eec6bff5d
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 * const 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_setg(errp, QERR_INVALID_PARAMETER_VALUE,
114 name ? name : "null", "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_setg(errp, QERR_INVALID_PARAMETER_VALUE,
132 name ? name : "null", "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_setg(errp, QERR_INVALID_PARAMETER_VALUE,
150 name ? name : "null", "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_setg(errp, QERR_INVALID_PARAMETER_VALUE,
181 name ? name : "null", "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_setg(errp, QERR_INVALID_PARAMETER_VALUE,
199 name ? name : "null", "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_setg(errp, QERR_INVALID_PARAMETER_VALUE,
217 name ? name : "null", "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 visit_type_any(Visitor *v, QObject **obj, const char *name,
264 Error **errp)
266 v->type_any(v, obj, name, errp);
269 void output_type_enum(Visitor *v, int *obj, const char * const strings[],
270 const char *kind, const char *name,
271 Error **errp)
273 int i = 0;
274 int value = *obj;
275 char *enum_str;
277 assert(strings);
278 while (strings[i++] != NULL);
279 if (value < 0 || value >= i - 1) {
280 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
281 return;
284 enum_str = (char *)strings[value];
285 visit_type_str(v, &enum_str, name, errp);
288 void input_type_enum(Visitor *v, int *obj, const char * const strings[],
289 const char *kind, const char *name,
290 Error **errp)
292 Error *local_err = NULL;
293 int64_t value = 0;
294 char *enum_str;
296 assert(strings);
298 visit_type_str(v, &enum_str, name, &local_err);
299 if (local_err) {
300 error_propagate(errp, local_err);
301 return;
304 while (strings[value] != NULL) {
305 if (strcmp(strings[value], enum_str) == 0) {
306 break;
308 value++;
311 if (strings[value] == NULL) {
312 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
313 g_free(enum_str);
314 return;
317 g_free(enum_str);
318 *obj = value;