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