a8e3fbedc8
A QOM property can be parsed as enum using the visit_type_enum() helper function, but this forces callers to use the more complex generic object_property_add() method when registering it. It also requires that users of that object have access to the string map when they want to read the property value. This patch introduces a specialized object_property_add_enum() method which simplifies the use of enum properties, so the setters/getters directly get passed the int value. typedef enum { MYDEV_TYPE_FROG, MYDEV_TYPE_ALLIGATOR, MYDEV_TYPE_PLATYPUS, MYDEV_TYPE_LAST } MyDevType; Then provide a table of enum <-> string mappings static const char *const mydevtypemap[MYDEV_TYPE_LAST + 1] = { [MYDEV_TYPE_FROG] = "frog", [MYDEV_TYPE_ALLIGATOR] = "alligator", [MYDEV_TYPE_PLATYPUS] = "platypus", [MYDEV_TYPE_LAST] = NULL, }; Assuming an object struct of typedef struct { Object parent_obj; MyDevType devtype; ...other fields... } MyDev; The property can then be registered as follows: static int mydev_prop_get_devtype(Object *obj, Error **errp G_GNUC_UNUSED) { MyDev *dev = MYDEV(obj); return dev->devtype; } static void mydev_prop_set_devtype(Object *obj, int value, Error **errp G_GNUC_UNUSED) { MyDev *dev = MYDEV(obj); dev->devtype = value; } object_property_add_enum(obj, "devtype", mydevtypemap, "MyDevType", mydev_prop_get_devtype, mydev_prop_set_devtype, NULL); Note there is no need to check the range of 'value' in the setter, because the string->enum conversion code will have already done that and reported an error as required. Signed-off-by: Daniel P. Berrange <berrange@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Signed-off-by: Andreas Färber <afaerber@suse.de> |
||
---|---|---|
.. | ||
acpi-test-data | ||
image-fuzzer | ||
libqos | ||
multiboot | ||
qapi-schema | ||
qemu-iotests | ||
rocker | ||
tcg | ||
vmstate-static-checker-data | ||
.gitignore | ||
ac97-test.c | ||
ahci-test.c | ||
bios-tables-test.c | ||
boot-order-test.c | ||
check-block.sh | ||
check-qdict.c | ||
check-qfloat.c | ||
check-qint.c | ||
check-qjson.c | ||
check-qlist.c | ||
check-qom-interface.c | ||
check-qom-proplist.c | ||
check-qstring.c | ||
display-vga-test.c | ||
drive_del-test.c | ||
e1000-test.c | ||
eepro100-test.c | ||
endianness-test.c | ||
es1370-test.c | ||
fdc-test.c | ||
fw_cfg-test.c | ||
hd-geo-test.c | ||
i440fx-test.c | ||
i82801b11-test.c | ||
ide-test.c | ||
intel-hda-test.c | ||
ioh3420-test.c | ||
ipoctal232-test.c | ||
libqtest.c | ||
libqtest.h | ||
m48t59-test.c | ||
Makefile | ||
ne2000-test.c | ||
nvme-test.c | ||
pc-cpu-test.c | ||
pcnet-test.c | ||
pvpanic-test.c | ||
q35-test.c | ||
qemu-iotests-quick.sh | ||
qom-test.c | ||
rcutorture.c | ||
rtc-test.c | ||
rtl8139-test.c | ||
spapr-phb-test.c | ||
test-aio.c | ||
test-bitops.c | ||
test-coroutine.c | ||
test-cutils.c | ||
test-hbitmap.c | ||
test-int128.c | ||
test-iov.c | ||
test-mul64.c | ||
test-opts-visitor.c | ||
test-qdev-global-props.c | ||
test-qemu-opts.c | ||
test-qmp-commands.c | ||
test-qmp-event.c | ||
test-qmp-input-strict.c | ||
test-qmp-input-visitor.c | ||
test-qmp-output-visitor.c | ||
test-rcu-list.c | ||
test-rfifolock.c | ||
test-string-input-visitor.c | ||
test-string-output-visitor.c | ||
test-thread-pool.c | ||
test-throttle.c | ||
test-visitor-serialization.c | ||
test-vmstate.c | ||
test-write-threshold.c | ||
test-x86-cpuid.c | ||
test-xbzrle.c | ||
tmp105-test.c | ||
tpci200-test.c | ||
usb-hcd-ehci-test.c | ||
usb-hcd-ohci-test.c | ||
usb-hcd-uhci-test.c | ||
usb-hcd-xhci-test.c | ||
vhost-user-test.c | ||
virtio-9p-test.c | ||
virtio-balloon-test.c | ||
virtio-blk-test.c | ||
virtio-console-test.c | ||
virtio-net-test.c | ||
virtio-rng-test.c | ||
virtio-scsi-test.c | ||
virtio-serial-test.c | ||
vmxnet3-test.c | ||
wdt_ib700-test.c |