tizen: kdbus: make i explicitly positive in make_single_header_vector
[platform/upstream/glib.git] / gobject / tests / singleton.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2006 Imendio AB
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <glib-object.h>
21
22 /* --- MySingleton class --- */
23
24 struct _MySingleton {
25   GObject parent_instance;
26   int myint;
27 };
28
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)
32
33 static MySingleton *the_one_and_only = NULL;
34
35 /* --- methods --- */
36 static GObject*
37 my_singleton_constructor (GType                  type,
38                           guint                  n_construct_properties,
39                           GObjectConstructParam *construct_properties)
40 {
41   if (the_one_and_only)
42     return g_object_ref (G_OBJECT (the_one_and_only));
43   else
44     return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
45 }
46
47 static void
48 my_singleton_finalize (GObject *object)
49 {
50   g_assert ((GObject *) the_one_and_only == object);
51   the_one_and_only = NULL;
52
53   G_OBJECT_CLASS (my_singleton_parent_class)->finalize (object);
54 }
55
56 static void
57 my_singleton_init (MySingleton *self)
58 {
59   g_assert_null (the_one_and_only);
60   the_one_and_only = self;
61 }
62
63 static void
64 my_singleton_set_property (GObject      *gobject,
65                            guint         prop_id,
66                            const GValue *value,
67                            GParamSpec   *pspec)
68 {
69   MySingleton *self = (MySingleton *) gobject;
70
71   g_assert (prop_id == 1);
72
73   self->myint = g_value_get_int (value);
74 }
75
76 static void
77 my_singleton_get_property (GObject    *gobject,
78                            guint       prop_id,
79                            GValue     *value,
80                            GParamSpec *pspec)
81 {
82   MySingleton *self = (MySingleton *) gobject;
83
84   g_assert (prop_id == 1);
85
86   g_value_set_int (value, self->myint);
87 }
88
89 static void
90 my_singleton_class_init (MySingletonClass *klass)
91 {
92   GObjectClass *object_class = G_OBJECT_CLASS (klass);
93
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;
98
99   g_object_class_install_property (G_OBJECT_CLASS (klass), 1,
100                                    g_param_spec_int ("foo", NULL, NULL,
101                                                      0, G_MAXINT, 0,
102                                                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
103 }
104
105 static void
106 test_singleton_construction (void)
107 {
108   MySingleton *singleton, *obj;
109
110   /* create the singleton */
111   singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
112   g_assert_nonnull (singleton);
113
114   /* assert _singleton_ creation */
115   obj = g_object_new (MY_TYPE_SINGLETON, NULL);
116   g_assert_true (singleton == obj);
117   g_object_unref (obj);
118
119   /* shutdown */
120   g_object_unref (singleton);
121 }
122
123 static void
124 test_singleton_construct_property (void)
125 {
126   MySingleton *singleton;
127
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");
130
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);
134
135   /* shutdown */
136   g_object_unref (singleton);
137 }
138
139 int
140 main (int   argc,
141       char *argv[])
142 {
143   g_test_init (&argc, &argv, NULL);
144
145   g_test_add_func ("/gobject/singleton/construction", test_singleton_construction);
146   g_test_add_func ("/gobject/singleton/construct-property", test_singleton_construct_property);
147
148   return g_test_run ();
149 }