From patchwork Tue Jun 18 09:44:48 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Crosthwaite X-Patchwork-Id: 252206 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1A5252C009F for ; Tue, 18 Jun 2013 19:52:17 +1000 (EST) Received: from localhost ([::1]:55215 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UosaF-0007rU-1T for incoming@patchwork.ozlabs.org; Tue, 18 Jun 2013 05:52:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56087) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UosZ5-0006kG-I9 for qemu-devel@nongnu.org; Tue, 18 Jun 2013 05:51:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UosZ3-0007k9-W1 for qemu-devel@nongnu.org; Tue, 18 Jun 2013 05:51:03 -0400 Received: from mail-pb0-x232.google.com ([2607:f8b0:400e:c01::232]:54568) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UosZ3-0007jf-O6 for qemu-devel@nongnu.org; Tue, 18 Jun 2013 05:51:01 -0400 Received: by mail-pb0-f50.google.com with SMTP id wz7so3687541pbc.23 for ; Tue, 18 Jun 2013 02:51:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=DTxk9H6Msty/33VMNdBRMBVJARX7TEiz36U8Pnpe8Rw=; b=pE7G2W5oJBLcBPn1DObg9XqHfKAoYa88pgcKDxEVDLUTOnudOfwSUOkZBsPDhy34R4 MiJP3gjq5qLp4YVrS5C0BmLHXtCsjVZ6QHQjqIB3T2GyDGdoFbjTI37pdBxOQMPIngcn nk/tswKNFl430klMGdtgek7ATAbf87Mx9eFX2+c5Gd08y5zkJEMV0J4cK3BwwwEYR5Tb rS/HSON3RT06cpYEQdacbh8JNeiKtITJb/7hTrzF93qjoYHvyjc9ZV6uTcX3coQjUxT5 ErO//yuzjRxtWQGaHCwYUEykikQkqMT7Nc6/dcRk6zHgcG4LVEc16LK4hhpi9F4+ZX78 oyBg== X-Received: by 10.68.129.100 with SMTP id nv4mr16286325pbb.178.1371549060850; Tue, 18 Jun 2013 02:51:00 -0700 (PDT) Received: from localhost ([203.126.243.116]) by mx.google.com with ESMTPSA id dc3sm17570275pbc.9.2013.06.18.02.50.53 for (version=TLSv1.1 cipher=RC4-SHA bits=128/128); Tue, 18 Jun 2013 02:50:59 -0700 (PDT) From: peter.crosthwaite@xilinx.com To: qemu-devel@nongnu.org Date: Tue, 18 Jun 2013 19:44:48 +1000 Message-Id: <69668ba381a64ff08a4cb7fcc9e2329a867a266b.1371548267.git.peter.crosthwaite@xilinx.com> X-Mailer: git-send-email 1.8.3.rc1.44.gb387c77.dirty In-Reply-To: References: X-Gm-Message-State: ALoCoQn7XuTeu49luSufke41a6QBJ7jQFBCgdzM+3DjK9Kdl3rZevDmHflKXBkYCb+JLYcR6qD/C X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400e:c01::232 Cc: peter.maydell@linaro.org, aliguori@us.ibm.com, mst@redhat.com, pbonzini@redhat.com, edgar.iglesias@gmail.com, afaerber@suse.de Subject: [Qemu-devel] [RFC PATCH v1 2/7] qom: Add super class accessor X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Peter Crosthwaite Add a function that allows obtaining a super class implementation of a given class. The super classing is done relative to a specified level. See added documentation comment for details. Signed-off-by: Peter Crosthwaite --- include/qom/object.h | 18 ++++++++++++++++++ qom/object.c | 15 +++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/qom/object.h b/include/qom/object.h index 23fc048..034c87c 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -706,6 +706,24 @@ ObjectClass *object_class_dynamic_cast(ObjectClass *klass, ObjectClass *object_class_get_parent(ObjectClass *klass); /** + * object_class_get_super: + * @klass: The class to obtain the super class for. + * @typename: The typename of the class level to super + * + * Returns: The super for @klass or %NULL if none. + * + * An example - suppose you have a inheritance heirachy: + * (parent) TYPE_A <- TYPE_B <- TYPE_C (concrete) + * object_class_get_parent(klass, TYPE_B) will return + * the ObjectClass for TYPE_A, if klass is either of TYPE_B + * or TYPE_C (or any other child class of TYPE_B). + * If typename is null, the typename is inferred as the concrete + * class level (functionaly identical to get_parent()). + */ + +ObjectClass *object_class_get_super(ObjectClass *klass, + const char *typename); +/** * object_class_get_name: * @klass: The class to obtain the QOM typename for. * diff --git a/qom/object.c b/qom/object.c index 803b94b..69d4889 100644 --- a/qom/object.c +++ b/qom/object.c @@ -608,6 +608,21 @@ ObjectClass *object_class_get_parent(ObjectClass *class) return type->class; } +/* FIXME: If this starts getting used in critical paths + * a cast cache should be added to save on the strcmp()s + */ + +ObjectClass *object_class_get_super(ObjectClass *class, + const char *typename) +{ + /* Move up to the specified typelevel */ + while (class && typename && strcmp(class->type->name, typename)) { + class = object_class_get_parent(class); + } + /* and go one more for the super class */ + return class ? object_class_get_parent(class) : NULL; +} + typedef struct OCFData { void (*fn)(ObjectClass *klass, void *opaque);