Patchwork [07/22] qom: allow object_class_foreach to take additional parameters to refine search

login
register
mail settings
Submitter Anthony Liguori
Date Feb. 1, 2012, 7:50 p.m.
Message ID <1328125863-6203-8-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/138989/
State New
Headers show

Comments

Anthony Liguori - Feb. 1, 2012, 7:50 p.m.
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 include/qemu/object.h |    1 +
 qom/object.c          |   18 ++++++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)
Andreas Färber - Feb. 1, 2012, 8:58 p.m.
Am 01.02.2012 20:50, schrieb Anthony Liguori:
> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>

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

Thanks,
Andreas

> ---
>  include/qemu/object.h |    1 +
>  qom/object.c          |   18 ++++++++++++++++--
>  2 files changed, 17 insertions(+), 2 deletions(-)
> 
> diff --git a/include/qemu/object.h b/include/qemu/object.h
> index ba37850..adbcfb1 100644
> --- a/include/qemu/object.h
> +++ b/include/qemu/object.h
> @@ -431,6 +431,7 @@ const char *object_class_get_name(ObjectClass *klass);
>  ObjectClass *object_class_by_name(const char *typename);
>  
>  void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
> +                          const char *implements_type, bool include_abstract,
>                            void *opaque);
>  
>  #endif
> diff --git a/qom/object.c b/qom/object.c
> index a12895f..3dabb1a 100644
> --- a/qom/object.c
> +++ b/qom/object.c
> @@ -467,6 +467,8 @@ ObjectClass *object_class_by_name(const char *typename)
>  typedef struct OCFData
>  {
>      void (*fn)(ObjectClass *klass, void *opaque);
> +    const char *implements_type;
> +    bool include_abstract;
>      void *opaque;
>  } OCFData;
>  
> @@ -475,16 +477,28 @@ static void object_class_foreach_tramp(gpointer key, gpointer value,
>  {
>      OCFData *data = opaque;
>      TypeImpl *type = value;
> +    ObjectClass *k;
>  
>      type_class_init(type);
> +    k = type->class;
>  
> -    data->fn(value, type->class);
> +    if (!data->include_abstract && type->abstract) {
> +        return;
> +    }
> +
> +    if (data->implements_type && 
> +        !object_class_dynamic_cast(k, data->implements_type)) {
> +        return;
> +    }
> +
> +    data->fn(k, data->opaque);
>  }
>  
>  void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
> +                          const char *implements_type, bool include_abstract,
>                            void *opaque)
>  {
> -    OCFData data = { fn, opaque };
> +    OCFData data = { fn, implements_type, include_abstract, opaque };
>  
>      g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
>  }

Patch

diff --git a/include/qemu/object.h b/include/qemu/object.h
index ba37850..adbcfb1 100644
--- a/include/qemu/object.h
+++ b/include/qemu/object.h
@@ -431,6 +431,7 @@  const char *object_class_get_name(ObjectClass *klass);
 ObjectClass *object_class_by_name(const char *typename);
 
 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
+                          const char *implements_type, bool include_abstract,
                           void *opaque);
 
 #endif
diff --git a/qom/object.c b/qom/object.c
index a12895f..3dabb1a 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -467,6 +467,8 @@  ObjectClass *object_class_by_name(const char *typename)
 typedef struct OCFData
 {
     void (*fn)(ObjectClass *klass, void *opaque);
+    const char *implements_type;
+    bool include_abstract;
     void *opaque;
 } OCFData;
 
@@ -475,16 +477,28 @@  static void object_class_foreach_tramp(gpointer key, gpointer value,
 {
     OCFData *data = opaque;
     TypeImpl *type = value;
+    ObjectClass *k;
 
     type_class_init(type);
+    k = type->class;
 
-    data->fn(value, type->class);
+    if (!data->include_abstract && type->abstract) {
+        return;
+    }
+
+    if (data->implements_type && 
+        !object_class_dynamic_cast(k, data->implements_type)) {
+        return;
+    }
+
+    data->fn(k, data->opaque);
 }
 
 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
+                          const char *implements_type, bool include_abstract,
                           void *opaque)
 {
-    OCFData data = { fn, opaque };
+    OCFData data = { fn, implements_type, include_abstract, opaque };
 
     g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
 }