|
1 /* GObject - GLib Type, Object, Parameter and Signal Library |
|
2 * Copyright (C) 2001 Red Hat, Inc. |
|
3 * Portions copyright (c) 2009 Nokia Corporation. All rights reserved. |
|
4 * This library is free software; you can redistribute it and/or |
|
5 * modify it under the terms of the GNU Lesser General Public |
|
6 * License as published by the Free Software Foundation; either |
|
7 * version 2 of the License, or (at your option) any later version. |
|
8 * |
|
9 * This library is distributed in the hope that it will be useful, |
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
12 * Lesser General Public License for more details. |
|
13 * |
|
14 * You should have received a copy of the GNU Lesser General |
|
15 * Public License along with this library; if not, write to the |
|
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
|
17 * Boston, MA 02111-1307, USA. |
|
18 */ |
|
19 |
|
20 #include <string.h> |
|
21 |
|
22 #undef G_LOG_DOMAIN |
|
23 #define G_LOG_DOMAIN "TestObject" |
|
24 #include <glib-object.h> |
|
25 #ifdef __SYMBIAN32__ |
|
26 #include "mrt2_glib2_test.h" |
|
27 #endif /*__SYMBIAN32__*/ |
|
28 |
|
29 /* --- TestIface --- */ |
|
30 #define TEST_TYPE_IFACE (test_iface_get_type ()) |
|
31 #define TEST_IFACE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_IFACE, TestIface)) |
|
32 #define TEST_IS_IFACE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_IFACE)) |
|
33 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass)) |
|
34 typedef struct _TestIface TestIface; |
|
35 typedef struct _TestIfaceClass TestIfaceClass; |
|
36 struct _TestIfaceClass |
|
37 { |
|
38 GTypeInterface base_iface; |
|
39 void (*print_string) (TestIface *tiobj, |
|
40 const gchar *string); |
|
41 }; |
|
42 static void iface_base_init (TestIfaceClass *iface); |
|
43 static void iface_base_finalize (TestIfaceClass *iface); |
|
44 static void print_foo (TestIface *tiobj, |
|
45 const gchar *string); |
|
46 static GType |
|
47 test_iface_get_type (void) |
|
48 { |
|
49 static GType test_iface_type = 0; |
|
50 |
|
51 if (!test_iface_type) |
|
52 { |
|
53 static const GTypeInfo test_iface_info = |
|
54 { |
|
55 sizeof (TestIfaceClass), |
|
56 (GBaseInitFunc) iface_base_init, /* base_init */ |
|
57 (GBaseFinalizeFunc) iface_base_finalize, /* base_finalize */ |
|
58 }; |
|
59 |
|
60 test_iface_type = g_type_register_static (G_TYPE_INTERFACE, "TestIface", &test_iface_info, 0); |
|
61 g_type_interface_add_prerequisite (test_iface_type, G_TYPE_OBJECT); |
|
62 } |
|
63 |
|
64 return test_iface_type; |
|
65 } |
|
66 static guint iface_base_init_count = 0; |
|
67 static void |
|
68 iface_base_init (TestIfaceClass *iface) |
|
69 { |
|
70 iface_base_init_count++; |
|
71 if (iface_base_init_count == 1) |
|
72 { |
|
73 /* add signals here */ |
|
74 } |
|
75 } |
|
76 static void |
|
77 iface_base_finalize (TestIfaceClass *iface) |
|
78 { |
|
79 iface_base_init_count--; |
|
80 if (iface_base_init_count == 0) |
|
81 { |
|
82 /* destroy signals here */ |
|
83 } |
|
84 } |
|
85 static void |
|
86 print_foo (TestIface *tiobj, |
|
87 const gchar *string) |
|
88 { |
|
89 if (!string) |
|
90 string = "<NULL>"; |
|
91 g_print ("Iface-FOO: \"%s\" from %p\n", string, tiobj); |
|
92 } |
|
93 static void |
|
94 test_object_test_iface_init (gpointer giface, |
|
95 gpointer iface_data) |
|
96 { |
|
97 TestIfaceClass *iface = giface; |
|
98 |
|
99 g_assert (iface_data == GUINT_TO_POINTER (42)); |
|
100 |
|
101 g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE); |
|
102 |
|
103 /* assert iface_base_init() was already called */ |
|
104 g_assert (iface_base_init_count > 0); |
|
105 |
|
106 /* initialize stuff */ |
|
107 iface->print_string = print_foo; |
|
108 } |
|
109 static void |
|
110 iface_print_string (TestIface *tiobj, |
|
111 const gchar *string) |
|
112 { |
|
113 TestIfaceClass *iface; |
|
114 |
|
115 g_return_if_fail (TEST_IS_IFACE (tiobj)); |
|
116 g_return_if_fail (G_IS_OBJECT (tiobj)); /* ensured through prerequisite */ |
|
117 |
|
118 iface = TEST_IFACE_GET_CLASS (tiobj); |
|
119 g_object_ref (tiobj); |
|
120 iface->print_string (tiobj, string); |
|
121 g_object_unref (tiobj); |
|
122 } |
|
123 |
|
124 |
|
125 /* --- TestObject --- */ |
|
126 #define TEST_TYPE_OBJECT (test_object_get_type ()) |
|
127 #define TEST_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), TEST_TYPE_OBJECT, TestObject)) |
|
128 #define TEST_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass)) |
|
129 #define TEST_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), TEST_TYPE_OBJECT)) |
|
130 #define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT)) |
|
131 #define TEST_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass)) |
|
132 #define TEST_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TEST_TYPE_OBJECT, TestObjectPrivate)) |
|
133 typedef struct _TestObject TestObject; |
|
134 typedef struct _TestObjectClass TestObjectClass; |
|
135 typedef struct _TestObjectPrivate TestObjectPrivate; |
|
136 struct _TestObject |
|
137 { |
|
138 GObject parent_instance; |
|
139 }; |
|
140 struct _TestObjectClass |
|
141 { |
|
142 GObjectClass parent_class; |
|
143 |
|
144 gchar* (*test_signal) (TestObject *tobject, |
|
145 TestIface *iface_object, |
|
146 gpointer tdata); |
|
147 }; |
|
148 struct _TestObjectPrivate |
|
149 { |
|
150 int dummy1; |
|
151 gdouble dummy2; |
|
152 }; |
|
153 static void test_object_class_init (TestObjectClass *class); |
|
154 static void test_object_init (TestObject *tobject); |
|
155 static gboolean test_signal_accumulator (GSignalInvocationHint *ihint, |
|
156 GValue *return_accu, |
|
157 const GValue *handler_return, |
|
158 gpointer data); |
|
159 static gchar* test_object_test_signal (TestObject *tobject, |
|
160 TestIface *iface_object, |
|
161 gpointer tdata); |
|
162 static GType |
|
163 test_object_get_type (void) |
|
164 { |
|
165 static GType test_object_type = 0; |
|
166 |
|
167 if (!test_object_type) |
|
168 { |
|
169 static const GTypeInfo test_object_info = |
|
170 { |
|
171 sizeof (TestObjectClass), |
|
172 NULL, /* base_init */ |
|
173 NULL, /* base_finalize */ |
|
174 (GClassInitFunc) test_object_class_init, |
|
175 NULL, /* class_finalize */ |
|
176 NULL, /* class_data */ |
|
177 sizeof (TestObject), |
|
178 5, /* n_preallocs */ |
|
179 (GInstanceInitFunc) test_object_init, |
|
180 }; |
|
181 GInterfaceInfo iface_info = { test_object_test_iface_init, NULL, GUINT_TO_POINTER (42) }; |
|
182 |
|
183 test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0); |
|
184 g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info); |
|
185 } |
|
186 |
|
187 return test_object_type; |
|
188 } |
|
189 static void |
|
190 test_object_class_init (TestObjectClass *class) |
|
191 { |
|
192 /* GObjectClass *gobject_class = G_OBJECT_CLASS (class); */ |
|
193 |
|
194 class->test_signal = test_object_test_signal; |
|
195 |
|
196 g_signal_new ("test-signal", |
|
197 G_OBJECT_CLASS_TYPE (class), |
|
198 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP, |
|
199 G_STRUCT_OFFSET (TestObjectClass, test_signal), |
|
200 test_signal_accumulator, NULL, |
|
201 g_cclosure_marshal_STRING__OBJECT_POINTER, |
|
202 G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER); |
|
203 |
|
204 g_type_class_add_private (class, sizeof (TestObjectPrivate)); |
|
205 } |
|
206 static void |
|
207 test_object_init (TestObject *tobject) |
|
208 { |
|
209 TestObjectPrivate *priv; |
|
210 |
|
211 priv = TEST_OBJECT_GET_PRIVATE (tobject); |
|
212 |
|
213 g_assert (priv); |
|
214 g_assert ((gchar *)priv >= (gchar *)tobject + sizeof (TestObject)); |
|
215 |
|
216 priv->dummy1 = 54321; |
|
217 } |
|
218 /* Check to see if private data initialization in the |
|
219 * instance init function works. |
|
220 */ |
|
221 static void |
|
222 test_object_check_private_init (TestObject *tobject) |
|
223 { |
|
224 TestObjectPrivate *priv; |
|
225 |
|
226 priv = TEST_OBJECT_GET_PRIVATE (tobject); |
|
227 |
|
228 g_print ("private data during initialization: %u == %u\n", priv->dummy1, 54321); |
|
229 g_assert (priv->dummy1 == 54321); |
|
230 } |
|
231 static gboolean |
|
232 test_signal_accumulator (GSignalInvocationHint *ihint, |
|
233 GValue *return_accu, |
|
234 const GValue *handler_return, |
|
235 gpointer data) |
|
236 { |
|
237 gchar *accu_string = g_value_get_string (return_accu); |
|
238 gchar *new_string = g_value_get_string (handler_return); |
|
239 gchar *result_string; |
|
240 |
|
241 if (accu_string) |
|
242 result_string = g_strconcat (accu_string, new_string, NULL); |
|
243 else if (new_string) |
|
244 result_string = g_strdup (new_string); |
|
245 else |
|
246 result_string = NULL; |
|
247 |
|
248 g_value_take_string (return_accu, result_string); |
|
249 |
|
250 return TRUE; |
|
251 } |
|
252 static gchar* |
|
253 test_object_test_signal (TestObject *tobject, |
|
254 TestIface *iface_object, |
|
255 gpointer tdata) |
|
256 { |
|
257 g_message ("::test_signal default_handler called"); |
|
258 |
|
259 g_return_val_if_fail (TEST_IS_IFACE (iface_object), NULL); |
|
260 |
|
261 return g_strdup ("<default_handler>"); |
|
262 } |
|
263 |
|
264 |
|
265 /* --- TestIface for DerivedObject --- */ |
|
266 static void |
|
267 print_bar (TestIface *tiobj, |
|
268 const gchar *string) |
|
269 { |
|
270 TestIfaceClass *parent_iface; |
|
271 |
|
272 g_return_if_fail (TEST_IS_IFACE (tiobj)); |
|
273 |
|
274 if (!string) |
|
275 string = "<NULL>"; |
|
276 g_print ("Iface-BAR: \"%s\" from %p\n", string, tiobj); |
|
277 |
|
278 g_print ("chaining: "); |
|
279 parent_iface = g_type_interface_peek_parent (TEST_IFACE_GET_CLASS (tiobj)); |
|
280 parent_iface->print_string (tiobj, string); |
|
281 |
|
282 g_assert (g_type_interface_peek_parent (parent_iface) == NULL); |
|
283 } |
|
284 |
|
285 static void |
|
286 derived_object_test_iface_init (gpointer giface, |
|
287 gpointer iface_data) |
|
288 { |
|
289 TestIfaceClass *iface = giface; |
|
290 |
|
291 g_assert (iface_data == GUINT_TO_POINTER (87)); |
|
292 |
|
293 g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE); |
|
294 |
|
295 /* assert test_object_test_iface_init() was already called */ |
|
296 g_assert (iface->print_string == print_foo); |
|
297 |
|
298 /* override stuff */ |
|
299 iface->print_string = print_bar; |
|
300 } |
|
301 |
|
302 |
|
303 /* --- DerivedObject --- */ |
|
304 #define DERIVED_TYPE_OBJECT (derived_object_get_type ()) |
|
305 #define DERIVED_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), DERIVED_TYPE_OBJECT, DerivedObject)) |
|
306 #define DERIVED_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DERIVED_TYPE_OBJECT, DerivedObjectClass)) |
|
307 #define DERIVED_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), DERIVED_TYPE_OBJECT)) |
|
308 #define DERIVED_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DERIVED_TYPE_OBJECT)) |
|
309 #define DERIVED_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DERIVED_TYPE_OBJECT, DerivedObjectClass)) |
|
310 #define DERIVED_OBJECT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DERIVED_TYPE_OBJECT, DerivedObjectPrivate)) |
|
311 typedef struct _DerivedObject DerivedObject; |
|
312 typedef struct _TestObjectClass DerivedObjectClass; |
|
313 typedef struct _DerivedObjectPrivate DerivedObjectPrivate; |
|
314 struct _DerivedObject |
|
315 { |
|
316 TestObject parent_instance; |
|
317 int dummy1; |
|
318 int dummy2; |
|
319 }; |
|
320 struct _DerivedObjectPrivate |
|
321 { |
|
322 char dummy; |
|
323 }; |
|
324 static void derived_object_class_init (DerivedObjectClass *class); |
|
325 static void derived_object_init (DerivedObject *dobject); |
|
326 GType |
|
327 derived_object_get_type (void) |
|
328 { |
|
329 static GType derived_object_type = 0; |
|
330 |
|
331 if (!derived_object_type) |
|
332 { |
|
333 static const GTypeInfo derived_object_info = |
|
334 { |
|
335 sizeof (DerivedObjectClass), |
|
336 NULL, /* base_init */ |
|
337 NULL, /* base_finalize */ |
|
338 (GClassInitFunc) derived_object_class_init, |
|
339 NULL, /* class_finalize */ |
|
340 NULL, /* class_data */ |
|
341 sizeof (DerivedObject), |
|
342 5, /* n_preallocs */ |
|
343 (GInstanceInitFunc) derived_object_init, |
|
344 }; |
|
345 GInterfaceInfo iface_info = { derived_object_test_iface_init, NULL, GUINT_TO_POINTER (87) }; |
|
346 |
|
347 derived_object_type = g_type_register_static (TEST_TYPE_OBJECT, "DerivedObject", &derived_object_info, 0); |
|
348 g_type_add_interface_static (derived_object_type, TEST_TYPE_IFACE, &iface_info); |
|
349 } |
|
350 |
|
351 return derived_object_type; |
|
352 } |
|
353 static void |
|
354 derived_object_class_init (DerivedObjectClass *class) |
|
355 { |
|
356 g_type_class_add_private (class, sizeof (DerivedObjectPrivate)); |
|
357 } |
|
358 static void |
|
359 derived_object_init (DerivedObject *dobject) |
|
360 { |
|
361 TestObjectPrivate *test_priv; |
|
362 DerivedObjectPrivate *derived_priv; |
|
363 |
|
364 derived_priv = DERIVED_OBJECT_GET_PRIVATE (dobject); |
|
365 |
|
366 g_assert (derived_priv); |
|
367 g_assert ((gchar *)derived_priv >= (gchar *)TEST_OBJECT_GET_PRIVATE (dobject) + sizeof (TestObjectPrivate)); |
|
368 |
|
369 test_priv = TEST_OBJECT_GET_PRIVATE (dobject); |
|
370 |
|
371 g_assert (test_priv); |
|
372 g_assert ((gchar *)test_priv >= (gchar *)dobject + sizeof (TestObject)); |
|
373 |
|
374 } |
|
375 |
|
376 /* --- main --- */ |
|
377 int |
|
378 main (int argc, |
|
379 char *argv[]) |
|
380 { |
|
381 GTypeInfo info = { 0, }; |
|
382 GTypeFundamentalInfo finfo = { 0, }; |
|
383 GType type; |
|
384 TestObject *sigarg; |
|
385 DerivedObject *dobject; |
|
386 TestObjectPrivate *priv; |
|
387 gchar *string = NULL; |
|
388 #ifdef __SYMBIAN32__ |
|
389 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
390 g_set_print_handler(mrtPrintHandler); |
|
391 #endif /*__SYMBIAN32__*/ |
|
392 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) | |
|
393 G_LOG_LEVEL_WARNING | |
|
394 G_LOG_LEVEL_CRITICAL); |
|
395 g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS); |
|
396 |
|
397 /* test new fundamentals */ |
|
398 g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST) == g_type_fundamental_next ()); |
|
399 type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow1", &info, &finfo, 0); |
|
400 g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1) == g_type_fundamental_next ()); |
|
401 type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow2", &info, &finfo, 0); |
|
402 g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 2) == g_type_fundamental_next ()); |
|
403 g_assert (g_type_from_name ("FooShadow1") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST)); |
|
404 g_assert (g_type_from_name ("FooShadow2") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1)); |
|
405 |
|
406 /* to test past class initialization interface setups, create the class here */ |
|
407 g_type_class_ref (TEST_TYPE_OBJECT); |
|
408 |
|
409 dobject = g_object_new (DERIVED_TYPE_OBJECT, NULL); |
|
410 test_object_check_private_init (TEST_OBJECT (dobject)); |
|
411 |
|
412 sigarg = g_object_new (TEST_TYPE_OBJECT, NULL); |
|
413 |
|
414 g_print ("MAIN: emit test-signal:\n"); |
|
415 g_signal_emit_by_name (dobject, "test-signal", sigarg, NULL, &string); |
|
416 g_message ("signal return: \"%s\"", string); |
|
417 g_assert (strcmp (string, "<default_handler><default_handler>") == 0); |
|
418 g_free (string); |
|
419 |
|
420 g_print ("MAIN: call iface print-string on test and derived object:\n"); |
|
421 iface_print_string (TEST_IFACE (sigarg), "iface-string-from-test-type"); |
|
422 iface_print_string (TEST_IFACE (dobject), "iface-string-from-derived-type"); |
|
423 |
|
424 priv = TEST_OBJECT_GET_PRIVATE (dobject); |
|
425 g_print ("private data after initialization: %u == %u\n", priv->dummy1, 54321); |
|
426 g_assert (priv->dummy1 == 54321); |
|
427 |
|
428 g_object_unref (sigarg); |
|
429 g_object_unref (dobject); |
|
430 |
|
431 g_message ("%s done", argv[0]); |
|
432 #ifdef __SYMBIAN32__ |
|
433 testResultXml("testgobject"); |
|
434 #endif /* EMULATOR */ |
|
435 return 0; |
|
436 } |