Coverage Report

Created: 2025-07-01 07:09

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