2006-08-16 14:11:03 +02:00
|
|
|
/* GObject - GLib Type, Object, Parameter and Signal Library
|
|
|
|
* Copyright (C) 2006 Imendio AB
|
|
|
|
*
|
|
|
|
* 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-08-16 14:11:03 +02: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-08-16 14:11:03 +02:00
|
|
|
*/
|
2022-03-13 17:46:48 +01:00
|
|
|
|
2006-08-16 14:11:03 +02:00
|
|
|
#include <glib-object.h>
|
|
|
|
|
|
|
|
/* --- MySingleton class --- */
|
|
|
|
|
2022-06-10 14:52:37 +02:00
|
|
|
struct _MySingleton {
|
|
|
|
GObject parent_instance;
|
2022-06-09 17:34:39 +02:00
|
|
|
int myint;
|
2022-06-10 14:52:37 +02:00
|
|
|
};
|
2006-08-16 14:11:03 +02:00
|
|
|
|
2022-06-10 14:52:37 +02:00
|
|
|
#define MY_TYPE_SINGLETON my_singleton_get_type ()
|
|
|
|
G_DECLARE_FINAL_TYPE (MySingleton, my_singleton, MY, SINGLETON, GObject)
|
|
|
|
G_DEFINE_FINAL_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT)
|
2006-08-16 14:11:03 +02:00
|
|
|
|
|
|
|
static MySingleton *the_one_and_only = NULL;
|
|
|
|
|
|
|
|
/* --- methods --- */
|
|
|
|
static GObject*
|
|
|
|
my_singleton_constructor (GType type,
|
|
|
|
guint n_construct_properties,
|
|
|
|
GObjectConstructParam *construct_properties)
|
|
|
|
{
|
|
|
|
if (the_one_and_only)
|
2017-11-22 09:00:39 +01:00
|
|
|
return g_object_ref (G_OBJECT (the_one_and_only));
|
2006-08-16 14:11:03 +02:00
|
|
|
else
|
|
|
|
return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
|
|
|
|
}
|
|
|
|
|
2022-06-09 17:34:39 +02:00
|
|
|
static void
|
|
|
|
my_singleton_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
g_assert ((GObject *) the_one_and_only == object);
|
|
|
|
the_one_and_only = NULL;
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (my_singleton_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2006-08-16 14:11:03 +02:00
|
|
|
static void
|
|
|
|
my_singleton_init (MySingleton *self)
|
|
|
|
{
|
2022-03-13 17:46:48 +01:00
|
|
|
g_assert_null (the_one_and_only);
|
2006-08-16 14:11:03 +02:00
|
|
|
the_one_and_only = self;
|
|
|
|
}
|
|
|
|
|
2022-06-09 17:34:39 +02:00
|
|
|
static void
|
|
|
|
my_singleton_set_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
MySingleton *self = (MySingleton *) gobject;
|
|
|
|
|
|
|
|
g_assert (prop_id == 1);
|
|
|
|
|
|
|
|
self->myint = g_value_get_int (value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_singleton_get_property (GObject *gobject,
|
|
|
|
guint prop_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
MySingleton *self = (MySingleton *) gobject;
|
|
|
|
|
|
|
|
g_assert (prop_id == 1);
|
|
|
|
|
|
|
|
g_value_set_int (value, self->myint);
|
|
|
|
}
|
|
|
|
|
2006-08-16 14:11:03 +02:00
|
|
|
static void
|
|
|
|
my_singleton_class_init (MySingletonClass *klass)
|
|
|
|
{
|
2022-06-09 17:34:39 +02:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->constructor = my_singleton_constructor;
|
|
|
|
object_class->finalize = my_singleton_finalize;
|
|
|
|
object_class->set_property = my_singleton_set_property;
|
|
|
|
object_class->get_property = my_singleton_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (G_OBJECT_CLASS (klass), 1,
|
|
|
|
g_param_spec_int ("foo", NULL, NULL,
|
|
|
|
0, G_MAXINT, 0,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
|
2006-08-16 14:11:03 +02:00
|
|
|
}
|
|
|
|
|
2022-06-10 14:48:40 +02:00
|
|
|
static void
|
|
|
|
test_singleton_construction (void)
|
2006-08-16 14:11:03 +02:00
|
|
|
{
|
|
|
|
MySingleton *singleton, *obj;
|
2012-10-15 18:00:51 +02:00
|
|
|
|
2006-08-16 14:11:03 +02:00
|
|
|
/* create the singleton */
|
|
|
|
singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
|
2022-03-13 17:46:48 +01:00
|
|
|
g_assert_nonnull (singleton);
|
|
|
|
|
2006-08-16 14:11:03 +02:00
|
|
|
/* assert _singleton_ creation */
|
|
|
|
obj = g_object_new (MY_TYPE_SINGLETON, NULL);
|
2022-03-13 17:46:48 +01:00
|
|
|
g_assert_true (singleton == obj);
|
2006-08-16 14:11:03 +02:00
|
|
|
g_object_unref (obj);
|
2022-03-13 17:46:48 +01:00
|
|
|
|
2006-08-16 14:11:03 +02:00
|
|
|
/* shutdown */
|
|
|
|
g_object_unref (singleton);
|
2022-06-10 14:48:40 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 17:34:39 +02:00
|
|
|
static void
|
|
|
|
test_singleton_construct_property (void)
|
|
|
|
{
|
|
|
|
MySingleton *singleton;
|
|
|
|
|
|
|
|
g_test_summary ("Test that creating a singleton with a construct-time property works");
|
|
|
|
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2666");
|
|
|
|
|
|
|
|
/* create the singleton and set a property at construction time */
|
|
|
|
singleton = g_object_new (MY_TYPE_SINGLETON, "foo", 1, NULL);
|
|
|
|
g_assert_nonnull (singleton);
|
|
|
|
|
|
|
|
/* shutdown */
|
|
|
|
g_object_unref (singleton);
|
|
|
|
}
|
|
|
|
|
2022-06-10 14:48:40 +02:00
|
|
|
int
|
|
|
|
main (int argc,
|
|
|
|
char *argv[])
|
|
|
|
{
|
|
|
|
g_test_init (&argc, &argv, NULL);
|
|
|
|
|
|
|
|
g_test_add_func ("/gobject/singleton/construction", test_singleton_construction);
|
2022-06-09 17:34:39 +02:00
|
|
|
g_test_add_func ("/gobject/singleton/construct-property", test_singleton_construct_property);
|
2022-06-10 14:48:40 +02:00
|
|
|
|
|
|
|
return g_test_run ();
|
2006-08-16 14:11:03 +02:00
|
|
|
}
|