Skip to content

Commit

Permalink
qapi: allow sharing enum implementation across visitors
Browse files Browse the repository at this point in the history
Most visitors will use the same code for enum parsing.  Move it to
the core.

Signed-off-by: Paolo Bonzini <[email protected]>
  • Loading branch information
bonzini committed Feb 21, 2012
1 parent 99c7f87 commit 0f71a1e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 52 deletions.
51 changes: 51 additions & 0 deletions qapi/qapi-visit-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
23 changes: 23 additions & 0 deletions qapi/qapi-visit-impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Core Definitions for QAPI Visitor implementations
*
* Copyright (C) 2012 Red Hat, Inc.
*
* Author: Paolo Bonizni <[email protected]>
*
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
* See the COPYING.LIB file in the top-level directory.
*
*/
#ifndef QAPI_VISITOR_IMPL_H
#define QAPI_VISITOR_IMPL_H

#include "qapi/qapi-types-core.h"
#include "qapi/qapi-visit-core.h"

void input_type_enum(Visitor *v, int *obj, const char *strings[],
const char *kind, const char *name, Error **errp);
void output_type_enum(Visitor *v, int *obj, const char *strings[],
const char *kind, const char *name, Error **errp);

#endif
34 changes: 2 additions & 32 deletions qapi/qmp-input-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

#include "qmp-input-visitor.h"
#include "qapi/qapi-visit-impl.h"
#include "qemu-queue.h"
#include "qemu-common.h"
#include "qemu-objects.h"
Expand Down Expand Up @@ -217,37 +218,6 @@ static void qmp_input_type_number(Visitor *v, double *obj, const char *name,
*obj = qfloat_get_double(qobject_to_qfloat(qobj));
}

static void qmp_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);

qmp_input_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;
}

static void qmp_input_start_optional(Visitor *v, bool *present,
const char *name, Error **errp)
{
Expand Down Expand Up @@ -288,7 +258,7 @@ QmpInputVisitor *qmp_input_visitor_new(QObject *obj)
v->visitor.start_list = qmp_input_start_list;
v->visitor.next_list = qmp_input_next_list;
v->visitor.end_list = qmp_input_end_list;
v->visitor.type_enum = qmp_input_type_enum;
v->visitor.type_enum = input_type_enum;
v->visitor.type_int = qmp_input_type_int;
v->visitor.type_bool = qmp_input_type_bool;
v->visitor.type_str = qmp_input_type_str;
Expand Down
22 changes: 2 additions & 20 deletions qapi/qmp-output-visitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

#include "qmp-output-visitor.h"
#include "qapi/qapi-visit-impl.h"
#include "qemu-queue.h"
#include "qemu-common.h"
#include "qemu-objects.h"
Expand Down Expand Up @@ -180,25 +181,6 @@ static void qmp_output_type_number(Visitor *v, double *obj, const char *name,
qmp_output_add(qov, name, qfloat_from_double(*obj));
}

static void qmp_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];
qmp_output_type_str(v, &enum_str, name, errp);
}

QObject *qmp_output_get_qobject(QmpOutputVisitor *qov)
{
QObject *obj = qmp_output_first(qov);
Expand Down Expand Up @@ -239,7 +221,7 @@ QmpOutputVisitor *qmp_output_visitor_new(void)
v->visitor.start_list = qmp_output_start_list;
v->visitor.next_list = qmp_output_next_list;
v->visitor.end_list = qmp_output_end_list;
v->visitor.type_enum = qmp_output_type_enum;
v->visitor.type_enum = output_type_enum;
v->visitor.type_int = qmp_output_type_int;
v->visitor.type_bool = qmp_output_type_bool;
v->visitor.type_str = qmp_output_type_str;
Expand Down

0 comments on commit 0f71a1e

Please sign in to comment.