sl@0: /* GObject - GLib Type, Object, Parameter and Signal Library sl@0: * Copyright (C) 2006 Imendio AB sl@0: * Portions copyright (c) 2009 Nokia Corporation. All rights reserved. sl@0: * This library is free software; you can redistribute it and/or sl@0: * modify it under the terms of the GNU Lesser General Public sl@0: * License as published by the Free Software Foundation; either sl@0: * version 2 of the License, or (at your option) any later version. sl@0: * sl@0: * This library is distributed in the hope that it will be useful, sl@0: * but WITHOUT ANY WARRANTY; without even the implied warranty of sl@0: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU sl@0: * Lesser General Public License for more details. sl@0: * sl@0: * You should have received a copy of the GNU Lesser General sl@0: * Public License along with this library; if not, write to the sl@0: * Free Software Foundation, Inc., 59 Temple Place, Suite 330, sl@0: * Boston, MA 02111-1307, USA. sl@0: */ sl@0: #undef G_LOG_DOMAIN sl@0: #define G_LOG_DOMAIN "TestSingleton" sl@0: #include sl@0: #include sl@0: #ifdef __SYMBIAN32__ sl@0: #include "mrt2_glib2_test.h" sl@0: #endif //__SYMBIAN32__ sl@0: /* --- MySingleton class --- */ sl@0: typedef struct { sl@0: GObject parent_instance; sl@0: } MySingleton; sl@0: typedef struct { sl@0: GObjectClass parent_class; sl@0: } MySingletonClass; sl@0: sl@0: #define MY_TYPE_SINGLETON (my_singleton_get_type ()) sl@0: #define MY_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton)) sl@0: #define MY_IS_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON)) sl@0: #define MY_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass)) sl@0: #define MY_IS_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON)) sl@0: #define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass)) sl@0: sl@0: G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT); sl@0: sl@0: static MySingleton *the_one_and_only = NULL; sl@0: sl@0: /* --- methods --- */ sl@0: static GObject* sl@0: my_singleton_constructor (GType type, sl@0: guint n_construct_properties, sl@0: GObjectConstructParam *construct_properties) sl@0: { sl@0: if (the_one_and_only) sl@0: return g_object_ref (the_one_and_only); sl@0: else sl@0: return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties); sl@0: } sl@0: sl@0: static void sl@0: my_singleton_init (MySingleton *self) sl@0: { sl@0: g_assert (the_one_and_only == NULL); sl@0: the_one_and_only = self; sl@0: } sl@0: sl@0: static void sl@0: my_singleton_class_init (MySingletonClass *klass) sl@0: { sl@0: G_OBJECT_CLASS (klass)->constructor = my_singleton_constructor; sl@0: } sl@0: sl@0: /* --- test program --- */ sl@0: int sl@0: main (int argc, sl@0: char *argv[]) sl@0: { sl@0: MySingleton *singleton, *obj; sl@0: #ifdef __SYMBIAN32__ sl@0: g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*__SYMBIAN32__*/ sl@0: g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS); sl@0: /* create the singleton */ sl@0: singleton = g_object_new (MY_TYPE_SINGLETON, NULL); sl@0: g_assert (singleton != NULL); sl@0: /* assert _singleton_ creation */ sl@0: obj = g_object_new (MY_TYPE_SINGLETON, NULL); sl@0: g_assert (singleton == obj); sl@0: g_object_unref (obj); sl@0: /* shutdown */ sl@0: g_object_unref (singleton); sl@0: #ifdef __SYMBIAN32__ sl@0: testResultXml("singleton"); sl@0: #endif //__SYMBIAN32__ sl@0: return 0; sl@0: }