mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-01 23:03:54 -06:00
qom: Add an object_property_add_enum() helper function
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>
This commit is contained in:
parent
2e4450ff43
commit
a8e3fbedc8
3 changed files with 150 additions and 0 deletions
|
@ -1343,6 +1343,25 @@ void object_property_add_bool(Object *obj, const char *name,
|
|||
void (*set)(Object *, bool, Error **),
|
||||
Error **errp);
|
||||
|
||||
/**
|
||||
* object_property_add_enum:
|
||||
* @obj: the object to add a property to
|
||||
* @name: the name of the property
|
||||
* @typename: the name of the enum data type
|
||||
* @get: the getter or %NULL if the property is write-only.
|
||||
* @set: the setter or %NULL if the property is read-only
|
||||
* @errp: if an error occurs, a pointer to an area to store the error
|
||||
*
|
||||
* Add an enum property using getters/setters. This function will add a
|
||||
* property of type '@typename'.
|
||||
*/
|
||||
void object_property_add_enum(Object *obj, const char *name,
|
||||
const char *typename,
|
||||
const char * const *strings,
|
||||
int (*get)(Object *, Error **),
|
||||
void (*set)(Object *, int, Error **),
|
||||
Error **errp);
|
||||
|
||||
/**
|
||||
* object_property_add_tm:
|
||||
* @obj: the object to add a property to
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue