/src/glib/gobject/genums.c
Line | Count | Source |
1 | | /* GObject - GLib Type, Object, Parameter and Signal Library |
2 | | * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc. |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General |
17 | | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | /* |
21 | | * MT safe |
22 | | */ |
23 | | |
24 | | #include "config.h" |
25 | | |
26 | | #include <string.h> |
27 | | |
28 | | #include "genums.h" |
29 | | #include "gtype-private.h" |
30 | | #include "gvalue.h" |
31 | | #include "gvaluecollector.h" |
32 | | |
33 | | |
34 | | /* --- prototypes --- */ |
35 | | static void g_enum_class_init (GEnumClass *class, |
36 | | gpointer class_data); |
37 | | static void g_flags_class_init (GFlagsClass *class, |
38 | | gpointer class_data); |
39 | | static void value_flags_enum_init (GValue *value); |
40 | | static void value_flags_enum_copy_value (const GValue *src_value, |
41 | | GValue *dest_value); |
42 | | static gchar* value_flags_enum_collect_value (GValue *value, |
43 | | guint n_collect_values, |
44 | | GTypeCValue *collect_values, |
45 | | guint collect_flags); |
46 | | static gchar* value_flags_enum_lcopy_value (const GValue *value, |
47 | | guint n_collect_values, |
48 | | GTypeCValue *collect_values, |
49 | | guint collect_flags); |
50 | | |
51 | | /* --- functions --- */ |
52 | | void |
53 | | _g_enum_types_init (void) |
54 | 4 | { |
55 | 4 | static gboolean initialized = FALSE; |
56 | 4 | static const GTypeValueTable flags_enum_value_table = { |
57 | 4 | value_flags_enum_init, /* value_init */ |
58 | 4 | NULL, /* value_free */ |
59 | 4 | value_flags_enum_copy_value, /* value_copy */ |
60 | 4 | NULL, /* value_peek_pointer */ |
61 | 4 | "i", /* collect_format */ |
62 | 4 | value_flags_enum_collect_value, /* collect_value */ |
63 | 4 | "p", /* lcopy_format */ |
64 | 4 | value_flags_enum_lcopy_value, /* lcopy_value */ |
65 | 4 | }; |
66 | 4 | GTypeInfo info = { |
67 | 4 | 0, /* class_size */ |
68 | 4 | NULL, /* base_init */ |
69 | 4 | NULL, /* base_destroy */ |
70 | 4 | NULL, /* class_init */ |
71 | 4 | NULL, /* class_destroy */ |
72 | 4 | NULL, /* class_data */ |
73 | 4 | 0, /* instance_size */ |
74 | 4 | 0, /* n_preallocs */ |
75 | 4 | NULL, /* instance_init */ |
76 | 4 | &flags_enum_value_table, /* value_table */ |
77 | 4 | }; |
78 | 4 | static const GTypeFundamentalInfo finfo = { |
79 | 4 | G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE, |
80 | 4 | }; |
81 | 4 | GType type G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; |
82 | | |
83 | 4 | g_return_if_fail (initialized == FALSE); |
84 | 4 | initialized = TRUE; |
85 | | |
86 | | /* G_TYPE_ENUM |
87 | | */ |
88 | 4 | info.class_size = sizeof (GEnumClass); |
89 | 4 | type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo, |
90 | 4 | G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT); |
91 | 4 | g_assert (type == G_TYPE_ENUM); |
92 | | |
93 | | /* G_TYPE_FLAGS |
94 | | */ |
95 | 4 | info.class_size = sizeof (GFlagsClass); |
96 | 4 | type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo, |
97 | 4 | G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT); |
98 | 4 | g_assert (type == G_TYPE_FLAGS); |
99 | 4 | } |
100 | | |
101 | | static void |
102 | | value_flags_enum_init (GValue *value) |
103 | 0 | { |
104 | 0 | value->data[0].v_long = 0; |
105 | 0 | } |
106 | | |
107 | | static void |
108 | | value_flags_enum_copy_value (const GValue *src_value, |
109 | | GValue *dest_value) |
110 | 0 | { |
111 | 0 | dest_value->data[0].v_long = src_value->data[0].v_long; |
112 | 0 | } |
113 | | |
114 | | static gchar* |
115 | | value_flags_enum_collect_value (GValue *value, |
116 | | guint n_collect_values, |
117 | | GTypeCValue *collect_values, |
118 | | guint collect_flags) |
119 | 0 | { |
120 | 0 | if (G_VALUE_HOLDS_ENUM (value)) |
121 | 0 | value->data[0].v_long = collect_values[0].v_int; |
122 | 0 | else |
123 | 0 | value->data[0].v_ulong = (guint) collect_values[0].v_int; |
124 | |
|
125 | 0 | return NULL; |
126 | 0 | } |
127 | | |
128 | | static gchar* |
129 | | value_flags_enum_lcopy_value (const GValue *value, |
130 | | guint n_collect_values, |
131 | | GTypeCValue *collect_values, |
132 | | guint collect_flags) |
133 | 0 | { |
134 | 0 | gint *int_p = collect_values[0].v_pointer; |
135 | |
|
136 | 0 | g_return_val_if_fail (int_p != NULL, g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value))); |
137 | | |
138 | 0 | *int_p = value->data[0].v_long; |
139 | | |
140 | 0 | return NULL; |
141 | 0 | } |
142 | | |
143 | | /** |
144 | | * g_enum_register_static: |
145 | | * @name: A nul-terminated string used as the name of the new type. |
146 | | * @const_static_values: (array zero-terminated=1): An array of |
147 | | * #GEnumValue structs for the possible enumeration values. The array is |
148 | | * terminated by a struct with all members being 0. GObject keeps a |
149 | | * reference to the data, so it cannot be stack-allocated. |
150 | | * |
151 | | * Registers a new static enumeration type with the name @name. |
152 | | * |
153 | | * It is normally more convenient to let [glib-mkenums][glib-mkenums], |
154 | | * generate a my_enum_get_type() function from a usual C enumeration |
155 | | * definition than to write one yourself using g_enum_register_static(). |
156 | | * |
157 | | * Returns: The new type identifier. |
158 | | */ |
159 | | GType |
160 | | g_enum_register_static (const gchar *name, |
161 | | const GEnumValue *const_static_values) |
162 | 0 | { |
163 | 0 | GTypeInfo enum_type_info = { |
164 | 0 | sizeof (GEnumClass), /* class_size */ |
165 | 0 | NULL, /* base_init */ |
166 | 0 | NULL, /* base_finalize */ |
167 | 0 | (GClassInitFunc) g_enum_class_init, |
168 | 0 | NULL, /* class_finalize */ |
169 | 0 | NULL, /* class_data */ |
170 | 0 | 0, /* instance_size */ |
171 | 0 | 0, /* n_preallocs */ |
172 | 0 | NULL, /* instance_init */ |
173 | 0 | NULL, /* value_table */ |
174 | 0 | }; |
175 | 0 | GType type; |
176 | | |
177 | 0 | g_return_val_if_fail (name != NULL, 0); |
178 | 0 | g_return_val_if_fail (const_static_values != NULL, 0); |
179 | | |
180 | 0 | enum_type_info.class_data = const_static_values; |
181 | | |
182 | 0 | type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0); |
183 | | |
184 | 0 | return type; |
185 | 0 | } |
186 | | |
187 | | /** |
188 | | * g_flags_register_static: |
189 | | * @name: A nul-terminated string used as the name of the new type. |
190 | | * @const_static_values: (array zero-terminated=1): An array of |
191 | | * #GFlagsValue structs for the possible flags values. The array is |
192 | | * terminated by a struct with all members being 0. GObject keeps a |
193 | | * reference to the data, so it cannot be stack-allocated. |
194 | | * |
195 | | * Registers a new static flags type with the name @name. |
196 | | * |
197 | | * It is normally more convenient to let [glib-mkenums][glib-mkenums] |
198 | | * generate a my_flags_get_type() function from a usual C enumeration |
199 | | * definition than to write one yourself using g_flags_register_static(). |
200 | | * |
201 | | * Returns: The new type identifier. |
202 | | */ |
203 | | GType |
204 | | g_flags_register_static (const gchar *name, |
205 | | const GFlagsValue *const_static_values) |
206 | 0 | { |
207 | 0 | GTypeInfo flags_type_info = { |
208 | 0 | sizeof (GFlagsClass), /* class_size */ |
209 | 0 | NULL, /* base_init */ |
210 | 0 | NULL, /* base_finalize */ |
211 | 0 | (GClassInitFunc) g_flags_class_init, |
212 | 0 | NULL, /* class_finalize */ |
213 | 0 | NULL, /* class_data */ |
214 | 0 | 0, /* instance_size */ |
215 | 0 | 0, /* n_preallocs */ |
216 | 0 | NULL, /* instance_init */ |
217 | 0 | NULL, /* value_table */ |
218 | 0 | }; |
219 | 0 | GType type; |
220 | | |
221 | 0 | g_return_val_if_fail (name != NULL, 0); |
222 | 0 | g_return_val_if_fail (const_static_values != NULL, 0); |
223 | | |
224 | 0 | flags_type_info.class_data = const_static_values; |
225 | | |
226 | 0 | type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0); |
227 | | |
228 | 0 | return type; |
229 | 0 | } |
230 | | |
231 | | /** |
232 | | * g_enum_complete_type_info: |
233 | | * @g_enum_type: the type identifier of the type being completed |
234 | | * @info: (out callee-allocates): the #GTypeInfo struct to be filled in |
235 | | * @const_values: (array zero-terminated=1): An array of #GEnumValue |
236 | | * structs for the possible enumeration values. The array is terminated |
237 | | * by a struct with all members being 0. |
238 | | * |
239 | | * This function is meant to be called from the `complete_type_info` |
240 | | * function of a #GTypePlugin implementation, as in the following |
241 | | * example: |
242 | | * |
243 | | * |[<!-- language="C" --> |
244 | | * static void |
245 | | * my_enum_complete_type_info (GTypePlugin *plugin, |
246 | | * GType g_type, |
247 | | * GTypeInfo *info, |
248 | | * GTypeValueTable *value_table) |
249 | | * { |
250 | | * static const GEnumValue values[] = { |
251 | | * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" }, |
252 | | * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" }, |
253 | | * { 0, NULL, NULL } |
254 | | * }; |
255 | | * |
256 | | * g_enum_complete_type_info (type, info, values); |
257 | | * } |
258 | | * ]| |
259 | | */ |
260 | | void |
261 | | g_enum_complete_type_info (GType g_enum_type, |
262 | | GTypeInfo *info, |
263 | | const GEnumValue *const_values) |
264 | 0 | { |
265 | 0 | g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type)); |
266 | 0 | g_return_if_fail (info != NULL); |
267 | 0 | g_return_if_fail (const_values != NULL); |
268 | | |
269 | 0 | info->class_size = sizeof (GEnumClass); |
270 | 0 | info->base_init = NULL; |
271 | 0 | info->base_finalize = NULL; |
272 | 0 | info->class_init = (GClassInitFunc) g_enum_class_init; |
273 | 0 | info->class_finalize = NULL; |
274 | 0 | info->class_data = const_values; |
275 | 0 | } |
276 | | |
277 | | /** |
278 | | * g_flags_complete_type_info: |
279 | | * @g_flags_type: the type identifier of the type being completed |
280 | | * @info: (out callee-allocates): the #GTypeInfo struct to be filled in |
281 | | * @const_values: (array zero-terminated=1): An array of #GFlagsValue |
282 | | * structs for the possible enumeration values. The array is terminated |
283 | | * by a struct with all members being 0. |
284 | | * |
285 | | * This function is meant to be called from the complete_type_info() |
286 | | * function of a #GTypePlugin implementation, see the example for |
287 | | * g_enum_complete_type_info() above. |
288 | | */ |
289 | | void |
290 | | g_flags_complete_type_info (GType g_flags_type, |
291 | | GTypeInfo *info, |
292 | | const GFlagsValue *const_values) |
293 | 0 | { |
294 | 0 | g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type)); |
295 | 0 | g_return_if_fail (info != NULL); |
296 | 0 | g_return_if_fail (const_values != NULL); |
297 | | |
298 | 0 | info->class_size = sizeof (GFlagsClass); |
299 | 0 | info->base_init = NULL; |
300 | 0 | info->base_finalize = NULL; |
301 | 0 | info->class_init = (GClassInitFunc) g_flags_class_init; |
302 | 0 | info->class_finalize = NULL; |
303 | 0 | info->class_data = const_values; |
304 | 0 | } |
305 | | |
306 | | static void |
307 | | g_enum_class_init (GEnumClass *class, |
308 | | gpointer class_data) |
309 | 0 | { |
310 | 0 | g_return_if_fail (G_IS_ENUM_CLASS (class)); |
311 | | |
312 | 0 | class->minimum = 0; |
313 | 0 | class->maximum = 0; |
314 | 0 | class->n_values = 0; |
315 | 0 | class->values = class_data; |
316 | | |
317 | 0 | if (class->values) |
318 | 0 | { |
319 | 0 | GEnumValue *values; |
320 | | |
321 | 0 | class->minimum = class->values->value; |
322 | 0 | class->maximum = class->values->value; |
323 | 0 | for (values = class->values; values->value_name; values++) |
324 | 0 | { |
325 | 0 | class->minimum = MIN (class->minimum, values->value); |
326 | 0 | class->maximum = MAX (class->maximum, values->value); |
327 | 0 | class->n_values++; |
328 | 0 | } |
329 | 0 | } |
330 | 0 | } |
331 | | |
332 | | static void |
333 | | g_flags_class_init (GFlagsClass *class, |
334 | | gpointer class_data) |
335 | 0 | { |
336 | 0 | g_return_if_fail (G_IS_FLAGS_CLASS (class)); |
337 | | |
338 | 0 | class->mask = 0; |
339 | 0 | class->n_values = 0; |
340 | 0 | class->values = class_data; |
341 | | |
342 | 0 | if (class->values) |
343 | 0 | { |
344 | 0 | GFlagsValue *values; |
345 | | |
346 | 0 | for (values = class->values; values->value_name; values++) |
347 | 0 | { |
348 | 0 | class->mask |= values->value; |
349 | 0 | class->n_values++; |
350 | 0 | } |
351 | 0 | } |
352 | 0 | } |
353 | | |
354 | | /* Internal function to compare strings without taking into account |
355 | | * character case and more... in order to ease the look ups. */ |
356 | | static int |
357 | | strcmp_ignore_case (const char *str1, const char *str2) |
358 | 0 | { |
359 | 0 | const char *ptr1 = str1, *ptr2 = str2; |
360 | 0 | char c1, c2; |
361 | |
|
362 | 0 | do |
363 | 0 | { |
364 | | /* Normalize to lower case */ |
365 | 0 | c1 = g_ascii_tolower (*ptr1++); |
366 | 0 | c2 = g_ascii_tolower (*ptr2++); |
367 | | |
368 | | /* End of either string */ |
369 | 0 | if (c1 == '\0' || c2 == '\0') |
370 | 0 | return c1 - c2; |
371 | | |
372 | | /* Normalize '-' to '_' */ |
373 | 0 | c1 = (c1 == '-') ? '_' : c1; |
374 | 0 | c2 = (c2 == '-') ? '_' : c2; |
375 | 0 | } |
376 | 0 | while (c1 == c2); |
377 | | |
378 | 0 | return c1 - c2; |
379 | 0 | } |
380 | | |
381 | | /** |
382 | | * g_enum_get_value_by_name: |
383 | | * @enum_class: a #GEnumClass |
384 | | * @name: the name to look up |
385 | | * |
386 | | * Looks up a #GEnumValue by name. |
387 | | * |
388 | | * Returns: (transfer none) (nullable): the #GEnumValue with name @name, |
389 | | * or %NULL if the enumeration doesn't have a member |
390 | | * with that name |
391 | | */ |
392 | | GEnumValue* |
393 | | g_enum_get_value_by_name (GEnumClass *enum_class, |
394 | | const gchar *name) |
395 | 0 | { |
396 | 0 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
397 | 0 | g_return_val_if_fail (name != NULL, NULL); |
398 | | |
399 | 0 | if (enum_class->n_values) |
400 | 0 | { |
401 | 0 | GEnumValue *enum_value; |
402 | | |
403 | 0 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
404 | 0 | if (strcmp_ignore_case (name, enum_value->value_name) == 0) |
405 | 0 | return enum_value; |
406 | 0 | } |
407 | | |
408 | 0 | return NULL; |
409 | 0 | } |
410 | | |
411 | | /** |
412 | | * g_flags_get_value_by_name: |
413 | | * @flags_class: a #GFlagsClass |
414 | | * @name: the name to look up |
415 | | * |
416 | | * Looks up a #GFlagsValue by name. |
417 | | * |
418 | | * Returns: (transfer none) (nullable): the #GFlagsValue with name @name, |
419 | | * or %NULL if there is no flag with that name |
420 | | */ |
421 | | GFlagsValue* |
422 | | g_flags_get_value_by_name (GFlagsClass *flags_class, |
423 | | const gchar *name) |
424 | 0 | { |
425 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
426 | 0 | g_return_val_if_fail (name != NULL, NULL); |
427 | | |
428 | 0 | if (flags_class->n_values) |
429 | 0 | { |
430 | 0 | GFlagsValue *flags_value; |
431 | | |
432 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
433 | 0 | if (strcmp_ignore_case (name, flags_value->value_name) == 0) |
434 | 0 | return flags_value; |
435 | 0 | } |
436 | | |
437 | 0 | return NULL; |
438 | 0 | } |
439 | | |
440 | | /** |
441 | | * g_enum_get_value_by_nick: |
442 | | * @enum_class: a #GEnumClass |
443 | | * @nick: the nickname to look up |
444 | | * |
445 | | * Looks up a #GEnumValue by nickname. |
446 | | * |
447 | | * Returns: (transfer none) (nullable): the #GEnumValue with nickname @nick, |
448 | | * or %NULL if the enumeration doesn't have a member |
449 | | * with that nickname |
450 | | */ |
451 | | GEnumValue* |
452 | | g_enum_get_value_by_nick (GEnumClass *enum_class, |
453 | | const gchar *nick) |
454 | 0 | { |
455 | 0 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
456 | 0 | g_return_val_if_fail (nick != NULL, NULL); |
457 | | |
458 | 0 | if (enum_class->n_values) |
459 | 0 | { |
460 | 0 | GEnumValue *enum_value; |
461 | | |
462 | 0 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
463 | 0 | if (enum_value->value_nick && strcmp_ignore_case (nick, enum_value->value_nick) == 0) |
464 | 0 | return enum_value; |
465 | 0 | } |
466 | | |
467 | 0 | return NULL; |
468 | 0 | } |
469 | | |
470 | | /** |
471 | | * g_flags_get_value_by_nick: |
472 | | * @flags_class: a #GFlagsClass |
473 | | * @nick: the nickname to look up |
474 | | * |
475 | | * Looks up a #GFlagsValue by nickname. |
476 | | * |
477 | | * Returns: (transfer none) (nullable): the #GFlagsValue with nickname @nick, |
478 | | * or %NULL if there is no flag with that nickname |
479 | | */ |
480 | | GFlagsValue* |
481 | | g_flags_get_value_by_nick (GFlagsClass *flags_class, |
482 | | const gchar *nick) |
483 | 0 | { |
484 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
485 | 0 | g_return_val_if_fail (nick != NULL, NULL); |
486 | | |
487 | 0 | if (flags_class->n_values) |
488 | 0 | { |
489 | 0 | GFlagsValue *flags_value; |
490 | | |
491 | 0 | for (flags_value = flags_class->values; flags_value->value_nick; flags_value++) |
492 | 0 | if (flags_value->value_nick && strcmp_ignore_case (nick, flags_value->value_nick) == 0) |
493 | 0 | return flags_value; |
494 | 0 | } |
495 | | |
496 | 0 | return NULL; |
497 | 0 | } |
498 | | |
499 | | /** |
500 | | * g_enum_get_value: |
501 | | * @enum_class: a #GEnumClass |
502 | | * @value: the value to look up |
503 | | * |
504 | | * Returns the #GEnumValue for a value. |
505 | | * |
506 | | * Returns: (transfer none) (nullable): the #GEnumValue for @value, or %NULL |
507 | | * if @value is not a member of the enumeration |
508 | | */ |
509 | | GEnumValue* |
510 | | g_enum_get_value (GEnumClass *enum_class, |
511 | | gint value) |
512 | 0 | { |
513 | 0 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
514 | | |
515 | 0 | if (enum_class->n_values) |
516 | 0 | { |
517 | 0 | GEnumValue *enum_value; |
518 | | |
519 | 0 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
520 | 0 | if (enum_value->value == value) |
521 | 0 | return enum_value; |
522 | 0 | } |
523 | | |
524 | 0 | return NULL; |
525 | 0 | } |
526 | | |
527 | | /** |
528 | | * g_flags_get_first_value: |
529 | | * @flags_class: a #GFlagsClass |
530 | | * @value: the value |
531 | | * |
532 | | * Returns the first #GFlagsValue which is set in @value. |
533 | | * |
534 | | * Returns: (transfer none) (nullable): the first #GFlagsValue which is set in |
535 | | * @value, or %NULL if none is set |
536 | | */ |
537 | | GFlagsValue* |
538 | | g_flags_get_first_value (GFlagsClass *flags_class, |
539 | | guint value) |
540 | 0 | { |
541 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
542 | | |
543 | 0 | if (flags_class->n_values) |
544 | 0 | { |
545 | 0 | GFlagsValue *flags_value; |
546 | |
|
547 | 0 | if (value == 0) |
548 | 0 | { |
549 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
550 | 0 | if (flags_value->value == 0) |
551 | 0 | return flags_value; |
552 | 0 | } |
553 | 0 | else |
554 | 0 | { |
555 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
556 | 0 | if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value) |
557 | 0 | return flags_value; |
558 | 0 | } |
559 | 0 | } |
560 | | |
561 | 0 | return NULL; |
562 | 0 | } |
563 | | |
564 | | /** |
565 | | * g_enum_to_string: |
566 | | * @g_enum_type: the type identifier of a #GEnumClass type |
567 | | * @value: the value |
568 | | * |
569 | | * Pretty-prints @value in the form of the enum’s name. |
570 | | * |
571 | | * This is intended to be used for debugging purposes. The format of the output |
572 | | * may change in the future. |
573 | | * |
574 | | * Returns: (transfer full): a newly-allocated text string |
575 | | * |
576 | | * Since: 2.54 |
577 | | */ |
578 | | gchar * |
579 | | g_enum_to_string (GType g_enum_type, |
580 | | gint value) |
581 | 0 | { |
582 | 0 | gchar *result; |
583 | 0 | GEnumClass *enum_class; |
584 | 0 | GEnumValue *enum_value; |
585 | |
|
586 | 0 | g_return_val_if_fail (G_TYPE_IS_ENUM (g_enum_type), NULL); |
587 | | |
588 | 0 | enum_class = g_type_class_ref (g_enum_type); |
589 | | |
590 | | /* Already warned */ |
591 | 0 | if (enum_class == NULL) |
592 | 0 | return g_strdup_printf ("%d", value); |
593 | | |
594 | 0 | enum_value = g_enum_get_value (enum_class, value); |
595 | |
|
596 | 0 | if (enum_value == NULL) |
597 | 0 | result = g_strdup_printf ("%d", value); |
598 | 0 | else |
599 | 0 | result = g_strdup (enum_value->value_name); |
600 | |
|
601 | 0 | g_type_class_unref (enum_class); |
602 | 0 | return result; |
603 | 0 | } |
604 | | |
605 | | /* |
606 | | * g_flags_get_value_string: |
607 | | * @flags_class: a #GFlagsClass |
608 | | * @value: the value |
609 | | * |
610 | | * Pretty-prints @value in the form of the flag names separated by ` | ` and |
611 | | * sorted. Any extra bits will be shown at the end as a hexadecimal number. |
612 | | * |
613 | | * This is intended to be used for debugging purposes. The format of the output |
614 | | * may change in the future. |
615 | | * |
616 | | * Returns: (transfer full): a newly-allocated text string |
617 | | * |
618 | | * Since: 2.54 |
619 | | */ |
620 | | static gchar * |
621 | | g_flags_get_value_string (GFlagsClass *flags_class, |
622 | | guint value) |
623 | 0 | { |
624 | 0 | GString *str; |
625 | 0 | GFlagsValue *flags_value; |
626 | |
|
627 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
628 | | |
629 | 0 | str = g_string_new (NULL); |
630 | |
|
631 | 0 | while ((str->len == 0 || value != 0) && |
632 | 0 | (flags_value = g_flags_get_first_value (flags_class, value)) != NULL) |
633 | 0 | { |
634 | 0 | if (str->len > 0) |
635 | 0 | g_string_append (str, " | "); |
636 | |
|
637 | 0 | g_string_append (str, flags_value->value_name); |
638 | |
|
639 | 0 | value &= ~flags_value->value; |
640 | 0 | } |
641 | | |
642 | | /* Show the extra bits */ |
643 | 0 | if (value != 0 || str->len == 0) |
644 | 0 | { |
645 | 0 | if (str->len > 0) |
646 | 0 | g_string_append (str, " | "); |
647 | |
|
648 | 0 | g_string_append_printf (str, "0x%x", value); |
649 | 0 | } |
650 | |
|
651 | 0 | return g_string_free (str, FALSE); |
652 | 0 | } |
653 | | |
654 | | /** |
655 | | * g_flags_to_string: |
656 | | * @flags_type: the type identifier of a #GFlagsClass type |
657 | | * @value: the value |
658 | | * |
659 | | * Pretty-prints @value in the form of the flag names separated by ` | ` and |
660 | | * sorted. Any extra bits will be shown at the end as a hexadecimal number. |
661 | | * |
662 | | * This is intended to be used for debugging purposes. The format of the output |
663 | | * may change in the future. |
664 | | * |
665 | | * Returns: (transfer full): a newly-allocated text string |
666 | | * |
667 | | * Since: 2.54 |
668 | | */ |
669 | | gchar * |
670 | | g_flags_to_string (GType flags_type, |
671 | | guint value) |
672 | 0 | { |
673 | 0 | gchar *result; |
674 | 0 | GFlagsClass *flags_class; |
675 | |
|
676 | 0 | g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL); |
677 | | |
678 | 0 | flags_class = g_type_class_ref (flags_type); |
679 | | |
680 | | /* Already warned */ |
681 | 0 | if (flags_class == NULL) |
682 | 0 | return NULL; |
683 | | |
684 | 0 | result = g_flags_get_value_string (flags_class, value); |
685 | |
|
686 | 0 | g_type_class_unref (flags_class); |
687 | 0 | return result; |
688 | 0 | } |
689 | | |
690 | | |
691 | | /** |
692 | | * g_value_set_enum: |
693 | | * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM |
694 | | * @v_enum: enum value to be set |
695 | | * |
696 | | * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum. |
697 | | */ |
698 | | void |
699 | | g_value_set_enum (GValue *value, |
700 | | gint v_enum) |
701 | 0 | { |
702 | 0 | g_return_if_fail (G_VALUE_HOLDS_ENUM (value)); |
703 | | |
704 | 0 | value->data[0].v_long = v_enum; |
705 | 0 | } |
706 | | |
707 | | /** |
708 | | * g_value_get_enum: |
709 | | * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM |
710 | | * |
711 | | * Get the contents of a %G_TYPE_ENUM #GValue. |
712 | | * |
713 | | * Returns: enum contents of @value |
714 | | */ |
715 | | gint |
716 | | g_value_get_enum (const GValue *value) |
717 | 0 | { |
718 | 0 | g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0); |
719 | | |
720 | 0 | return value->data[0].v_long; |
721 | 0 | } |
722 | | |
723 | | /** |
724 | | * g_value_set_flags: |
725 | | * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS |
726 | | * @v_flags: flags value to be set |
727 | | * |
728 | | * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags. |
729 | | */ |
730 | | void |
731 | | g_value_set_flags (GValue *value, |
732 | | guint v_flags) |
733 | 0 | { |
734 | 0 | g_return_if_fail (G_VALUE_HOLDS_FLAGS (value)); |
735 | | |
736 | 0 | value->data[0].v_ulong = v_flags; |
737 | 0 | } |
738 | | |
739 | | /** |
740 | | * g_value_get_flags: |
741 | | * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS |
742 | | * |
743 | | * Get the contents of a %G_TYPE_FLAGS #GValue. |
744 | | * |
745 | | * Returns: flags contents of @value |
746 | | */ |
747 | | guint |
748 | | g_value_get_flags (const GValue *value) |
749 | 0 | { |
750 | 0 | g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0); |
751 | | |
752 | 0 | return value->data[0].v_ulong; |
753 | 0 | } |