glib/tests/gobject/singleton.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GObject - GLib Type, Object, Parameter and Signal Library
       
     2  * Copyright (C) 2006 Imendio AB
       
     3  * Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.
       
     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 #ifdef __SYMBIAN32__
       
    24 #include "mrt2_glib2_test.h"
       
    25 #endif //__SYMBIAN32__
       
    26 /* --- MySingleton class --- */
       
    27 typedef struct {
       
    28   GObject parent_instance;
       
    29 } MySingleton;
       
    30 typedef struct {
       
    31   GObjectClass parent_class;
       
    32 } MySingletonClass;
       
    33 
       
    34 #define MY_TYPE_SINGLETON         (my_singleton_get_type ())
       
    35 #define MY_SINGLETON(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton))
       
    36 #define MY_IS_SINGLETON(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON))
       
    37 #define MY_SINGLETON_CLASS(c)     (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass))
       
    38 #define MY_IS_SINGLETON_CLASS(c)  (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON))
       
    39 #define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass))
       
    40 
       
    41 G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT);
       
    42 
       
    43 static MySingleton *the_one_and_only = NULL;
       
    44 
       
    45 /* --- methods --- */
       
    46 static GObject*
       
    47 my_singleton_constructor (GType                  type,
       
    48                           guint                  n_construct_properties,
       
    49                           GObjectConstructParam *construct_properties)
       
    50 {
       
    51   if (the_one_and_only)
       
    52     return g_object_ref (the_one_and_only);
       
    53   else
       
    54     return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
       
    55 }
       
    56 
       
    57 static void
       
    58 my_singleton_init (MySingleton *self)
       
    59 {
       
    60   g_assert (the_one_and_only == NULL);
       
    61   the_one_and_only = self;
       
    62 }
       
    63 
       
    64 static void
       
    65 my_singleton_class_init (MySingletonClass *klass)
       
    66 {
       
    67   G_OBJECT_CLASS (klass)->constructor = my_singleton_constructor;
       
    68 }
       
    69 
       
    70 /* --- test program --- */
       
    71 int
       
    72 main (int   argc,
       
    73       char *argv[])
       
    74 {
       
    75   MySingleton *singleton, *obj;
       
    76     #ifdef __SYMBIAN32__
       
    77   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);
       
    78   g_set_print_handler(mrtPrintHandler);
       
    79   #endif /*__SYMBIAN32__*/
       
    80   g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
       
    81   /* create the singleton */
       
    82   singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
       
    83   g_assert (singleton != NULL);
       
    84   /* assert _singleton_ creation */
       
    85   obj = g_object_new (MY_TYPE_SINGLETON, NULL);
       
    86   g_assert (singleton == obj);
       
    87   g_object_unref (obj);
       
    88   /* shutdown */
       
    89   g_object_unref (singleton);
       
    90       #ifdef __SYMBIAN32__
       
    91   testResultXml("singleton");
       
    92   #endif //__SYMBIAN32__
       
    93   return 0;
       
    94 }