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