• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.1 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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <string.h>
19 
20 #undef	G_LOG_DOMAIN
21 #define	G_LOG_DOMAIN "TestObject"
22 #include	<glib-object.h>
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 static GType
test_iface_get_type(void)42 test_iface_get_type (void)
43 {
44   static GType test_iface_type = 0;
45 
46   if (!test_iface_type)
47     {
48       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
iface_base_init(TestIfaceClass * iface)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
iface_base_finalize(TestIfaceClass * iface)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
print_foo(TestIface * tiobj,const gchar * string)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
test_object_test_iface_init(gpointer giface,gpointer iface_data)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 static void
iface_print_string(TestIface * tiobj,const gchar * string)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 #define TEST_OBJECT_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), TEST_TYPE_OBJECT, TestObjectPrivate))
128 typedef struct _TestObject        TestObject;
129 typedef struct _TestObjectClass   TestObjectClass;
130 typedef struct _TestObjectPrivate TestObjectPrivate;
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 struct _TestObjectPrivate
144 {
145   int     dummy1;
146   gdouble dummy2;
147 };
148 static void	test_object_class_init	(TestObjectClass	*class);
149 static void	test_object_init	(TestObject		*tobject);
150 static gboolean	test_signal_accumulator	(GSignalInvocationHint	*ihint,
151 					 GValue            	*return_accu,
152 					 const GValue       	*handler_return,
153 					 gpointer                data);
154 static gchar*	test_object_test_signal	(TestObject		*tobject,
155 					 TestIface		*iface_object,
156 					 gpointer		 tdata);
157 static gint TestObject_private_offset;
158 static inline gpointer
test_object_get_instance_private(TestObject * self)159 test_object_get_instance_private (TestObject *self)
160 {
161   return (G_STRUCT_MEMBER_P (self, TestObject_private_offset));
162 }
163 
164 static GType
test_object_get_type(void)165 test_object_get_type (void)
166 {
167   static GType test_object_type = 0;
168 
169   if (!test_object_type)
170     {
171       const GTypeInfo test_object_info =
172       {
173 	sizeof (TestObjectClass),
174 	NULL,           /* base_init */
175 	NULL,           /* base_finalize */
176 	(GClassInitFunc) test_object_class_init,
177 	NULL,           /* class_finalize */
178 	NULL,           /* class_data */
179 	sizeof (TestObject),
180 	5,              /* n_preallocs */
181 	(GInstanceInitFunc) test_object_init,
182       };
183       GInterfaceInfo iface_info = { test_object_test_iface_init, NULL, GUINT_TO_POINTER (42) };
184 
185       test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0);
186       g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info);
187 
188       TestObject_private_offset =
189         g_type_add_instance_private (test_object_type, sizeof (TestObjectPrivate));
190     }
191 
192   return test_object_type;
193 }
194 static void
test_object_class_init(TestObjectClass * class)195 test_object_class_init (TestObjectClass *class)
196 {
197   /*  GObjectClass *gobject_class = G_OBJECT_CLASS (class); */
198   g_type_class_adjust_private_offset (class, &TestObject_private_offset);
199 
200   class->test_signal = test_object_test_signal;
201 
202   g_signal_new ("test-signal",
203 		G_OBJECT_CLASS_TYPE (class),
204 		G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
205 		G_STRUCT_OFFSET (TestObjectClass, test_signal),
206 		test_signal_accumulator, NULL,
207 		g_cclosure_marshal_STRING__OBJECT_POINTER,
208 		G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER);
209 }
210 static void
test_object_init(TestObject * tobject)211 test_object_init (TestObject *tobject)
212 {
213   TestObjectPrivate *priv = test_object_get_instance_private (tobject);
214 
215   g_assert (priv);
216 
217   priv->dummy1 = 54321;
218 }
219 /* Check to see if private data initialization in the
220  * instance init function works.
221  */
222 static void
test_object_check_private_init(TestObject * tobject)223 test_object_check_private_init (TestObject *tobject)
224 {
225   TestObjectPrivate *priv = test_object_get_instance_private (tobject);
226 
227   g_print ("private data during initialization: %u == %u\n", priv->dummy1, 54321);
228   g_assert (priv->dummy1 == 54321);
229 }
230 static gboolean
test_signal_accumulator(GSignalInvocationHint * ihint,GValue * return_accu,const GValue * handler_return,gpointer data)231 test_signal_accumulator (GSignalInvocationHint *ihint,
232 			 GValue                *return_accu,
233 			 const GValue          *handler_return,
234 			 gpointer               data)
235 {
236   const gchar *accu_string = g_value_get_string (return_accu);
237   const gchar *new_string = g_value_get_string (handler_return);
238   gchar *result_string;
239 
240   if (accu_string)
241     result_string = g_strconcat (accu_string, new_string, NULL);
242   else if (new_string)
243     result_string = g_strdup (new_string);
244   else
245     result_string = NULL;
246 
247   g_value_take_string (return_accu, result_string);
248 
249   return TRUE;
250 }
251 static gchar*
test_object_test_signal(TestObject * tobject,TestIface * iface_object,gpointer tdata)252 test_object_test_signal (TestObject *tobject,
253 			 TestIface  *iface_object,
254 			 gpointer    tdata)
255 {
256   g_message ("::test_signal default_handler called");
257 
258   g_return_val_if_fail (TEST_IS_IFACE (iface_object), NULL);
259 
260   return g_strdup ("<default_handler>");
261 }
262 
263 
264 /* --- TestIface for DerivedObject --- */
265 static void
print_bar(TestIface * tiobj,const gchar * string)266 print_bar (TestIface   *tiobj,
267 	   const gchar *string)
268 {
269   TestIfaceClass *parent_iface;
270 
271   g_return_if_fail (TEST_IS_IFACE (tiobj));
272 
273   if (!string)
274     string = "<NULL>";
275   g_print ("Iface-BAR: \"%s\" from %p\n", string, tiobj);
276 
277   g_print ("chaining: ");
278   parent_iface = g_type_interface_peek_parent (TEST_IFACE_GET_CLASS (tiobj));
279   parent_iface->print_string (tiobj, string);
280 
281   g_assert (g_type_interface_peek_parent (parent_iface) == NULL);
282 }
283 
284 static void
derived_object_test_iface_init(gpointer giface,gpointer iface_data)285 derived_object_test_iface_init (gpointer giface,
286 				gpointer iface_data)
287 {
288   TestIfaceClass *iface = giface;
289 
290   g_assert (iface_data == GUINT_TO_POINTER (87));
291 
292   g_assert (G_TYPE_FROM_INTERFACE (iface) == TEST_TYPE_IFACE);
293 
294   /* assert test_object_test_iface_init() was already called */
295   g_assert (iface->print_string == print_foo);
296 
297   /* override stuff */
298   iface->print_string = print_bar;
299 }
300 
301 
302 /* --- DerivedObject --- */
303 #define DERIVED_TYPE_OBJECT            (derived_object_get_type ())
304 #define DERIVED_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), DERIVED_TYPE_OBJECT, DerivedObject))
305 #define DERIVED_OBJECT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), DERIVED_TYPE_OBJECT, DerivedObjectClass))
306 #define DERIVED_IS_OBJECT(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), DERIVED_TYPE_OBJECT))
307 #define DERIVED_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DERIVED_TYPE_OBJECT))
308 #define DERIVED_OBJECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), DERIVED_TYPE_OBJECT, DerivedObjectClass))
309 #define DERIVED_OBJECT_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), DERIVED_TYPE_OBJECT, DerivedObjectPrivate))
310 typedef struct _DerivedObject        DerivedObject;
311 typedef struct _TestObjectClass      DerivedObjectClass;
312 typedef struct _DerivedObjectPrivate DerivedObjectPrivate;
313 struct _DerivedObject
314 {
315   TestObject parent_instance;
316   int  dummy1;
317   int  dummy2;
318 };
319 struct _DerivedObjectPrivate
320 {
321   char dummy;
322 };
323 static void derived_object_class_init (DerivedObjectClass *class);
324 static void derived_object_init       (DerivedObject      *dobject);
325 static gint DerivedObject_private_offset;
326 static inline gpointer
derived_object_get_instance_private(DerivedObject * self)327 derived_object_get_instance_private (DerivedObject *self)
328 {
329   return (G_STRUCT_MEMBER_P (self, DerivedObject_private_offset));
330 }
331 static GType
derived_object_get_type(void)332 derived_object_get_type (void)
333 {
334   static GType derived_object_type = 0;
335 
336   if (!derived_object_type)
337     {
338       const GTypeInfo derived_object_info =
339       {
340 	sizeof (DerivedObjectClass),
341 	NULL,           /* base_init */
342 	NULL,           /* base_finalize */
343 	(GClassInitFunc) derived_object_class_init,
344 	NULL,           /* class_finalize */
345 	NULL,           /* class_data */
346 	sizeof (DerivedObject),
347 	5,              /* n_preallocs */
348 	(GInstanceInitFunc) derived_object_init,
349       };
350       GInterfaceInfo iface_info = { derived_object_test_iface_init, NULL, GUINT_TO_POINTER (87) };
351 
352       derived_object_type = g_type_register_static (TEST_TYPE_OBJECT, "DerivedObject", &derived_object_info, 0);
353       g_type_add_interface_static (derived_object_type, TEST_TYPE_IFACE, &iface_info);
354       DerivedObject_private_offset =
355         g_type_add_instance_private (derived_object_type, sizeof (DerivedObjectPrivate));
356     }
357 
358   return derived_object_type;
359 }
360 static void
derived_object_class_init(DerivedObjectClass * class)361 derived_object_class_init (DerivedObjectClass *class)
362 {
363   g_type_class_adjust_private_offset (class, &DerivedObject_private_offset);
364 }
365 static void
derived_object_init(DerivedObject * dobject)366 derived_object_init (DerivedObject *dobject)
367 {
368   TestObjectPrivate *test_priv;
369   DerivedObjectPrivate *derived_priv;
370 
371   derived_priv = derived_object_get_instance_private (dobject);
372 
373   g_assert (derived_priv);
374 
375   test_priv = test_object_get_instance_private (TEST_OBJECT (dobject));
376 
377   g_assert (test_priv);
378 }
379 
380 /* --- main --- */
381 int
main(int argc,char * argv[])382 main (int   argc,
383       char *argv[])
384 {
385   GTypeInfo info = { 0, };
386   GTypeFundamentalInfo finfo = { 0, };
387   GType type;
388   TestObject *sigarg;
389   DerivedObject *dobject;
390   TestObjectPrivate *priv;
391   gchar *string = NULL;
392 
393   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
394 			  G_LOG_LEVEL_WARNING |
395 			  G_LOG_LEVEL_CRITICAL);
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 (type == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
401   g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1) == g_type_fundamental_next ());
402   type = g_type_register_fundamental (g_type_fundamental_next (), "FooShadow2", &info, &finfo, 0);
403   g_assert (type == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1));
404   g_assert (G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 2) == g_type_fundamental_next ());
405   g_assert (g_type_from_name ("FooShadow1") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST));
406   g_assert (g_type_from_name ("FooShadow2") == G_TYPE_MAKE_FUNDAMENTAL (G_TYPE_RESERVED_USER_FIRST + 1));
407 
408   /* to test past class initialization interface setups, create the class here */
409   g_type_class_ref (TEST_TYPE_OBJECT);
410 
411   dobject = g_object_new (DERIVED_TYPE_OBJECT, NULL);
412   test_object_check_private_init (TEST_OBJECT (dobject));
413 
414   sigarg = g_object_new (TEST_TYPE_OBJECT, NULL);
415 
416   g_print ("MAIN: emit test-signal:\n");
417   g_signal_emit_by_name (dobject, "test-signal", sigarg, NULL, &string);
418   g_message ("signal return: \"%s\"", string);
419   g_assert_cmpstr (string, ==, "<default_handler><default_handler><default_handler>");
420   g_free (string);
421 
422   g_print ("MAIN: call iface print-string on test and derived object:\n");
423   iface_print_string (TEST_IFACE (sigarg), "iface-string-from-test-type");
424   iface_print_string (TEST_IFACE (dobject), "iface-string-from-derived-type");
425 
426   priv = test_object_get_instance_private (TEST_OBJECT (dobject));
427   g_print ("private data after initialization: %u == %u\n", priv->dummy1, 54321);
428   g_assert (priv->dummy1 == 54321);
429 
430   g_object_unref (sigarg);
431   g_object_unref (dobject);
432 
433   g_message ("%s done", argv[0]);
434 
435   return 0;
436 }
437