qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH for-1.5 9/9] qom: simplify object_class_dynamic_cast


From: Paolo Bonzini
Subject: [Qemu-devel] [PATCH for-1.5 9/9] qom: simplify object_class_dynamic_cast, part 2
Date: Fri, 10 May 2013 14:16:43 +0200

Detect ambiguous matches right away, without going through all
interfaces first.

Signed-off-by: Paolo Bonzini <address@hidden>
---
 qom/object.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/qom/object.c b/qom/object.c
index f82f12c..c69e6a3 100644
--- a/qom/object.c
+++ b/qom/object.c
@@ -455,7 +455,6 @@ Object *object_dynamic_cast_assert(Object *obj, const char 
*typename,
 ObjectClass *object_class_dynamic_cast(ObjectClass *class,
                                        const char *typename)
 {
-    ObjectClass *ret = NULL;
     TypeImpl *target_type;
 
     if (!class) {
@@ -475,27 +474,26 @@ ObjectClass *object_class_dynamic_cast(ObjectClass *class,
 
     if (class->interfaces &&
             type_is_ancestor(target_type, type_interface)) {
-        int found = 0;
+        ObjectClass *ret = NULL;
         GSList *i;
 
         for (i = class->interfaces; i; i = i->next) {
             ObjectClass *target_class = i->data;
 
             if (type_is_ancestor(target_class->type, target_type)) {
+                /* If the match is ambiguous, don't allow a cast */
+                if (ret) {
+                    ret = NULL;
+                }
                 ret = target_class;
-                found++;
             }
          }
-
-        /* The match was ambiguous, don't allow a cast */
-        if (found > 1) {
-            ret = NULL;
-        }
+        return ret;
     } else if (type_is_ancestor(class->type, target_type)) {
-        ret = class;
+        return class;
+    } else {
+        return NULL;
     }
-
-    return ret;
 }
 
 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
-- 
1.8.1.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]