1 /* GObject - GLib Type, Object, Parameter and Signal Library
2 * Copyright (C) 2001 Red Hat, Inc.
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.
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.
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.
20 #define G_LOG_DOMAIN "TestObject"
21 #include <glib-object.h>
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))
30 typedef struct _TestIface TestIface;
33 GTypeInterface base_iface;
37 GObject parent_instance;
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))
46 test_iface_get_type (void)
48 static GType test_iface_type = 0;
52 static const GTypeInfo test_iface_info =
54 sizeof (TestIfaceClass),
56 NULL, /* base_finalize */
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);
63 return test_iface_type;
68 GObjectClass parent_class;
70 gchar* (*test_signal) (TestObject *tobject,
71 TestIface *iface_object,
76 test_object_init (TestObject *tobject)
81 test_signal_accumulator (GSignalInvocationHint *ihint,
83 const GValue *handler_return,
86 gchar *accu_string = g_value_get_string (return_accu);
87 gchar *new_string = g_value_get_string (handler_return);
91 result_string = g_strconcat (accu_string, new_string, NULL);
93 result_string = g_strdup (new_string);
97 g_value_set_string_take_ownership (return_accu, result_string);
103 test_object_test_signal (TestObject *tobject,
104 TestIface *iface_object,
107 g_message ("::test_signal default_handler called");
109 g_return_val_if_fail (TEST_IS_IFACE (iface_object), NULL);
111 return g_strdup ("<default_handler>");
115 test_object_class_init (TestObjectClass *class)
117 /* GObjectClass *gobject_class = G_OBJECT_CLASS (class); */
119 class->test_signal = test_object_test_signal;
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);
131 test_object_get_type (void)
133 static GType test_object_type = 0;
135 if (!test_object_type)
137 static const GTypeInfo test_object_info =
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 */
147 (GInstanceInitFunc) test_object_init,
149 GInterfaceInfo iface_info = { NULL, NULL, NULL };
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);
155 return test_object_type;
164 TestObject *tobject, *sigarg;
165 gchar *string = NULL;
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 (G_TYPE_DEBUG_OBJECTS | G_TYPE_DEBUG_SIGNALS);
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);
178 g_object_unref (sigarg);
179 g_object_unref (tobject);
181 g_message ("%s done", argv[0]);