Patchwork [RFC,v1,2/7] qom: Add super class accessor

login
register
mail settings
Submitter Peter Crosthwaite
Date June 18, 2013, 9:44 a.m.
Message ID <69668ba381a64ff08a4cb7fcc9e2329a867a266b.1371548267.git.peter.crosthwaite@xilinx.com>
Download mbox | patch
Permalink /patch/252206/
State New
Headers show

Comments

Peter Crosthwaite - June 18, 2013, 9:44 a.m.
From: Peter Crosthwaite <peter.crosthwaite@xilinx.com>

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 <peter.crosthwaite@xilinx.com>
---

 include/qom/object.h | 18 ++++++++++++++++++
 qom/object.c         | 15 +++++++++++++++
 2 files changed, 33 insertions(+)

Patch

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);