Patchwork [v2,16/27] qdev: remove print/parse methods from LostTickPolicy properties

login
register
mail settings
Submitter Paolo Bonzini
Date Feb. 4, 2012, 8:02 a.m.
Message ID <1328342577-25732-17-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/139543/
State New
Headers show

Comments

Paolo Bonzini - Feb. 4, 2012, 8:02 a.m.
Also generalize the code so that we can have more enum properties
in the future.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/qdev-properties.c |   62 +++++++++++++++++++++++++-------------------------
 hw/qdev.h            |    1 +
 qemu-common.h        |    1 +
 3 files changed, 33 insertions(+), 31 deletions(-)
Anthony Liguori - Feb. 6, 2012, 2:32 p.m.
On 02/04/2012 02:02 AM, Paolo Bonzini wrote:
> Also generalize the code so that we can have more enum properties
> in the future.
>
> Signed-off-by: Paolo Bonzini<pbonzini@redhat.com>

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>

Regards,

Anthony Liguori

> ---
>   hw/qdev-properties.c |   62 +++++++++++++++++++++++++-------------------------
>   hw/qdev.h            |    1 +
>   qemu-common.h        |    1 +
>   3 files changed, 33 insertions(+), 31 deletions(-)
>
> diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
> index 1fc77b5..dea287a 100644
> --- a/hw/qdev-properties.c
> +++ b/hw/qdev-properties.c
> @@ -893,50 +893,50 @@ PropertyInfo qdev_prop_macaddr = {
>
>   /* --- lost tick policy --- */
>
> -static const struct {
> -    const char *name;
> -    LostTickPolicy code;
> -} lost_tick_policy_table[] = {
> -    { .name = "discard", .code = LOST_TICK_DISCARD },
> -    { .name = "delay", .code = LOST_TICK_DELAY },
> -    { .name = "merge", .code = LOST_TICK_MERGE },
> -    { .name = "slew", .code = LOST_TICK_SLEW },
> +static const char *lost_tick_policy_table[LOST_TICK_MAX+1] = {
> +    [LOST_TICK_DISCARD] = "discard",
> +    [LOST_TICK_DELAY] = "delay",
> +    [LOST_TICK_MERGE] = "merge",
> +    [LOST_TICK_SLEW] = "slew",
> +    [LOST_TICK_MAX] = NULL,
>   };
>
> -static int parse_lost_tick_policy(DeviceState *dev, Property *prop,
> -                                  const char *str)
> +QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
> +
> +static void get_enum(Object *obj, Visitor *v, void *opaque,
> +                     const char *name, Error **errp)
>   {
> -    LostTickPolicy *ptr = qdev_get_prop_ptr(dev, prop);
> -    int i;
> +    DeviceState *dev = DEVICE(obj);
> +    Property *prop = opaque;
> +    int *ptr = qdev_get_prop_ptr(dev, prop);
>
> -    for (i = 0; i<  ARRAY_SIZE(lost_tick_policy_table); i++) {
> -        if (!strcasecmp(str, lost_tick_policy_table[i].name)) {
> -            *ptr = lost_tick_policy_table[i].code;
> -            break;
> -        }
> -    }
> -    if (i == ARRAY_SIZE(lost_tick_policy_table)) {
> -        return -EINVAL;
> -    }
> -    return 0;
> +    visit_type_enum(v, ptr, prop->info->enum_table,
> +                    prop->info->name, prop->name, errp);
>   }
>
> -static int print_lost_tick_policy(DeviceState *dev, Property *prop, char *dest,
> -                                  size_t len)
> +static void set_enum(Object *obj, Visitor *v, void *opaque,
> +                     const char *name, Error **errp)
>   {
> -    LostTickPolicy *ptr = qdev_get_prop_ptr(dev, prop);
> +    DeviceState *dev = DEVICE(obj);
> +    Property *prop = opaque;
> +    int *ptr = qdev_get_prop_ptr(dev, prop);
> +
> +    if (dev->state != DEV_STATE_CREATED) {
> +        error_set(errp, QERR_PERMISSION_DENIED);
> +        return;
> +    }
>
> -    return snprintf(dest, len, "%s", lost_tick_policy_table[*ptr].name);
> +    visit_type_enum(v, ptr, prop->info->enum_table,
> +                    prop->info->name, prop->name, errp);
>   }
>
>   PropertyInfo qdev_prop_losttickpolicy = {
> -    .name  = "lost_tick_policy",
> +    .name  = "LostTickPolicy",
>       .type  = PROP_TYPE_LOSTTICKPOLICY,
>       .size  = sizeof(LostTickPolicy),
> -    .parse = parse_lost_tick_policy,
> -    .print = print_lost_tick_policy,
> -    .get   = get_generic,
> -    .set   = set_generic,
> +    .enum_table  = lost_tick_policy_table,
> +    .get   = get_enum,
> +    .set   = set_enum,
>   };
>
>   /* --- pci address --- */
> diff --git a/hw/qdev.h b/hw/qdev.h
> index ab53273..c31dc4e 100644
> --- a/hw/qdev.h
> +++ b/hw/qdev.h
> @@ -140,6 +140,7 @@ struct PropertyInfo {
>       const char *legacy_name;
>       size_t size;
>       enum PropertyType type;
> +    const char **enum_table;
>       int64_t min;
>       int64_t max;
>       int (*parse)(DeviceState *dev, Property *prop, const char *str);
> diff --git a/qemu-common.h b/qemu-common.h
> index 8b69a9e..9b997f8 100644
> --- a/qemu-common.h
> +++ b/qemu-common.h
> @@ -255,6 +255,7 @@ typedef enum LostTickPolicy {
>       LOST_TICK_DELAY,
>       LOST_TICK_MERGE,
>       LOST_TICK_SLEW,
> +    LOST_TICK_MAX
>   } LostTickPolicy;
>
>   void tcg_exec_init(unsigned long tb_size);

Patch

diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c
index 1fc77b5..dea287a 100644
--- a/hw/qdev-properties.c
+++ b/hw/qdev-properties.c
@@ -893,50 +893,50 @@  PropertyInfo qdev_prop_macaddr = {
 
 /* --- lost tick policy --- */
 
-static const struct {
-    const char *name;
-    LostTickPolicy code;
-} lost_tick_policy_table[] = {
-    { .name = "discard", .code = LOST_TICK_DISCARD },
-    { .name = "delay", .code = LOST_TICK_DELAY },
-    { .name = "merge", .code = LOST_TICK_MERGE },
-    { .name = "slew", .code = LOST_TICK_SLEW },
+static const char *lost_tick_policy_table[LOST_TICK_MAX+1] = {
+    [LOST_TICK_DISCARD] = "discard",
+    [LOST_TICK_DELAY] = "delay",
+    [LOST_TICK_MERGE] = "merge",
+    [LOST_TICK_SLEW] = "slew",
+    [LOST_TICK_MAX] = NULL,
 };
 
-static int parse_lost_tick_policy(DeviceState *dev, Property *prop,
-                                  const char *str)
+QEMU_BUILD_BUG_ON(sizeof(LostTickPolicy) != sizeof(int));
+
+static void get_enum(Object *obj, Visitor *v, void *opaque,
+                     const char *name, Error **errp)
 {
-    LostTickPolicy *ptr = qdev_get_prop_ptr(dev, prop);
-    int i;
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
+    int *ptr = qdev_get_prop_ptr(dev, prop);
 
-    for (i = 0; i < ARRAY_SIZE(lost_tick_policy_table); i++) {
-        if (!strcasecmp(str, lost_tick_policy_table[i].name)) {
-            *ptr = lost_tick_policy_table[i].code;
-            break;
-        }
-    }
-    if (i == ARRAY_SIZE(lost_tick_policy_table)) {
-        return -EINVAL;
-    }
-    return 0;
+    visit_type_enum(v, ptr, prop->info->enum_table,
+                    prop->info->name, prop->name, errp);
 }
 
-static int print_lost_tick_policy(DeviceState *dev, Property *prop, char *dest,
-                                  size_t len)
+static void set_enum(Object *obj, Visitor *v, void *opaque,
+                     const char *name, Error **errp)
 {
-    LostTickPolicy *ptr = qdev_get_prop_ptr(dev, prop);
+    DeviceState *dev = DEVICE(obj);
+    Property *prop = opaque;
+    int *ptr = qdev_get_prop_ptr(dev, prop);
+
+    if (dev->state != DEV_STATE_CREATED) {
+        error_set(errp, QERR_PERMISSION_DENIED);
+        return;
+    }
 
-    return snprintf(dest, len, "%s", lost_tick_policy_table[*ptr].name);
+    visit_type_enum(v, ptr, prop->info->enum_table,
+                    prop->info->name, prop->name, errp);
 }
 
 PropertyInfo qdev_prop_losttickpolicy = {
-    .name  = "lost_tick_policy",
+    .name  = "LostTickPolicy",
     .type  = PROP_TYPE_LOSTTICKPOLICY,
     .size  = sizeof(LostTickPolicy),
-    .parse = parse_lost_tick_policy,
-    .print = print_lost_tick_policy,
-    .get   = get_generic,
-    .set   = set_generic,
+    .enum_table  = lost_tick_policy_table,
+    .get   = get_enum,
+    .set   = set_enum,
 };
 
 /* --- pci address --- */
diff --git a/hw/qdev.h b/hw/qdev.h
index ab53273..c31dc4e 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -140,6 +140,7 @@  struct PropertyInfo {
     const char *legacy_name;
     size_t size;
     enum PropertyType type;
+    const char **enum_table;
     int64_t min;
     int64_t max;
     int (*parse)(DeviceState *dev, Property *prop, const char *str);
diff --git a/qemu-common.h b/qemu-common.h
index 8b69a9e..9b997f8 100644
--- a/qemu-common.h
+++ b/qemu-common.h
@@ -255,6 +255,7 @@  typedef enum LostTickPolicy {
     LOST_TICK_DELAY,
     LOST_TICK_MERGE,
     LOST_TICK_SLEW,
+    LOST_TICK_MAX
 } LostTickPolicy;
 
 void tcg_exec_init(unsigned long tb_size);