Adding a G_TYPE_POINTER



Hi Tim,

I have a patch below to add a G_TYPE_POINTER to the list of fundamental
types (and, as a result, to the options to G_VALUE).

The rationale for having such a type is that:

 * I want to store a function pointer in my tree.  There is no other way
   to use GValue to do such a thing without resorting to gross hacks
   (like casting the pointer to a long, or something.)

 * Other people will want to do this, and will resort to gross hacks
   unless we let them.

May I commit this?

Thanks,
-Jonathan

Index: gobject/gtype.h
===================================================================
RCS file: /cvs/gnome/glib/gobject/gtype.h,v
retrieving revision 1.6
diff -u -r1.6 gtype.h
--- gobject/gtype.h     2000/07/25 22:47:41     1.6
+++ gobject/gtype.h     2000/10/19 00:37:26
@@ -54,6 +54,7 @@
   G_TYPE_UINT,
   G_TYPE_LONG,
   G_TYPE_ULONG,
+  G_TYPE_POINTER,
   G_TYPE_ENUM,
   G_TYPE_FLAGS,
   G_TYPE_FLOAT,
Index: gobject/gvaluetypes.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/gvaluetypes.c,v
retrieving revision 1.1
diff -u -r1.1 gvaluetypes.c
--- gobject/gvaluetypes.c       2000/06/24 22:30:10     1.1
+++ gobject/gvaluetypes.c       2000/10/19 00:37:26
@@ -36,7 +36,49 @@
   dest_value->data[0].v_long = src_value->data[0].v_long;
 }
 
+static void
+value_pointer0_init (GValue *value)
+{
+  value->data[0].v_pointer = 0;
+}
+
+static void
+value_pointer0_copy (const GValue *src_value,
+                    GValue       *dest_value)
+{
+  dest_value->data[0].v_pointer = src_value->data[0].v_pointer;
+}
+
 static gchar*
+value_pointer_collect_value (GValue      *value,
+                            guint        nth_value,
+                            GType       *collect_type,
+                            GTypeCValue *collect_value)
+{
+  value->data[0].v_pointer = collect_value->v_pointer;
+  
+  *collect_type = 0;
+  return NULL;
+}
+
+static gchar*
+value_pointer_lcopy_value (const GValue *value,
+                          guint         nth_value,
+                          GType        *collect_type,
+                          GTypeCValue  *collect_value)
+{
+  gpointer *pointer_p = collect_value->v_pointer;
+  
+  if (!pointer_p)
+    return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
+  
+  *pointer_p = value->data[0].v_pointer;
+  
+  *collect_type = 0;
+  return NULL;
+}
+
+static gchar*
 value_char_lcopy_value (const GValue *value,
                        guint         nth_value,
                        GType        *collect_type,
@@ -353,7 +395,24 @@
     type = g_type_register_fundamental (G_TYPE_ULONG, "gulong", &info, &finfo);
     g_assert (type == G_TYPE_ULONG);
   }
-  
+
+  /* G_TYPE_POINTER
+   */
+  {
+    static const GTypeValueTable value_table = {
+      value_pointer0_init,     /* value_init */
+      NULL,                    /* value_free */
+      value_pointer0_copy,     /* value_copy */
+      G_VALUE_COLLECT_POINTER, /* collect_type */
+      value_pointer_collect_value,     /* collect_value */
+      G_VALUE_COLLECT_POINTER, /* lcopy_type */
+      value_pointer_lcopy_value,       /* lcopy_value */
+    };
+    info.value_table = &value_table;
+    type = g_type_register_fundamental (G_TYPE_POINTER, "gpointer", &info, &finfo);
+    g_assert (type == G_TYPE_POINTER);
+  }
+
   /* G_TYPE_FLOAT
    */
   {
@@ -525,6 +584,23 @@
   g_return_val_if_fail (G_IS_VALUE_ULONG (value), 0);
   
   return value->data[0].v_ulong;
+}
+
+void
+g_value_set_pointer (GValue   *value,
+                    gpointer  v_pointer)
+{
+  g_return_if_fail (G_IS_VALUE_POINTER (value));
+
+  value->data[0].v_pointer = v_pointer;
+}
+
+gpointer
+g_value_get_pointer (GValue *value)
+{
+  g_return_val_if_fail (G_IS_VALUE_POINTER (value), NULL);
+
+  return value->data[0].v_pointer;
 }
 
 void
Index: gobject/gvaluetypes.h
===================================================================
RCS file: /cvs/gnome/glib/gobject/gvaluetypes.h,v
retrieving revision 1.1
diff -u -r1.1 gvaluetypes.h
--- gobject/gvaluetypes.h       2000/06/24 22:30:10     1.1
+++ gobject/gvaluetypes.h       2000/10/19 00:37:26
@@ -38,6 +38,7 @@
 #define G_IS_VALUE_UINT(value)          (G_TYPE_CHECK_CLASS_TYPE ((value), G_TYPE_UINT))
 #define G_IS_VALUE_LONG(value)          (G_TYPE_CHECK_CLASS_TYPE ((value), G_TYPE_LONG))
 #define G_IS_VALUE_ULONG(value)         (G_TYPE_CHECK_CLASS_TYPE ((value), G_TYPE_ULONG))
+#define G_IS_VALUE_POINTER(value)       (G_TYPE_CHECK_CLASS_TYPE ((value), G_TYPE_POINTER))
 #define G_IS_VALUE_FLOAT(value)         (G_TYPE_CHECK_CLASS_TYPE ((value), G_TYPE_FLOAT))
 #define G_IS_VALUE_DOUBLE(value)        (G_TYPE_CHECK_CLASS_TYPE ((value), G_TYPE_DOUBLE))
 #define G_IS_VALUE_STRING(value)        (G_TYPE_CHECK_CLASS_TYPE ((value), G_TYPE_STRING))
@@ -65,6 +66,9 @@
 void            g_value_set_ulong       (GValue         *value,
                                          gulong          v_ulong);
 gulong          g_value_get_ulong       (GValue         *value);
+void            g_value_set_pointer     (GValue         *value,
+                                         gpointer        v_pointer);
+gpointer        g_value_get_pointer     (GValue         *value);
 void            g_value_set_float       (GValue         *value,
                                          gfloat          v_float);
 gfloat          g_value_get_float       (GValue         *value);




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