1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2006 Imendio AB
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include <glib-object.h>
22 /* --- MySingleton class --- */
25 GObject parent_instance;
29 #define MY_TYPE_SINGLETON my_singleton_get_type ()
30 G_DECLARE_FINAL_TYPE (MySingleton, my_singleton, MY, SINGLETON, GObject)
31 G_DEFINE_FINAL_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT)
33 static MySingleton *the_one_and_only = NULL;
37 my_singleton_constructor (GType type,
38 guint n_construct_properties,
39 GObjectConstructParam *construct_properties)
42 return g_object_ref (G_OBJECT (the_one_and_only));
44 return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
48 my_singleton_finalize (GObject *object)
50 g_assert ((GObject *) the_one_and_only == object);
51 the_one_and_only = NULL;
53 G_OBJECT_CLASS (my_singleton_parent_class)->finalize (object);
57 my_singleton_init (MySingleton *self)
59 g_assert_null (the_one_and_only);
60 the_one_and_only = self;
64 my_singleton_set_property (GObject *gobject,
69 MySingleton *self = (MySingleton *) gobject;
71 g_assert (prop_id == 1);
73 self->myint = g_value_get_int (value);
77 my_singleton_get_property (GObject *gobject,
82 MySingleton *self = (MySingleton *) gobject;
84 g_assert (prop_id == 1);
86 g_value_set_int (value, self->myint);
90 my_singleton_class_init (MySingletonClass *klass)
92 GObjectClass *object_class = G_OBJECT_CLASS (klass);
94 object_class->constructor = my_singleton_constructor;
95 object_class->finalize = my_singleton_finalize;
96 object_class->set_property = my_singleton_set_property;
97 object_class->get_property = my_singleton_get_property;
99 g_object_class_install_property (G_OBJECT_CLASS (klass), 1,
100 g_param_spec_int ("foo", NULL, NULL,
102 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
106 test_singleton_construction (void)
108 MySingleton *singleton, *obj;
110 /* create the singleton */
111 singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
112 g_assert_nonnull (singleton);
114 /* assert _singleton_ creation */
115 obj = g_object_new (MY_TYPE_SINGLETON, NULL);
116 g_assert_true (singleton == obj);
117 g_object_unref (obj);
120 g_object_unref (singleton);
124 test_singleton_construct_property (void)
126 MySingleton *singleton;
128 g_test_summary ("Test that creating a singleton with a construct-time property works");
129 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2666");
131 /* create the singleton and set a property at construction time */
132 singleton = g_object_new (MY_TYPE_SINGLETON, "foo", 1, NULL);
133 g_assert_nonnull (singleton);
136 g_object_unref (singleton);
143 g_test_init (&argc, &argv, NULL);
145 g_test_add_func ("/gobject/singleton/construction", test_singleton_construction);
146 g_test_add_func ("/gobject/singleton/construct-property", test_singleton_construct_property);
148 return g_test_run ();