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