[perl-Glib] Ensure that custom signal marshallers are always used



commit c164b23a45dcec2ae7b3f9b06acc01a3f530ca17
Author: Torsten SchÃnfeld <kaffeetisch gmx de>
Date:   Sat Aug 25 20:04:30 2012 +0200

    Ensure that custom signal marshallers are always used
    
    ... irregardless of the spelling of the signal name used when the user connects
    a handler to a signal.  In practice, this means treating hyphens and
    underscores as equivalent in signal names.

 GSignal.xs |   35 +++++++++++++++++++++++++++++------
 NEWS       |    2 ++
 2 files changed, 31 insertions(+), 6 deletions(-)
---
diff --git a/GSignal.xs b/GSignal.xs
index c89562d..a94b112 100644
--- a/GSignal.xs
+++ b/GSignal.xs
@@ -208,6 +208,9 @@ I<instance_type>.  C<gperl_signal_connect> will look for marshallers
 registered here, and apply them to the GPerlClosure it creates for the given
 callback being connected.
 
+A canonical form of I<detailed_signal> will be used so that I<marshaller> is
+applied for all possible spellings of the signal name.
+
 Use the helper macros in gperl_marshal.h to help write your marshaller
 function.  That header, which is installed with the Glib module but not
 #included through gperl.h, includes commentary and examples which you
@@ -228,6 +231,15 @@ bugfixes.
 static GHashTable * marshallers_by_type = NULL;
 G_LOCK_DEFINE_STATIC (marshallers_by_type);
 
+/* gobject treats hyphens and underscores in signal names as equivalent.  We
+ * thus need to do this as well to ensure that a custom marshaller is used for
+ * all spellings of a signal name. */
+static char *
+canonicalize_signal_name (char * signal_name)
+{
+	return g_strdelimit (signal_name, "_", '-');
+}
+
 void
 gperl_signal_set_marshaller_for (GType instance_type,
                                  char * detailed_signal,
@@ -240,6 +252,7 @@ gperl_signal_set_marshaller_for (GType instance_type,
 		/* nothing to do */
 	} else {
 		GHashTable *marshallers_by_signal;
+		char *canonical_detailed_signal;
 		if (!marshallers_by_type)
 			marshallers_by_type =
 				g_hash_table_new_full (g_direct_hash,
@@ -260,14 +273,18 @@ gperl_signal_set_marshaller_for (GType instance_type,
 			                     (gpointer) instance_type,
 			                     marshallers_by_signal);
 		}
-		if (marshaller)
+		canonical_detailed_signal = canonicalize_signal_name (
+			g_strdup (detailed_signal));
+		if (marshaller) {
 			g_hash_table_insert
 					(marshallers_by_signal,
-					 g_strdup (detailed_signal),
+					 canonical_detailed_signal,
 					 marshaller);
-		else
+		} else {
 			g_hash_table_remove (marshallers_by_signal,
-			                     detailed_signal);
+			                     canonical_detailed_signal);
+			g_free (canonical_detailed_signal);
+		}
 	}
 	G_UNLOCK (marshallers_by_type);
 }
@@ -281,8 +298,14 @@ lookup_specific_marshaller (GType specific_type,
 		g_hash_table_lookup (marshallers_by_type,
 		                     (gpointer) specific_type);
 	if (marshallers_by_signal) {
-		return g_hash_table_lookup (marshallers_by_signal,
-		                            detailed_signal);
+		char *canonical_detailed_signal;
+		GClosureMarshal marshaller;
+		canonical_detailed_signal = canonicalize_signal_name (
+			g_strdup (detailed_signal));
+		marshaller = g_hash_table_lookup (marshallers_by_signal,
+		                                  canonical_detailed_signal);
+		g_free (canonical_detailed_signal);
+		return marshaller;
 	}
 	return NULL;
 }
diff --git a/NEWS b/NEWS
index f3fd4e9..77998a6 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,8 @@ Overview of changes in Glib <next>
 ==================================
 
 * Correctly handle the boxed type for GError.
+* Ensure that custom signal marshallers are always used irregardless of the
+  spelling used for the signal name.
 * Make the stack handling of some marshallers more robust, in
   preparation for custom Glib::Boxed converters that call back into Perl
   code.



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