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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
21 #define G_LOG_DOMAIN "TestDynamicType"
23 #undef G_DISABLE_ASSERT
24 #undef G_DISABLE_CHECKS
25 #undef G_DISABLE_CAST_CHECKS
27 #include <glib-object.h>
29 #include "testcommon.h"
30 #include "testmodule.h"
32 /* This test tests the macros for defining dynamic types.
35 static gboolean loaded = FALSE;
37 struct _TestIfaceClass
39 GTypeInterface base_iface;
43 #define TEST_TYPE_IFACE (test_iface_get_type ())
44 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
45 typedef struct _TestIface TestIface;
46 typedef struct _TestIfaceClass TestIfaceClass;
48 static void test_iface_base_init (TestIfaceClass *iface);
49 static void test_iface_default_init (TestIfaceClass *iface, gpointer class_data);
51 static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, test_iface_default_init)
54 test_iface_default_init (TestIfaceClass *iface,
60 test_iface_base_init (TestIfaceClass *iface)
64 #define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
66 typedef GObject DynamicObject;
67 typedef struct _DynamicObjectClass DynamicObjectClass;
69 struct _DynamicObjectClass
71 GObjectClass parent_class;
75 static void dynamic_object_iface_init (gpointer g_iface,
78 G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynamicObject, dynamic_object, G_TYPE_OBJECT, 0,
79 G_IMPLEMENT_INTERFACE_DYNAMIC (TEST_TYPE_IFACE,
80 dynamic_object_iface_init));
83 dynamic_object_class_init (DynamicObjectClass *class)
90 dynamic_object_class_finalize (DynamicObjectClass *class)
96 dynamic_object_iface_init (gpointer g_iface,
102 dynamic_object_init (DynamicObject *dynamic_object)
107 module_register (GTypeModule *module)
109 dynamic_object_register_type (module);
113 test_dynamic_type (void)
116 DynamicObjectClass *class;
118 module = test_module_new (module_register);
120 /* Not loaded until we call ref for the first time */
121 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
122 g_assert (class == NULL);
125 /* Make sure interfaces work */
126 g_assert (g_type_is_a (DYNAMIC_OBJECT_TYPE,
130 class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
131 g_assert (class && class->val == 42);
134 /* Peek then works */
135 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
136 g_assert (class && class->val == 42);
139 /* Make sure interfaces still work */
140 g_assert (g_type_is_a (DYNAMIC_OBJECT_TYPE,
143 /* Unref causes finalize */
144 g_type_class_unref (class);
146 /* Peek returns NULL */
147 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
152 class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
153 g_assert (class && class->val == 42);
156 /* And Unref causes finalize once more*/
157 g_type_class_unref (class);
158 class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
167 g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
168 G_LOG_LEVEL_WARNING |
169 G_LOG_LEVEL_CRITICAL);
172 test_dynamic_type ();