2006-12-29 07:12:11 +01:00
|
|
|
/* GObject - GLib Type, Object, Parameter and Signal Library
|
|
|
|
* Copyright (C) 2001, 2003 Red Hat, Inc.
|
|
|
|
*
|
2022-06-01 13:17:28 +02:00
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
*
|
2006-12-29 07:12:11 +01:00
|
|
|
* 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
|
2017-05-28 14:09:39 +02:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-12-29 07:12:11 +01:00
|
|
|
*
|
|
|
|
* 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
|
2014-01-23 12:58:29 +01:00
|
|
|
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2006-12-29 07:12:11 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
#include "testcommon.h"
|
|
|
|
#include "testmodule.h"
|
|
|
|
|
2022-03-13 17:44:45 +01:00
|
|
|
/* This test tests the macros for defining dynamic types */
|
2006-12-29 07:12:11 +01:00
|
|
|
|
|
|
|
static gboolean loaded = FALSE;
|
|
|
|
|
2009-11-26 11:54:44 +01:00
|
|
|
struct _TestIfaceClass
|
|
|
|
{
|
|
|
|
GTypeInterface base_iface;
|
|
|
|
guint val;
|
|
|
|
};
|
|
|
|
|
2012-11-02 16:19:20 +01:00
|
|
|
static GType test_iface_get_type (void);
|
2022-03-13 17:44:45 +01:00
|
|
|
|
2009-11-26 11:54:44 +01:00
|
|
|
#define TEST_TYPE_IFACE (test_iface_get_type ())
|
2022-03-13 17:44:45 +01:00
|
|
|
#define TEST_IFACE_GET_CLASS(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_INTERFACE ((obj), TEST_TYPE_IFACE, TestIfaceClass))
|
|
|
|
|
2009-11-26 11:54:44 +01:00
|
|
|
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,
|
2022-03-13 17:44:45 +01:00
|
|
|
gpointer class_data)
|
2009-11-26 11:54:44 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_iface_base_init (TestIfaceClass *iface)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-02 16:19:20 +01:00
|
|
|
GType dynamic_object_get_type (void);
|
2006-12-29 07:12:11 +01:00
|
|
|
#define DYNAMIC_OBJECT_TYPE (dynamic_object_get_type ())
|
|
|
|
|
|
|
|
typedef GObject DynamicObject;
|
|
|
|
typedef struct _DynamicObjectClass DynamicObjectClass;
|
|
|
|
|
|
|
|
struct _DynamicObjectClass
|
|
|
|
{
|
|
|
|
GObjectClass parent_class;
|
|
|
|
guint val;
|
|
|
|
};
|
|
|
|
|
2009-11-30 03:19:10 +01:00
|
|
|
static void dynamic_object_iface_init (TestIface *iface);
|
2009-11-26 11:54:44 +01:00
|
|
|
|
|
|
|
G_DEFINE_DYNAMIC_TYPE_EXTENDED(DynamicObject, dynamic_object, G_TYPE_OBJECT, 0,
|
2022-03-13 17:44:45 +01:00
|
|
|
G_IMPLEMENT_INTERFACE_DYNAMIC (TEST_TYPE_IFACE,
|
|
|
|
dynamic_object_iface_init));
|
2006-12-29 07:12:11 +01:00
|
|
|
|
2022-03-13 17:44:45 +01:00
|
|
|
static void
|
2006-12-29 07:12:11 +01:00
|
|
|
dynamic_object_class_init (DynamicObjectClass *class)
|
|
|
|
{
|
|
|
|
class->val = 42;
|
|
|
|
loaded = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dynamic_object_class_finalize (DynamicObjectClass *class)
|
|
|
|
{
|
|
|
|
loaded = FALSE;
|
|
|
|
}
|
|
|
|
|
2009-11-26 11:54:44 +01:00
|
|
|
static void
|
2009-11-26 12:30:14 +01:00
|
|
|
dynamic_object_iface_init (TestIface *iface)
|
2006-12-29 07:12:11 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-11-26 11:54:44 +01:00
|
|
|
static void
|
|
|
|
dynamic_object_init (DynamicObject *dynamic_object)
|
|
|
|
{
|
|
|
|
}
|
2006-12-29 07:12:11 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
module_register (GTypeModule *module)
|
|
|
|
{
|
|
|
|
dynamic_object_register_type (module);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_dynamic_type (void)
|
|
|
|
{
|
|
|
|
DynamicObjectClass *class;
|
|
|
|
|
|
|
|
/* Not loaded until we call ref for the first time */
|
|
|
|
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
|
2022-03-13 17:44:45 +01:00
|
|
|
g_assert_null (class);
|
|
|
|
g_assert_false (loaded);
|
2006-12-29 07:12:11 +01:00
|
|
|
|
2009-11-26 11:54:44 +01:00
|
|
|
/* Make sure interfaces work */
|
2022-03-13 17:44:45 +01:00
|
|
|
g_assert_true (g_type_is_a (DYNAMIC_OBJECT_TYPE,
|
|
|
|
TEST_TYPE_IFACE));
|
2009-11-26 11:54:44 +01:00
|
|
|
|
2006-12-29 07:12:11 +01:00
|
|
|
/* Ref loads */
|
|
|
|
class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
|
2022-03-13 17:44:45 +01:00
|
|
|
g_assert_nonnull (class);
|
|
|
|
g_assert_cmpint (class->val, ==, 42);
|
|
|
|
g_assert_true (loaded);
|
2006-12-29 07:12:11 +01:00
|
|
|
|
|
|
|
/* Peek then works */
|
|
|
|
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
|
2022-03-13 17:44:45 +01:00
|
|
|
g_assert_nonnull (class);
|
|
|
|
g_assert_cmpint (class->val, ==, 42);
|
|
|
|
g_assert_true (loaded);
|
2009-11-26 11:54:44 +01:00
|
|
|
|
|
|
|
/* Make sure interfaces still work */
|
2022-03-13 17:44:45 +01:00
|
|
|
g_assert_true (g_type_is_a (DYNAMIC_OBJECT_TYPE,
|
|
|
|
TEST_TYPE_IFACE));
|
2009-11-26 11:54:44 +01:00
|
|
|
|
2006-12-29 07:12:11 +01:00
|
|
|
/* Unref causes finalize */
|
|
|
|
g_type_class_unref (class);
|
|
|
|
|
|
|
|
/* Peek returns NULL */
|
|
|
|
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
|
2013-02-07 20:05:28 +01:00
|
|
|
#if 0
|
2022-03-13 17:44:45 +01:00
|
|
|
/* Disabled as unloading dynamic types is disabled.
|
|
|
|
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
|
|
|
|
g_assert_false (class);
|
|
|
|
g_assert_false (loaded);
|
2013-02-07 20:05:28 +01:00
|
|
|
#endif
|
2022-03-13 17:44:45 +01:00
|
|
|
|
2006-12-29 07:12:11 +01:00
|
|
|
/* Ref reloads */
|
|
|
|
class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
|
2022-03-13 17:44:45 +01:00
|
|
|
g_assert_nonnull (class);
|
|
|
|
g_assert_cmpint (class->val, ==, 42);
|
|
|
|
g_assert_true (loaded);
|
2006-12-29 07:12:11 +01:00
|
|
|
|
|
|
|
/* And Unref causes finalize once more*/
|
|
|
|
g_type_class_unref (class);
|
|
|
|
class = g_type_class_peek (DYNAMIC_OBJECT_TYPE);
|
2013-02-07 20:05:28 +01:00
|
|
|
#if 0
|
2022-03-13 17:44:45 +01:00
|
|
|
/* Disabled as unloading dynamic types is disabled.
|
|
|
|
* See https://gitlab.gnome.org/GNOME/glib/-/issues/667 */
|
|
|
|
g_assert_null (class);
|
|
|
|
g_assert_false (loaded);
|
2013-02-07 20:05:28 +01:00
|
|
|
#endif
|
2006-12-29 07:12:11 +01:00
|
|
|
}
|
|
|
|
|
2023-07-30 10:25:42 +02:00
|
|
|
static void
|
|
|
|
test_dynamic_type_query (void)
|
|
|
|
{
|
|
|
|
DynamicObjectClass *class;
|
|
|
|
GTypeQuery query_result;
|
|
|
|
|
|
|
|
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/623");
|
|
|
|
|
|
|
|
class = g_type_class_ref (DYNAMIC_OBJECT_TYPE);
|
|
|
|
g_assert_nonnull (class);
|
|
|
|
|
|
|
|
g_type_query (DYNAMIC_OBJECT_TYPE, &query_result);
|
|
|
|
|
|
|
|
g_assert_cmpuint (query_result.type, !=, 0);
|
|
|
|
g_assert_cmpstr (query_result.type_name, ==, "DynamicObject");
|
|
|
|
g_assert_cmpuint (query_result.class_size, >=, sizeof (DynamicObjectClass));
|
|
|
|
g_assert_cmpuint (query_result.instance_size, >=, sizeof (DynamicObject));
|
|
|
|
|
|
|
|
g_type_class_unref (class);
|
|
|
|
}
|
|
|
|
|
2006-12-29 07:12:11 +01:00
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
|
|
|
g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
|
2022-03-13 17:44:45 +01:00
|
|
|
G_LOG_LEVEL_WARNING |
|
|
|
|
G_LOG_LEVEL_CRITICAL);
|
|
|
|
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
2023-07-30 10:25:42 +02:00
|
|
|
test_module_new (module_register);
|
|
|
|
|
2022-03-13 17:44:45 +01:00
|
|
|
g_test_add_func ("/gobject/dynamic-type", test_dynamic_type);
|
2023-07-30 10:25:42 +02:00
|
|
|
g_test_add_func ("/gobject/dynamic-type/query", test_dynamic_type_query);
|
2006-12-29 07:12:11 +01:00
|
|
|
|
2022-03-13 17:44:45 +01:00
|
|
|
return g_test_run ();
|
2006-12-29 07:12:11 +01:00
|
|
|
}
|