=== Released 2.2.0 ===
[platform/upstream/glib.git] / gobject / testgobject.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001 Red Hat, Inc.
3  *
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
26 /* --- TestIface --- */
27 #define TEST_TYPE_IFACE           (test_iface_get_type ())
28 #define TEST_IFACE(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_IFACE, TestIface))
29 #define TEST_IS_IFACE(obj)        (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_IFACE))
30 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
31 typedef struct _TestIface      TestIface;
32 typedef struct _TestIfaceClass TestIfaceClass;
33 struct _TestIfaceClass
34 {
35   GTypeInterface base_iface;
36   void  (*print_string) (TestIface      *tiobj,
37                          const gchar    *string);
38 };
39 static void     iface_base_init         (TestIfaceClass *iface);
40 static void     iface_base_finalize     (TestIfaceClass *iface);
41 static void     print_foo               (TestIface      *tiobj,
42                                          const gchar    *string);
43 GType
44 test_iface_get_type (void)
45 {
46   static GType test_iface_type = 0;
47
48   if (!test_iface_type)
49     {
50       static const GTypeInfo test_iface_info =
51       {
52         sizeof (TestIfaceClass),
53         (GBaseInitFunc) iface_base_init,                /* base_init */
54         (GBaseFinalizeFunc) iface_base_finalize,        /* base_finalize */
55       };
56
57       test_iface_type = g_type_register_static (G_TYPE_INTERFACE, "TestIface", &test_iface_info, 0);
58       g_type_interface_add_prerequisite (test_iface_type, G_TYPE_OBJECT);
59     }
60
61   return test_iface_type;
62 }
63 static guint iface_base_init_count = 0;
64 static void
65 iface_base_init (TestIfaceClass *iface)
66 {
67   iface_base_init_count++;
68   if (iface_base_init_count == 1)
69     {
70       /* add signals here */
71     }
72 }
73 static void
74 iface_base_finalize (TestIfaceClass *iface)
75 {
76   iface_base_init_count--;
77   if (iface_base_init_count == 0)
78     {
79       /* destroy signals here */
80     }
81 }
82 static void
83 print_foo (TestIface   *tiobj,
84            const gchar *string)
85 {
86   if (!string)
87     string = "<NULL>";
88   g_print ("Iface-FOO: \"%s\" from %p\n", string, tiobj);
89 }
90 static void
91 test_object_test_iface_init (gpointer giface,
92                              gpointer iface_data)
93 {
94   TestIfaceClass *iface = giface;
95
96   g_assert (iface_data == GUINT_TO_POINTER (42));
97
98   g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
99
100   /* assert iface_base_init() was already called */
101   g_assert (iface_base_init_count > 0);
102
103   /* initialize stuff */
104   iface->print_string = print_foo;
105 }
106 void
107 iface_print_string (TestIface   *tiobj,
108                     const gchar *string)
109 {
110   TestIfaceClass *iface;
111
112   g_return_if_fail (TEST_IS_IFACE (tiobj));
113   g_return_if_fail (G_IS_OBJECT (tiobj)); /* ensured through prerequisite */
114
115   iface = TEST_IFACE_GET_CLASS (tiobj);
116   g_object_ref (tiobj);
117   iface->print_string (tiobj, string);
118   g_object_unref (tiobj);
119 }
120
121
122 /* --- TestObject --- */
123 #define TEST_TYPE_OBJECT            (test_object_get_type ())
124 #define TEST_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), TEST_TYPE_OBJECT, TestObject))
125 #define TEST_OBJECT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass))
126 #define TEST_IS_OBJECT(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), TEST_TYPE_OBJECT))
127 #define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT))
128 #define TEST_OBJECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass))
129 typedef struct _TestObject      TestObject;
130 typedef struct _TestObjectClass TestObjectClass;
131 struct _TestObject
132 {
133   GObject parent_instance;
134 };
135 struct _TestObjectClass
136 {
137   GObjectClass parent_class;
138
139   gchar* (*test_signal) (TestObject *tobject,
140                          TestIface  *iface_object,
141                          gpointer    tdata);
142 };
143 static void     test_object_class_init  (TestObjectClass        *class);
144 static void     test_object_init        (TestObject             *tobject);
145 static gboolean test_signal_accumulator (GSignalInvocationHint  *ihint,
146                                          GValue                 *return_accu,
147                                          const GValue           *handler_return,
148                                          gpointer                data);
149 static gchar*   test_object_test_signal (TestObject             *tobject,
150                                          TestIface              *iface_object,
151                                          gpointer                tdata);
152 GType
153 test_object_get_type (void)
154 {
155   static GType test_object_type = 0;
156
157   if (!test_object_type)
158     {
159       static const GTypeInfo test_object_info =
160       {
161         sizeof (TestObjectClass),
162         NULL,           /* base_init */
163         NULL,           /* base_finalize */
164         (GClassInitFunc) test_object_class_init,
165         NULL,           /* class_finalize */
166         NULL,           /* class_data */
167         sizeof (TestObject),
168         5,              /* n_preallocs */
169         (GInstanceInitFunc) test_object_init,
170       };
171       GInterfaceInfo iface_info = { test_object_test_iface_init, NULL, GUINT_TO_POINTER (42) };
172
173       test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0);
174       g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info);
175     }
176
177   return test_object_type;
178 }
179 static void
180 test_object_class_init (TestObjectClass *class)
181 {
182   /*  GObjectClass *gobject_class = G_OBJECT_CLASS (class); */
183
184   class->test_signal = test_object_test_signal;
185
186   g_signal_new ("test-signal",
187                 G_OBJECT_CLASS_TYPE (class),
188                 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
189                 G_STRUCT_OFFSET (TestObjectClass, test_signal),
190                 test_signal_accumulator, NULL,
191                 g_cclosure_marshal_STRING__OBJECT_POINTER,
192                 G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER);
193 }
194 static void
195 test_object_init (TestObject *tobject)
196 {
197 }
198 static gboolean
199 test_signal_accumulator (GSignalInvocationHint *ihint,
200                          GValue                *return_accu,
201                          const GValue          *handler_return,
202                          gpointer               data)
203 {
204   gchar *accu_string = g_value_get_string (return_accu);
205   gchar *new_string = g_value_get_string (handler_return);
206   gchar *result_string;
207
208   if (accu_string)
209     result_string = g_strconcat (accu_string, new_string, NULL);
210   else if (new_string)
211     result_string = g_strdup (new_string);
212   else
213     result_string = NULL;
214
215   g_value_set_string_take_ownership (return_accu, result_string);
216
217   return TRUE;
218 }
219 static gchar*
220 test_object_test_signal (TestObject *tobject,
221                          TestIface  *iface_object,
222                          gpointer    tdata)
223 {
224   g_message ("::test_signal default_handler called");
225
226   g_return_val_if_fail (TEST_IS_IFACE (iface_object), NULL);
227   
228   return g_strdup ("<default_handler>");
229 }
230
231
232 /* --- TestIface for DerivedObject --- */
233 static void
234 print_bar (TestIface   *tiobj,
235            const gchar *string)
236 {
237   TestIfaceClass *parent_iface;
238
239   g_return_if_fail (TEST_IS_IFACE (tiobj));
240
241   if (!string)
242     string = "<NULL>";
243   g_print ("Iface-BAR: \"%s\" from %p\n", string, tiobj);
244
245   g_print ("chaining: ");
246   parent_iface = g_type_interface_peek_parent (TEST_IFACE_GET_CLASS (tiobj));
247   parent_iface->print_string (tiobj, string);
248
249   g_assert (g_type_interface_peek_parent (parent_iface) == NULL);
250 }
251
252 static void
253 derived_object_test_iface_init (gpointer giface,
254                                 gpointer iface_data)
255 {
256   TestIfaceClass *iface = giface;
257
258   g_assert (iface_data == GUINT_TO_POINTER (87));
259
260   g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
261
262   /* assert test_object_test_iface_init() was already called */
263   g_assert (iface->print_string == print_foo);
264
265   /* override stuff */
266   iface->print_string = print_bar;
267 }
268
269
270 /* --- DerivedObject --- */
271 #define DERIVED_TYPE_OBJECT            (derived_object_get_type ())
272 #define DERIVED_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), DERIVED_TYPE_OBJECT, DerivedObject))
273 #define DERIVED_OBJECT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), DERIVED_TYPE_OBJECT, DerivedObjectClass))
274 #define DERIVED_IS_OBJECT(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), DERIVED_TYPE_OBJECT))
275 #define DERIVED_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DERIVED_TYPE_OBJECT))
276 #define DERIVED_OBJECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), DERIVED_TYPE_OBJECT, DerivedObjectClass))
277 typedef struct _TestObject      DerivedObject;
278 typedef struct _TestObjectClass DerivedObjectClass;
279 GType
280 derived_object_get_type (void)
281 {
282   static GType derived_object_type = 0;
283
284   if (!derived_object_type)
285     {
286       static const GTypeInfo derived_object_info =
287       {
288         sizeof (DerivedObjectClass),
289         NULL,           /* base_init */
290         NULL,           /* base_finalize */
291         NULL,           /* class_init */
292         NULL,           /* class_finalize */
293         NULL,           /* class_data */
294         sizeof (DerivedObject),
295         5,              /* n_preallocs */
296         NULL,           /* instance_init */
297       };
298       GInterfaceInfo iface_info = { derived_object_test_iface_init, NULL, GUINT_TO_POINTER (87) };
299
300       derived_object_type = g_type_register_static (TEST_TYPE_OBJECT, "DerivedObject", &derived_object_info, 0);
301       g_type_add_interface_static (derived_object_type, TEST_TYPE_IFACE, &iface_info);
302     }
303
304   return derived_object_type;
305 }
306
307
308 /* --- main --- */
309 int
310 main (int   argc,
311       char *argv[])
312 {
313   GTypeInfo info = { 0, };
314   GTypeFundamentalInfo finfo = { 0, };
315   GType type;
316   TestObject *sigarg;
317   DerivedObject *dobject;
318   gchar *string = NULL;
319
320   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
321                           G_LOG_LEVEL_WARNING |
322                           G_LOG_LEVEL_CRITICAL);
323   g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
324
325   /* test new fundamentals */
326   g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST) == g_type_fundamental_next ());
327   type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow1", &info, &finfo, 0);
328   g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1) == g_type_fundamental_next ());
329   type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow2", &info, &finfo, 0);
330   g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 2) == g_type_fundamental_next ());
331   g_assert (g_type_from_name ("FooShadow1") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
332   g_assert (g_type_from_name ("FooShadow2") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1));
333
334   /* to test past class initialization interface setups, create the class here */
335   g_type_class_ref (TEST_TYPE_OBJECT);
336
337   dobject = g_object_new (DERIVED_TYPE_OBJECT, NULL);
338   sigarg = g_object_new (TEST_TYPE_OBJECT, NULL);
339
340   g_print ("MAIN: emit test-signal:\n");
341   g_signal_emit_by_name (dobject, "test-signal", sigarg, NULL, &string);
342   g_message ("signal return: \"%s\"", string);
343   g_assert (strcmp (string, "<default_handler><default_handler>") == 0);
344   g_free (string);
345
346   g_print ("MAIN: call iface print-string on test and derived object:\n");
347   iface_print_string (TEST_IFACE (sigarg), "iface-string-from-test-type");
348   iface_print_string (TEST_IFACE (dobject), "iface-string-from-derived-type");
349   
350   g_object_unref (sigarg);
351   g_object_unref (dobject);
352
353   g_message ("%s done", argv[0]);
354
355   return 0;
356 }