diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2012-02-09 09:11:52 +0100 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2012-02-21 10:21:05 +0100 |
commit | 0f71a1e0c8d64b84edd5ac5abdce70a4fb9357a9 (patch) | |
tree | ed0d41c8d49750ac961afb7c9d120f6e2912148d /qapi/qapi-visit-core.c | |
parent | 99c7f87826337fa81f2f0f9baa9ca0a44faf90e9 (diff) |
qapi: allow sharing enum implementation across visitors
Most visitors will use the same code for enum parsing. Move it to
the core.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'qapi/qapi-visit-core.c')
-rw-r--r-- | qapi/qapi-visit-core.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c index ddef3eda12..a4e088c9fc 100644 --- a/qapi/qapi-visit-core.c +++ b/qapi/qapi-visit-core.c @@ -12,6 +12,7 @@ */ #include "qapi/qapi-visit-core.h" +#include "qapi/qapi-visit-impl.h" void visit_start_handle(Visitor *v, void **obj, const char *kind, const char *name, Error **errp) @@ -116,3 +117,53 @@ void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp) v->type_number(v, obj, name, errp); } } + +void output_type_enum(Visitor *v, int *obj, const char *strings[], + const char *kind, const char *name, + Error **errp) +{ + int i = 0; + int value = *obj; + char *enum_str; + + assert(strings); + while (strings[i++] != NULL); + if (value < 0 || value >= i - 1) { + error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null"); + return; + } + + enum_str = (char *)strings[value]; + visit_type_str(v, &enum_str, name, errp); +} + +void input_type_enum(Visitor *v, int *obj, const char *strings[], + const char *kind, const char *name, + Error **errp) +{ + int64_t value = 0; + char *enum_str; + + assert(strings); + + visit_type_str(v, &enum_str, name, errp); + if (error_is_set(errp)) { + return; + } + + while (strings[value] != NULL) { + if (strcmp(strings[value], enum_str) == 0) { + break; + } + value++; + } + + if (strings[value] == NULL) { + error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null"); + g_free(enum_str); + return; + } + + g_free(enum_str); + *obj = value; +} |