Statistics
| Branch: | Revision:

root / qapi / qapi-visit-core.c @ 25a70175

History | View | Annotate | Download (8.8 kB)

1 2345c77c Michael Roth
/*
2 2345c77c Michael Roth
 * Core Definitions for QAPI Visitor Classes
3 2345c77c Michael Roth
 *
4 2345c77c Michael Roth
 * Copyright IBM, Corp. 2011
5 2345c77c Michael Roth
 *
6 2345c77c Michael Roth
 * Authors:
7 2345c77c Michael Roth
 *  Anthony Liguori   <aliguori@us.ibm.com>
8 2345c77c Michael Roth
 *
9 2345c77c Michael Roth
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 2345c77c Michael Roth
 * See the COPYING.LIB file in the top-level directory.
11 2345c77c Michael Roth
 *
12 2345c77c Michael Roth
 */
13 2345c77c Michael Roth
14 79ee7df8 Paolo Bonzini
#include "qemu-common.h"
15 69dd62df Kevin Wolf
#include "qapi/qmp/qobject.h"
16 7b1b5d19 Paolo Bonzini
#include "qapi/qmp/qerror.h"
17 7b1b5d19 Paolo Bonzini
#include "qapi/visitor.h"
18 7b1b5d19 Paolo Bonzini
#include "qapi/visitor-impl.h"
19 2345c77c Michael Roth
20 2345c77c Michael Roth
void visit_start_handle(Visitor *v, void **obj, const char *kind,
21 2345c77c Michael Roth
                        const char *name, Error **errp)
22 2345c77c Michael Roth
{
23 2345c77c Michael Roth
    if (!error_is_set(errp) && v->start_handle) {
24 2345c77c Michael Roth
        v->start_handle(v, obj, kind, name, errp);
25 2345c77c Michael Roth
    }
26 2345c77c Michael Roth
}
27 2345c77c Michael Roth
28 2345c77c Michael Roth
void visit_end_handle(Visitor *v, Error **errp)
29 2345c77c Michael Roth
{
30 2345c77c Michael Roth
    if (!error_is_set(errp) && v->end_handle) {
31 2345c77c Michael Roth
        v->end_handle(v, errp);
32 2345c77c Michael Roth
    }
33 2345c77c Michael Roth
}
34 2345c77c Michael Roth
35 2345c77c Michael Roth
void visit_start_struct(Visitor *v, void **obj, const char *kind,
36 2345c77c Michael Roth
                        const char *name, size_t size, Error **errp)
37 2345c77c Michael Roth
{
38 2345c77c Michael Roth
    if (!error_is_set(errp)) {
39 2345c77c Michael Roth
        v->start_struct(v, obj, kind, name, size, errp);
40 2345c77c Michael Roth
    }
41 2345c77c Michael Roth
}
42 2345c77c Michael Roth
43 2345c77c Michael Roth
void visit_end_struct(Visitor *v, Error **errp)
44 2345c77c Michael Roth
{
45 d195325b Paolo Bonzini
    assert(!error_is_set(errp));
46 d195325b Paolo Bonzini
    v->end_struct(v, errp);
47 2345c77c Michael Roth
}
48 2345c77c Michael Roth
49 761d524d Kevin Wolf
void visit_start_implicit_struct(Visitor *v, void **obj, size_t size,
50 761d524d Kevin Wolf
                                 Error **errp)
51 761d524d Kevin Wolf
{
52 761d524d Kevin Wolf
    if (!error_is_set(errp) && v->start_implicit_struct) {
53 761d524d Kevin Wolf
        v->start_implicit_struct(v, obj, size, errp);
54 761d524d Kevin Wolf
    }
55 761d524d Kevin Wolf
}
56 761d524d Kevin Wolf
57 761d524d Kevin Wolf
void visit_end_implicit_struct(Visitor *v, Error **errp)
58 761d524d Kevin Wolf
{
59 761d524d Kevin Wolf
    assert(!error_is_set(errp));
60 761d524d Kevin Wolf
    if (v->end_implicit_struct) {
61 761d524d Kevin Wolf
        v->end_implicit_struct(v, errp);
62 761d524d Kevin Wolf
    }
63 761d524d Kevin Wolf
}
64 761d524d Kevin Wolf
65 2345c77c Michael Roth
void visit_start_list(Visitor *v, const char *name, Error **errp)
66 2345c77c Michael Roth
{
67 2345c77c Michael Roth
    if (!error_is_set(errp)) {
68 2345c77c Michael Roth
        v->start_list(v, name, errp);
69 2345c77c Michael Roth
    }
70 2345c77c Michael Roth
}
71 2345c77c Michael Roth
72 2345c77c Michael Roth
GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
73 2345c77c Michael Roth
{
74 2345c77c Michael Roth
    if (!error_is_set(errp)) {
75 2345c77c Michael Roth
        return v->next_list(v, list, errp);
76 2345c77c Michael Roth
    }
77 2345c77c Michael Roth
78 2345c77c Michael Roth
    return 0;
79 2345c77c Michael Roth
}
80 2345c77c Michael Roth
81 2345c77c Michael Roth
void visit_end_list(Visitor *v, Error **errp)
82 2345c77c Michael Roth
{
83 d195325b Paolo Bonzini
    assert(!error_is_set(errp));
84 d195325b Paolo Bonzini
    v->end_list(v, errp);
85 2345c77c Michael Roth
}
86 2345c77c Michael Roth
87 2345c77c Michael Roth
void visit_start_optional(Visitor *v, bool *present, const char *name,
88 2345c77c Michael Roth
                          Error **errp)
89 2345c77c Michael Roth
{
90 2345c77c Michael Roth
    if (!error_is_set(errp) && v->start_optional) {
91 2345c77c Michael Roth
        v->start_optional(v, present, name, errp);
92 2345c77c Michael Roth
    }
93 2345c77c Michael Roth
}
94 2345c77c Michael Roth
95 2345c77c Michael Roth
void visit_end_optional(Visitor *v, Error **errp)
96 2345c77c Michael Roth
{
97 2345c77c Michael Roth
    if (!error_is_set(errp) && v->end_optional) {
98 2345c77c Michael Roth
        v->end_optional(v, errp);
99 2345c77c Michael Roth
    }
100 2345c77c Michael Roth
}
101 2345c77c Michael Roth
102 69dd62df Kevin Wolf
void visit_get_next_type(Visitor *v, int *obj, const int *qtypes,
103 69dd62df Kevin Wolf
                         const char *name, Error **errp)
104 69dd62df Kevin Wolf
{
105 69dd62df Kevin Wolf
    if (!error_is_set(errp) && v->get_next_type) {
106 69dd62df Kevin Wolf
        v->get_next_type(v, obj, qtypes, name, errp);
107 69dd62df Kevin Wolf
    }
108 69dd62df Kevin Wolf
}
109 69dd62df Kevin Wolf
110 2345c77c Michael Roth
void visit_type_enum(Visitor *v, int *obj, const char *strings[],
111 2345c77c Michael Roth
                     const char *kind, const char *name, Error **errp)
112 2345c77c Michael Roth
{
113 2345c77c Michael Roth
    if (!error_is_set(errp)) {
114 2345c77c Michael Roth
        v->type_enum(v, obj, strings, kind, name, errp);
115 2345c77c Michael Roth
    }
116 2345c77c Michael Roth
}
117 2345c77c Michael Roth
118 2345c77c Michael Roth
void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
119 2345c77c Michael Roth
{
120 2345c77c Michael Roth
    if (!error_is_set(errp)) {
121 2345c77c Michael Roth
        v->type_int(v, obj, name, errp);
122 2345c77c Michael Roth
    }
123 2345c77c Michael Roth
}
124 2345c77c Michael Roth
125 4e27e819 Michael Roth
void visit_type_uint8(Visitor *v, uint8_t *obj, const char *name, Error **errp)
126 4e27e819 Michael Roth
{
127 4e27e819 Michael Roth
    int64_t value;
128 4e27e819 Michael Roth
    if (!error_is_set(errp)) {
129 4e27e819 Michael Roth
        if (v->type_uint8) {
130 4e27e819 Michael Roth
            v->type_uint8(v, obj, name, errp);
131 4e27e819 Michael Roth
        } else {
132 4e27e819 Michael Roth
            value = *obj;
133 4e27e819 Michael Roth
            v->type_int(v, &value, name, errp);
134 4e27e819 Michael Roth
            if (value < 0 || value > UINT8_MAX) {
135 4e27e819 Michael Roth
                error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
136 4e27e819 Michael Roth
                          "uint8_t");
137 4e27e819 Michael Roth
                return;
138 4e27e819 Michael Roth
            }
139 4e27e819 Michael Roth
            *obj = value;
140 4e27e819 Michael Roth
        }
141 4e27e819 Michael Roth
    }
142 4e27e819 Michael Roth
}
143 4e27e819 Michael Roth
144 4e27e819 Michael Roth
void visit_type_uint16(Visitor *v, uint16_t *obj, const char *name, Error **errp)
145 4e27e819 Michael Roth
{
146 4e27e819 Michael Roth
    int64_t value;
147 4e27e819 Michael Roth
    if (!error_is_set(errp)) {
148 4e27e819 Michael Roth
        if (v->type_uint16) {
149 4e27e819 Michael Roth
            v->type_uint16(v, obj, name, errp);
150 4e27e819 Michael Roth
        } else {
151 4e27e819 Michael Roth
            value = *obj;
152 4e27e819 Michael Roth
            v->type_int(v, &value, name, errp);
153 4e27e819 Michael Roth
            if (value < 0 || value > UINT16_MAX) {
154 4e27e819 Michael Roth
                error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
155 4e27e819 Michael Roth
                          "uint16_t");
156 4e27e819 Michael Roth
                return;
157 4e27e819 Michael Roth
            }
158 4e27e819 Michael Roth
            *obj = value;
159 4e27e819 Michael Roth
        }
160 4e27e819 Michael Roth
    }
161 4e27e819 Michael Roth
}
162 4e27e819 Michael Roth
163 4e27e819 Michael Roth
void visit_type_uint32(Visitor *v, uint32_t *obj, const char *name, Error **errp)
164 4e27e819 Michael Roth
{
165 4e27e819 Michael Roth
    int64_t value;
166 4e27e819 Michael Roth
    if (!error_is_set(errp)) {
167 4e27e819 Michael Roth
        if (v->type_uint32) {
168 4e27e819 Michael Roth
            v->type_uint32(v, obj, name, errp);
169 4e27e819 Michael Roth
        } else {
170 4e27e819 Michael Roth
            value = *obj;
171 4e27e819 Michael Roth
            v->type_int(v, &value, name, errp);
172 4e27e819 Michael Roth
            if (value < 0 || value > UINT32_MAX) {
173 4e27e819 Michael Roth
                error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
174 4e27e819 Michael Roth
                          "uint32_t");
175 4e27e819 Michael Roth
                return;
176 4e27e819 Michael Roth
            }
177 4e27e819 Michael Roth
            *obj = value;
178 4e27e819 Michael Roth
        }
179 4e27e819 Michael Roth
    }
180 4e27e819 Michael Roth
}
181 4e27e819 Michael Roth
182 4e27e819 Michael Roth
void visit_type_uint64(Visitor *v, uint64_t *obj, const char *name, Error **errp)
183 4e27e819 Michael Roth
{
184 4e27e819 Michael Roth
    int64_t value;
185 4e27e819 Michael Roth
    if (!error_is_set(errp)) {
186 4e27e819 Michael Roth
        if (v->type_uint64) {
187 4e27e819 Michael Roth
            v->type_uint64(v, obj, name, errp);
188 4e27e819 Michael Roth
        } else {
189 4e27e819 Michael Roth
            value = *obj;
190 4e27e819 Michael Roth
            v->type_int(v, &value, name, errp);
191 4e27e819 Michael Roth
            *obj = value;
192 4e27e819 Michael Roth
        }
193 4e27e819 Michael Roth
    }
194 4e27e819 Michael Roth
}
195 4e27e819 Michael Roth
196 4e27e819 Michael Roth
void visit_type_int8(Visitor *v, int8_t *obj, const char *name, Error **errp)
197 4e27e819 Michael Roth
{
198 4e27e819 Michael Roth
    int64_t value;
199 4e27e819 Michael Roth
    if (!error_is_set(errp)) {
200 4e27e819 Michael Roth
        if (v->type_int8) {
201 4e27e819 Michael Roth
            v->type_int8(v, obj, name, errp);
202 4e27e819 Michael Roth
        } else {
203 4e27e819 Michael Roth
            value = *obj;
204 4e27e819 Michael Roth
            v->type_int(v, &value, name, errp);
205 4e27e819 Michael Roth
            if (value < INT8_MIN || value > INT8_MAX) {
206 4e27e819 Michael Roth
                error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
207 4e27e819 Michael Roth
                          "int8_t");
208 4e27e819 Michael Roth
                return;
209 4e27e819 Michael Roth
            }
210 4e27e819 Michael Roth
            *obj = value;
211 4e27e819 Michael Roth
        }
212 4e27e819 Michael Roth
    }
213 4e27e819 Michael Roth
}
214 4e27e819 Michael Roth
215 4e27e819 Michael Roth
void visit_type_int16(Visitor *v, int16_t *obj, const char *name, Error **errp)
216 4e27e819 Michael Roth
{
217 4e27e819 Michael Roth
    int64_t value;
218 4e27e819 Michael Roth
    if (!error_is_set(errp)) {
219 4e27e819 Michael Roth
        if (v->type_int16) {
220 4e27e819 Michael Roth
            v->type_int16(v, obj, name, errp);
221 4e27e819 Michael Roth
        } else {
222 4e27e819 Michael Roth
            value = *obj;
223 4e27e819 Michael Roth
            v->type_int(v, &value, name, errp);
224 4e27e819 Michael Roth
            if (value < INT16_MIN || value > INT16_MAX) {
225 4e27e819 Michael Roth
                error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
226 4e27e819 Michael Roth
                          "int16_t");
227 4e27e819 Michael Roth
                return;
228 4e27e819 Michael Roth
            }
229 4e27e819 Michael Roth
            *obj = value;
230 4e27e819 Michael Roth
        }
231 4e27e819 Michael Roth
    }
232 4e27e819 Michael Roth
}
233 4e27e819 Michael Roth
234 4e27e819 Michael Roth
void visit_type_int32(Visitor *v, int32_t *obj, const char *name, Error **errp)
235 4e27e819 Michael Roth
{
236 4e27e819 Michael Roth
    int64_t value;
237 4e27e819 Michael Roth
    if (!error_is_set(errp)) {
238 4e27e819 Michael Roth
        if (v->type_int32) {
239 4e27e819 Michael Roth
            v->type_int32(v, obj, name, errp);
240 4e27e819 Michael Roth
        } else {
241 4e27e819 Michael Roth
            value = *obj;
242 4e27e819 Michael Roth
            v->type_int(v, &value, name, errp);
243 4e27e819 Michael Roth
            if (value < INT32_MIN || value > INT32_MAX) {
244 4e27e819 Michael Roth
                error_set(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null",
245 4e27e819 Michael Roth
                          "int32_t");
246 4e27e819 Michael Roth
                return;
247 4e27e819 Michael Roth
            }
248 4e27e819 Michael Roth
            *obj = value;
249 4e27e819 Michael Roth
        }
250 4e27e819 Michael Roth
    }
251 4e27e819 Michael Roth
}
252 4e27e819 Michael Roth
253 4e27e819 Michael Roth
void visit_type_int64(Visitor *v, int64_t *obj, const char *name, Error **errp)
254 4e27e819 Michael Roth
{
255 4e27e819 Michael Roth
    if (!error_is_set(errp)) {
256 4e27e819 Michael Roth
        if (v->type_int64) {
257 4e27e819 Michael Roth
            v->type_int64(v, obj, name, errp);
258 4e27e819 Michael Roth
        } else {
259 4e27e819 Michael Roth
            v->type_int(v, obj, name, errp);
260 4e27e819 Michael Roth
        }
261 4e27e819 Michael Roth
    }
262 4e27e819 Michael Roth
}
263 4e27e819 Michael Roth
264 092705d4 Laszlo Ersek
void visit_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
265 092705d4 Laszlo Ersek
{
266 b8877962 Vasilis Liaskovitis
    int64_t value;
267 092705d4 Laszlo Ersek
    if (!error_is_set(errp)) {
268 b8877962 Vasilis Liaskovitis
        if (v->type_size) {
269 b8877962 Vasilis Liaskovitis
            v->type_size(v, obj, name, errp);
270 b8877962 Vasilis Liaskovitis
        } else if (v->type_uint64) {
271 b8877962 Vasilis Liaskovitis
            v->type_uint64(v, obj, name, errp);
272 b8877962 Vasilis Liaskovitis
        } else {
273 b8877962 Vasilis Liaskovitis
            value = *obj;
274 b8877962 Vasilis Liaskovitis
            v->type_int(v, &value, name, errp);
275 b8877962 Vasilis Liaskovitis
            *obj = value;
276 b8877962 Vasilis Liaskovitis
        }
277 092705d4 Laszlo Ersek
    }
278 092705d4 Laszlo Ersek
}
279 092705d4 Laszlo Ersek
280 2345c77c Michael Roth
void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
281 2345c77c Michael Roth
{
282 2345c77c Michael Roth
    if (!error_is_set(errp)) {
283 2345c77c Michael Roth
        v->type_bool(v, obj, name, errp);
284 2345c77c Michael Roth
    }
285 2345c77c Michael Roth
}
286 2345c77c Michael Roth
287 2345c77c Michael Roth
void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
288 2345c77c Michael Roth
{
289 2345c77c Michael Roth
    if (!error_is_set(errp)) {
290 2345c77c Michael Roth
        v->type_str(v, obj, name, errp);
291 2345c77c Michael Roth
    }
292 2345c77c Michael Roth
}
293 2345c77c Michael Roth
294 2345c77c Michael Roth
void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
295 2345c77c Michael Roth
{
296 2345c77c Michael Roth
    if (!error_is_set(errp)) {
297 2345c77c Michael Roth
        v->type_number(v, obj, name, errp);
298 2345c77c Michael Roth
    }
299 2345c77c Michael Roth
}
300 0f71a1e0 Paolo Bonzini
301 0f71a1e0 Paolo Bonzini
void output_type_enum(Visitor *v, int *obj, const char *strings[],
302 0f71a1e0 Paolo Bonzini
                      const char *kind, const char *name,
303 0f71a1e0 Paolo Bonzini
                      Error **errp)
304 0f71a1e0 Paolo Bonzini
{
305 0f71a1e0 Paolo Bonzini
    int i = 0;
306 0f71a1e0 Paolo Bonzini
    int value = *obj;
307 0f71a1e0 Paolo Bonzini
    char *enum_str;
308 0f71a1e0 Paolo Bonzini
309 0f71a1e0 Paolo Bonzini
    assert(strings);
310 0f71a1e0 Paolo Bonzini
    while (strings[i++] != NULL);
311 0f71a1e0 Paolo Bonzini
    if (value < 0 || value >= i - 1) {
312 0f71a1e0 Paolo Bonzini
        error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
313 0f71a1e0 Paolo Bonzini
        return;
314 0f71a1e0 Paolo Bonzini
    }
315 0f71a1e0 Paolo Bonzini
316 0f71a1e0 Paolo Bonzini
    enum_str = (char *)strings[value];
317 0f71a1e0 Paolo Bonzini
    visit_type_str(v, &enum_str, name, errp);
318 0f71a1e0 Paolo Bonzini
}
319 0f71a1e0 Paolo Bonzini
320 0f71a1e0 Paolo Bonzini
void input_type_enum(Visitor *v, int *obj, const char *strings[],
321 0f71a1e0 Paolo Bonzini
                     const char *kind, const char *name,
322 0f71a1e0 Paolo Bonzini
                     Error **errp)
323 0f71a1e0 Paolo Bonzini
{
324 0f71a1e0 Paolo Bonzini
    int64_t value = 0;
325 0f71a1e0 Paolo Bonzini
    char *enum_str;
326 0f71a1e0 Paolo Bonzini
327 0f71a1e0 Paolo Bonzini
    assert(strings);
328 0f71a1e0 Paolo Bonzini
329 0f71a1e0 Paolo Bonzini
    visit_type_str(v, &enum_str, name, errp);
330 0f71a1e0 Paolo Bonzini
    if (error_is_set(errp)) {
331 0f71a1e0 Paolo Bonzini
        return;
332 0f71a1e0 Paolo Bonzini
    }
333 0f71a1e0 Paolo Bonzini
334 0f71a1e0 Paolo Bonzini
    while (strings[value] != NULL) {
335 0f71a1e0 Paolo Bonzini
        if (strcmp(strings[value], enum_str) == 0) {
336 0f71a1e0 Paolo Bonzini
            break;
337 0f71a1e0 Paolo Bonzini
        }
338 0f71a1e0 Paolo Bonzini
        value++;
339 0f71a1e0 Paolo Bonzini
    }
340 0f71a1e0 Paolo Bonzini
341 0f71a1e0 Paolo Bonzini
    if (strings[value] == NULL) {
342 94c3db85 Luiz Capitulino
        error_set(errp, QERR_INVALID_PARAMETER, enum_str);
343 0f71a1e0 Paolo Bonzini
        g_free(enum_str);
344 0f71a1e0 Paolo Bonzini
        return;
345 0f71a1e0 Paolo Bonzini
    }
346 0f71a1e0 Paolo Bonzini
347 0f71a1e0 Paolo Bonzini
    g_free(enum_str);
348 0f71a1e0 Paolo Bonzini
    *obj = value;
349 0f71a1e0 Paolo Bonzini
}