mirror of
https://github.com/qemu/qemu.git
synced 2024-11-25 11:53:39 +08:00
qom: add string property type
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
This commit is contained in:
parent
ad6d45fa08
commit
6a146eba33
59
hw/qdev.c
59
hw/qdev.c
@ -1449,3 +1449,62 @@ DeviceState *qdev_resolve_path(const char *path, bool *ambiguous)
|
||||
return dev;
|
||||
}
|
||||
|
||||
typedef struct StringProperty
|
||||
{
|
||||
char *(*get)(DeviceState *, Error **);
|
||||
void (*set)(DeviceState *, const char *, Error **);
|
||||
} StringProperty;
|
||||
|
||||
static void qdev_property_get_str(DeviceState *dev, Visitor *v, void *opaque,
|
||||
const char *name, Error **errp)
|
||||
{
|
||||
StringProperty *prop = opaque;
|
||||
char *value;
|
||||
|
||||
value = prop->get(dev, errp);
|
||||
if (value) {
|
||||
visit_type_str(v, &value, name, errp);
|
||||
g_free(value);
|
||||
}
|
||||
}
|
||||
|
||||
static void qdev_property_set_str(DeviceState *dev, Visitor *v, void *opaque,
|
||||
const char *name, Error **errp)
|
||||
{
|
||||
StringProperty *prop = opaque;
|
||||
char *value;
|
||||
Error *local_err = NULL;
|
||||
|
||||
visit_type_str(v, &value, name, &local_err);
|
||||
if (local_err) {
|
||||
error_propagate(errp, local_err);
|
||||
return;
|
||||
}
|
||||
|
||||
prop->set(dev, value, errp);
|
||||
g_free(value);
|
||||
}
|
||||
|
||||
static void qdev_property_release_str(DeviceState *dev, const char *name,
|
||||
void *opaque)
|
||||
{
|
||||
StringProperty *prop = opaque;
|
||||
g_free(prop);
|
||||
}
|
||||
|
||||
void qdev_property_add_str(DeviceState *dev, const char *name,
|
||||
char *(*get)(DeviceState *, Error **),
|
||||
void (*set)(DeviceState *, const char *, Error **),
|
||||
Error **errp)
|
||||
{
|
||||
StringProperty *prop = g_malloc0(sizeof(*prop));
|
||||
|
||||
prop->get = get;
|
||||
prop->set = set;
|
||||
|
||||
qdev_property_add(dev, name, "string",
|
||||
get ? qdev_property_get_str : NULL,
|
||||
set ? qdev_property_set_str : NULL,
|
||||
qdev_property_release_str,
|
||||
prop, errp);
|
||||
}
|
||||
|
22
hw/qdev.h
22
hw/qdev.h
@ -574,4 +574,26 @@ void qdev_property_add_link(DeviceState *dev, const char *name,
|
||||
const char *type, DeviceState **child,
|
||||
Error **errp);
|
||||
|
||||
/**
|
||||
* @qdev_property_add_str
|
||||
*
|
||||
* Add a string property using getters/setters. This function will add a
|
||||
* property of type 'string'.
|
||||
*
|
||||
* @dev - the device to add a property to
|
||||
*
|
||||
* @name - the name of the property
|
||||
*
|
||||
* @get - the getter or NULL if the property is write-only. This function must
|
||||
* return a string to be freed by @g_free().
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
void qdev_property_add_str(DeviceState *dev, const char *name,
|
||||
char *(*get)(DeviceState *, Error **),
|
||||
void (*set)(DeviceState *, const char *, Error **),
|
||||
Error **errp);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user