More clean up for Unicode corrigendum #9
[platform/upstream/glib.git] / tests / gobject / ifaceinherit.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001, 2003 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 #undef  G_LOG_DOMAIN
21 #define G_LOG_DOMAIN "TestIfaceInherit"
22
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
26
27 #include <glib-object.h>
28
29 #include "testcommon.h"
30 #include "testmodule.h"
31
32 /* This test tests inheritance of interface. We two object
33  * class BaseObject and DerivedObject we add an interface
34  * to BaseObject:
35  *
36  * I1) Before DerivedObject is registered
37  * I2) After DerivedObject is registered, but before
38  *     DerivedObject is class initialized
39  * I3) During DerivedObject's class_init
40  * I4) After DerivedObject's class init
41  *
42  * We also do some tests of overriding.
43  * 
44  * I5) We add an interface to BaseObject, then add the same
45  *     interface to DerivedObject. (Note that this is only legal
46  *     before DerivedObject's class_init; the results of
47  *     g_type_interface_peek() are not allowed to change from one
48  *     non-NULL vtable to another non-NULL vtable)
49  */
50    
51 /*
52  * BaseObject, a parent class for DerivedObject
53  */
54 #define BASE_TYPE_OBJECT          (base_object_get_type ())
55 typedef struct _BaseObject        BaseObject;
56 typedef struct _BaseObjectClass   BaseObjectClass;
57
58 struct _BaseObject
59 {
60   GObject parent_instance;
61 };
62 struct _BaseObjectClass
63 {
64   GObjectClass parent_class;
65 };
66
67 static GType base_object_get_type (void);
68 static GType derived_object_get_type (void);
69
70 /*
71  * DerivedObject, the child class of DerivedObject
72  */
73 #define DERIVED_TYPE_OBJECT          (derived_object_get_type ())
74 typedef struct _DerivedObject        DerivedObject;
75 typedef struct _DerivedObjectClass   DerivedObjectClass;
76
77 struct _DerivedObject
78 {
79   BaseObject parent_instance;
80 };
81 struct _DerivedObjectClass
82 {
83   BaseObjectClass parent_class;
84 };
85
86 /*
87  * The interfaces
88  */
89 typedef struct _TestIfaceClass TestIfaceClass;
90 typedef struct _TestIfaceClass TestIface1Class;
91 typedef struct _TestIfaceClass TestIface2Class;
92 typedef struct _TestIfaceClass TestIface3Class;
93 typedef struct _TestIfaceClass TestIface4Class;
94 typedef struct _TestIfaceClass TestIface5Class;
95
96 struct _TestIfaceClass
97 {
98   GTypeInterface base_iface;
99   guint val;
100 };
101
102 static GType test_iface1_get_type (void);
103 static GType test_iface2_get_type (void);
104 static GType test_iface3_get_type (void);
105 static GType test_iface4_get_type (void);
106 static GType test_iface5_get_type (void);
107
108 #define TEST_TYPE_IFACE1 (test_iface1_get_type ())
109 #define TEST_TYPE_IFACE2 (test_iface2_get_type ())
110 #define TEST_TYPE_IFACE3 (test_iface3_get_type ())
111 #define TEST_TYPE_IFACE4 (test_iface4_get_type ())
112 #define TEST_TYPE_IFACE5 (test_iface5_get_type ())
113
114 static DEFINE_IFACE (TestIface1, test_iface1,  NULL, NULL)
115 static DEFINE_IFACE (TestIface2, test_iface2,  NULL, NULL)
116 static DEFINE_IFACE (TestIface3, test_iface3,  NULL, NULL)
117 static DEFINE_IFACE (TestIface4, test_iface4,  NULL, NULL)
118 static DEFINE_IFACE (TestIface5, test_iface5,  NULL, NULL)
119
120 static void
121 add_interface (GType              object_type,
122                GType              iface_type,
123                GInterfaceInitFunc init_func)
124 {
125   GInterfaceInfo iface_info = { NULL, NULL, NULL };
126
127   iface_info.interface_init = init_func;
128                                                                 
129   g_type_add_interface_static (object_type, iface_type, &iface_info);
130 }
131
132 static void
133 init_base_interface (TestIfaceClass *iface)
134 {
135   iface->val = 21;
136 }
137
138 static void
139 add_base_interface (GType object_type,
140                     GType iface_type)
141 {
142   add_interface (object_type, iface_type,
143                  (GInterfaceInitFunc)init_base_interface);
144 }
145
146 static gboolean
147 interface_is_base (GType object_type,
148                    GType iface_type)
149 {
150   gpointer g_class = g_type_class_peek (object_type);
151   TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
152   return iface && iface->val == 21;
153 }
154
155 static void
156 init_derived_interface (TestIfaceClass *iface)
157 {
158   iface->val = 42;
159 }
160
161 static void
162 add_derived_interface (GType object_type,
163                        GType iface_type)
164 {
165   add_interface (object_type, iface_type,
166                  (GInterfaceInitFunc)init_derived_interface);
167 }
168
169 static gboolean
170 interface_is_derived (GType object_type,
171                       GType iface_type)
172 {
173   gpointer g_class = g_type_class_peek (object_type);
174   TestIfaceClass *iface = g_type_interface_peek (g_class, iface_type);
175   return iface && iface->val == 42;
176 }
177
178 static void
179 derived_object_class_init (BaseObjectClass *class)
180 {
181   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE3);
182 }
183
184 static DEFINE_TYPE(BaseObject, base_object,
185                    NULL, NULL, NULL,
186                    G_TYPE_OBJECT)
187 static DEFINE_TYPE(DerivedObject, derived_object,
188                    derived_object_class_init, NULL, NULL,
189                    BASE_TYPE_OBJECT)
190
191 int
192 main (int   argc,
193       char *argv[])
194 {
195   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
196                           G_LOG_LEVEL_WARNING |
197                           G_LOG_LEVEL_CRITICAL);
198
199   /* Register BaseObject */
200   BASE_TYPE_OBJECT;
201   
202   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE5);
203
204   /* Class init BaseObject */
205   g_type_class_ref (BASE_TYPE_OBJECT);
206   
207   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE1);
208
209   /* Register DerivedObject */
210   DERIVED_TYPE_OBJECT;
211
212   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE2);
213   add_derived_interface (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5);
214
215   /* Class init DerivedObject */
216   g_type_class_ref (DERIVED_TYPE_OBJECT);
217   
218   add_base_interface (BASE_TYPE_OBJECT, TEST_TYPE_IFACE4);
219
220   /* Check that all the non-overridden interfaces were properly inherited
221    */
222   g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE1));
223   g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE2));
224   g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE3));
225   g_assert (interface_is_base (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE4));
226
227   /* Check that all the overridden interfaces were properly overridden
228    */
229   g_assert (interface_is_derived (DERIVED_TYPE_OBJECT, TEST_TYPE_IFACE5));
230
231   return 0;
232 }