g_boolean_handled_accumulator patch



Here's a patch to add g_boolean_handled_accumulator().

(http://bugzilla.gnome.org/show_bug.cgi?id=80487)

It adds a testaccumulator.c to testgobject, which includes
a test for this, and the accumulator testing from 
testgobject.c (I didn't remove it from their, however.)

I added a separate testmarshalers.[ch] to support the 
test case, since I didn't want to add "random" marshalers
to gmarshal.h.

Look good?
				Owen

Index: gobject/.cvsignore
===================================================================
RCS file: /cvs/gnome/glib/gobject/.cvsignore,v
retrieving revision 1.14
diff -u -p -u -r1.14 .cvsignore
--- gobject/.cvsignore	28 Aug 2003 02:20:16 -0000	1.14
+++ gobject/.cvsignore	11 Sep 2003 19:23:40 -0000
@@ -25,6 +25,8 @@ gmarshal.h
 gmarshal.strings
 oldest-source-stamp
 stamp-gmarshal.h
+testaccumulator.c
+testaccumulator.h
 testgobject
 testoverride
 testifaceinit
Index: gobject/ChangeLog
===================================================================
RCS file: /cvs/gnome/glib/gobject/ChangeLog,v
retrieving revision 1.260
diff -u -p -u -r1.260 ChangeLog
--- gobject/ChangeLog	2 Sep 2003 17:57:22 -0000	1.260
+++ gobject/ChangeLog	11 Sep 2003 19:23:41 -0000
@@ -1,3 +1,8 @@
+Wed Sep 10 12:52:35 2003  Owen Taylor  <otaylor redhat com>
+
+	* gsignal.[ch]: Add g_boolean_handled_accumulator(), to
+	do TRUE-stops-emit signals.
+
 Tue Sep  2 19:37:21 2003  Tim Janik  <timj gtk org>
 
 	* gtype.[hc]: added support for a "default vtable" per interface,
Index: gobject/Makefile.am
===================================================================
RCS file: /cvs/gnome/glib/gobject/Makefile.am,v
retrieving revision 1.55
diff -u -p -u -r1.55 Makefile.am
--- gobject/Makefile.am	27 Aug 2003 05:28:39 -0000	1.55
+++ gobject/Makefile.am	11 Sep 2003 19:23:41 -0000
@@ -103,7 +103,7 @@ gobject_c_sources = \
 	gvaluetypes.c
 
 # these sources (also mentioned above) are generated.
-BUILT_SOURCES = gmarshal.h gmarshal.c
+BUILT_SOURCES = gmarshal.h gmarshal.c testmarshal.h testmarshal.c
 
 # non-header sources (headers should be specified in the above variables)
 # that don't serve as direct make target sources, i.e. they don't have
@@ -164,6 +164,19 @@ gmarshal.strings: @REBUILD@ $(srcdir)/gm
 	&& cp xgen-gms gmarshal.strings \
 	&& rm -f xgen-gms xgen-gms~
 
+testmarshal.h: stamp-testmarshal.h
+	@true
+stamp-testmarshal.h: @REBUILD@ testmarshal.list glib-genmarshal$(EXEEXT)
+	$(MAKE) glib-genmarshal$(EXEEXT) \
+	&& $(glib_genmarshal) --prefix=test_marshal $(srcdir)/testmarshal.list --header >> xgen-gmh \
+	&& (cmp -s xgen-gmh testmarshal.h 2>/dev/null || cp xgen-gmh testmarshal.h) \
+	&& rm -f xgen-gmh xgen-gmh~ \
+	&& echo timestamp > $@
+testmarshal.c: @REBUILD@ stamp-testmarshal.h
+	$(glib_genmarshal) --prefix=test_marshal $(srcdir)/testmarshal.list --body >> xgen-gmc \
+	&& cp xgen-gmc testmarshal.c \
+	&& rm -f xgen-gmc xgen-gmc~
+
 glib-genmarshal.o: gmarshal.strings
 gsignal.lo: gmarshal.c
 
@@ -177,10 +190,11 @@ libgobject_2_0_la_SOURCES = $(gobject_ta
 #
 bin_PROGRAMS = gobject-query glib-genmarshal
 bin_SCRIPTS = glib-mkenums
-noinst_PROGRAMS = testgobject testifaceinit testoverride
+noinst_PROGRAMS = testaccumulator testgobject testifaceinit testoverride
 # source files
 gobject_query_SOURCES = gobject-query.c
 glib_genmarshal_SOURCES = glib-genmarshal.c
+testaccumulator_SOURCES = testaccumulator.c testmarshal.c testmarshal.h
 testgobject_SOURCES = testgobject.c
 testifaceinit_SOURCES = testifaceinit.c
 testoverride_SOURCES = testoverride.c
@@ -188,6 +202,7 @@ testoverride_SOURCES = testoverride.c
 progs_LDADD = ./libgobject-2.0.la $(libglib)
 glib_genmarshal_LDADD = $(libglib)
 gobject_query_LDADD = $(progs_LDADD)
+testaccumulator_LDADD = $(progs_LDADD)
 testgobject_LDADD = $(progs_LDADD)
 testifaceinit_LDADD = $(progs_LDADD)
 testoverride_LDADD = $(progs_LDADD)
@@ -210,6 +225,8 @@ BUILT_EXTRA_DIST = \
 	gobject.rc		\
 	gmarshal.h		\
 	gmarshal.c		\
+	testmarshal.h		\
+	testmarshal.c		\
 	stamp-gmarshal.h
 
 if OS_WIN32
Index: gobject/gmarshal.list
===================================================================
RCS file: /cvs/gnome/glib/gobject/gmarshal.list,v
retrieving revision 1.4
diff -u -p -u -r1.4 gmarshal.list
--- gobject/gmarshal.list	3 Sep 2001 22:13:16 -0000	1.4
+++ gobject/gmarshal.list	11 Sep 2003 19:23:41 -0000
@@ -40,8 +40,8 @@ VOID:PARAM
 VOID:BOXED
 VOID:POINTER
 VOID:OBJECT
-STRING:OBJECT,POINTER
 
 # GRuntime specific marshallers
 VOID:UINT,POINTER
 BOOL:FLAGS
+STRING:OBJECT,POINTER
Index: gobject/gsignal.c
===================================================================
RCS file: /cvs/gnome/glib/gobject/gsignal.c,v
retrieving revision 1.52
diff -u -p -u -r1.52 gsignal.c
--- gobject/gsignal.c	19 Aug 2003 02:15:40 -0000	1.52
+++ gobject/gsignal.c	11 Sep 2003 19:23:41 -0000
@@ -2586,6 +2586,22 @@ type_debug_name (GType type)
     return "<invalid>";
 }
 
+gboolean
+g_boolean_handled_accumulator (GSignalInvocationHint *ihint,
+			       GValue                *return_accu,
+			       const GValue          *handler_return,
+			       gpointer               dummy)
+{
+  gboolean continue_emission;
+  gboolean signal_handled;
+  
+  signal_handled = g_value_get_boolean (handler_return);
+  g_value_set_boolean (return_accu, signal_handled);
+  continue_emission = !signal_handled;
+  
+  return continue_emission;
+}
+
 /* --- compile standard marshallers --- */
 #include	"gobject.h"
 #include	"genums.h"
Index: gobject/gsignal.h
===================================================================
RCS file: /cvs/gnome/glib/gobject/gsignal.h,v
retrieving revision 1.35
diff -u -p -u -r1.35 gsignal.h
--- gobject/gsignal.h	3 Dec 2002 23:54:54 -0000	1.35
+++ gobject/gsignal.h	11 Sep 2003 19:23:41 -0000
@@ -256,6 +256,11 @@ void	g_signal_chain_from_overridden	    
 				          0, 0, NULL, (func), (data))
 
 
+gboolean g_boolean_handled_accumulator (GSignalInvocationHint *ihint,
+					GValue                *return_accu,
+					const GValue          *handler_return,
+					gpointer               dummy);
+
 /*< private >*/
 void	 g_signal_handlers_destroy	      (gpointer		  instance);
 void	 _g_signals_destroy		      (GType		  itype);
Index: gobject/testaccumulator.c
===================================================================
RCS file: gobject/testaccumulator.c
diff -N gobject/testaccumulator.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gobject/testaccumulator.c	11 Sep 2003 19:23:41 -0000
@@ -0,0 +1,257 @@
+/* GObject - GLib Type, Object, Parameter and Signal Library
+ * Copyright (C) 2001, 2003 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#undef	G_LOG_DOMAIN
+#define	G_LOG_DOMAIN "TestAccumulator"
+
+#undef G_DISABLE_ASSERT
+#undef G_DISABLE_CHECKS
+#undef G_DISABLE_CAST_CHECKS
+
+#include <string.h>
+
+#include	<glib-object.h>
+
+#include <testmarshal.h>
+
+/* What this test tests is the behavior of signal accumulators
+ * Two accumulators are tested:
+ *
+ * 1: A custom accumulator that appends the returned strings
+ * 2: The standard g_boolean_handled_accumulator that stops
+ *    emission on TRUE returns.
+ */
+
+#define DEFINE_TYPE(name, prefix,				\
+		    class_init, base_init, instance_init,	\
+		    parent_type)				\
+GType								\
+prefix ## _get_type (void)					\
+{								\
+  static GType object_type = 0;					\
+								\
+  if (!object_type)						\
+    {								\
+      static const GTypeInfo object_info =			\
+	{							\
+	  sizeof (name ## Class),				\
+	  (GBaseInitFunc) base_init,				\
+	  (GBaseFinalizeFunc) NULL,				\
+	  (GClassInitFunc) class_init,				\
+	  (GClassFinalizeFunc) NULL,				\
+	  NULL,           /* class_data */			\
+	  sizeof (name),					\
+	  0,             /* n_prelocs */			\
+	  (GInstanceInitFunc) instance_init			\
+	};							\
+								\
+      object_type = g_type_register_static (parent_type,	\
+					    # name,		\
+					    &object_info, 0);	\
+    }								\
+								\
+  return object_type;						\
+}
+
+/*
+ * TestObject, a parent class for TestObject
+ */
+#define TEST_TYPE_OBJECT          (test_object_get_type ())
+typedef struct _TestObject        TestObject;
+typedef struct _TestObjectClass   TestObjectClass;
+
+struct _TestObject
+{
+  GObject parent_instance;
+};
+struct _TestObjectClass
+{
+  GObjectClass parent_class;
+
+  gchar*   (*test_signal1) (TestObject *tobject,
+			    gint        param);
+  gboolean (*test_signal2) (TestObject *tobject,
+			    gint        param);
+};
+
+static GType test_object_get_type (void);
+
+static gboolean
+test_signal1_accumulator (GSignalInvocationHint *ihint,
+			  GValue                *return_accu,
+			  const GValue          *handler_return,
+			  gpointer               data)
+{
+  gchar *accu_string = g_value_get_string (return_accu);
+  gchar *new_string = g_value_get_string (handler_return);
+  gchar *result_string;
+
+  if (accu_string)
+    result_string = g_strconcat (accu_string, new_string, NULL);
+  else if (new_string)
+    result_string = g_strdup (new_string);
+  else
+    result_string = NULL;
+
+  g_value_set_string_take_ownership (return_accu, result_string);
+
+  return TRUE;
+}
+
+gchar*
+test_object_signal1_callback_before (TestObject *tobject,
+				     gint        param,
+				     gpointer    data)
+{
+  return g_strdup ("<before>");
+}
+
+gchar*
+test_object_real_signal1 (TestObject *tobject,
+			  gint        param)
+{
+  return g_strdup ("<default>");
+}
+
+gchar*
+test_object_signal1_callback_after (TestObject *tobject,
+				    gint        param,
+				    gpointer    data)
+{
+  return g_strdup ("<after>");
+}
+
+gboolean
+test_object_signal2_callback_before (TestObject *tobject,
+				     gint        param)
+{
+  switch (param)
+    {
+    case 1: return TRUE;
+    case 2: return FALSE;
+    case 3: return FALSE;
+    case 4: return FALSE;
+    }
+
+  g_assert_not_reached ();
+  return FALSE;
+}
+
+gboolean
+test_object_real_signal2 (TestObject *tobject,
+			  gint        param)
+{
+  switch (param)
+    {
+    case 1: g_assert_not_reached (); return FALSE;
+    case 2: return TRUE;
+    case 3: return FALSE;
+    case 4: return FALSE;
+    }
+  
+  g_assert_not_reached ();
+  return FALSE;
+}
+
+gboolean
+test_object_signal2_callback_after (TestObject *tobject,
+				     gint        param)
+{
+  switch (param)
+    {
+    case 1: g_assert_not_reached (); return FALSE;
+    case 2: g_assert_not_reached (); return FALSE;
+    case 3: return TRUE;
+    case 4: return FALSE;
+    }
+      
+  g_assert_not_reached ();
+  return FALSE;
+}
+
+static void
+test_object_class_init (TestObjectClass *class)
+{
+  class->test_signal1 = test_object_real_signal1;
+  class->test_signal2 = test_object_real_signal2;
+  
+  g_signal_new ("test-signal1",
+		G_OBJECT_CLASS_TYPE (class),
+		G_SIGNAL_RUN_LAST,
+		G_STRUCT_OFFSET (TestObjectClass, test_signal1),
+		test_signal1_accumulator, NULL,
+		test_marshal_STRING__INT,
+		G_TYPE_STRING, 1, G_TYPE_INT);
+  g_signal_new ("test-signal2",
+		G_OBJECT_CLASS_TYPE (class),
+		G_SIGNAL_RUN_LAST,
+		G_STRUCT_OFFSET (TestObjectClass, test_signal2),
+		g_boolean_handled_accumulator, NULL,
+		test_marshal_BOOLEAN__INT,
+		G_TYPE_BOOLEAN, 1, G_TYPE_INT);
+}
+
+static DEFINE_TYPE(TestObject, test_object,
+		   test_object_class_init, NULL, NULL,
+		   G_TYPE_OBJECT);
+
+int
+main (int   argc,
+      char *argv[])
+{
+  TestObject *object;
+  gchar *string_result;
+  gboolean bool_result;
+	
+  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
+			  G_LOG_LEVEL_WARNING |
+			  G_LOG_LEVEL_CRITICAL);
+  g_type_init ();
+
+  object = g_object_new (TEST_TYPE_OBJECT, NULL);
+
+  g_signal_connect (object, "test-signal1",
+		    G_CALLBACK (test_object_signal1_callback_before), NULL);
+  g_signal_connect_after (object, "test-signal1",
+			  G_CALLBACK (test_object_signal1_callback_after), NULL);
+  
+  g_signal_emit_by_name (object, "test-signal1", 0, &string_result);
+  g_assert (strcmp (string_result, "<before><default><after>") == 0);
+  g_free (string_result);
+
+  g_signal_connect (object, "test-signal2",
+		    G_CALLBACK (test_object_signal2_callback_before), NULL);
+  g_signal_connect_after (object, "test-signal2",
+			  G_CALLBACK (test_object_signal2_callback_after), NULL);
+  
+  bool_result = FALSE;
+  g_signal_emit_by_name (object, "test-signal2", 1, &bool_result);
+  g_assert (bool_result == TRUE);
+  bool_result = FALSE;
+  g_signal_emit_by_name (object, "test-signal2", 2, &bool_result);
+  g_assert (bool_result == TRUE);
+  bool_result = FALSE;
+  g_signal_emit_by_name (object, "test-signal2", 3, &bool_result);
+  g_assert (bool_result == TRUE);
+  bool_result = FALSE;
+  g_signal_emit_by_name (object, "test-signal2", 4, &bool_result);
+  g_assert (bool_result == FALSE);
+
+  return 0;
+}
Index: gobject/testmarshal.list
===================================================================
RCS file: gobject/testmarshal.list
diff -N gobject/testmarshal.list
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gobject/testmarshal.list	11 Sep 2003 19:23:41 -0000
@@ -0,0 +1,4 @@
+# Marshallers used in tests
+BOOLEAN:INT
+STRING:INT
+
Index: docs/reference/gobject/Makefile.am
===================================================================
RCS file: /cvs/gnome/glib/docs/reference/gobject/Makefile.am,v
retrieving revision 1.28
diff -u -p -u -r1.28 Makefile.am
--- docs/reference/gobject/Makefile.am	17 Jun 2003 23:08:37 -0000	1.28
+++ docs/reference/gobject/Makefile.am	11 Sep 2003 19:23:41 -0000
@@ -15,6 +15,10 @@ DOC_SOURCE_DIR=../../../gobject
 HFILE_GLOB=$(top_srcdir)/gobject/*.h
 CFILE_GLOB=$(top_srcdir)/gobject/*.c
 
+# Header files to ignore when scanning
+IGNORE_HFILES=			\
+	testmarshal.h
+
 # CFLAGS and LDFLAGS for compiling scan program. Only needed
 # if $(DOC_MODULE).types is non-empty.
 INCLUDES =
Index: docs/reference/gobject/gobject-sections.txt
===================================================================
RCS file: /cvs/gnome/glib/docs/reference/gobject/gobject-sections.txt,v
retrieving revision 1.25
diff -u -p -u -r1.25 gobject-sections.txt
--- docs/reference/gobject/gobject-sections.txt	24 Mar 2003 23:28:34 -0000	1.25
+++ docs/reference/gobject/gobject-sections.txt	11 Sep 2003 19:23:41 -0000
@@ -657,6 +657,7 @@ g_signal_remove_emission_hook
 g_signal_parse_name
 g_signal_get_invocation_hint
 g_signal_type_cclosure_new
+g_boolean_handled_accumulator
 <SUBSECTION Private>
 g_signal_handlers_destroy
 </SECTION>
Index: docs/reference/gobject/tmpl/signals.sgml
===================================================================
RCS file: /cvs/gnome/glib/docs/reference/gobject/tmpl/signals.sgml,v
retrieving revision 1.28
diff -u -p -u -r1.28 signals.sgml
--- docs/reference/gobject/tmpl/signals.sgml	18 Apr 2003 00:18:06 -0000	1.28
+++ docs/reference/gobject/tmpl/signals.sgml	11 Sep 2003 19:23:41 -0000
@@ -870,3 +870,21 @@ Returns the invocation hint of the inner
 @Returns: 
 
 
+<!-- ##### FUNCTION g_boolean_handled_accumulator ##### -->
+<para>
+A predefined #GSignalAccumulator for signals that return a
+boolean values. The behavior that this accumulator gives is
+that a return of %TRUE stops emission of subsequent callbacks,
+while a return of %FALSE allows emission to coninue. The idea
+here is that a %TRUE return indicates that the callback
+<emphasis>handled</emphasis> the signal, and no further
+handling is needed.
+</para>
+
+ ihint: standard #GSignalAccumulator parameter
+ return_accu: standard #GSignalAccumulator parameter
+ handler_return: standard #GSignalAccumulator parameter
+ dummy: standard #GSignalAccumulator parameter
+ Returns: standard #GSignalAccumulator result
+
+


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