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