Patchwork [v2,12/27] qom: add property get/set wrappers for links

login
register
mail settings
Submitter Paolo Bonzini
Date Feb. 4, 2012, 8:02 a.m.
Message ID <1328342577-25732-13-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/139549/
State New
Headers show

Comments

Paolo Bonzini - Feb. 4, 2012, 8:02 a.m.
These can set a link to any object, as long as it is included in
the composition tree.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qemu/object.h |   24 ++++++++++++++++++++++++
 qom/object.c          |   25 +++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 0 deletions(-)
Anthony Liguori - Feb. 6, 2012, 2:27 p.m.
On 02/04/2012 02:02 AM, Paolo Bonzini wrote:
> These can set a link to any object, as long as it is included in
> the composition tree.
>
> Signed-off-by: Paolo Bonzini<pbonzini@redhat.com>
> ---
>   include/qemu/object.h |   24 ++++++++++++++++++++++++
>   qom/object.c          |   25 +++++++++++++++++++++++++
>   2 files changed, 49 insertions(+), 0 deletions(-)
>
> diff --git a/include/qemu/object.h b/include/qemu/object.h
> index 4ec7942..910b767 100644
> --- a/include/qemu/object.h
> +++ b/include/qemu/object.h
> @@ -658,6 +658,30 @@ char *object_property_get_str(Object *obj, const char *name,
>                                 struct Error **errp);
>
>   /**
> + * object_property_set_link:
> + * @value: the value to be written to the property
> + * @name: the name of the property
> + * @errp: returns an error if this function fails
> + *
> + * Writes an object's canonical path to a property.
> + */
> +void object_property_set_link(Object *obj, Object *value,
> +                              const char *name, struct Error **errp);
> +
> +/**
> + * object_property_get_link:
> + * @obj: the object
> + * @name: the name of the property
> + * @errp: returns an error if this function fails
> + *
> + * Returns: the value of the property, resolved from a path to an Object,
> + * or NULL if an error occurs (including when the property value is not a
> + * string or not a valid object path).
> + */
> +Object *object_property_get_link(Object *obj, const char *name,
> +                                 struct Error **errp);
> +
> +/**
>    * object_property_set_bool:
>    * @value: the value to be written to the property
>    * @name: the name of the property
> diff --git a/qom/object.c b/qom/object.c
> index e8418bc..b3cff50 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -710,6 +710,30 @@ char *object_property_get_str(Object *obj, const char *name,
>       return retval;
>   }
>
> +void object_property_set_link(Object *obj, Object *value,
> +                              const char *name, Error **errp)
> +{
> +    object_property_set_str(obj, object_get_canonical_path(value),
> +                            name, errp);
> +}

We could fall back to setting the link directly via the property opaque, but I 
guess that might let us get lazy about filling in the composition tree.

Regards,

Anthony Liguori

> +
> +Object *object_property_get_link(Object *obj, const char *name,
> +                                 Error **errp)
> +{
> +    char *str = object_property_get_str(obj, name, errp);
> +    Object *target = NULL;
> +
> +    if (str&&  *str) {
> +        target = object_resolve_path(str, NULL);
> +        if (!target) {
> +            error_set(errp, QERR_DEVICE_NOT_FOUND, str);
> +        }
> +    }
> +
> +    g_free(str);
> +    return target;
> +}
> +
>   void object_property_set_bool(Object *obj, bool value,
>                                 const char *name, Error **errp)
>   {

Patch

diff --git a/include/qemu/object.h b/include/qemu/object.h
index 4ec7942..910b767 100644
--- a/include/qemu/object.h
+++ b/include/qemu/object.h
@@ -658,6 +658,30 @@  char *object_property_get_str(Object *obj, const char *name,
                               struct Error **errp);
 
 /**
+ * object_property_set_link:
+ * @value: the value to be written to the property
+ * @name: the name of the property
+ * @errp: returns an error if this function fails
+ *
+ * Writes an object's canonical path to a property.
+ */
+void object_property_set_link(Object *obj, Object *value,
+                              const char *name, struct Error **errp);
+
+/**
+ * object_property_get_link:
+ * @obj: the object
+ * @name: the name of the property
+ * @errp: returns an error if this function fails
+ *
+ * Returns: the value of the property, resolved from a path to an Object,
+ * or NULL if an error occurs (including when the property value is not a
+ * string or not a valid object path).
+ */
+Object *object_property_get_link(Object *obj, const char *name,
+                                 struct Error **errp);
+
+/**
  * object_property_set_bool:
  * @value: the value to be written to the property
  * @name: the name of the property
diff --git a/qom/object.c b/qom/object.c
index e8418bc..b3cff50 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -710,6 +710,30 @@  char *object_property_get_str(Object *obj, const char *name,
     return retval;
 }
 
+void object_property_set_link(Object *obj, Object *value,
+                              const char *name, Error **errp)
+{
+    object_property_set_str(obj, object_get_canonical_path(value),
+                            name, errp);
+}
+
+Object *object_property_get_link(Object *obj, const char *name,
+                                 Error **errp)
+{
+    char *str = object_property_get_str(obj, name, errp);
+    Object *target = NULL;
+
+    if (str && *str) {
+        target = object_resolve_path(str, NULL);
+        if (!target) {
+            error_set(errp, QERR_DEVICE_NOT_FOUND, str);
+        }
+    }
+
+    g_free(str);
+    return target;
+}
+
 void object_property_set_bool(Object *obj, bool value,
                               const char *name, Error **errp)
 {