Imported Upstream version 2.55.1
[platform/upstream/glib.git] / tests / gobject / singleton.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2006 Imendio AB
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 #undef  G_LOG_DOMAIN
18 #define G_LOG_DOMAIN "TestSingleton"
19 #include <glib-object.h>
20 #include <string.h>
21
22 /* --- MySingleton class --- */
23 typedef struct {
24   GObject parent_instance;
25 } MySingleton;
26 typedef struct {
27   GObjectClass parent_class;
28 } MySingletonClass;
29
30 static GType my_singleton_get_type (void);
31 #define MY_TYPE_SINGLETON         (my_singleton_get_type ())
32 #define MY_SINGLETON(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton))
33 #define MY_IS_SINGLETON(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON))
34 #define MY_SINGLETON_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass))
35 #define MY_IS_SINGLETON_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON))
36 #define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass))
37
38 G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT)
39
40 static MySingleton *the_one_and_only = NULL;
41
42 /* --- methods --- */
43 static GObject*
44 my_singleton_constructor (GType                  type,
45                           guint                  n_construct_properties,
46                           GObjectConstructParam *construct_properties)
47 {
48   if (the_one_and_only)
49     return g_object_ref (G_OBJECT (the_one_and_only));
50   else
51     return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
52 }
53
54 static void
55 my_singleton_init (MySingleton *self)
56 {
57   g_assert (the_one_and_only == NULL);
58   the_one_and_only = self;
59 }
60
61 static void
62 my_singleton_class_init (MySingletonClass *klass)
63 {
64   G_OBJECT_CLASS (klass)->constructor = my_singleton_constructor;
65 }
66
67 /* --- test program --- */
68 int
69 main (int   argc,
70       char *argv[])
71 {
72   MySingleton *singleton, *obj;
73
74   /* create the singleton */
75   singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
76   g_assert (singleton != NULL);
77   /* assert _singleton_ creation */
78   obj = g_object_new (MY_TYPE_SINGLETON, NULL);
79   g_assert (singleton == obj);
80   g_object_unref (obj);
81   /* shutdown */
82   g_object_unref (singleton);
83   return 0;
84 }