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