]> Git Repo - qemu.git/blob - qapi/qapi-visit-core.c
qapi: Add visitor for implicit structs
[qemu.git] / qapi / qapi-visit-core.c
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
14 #include "qemu-common.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qapi/visitor.h"
17 #include "qapi/visitor-impl.h"
18
19 void visit_start_handle(Visitor *v, void **obj, const char *kind,
20                         const char *name, Error **errp)
21 {
22     if (!error_is_set(errp) && v->start_handle) {
23         v->start_handle(v, obj, kind, name, errp);
24     }
25 }
26
27 void visit_end_handle(Visitor *v, Error **errp)
28 {
29     if (!error_is_set(errp) && v->end_handle) {
30         v->end_handle(v, errp);
31     }
32 }
33
34 void visit_start_struct(Visitor *v, void **obj, const char *kind,
35                         const char *name, size_t size, Error **errp)
36 {
37     if (!error_is_set(errp)) {
38         v->start_struct(v, obj, kind, name, size, errp);
39     }
40 }
41
42 void visit_end_struct(Visitor *v, Error **errp)
43 {
44     assert(!error_is_set(errp));
45     v->end_struct(v, errp);
46 }
47
48 void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
49                                  Error **errp)
50 {
51     if (!error_is_set(errp) && v->start_implicit_struct) {
52         v->start_implicit_struct(v, obj, size, errp);
53     }
54 }
55
56 void visit_end_implicit_struct(Visitor *v, Error **errp)
57 {
58     assert(!error_is_set(errp));
59     if (v->end_implicit_struct) {
60         v->end_implicit_struct(v, errp);
61     }
62 }
63
64 void visit_start_list(Visitor *v, const char *name, Error **errp)
65 {
66     if (!error_is_set(errp)) {
67         v->start_list(v, name, errp);
68     }
69 }
70
71 GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
72 {
73     if (!error_is_set(errp)) {
74         return v->next_list(v, list, errp);
75     }
76
77     return 0;
78 }
79
80 void visit_end_list(Visitor *v, Error **errp)
81 {
82     assert(!error_is_set(errp));
83     v->end_list(v, errp);
84 }
85
86 void visit_start_optional(Visitor *v, bool *present, const char *name,
87                           Error **errp)
88 {
89     if (!error_is_set(errp) && v->start_optional) {
90         v->start_optional(v, present, name, errp);
91     }
92 }
93
94 void visit_end_optional(Visitor *v, Error **errp)
95 {
96     if (!error_is_set(errp) && v->end_optional) {
97         v->end_optional(v, errp);
98     }
99 }
100
101 void visit_type_enum(Visitor *v, int *obj, const char *strings[],
102                      const char *kind, const char *name, Error **errp)
103 {
104     if (!error_is_set(errp)) {
105         v->type_enum(v, obj, strings, kind, name, errp);
106     }
107 }
108
109 void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
110 {
111     if (!error_is_set(errp)) {
112         v->type_int(v, obj, name, errp);
113     }
114 }
115
116 void visit_type_uint8(Visitor *v, uint8_t *obj, const char *name, Error **errp)
117 {
118     int64_t value;
119     if (!error_is_set(errp)) {
120         if (v->type_uint8) {
121             v->type_uint8(v, obj, name, errp);
122         } else {
123             value = *obj;
124             v->type_int(v, &value, name, errp);
125             if (value < 0 || value > UINT8_MAX) {
126                 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
127                           "uint8_t");
128                 return;
129             }
130             *obj = value;
131         }
132     }
133 }
134
135 void visit_type_uint16(Visitor *v, uint16_t *obj, const char *name, Error **errp)
136 {
137     int64_t value;
138     if (!error_is_set(errp)) {
139         if (v->type_uint16) {
140             v->type_uint16(v, obj, name, errp);
141         } else {
142             value = *obj;
143             v->type_int(v, &value, name, errp);
144             if (value < 0 || value > UINT16_MAX) {
145                 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
146                           "uint16_t");
147                 return;
148             }
149             *obj = value;
150         }
151     }
152 }
153
154 void visit_type_uint32(Visitor *v, uint32_t *obj, const char *name, Error **errp)
155 {
156     int64_t value;
157     if (!error_is_set(errp)) {
158         if (v->type_uint32) {
159             v->type_uint32(v, obj, name, errp);
160         } else {
161             value = *obj;
162             v->type_int(v, &value, name, errp);
163             if (value < 0 || value > UINT32_MAX) {
164                 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
165                           "uint32_t");
166                 return;
167             }
168             *obj = value;
169         }
170     }
171 }
172
173 void visit_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
174 {
175     int64_t value;
176     if (!error_is_set(errp)) {
177         if (v->type_uint64) {
178             v->type_uint64(v, obj, name, errp);
179         } else {
180             value = *obj;
181             v->type_int(v, &value, name, errp);
182             *obj = value;
183         }
184     }
185 }
186
187 void visit_type_int8(Visitor *v, int8_t *obj, const char *name, Error **errp)
188 {
189     int64_t value;
190     if (!error_is_set(errp)) {
191         if (v->type_int8) {
192             v->type_int8(v, obj, name, errp);
193         } else {
194             value = *obj;
195             v->type_int(v, &value, name, errp);
196             if (value < INT8_MIN || value > INT8_MAX) {
197                 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
198                           "int8_t");
199                 return;
200             }
201             *obj = value;
202         }
203     }
204 }
205
206 void visit_type_int16(Visitor *v, int16_t *obj, const char *name, Error **errp)
207 {
208     int64_t value;
209     if (!error_is_set(errp)) {
210         if (v->type_int16) {
211             v->type_int16(v, obj, name, errp);
212         } else {
213             value = *obj;
214             v->type_int(v, &value, name, errp);
215             if (value < INT16_MIN || value > INT16_MAX) {
216                 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
217                           "int16_t");
218                 return;
219             }
220             *obj = value;
221         }
222     }
223 }
224
225 void visit_type_int32(Visitor *v, int32_t *obj, const char *name, Error **errp)
226 {
227     int64_t value;
228     if (!error_is_set(errp)) {
229         if (v->type_int32) {
230             v->type_int32(v, obj, name, errp);
231         } else {
232             value = *obj;
233             v->type_int(v, &value, name, errp);
234             if (value < INT32_MIN || value > INT32_MAX) {
235                 error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
236                           "int32_t");
237                 return;
238             }
239             *obj = value;
240         }
241     }
242 }
243
244 void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp)
245 {
246     if (!error_is_set(errp)) {
247         if (v->type_int64) {
248             v->type_int64(v, obj, name, errp);
249         } else {
250             v->type_int(v, obj, name, errp);
251         }
252     }
253 }
254
255 void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
256 {
257     if (!error_is_set(errp)) {
258         (v->type_size ? v->type_size : v->type_uint64)(v, obj, name, errp);
259     }
260 }
261
262 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
263 {
264     if (!error_is_set(errp)) {
265         v->type_bool(v, obj, name, errp);
266     }
267 }
268
269 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
270 {
271     if (!error_is_set(errp)) {
272         v->type_str(v, obj, name, errp);
273     }
274 }
275
276 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
277 {
278     if (!error_is_set(errp)) {
279         v->type_number(v, obj, name, errp);
280     }
281 }
282
283 void output_type_enum(Visitor *v, int *obj, const char *strings[],
284                       const char *kind, const char *name,
285                       Error **errp)
286 {
287     int i = 0;
288     int value = *obj;
289     char *enum_str;
290
291     assert(strings);
292     while (strings[i++] != NULL);
293     if (value < 0 || value >= i - 1) {
294         error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
295         return;
296     }
297
298     enum_str = (char *)strings[value];
299     visit_type_str(v, &enum_str, name, errp);
300 }
301
302 void input_type_enum(Visitor *v, int *obj, const char *strings[],
303                      const char *kind, const char *name,
304                      Error **errp)
305 {
306     int64_t value = 0;
307     char *enum_str;
308
309     assert(strings);
310
311     visit_type_str(v, &enum_str, name, errp);
312     if (error_is_set(errp)) {
313         return;
314     }
315
316     while (strings[value] != NULL) {
317         if (strcmp(strings[value], enum_str) == 0) {
318             break;
319         }
320         value++;
321     }
322
323     if (strings[value] == NULL) {
324         error_set(errp, QERR_INVALID_PARAMETER, enum_str);
325         g_free(enum_str);
326         return;
327     }
328
329     g_free(enum_str);
330     *obj = value;
331 }
This page took 0.04582 seconds and 4 git commands to generate.