Patchwork [1/6] object: add object_property_add_bool

login
register
mail settings
Submitter Anthony Liguori
Date Oct. 15, 2012, 7:34 p.m.
Message ID <1350329657-18665-2-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/191641/
State New
Headers show

Comments

Anthony Liguori - Oct. 15, 2012, 7:34 p.m.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 include/qemu/object.h |   17 ++++++++++++++
 qom/object.c          |   58 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 75 insertions(+), 0 deletions(-)
Andreas Färber - Oct. 16, 2012, 3:44 p.m.
Am 15.10.2012 21:34, schrieb Anthony Liguori:
> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

This feels like a resend or v2. I remember discussing whether an opaque
might be a good idea and we decided not to lacking an immanent use case.

Reviewed-by: Andreas Färber <afaerber@suse.de>

Andreas
Markus Armbruster - Oct. 23, 2012, 11:30 a.m.
Anthony Liguori <aliguori@us.ibm.com> writes:

> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
> ---
>  include/qemu/object.h |   17 ++++++++++++++
>  qom/object.c          |   58 +++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 75 insertions(+), 0 deletions(-)
>
> diff --git a/include/qemu/object.h b/include/qemu/object.h
> index cc75fee..f4dc2ea 100644
> --- a/include/qemu/object.h
> +++ b/include/qemu/object.h
> @@ -947,6 +947,23 @@ void object_property_add_str(Object *obj, const char *name,
>                               struct Error **errp);
>  
>  /**
> + * object_property_add_bool:
> + * @obj: the object 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().

Pasto.  It returns a bool.

> + * @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 a bool property using getters/setters.  This function will add a
> + * property of type 'bool'.
> + */
> +void object_property_add_bool(Object *obj, const char *name,
> +                              bool (*get)(Object *, struct Error **),
> +                              void (*set)(Object *, bool, struct Error **),
> +                              struct Error **errp);
> +
> +/**
>   * object_child_foreach:
>   * @obj: the object whose children will be navigated
>   * @fn: the iterator function to be called
[...]

Patch

diff --git a/include/qemu/object.h b/include/qemu/object.h
index cc75fee..f4dc2ea 100644
--- a/include/qemu/object.h
+++ b/include/qemu/object.h
@@ -947,6 +947,23 @@  void object_property_add_str(Object *obj, const char *name,
                              struct Error **errp);
 
 /**
+ * object_property_add_bool:
+ * @obj: the object 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
+ *
+ * Add a bool property using getters/setters.  This function will add a
+ * property of type 'bool'.
+ */
+void object_property_add_bool(Object *obj, const char *name,
+                              bool (*get)(Object *, struct Error **),
+                              void (*set)(Object *, bool, struct Error **),
+                              struct Error **errp);
+
+/**
  * object_child_foreach:
  * @obj: the object whose children will be navigated
  * @fn: the iterator function to be called
diff --git a/qom/object.c b/qom/object.c
index e3e9242..efa25ee 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -1183,6 +1183,64 @@  void object_property_add_str(Object *obj, const char *name,
                         prop, errp);
 }
 
+typedef struct BoolProperty
+{
+    bool (*get)(Object *, Error **);
+    void (*set)(Object *, bool, Error **);
+} BoolProperty;
+
+static void property_get_bool(Object *obj, Visitor *v, void *opaque,
+                              const char *name, Error **errp)
+{
+    BoolProperty *prop = opaque;
+    bool value;
+
+    value = prop->get(obj, errp);
+    if (value) {
+        visit_type_bool(v, &value, name, errp);
+    }
+}
+
+static void property_set_bool(Object *obj, Visitor *v, void *opaque,
+                              const char *name, Error **errp)
+{
+    BoolProperty *prop = opaque;
+    Error *local_err = NULL;
+    bool value;
+
+    visit_type_bool(v, &value, name, &local_err);
+    if (local_err) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
+    prop->set(obj, value, errp);
+}
+
+static void property_release_bool(Object *obj, const char *name,
+                                  void *opaque)
+{
+    BoolProperty *prop = opaque;
+    g_free(prop);
+}
+
+void object_property_add_bool(Object *obj, const char *name,
+                              bool (*get)(Object *, Error **),
+                              void (*set)(Object *, bool, Error **),
+                              Error **errp)
+{
+    BoolProperty *prop = g_malloc0(sizeof(*prop));
+
+    prop->get = get;
+    prop->set = set;
+
+    object_property_add(obj, name, "bool",
+                        get ? property_get_bool : NULL,
+                        set ? property_set_bool : NULL,
+                        property_release_bool,
+                        prop, errp);
+}
+
 static char *qdev_get_type(Object *obj, Error **errp)
 {
     return g_strdup(object_get_typename(obj));