From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 03/19] Add enum handlers for easy & efficient string <-> int conversion
Date: Mon, 7 Jun 2010 15:42:16 +0100 [thread overview]
Message-ID: <1275921752-29420-4-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1275921752-29420-1-git-send-email-berrange@redhat.com>
There is quite alot of code using an enumeration of possible
values, which also needs todo conversions to/from a string
representation of enum values. These string <-> int conversions
have been repeated in an adhoc manner throughout the code.
This makes it hard to report on the list of valid strings,
eg in help output, or todo proper validation in the qemu
config/option parsing routines.
This addresses the first problem by introducing a standard
set of routines for performing string <-> int conversions
for enums. There are two restrictions on using these helpers,
the first enum value must be 0, and there must be a sentinal
in the enum to provide the max value.
For each enumeration, three functions will be made available
- string to int convertor:
int XXXX_from_string(const char *value);
Returns -1 if the value was not an allowed string for the
enumeration. Returns >= 0 for a valid value
- int to string convertor
const char * XXXX_to_string(int value);
Returns NULL if the value was not a member of the
enumeration. Returns a non-NULL sstring for valid value
- string list generator
char * XXXX_to_string_list(void);
Returns a malloc'd string containing all valid values,
separated by commas. Caller must free the string.
The general usage pattern is as follows.
In the header file (eg qemu-option.h):
enum QemuOptType {
QEMU_OPT_STRING = 0, /* no parsing (use string as-is) */
QEMU_OPT_BOOL, /* on/off */
QEMU_OPT_NUMBER, /* simple number */
QEMU_OPT_SIZE, /* size, accepts (K)ilo, (M)ega, (G)iga, (T)era postfix */
QEMU_OPT_LAST
};
QEMU_ENUM_DECL(qemu_opt_type);
This declares the function prototypes for the 3 methods
outlined above.
In the corresponding source file (eg qemu-option.c):
QEMU_ENUM_IMPL(qemu_opt_type,
QEMU_OPT_LAST,
"string", "bool", "number", "size");
This provides the implementation of the 3 methods. If there
are greater/fewer strings provided than the number of values
in the enumeration, this generates a compile time assertion
failure that looks like
qemu-option.c:35: error: negative width in bit-field ‘verify_error_if_negative_size__’
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
Makefile.objs | 2 +-
qemu-enum.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
qemu-enum.h | 45 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 90 insertions(+), 1 deletions(-)
create mode 100644 qemu-enum.c
create mode 100644 qemu-enum.h
diff --git a/Makefile.objs b/Makefile.objs
index 9796dcb..0ba9966 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -8,7 +8,7 @@ qobject-obj-y += qerror.o
# block-obj-y is code used by both qemu system emulation and qemu-img
block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
-block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-config.o
+block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-config.o qemu-enum.o
block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
diff --git a/qemu-enum.c b/qemu-enum.c
new file mode 100644
index 0000000..9bb33ac
--- /dev/null
+++ b/qemu-enum.c
@@ -0,0 +1,44 @@
+#include "qemu-enum.h"
+
+int qemu_enum_from_string(const char *const*types,
+ unsigned int ntypes,
+ const char *type)
+{
+ unsigned int i;
+ if (!type)
+ return -1;
+
+ for (i = 0 ; i < ntypes ; i++)
+ if (strcmp(types[i], type) == 0)
+ return i;
+
+ return -1;
+}
+
+const char *qemu_enum_to_string(const char *const*types,
+ unsigned int ntypes,
+ int type)
+{
+ if (type < 0 || type >= ntypes)
+ return NULL;
+
+ return types[type];
+}
+
+char *qemu_enum_to_string_list(const char *const*types,
+ unsigned int ntypes)
+{
+ size_t len = 0;
+ char *ret;
+ int i;
+ for (i = 0 ; i < ntypes ; i++)
+ len += strlen(types[i]) + 2;
+ ret = qemu_malloc(len);
+ *ret = '\0';
+ for (i = 0 ; i < ntypes ; i++) {
+ if (i > 0)
+ strcat(ret, ", ");
+ strcat(ret, types[i]);
+ }
+ return ret;
+}
diff --git a/qemu-enum.h b/qemu-enum.h
new file mode 100644
index 0000000..ff47798
--- /dev/null
+++ b/qemu-enum.h
@@ -0,0 +1,45 @@
+#ifndef QEMU_ENUM_H
+#define QEMU_ENUM_H
+
+#include "qemu-common.h"
+#include "verify.h"
+
+
+int qemu_enum_from_string(const char *const*types,
+ unsigned int ntypes,
+ const char *type);
+
+const char *qemu_enum_to_string(const char *const*types,
+ unsigned int ntypes,
+ int type);
+
+char *qemu_enum_to_string_list(const char *const*types,
+ unsigned int ntypes);
+
+#define QEMU_ENUM_IMPL(name, lastVal, ...) \
+ static const char *const name ## _string_list[] = { __VA_ARGS__ }; \
+ char *name ## _to_string_list(void) { \
+ return qemu_enum_to_string_list(name ## _string_list, \
+ ARRAY_SIZE(name ## _string_list)); \
+ } \
+ const char *name ## _to_string(int type) { \
+ return qemu_enum_to_string(name ## _string_list, \
+ ARRAY_SIZE(name ## _string_list), \
+ type); \
+ } \
+ int name ## _from_string(const char *type) { \
+ return qemu_enum_from_string(name ## _string_list, \
+ ARRAY_SIZE(name ## _string_list), \
+ type); \
+ } \
+ extern int (* name ## Verify (void)) \
+ [verify_true (ARRAY_SIZE(name ## _string_list) == lastVal)]
+
+# define QEMU_ENUM_DECL(name) \
+ const char *name ## _to_string(int type); \
+ char *name ## _to_string_list(void); \
+ int name ## _from_string(const char*type)
+
+
+
+#endif /* QEMU_ENUM_H */
--
1.6.6.1
next prev parent reply other threads:[~2010-06-07 14:43 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-06-07 14:42 [Qemu-devel] [PATCH 00/19] RFC: Reporting QEMU binary capabilities Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 01/19] Add support for JSON pretty printing Daniel P. Berrange
2010-06-09 19:51 ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 02/19] Add support for compile time assertions Daniel P. Berrange
2010-06-07 15:35 ` [Qemu-devel] " Paolo Bonzini
2010-06-07 14:42 ` Daniel P. Berrange [this message]
2010-06-09 19:52 ` [Qemu-devel] [PATCH 03/19] Add enum handlers for easy & efficient string <-> int conversion Luiz Capitulino
2010-06-26 6:52 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 04/19] Add support for a option parameter as an enum Daniel P. Berrange
2010-06-26 6:59 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 05/19] Ensure that QEMU exits if drive_add parsing fails Daniel P. Berrange
2010-06-26 7:04 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 06/19] Convert drive options to use enumeration data type Daniel P. Berrange
2010-06-09 19:52 ` Luiz Capitulino
2010-06-26 7:07 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 07/19] Convert netdev client types to use an enumeration Daniel P. Berrange
2010-06-07 15:09 ` Anthony Liguori
2010-06-07 15:13 ` Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 08/19] Convert RTC to use enumerations for configuration parameters Daniel P. Berrange
2010-06-09 19:54 ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 09/19] Change 'query-version' to output broken down version string Daniel P. Berrange
2010-06-07 15:11 ` Anthony Liguori
2010-06-09 20:04 ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 10/19] Add a query-machines command to QMP Daniel P. Berrange
2010-06-07 15:13 ` Anthony Liguori
2010-06-07 16:44 ` Daniel P. Berrange
2010-06-07 17:07 ` Anthony Liguori
2010-06-07 17:14 ` Daniel P. Berrange
2010-06-09 20:06 ` Luiz Capitulino
2010-06-07 14:42 ` [Qemu-devel] [PATCH 11/19] Add a query-devices " Daniel P. Berrange
2010-06-07 15:14 ` Anthony Liguori
2010-06-07 16:03 ` Daniel P. Berrange
2010-06-26 7:12 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 12/19] Add a query-cputypes " Daniel P. Berrange
2010-06-26 7:18 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 13/19] Add a query-target " Daniel P. Berrange
2010-06-07 15:43 ` [Qemu-devel] " Paolo Bonzini
2010-06-07 14:42 ` [Qemu-devel] [PATCH 14/19] Add a query-argv " Daniel P. Berrange
2010-06-07 15:01 ` Anthony Liguori
2010-06-10 15:34 ` [Qemu-devel] " Paolo Bonzini
2010-06-26 7:30 ` [Qemu-devel] " Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 15/19] Expand query-argv to include help string Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 16/19] Add a query-netdev command to QMP Daniel P. Berrange
2010-06-07 15:15 ` Anthony Liguori
2010-06-26 7:32 ` Markus Armbruster
2010-06-07 19:13 ` Miguel Di Ciurcio Filho
2010-06-08 8:38 ` Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 17/19] Add a query-config " Daniel P. Berrange
2010-06-26 7:34 ` Markus Armbruster
2010-06-07 14:42 ` [Qemu-devel] [PATCH 18/19] Add option to turn on JSON pretty printing in monitor Daniel P. Berrange
2010-06-07 14:42 ` [Qemu-devel] [PATCH 19/19] Add a -capabilities argument to allow easy query for static QEMU info Daniel P. Berrange
2010-06-07 16:04 ` [Qemu-devel] " Paolo Bonzini
2010-06-07 16:09 ` Daniel P. Berrange
2010-06-07 16:04 ` [Qemu-devel] " Anthony Liguori
2010-06-07 14:58 ` [Qemu-devel] [PATCH 00/19] RFC: Reporting QEMU binary capabilities Anthony Liguori
2010-06-07 15:10 ` Daniel P. Berrange
2010-06-07 15:18 ` Anthony Liguori
2010-06-07 16:02 ` [Qemu-devel] " Paolo Bonzini
2010-06-07 16:03 ` Anthony Liguori
2010-06-07 16:07 ` [Qemu-devel] " Anthony Liguori
2010-06-09 20:25 ` Luiz Capitulino
2010-06-26 6:45 ` Markus Armbruster
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=1275921752-29420-4-git-send-email-berrange@redhat.com \
--to=berrange@redhat.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).