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