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.
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.
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.
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
23 #include "ibusobject.h"
24 #include "ibusmarshalers.h"
25 #include "ibusinternal.h"
27 #define IBUS_OBJECT_GET_PRIVATE(o) \
28 (G_TYPE_INSTANCE_GET_PRIVATE ((o), IBUS_TYPE_OBJECT, IBusObjectPrivate))
35 struct _IBusObjectPrivate {
39 static guint object_signals[LAST_SIGNAL] = { 0 };
41 // #define DEBUG_MEMORY
43 static GHashTable *_count_table;
44 static guint _count = 0;
47 /* functions prototype */
48 static GObject *ibus_object_constructor (GType type,
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);
56 G_DEFINE_TYPE (IBusObject, ibus_object, G_TYPE_INITIALLY_UNOWNED)
59 ibus_object_class_init (IBusObjectClass *class)
61 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
63 gobject_class->constructor = ibus_object_constructor;
64 gobject_class->dispose = (GObjectFinalizeFunc) ibus_object_dispose;
65 gobject_class->finalize = (GObjectFinalizeFunc) ibus_object_finalize;
67 class->destroy = ibus_object_real_destroy;
71 * IBusObject::destroy:
72 * @object: An IBusObject.
74 * Destroy and free an IBusObject
76 * See also: ibus_object_destroy().
78 * <note><para>Argument @user_data is ignored in this function.</para></note>
80 object_signals[DESTROY] =
81 g_signal_new (I_("destroy"),
82 G_TYPE_FROM_CLASS (gobject_class),
84 G_STRUCT_OFFSET (IBusObjectClass, destroy),
86 _ibus_marshal_VOID__VOID,
89 g_type_class_add_private (class, sizeof (IBusObjectPrivate));
92 _count_table = g_hash_table_new (g_direct_hash, g_direct_equal);
97 ibus_object_init (IBusObject *obj)
100 obj->priv = IBUS_OBJECT_GET_PRIVATE (obj);
105 ibus_object_constructor (GType type,
107 GObjectConstructParam *args)
111 object = G_OBJECT_CLASS (ibus_object_parent_class)->constructor (type, n ,args);
114 if (object != NULL) {
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));
121 g_debug ("new %s, count = %d, all = %d", g_type_name (type), count, _count);
130 ibus_object_dispose (IBusObject *obj)
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);
138 IBUS_OBJECT_UNSET_FLAGS (obj, IBUS_IN_DESTRUCTION);
141 G_OBJECT_CLASS(ibus_object_parent_class)->dispose (G_OBJECT (obj));
145 ibus_object_finalize (IBusObject *obj)
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);
156 G_OBJECT_CLASS(ibus_object_parent_class)->finalize (G_OBJECT (obj));
160 ibus_object_real_destroy (IBusObject *obj)
162 g_signal_handlers_destroy (obj);
168 * Creates a new instance of an #IBusObject.
170 * Returns: a new instance of #IBusObject.
173 ibus_object_new (void)
175 GObject *object = g_object_new (IBUS_TYPE_OBJECT, NULL);
176 return IBUS_OBJECT (object);
180 ibus_object_destroy (IBusObject *obj)
182 g_return_if_fail (IBUS_IS_OBJECT (obj));
184 if (! (IBUS_OBJECT_FLAGS (obj) & IBUS_IN_DESTRUCTION)) {
185 g_object_run_dispose (G_OBJECT (obj));