[glib] gtype: Add check for fundamental instance type



commit 6072e3650fba8ff57b5c3f135f81488c83f27f0b
Author: Edward Hervey <edward collabora com>
Date:   Wed May 28 10:57:28 2014 +0200

    gtype: Add check for fundamental instance type
    
    When checking whether an instance is of a given fundamental type (such
    as G_TYPE_OBJECT), we can avoid over 60%+ of the cost of checking types.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=730984

 docs/reference/gobject/gobject-sections.txt |    2 ++
 gobject/gtype.c                             |    9 +++++++++
 gobject/gtype.h                             |   16 ++++++++++++++++
 3 files changed, 27 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gobject/gobject-sections.txt b/docs/reference/gobject/gobject-sections.txt
index 5259f42..5a78278 100644
--- a/docs/reference/gobject/gobject-sections.txt
+++ b/docs/reference/gobject/gobject-sections.txt
@@ -34,6 +34,7 @@ G_TYPE_CLASS_GET_PRIVATE
 G_TYPE_CHECK_INSTANCE
 G_TYPE_CHECK_INSTANCE_CAST
 G_TYPE_CHECK_INSTANCE_TYPE
+G_TYPE_CHECK_INSTANCE_FUNDAMENTAL_TYPE
 G_TYPE_CHECK_CLASS_CAST
 G_TYPE_CHECK_CLASS_TYPE
 G_TYPE_CHECK_VALUE
@@ -125,6 +126,7 @@ G_TYPE_FUNDAMENTAL_SHIFT
 g_type_check_instance
 g_type_check_instance_cast
 g_type_check_instance_is_a
+g_type_check_instance_is_fundamentally_a
 g_type_check_class_cast
 g_type_check_class_is_a
 g_type_check_is_value_type
diff --git a/gobject/gtype.c b/gobject/gtype.c
index 3db19e6..371a0f2 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -3975,6 +3975,15 @@ g_type_check_instance_is_a (GTypeInstance *type_instance,
 }
 
 gboolean
+g_type_check_instance_is_fundamentally_a (GTypeInstance *type_instance,
+                                          GType          fundamental_type)
+{
+  if (!type_instance || !type_instance->g_class)
+    return FALSE;
+  return NODE_FUNDAMENTAL_TYPE(lookup_type_node_I (type_instance->g_class->g_type)) == fundamental_type;
+}
+
+gboolean
 g_type_check_class_is_a (GTypeClass *type_class,
                         GType       is_a_type)
 {
diff --git a/gobject/gtype.h b/gobject/gtype.h
index 55efeea..81803fc 100644
--- a/gobject/gtype.h
+++ b/gobject/gtype.h
@@ -490,6 +490,18 @@ struct _GTypeQuery
  */
 #define G_TYPE_CHECK_INSTANCE_TYPE(instance, g_type)            (_G_TYPE_CIT ((instance), (g_type)))
 /**
+ * G_TYPE_CHECK_INSTANCE_FUNDAMENTAL_TYPE:
+ * @instance: Location of a #GTypeInstance structure.
+ * @g_type: The fundamental type to be checked
+ *
+ * Checks if @instance is an instance of the fundamental type identified by @g_type.
+ *
+ * This macro should only be used in type implementations.
+ *
+ * Returns: %TRUE on success.
+ */
+#define G_TYPE_CHECK_INSTANCE_FUNDAMENTAL_TYPE(instance, g_type)            (_G_TYPE_CIFT ((instance), 
(g_type)))
+/**
  * G_TYPE_INSTANCE_GET_CLASS:
  * @instance: Location of the #GTypeInstance structure.
  * @g_type: The #GType of the class to be returned.
@@ -1895,6 +1907,9 @@ GTypeInstance*   g_type_check_instance_cast     (GTypeInstance      *instance,
 GLIB_AVAILABLE_IN_ALL
 gboolean         g_type_check_instance_is_a    (GTypeInstance      *instance,
                                                 GType               iface_type) G_GNUC_PURE;
+GLIB_AVAILABLE_IN_2_42
+gboolean         g_type_check_instance_is_fundamentally_a (GTypeInstance *instance,
+                                                           GType          fundamental_type) G_GNUC_PURE;
 GLIB_AVAILABLE_IN_ALL
 GTypeClass*      g_type_check_class_cast        (GTypeClass         *g_class,
                                                 GType               is_a_type);
@@ -1934,6 +1949,7 @@ const gchar *    g_type_name_from_class         (GTypeClass       *g_class);
 #define _G_TYPE_CHV(vl)                        (g_type_check_value ((GValue*) vl))
 #define _G_TYPE_IGC(ip, gt, ct)         ((ct*) (((GTypeInstance*) ip)->g_class))
 #define _G_TYPE_IGI(ip, gt, ct)         ((ct*) g_type_interface_peek (((GTypeInstance*) ip)->g_class, gt))
+#define _G_TYPE_CIFT(ip, ft)            (g_type_check_instance_is_fundamentally_a ((GTypeInstance*) ip, ft))
 #ifdef __GNUC__
 #  define _G_TYPE_CIT(ip, gt)             (G_GNUC_EXTENSION ({ \
   GTypeInstance *__inst = (GTypeInstance*) ip; GType __t = gt; gboolean __r; \


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