1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#include <glib-object.h>
static void
test_registration_serial (void)
{
gint serial1, serial2, serial3;
serial1 = g_type_get_type_registration_serial ();
g_pointer_type_register_static ("my+pointer");
serial2 = g_type_get_type_registration_serial ();
g_assert (serial1 != serial2);
serial3 = g_type_get_type_registration_serial ();
g_assert (serial2 == serial3);
}
typedef struct {
GTypeInterface g_iface;
} BarInterface;
GType bar_get_type (void);
G_DEFINE_INTERFACE (Bar, bar, G_TYPE_OBJECT)
static void
bar_default_init (BarInterface *iface)
{
}
typedef struct {
GTypeInterface g_iface;
} FooInterface;
GType foo_get_type (void);
G_DEFINE_INTERFACE_WITH_CODE (Foo, foo, G_TYPE_OBJECT,
g_type_interface_add_prerequisite (g_define_type_id, bar_get_type ()))
static void
foo_default_init (FooInterface *iface)
{
}
typedef struct {
GTypeInterface g_iface;
} BaaInterface;
GType baa_get_type (void);
G_DEFINE_INTERFACE (Baa, baa, G_TYPE_INVALID)
static void
baa_default_init (BaaInterface *iface)
{
}
typedef struct {
GTypeInterface g_iface;
} BooInterface;
GType boo_get_type (void);
G_DEFINE_INTERFACE_WITH_CODE (Boo, boo, G_TYPE_INVALID,
g_type_interface_add_prerequisite (g_define_type_id, baa_get_type ()))
static void
boo_default_init (BooInterface *iface)
{
}
typedef struct {
GTypeInterface g_iface;
} BibiInterface;
GType bibi_get_type (void);
G_DEFINE_INTERFACE (Bibi, bibi, G_TYPE_INITIALLY_UNOWNED)
static void
bibi_default_init (BibiInterface *iface)
{
}
typedef struct {
GTypeInterface g_iface;
} BozoInterface;
GType bozo_get_type (void);
G_DEFINE_INTERFACE_WITH_CODE (Bozo, bozo, G_TYPE_INVALID,
g_type_interface_add_prerequisite (g_define_type_id, foo_get_type ());
g_type_interface_add_prerequisite (g_define_type_id, bibi_get_type ()))
static void
bozo_default_init (BozoInterface *iface)
{
}
static void
test_interface_prerequisite (void)
{
GType *prereqs;
guint n_prereqs;
gpointer iface;
gpointer parent;
prereqs = g_type_interface_prerequisites (foo_get_type (), &n_prereqs);
g_assert_cmpint (n_prereqs, ==, 2);
g_assert (prereqs[0] == bar_get_type ());
g_assert (prereqs[1] == G_TYPE_OBJECT);
g_assert (g_type_interface_instantiatable_prerequisite (foo_get_type ()) == G_TYPE_OBJECT);
iface = g_type_default_interface_ref (foo_get_type ());
parent = g_type_interface_peek_parent (iface);
g_assert (parent == NULL);
g_type_default_interface_unref (iface);
g_free (prereqs);
g_assert_cmpint (g_type_interface_instantiatable_prerequisite (baa_get_type ()), ==, G_TYPE_INVALID);
g_assert_cmpint (g_type_interface_instantiatable_prerequisite (boo_get_type ()), ==, G_TYPE_INVALID);
g_assert_cmpint (g_type_interface_instantiatable_prerequisite (bozo_get_type ()), ==, G_TYPE_INITIALLY_UNOWNED);
}
typedef struct {
GTypeInterface g_iface;
} BazInterface;
GType baz_get_type (void);
G_DEFINE_INTERFACE (Baz, baz, G_TYPE_OBJECT)
static void
baz_default_init (BazInterface *iface)
{
}
typedef struct {
GObject parent;
} Bazo;
typedef struct {
GObjectClass parent_class;
} BazoClass;
GType bazo_get_type (void);
static void bazo_iface_init (BazInterface *i);
G_DEFINE_TYPE_WITH_CODE (Bazo, bazo, G_TYPE_INITIALLY_UNOWNED,
G_IMPLEMENT_INTERFACE (baz_get_type (),
bazo_iface_init);)
static void
bazo_init (Bazo *b)
{
}
static void
bazo_class_init (BazoClass *c)
{
}
static void
bazo_iface_init (BazInterface *i)
{
}
static gint check_called;
static void
check_func (gpointer check_data,
gpointer g_iface)
{
g_assert (check_data == &check_called);
check_called++;
}
static void
test_interface_check (void)
{
GObject *o;
check_called = 0;
g_type_add_interface_check (&check_called, check_func);
o = g_object_ref_sink (g_object_new (bazo_get_type (), NULL));
g_object_unref (o);
g_assert_cmpint (check_called, ==, 1);
g_type_remove_interface_check (&check_called, check_func);
}
static void
test_next_base (void)
{
GType type;
type = g_type_next_base (bazo_get_type (), G_TYPE_OBJECT);
g_assert (type == G_TYPE_INITIALLY_UNOWNED);
}
/* Test that the macro an function versions of g_type_is_a
* work the same
*/
static void
test_is_a (void)
{
g_assert_true (g_type_is_a (G_TYPE_OBJECT, G_TYPE_OBJECT));
g_assert_true ((g_type_is_a) (G_TYPE_OBJECT, G_TYPE_OBJECT));
g_assert_true (g_type_is_a (bar_get_type (), G_TYPE_OBJECT));
g_assert_true ((g_type_is_a) (bar_get_type (), G_TYPE_OBJECT));
g_assert_false (g_type_is_a (bar_get_type (), bibi_get_type ()));
g_assert_false ((g_type_is_a) (bar_get_type (), bibi_get_type ()));
}
int
main (int argc, char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/type/registration-serial", test_registration_serial);
g_test_add_func ("/type/interface-prerequisite", test_interface_prerequisite);
g_test_add_func ("/type/interface-check", test_interface_check);
g_test_add_func ("/type/next-base", test_next_base);
g_test_add_func ("/type/is-a", test_is_a);
return g_test_run ();
}
|