diff options
author | Paolo Bonzini <pbonzini@redhat.com> | 2012-02-02 22:09:44 +0100 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2012-02-07 13:52:41 +0100 |
commit | 1ce05125571e9dbbd16e1de0f8924fe45bef2bf0 (patch) | |
tree | 74df8e956ee118aa0a3829b2e776d9e5c5919b1c | |
parent | acbac4a1dc974a3915760b4551336544c4360241 (diff) |
qdev: remove print/parse methods from LostTickPolicy properties
Also generalize the code so that we can have more enum properties
in the future.
Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r-- | hw/qdev-properties.c | 62 | ||||
-rw-r--r-- | hw/qdev.h | 1 | ||||
-rw-r--r-- | qemu-common.h | 1 |
3 files changed, 33 insertions, 31 deletions
diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c index 1fc77b5979..dea287adf0 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 --- */ @@ -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 8b69a9eea5..9b997f8838 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); |