s390x/css: only suspend when enabled by orb
[qemu/ar7.git] / qapi / qapi-visit-core.c
blob856606b2538dc1cfc864c6007adbbbdb0252e327
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 "qemu-common.h"
17 #include "qapi/qmp/qobject.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qapi/visitor.h"
20 #include "qapi/visitor-impl.h"
22 void visit_start_struct(Visitor *v, const char *name, void **obj,
23 size_t size, Error **errp)
25 v->start_struct(v, name, obj, size, errp);
28 void visit_end_struct(Visitor *v, Error **errp)
30 v->end_struct(v, errp);
33 void visit_start_list(Visitor *v, const char *name, Error **errp)
35 v->start_list(v, name, errp);
38 GenericList *visit_next_list(Visitor *v, GenericList **list, size_t size)
40 assert(list && size >= sizeof(GenericList));
41 return v->next_list(v, list, size);
44 void visit_end_list(Visitor *v)
46 v->end_list(v);
49 void visit_start_alternate(Visitor *v, const char *name,
50 GenericAlternate **obj, size_t size,
51 bool promote_int, Error **errp)
53 assert(obj && size >= sizeof(GenericAlternate));
54 if (v->start_alternate) {
55 v->start_alternate(v, name, obj, size, promote_int, errp);
59 void visit_end_alternate(Visitor *v)
61 if (v->end_alternate) {
62 v->end_alternate(v);
66 bool visit_optional(Visitor *v, const char *name, bool *present)
68 if (v->optional) {
69 v->optional(v, name, present);
71 return *present;
74 void visit_type_enum(Visitor *v, const char *name, int *obj,
75 const char *const strings[], Error **errp)
77 v->type_enum(v, name, obj, strings, errp);
80 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp)
82 v->type_int64(v, name, obj, errp);
85 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name,
86 uint64_t max, const char *type, Error **errp)
88 Error *err = NULL;
89 uint64_t value = *obj;
91 v->type_uint64(v, name, &value, &err);
92 if (err) {
93 error_propagate(errp, err);
94 } else if (value > max) {
95 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
96 name ? name : "null", type);
97 } else {
98 *obj = value;
102 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj,
103 Error **errp)
105 uint64_t value = *obj;
106 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp);
107 *obj = value;
110 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj,
111 Error **errp)
113 uint64_t value = *obj;
114 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp);
115 *obj = value;
118 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj,
119 Error **errp)
121 uint64_t value = *obj;
122 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp);
123 *obj = value;
126 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj,
127 Error **errp)
129 v->type_uint64(v, name, obj, errp);
132 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name,
133 int64_t min, int64_t max, const char *type,
134 Error **errp)
136 Error *err = NULL;
137 int64_t value = *obj;
139 v->type_int64(v, name, &value, &err);
140 if (err) {
141 error_propagate(errp, err);
142 } else if (value < min || value > max) {
143 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
144 name ? name : "null", type);
145 } else {
146 *obj = value;
150 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp)
152 int64_t value = *obj;
153 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp);
154 *obj = value;
157 void visit_type_int16(Visitor *v, const char *name, int16_t *obj,
158 Error **errp)
160 int64_t value = *obj;
161 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp);
162 *obj = value;
165 void visit_type_int32(Visitor *v, const char *name, int32_t *obj,
166 Error **errp)
168 int64_t value = *obj;
169 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp);
170 *obj = value;
173 void visit_type_int64(Visitor *v, const char *name, int64_t *obj,
174 Error **errp)
176 v->type_int64(v, name, obj, errp);
179 void visit_type_size(Visitor *v, const char *name, uint64_t *obj,
180 Error **errp)
182 if (v->type_size) {
183 v->type_size(v, name, obj, errp);
184 } else {
185 v->type_uint64(v, name, obj, errp);
189 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp)
191 v->type_bool(v, name, obj, errp);
194 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp)
196 v->type_str(v, name, obj, errp);
199 void visit_type_number(Visitor *v, const char *name, double *obj,
200 Error **errp)
202 v->type_number(v, name, obj, errp);
205 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp)
207 v->type_any(v, name, obj, errp);
210 void output_type_enum(Visitor *v, const char *name, int *obj,
211 const char *const strings[], Error **errp)
213 int i = 0;
214 int value = *obj;
215 char *enum_str;
217 assert(strings);
218 while (strings[i++] != NULL);
219 if (value < 0 || value >= i - 1) {
220 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null");
221 return;
224 enum_str = (char *)strings[value];
225 visit_type_str(v, name, &enum_str, errp);
228 void input_type_enum(Visitor *v, const char *name, int *obj,
229 const char *const strings[], Error **errp)
231 Error *local_err = NULL;
232 int64_t value = 0;
233 char *enum_str;
235 assert(strings);
237 visit_type_str(v, name, &enum_str, &local_err);
238 if (local_err) {
239 error_propagate(errp, local_err);
240 return;
243 while (strings[value] != NULL) {
244 if (strcmp(strings[value], enum_str) == 0) {
245 break;
247 value++;
250 if (strings[value] == NULL) {
251 error_setg(errp, QERR_INVALID_PARAMETER, enum_str);
252 g_free(enum_str);
253 return;
256 g_free(enum_str);
257 *obj = value;