Patchwork [v2,02/24] qom: add object_child_foreach

login
register
mail settings
Submitter Paolo Bonzini
Date April 11, 2012, 9:30 p.m.
Message ID <1334179842-6061-2-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/151887/
State New
Headers show

Comments

Paolo Bonzini - April 11, 2012, 9:30 p.m.
A utility function that will be used to implement hierarchical realization.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 include/qemu/object.h |   14 +++++++++++++-
 qom/object.c          |   17 +++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)
Andreas Färber - April 12, 2012, 4:03 p.m.
Am 11.04.2012 23:30, schrieb Paolo Bonzini:
> A utility function that will be used to implement hierarchical realization.
> 
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
> ---
>  include/qemu/object.h |   14 +++++++++++++-
>  qom/object.c          |   17 +++++++++++++++++
>  2 files changed, 30 insertions(+), 1 deletion(-)
> 
> diff --git a/include/qemu/object.h b/include/qemu/object.h
> index f814521..470efe3 100644
> --- a/include/qemu/object.h
> +++ b/include/qemu/object.h
> @@ -918,6 +918,19 @@ void object_property_add_str(Object *obj, const char *name,
>                               struct Error **errp);
>  
>  /**
> + * object_child_foreach:
> + * @obj: the object whose children will be navigated
> + * @fn: the iterator function to be called
> + * @opaque: an opaque value that will be passed to the iterator
> + *
> + * Call @fn passing each child of @obj and @opaque to it, until @fn returns
> + * non-zero.  Return the last value returned by @fn, or 0 if there is no
> + * child.
> + */
> +int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
> +                         void *opaque);
> +
> +/**
>   * container_get:
>   * @path: path to the container
>   *
> @@ -928,5 +941,4 @@ void object_property_add_str(Object *obj, const char *name,
>   */
>  Object *container_get(const char *path);
>  
> -

Unrelated whitespace change. I intentionally leave two empty lines
before #endif in my header files. Otherwise looks okay.

Andreas

>  #endif
> diff --git a/qom/object.c b/qom/object.c
> index 3e9fed7..4975d93 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -597,6 +597,23 @@ void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
>      g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
>  }
>  
> +int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
> +                         void *opaque)
> +{
> +    ObjectProperty *prop;
> +    int ret = 0;
> +
> +    QTAILQ_FOREACH(prop, &obj->properties, node) {
> +        if (strstart(prop->type, "child<", NULL)) {
> +            ret = fn(prop->opaque, opaque);
> +            if (ret != 0) {
> +                break;
> +            }
> +        }
> +    }
> +    return ret;
> +}
> +
>  static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
>  {
>      GSList **list = opaque;
Andreas Färber - May 11, 2012, 2:59 p.m.
Am 12.04.2012 18:03, schrieb Andreas Färber:
> Am 11.04.2012 23:30, schrieb Paolo Bonzini:
>> A utility function that will be used to implement hierarchical realization.
>>
>> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Reviewed-by: Anthony Liguori <aliguori@us.ibm.com>

>> ---
>>  include/qemu/object.h |   14 +++++++++++++-
>>  qom/object.c          |   17 +++++++++++++++++
>>  2 files changed, 30 insertions(+), 1 deletion(-)
>>
>> diff --git a/include/qemu/object.h b/include/qemu/object.h
>> index f814521..470efe3 100644
>> --- a/include/qemu/object.h
>> +++ b/include/qemu/object.h
>> @@ -918,6 +918,19 @@ void object_property_add_str(Object *obj, const char *name,
>>                               struct Error **errp);
>>  
>>  /**
>> + * object_child_foreach:
>> + * @obj: the object whose children will be navigated
>> + * @fn: the iterator function to be called
>> + * @opaque: an opaque value that will be passed to the iterator
>> + *
>> + * Call @fn passing each child of @obj and @opaque to it, until @fn returns
>> + * non-zero.  Return the last value returned by @fn, or 0 if there is no
>> + * child.

I've turned the last sentence into a gtk-doc Returns: statement.

>> + */
>> +int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
>> +                         void *opaque);
>> +
>> +/**
>>   * container_get:
>>   * @path: path to the container
>>   *
>> @@ -928,5 +941,4 @@ void object_property_add_str(Object *obj, const char *name,
>>   */
>>  Object *container_get(const char *path);
>>  
>> -
> 
> Unrelated whitespace change.

Thanks, applied to qom-next (with whitespace change dropped):
http://repo.or.cz/w/qemu/afaerber.git/shortlog/refs/heads/qom-next

/-F

Patch

diff --git a/include/qemu/object.h b/include/qemu/object.h
index f814521..470efe3 100644
--- a/include/qemu/object.h
+++ b/include/qemu/object.h
@@ -918,6 +918,19 @@  void object_property_add_str(Object *obj, const char *name,
                              struct Error **errp);
 
 /**
+ * object_child_foreach:
+ * @obj: the object whose children will be navigated
+ * @fn: the iterator function to be called
+ * @opaque: an opaque value that will be passed to the iterator
+ *
+ * Call @fn passing each child of @obj and @opaque to it, until @fn returns
+ * non-zero.  Return the last value returned by @fn, or 0 if there is no
+ * child.
+ */
+int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
+                         void *opaque);
+
+/**
  * container_get:
  * @path: path to the container
  *
@@ -928,5 +941,4 @@  void object_property_add_str(Object *obj, const char *name,
  */
 Object *container_get(const char *path);
 
-
 #endif
diff --git a/qom/object.c b/qom/object.c
index 3e9fed7..4975d93 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -597,6 +597,23 @@  void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
     g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
 }
 
+int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
+                         void *opaque)
+{
+    ObjectProperty *prop;
+    int ret = 0;
+
+    QTAILQ_FOREACH(prop, &obj->properties, node) {
+        if (strstart(prop->type, "child<", NULL)) {
+            ret = fn(prop->opaque, opaque);
+            if (ret != 0) {
+                break;
+            }
+        }
+    }
+    return ret;
+}
+
 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
 {
     GSList **list = opaque;