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
|
/* GObject - GLib Type, Object, Parameter and Signal Library
* Copyright (C) 2001, 2003 Red Hat, Inc.
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* 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.1 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, see <http://www.gnu.org/licenses/>.
*/
#include <glib-object.h>
#include "testcommon.h"
#include "testmodule.h"
/* This test tests the macros for defining dynamic types */
static gboolean loaded = FALSE;
struct _TestIfaceClass
{
GTypeInterface base_iface;
guint val;
};
static GType test_iface_get_type (void);
#define TEST_TYPE_IFACE (test_iface_get_type ())
#define TEST_IFACE_GET_CLASS(obj) \
(G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
typedef struct _TestIface TestIface;
typedef struct _TestIfaceClass TestIfaceClass;
static void test_iface_base_init (TestIfaceClass *iface);
static void test_iface_default_init (TestIfaceClass *iface, gpointer class_data);
static DEFINE_IFACE(TestIface, test_iface, test_iface_base_init, test_iface_default_init)
static void
test_iface_default_init (TestIfaceClass *iface,
gpointer class_data)
{
}
static void
test_iface_base_init (TestIfaceClass *iface)
{
}
GType dynamic_object_get_type (void);
#define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
typedef GObject DynamicObject;
typedef struct _DynamicObjectClass DynamicObjectClass;
struct _DynamicObjectClass
{
GObjectClass parent_class;
guint val;
};
static void dynamic_object_iface_init (TestIface *iface);
G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynamicObject, dynamic_object, G_TYPE_OBJECT, 0,
G_IMPLEMENT_INTERFACE_DYNAMIC (TEST_TYPE_IFACE,
dynamic_object_iface_init));
static void
dynamic_object_class_init (DynamicObjectClass *class)
{
class->val = 42;
loaded = TRUE;
}
static void
dynamic_object_class_finalize (DynamicObjectClass *class)
{
loaded = FALSE;
}
static void
dynamic_object_iface_init (TestIface *iface)
{
}
static void
dynamic_object_init (DynamicObject *dynamic_object)
{
}
static void
module_register (GTypeModule *module)
{
dynamic_object_register_type (module);
}
static void
test_dynamic_type (void)
{
DynamicObjectClass *class;
test_module_new (module_register);
/* Not loaded until we call ref for the first time */
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
g_assert_null (class);
g_assert_false (loaded);
/* Make sure interfaces work */
g_assert_true (g_type_is_a (DYNAMIC_OBJECT_TYPE,
TEST_TYPE_IFACE));
/* Ref loads */
class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
g_assert_nonnull (class);
g_assert_cmpint (class->val, ==, 42);
g_assert_true (loaded);
/* Peek then works */
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
g_assert_nonnull (class);
g_assert_cmpint (class->val, ==, 42);
g_assert_true (loaded);
/* Make sure interfaces still work */
g_assert_true (g_type_is_a (DYNAMIC_OBJECT_TYPE,
TEST_TYPE_IFACE));
/* Unref causes finalize */
g_type_class_unref (class);
/* Peek returns NULL */
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
#if 0
/* Disabled as unloading dynamic types is disabled.
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
g_assert_false (class);
g_assert_false (loaded);
#endif
/* Ref reloads */
class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
g_assert_nonnull (class);
g_assert_cmpint (class->val, ==, 42);
g_assert_true (loaded);
/* And Unref causes finalize once more*/
g_type_class_unref (class);
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
#if 0
/* Disabled as unloading dynamic types is disabled.
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
g_assert_null (class);
g_assert_false (loaded);
#endif
}
int
main (int argc,
char *argv[])
{
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
G_LOG_LEVEL_WARNING |
G_LOG_LEVEL_CRITICAL);
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/gobject/dynamic-type", test_dynamic_type);
return g_test_run ();
}
|