Remove debug flag argument to g_type_init() and add
[platform/upstream/glib.git] / gobject / testgobject.c
1 /* GObject - GLib Type, Object, Parameter and Signal Library
2  * Copyright (C) 2001 Red Hat, Inc.
3  *
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 "TestObject"
21 #include        <glib-object.h>
22
23 #define TEST_TYPE_OBJECT            (test_object_get_type ())
24 #define TEST_OBJECT(object)         (G_TYPE_CHECK_INSTANCE_CAST ((object), TEST_TYPE_OBJECT, TestObject))
25 #define TEST_OBJECT_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), TEST_TYPE_OBJECT, TestObjectClass))
26 #define TEST_IS_OBJECT(object)      (G_TYPE_CHECK_INSTANCE_TYPE ((object), TEST_TYPE_OBJECT))
27 #define TEST_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TEST_TYPE_OBJECT))
28 #define TEST_OBJECT_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), TEST_TYPE_OBJECT, TestObjectClass))
29
30 typedef struct _TestIface TestIface;
31 typedef struct
32 {
33   GTypeInterface base_iface;
34 } TestIfaceClass;
35 typedef struct
36 {
37   GObject parent_instance;
38 } TestObject;
39
40 #define TEST_TYPE_IFACE           (test_iface_get_type ())
41 #define TEST_IFACE(obj)           (G_TYPE_CHECK_INSTANCE_CAST ((obj), TEST_TYPE_IFACE, TestIface))
42 #define TEST_IS_IFACE(obj)        (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TEST_TYPE_IFACE))
43 #define TEST_IFACE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((inst), TEST_TYPE_IFACE, TestIfaceClass))
44
45 GType
46 test_iface_get_type (void)
47 {
48   static GType test_iface_type = 0;
49
50   if (!test_iface_type)
51     {
52       static const GTypeInfo test_iface_info =
53       {
54         sizeof (TestIfaceClass),
55         NULL,           /* base_init */
56         NULL,           /* base_finalize */
57       };
58
59       test_iface_type = g_type_register_static (G_TYPE_INTERFACE, "TestIface", &test_iface_info, 0);
60       g_type_interface_add_prerequisite (test_iface_type, G_TYPE_OBJECT);
61     }
62
63   return test_iface_type;
64 }
65
66 typedef struct
67 {
68   GObjectClass parent_class;
69
70   gchar* (*test_signal) (TestObject *tobject,
71                          TestIface  *iface_object,
72                          gpointer    tdata);
73 } TestObjectClass;
74
75 static void
76 test_object_init (TestObject *tobject)
77 {
78 }
79
80 static gboolean
81 test_signal_accumulator (GSignalInvocationHint *ihint,
82                          GValue                *return_accu,
83                          const GValue          *handler_return,
84                          gpointer               data)
85 {
86   gchar *accu_string = g_value_get_string (return_accu);
87   gchar *new_string = g_value_get_string (handler_return);
88   gchar *result_string;
89
90   if (accu_string)
91     result_string = g_strconcat (accu_string, new_string, NULL);
92   else if (new_string)
93     result_string = g_strdup (new_string);
94   else
95     result_string = NULL;
96
97   g_value_set_string_take_ownership (return_accu, result_string);
98
99   return TRUE;
100 }
101
102 static gchar*
103 test_object_test_signal (TestObject *tobject,
104                          TestIface  *iface_object,
105                          gpointer    tdata)
106 {
107   g_message ("::test_signal default_handler called");
108
109   g_return_val_if_fail (TEST_IS_IFACE (iface_object), NULL);
110   
111   return g_strdup ("<default_handler>");
112 }
113
114 static void
115 test_object_class_init (TestObjectClass *class)
116 {
117   /*  GObjectClass *gobject_class = G_OBJECT_CLASS (class); */
118
119   class->test_signal = test_object_test_signal;
120
121   g_signal_newc ("test-signal",
122                  G_OBJECT_CLASS_TYPE (class),
123                  G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_LAST | G_SIGNAL_RUN_CLEANUP,
124                  G_STRUCT_OFFSET (TestObjectClass, test_signal),
125                  test_signal_accumulator, NULL,
126                  g_cclosure_marshal_STRING__OBJECT_POINTER,
127                  G_TYPE_STRING, 2, TEST_TYPE_IFACE, G_TYPE_POINTER);
128 }
129
130 GType
131 test_object_get_type (void)
132 {
133   static GType test_object_type = 0;
134
135   if (!test_object_type)
136     {
137       static const GTypeInfo test_object_info =
138       {
139         sizeof (TestObjectClass),
140         NULL,           /* base_init */
141         NULL,           /* base_finalize */
142         (GClassInitFunc) test_object_class_init,
143         NULL,           /* class_finalize */
144         NULL,           /* class_data */
145         sizeof (TestObject),
146         5,              /* n_preallocs */
147         (GInstanceInitFunc) test_object_init,
148       };
149       GInterfaceInfo iface_info = { NULL, NULL, NULL };
150
151       test_object_type = g_type_register_static (G_TYPE_OBJECT, "TestObject", &test_object_info, 0);
152       g_type_add_interface_static (test_object_type, TEST_TYPE_IFACE, &iface_info);
153     }
154
155   return test_object_type;
156 }
157
158
159
160 int
161 main (int   argc,
162       char *argv[])
163 {
164   TestObject *tobject, *sigarg;
165   gchar *string = NULL;
166
167   g_log_set_always_fatal (g_log_set_always_fatal (G_LOG_FATAL_MASK) |
168                           G_LOG_LEVEL_WARNING |
169                           G_LOG_LEVEL_CRITICAL);
170   g_type_init_with_debug_flags (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
171
172   tobject = g_object_new (TEST_TYPE_OBJECT, NULL);
173   sigarg = g_object_new (TEST_TYPE_OBJECT, NULL);
174   g_signal_emit_by_name (tobject, "test-signal", sigarg, NULL, &string);
175   g_message ("signal return: \"%s\"", string);
176   g_free (string);
177   
178   g_object_unref (sigarg);
179   g_object_unref (tobject);
180
181   g_message ("%s done", argv[0]);
182
183   return 0;
184 }