From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: aliguori@us.ibm.com
Subject: [Qemu-devel] [PATCH 1/9] qapi: allow sharing enum implementation across visitors
Date: Thu, 9 Feb 2012 15:31:50 +0100 [thread overview]
Message-ID: <1328797918-1316-2-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1328797918-1316-1-git-send-email-pbonzini@redhat.com>
Most visitors will use the same code for enum parsing. Move it to
the core.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
qapi/qapi-visit-core.c | 51 +++++++++++++++++++++++++++++++++++++++++++++
qapi/qapi-visit-impl.h | 23 ++++++++++++++++++++
qapi/qmp-input-visitor.c | 34 +----------------------------
qapi/qmp-output-visitor.c | 22 +-----------------
4 files changed, 78 insertions(+), 52 deletions(-)
create mode 100644 qapi/qapi-visit-impl.h
diff --git a/qapi/qapi-visit-core.c b/qapi/qapi-visit-core.c
index ddef3ed..a4e088c 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;
+}
diff --git a/qapi/qapi-visit-impl.h b/qapi/qapi-visit-impl.h
new file mode 100644
index 0000000..0f3a189
--- /dev/null
+++ b/qapi/qapi-visit-impl.h
@@ -0,0 +1,23 @@
+/*
+ * Core Definitions for QAPI Visitor implementations
+ *
+ * Copyright (C) 2012 Red Hat, Inc.
+ *
+ * Author: Paolo Bonizni <pbonzini@redhat.com>
+ *
+ * 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
diff --git a/qapi/qmp-input-visitor.c b/qapi/qmp-input-visitor.c
index c78022b..012cd0a 100644
--- a/qapi/qmp-input-visitor.c
+++ b/qapi/qmp-input-visitor.c
@@ -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"
@@ -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)
{
@@ -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;
diff --git a/qapi/qmp-output-visitor.c b/qapi/qmp-output-visitor.c
index f76d015..e0697b0 100644
--- a/qapi/qmp-output-visitor.c
+++ b/qapi/qmp-output-visitor.c
@@ -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"
@@ -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);
@@ -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;
--
1.7.7.6
next prev parent reply other threads:[~2012-02-09 14:32 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-09 14:31 [Qemu-devel] [PATCH 0/9] qdev deconstruction, command-line episode Paolo Bonzini
2012-02-09 14:31 ` Paolo Bonzini [this message]
2012-02-21 20:31 ` [Qemu-devel] [PATCH 1/9] qapi: allow sharing enum implementation across visitors Andreas Färber
2012-02-22 7:30 ` Paolo Bonzini
2012-02-09 14:31 ` [Qemu-devel] [PATCH 2/9] qapi: drop qmp_input_end_optional Paolo Bonzini
2012-02-09 14:31 ` [Qemu-devel] [PATCH 3/9] qapi: add string-based visitors Paolo Bonzini
2012-02-09 14:31 ` [Qemu-devel] [PATCH 4/9] qapi: add tests for " Paolo Bonzini
2012-02-09 14:31 ` [Qemu-devel] [PATCH 5/9] qom: add generic string parsing/printing Paolo Bonzini
2012-02-21 20:47 ` Andreas Färber
2012-02-22 7:31 ` Paolo Bonzini
2012-02-09 14:31 ` [Qemu-devel] [PATCH 6/9] qdev: accept both strings and integers for PCI addresses Paolo Bonzini
2012-02-09 14:31 ` [Qemu-devel] [PATCH 7/9] qdev: accept hex properties only if prefixed by 0x Paolo Bonzini
2012-02-09 14:31 ` [Qemu-devel] [PATCH 8/9] qdev: use built-in QOM string parser Paolo Bonzini
2012-02-09 14:31 ` [Qemu-devel] [PATCH 9/9] qdev: drop unnecessary parse/print methods Paolo Bonzini
2012-02-21 17:13 ` [Qemu-devel] [PULL v2 0/9] qdev deconstruction, command-line episode Paolo Bonzini
2012-02-22 14:44 ` Anthony Liguori
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1328797918-1316-2-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=aliguori@us.ibm.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).