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