Clean spec file for yocto compliance.
[platform/upstream/ibus.git] / src / ibusobject.c
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* ibus - The Input Bus
4  * Copyright (C) 2008-2010 Peng Huang <shawn.p.huang@gmail.com>
5  * Copyright (C) 2008-2010 Red Hat, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
20  * USA
21  */
22
23 #include "ibusobject.h"
24 #include "ibusmarshalers.h"
25 #include "ibusinternal.h"
26
27 #define IBUS_OBJECT_GET_PRIVATE(o)  \
28    (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_OBJECT, IBusObjectPrivate))
29
30 enum {
31     DESTROY,
32     LAST_SIGNAL,
33 };
34
35 struct _IBusObjectPrivate {
36     gpointer pad;
37 };
38
39 static guint            object_signals[LAST_SIGNAL] = { 0 };
40
41 // #define DEBUG_MEMORY
42 #ifdef DEBUG_MEMORY
43 static GHashTable      *_count_table;
44 static guint            _count = 0;
45 #endif
46
47 /* functions prototype */
48 static GObject  *ibus_object_constructor    (GType               type,
49                                              guint               n,
50                                              GObjectConstructParam
51                                                                 *args);
52 static void      ibus_object_dispose        (IBusObject         *obj);
53 static void      ibus_object_finalize       (IBusObject         *obj);
54 static void      ibus_object_real_destroy   (IBusObject         *obj);
55
56 G_DEFINE_TYPE (IBusObject, ibus_object, G_TYPE_INITIALLY_UNOWNED)
57
58 static void
59 ibus_object_class_init     (IBusObjectClass *class)
60 {
61     GObjectClass *gobject_class = G_OBJECT_CLASS (class);
62
63     gobject_class->constructor = ibus_object_constructor;
64     gobject_class->dispose = (GObjectFinalizeFunc) ibus_object_dispose;
65     gobject_class->finalize = (GObjectFinalizeFunc) ibus_object_finalize;
66
67     class->destroy = ibus_object_real_destroy;
68
69     /* install signals */
70     /**
71      * IBusObject::destroy:
72      * @object: An IBusObject.
73      *
74      * Destroy and free an IBusObject
75      *
76      * See also:  ibus_object_destroy().
77      *
78      * <note><para>Argument @user_data is ignored in this function.</para></note>
79      */
80     object_signals[DESTROY] =
81         g_signal_new (I_("destroy"),
82             G_TYPE_FROM_CLASS (gobject_class),
83             G_SIGNAL_RUN_LAST,
84             G_STRUCT_OFFSET (IBusObjectClass, destroy),
85             NULL, NULL,
86             _ibus_marshal_VOID__VOID,
87             G_TYPE_NONE, 0);
88
89     g_type_class_add_private (class, sizeof (IBusObjectPrivate));
90
91 #ifdef DEBUG_MEMORY
92     _count_table = g_hash_table_new (g_direct_hash, g_direct_equal);
93 #endif
94 }
95
96 static void
97 ibus_object_init (IBusObject *obj)
98 {
99     obj->flags = 0;
100     obj->priv = IBUS_OBJECT_GET_PRIVATE (obj);
101 }
102
103
104 static GObject *
105 ibus_object_constructor (GType                   type,
106                          guint                   n,
107                          GObjectConstructParam  *args)
108 {
109     GObject *object;
110
111     object = G_OBJECT_CLASS (ibus_object_parent_class)->constructor (type, n ,args);
112
113 #ifdef DEBUG_MEMORY
114     if (object != NULL) {
115         guint count;
116         _count ++;
117
118         count = GPOINTER_TO_UINT (g_hash_table_lookup (_count_table, (gpointer) type));
119         g_hash_table_replace (_count_table, (gpointer) type, GUINT_TO_POINTER (++count));
120
121         g_debug ("new %s, count = %d, all = %d", g_type_name (type), count, _count);
122     }
123 #endif
124
125     return object;
126 }
127
128
129 static void
130 ibus_object_dispose (IBusObject *obj)
131 {
132     if (! (IBUS_OBJECT_FLAGS (obj) & IBUS_IN_DESTRUCTION)) {
133         IBUS_OBJECT_SET_FLAGS (obj, IBUS_IN_DESTRUCTION);
134         if (! (IBUS_OBJECT_FLAGS (obj) & IBUS_DESTROYED)) {
135             g_signal_emit (obj, object_signals[DESTROY], 0);
136             IBUS_OBJECT_SET_FLAGS (obj, IBUS_DESTROYED);
137         }
138         IBUS_OBJECT_UNSET_FLAGS (obj, IBUS_IN_DESTRUCTION);
139     }
140
141     G_OBJECT_CLASS(ibus_object_parent_class)->dispose (G_OBJECT (obj));
142 }
143
144 static void
145 ibus_object_finalize (IBusObject *obj)
146 {
147 #ifdef DEBUG_MEMORY
148     guint count;
149
150     _count --;
151     count = GPOINTER_TO_UINT (g_hash_table_lookup (_count_table, (gpointer)G_OBJECT_TYPE (obj)));
152     g_hash_table_replace (_count_table, (gpointer)G_OBJECT_TYPE (obj), GUINT_TO_POINTER (--count));
153     g_debug ("Finalize %s, count = %d, all = %d", G_OBJECT_TYPE_NAME (obj), count, _count);
154 #endif
155
156     G_OBJECT_CLASS(ibus_object_parent_class)->finalize (G_OBJECT (obj));
157 }
158
159 static void
160 ibus_object_real_destroy (IBusObject *obj)
161 {
162     g_signal_handlers_destroy (obj);
163 }
164
165 /**
166  * ibus_object_new:
167  *
168  * Creates a new instance of an #IBusObject.
169  *
170  * Returns: a new instance of #IBusObject.
171  */
172 IBusObject *
173 ibus_object_new (void)
174 {
175     GObject *object = g_object_new (IBUS_TYPE_OBJECT, NULL);
176     return IBUS_OBJECT (object);
177 }
178
179 void
180 ibus_object_destroy (IBusObject *obj)
181 {
182     g_return_if_fail (IBUS_IS_OBJECT (obj));
183
184     if (! (IBUS_OBJECT_FLAGS (obj) & IBUS_IN_DESTRUCTION)) {
185         g_object_run_dispose (G_OBJECT (obj));
186     }
187 }