Varargs type stringification (#50972)
- From: Owen Taylor <otaylor redhat com>
- To: timj gtk org
- Cc: gtk-devel-list gtk org, hp redhat com
- Subject: Varargs type stringification (#50972)
- Date: 27 Mar 2001 11:53:33 -0500
g_strdup_value_contents() is neat, but not all that convenient to
use since:
- You have to construct a value if you don't
- You have to free the results
Here's a patch to add:
gchar* g_strdup_type (GType type,
...);
G_CONST_RETURN char * g_stringify_type (GType type,
...);
Which do the same thing but with GType + varargs. g_stringfy_type()
uses a small ring buffer of static results to make it real easy
to use in debug printfs.
Regards,
Owen
Index: gvaluetypes.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/gvaluetypes.c,v
retrieving revision 1.11
diff -u -r1.11 gvaluetypes.c
--- gvaluetypes.c 2001/03/18 04:44:38 1.11
+++ gvaluetypes.c 2001/03/27 16:47:17
@@ -779,3 +779,90 @@
return contents;
}
+
+static gchar*
+g_strdup_type_valist (GType type,
+ va_list var_args)
+{
+ GValue tmp_value = { 0 };
+ gchar *error = NULL;
+ gchar *value;
+
+ g_value_init (&tmp_value, type);
+
+ G_VALUE_COLLECT (&tmp_value, var_args, 0, &error);
+
+ if (error)
+ {
+ /* we deliberately leak the value here, it might not be
+ * in a sane state if an error condition occoured
+ */
+ g_free (error);
+ value = g_strdup ("???");
+ }
+ else
+ {
+ value = g_strdup_value_contents (&tmp_value);
+ g_value_unset (&tmp_value);
+ }
+
+ return value;
+}
+
+/**
+ * g_strdup_type:
+ * @type: a #GType
+ *
+ * Produce a string representation of the first argument in the
+ * variable argument list, which is of type @type.
+ *
+ * Return value: a string representation of the type. Free with g_free().
+ **/
+gchar*
+g_strdup_type (GType type, ...)
+{
+ gchar *result;
+ va_list var_args;
+
+ va_start (var_args, type);
+ result = g_strdup_type_valist (type, var_args);
+ va_end (var_args);
+
+ return result;
+}
+
+#define N_DEBUG_STRINGS 16
+
+/**
+ * g_stringify_type:
+ * @type: a #GType
+ *
+ * Like g_strdup_type() but the result is a pointer to a static
+ * buffer and doesn't need to be freed. This function is not
+ * thread safe. The static buffers will be recycled after some
+ * number of calls to this function (currently 16.)
+ *
+ * Return value: a string representation of the type. Should
+ * not be freed.
+ **/
+G_CONST_RETURN char *
+g_stringify_type (GType type, ...)
+{
+ static char *debug_strings[N_DEBUG_STRINGS];
+ static int debug_index = 0;
+
+ G_CONST_RETURN char *result;
+ va_list var_args;
+
+ g_free (debug_strings[debug_index]);
+
+ va_start (var_args, type);
+ result = debug_strings[debug_index] = g_strdup_type_valist (type, var_args);
+ va_end (var_args);
+
+ debug_index = (debug_index + 1) % N_DEBUG_STRINGS;
+
+ return result;
+}
+
+
Index: gvaluetypes.h
===================================================================
RCS file: /cvs/gnome/glib/gobject/gvaluetypes.h,v
retrieving revision 1.10
diff -u -r1.10 gvaluetypes.h
--- gvaluetypes.h 2001/03/18 04:44:38 1.10
+++ gvaluetypes.h 2001/03/27 16:47:17
@@ -79,9 +79,12 @@
gpointer g_value_get_pointer (const GValue *value);
-/* debugging aid, describe value contents as string */
+/* debugging aid, describe type/value contents as string */
gchar* g_strdup_value_contents (const GValue *value);
-
+gchar* g_strdup_type (GType type,
+ ...);
+G_CONST_RETURN char * g_stringify_type (GType type,
+ ...);
/* --- marshaller specific --- */
void g_value_set_string_take_ownership (GValue *value,
Index: testgruntime.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/testgruntime.c,v
retrieving revision 1.1
diff -u -r1.1 testgruntime.c
--- testgruntime.c 2001/03/09 14:02:30 1.1
+++ testgruntime.c 2001/03/27 16:47:17
@@ -155,8 +155,22 @@
return test_object_type;
}
+void
+test_type_stringify (void)
+{
+ TestObject *tobject;
+ g_print ("\nTesting stringification...\n");
+ g_print ("int - %s\n", g_stringify_type (G_TYPE_INT, 5));
+ g_print ("double - %s\n", g_stringify_type (G_TYPE_DOUBLE, 5.0));
+ tobject = g_object_new (TEST_TYPE_OBJECT, NULL);
+ g_print ("object - %s\n", g_stringify_type (TEST_TYPE_OBJECT, tobject));
+ g_print ("object (generic) - %s\n", g_stringify_type (G_TYPE_OBJECT, tobject));
+ g_print ("done testing stringification...\n\n");
+ g_object_unref (tobject);
+}
+
int
main (int argc,
char *argv[])
@@ -178,6 +192,8 @@
g_object_unref (sigarg);
g_object_unref (tobject);
+ test_type_stringify();
+
g_message ("%s done", argv[0]);
return 0;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]