1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001, 2003 Red Hat, Inc.
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.
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.
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/>.
19 #define G_LOG_DOMAIN "TestIfaceInherit"
21 #undef G_DISABLE_ASSERT
22 #undef G_DISABLE_CHECKS
23 #undef G_DISABLE_CAST_CHECKS
25 #include <glib-object.h>
27 #include "testcommon.h"
28 #include "testmodule.h"
30 /* This test tests inheritance of interface. We two object
31 * class BaseObject and DerivedObject we add an interface
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
40 * We also do some tests of overriding.
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)
50 * BaseObject, a parent class for DerivedObject
52 #define BASE_TYPE_OBJECT (base_object_get_type ())
53 typedef struct _BaseObject BaseObject
;
54 typedef struct _BaseObjectClass BaseObjectClass
;
58 GObject parent_instance
;
60 struct _BaseObjectClass
62 GObjectClass parent_class
;
65 static GType
base_object_get_type (void);
66 static GType
derived_object_get_type (void);
69 * DerivedObject, the child class of DerivedObject
71 #define DERIVED_TYPE_OBJECT (derived_object_get_type ())
72 typedef struct _DerivedObject DerivedObject
;
73 typedef struct _DerivedObjectClass DerivedObjectClass
;
77 BaseObject parent_instance
;
79 struct _DerivedObjectClass
81 BaseObjectClass parent_class
;
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
;
94 struct _TestIfaceClass
96 GTypeInterface base_iface
;
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);
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 ())
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
)
119 add_interface (GType object_type
,
121 GInterfaceInitFunc init_func
)
123 GInterfaceInfo iface_info
= { NULL
, NULL
, NULL
};
125 iface_info
.interface_init
= init_func
;
127 g_type_add_interface_static (object_type
, iface_type
, &iface_info
);
131 init_base_interface (TestIfaceClass
*iface
)
137 add_base_interface (GType object_type
,
140 add_interface (object_type
, iface_type
,
141 (GInterfaceInitFunc
)init_base_interface
);
145 interface_is_base (GType object_type
,
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;
154 init_derived_interface (TestIfaceClass
*iface
)
160 add_derived_interface (GType object_type
,
163 add_interface (object_type
, iface_type
,
164 (GInterfaceInitFunc
)init_derived_interface
);
168 interface_is_derived (GType object_type
,
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;
177 derived_object_class_init (BaseObjectClass
*class)
179 add_base_interface (BASE_TYPE_OBJECT
, TEST_TYPE_IFACE3
);
182 static DEFINE_TYPE(BaseObject
, base_object
,
185 static DEFINE_TYPE(DerivedObject
, derived_object
,
186 derived_object_class_init
, NULL
, NULL
,
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
);
197 /* Register BaseObject */
200 add_base_interface (BASE_TYPE_OBJECT
, TEST_TYPE_IFACE5
);
202 /* Class init BaseObject */
203 g_type_class_ref (BASE_TYPE_OBJECT
);
205 add_base_interface (BASE_TYPE_OBJECT
, TEST_TYPE_IFACE1
);
207 /* Register DerivedObject */
210 add_base_interface (BASE_TYPE_OBJECT
, TEST_TYPE_IFACE2
);
211 add_derived_interface (DERIVED_TYPE_OBJECT
, TEST_TYPE_IFACE5
);
213 /* Class init DerivedObject */
214 g_type_class_ref (DERIVED_TYPE_OBJECT
);
216 add_base_interface (BASE_TYPE_OBJECT
, TEST_TYPE_IFACE4
);
218 /* Check that all the non-overridden interfaces were properly inherited
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
));
225 /* Check that all the overridden interfaces were properly overridden
227 g_assert (interface_is_derived (DERIVED_TYPE_OBJECT
, TEST_TYPE_IFACE5
));