qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: David Gibson <david@gibson.dropbear.id.au>
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
	qemu-ppc@nongnu.org, "Cédric Le Goater" <clg@kaod.org>,
	qemu-devel@nongnu.org
Subject: [PATCH 4/6] qom: Add object_child_foreach_type() helper function
Date: Wed, 23 Oct 2019 16:52:16 +0200	[thread overview]
Message-ID: <157184233616.3053790.246919545000657597.stgit@bahia.lan> (raw)
In-Reply-To: <157184231371.3053790.17713393349394736594.stgit@bahia.lan>

Calling a function for children of a certain type is a recurring pattern
in the QEMU code base. In order to avoid the need to setup the same boiler
plate again and again, introduce a variant of object_child_foreach() that
only considers children of the given type.

Signed-off-by: Greg Kurz <groug@kaod.org>
---
 include/qom/object.h |   35 +++++++++++++++++++++++++++++++++++
 qom/object.c         |   30 +++++++++++++++++++++++-------
 2 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/include/qom/object.h b/include/qom/object.h
index 128d00c77fd6..e9e3c2eae8ed 100644
--- a/include/qom/object.h
+++ b/include/qom/object.h
@@ -1728,6 +1728,41 @@ int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
 int object_child_foreach_recursive(Object *obj,
                                    int (*fn)(Object *child, void *opaque),
                                    void *opaque);
+
+/**
+ * object_child_foreach_type:
+ * @obj: the object whose children will be navigated
+ * @typename: the type of child objects to consider
+ * @fn: the iterator function to be called
+ * @opaque: an opaque value that will be passed to the iterator
+ *
+ * This is similar to object_child_foreach, but it only calls @fn for
+ * child objects of the give @typename.
+ *
+ * Returns: The last value returned by @fn, or 0 if there is no child of
+ * the given @typename.
+ */
+int object_child_foreach_type(Object *obj, const char *typename,
+                              int (*fn)(Object *child, void *opaque),
+                              void *opaque);
+
+/**
+ * object_child_foreach_recursive_type:
+ * @obj: the object whose children will be navigated
+ * @typename: the type of child objects to consider
+ * @fn: the iterator function to be called
+ * @opaque: an opaque value that will be passed to the iterator
+ *
+ * This is similar to object_child_foreach_recursive, but it only calls
+ * @fn for child objects of the give @typename.
+ *
+ * Returns: The last value returned by @fn, or 0 if there is no child of
+ * the given @typename.
+ */
+int object_child_foreach_recursive_type(Object *obj, const char *typename,
+                                        int (*fn)(Object *child, void *opaque),
+                                        void *opaque);
+
 /**
  * container_get:
  * @root: root of the #path, e.g., object_get_root()
diff --git a/qom/object.c b/qom/object.c
index 6fa9c619fac4..a2dec1261ff7 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -986,7 +986,7 @@ void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
     enumerating_types = false;
 }
 
-static int do_object_child_foreach(Object *obj,
+static int do_object_child_foreach(Object *obj, const char *typename,
                                    int (*fn)(Object *child, void *opaque),
                                    void *opaque, bool recurse)
 {
@@ -999,12 +999,14 @@ static int do_object_child_foreach(Object *obj,
         if (object_property_is_child(prop)) {
             Object *child = prop->opaque;
 
-            ret = fn(child, opaque);
-            if (ret != 0) {
-                break;
+            if (!typename || object_dynamic_cast(child, typename)) {
+                ret = fn(child, opaque);
+                if (ret != 0) {
+                    break;
+                }
             }
             if (recurse) {
-                do_object_child_foreach(child, fn, opaque, true);
+                do_object_child_foreach(child, typename, fn, opaque, true);
             }
         }
     }
@@ -1014,14 +1016,28 @@ static int do_object_child_foreach(Object *obj,
 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
                          void *opaque)
 {
-    return do_object_child_foreach(obj, fn, opaque, false);
+    return do_object_child_foreach(obj, NULL, fn, opaque, false);
 }
 
 int object_child_foreach_recursive(Object *obj,
                                    int (*fn)(Object *child, void *opaque),
                                    void *opaque)
 {
-    return do_object_child_foreach(obj, fn, opaque, true);
+    return do_object_child_foreach(obj, NULL, fn, opaque, true);
+}
+
+int object_child_foreach_type(Object *obj, const char *typename,
+                              int (*fn)(Object *child, void *opaque),
+                              void *opaque)
+{
+    return do_object_child_foreach(obj, typename, fn, opaque, false);
+}
+
+int object_child_foreach_recursive_type(Object *obj, const char *typename,
+                                        int (*fn)(Object *child, void *opaque),
+                                        void *opaque)
+{
+    return do_object_child_foreach(obj, typename, fn, opaque, true);
 }
 
 static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)



  parent reply	other threads:[~2019-10-23 15:07 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-23 14:51 [PATCH 0/6] ppc: Reparent the interrupt presenter Greg Kurz
2019-10-23 14:51 ` [PATCH 1/6] ppc: Add intc_destroy() handlers to SpaprInterruptController/PnvChip Greg Kurz
2019-10-24  2:50   ` David Gibson
2019-10-24  7:20     ` Greg Kurz
2019-10-23 14:52 ` [PATCH 2/6] xive, xics: Fix reference counting on CPU objects Greg Kurz
2019-10-24  2:50   ` David Gibson
2019-10-23 14:52 ` [PATCH 3/6] ppc: Reparent presenter objects to the interrupt controller object Greg Kurz
2019-10-24  2:58   ` David Gibson
2019-10-24  9:04     ` Greg Kurz
2019-10-27 16:57       ` David Gibson
2019-10-23 14:52 ` Greg Kurz [this message]
2019-10-24  2:59   ` [PATCH 4/6] qom: Add object_child_foreach_type() helper function David Gibson
2019-10-24  3:07     ` David Gibson
2019-10-24  9:20       ` Greg Kurz
2019-10-23 14:52 ` [PATCH 5/6] spapr: Don't use CPU_FOREACH() in 'info pic' Greg Kurz
2019-10-24  3:02   ` David Gibson
2019-10-24  9:28     ` Greg Kurz
2019-10-27 17:01       ` David Gibson
2019-10-23 14:52 ` [PATCH 6/6] xive: Don't use CPU_FOREACH() to perform CAM line matching Greg Kurz
2019-10-24  3:05   ` David Gibson
2019-10-24 12:33     ` Greg Kurz
2019-10-27 17:03       ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=157184233616.3053790.246919545000657597.stgit@bahia.lan \
    --to=groug@kaod.org \
    --cc=clg@kaod.org \
    --cc=david@gibson.dropbear.id.au \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).