aboutsummaryrefslogtreecommitdiff
path: root/hw/qdev.c
diff options
context:
space:
mode:
authorAnthony Liguori <aliguori@us.ibm.com>2011-12-12 14:29:42 -0600
committerAnthony Liguori <aliguori@us.ibm.com>2011-12-15 09:20:49 -0600
commit6a146eba33159cb97d28457d409d81066cbefb09 (patch)
tree272555e4ad8544561d44616cbe52ba5a91101c49 /hw/qdev.c
parentad6d45fa0837acf3e8cab323ee5b08e05a9410a5 (diff)
downloadqemu-6a146eba33159cb97d28457d409d81066cbefb09.zip
qemu-6a146eba33159cb97d28457d409d81066cbefb09.tar.gz
qemu-6a146eba33159cb97d28457d409d81066cbefb09.tar.bz2
qom: add string property type
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Diffstat (limited to 'hw/qdev.c')
-rw-r--r--hw/qdev.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/hw/qdev.c b/hw/qdev.c
index 4004860..0fc20fc 100644
--- a/hw/qdev.c
+++ b/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);
+}