1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * override.c: Closure override test program
3 * Copyright (C) 2001, James Henstridge
4 * Copyright (C) 2003, Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #define G_LOG_DOMAIN "TestOverride"
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
32 #include <glib-object.h>
34 #include "testcommon.h"
36 static guint foo_signal_id
= 0;
37 static guint bar_signal_id
= 0;
38 static guint baz_signal_id
= 0;
40 static GType
test_i_get_type (void);
41 static GType
test_a_get_type (void);
42 static GType
test_b_get_type (void);
43 static GType
test_c_get_type (void);
45 static void record (const gchar
*str
);
47 #define TEST_TYPE_I (test_i_get_type ())
49 typedef struct _TestI TestI
;
50 typedef struct _TestIClass TestIClass
;
54 GTypeInterface base_iface
;
58 test_i_foo (TestI
*self
)
60 record ("TestI::foo");
64 test_i_default_init (gpointer g_class
)
66 foo_signal_id
= g_signal_newv ("foo",
69 g_cclosure_new(G_CALLBACK(test_i_foo
),
72 g_cclosure_marshal_VOID__VOID
,
73 G_TYPE_NONE
, 0, NULL
);
76 static DEFINE_IFACE (TestI
, test_i
, NULL
, test_i_default_init
)
78 #define TEST_TYPE_A (test_a_get_type())
80 typedef struct _TestA TestA
;
81 typedef struct _TestAClass TestAClass
;
87 GObjectClass parent_class
;
89 void (* bar
) (TestA
*self
);
93 test_a_foo (TestI
*self
)
95 GValue args
[1] = { G_VALUE_INIT
};
97 record ("TestA::foo");
99 g_value_init (&args
[0], TEST_TYPE_A
);
100 g_value_set_object (&args
[0], self
);
102 g_assert (g_signal_get_invocation_hint (self
)->signal_id
== foo_signal_id
);
103 g_signal_chain_from_overridden (args
, NULL
);
105 g_value_unset (&args
[0]);
109 test_a_bar (TestA
*self
)
111 record ("TestA::bar");
115 test_a_baz (TestA
*self
,
119 record ("TestA::baz");
121 g_assert (object
== G_OBJECT (self
));
122 g_assert (GPOINTER_TO_INT (pointer
) == 23);
124 return g_strdup ("TestA::baz");
128 test_a_class_init (TestAClass
*class)
130 class->bar
= test_a_bar
;
132 bar_signal_id
= g_signal_new ("bar",
135 G_STRUCT_OFFSET (TestAClass
, bar
),
137 g_cclosure_marshal_VOID__VOID
,
138 G_TYPE_NONE
, 0, NULL
);
140 baz_signal_id
= g_signal_new_class_handler ("baz",
143 G_CALLBACK (test_a_baz
),
145 g_cclosure_marshal_STRING__OBJECT_POINTER
,
152 test_a_interface_init (TestIClass
*iface
)
154 g_signal_override_class_closure (foo_signal_id
,
156 g_cclosure_new (G_CALLBACK (test_a_foo
),
160 static DEFINE_TYPE_FULL (TestA
, test_a
,
161 test_a_class_init
, NULL
, NULL
,
163 INTERFACE (test_a_interface_init
, TEST_TYPE_I
))
165 #define TEST_TYPE_B (test_b_get_type())
167 typedef struct _TestB TestB
;
168 typedef struct _TestBClass TestBClass
;
174 TestAClass parent_class
;
178 test_b_foo (TestI
*self
)
180 GValue args
[1] = { G_VALUE_INIT
};
182 record ("TestB::foo");
184 g_value_init (&args
[0], TEST_TYPE_A
);
185 g_value_set_object (&args
[0], self
);
187 g_assert (g_signal_get_invocation_hint (self
)->signal_id
== foo_signal_id
);
188 g_signal_chain_from_overridden (args
, NULL
);
190 g_value_unset (&args
[0]);
194 test_b_bar (TestA
*self
)
196 GValue args
[1] = { G_VALUE_INIT
};
198 record ("TestB::bar");
200 g_value_init (&args
[0], TEST_TYPE_A
);
201 g_value_set_object (&args
[0], self
);
203 g_assert (g_signal_get_invocation_hint (self
)->signal_id
== bar_signal_id
);
204 g_signal_chain_from_overridden (args
, NULL
);
206 g_value_unset (&args
[0]);
210 test_b_baz (TestA
*self
,
214 gchar
*retval
= NULL
;
216 record ("TestB::baz");
218 g_assert (object
== G_OBJECT (self
));
219 g_assert (GPOINTER_TO_INT (pointer
) == 23);
221 g_signal_chain_from_overridden_handler (self
, object
, pointer
, &retval
);
225 gchar
*tmp
= g_strconcat (retval
, ",TestB::baz", NULL
);
234 test_b_class_init (TestBClass
*class)
236 g_signal_override_class_closure (foo_signal_id
,
238 g_cclosure_new (G_CALLBACK (test_b_foo
),
240 g_signal_override_class_closure (bar_signal_id
,
242 g_cclosure_new (G_CALLBACK (test_b_bar
),
244 g_signal_override_class_handler ("baz",
246 G_CALLBACK (test_b_baz
));
249 static DEFINE_TYPE (TestB
, test_b
,
250 test_b_class_init
, NULL
, NULL
,
253 #define TEST_TYPE_C (test_c_get_type())
255 typedef struct _TestC TestC
;
256 typedef struct _TestCClass TestCClass
;
262 TestBClass parent_class
;
266 test_c_foo (TestI
*self
)
268 GValue args
[1] = { G_VALUE_INIT
};
270 record ("TestC::foo");
272 g_value_init (&args
[0], TEST_TYPE_A
);
273 g_value_set_object (&args
[0], self
);
275 g_assert (g_signal_get_invocation_hint (self
)->signal_id
== foo_signal_id
);
276 g_signal_chain_from_overridden (args
, NULL
);
278 g_value_unset (&args
[0]);
282 test_c_bar (TestA
*self
)
284 GValue args
[1] = { G_VALUE_INIT
};
286 record ("TestC::bar");
288 g_value_init (&args
[0], TEST_TYPE_A
);
289 g_value_set_object (&args
[0], self
);
291 g_assert (g_signal_get_invocation_hint (self
)->signal_id
== bar_signal_id
);
292 g_signal_chain_from_overridden (args
, NULL
);
294 g_value_unset (&args
[0]);
298 test_c_baz (TestA
*self
,
302 gchar
*retval
= NULL
;
304 record ("TestC::baz");
306 g_assert (object
== G_OBJECT (self
));
307 g_assert (GPOINTER_TO_INT (pointer
) == 23);
309 g_signal_chain_from_overridden_handler (self
, object
, pointer
, &retval
);
313 gchar
*tmp
= g_strconcat (retval
, ",TestC::baz", NULL
);
322 test_c_class_init (TestBClass
*class)
324 g_signal_override_class_closure (foo_signal_id
,
326 g_cclosure_new (G_CALLBACK (test_c_foo
),
328 g_signal_override_class_closure (bar_signal_id
,
330 g_cclosure_new (G_CALLBACK (test_c_bar
),
332 g_signal_override_class_handler ("baz",
334 G_CALLBACK (test_c_baz
));
338 static DEFINE_TYPE (TestC
, test_c
,
339 test_c_class_init
, NULL
, NULL
,
342 static GString
*test_string
= NULL
;
343 gboolean failed
= FALSE
;
346 record (const gchar
*str
)
348 if (test_string
->len
)
349 g_string_append_c (test_string
, ',');
350 g_string_append (test_string
, str
);
356 const gchar
*expected
,
357 const gchar
*expected_retval
)
359 GObject
*self
= g_object_new (type
, NULL
);
361 test_string
= g_string_new (NULL
);
363 if (strcmp (signal
, "baz"))
365 g_signal_emit_by_name (self
, signal
);
371 g_signal_emit_by_name (self
, signal
, self
, GINT_TO_POINTER (23), &ret
);
373 if (strcmp (ret
, expected_retval
) != 0)
380 if (strcmp (test_string
->str
, expected
) != 0)
383 g_printerr ("*** emitting %s on a %s instance\n"
386 signal
, g_type_name (type
),
390 if (strcmp (test_string
->str
, expected
) != 0)
394 g_string_free (test_string
, TRUE
);
395 g_object_unref (self
);
399 main (int argc
, char **argv
)
401 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK
) |
402 G_LOG_LEVEL_WARNING
|
403 G_LOG_LEVEL_CRITICAL
);
405 test (TEST_TYPE_A
, "foo", "TestA::foo,TestI::foo", NULL
);
406 test (TEST_TYPE_A
, "bar", "TestA::bar", NULL
);
407 test (TEST_TYPE_A
, "baz", "TestA::baz", "TestA::baz");
409 test (TEST_TYPE_B
, "foo", "TestB::foo,TestA::foo,TestI::foo", NULL
);
410 test (TEST_TYPE_B
, "bar", "TestB::bar,TestA::bar", NULL
);
411 test (TEST_TYPE_B
, "baz", "TestB::baz,TestA::baz", "TestA::baz,TestB::baz");
413 test (TEST_TYPE_C
, "foo", "TestC::foo,TestB::foo,TestA::foo,TestI::foo", NULL
);
414 test (TEST_TYPE_C
, "bar", "TestC::bar,TestB::bar,TestA::bar", NULL
);
415 test (TEST_TYPE_C
, "baz", "TestC::baz,TestB::baz,TestA::baz", "TestA::baz,TestB::baz,TestC::baz");
417 return failed
? 1 : 0;