2 * Core Definitions for QAPI Visitor Classes
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
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)
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) {
67 bool visit_optional(Visitor *v, const char *name, bool *present)
70 v->optional(v, name, 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)
90 uint64_t value = *obj;
92 v->type_uint64(v, name, &value, &err);
94 error_propagate(errp, err);
95 } else if (value > max) {
96 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
97 name ? name : "null", type);
103 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
106 uint64_t value = *obj;
107 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
111 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
114 uint64_t value = *obj;
115 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
119 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
122 uint64_t value = *obj;
123 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
127 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
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,
138 int64_t value = *obj;
140 v->type_int64(v, name, &value, &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);
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);
158 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
161 int64_t value = *obj;
162 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
166 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
169 int64_t value = *obj;
170 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
174 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
177 v->type_int64(v, name, obj, errp);
180 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
184 v->type_size(v, name, obj, errp);
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,
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)
219 while (strings[i++] != NULL);
220 if (value < 0 || value >= i - 1) {
221 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
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;
238 visit_type_str(v, name, &enum_str, &local_err);
240 error_propagate(errp, local_err);
244 while (strings[value] != NULL) {
245 if (strcmp(strings[value], enum_str) == 0) {
251 if (strings[value] == NULL) {
252 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);