diff options
author | Tim Janik <timj@gtk.org> | 2006-08-16 12:11:03 +0000 |
---|---|---|
committer | Tim Janik <timj@src.gnome.org> | 2006-08-16 12:11:03 +0000 |
commit | aa4c9326300f5ac0b8ee04e124013ba649ec152c (patch) | |
tree | 3bc5918927ac9e2b8ea25dae25e974c511875f40 | |
parent | f1f0177b9e8da7d44c1dc4be9b27ce174af181d8 (diff) |
added test program which demonstrates and checks singleton construction.
Wed Aug 16 13:59:07 2006 Tim Janik <timj@gtk.org>
* tests/gobject/Makefile.am:
* tests/gobject/singleton.c: added test program which demonstrates
and checks singleton construction.
-rw-r--r-- | ChangeLog | 6 | ||||
-rw-r--r-- | ChangeLog.pre-2-12 | 6 | ||||
-rw-r--r-- | tests/gobject/Makefile.am | 1 | ||||
-rw-r--r-- | tests/gobject/singleton.c | 85 |
4 files changed, 98 insertions, 0 deletions
@@ -1,3 +1,9 @@ +Wed Aug 16 13:59:07 2006 Tim Janik <timj@gtk.org> + + * tests/gobject/Makefile.am: + * tests/gobject/singleton.c: added test program which demonstrates + and checks singleton construction. + 2006-08-15 Matthias Clasen <mclasen@redhat.com> * glib/gbookmarkfile.c (g_bookmark_file_get_groups): diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 6ff472c20..cdd11954c 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,9 @@ +Wed Aug 16 13:59:07 2006 Tim Janik <timj@gtk.org> + + * tests/gobject/Makefile.am: + * tests/gobject/singleton.c: added test program which demonstrates + and checks singleton construction. + 2006-08-15 Matthias Clasen <mclasen@redhat.com> * glib/gbookmarkfile.c (g_bookmark_file_get_groups): diff --git a/tests/gobject/Makefile.am b/tests/gobject/Makefile.am index 5d7e01bea..09be33952 100644 --- a/tests/gobject/Makefile.am +++ b/tests/gobject/Makefile.am @@ -56,6 +56,7 @@ test_programs = \ ifaceinherit \ ifaceproperties \ override \ + singleton \ references check_PROGRAMS = $(test_programs) diff --git a/tests/gobject/singleton.c b/tests/gobject/singleton.c new file mode 100644 index 000000000..c7609a646 --- /dev/null +++ b/tests/gobject/singleton.c @@ -0,0 +1,85 @@ +/* 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 + * version 2 of the License, or (at your option) any later version. + * + * 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 + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + */ +#undef G_LOG_DOMAIN +#define G_LOG_DOMAIN "TestSingleton" +#include <glib-object.h> +#include <string.h> + +/* --- MySingleton class --- */ +typedef struct { + GObject parent_instance; +} MySingleton; +typedef struct { + GObjectClass parent_class; +} MySingletonClass; + +#define MY_TYPE_SINGLETON (my_singleton_get_type ()) +#define MY_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton)) +#define MY_IS_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON)) +#define MY_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass)) +#define MY_IS_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON)) +#define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass)) + +G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT); + +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) + return g_object_ref (the_one_and_only); + else + return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties); +} + +static void +my_singleton_init (MySingleton *self) +{ + g_assert (the_one_and_only == NULL); + the_one_and_only = self; +} + +static void +my_singleton_class_init (MySingletonClass *klass) +{ + G_OBJECT_CLASS (klass)->constructor = my_singleton_constructor; +} + +/* --- test program --- */ +int +main (int argc, + char *argv[]) +{ + MySingleton *singleton, *obj; + g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS); + /* create the singleton */ + singleton = g_object_new (MY_TYPE_SINGLETON, NULL); + g_assert (singleton != NULL); + /* assert _singleton_ creation */ + obj = g_object_new (MY_TYPE_SINGLETON, NULL); + g_assert (singleton == obj); + g_object_unref (obj); + /* shutdown */ + g_object_unref (singleton); + return 0; +} |