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