[Telepathy-commits] [telepathy-glib/master] util: add tp_mixin_instance_get_offset, tp_mixin_class_get_offset

Simon McVittie simon.mcvittie at collabora.co.uk
Mon Dec 1 03:13:53 PST 2008


These are like the old mixin macros, but if retrieving qdata from the
given type fails, they will walk up the inheritance tree all the way to
GObject looking for qdata. This is necessary to allow objects with mixins
to be subclassed, closing fd.o #15092.
---
 docs/reference/telepathy-glib-sections.txt |    2 +
 telepathy-glib/util.c                      |   63 ++++++++++++++++++++++++++++
 telepathy-glib/util.h                      |    2 +
 3 files changed, 67 insertions(+), 0 deletions(-)

diff --git a/docs/reference/telepathy-glib-sections.txt b/docs/reference/telepathy-glib-sections.txt
index 25ec168..1c5456e 100644
--- a/docs/reference/telepathy-glib-sections.txt
+++ b/docs/reference/telepathy-glib-sections.txt
@@ -930,6 +930,8 @@ tp_g_value_slice_free
 tp_g_value_slice_dup
 tp_strdiff
 tp_mixin_offset_cast
+tp_mixin_class_get_offset
+tp_mixin_instance_get_offset
 tp_escape_as_identifier
 tp_strv_contains
 </SECTION>
diff --git a/telepathy-glib/util.c b/telepathy-glib/util.c
index f1da86a..05cf474 100644
--- a/telepathy-glib/util.c
+++ b/telepathy-glib/util.c
@@ -211,6 +211,69 @@ tp_mixin_offset_cast (gpointer instance, guint offset)
   return ((guchar *) instance + offset);
 }
 
+
+/**
+ * tp_mixin_instance_get_offset:
+ * @instance: A pointer to a GObject-derived instance structure
+ * @quark: A quark that was used to store the offset with g_type_set_qdata()
+ *
+ * If the type of @instance, or any of its ancestor types, has had an offset
+ * attached using qdata with the given @quark, return that offset. If not,
+ * this indicates a programming error and results are undefined.
+ *
+ * This is used to implement the telepathy-glib mixin classes.
+ */
+guint
+tp_mixin_instance_get_offset (gpointer instance,
+                              GQuark quark)
+{
+  GType t;
+
+  for (t = G_OBJECT_TYPE (instance);
+       t != 0;
+       t = g_type_parent (t))
+    {
+      gpointer qdata = g_type_get_qdata (t, quark);
+
+      if (qdata != NULL)
+        return GPOINTER_TO_UINT (qdata);
+    }
+
+  g_return_val_if_reached (0);
+}
+
+
+/**
+ * tp_mixin_class_get_offset:
+ * @klass: A pointer to a GObjectClass-derived class structure
+ * @quark: A quark that was used to store the offset with g_type_set_qdata()
+ *
+ * If the type of @klass, or any of its ancestor types, has had an offset
+ * attached using qdata with the given @quark, return that offset. If not,
+ * this indicates a programming error and results are undefined.
+ *
+ * This is used to implement the telepathy-glib mixin classes.
+ */
+guint
+tp_mixin_class_get_offset (gpointer klass,
+                           GQuark quark)
+{
+  GType t;
+
+  for (t = G_OBJECT_CLASS_TYPE (klass);
+       t != 0;
+       t = g_type_parent (t))
+    {
+      gpointer qdata = g_type_get_qdata (t, quark);
+
+      if (qdata != NULL)
+        return GPOINTER_TO_UINT (qdata);
+    }
+
+  g_return_val_if_reached (0);
+}
+
+
 static inline gboolean
 _esc_ident_bad (gchar c, gboolean is_first)
 {
diff --git a/telepathy-glib/util.h b/telepathy-glib/util.h
index bb65978..dcc0763 100644
--- a/telepathy-glib/util.h
+++ b/telepathy-glib/util.h
@@ -41,6 +41,8 @@ void tp_g_hash_table_update (GHashTable *target, GHashTable *source,
 gboolean tp_strdiff (const gchar *left, const gchar *right);
 
 gpointer tp_mixin_offset_cast (gpointer instance, guint offset);
+guint tp_mixin_instance_get_offset (gpointer instance, GQuark quark);
+guint tp_mixin_class_get_offset (gpointer klass, GQuark quark);
 
 gchar *tp_escape_as_identifier (const gchar *name);
 
-- 
1.5.6.5




More information about the Telepathy-commits mailing list