finish up vector deserialiser
[platform/upstream/glib.git] / tests / gobject / dynamictype.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 "TestDynamicType"
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 the macros for defining dynamic types.
31  */
32
33 static gboolean loaded = FALSE;
34
35 struct _TestIfaceClass
36 {
37   GTypeInterface base_iface;
38   guint val;
39 };
40
41 static GType test_iface_get_type (void);
42 #define TEST_TYPE_IFACE           (test_iface_get_type ())
43 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
44 typedef struct _TestIface      TestIface;
45 typedef struct _TestIfaceClass TestIfaceClass;
46
47 static void test_iface_base_init    (TestIfaceClass *iface);
48 static void test_iface_default_init (TestIfaceClass *iface, gpointer class_data);
49
50 static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, test_iface_default_init)
51
52 static void
53 test_iface_default_init (TestIfaceClass *iface,
54                          gpointer        class_data)
55 {
56 }
57
58 static void
59 test_iface_base_init (TestIfaceClass *iface)
60 {
61 }
62
63 GType dynamic_object_get_type (void);
64 #define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
65
66 typedef GObject DynamicObject;
67 typedef struct _DynamicObjectClass DynamicObjectClass;
68
69 struct _DynamicObjectClass
70 {
71   GObjectClass parent_class;
72   guint val;
73 };
74
75 static void dynamic_object_iface_init (TestIface *iface);
76
77 G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynamicObject, dynamic_object, G_TYPE_OBJECT, 0,
78                                G_IMPLEMENT_INTERFACE_DYNAMIC (TEST_TYPE_IFACE,
79                                                               dynamic_object_iface_init));
80
81 static void 
82 dynamic_object_class_init (DynamicObjectClass *class)
83 {
84   class->val = 42;
85   loaded = TRUE;
86 }
87
88 static void
89 dynamic_object_class_finalize (DynamicObjectClass *class)
90 {
91   loaded = FALSE;
92 }
93
94 static void
95 dynamic_object_iface_init (TestIface *iface)
96 {
97 }
98
99 static void
100 dynamic_object_init (DynamicObject *dynamic_object)
101 {
102 }
103
104 static void
105 module_register (GTypeModule *module)
106 {
107   dynamic_object_register_type (module);
108 }
109
110 static void
111 test_dynamic_type (void)
112 {
113   DynamicObjectClass *class;
114
115   test_module_new (module_register);
116
117   /* Not loaded until we call ref for the first time */
118   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
119   g_assert (class == NULL);
120   g_assert (!loaded);
121
122   /* Make sure interfaces work */
123   g_assert (g_type_is_a (DYNAMIC_OBJECT_TYPE,
124                          TEST_TYPE_IFACE));
125
126   /* Ref loads */
127   class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
128   g_assert (class && class->val == 42);
129   g_assert (loaded);
130
131   /* Peek then works */
132   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
133   g_assert (class && class->val == 42);
134   g_assert (loaded);
135
136   /* Make sure interfaces still work */
137   g_assert (g_type_is_a (DYNAMIC_OBJECT_TYPE,
138                          TEST_TYPE_IFACE));
139
140   /* Unref causes finalize */
141   g_type_class_unref (class);
142
143   /* Peek returns NULL */
144   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
145 #if 0
146   g_assert (!class);
147   g_assert (!loaded);
148 #endif
149   
150   /* Ref reloads */
151   class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
152   g_assert (class && class->val == 42);
153   g_assert (loaded);
154
155   /* And Unref causes finalize once more*/
156   g_type_class_unref (class);
157   class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
158 #if 0
159   g_assert (!class);
160   g_assert (!loaded);
161 #endif
162 }
163
164 int
165 main (int   argc,
166       char *argv[])
167 {
168   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
169                           G_LOG_LEVEL_WARNING |
170                           G_LOG_LEVEL_CRITICAL);
171
172   test_dynamic_type ();
173   
174   return 0;
175 }