Debian Codegraph @

test/test_object.c

raw symbols SCIP
1 #include "test_object.h"
2
3 #include <glib.h>
4 #include <glib-object.h>
5
6 // helper enum
7 GType
8 gi_cpp_enum_get_type (void)
9 {
10 static GType enum_type = 0;
11
12 static const GEnumValue enum_types[] = {
13 {ENUM_VALUE_0, "EnumValue0", "v0"},
14 {ENUM_VALUE_1, "EnumValue1", "v1"},
15 {0, NULL, NULL}
16 };
17
18 if (!enum_type) {
19 enum_type = g_enum_register_static ("GICppEnum", enum_types);
20 }
21 return enum_type;
22 }
23
24 // helper flags
25 GType
26 gi_cpp_flags_get_type (void)
27 {
28 static GType flags_type = 0;
29
30 static const GFlagsValue flags_types[] = {
31 {FLAG_VALUE_0, "FlagValue0", "f0"},
32 {FLAG_VALUE_1, "FlagValue1", "f1"},
33 {0, NULL, NULL}
34 };
35
36 if (!flags_type) {
37 flags_type = g_flags_register_static ("GICppFlags", flags_types);
38 }
39 return flags_type;
40 }
41
42 // interface
43 typedef GICppExampleInterface GICppIExampleInterface;
44 G_DEFINE_INTERFACE (GICppIExample, gi_cpp_example_interface, 0)
45
46 static void
47 gi_cpp_example_interface_default_init (GICppExampleInterface *iface)
48 {
49 (void) iface;
50 }
51
52 // property interface
53 typedef GICppPropertyInterface GICppIPropertyInterface;
54 G_DEFINE_INTERFACE (GICppIProperty, gi_cpp_property_interface, 0)
55
56 static void
57 gi_cpp_property_interface_default_init (GICppPropertyInterface *iface)
58 {
59 (void) iface;
60
61 g_object_interface_install_property (iface,
62 g_param_spec_int (NAME_INUMBER, "ItfNumber",
63 "ItfNumber", 0, 50, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
64 }
65
66 enum {
67 EXAMPLE_TO_INT,
68 EXAMPLE_TO_STRING,
69 EXAMPLE_TO_VOID,
70 EXAMPLE_TO_OUTPUT_INT,
71 /* FILL ME */
72 LAST_SIGNAL
73 };
74
75 static guint example_signals[LAST_SIGNAL] = { 0 };
76
77 struct _GICppExample
78 {
79 GObject object;
80
81 /* properties */
82 gchar *data;
83 gint number;
84 gdouble fnumber;
85 GObject *obj;
86 gboolean present;
87 CEnum enumvalue;
88 CFlags flagsvalue;
89 GError *error;
90 gint itf_number;
91 };
92
93 static int
94 vmethod_interface_default (GICppExampleItf * itf, int a)
95 {
96 // sanity check
97 g_assert (GI_IS_CPP_EXAMPLE (itf));
98
99 return 2 + a;
100 }
101
102 static void
103 gi_cpp_example_interface_init (GICppExampleInterface *iface)
104 {
105 iface->vmethod = vmethod_interface_default;
106 }
107
108 #define gi_cpp_example_parent_class parent_class
109 G_DEFINE_TYPE_WITH_CODE (GICppExample, gi_cpp_example, G_TYPE_OBJECT,
110 G_IMPLEMENT_INTERFACE (gi_cpp_example_interface_get_type(), gi_cpp_example_interface_init))
111
112 static void
113 gi_cpp_example_get_property (GObject * object, guint prop_id, GValue * value,
114 GParamSpec * pspec)
115 {
116 GICppExample *ex = GI_CPP_EXAMPLE (object);
117
118 switch (prop_id) {
119 case PROP_DATA:
120 g_value_set_string (value, ex->data);
121 break;
122 case PROP_NUMBER:
123 g_value_set_int (value, ex->number);
124 break;
125 case PROP_FNUMBER:
126 g_value_set_double (value, ex->fnumber);
127 break;
128 case PROP_PRESENT:
129 g_value_set_boolean (value, ex->present);
130 break;
131 case PROP_OBJECT:
132 g_value_set_object (value, ex->obj);
133 break;
134 case PROP_ENUM:
135 g_value_set_enum (value, ex->enumvalue);
136 break;
137 case PROP_FLAGS:
138 g_value_set_flags (value, ex->flagsvalue);
139 break;
140 case PROP_ERROR:
141 g_value_set_boxed (value, ex->error);
142 break;
143 default:
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
145 break;
146 }
147 }
148
149 static void
150 gi_cpp_example_set_property (GObject * object, guint prop_id, const GValue * value,
151 GParamSpec * pspec)
152 {
153 GICppExample *ex = GI_CPP_EXAMPLE (object);
154
155 switch (prop_id) {
156 case PROP_DATA:
157 g_free (ex->data);
158 ex->data = g_value_dup_string (value);
159 break;
160 case PROP_NUMBER:
161 ex->number = g_value_get_int (value);
162 break;
163 case PROP_FNUMBER:
164 ex->fnumber = g_value_get_double (value);
165 break;
166 case PROP_PRESENT:
167 ex->present = g_value_get_boolean (value);
168 break;
169 case PROP_OBJECT:
170 if (ex->obj)
171 g_object_unref (ex->obj);
172 ex->obj = g_value_dup_object (value);
173 break;
174 case PROP_ENUM:
175 ex->enumvalue = g_value_get_enum (value);
176 break;
177 case PROP_FLAGS:
178 ex->flagsvalue = g_value_get_flags (value);
179 break;
180 case PROP_ERROR:
181 if (ex->error)
182 g_error_free(ex->error);
183 ex->error = g_value_dup_boxed(value);
184 break;
185 default:
186 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
187 break;
188 }
189 }
190
191 static void
192 gi_cpp_example_finalize (GObject * object)
193 {
194 GICppExample *ex = GI_CPP_EXAMPLE (object);
195
196 g_free (ex->data);
197 if (ex->obj)
198 g_object_unref (ex->obj);
199 if (ex->error)
200 g_error_free(ex->error);
201
202 G_OBJECT_CLASS (parent_class)->finalize (object);
203 }
204
205 static int
206 vmethod_default (GICppExample * ex, int a, int b)
207 {
208 // sanity check
209 g_assert (GI_IS_CPP_EXAMPLE (ex));
210
211 (void)ex;
212 return a + b;
213 }
214
215 static void
216 gi_cpp_example_class_init (GICppExampleClass * klass)
217 {
218 GObjectClass *gobject_class;
219
220 gobject_class = (GObjectClass *) klass;
221
222 gobject_class->finalize = gi_cpp_example_finalize;
223 gobject_class->set_property = gi_cpp_example_set_property;
224 gobject_class->get_property = gi_cpp_example_get_property;
225
226 g_object_class_install_property (gobject_class, PROP_NUMBER,
227 g_param_spec_int (NAME_NUMBER, "Number",
228 "Number", 0, 50, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229
230 g_object_class_install_property (gobject_class, PROP_FNUMBER,
231 g_param_spec_double (NAME_FNUMBER, "FNumber",
232 "FNumber", 0, 50, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233
234 g_object_class_install_property (gobject_class, PROP_DATA,
235 g_param_spec_string (NAME_DATA, "Data", "Data", NULL,
236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
237
238 g_object_class_install_property (gobject_class, PROP_PRESENT,
239 g_param_spec_boolean (NAME_PRESENT, "Present", "Present",
240 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241
242 g_object_class_install_property (gobject_class, PROP_OBJECT,
243 g_param_spec_object (NAME_OBJECT, "Object", "Object",
244 G_TYPE_OBJECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
245
246 g_object_class_install_property (gobject_class, PROP_ENUM,
247 g_param_spec_enum (NAME_ENUM, "Enum", "Enum",
248 GI_CPP_TYPE_ENUM, ENUM_VALUE_0,
249 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250
251 g_object_class_install_property (gobject_class, PROP_FLAGS,
252 g_param_spec_flags (NAME_FLAGS, "Flags", "Flags",
253 GI_CPP_TYPE_FLAGS, FLAG_VALUE_0,
254 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
255
256 g_object_class_install_property (gobject_class, PROP_ERROR,
257 g_param_spec_boxed (NAME_ERROR, "Error", "Error",
258 G_TYPE_ERROR, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259
260 example_signals[EXAMPLE_TO_INT] =
261 g_signal_new ("to-int", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
262 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_INT, 4,
263 G_TYPE_OBJECT, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_STRING);
264
265 example_signals[EXAMPLE_TO_STRING] =
266 g_signal_new ("to-string", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
267 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_STRING, 2,
268 G_TYPE_INT, G_TYPE_INT64);
269
270 example_signals[EXAMPLE_TO_VOID] =
271 g_signal_new ("to-void", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
272 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 3,
273 G_TYPE_DOUBLE, GI_CPP_TYPE_ENUM, GI_CPP_TYPE_FLAGS);
274
275 example_signals[EXAMPLE_TO_VOID] =
276 g_signal_new ("to-output-int", G_TYPE_FROM_CLASS (klass),
277 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
278 G_TYPE_NONE, 3, G_TYPE_POINTER, G_TYPE_PTR_ARRAY, G_TYPE_POINTER);
279
280 klass->vmethod = vmethod_default;
281 }
282
283 static void
284 gi_cpp_example_init (GICppExample * ex)
285 {
286 (void)ex;
287 }
288
289 GICppExample *
290 gi_cpp_example_new ()
291 {
292 return g_object_new (GI_CPP_TYPE_EXAMPLE, NULL);
293 }