sl@0
|
1 |
/* GObject - GLib Type, Object, Parameter and Signal Library
|
sl@0
|
2 |
* Copyright (C) 2006 Imendio AB
|
sl@0
|
3 |
* Portions copyright (c) 2009 Nokia Corporation. All rights reserved.
|
sl@0
|
4 |
* This library is free software; you can redistribute it and/or
|
sl@0
|
5 |
* modify it under the terms of the GNU Lesser General Public
|
sl@0
|
6 |
* License as published by the Free Software Foundation; either
|
sl@0
|
7 |
* version 2 of the License, or (at your option) any later version.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is distributed in the hope that it will be useful,
|
sl@0
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
sl@0
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
sl@0
|
12 |
* Lesser General Public License for more details.
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* You should have received a copy of the GNU Lesser General
|
sl@0
|
15 |
* Public License along with this library; if not, write to the
|
sl@0
|
16 |
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
sl@0
|
17 |
* Boston, MA 02111-1307, USA.
|
sl@0
|
18 |
*/
|
sl@0
|
19 |
#undef G_LOG_DOMAIN
|
sl@0
|
20 |
#define G_LOG_DOMAIN "TestSingleton"
|
sl@0
|
21 |
#include <glib-object.h>
|
sl@0
|
22 |
#include <string.h>
|
sl@0
|
23 |
#ifdef __SYMBIAN32__
|
sl@0
|
24 |
#include "mrt2_glib2_test.h"
|
sl@0
|
25 |
#endif //__SYMBIAN32__
|
sl@0
|
26 |
/* --- MySingleton class --- */
|
sl@0
|
27 |
typedef struct {
|
sl@0
|
28 |
GObject parent_instance;
|
sl@0
|
29 |
} MySingleton;
|
sl@0
|
30 |
typedef struct {
|
sl@0
|
31 |
GObjectClass parent_class;
|
sl@0
|
32 |
} MySingletonClass;
|
sl@0
|
33 |
|
sl@0
|
34 |
#define MY_TYPE_SINGLETON (my_singleton_get_type ())
|
sl@0
|
35 |
#define MY_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), MY_TYPE_SINGLETON, MySingleton))
|
sl@0
|
36 |
#define MY_IS_SINGLETON(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), MY_TYPE_SINGLETON))
|
sl@0
|
37 |
#define MY_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), MY_TYPE_SINGLETON, MySingletonClass))
|
sl@0
|
38 |
#define MY_IS_SINGLETON_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), MY_TYPE_SINGLETON))
|
sl@0
|
39 |
#define MY_SINGLETON_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), MY_TYPE_SINGLETON, MySingletonClass))
|
sl@0
|
40 |
|
sl@0
|
41 |
G_DEFINE_TYPE (MySingleton, my_singleton, G_TYPE_OBJECT);
|
sl@0
|
42 |
|
sl@0
|
43 |
static MySingleton *the_one_and_only = NULL;
|
sl@0
|
44 |
|
sl@0
|
45 |
/* --- methods --- */
|
sl@0
|
46 |
static GObject*
|
sl@0
|
47 |
my_singleton_constructor (GType type,
|
sl@0
|
48 |
guint n_construct_properties,
|
sl@0
|
49 |
GObjectConstructParam *construct_properties)
|
sl@0
|
50 |
{
|
sl@0
|
51 |
if (the_one_and_only)
|
sl@0
|
52 |
return g_object_ref (the_one_and_only);
|
sl@0
|
53 |
else
|
sl@0
|
54 |
return G_OBJECT_CLASS (my_singleton_parent_class)->constructor (type, n_construct_properties, construct_properties);
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
static void
|
sl@0
|
58 |
my_singleton_init (MySingleton *self)
|
sl@0
|
59 |
{
|
sl@0
|
60 |
g_assert (the_one_and_only == NULL);
|
sl@0
|
61 |
the_one_and_only = self;
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
static void
|
sl@0
|
65 |
my_singleton_class_init (MySingletonClass *klass)
|
sl@0
|
66 |
{
|
sl@0
|
67 |
G_OBJECT_CLASS (klass)->constructor = my_singleton_constructor;
|
sl@0
|
68 |
}
|
sl@0
|
69 |
|
sl@0
|
70 |
/* --- test program --- */
|
sl@0
|
71 |
int
|
sl@0
|
72 |
main (int argc,
|
sl@0
|
73 |
char *argv[])
|
sl@0
|
74 |
{
|
sl@0
|
75 |
MySingleton *singleton, *obj;
|
sl@0
|
76 |
#ifdef __SYMBIAN32__
|
sl@0
|
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);
|
sl@0
|
78 |
g_set_print_handler(mrtPrintHandler);
|
sl@0
|
79 |
#endif /*__SYMBIAN32__*/
|
sl@0
|
80 |
g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
|
sl@0
|
81 |
/* create the singleton */
|
sl@0
|
82 |
singleton = g_object_new (MY_TYPE_SINGLETON, NULL);
|
sl@0
|
83 |
g_assert (singleton != NULL);
|
sl@0
|
84 |
/* assert _singleton_ creation */
|
sl@0
|
85 |
obj = g_object_new (MY_TYPE_SINGLETON, NULL);
|
sl@0
|
86 |
g_assert (singleton == obj);
|
sl@0
|
87 |
g_object_unref (obj);
|
sl@0
|
88 |
/* shutdown */
|
sl@0
|
89 |
g_object_unref (singleton);
|
sl@0
|
90 |
#ifdef __SYMBIAN32__
|
sl@0
|
91 |
testResultXml("singleton");
|
sl@0
|
92 |
#endif //__SYMBIAN32__
|
sl@0
|
93 |
return 0;
|
sl@0
|
94 |
}
|