1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tests/gobject/singleton.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,94 @@
1.4 +/* GObject - GLib Type, Object, Parameter and Signal Library
1.5 + * Copyright (C) 2006 Imendio AB
1.6 + * Portions copyright (c) 2009 Nokia Corporation. All rights reserved.
1.7 + * This library is free software; you can redistribute it and/or
1.8 + * modify it under the terms of the GNU Lesser General Public
1.9 + * License as published by the Free Software Foundation; either
1.10 + * version 2 of the License, or (at your option) any later version.
1.11 + *
1.12 + * This library is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1.15 + * Lesser General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU Lesser General
1.18 + * Public License along with this library; if not, write to the
1.19 + * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1.20 + * Boston, MA 02111-1307, USA.
1.21 + */
1.22 +#undef G_LOG_DOMAIN
1.23 +#define G_LOG_DOMAIN "TestSingleton"
1.24 +#include <glib-object.h>
1.25 +#include <string.h>
1.26 +#ifdef __SYMBIAN32__
1.27 +#include "mrt2_glib2_test.h"
1.28 +#endif //__SYMBIAN32__
1.29 +/* --- MySingleton class --- */
1.30 +typedef struct {
1.31 + GObject parent_instance;
1.32 +} MySingleton;
1.33 +typedef struct {
1.34 + GObjectClass parent_class;
1.35 +} MySingletonClass;
1.36 +
1.37 +#define MY_TYPE_SINGLETON (my_singleton_get_type ())
1.38 +#define MY_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton))
1.39 +#define MY_IS_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON))
1.40 +#define MY_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass))
1.41 +#define MY_IS_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON))
1.42 +#define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass))
1.43 +
1.44 +G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT);
1.45 +
1.46 +static MySingleton *the_one_and_only = NULL;
1.47 +
1.48 +/* --- methods --- */
1.49 +static GObject*
1.50 +my_singleton_constructor (GType type,
1.51 + guint n_construct_properties,
1.52 + GObjectConstructParam *construct_properties)
1.53 +{
1.54 + if (the_one_and_only)
1.55 + return g_object_ref (the_one_and_only);
1.56 + else
1.57 + return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
1.58 +}
1.59 +
1.60 +static void
1.61 +my_singleton_init (MySingleton *self)
1.62 +{
1.63 + g_assert (the_one_and_only == NULL);
1.64 + the_one_and_only = self;
1.65 +}
1.66 +
1.67 +static void
1.68 +my_singleton_class_init (MySingletonClass *klass)
1.69 +{
1.70 + G_OBJECT_CLASS (klass)->constructor = my_singleton_constructor;
1.71 +}
1.72 +
1.73 +/* --- test program --- */
1.74 +int
1.75 +main (int argc,
1.76 + char *argv[])
1.77 +{
1.78 + MySingleton *singleton, *obj;
1.79 + #ifdef __SYMBIAN32__
1.80 + 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);
1.81 + g_set_print_handler(mrtPrintHandler);
1.82 + #endif /*__SYMBIAN32__*/
1.83 + g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
1.84 + /* create the singleton */
1.85 + singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
1.86 + g_assert (singleton != NULL);
1.87 + /* assert _singleton_ creation */
1.88 + obj = g_object_new (MY_TYPE_SINGLETON, NULL);
1.89 + g_assert (singleton == obj);
1.90 + g_object_unref (obj);
1.91 + /* shutdown */
1.92 + g_object_unref (singleton);
1.93 + #ifdef __SYMBIAN32__
1.94 + testResultXml("singleton");
1.95 + #endif //__SYMBIAN32__
1.96 + return 0;
1.97 +}