MAINTAINERS: Block layer core, qcow2 and blkdebug
[qemu/ar7.git] / qapi / qapi-visit-core.c
blobfa680c9991d686b313a409d112b7819cb170996b
1 /*
2 * Core Definitions for QAPI Visitor Classes
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu-common.h"
18 #include "qapi/qmp/qobject.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qapi/visitor.h"
21 #include "qapi/visitor-impl.h"
23 void visit_start_struct(Visitor *v, const char *name, void **obj,
24 size_t size, Error **errp)
26 v->start_struct(v, name, obj, size, errp);
29 void visit_end_struct(Visitor *v, Error **errp)
31 v->end_struct(v, errp);
34 void visit_start_list(Visitor *v, const char *name, Error **errp)
36 v->start_list(v, name, errp);
39 GenericList *visit_next_list(Visitor *v, GenericList **list, size_t size)
41 assert(list && size >= sizeof(GenericList));
42 return v->next_list(v, list, size);
45 void visit_end_list(Visitor *v)
47 v->end_list(v);
50 void visit_start_alternate(Visitor *v, const char *name,
51 GenericAlternate **obj, size_t size,
52 bool promote_int, Error **errp)
54 assert(obj && size >= sizeof(GenericAlternate));
55 if (v->start_alternate) {
56 v->start_alternate(v, name, obj, size, promote_int, errp);
60 void visit_end_alternate(Visitor *v)
62 if (v->end_alternate) {
63 v->end_alternate(v);
67 bool visit_optional(Visitor *v, const char *name, bool *present)
69 if (v->optional) {
70 v->optional(v, name, present);
72 return *present;
75 void visit_type_enum(Visitor *v, const char *name, int *obj,
76 const char *const strings[], Error **errp)
78 v->type_enum(v, name, obj, strings, errp);
81 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
83 v->type_int64(v, name, obj, errp);
86 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
87 uint64_t max, const char *type, Error **errp)
89 Error *err = NULL;
90 uint64_t value = *obj;
92 v->type_uint64(v, name, &value, &err);
93 if (err) {
94 error_propagate(errp, err);
95 } else if (value > max) {
96 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
97 name ? name : "null", type);
98 } else {
99 *obj = value;
103 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
104 Error **errp)
106 uint64_t value = *obj;
107 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
108 *obj = value;
111 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
112 Error **errp)
114 uint64_t value = *obj;
115 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
116 *obj = value;
119 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
120 Error **errp)
122 uint64_t value = *obj;
123 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
124 *obj = value;
127 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
128 Error **errp)
130 v->type_uint64(v, name, obj, errp);
133 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name,
134 int64_t min, int64_t max, const char *type,
135 Error **errp)
137 Error *err = NULL;
138 int64_t value = *obj;
140 v->type_int64(v, name, &value, &err);
141 if (err) {
142 error_propagate(errp, err);
143 } else if (value < min || value > max) {
144 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
145 name ? name : "null", type);
146 } else {
147 *obj = value;
151 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
153 int64_t value = *obj;
154 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
155 *obj = value;
158 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
159 Error **errp)
161 int64_t value = *obj;
162 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
163 *obj = value;
166 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
167 Error **errp)
169 int64_t value = *obj;
170 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
171 *obj = value;
174 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
175 Error **errp)
177 v->type_int64(v, name, obj, errp);
180 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
181 Error **errp)
183 if (v->type_size) {
184 v->type_size(v, name, obj, errp);
185 } else {
186 v->type_uint64(v, name, obj, errp);
190 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
192 v->type_bool(v, name, obj, errp);
195 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
197 v->type_str(v, name, obj, errp);
200 void visit_type_number(Visitor *v, const char *name, double *obj,
201 Error **errp)
203 v->type_number(v, name, obj, errp);
206 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
208 v->type_any(v, name, obj, errp);
211 void output_type_enum(Visitor *v, const char *name, int *obj,
212 const char *const strings[], Error **errp)
214 int i = 0;
215 int value = *obj;
216 char *enum_str;
218 assert(strings);
219 while (strings[i++] != NULL);
220 if (value < 0 || value >= i - 1) {
221 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
222 return;
225 enum_str = (char *)strings[value];
226 visit_type_str(v, name, &enum_str, errp);
229 void input_type_enum(Visitor *v, const char *name, int *obj,
230 const char *const strings[], Error **errp)
232 Error *local_err = NULL;
233 int64_t value = 0;
234 char *enum_str;
236 assert(strings);
238 visit_type_str(v, name, &enum_str, &local_err);
239 if (local_err) {
240 error_propagate(errp, local_err);
241 return;
244 while (strings[value] != NULL) {
245 if (strcmp(strings[value], enum_str) == 0) {
246 break;
248 value++;
251 if (strings[value] == NULL) {
252 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
253 g_free(enum_str);
254 return;
257 g_free(enum_str);
258 *obj = value;