From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:40022) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UamG3-00049q-Vf for qemu-devel@nongnu.org; Fri, 10 May 2013 08:17:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UamG2-0007mf-Q5 for qemu-devel@nongnu.org; Fri, 10 May 2013 08:17:07 -0400 Received: from mail-wg0-x229.google.com ([2a00:1450:400c:c00::229]:46255) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UamG2-0007mX-Kv for qemu-devel@nongnu.org; Fri, 10 May 2013 08:17:06 -0400 Received: by mail-wg0-f41.google.com with SMTP id y10so583492wgg.2 for ; Fri, 10 May 2013 05:17:06 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Fri, 10 May 2013 14:16:43 +0200 Message-Id: <1368188203-3407-10-git-send-email-pbonzini@redhat.com> In-Reply-To: <1368188203-3407-1-git-send-email-pbonzini@redhat.com> References: <1368188203-3407-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH for-1.5 9/9] qom: simplify object_class_dynamic_cast, part 2 List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Anthony Liguori , Andreas Faerber , Aurelien Jarno , mst@redhat.com Detect ambiguous matches right away, without going through all interfaces first. Signed-off-by: Paolo Bonzini --- 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