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 size_t size, Error **errp) 24 { 25 v->start_struct(v, name, obj, 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) 42 { 43 if (v->end_implicit_struct) { 44 v->end_implicit_struct(v); 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) 54 { 55 return v->next_list(v, list); 56 } 57 58 void visit_end_list(Visitor *v) 59 { 60 v->end_list(v); 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, name, present); 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, name, type, promote_int, errp); 84 } 85 } 86 87 void visit_type_enum(Visitor *v, const char *name, int *obj, 88 const char *const strings[], Error **errp) 89 { 90 v->type_enum(v, name, obj, strings, errp); 91 } 92 93 void visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp) 94 { 95 v->type_int64(v, name, obj, errp); 96 } 97 98 static void visit_type_uintN(Visitor *v, uint64_t *obj, const char *name, 99 uint64_t max, const char *type, Error **errp) 100 { 101 Error *err = NULL; 102 uint64_t value = *obj; 103 104 v->type_uint64(v, name, &value, &err); 105 if (err) { 106 error_propagate(errp, err); 107 } else if (value > max) { 108 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 109 name ? name : "null", type); 110 } else { 111 *obj = value; 112 } 113 } 114 115 void visit_type_uint8(Visitor *v, const char *name, uint8_t *obj, 116 Error **errp) 117 { 118 uint64_t value = *obj; 119 visit_type_uintN(v, &value, name, UINT8_MAX, "uint8_t", errp); 120 *obj = value; 121 } 122 123 void visit_type_uint16(Visitor *v, const char *name, uint16_t *obj, 124 Error **errp) 125 { 126 uint64_t value = *obj; 127 visit_type_uintN(v, &value, name, UINT16_MAX, "uint16_t", errp); 128 *obj = value; 129 } 130 131 void visit_type_uint32(Visitor *v, const char *name, uint32_t *obj, 132 Error **errp) 133 { 134 uint64_t value = *obj; 135 visit_type_uintN(v, &value, name, UINT32_MAX, "uint32_t", errp); 136 *obj = value; 137 } 138 139 void visit_type_uint64(Visitor *v, const char *name, uint64_t *obj, 140 Error **errp) 141 { 142 v->type_uint64(v, name, obj, errp); 143 } 144 145 static void visit_type_intN(Visitor *v, int64_t *obj, const char *name, 146 int64_t min, int64_t max, const char *type, 147 Error **errp) 148 { 149 Error *err = NULL; 150 int64_t value = *obj; 151 152 v->type_int64(v, name, &value, &err); 153 if (err) { 154 error_propagate(errp, err); 155 } else if (value < min || value > max) { 156 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, 157 name ? name : "null", type); 158 } else { 159 *obj = value; 160 } 161 } 162 163 void visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp) 164 { 165 int64_t value = *obj; 166 visit_type_intN(v, &value, name, INT8_MIN, INT8_MAX, "int8_t", errp); 167 *obj = value; 168 } 169 170 void visit_type_int16(Visitor *v, const char *name, int16_t *obj, 171 Error **errp) 172 { 173 int64_t value = *obj; 174 visit_type_intN(v, &value, name, INT16_MIN, INT16_MAX, "int16_t", errp); 175 *obj = value; 176 } 177 178 void visit_type_int32(Visitor *v, const char *name, int32_t *obj, 179 Error **errp) 180 { 181 int64_t value = *obj; 182 visit_type_intN(v, &value, name, INT32_MIN, INT32_MAX, "int32_t", errp); 183 *obj = value; 184 } 185 186 void visit_type_int64(Visitor *v, const char *name, int64_t *obj, 187 Error **errp) 188 { 189 v->type_int64(v, name, obj, errp); 190 } 191 192 void visit_type_size(Visitor *v, const char *name, uint64_t *obj, 193 Error **errp) 194 { 195 if (v->type_size) { 196 v->type_size(v, name, obj, errp); 197 } else { 198 v->type_uint64(v, name, obj, errp); 199 } 200 } 201 202 void visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) 203 { 204 v->type_bool(v, name, obj, errp); 205 } 206 207 void visit_type_str(Visitor *v, const char *name, char **obj, Error **errp) 208 { 209 v->type_str(v, name, obj, errp); 210 } 211 212 void visit_type_number(Visitor *v, const char *name, double *obj, 213 Error **errp) 214 { 215 v->type_number(v, name, obj, errp); 216 } 217 218 void visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp) 219 { 220 v->type_any(v, name, obj, errp); 221 } 222 223 void output_type_enum(Visitor *v, const char *name, int *obj, 224 const char *const strings[], Error **errp) 225 { 226 int i = 0; 227 int value = *obj; 228 char *enum_str; 229 230 assert(strings); 231 while (strings[i++] != NULL); 232 if (value < 0 || value >= i - 1) { 233 error_setg(errp, QERR_INVALID_PARAMETER, name ? name : "null"); 234 return; 235 } 236 237 enum_str = (char *)strings[value]; 238 visit_type_str(v, name, &enum_str, errp); 239 } 240 241 void input_type_enum(Visitor *v, const char *name, int *obj, 242 const char *const strings[], Error **errp) 243 { 244 Error *local_err = NULL; 245 int64_t value = 0; 246 char *enum_str; 247 248 assert(strings); 249 250 visit_type_str(v, name, &enum_str, &local_err); 251 if (local_err) { 252 error_propagate(errp, local_err); 253 return; 254 } 255 256 while (strings[value] != NULL) { 257 if (strcmp(strings[value], enum_str) == 0) { 258 break; 259 } 260 value++; 261 } 262 263 if (strings[value] == NULL) { 264 error_setg(errp, QERR_INVALID_PARAMETER, enum_str); 265 g_free(enum_str); 266 return; 267 } 268 269 g_free(enum_str); 270 *obj = value; 271 } 272