signals: Fix leak in test
[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 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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #undef  G_LOG_DOMAIN
20 #define G_LOG_DOMAIN "TestSingleton"
21 #include <glib-object.h>
22 #include <string.h>
23
24 /* --- MySingleton class --- */
25 typedef struct {
26   GObject parent_instance;
27 } MySingleton;
28 typedef struct {
29   GObjectClass parent_class;
30 } MySingletonClass;
31
32 static GType my_singleton_get_type (void);
33 #define MY_TYPE_SINGLETON         (my_singleton_get_type ())
34 #define MY_SINGLETON(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton))
35 #define MY_IS_SINGLETON(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON))
36 #define MY_SINGLETON_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass))
37 #define MY_IS_SINGLETON_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON))
38 #define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass))
39
40 G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT);
41
42 static MySingleton *the_one_and_only = NULL;
43
44 /* --- methods --- */
45 static GObject*
46 my_singleton_constructor (GType                  type,
47                           guint                  n_construct_properties,
48                           GObjectConstructParam *construct_properties)
49 {
50   if (the_one_and_only)
51     return g_object_ref (the_one_and_only);
52   else
53     return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
54 }
55
56 static void
57 my_singleton_init (MySingleton *self)
58 {
59   g_assert (the_one_and_only == NULL);
60   the_one_and_only = self;
61 }
62
63 static void
64 my_singleton_class_init (MySingletonClass *klass)
65 {
66   G_OBJECT_CLASS (klass)->constructor = my_singleton_constructor;
67 }
68
69 /* --- test program --- */
70 int
71 main (int   argc,
72       char *argv[])
73 {
74   MySingleton *singleton, *obj;
75
76   /* create the singleton */
77   singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
78   g_assert (singleton != NULL);
79   /* assert _singleton_ creation */
80   obj = g_object_new (MY_TYPE_SINGLETON, NULL);
81   g_assert (singleton == obj);
82   g_object_unref (obj);
83   /* shutdown */
84   g_object_unref (singleton);
85   return 0;
86 }