File: ifaceinit.c

package info (click to toggle)
glib2.0 2.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 46,108 kB
  • ctags: 31,166
  • sloc: ansic: 239,332; sh: 11,438; xml: 5,729; makefile: 2,551; perl: 1,259; python: 754; cpp: 9
file content (422 lines) | stat: -rw-r--r-- 11,834 bytes parent folder | download | duplicates (2)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/* GObject - GLib Type, Object, Parameter and Signal Library
 * Copyright (C) 2001, 2003 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#undef	G_LOG_DOMAIN
#define	G_LOG_DOMAIN "TestIfaceInit"

#undef G_DISABLE_ASSERT
#undef G_DISABLE_CHECKS
#undef G_DISABLE_CAST_CHECKS

#include <glib-object.h>

#include "testcommon.h"

/* What this test tests is the ability to add interfaces dynamically; in
 * particular adding interfaces to a class while that class is being
 * initialized.
 *
 * The test defines 5 interfaces:
 * 
 * - TestIface1 is added before the class is initialized
 * - TestIface2 is added in base_object_base_init()
 * - TestIface3 is added in test_iface1_base_init()
 * - TestIface4 is added in test_object_class_init()
 * - TestIface5 is added in test_object_test_iface1_init()
 * - TestIface6 is added after the class is initialized
 */

/* All 6 interfaces actually share the same class structure, though
 * we use separate typedefs
 */
typedef struct _TestIfaceClass TestIfaceClass;

struct _TestIfaceClass
{
  GTypeInterface base_iface;
  guint val;
  guint base_val;
  guint default_val;
};

#define TEST_TYPE_IFACE1           (test_iface1_get_type ())
#define TEST_IFACE1_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE1, TestIface1Class))
typedef struct _TestIface1      TestIface1;
typedef struct _TestIfaceClass  TestIface1Class;

static void test_iface1_base_init    (TestIface1Class *iface);
static void test_iface1_default_init (TestIface1Class *iface, gpointer class_data);

static DEFINE_IFACE(TestIface1, test_iface1, test_iface1_base_init, test_iface1_default_init)

#define TEST_TYPE_IFACE2           (test_iface2_get_type ())
#define TEST_IFACE2_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE2, TestIface2Class))
typedef struct _TestIface2      TestIface2;
typedef struct _TestIfaceClass  TestIface2Class;

static void test_iface2_base_init (TestIface2Class *iface);

static DEFINE_IFACE(TestIface2, test_iface2, test_iface2_base_init, NULL)

#define TEST_TYPE_IFACE3           (test_iface3_get_type ())
#define TEST_IFACE3_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE3, TestIface3Class))
typedef struct _TestIface3      TestIface3;
typedef struct _TestIfaceClass  TestIface3Class;

static void  test_iface3_base_init (TestIface3Class *iface);

static DEFINE_IFACE(TestIface3, test_iface3, test_iface3_base_init, NULL)

#define TEST_TYPE_IFACE4           (test_iface4_get_type ())
#define TEST_IFACE4_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE4, TestIface4Class))
typedef struct _TestIface4      TestIface4;
typedef struct _TestIfaceClass  TestIface4Class;

static void  test_iface4_base_init (TestIface4Class *iface);

static DEFINE_IFACE(TestIface4, test_iface4, test_iface4_base_init, NULL)

#define TEST_TYPE_IFACE5           (test_iface5_get_type ())
#define TEST_IFACE5_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE5, TestIface5Class))
typedef struct _TestIface5      TestIface5;
typedef struct _TestIfaceClass  TestIface5Class;

static void  test_iface5_base_init (TestIface5Class *iface);

static DEFINE_IFACE(TestIface5, test_iface5, test_iface5_base_init, NULL)

#define TEST_TYPE_IFACE6           (test_iface6_get_type ())
#define TEST_IFACE6_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE6, TestIface6Class))
typedef struct _TestIface6      TestIface6;
typedef struct _TestIfaceClass  TestIface6Class;

static void  test_iface6_base_init (TestIface6Class *iface);

static DEFINE_IFACE(TestIface6, test_iface6, test_iface6_base_init, NULL)

/*
 * BaseObject, a parent class for TestObject
 */
#define BASE_TYPE_OBJECT          (base_object_get_type ())
typedef struct _BaseObject        BaseObject;
typedef struct _BaseObjectClass   BaseObjectClass;

struct _BaseObject
{
  GObject parent_instance;
};
struct _BaseObjectClass
{
  GObjectClass parent_class;
};

/*
 * TestObject, a parent class for TestObject
 */
#define TEST_TYPE_OBJECT          (test_object_get_type ())
typedef struct _TestObject        TestObject;
typedef struct _TestObjectClass   TestObjectClass;

struct _TestObject
{
  BaseObject parent_instance;
};
struct _TestObjectClass
{
  BaseObjectClass parent_class;
};

#define TEST_CALLED_ONCE() G_STMT_START { \
  static gboolean called = 0;           \
  g_assert (!called);                   \
  called = TRUE;                        \
} G_STMT_END

#define CHECK_IFACE_TWICE(iface) G_STMT_START {                                 \
  static guint n_calls = 0;                                                     \
  n_calls++;                                                                    \
  g_assert (n_calls <= 2);                                                      \
  g_assert (G_TYPE_IS_INTERFACE (((GTypeInterface*) iface)->g_type));           \
  if (n_calls == 1)                                                             \
    g_assert (((GTypeInterface*) iface)->g_instance_type == 0);                 \
  else                                                                          \
    g_assert (G_TYPE_IS_OBJECT (((GTypeInterface*) iface)->g_instance_type));   \
} G_STMT_END

#define ADD_IFACE(n)  G_STMT_START {				\
  static GInterfaceInfo iface_info = {				\
    (GInterfaceInitFunc)test_object_test_iface##n##_init,	\
    NULL, NULL };						\
								\
  g_type_add_interface_static (TEST_TYPE_OBJECT,		\
			       test_iface##n##_get_type (),	\
			       &iface_info);			\
								\
} G_STMT_END

static gboolean base1, base2, base3, base4, base5, base6;
static gboolean iface1, iface2, iface3, iface4, iface5, iface6;

static void test_object_test_iface1_init (TestIface1Class *iface);
static void test_object_test_iface2_init (TestIface1Class *iface);
static void test_object_test_iface3_init (TestIface3Class *iface);
static void test_object_test_iface4_init (TestIface4Class *iface);
static void test_object_test_iface5_init (TestIface5Class *iface);
static void test_object_test_iface6_init (TestIface6Class *iface);

static GType test_object_get_type (void);

static void
test_object_test_iface1_init (TestIface1Class *iface)
{
  TEST_CALLED_ONCE();

  g_assert (iface->default_val == 0x111111);

  iface->val = 0x10001;

  ADD_IFACE(5);

  iface1 = TRUE;
}

static void
test_object_test_iface2_init (TestIface2Class *iface)
{
  TEST_CALLED_ONCE();
  
  iface->val = 0x20002;
  
  iface2 = TRUE;
}

static void
test_object_test_iface3_init (TestIface3Class *iface)
{
  TEST_CALLED_ONCE();
  
  iface->val = 0x30003;
  
  iface3 = TRUE;
}

static void
test_object_test_iface4_init (TestIface4Class *iface)
{
  TEST_CALLED_ONCE();

  iface->val = 0x40004;
  
  iface4 = TRUE;
}

static void
test_object_test_iface5_init (TestIface5Class *iface)
{
  TEST_CALLED_ONCE();

  iface->val = 0x50005;
  
  iface5 = TRUE;
}

static void
test_object_test_iface6_init (TestIface6Class *iface)
{
  TEST_CALLED_ONCE();

  iface->val = 0x60006;
  
  iface6 = TRUE;
}

static void
test_iface1_default_init (TestIface1Class *iface,
                          gpointer         class_data)
{
  TEST_CALLED_ONCE();
  g_assert (iface->base_iface.g_type == TEST_TYPE_IFACE1);
  g_assert (iface->base_iface.g_instance_type == 0);
  g_assert (iface->base_val == 0x110011);
  g_assert (iface->val == 0);
  g_assert (iface->default_val == 0);
  iface->default_val = 0x111111;
}

static void
test_iface1_base_init (TestIface1Class *iface)
{
  static guint n_calls = 0;
  n_calls++;
  g_assert (n_calls <= 2);

  if (n_calls == 1)
    {
      iface->base_val = 0x110011;
      g_assert (iface->default_val == 0);
    }
  else
    {
      g_assert (iface->base_val == 0x110011);
      g_assert (iface->default_val == 0x111111);
    }

  if (n_calls == 1)
    ADD_IFACE(3);
  
  base1 = TRUE;
}

static void
test_iface2_base_init (TestIface2Class *iface)
{
  CHECK_IFACE_TWICE (iface);

  iface->base_val = 0x220022;
  
  base2 = TRUE;
}

static void
test_iface3_base_init (TestIface3Class *iface)
{
  CHECK_IFACE_TWICE (iface);

  iface->base_val = 0x330033;
  
  base3 = TRUE;
}

static void
test_iface4_base_init (TestIface4Class *iface)
{
  CHECK_IFACE_TWICE (iface);

  iface->base_val = 0x440044;

  base4 = TRUE;
}

static void
test_iface5_base_init (TestIface5Class *iface)
{
  CHECK_IFACE_TWICE (iface);

  iface->base_val = 0x550055;

  base5 = TRUE;
}

static void
test_iface6_base_init (TestIface6Class *iface)
{
  CHECK_IFACE_TWICE (iface);

  iface->base_val = 0x660066;
  
  base6 = TRUE;
}

static void
base_object_base_init (BaseObjectClass *class)
{
  static int n_called = 0;
  n_called++;
  
  /* The second time this is called is for TestObject */
  if (n_called == 2)
    {
      ADD_IFACE(2);
      
      /* No interface base init functions should have been called yet
       */
      g_assert (!base1 && !base2 && !base3 && !base4 && !base5 && !base6);
      g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
    }
}

static void
test_object_class_init (TestObjectClass *class)
{
  ADD_IFACE(4);

  /* At this point, the base init functions for all interfaces that have
   * been added should be called, but no interface init functions.
   */
  g_assert (base1 && base2 && base3 && base4 && !base5 && !base6);
  g_assert (!iface1 && !iface2 && !iface3 && !iface4 && !iface5 && !iface6);
}

static DEFINE_TYPE(BaseObject, base_object,
		   NULL, base_object_base_init, NULL,
		   G_TYPE_OBJECT)
static DEFINE_TYPE(TestObject, test_object,
		   test_object_class_init, NULL, NULL,
		   BASE_TYPE_OBJECT)

int
main (int   argc,
      char *argv[])
{
  TestObject *object;
  TestObjectClass *object_class;
  TestIfaceClass *iface;
	
  g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
			  G_LOG_LEVEL_WARNING |
			  G_LOG_LEVEL_CRITICAL);
  g_type_init ();

  /* We force the interfaces to be registered in a different order
   * than we add them, so our logic doesn't always deal with interfaces
   * added at the end.
   */
  (void)TEST_TYPE_IFACE4;
  (void)TEST_TYPE_IFACE2;
  (void)TEST_TYPE_IFACE6;
  (void)TEST_TYPE_IFACE5;
  (void)TEST_TYPE_IFACE3;
  (void)TEST_TYPE_IFACE1;

  ADD_IFACE(1);

  object_class = g_type_class_ref (TEST_TYPE_OBJECT);

  ADD_IFACE(6);

  /* All base and interface init functions should have been called
   */
  g_assert (base1 && base2 && base3 && base4 && base5 && base6);
  g_assert (iface1 && iface2 && iface3 && iface4 && iface5 && iface6);
  
  object = g_object_new (TEST_TYPE_OBJECT, NULL);

  iface = TEST_IFACE1_GET_CLASS (object);
  g_assert (iface && iface->val == 0x10001 && iface->base_val == 0x110011);
  iface = TEST_IFACE3_GET_CLASS (object);
  g_assert (iface && iface->val == 0x30003 && iface->base_val == 0x330033);
  iface = TEST_IFACE4_GET_CLASS (object);
  g_assert (iface && iface->val == 0x40004 && iface->base_val == 0x440044);
  iface = TEST_IFACE5_GET_CLASS (object);
  g_assert (iface && iface->val == 0x50005 && iface->base_val == 0x550055);
  iface = TEST_IFACE6_GET_CLASS (object);
  g_assert (iface && iface->val == 0x60006 && iface->base_val == 0x660066);

  return 0;
}