/src/pango/subprojects/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 | | * 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 | 12 | { |
55 | 12 | static gboolean initialized = FALSE; |
56 | 12 | static const GTypeValueTable flags_enum_value_table = { |
57 | 12 | value_flags_enum_init, /* value_init */ |
58 | 12 | NULL, /* value_free */ |
59 | 12 | value_flags_enum_copy_value, /* value_copy */ |
60 | 12 | NULL, /* value_peek_pointer */ |
61 | 12 | "i", /* collect_format */ |
62 | 12 | value_flags_enum_collect_value, /* collect_value */ |
63 | 12 | "p", /* lcopy_format */ |
64 | 12 | value_flags_enum_lcopy_value, /* lcopy_value */ |
65 | 12 | }; |
66 | 12 | GTypeInfo info = { |
67 | 12 | 0, /* class_size */ |
68 | 12 | NULL, /* base_init */ |
69 | 12 | NULL, /* base_destroy */ |
70 | 12 | NULL, /* class_init */ |
71 | 12 | NULL, /* class_destroy */ |
72 | 12 | NULL, /* class_data */ |
73 | 12 | 0, /* instance_size */ |
74 | 12 | 0, /* n_preallocs */ |
75 | 12 | NULL, /* instance_init */ |
76 | 12 | &flags_enum_value_table, /* value_table */ |
77 | 12 | }; |
78 | 12 | static const GTypeFundamentalInfo finfo = { |
79 | 12 | G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE, |
80 | 12 | }; |
81 | 12 | GType type G_GNUC_UNUSED /* when compiling with G_DISABLE_ASSERT */; |
82 | | |
83 | 12 | g_return_if_fail (initialized == FALSE); |
84 | 12 | initialized = TRUE; |
85 | | |
86 | | /* G_TYPE_ENUM |
87 | | */ |
88 | 12 | info.class_size = sizeof (GEnumClass); |
89 | 12 | type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo, |
90 | 12 | G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT); |
91 | 12 | g_assert (type == G_TYPE_ENUM); |
92 | | |
93 | | /* G_TYPE_FLAGS |
94 | | */ |
95 | 12 | info.class_size = sizeof (GFlagsClass); |
96 | 12 | type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo, |
97 | 12 | G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT); |
98 | 12 | g_assert (type == G_TYPE_FLAGS); |
99 | 12 | } |
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 | 35 | { |
163 | 35 | GTypeInfo enum_type_info = { |
164 | 35 | sizeof (GEnumClass), /* class_size */ |
165 | 35 | NULL, /* base_init */ |
166 | 35 | NULL, /* base_finalize */ |
167 | 35 | (GClassInitFunc) g_enum_class_init, |
168 | 35 | NULL, /* class_finalize */ |
169 | 35 | NULL, /* class_data */ |
170 | 35 | 0, /* instance_size */ |
171 | 35 | 0, /* n_preallocs */ |
172 | 35 | NULL, /* instance_init */ |
173 | 35 | NULL, /* value_table */ |
174 | 35 | }; |
175 | 35 | GType type; |
176 | | |
177 | 35 | g_return_val_if_fail (name != NULL, 0); |
178 | 35 | g_return_val_if_fail (const_static_values != NULL, 0); |
179 | | |
180 | 35 | enum_type_info.class_data = const_static_values; |
181 | | |
182 | 35 | type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0); |
183 | | |
184 | 35 | return type; |
185 | 35 | } |
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 | 10 | { |
207 | 10 | GTypeInfo flags_type_info = { |
208 | 10 | sizeof (GFlagsClass), /* class_size */ |
209 | 10 | NULL, /* base_init */ |
210 | 10 | NULL, /* base_finalize */ |
211 | 10 | (GClassInitFunc) g_flags_class_init, |
212 | 10 | NULL, /* class_finalize */ |
213 | 10 | NULL, /* class_data */ |
214 | 10 | 0, /* instance_size */ |
215 | 10 | 0, /* n_preallocs */ |
216 | 10 | NULL, /* instance_init */ |
217 | 10 | NULL, /* value_table */ |
218 | 10 | }; |
219 | 10 | GType type; |
220 | | |
221 | 10 | g_return_val_if_fail (name != NULL, 0); |
222 | 10 | g_return_val_if_fail (const_static_values != NULL, 0); |
223 | | |
224 | 10 | flags_type_info.class_data = const_static_values; |
225 | | |
226 | 10 | type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0); |
227 | | |
228 | 10 | return type; |
229 | 10 | } |
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 | 35 | { |
310 | 35 | g_return_if_fail (G_IS_ENUM_CLASS (class)); |
311 | | |
312 | 35 | class->minimum = 0; |
313 | 35 | class->maximum = 0; |
314 | 35 | class->n_values = 0; |
315 | 35 | class->values = class_data; |
316 | | |
317 | 35 | if (class->values) |
318 | 35 | { |
319 | 35 | GEnumValue *values; |
320 | | |
321 | 35 | class->minimum = class->values->value; |
322 | 35 | class->maximum = class->values->value; |
323 | 265 | for (values = class->values; values->value_name; values++) |
324 | 230 | { |
325 | 230 | class->minimum = MIN (class->minimum, values->value); |
326 | 230 | class->maximum = MAX (class->maximum, values->value); |
327 | 230 | class->n_values++; |
328 | 230 | } |
329 | 35 | } |
330 | 35 | } |
331 | | |
332 | | static void |
333 | | g_flags_class_init (GFlagsClass *class, |
334 | | gpointer class_data) |
335 | 10 | { |
336 | 10 | g_return_if_fail (G_IS_FLAGS_CLASS (class)); |
337 | | |
338 | 10 | class->mask = 0; |
339 | 10 | class->n_values = 0; |
340 | 10 | class->values = class_data; |
341 | | |
342 | 10 | if (class->values) |
343 | 10 | { |
344 | 10 | GFlagsValue *values; |
345 | | |
346 | 95 | for (values = class->values; values->value_name; values++) |
347 | 85 | { |
348 | 85 | class->mask |= values->value; |
349 | 85 | class->n_values++; |
350 | 85 | } |
351 | 10 | } |
352 | 10 | } |
353 | | |
354 | | /** |
355 | | * g_enum_get_value_by_name: |
356 | | * @enum_class: a #GEnumClass |
357 | | * @name: the name to look up |
358 | | * |
359 | | * Looks up a #GEnumValue by name. |
360 | | * |
361 | | * Returns: (transfer none) (nullable): the #GEnumValue with name @name, |
362 | | * or %NULL if the enumeration doesn't have a member |
363 | | * with that name |
364 | | */ |
365 | | GEnumValue* |
366 | | g_enum_get_value_by_name (GEnumClass *enum_class, |
367 | | const gchar *name) |
368 | 0 | { |
369 | 0 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
370 | 0 | g_return_val_if_fail (name != NULL, NULL); |
371 | | |
372 | 0 | if (enum_class->n_values) |
373 | 0 | { |
374 | 0 | GEnumValue *enum_value; |
375 | | |
376 | 0 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
377 | 0 | if (strcmp (name, enum_value->value_name) == 0) |
378 | 0 | return enum_value; |
379 | 0 | } |
380 | | |
381 | 0 | return NULL; |
382 | 0 | } |
383 | | |
384 | | /** |
385 | | * g_flags_get_value_by_name: |
386 | | * @flags_class: a #GFlagsClass |
387 | | * @name: the name to look up |
388 | | * |
389 | | * Looks up a #GFlagsValue by name. |
390 | | * |
391 | | * Returns: (transfer none) (nullable): the #GFlagsValue with name @name, |
392 | | * or %NULL if there is no flag with that name |
393 | | */ |
394 | | GFlagsValue* |
395 | | g_flags_get_value_by_name (GFlagsClass *flags_class, |
396 | | const gchar *name) |
397 | 0 | { |
398 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
399 | 0 | g_return_val_if_fail (name != NULL, NULL); |
400 | | |
401 | 0 | if (flags_class->n_values) |
402 | 0 | { |
403 | 0 | GFlagsValue *flags_value; |
404 | | |
405 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
406 | 0 | if (strcmp (name, flags_value->value_name) == 0) |
407 | 0 | return flags_value; |
408 | 0 | } |
409 | | |
410 | 0 | return NULL; |
411 | 0 | } |
412 | | |
413 | | /** |
414 | | * g_enum_get_value_by_nick: |
415 | | * @enum_class: a #GEnumClass |
416 | | * @nick: the nickname to look up |
417 | | * |
418 | | * Looks up a #GEnumValue by nickname. |
419 | | * |
420 | | * Returns: (transfer none) (nullable): the #GEnumValue with nickname @nick, |
421 | | * or %NULL if the enumeration doesn't have a member |
422 | | * with that nickname |
423 | | */ |
424 | | GEnumValue* |
425 | | g_enum_get_value_by_nick (GEnumClass *enum_class, |
426 | | const gchar *nick) |
427 | 0 | { |
428 | 0 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
429 | 0 | g_return_val_if_fail (nick != NULL, NULL); |
430 | | |
431 | 0 | if (enum_class->n_values) |
432 | 0 | { |
433 | 0 | GEnumValue *enum_value; |
434 | | |
435 | 0 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
436 | 0 | if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0) |
437 | 0 | return enum_value; |
438 | 0 | } |
439 | | |
440 | 0 | return NULL; |
441 | 0 | } |
442 | | |
443 | | /** |
444 | | * g_flags_get_value_by_nick: |
445 | | * @flags_class: a #GFlagsClass |
446 | | * @nick: the nickname to look up |
447 | | * |
448 | | * Looks up a #GFlagsValue by nickname. |
449 | | * |
450 | | * Returns: (transfer none) (nullable): the #GFlagsValue with nickname @nick, |
451 | | * or %NULL if there is no flag with that nickname |
452 | | */ |
453 | | GFlagsValue* |
454 | | g_flags_get_value_by_nick (GFlagsClass *flags_class, |
455 | | const gchar *nick) |
456 | 0 | { |
457 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
458 | 0 | g_return_val_if_fail (nick != NULL, NULL); |
459 | | |
460 | 0 | if (flags_class->n_values) |
461 | 0 | { |
462 | 0 | GFlagsValue *flags_value; |
463 | | |
464 | 0 | for (flags_value = flags_class->values; flags_value->value_nick; flags_value++) |
465 | 0 | if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0) |
466 | 0 | return flags_value; |
467 | 0 | } |
468 | | |
469 | 0 | return NULL; |
470 | 0 | } |
471 | | |
472 | | /** |
473 | | * g_enum_get_value: |
474 | | * @enum_class: a #GEnumClass |
475 | | * @value: the value to look up |
476 | | * |
477 | | * Returns the #GEnumValue for a value. |
478 | | * |
479 | | * Returns: (transfer none) (nullable): the #GEnumValue for @value, or %NULL |
480 | | * if @value is not a member of the enumeration |
481 | | */ |
482 | | GEnumValue* |
483 | | g_enum_get_value (GEnumClass *enum_class, |
484 | | gint value) |
485 | 35 | { |
486 | 35 | g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL); |
487 | | |
488 | 35 | if (enum_class->n_values) |
489 | 35 | { |
490 | 35 | GEnumValue *enum_value; |
491 | | |
492 | 35 | for (enum_value = enum_class->values; enum_value->value_name; enum_value++) |
493 | 35 | if (enum_value->value == value) |
494 | 35 | return enum_value; |
495 | 35 | } |
496 | | |
497 | 0 | return NULL; |
498 | 35 | } |
499 | | |
500 | | /** |
501 | | * g_flags_get_first_value: |
502 | | * @flags_class: a #GFlagsClass |
503 | | * @value: the value |
504 | | * |
505 | | * Returns the first #GFlagsValue which is set in @value. |
506 | | * |
507 | | * Returns: (transfer none) (nullable): the first #GFlagsValue which is set in |
508 | | * @value, or %NULL if none is set |
509 | | */ |
510 | | GFlagsValue* |
511 | | g_flags_get_first_value (GFlagsClass *flags_class, |
512 | | guint value) |
513 | 0 | { |
514 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
515 | | |
516 | 0 | if (flags_class->n_values) |
517 | 0 | { |
518 | 0 | GFlagsValue *flags_value; |
519 | |
|
520 | 0 | if (value == 0) |
521 | 0 | { |
522 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
523 | 0 | if (flags_value->value == 0) |
524 | 0 | return flags_value; |
525 | 0 | } |
526 | 0 | else |
527 | 0 | { |
528 | 0 | for (flags_value = flags_class->values; flags_value->value_name; flags_value++) |
529 | 0 | if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value) |
530 | 0 | return flags_value; |
531 | 0 | } |
532 | 0 | } |
533 | | |
534 | 0 | return NULL; |
535 | 0 | } |
536 | | |
537 | | /** |
538 | | * g_enum_to_string: |
539 | | * @g_enum_type: the type identifier of a #GEnumClass type |
540 | | * @value: the value |
541 | | * |
542 | | * Pretty-prints @value in the form of the enum’s name. |
543 | | * |
544 | | * This is intended to be used for debugging purposes. The format of the output |
545 | | * may change in the future. |
546 | | * |
547 | | * Returns: (transfer full): a newly-allocated text string |
548 | | * |
549 | | * Since: 2.54 |
550 | | */ |
551 | | gchar * |
552 | | g_enum_to_string (GType g_enum_type, |
553 | | gint value) |
554 | 0 | { |
555 | 0 | gchar *result; |
556 | 0 | GEnumClass *enum_class; |
557 | 0 | GEnumValue *enum_value; |
558 | |
|
559 | 0 | g_return_val_if_fail (G_TYPE_IS_ENUM (g_enum_type), NULL); |
560 | | |
561 | 0 | enum_class = g_type_class_ref (g_enum_type); |
562 | | |
563 | | /* Already warned */ |
564 | 0 | if (enum_class == NULL) |
565 | 0 | return g_strdup_printf ("%d", value); |
566 | | |
567 | 0 | enum_value = g_enum_get_value (enum_class, value); |
568 | |
|
569 | 0 | if (enum_value == NULL) |
570 | 0 | result = g_strdup_printf ("%d", value); |
571 | 0 | else |
572 | 0 | result = g_strdup (enum_value->value_name); |
573 | |
|
574 | 0 | g_type_class_unref (enum_class); |
575 | 0 | return result; |
576 | 0 | } |
577 | | |
578 | | /* |
579 | | * g_flags_get_value_string: |
580 | | * @flags_class: a #GFlagsClass |
581 | | * @value: the value |
582 | | * |
583 | | * Pretty-prints @value in the form of the flag names separated by ` | ` and |
584 | | * sorted. Any extra bits will be shown at the end as a hexadecimal number. |
585 | | * |
586 | | * This is intended to be used for debugging purposes. The format of the output |
587 | | * may change in the future. |
588 | | * |
589 | | * Returns: (transfer full): a newly-allocated text string |
590 | | * |
591 | | * Since: 2.54 |
592 | | */ |
593 | | static gchar * |
594 | | g_flags_get_value_string (GFlagsClass *flags_class, |
595 | | guint value) |
596 | 0 | { |
597 | 0 | GString *str; |
598 | 0 | GFlagsValue *flags_value; |
599 | |
|
600 | 0 | g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL); |
601 | | |
602 | 0 | str = g_string_new (NULL); |
603 | |
|
604 | 0 | while ((str->len == 0 || value != 0) && |
605 | 0 | (flags_value = g_flags_get_first_value (flags_class, value)) != NULL) |
606 | 0 | { |
607 | 0 | if (str->len > 0) |
608 | 0 | g_string_append (str, " | "); |
609 | |
|
610 | 0 | g_string_append (str, flags_value->value_name); |
611 | |
|
612 | 0 | value &= ~flags_value->value; |
613 | 0 | } |
614 | | |
615 | | /* Show the extra bits */ |
616 | 0 | if (value != 0 || str->len == 0) |
617 | 0 | { |
618 | 0 | if (str->len > 0) |
619 | 0 | g_string_append (str, " | "); |
620 | |
|
621 | 0 | g_string_append_printf (str, "0x%x", value); |
622 | 0 | } |
623 | |
|
624 | 0 | return g_string_free (str, FALSE); |
625 | 0 | } |
626 | | |
627 | | /** |
628 | | * g_flags_to_string: |
629 | | * @flags_type: the type identifier of a #GFlagsClass type |
630 | | * @value: the value |
631 | | * |
632 | | * Pretty-prints @value in the form of the flag names separated by ` | ` and |
633 | | * sorted. Any extra bits will be shown at the end as a hexadecimal number. |
634 | | * |
635 | | * This is intended to be used for debugging purposes. The format of the output |
636 | | * may change in the future. |
637 | | * |
638 | | * Returns: (transfer full): a newly-allocated text string |
639 | | * |
640 | | * Since: 2.54 |
641 | | */ |
642 | | gchar * |
643 | | g_flags_to_string (GType flags_type, |
644 | | guint value) |
645 | 0 | { |
646 | 0 | gchar *result; |
647 | 0 | GFlagsClass *flags_class; |
648 | |
|
649 | 0 | g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL); |
650 | | |
651 | 0 | flags_class = g_type_class_ref (flags_type); |
652 | | |
653 | | /* Already warned */ |
654 | 0 | if (flags_class == NULL) |
655 | 0 | return NULL; |
656 | | |
657 | 0 | result = g_flags_get_value_string (flags_class, value); |
658 | |
|
659 | 0 | g_type_class_unref (flags_class); |
660 | 0 | return result; |
661 | 0 | } |
662 | | |
663 | | |
664 | | /** |
665 | | * g_value_set_enum: |
666 | | * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM |
667 | | * @v_enum: enum value to be set |
668 | | * |
669 | | * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum. |
670 | | */ |
671 | | void |
672 | | g_value_set_enum (GValue *value, |
673 | | gint v_enum) |
674 | 0 | { |
675 | 0 | g_return_if_fail (G_VALUE_HOLDS_ENUM (value)); |
676 | | |
677 | 0 | value->data[0].v_long = v_enum; |
678 | 0 | } |
679 | | |
680 | | /** |
681 | | * g_value_get_enum: |
682 | | * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM |
683 | | * |
684 | | * Get the contents of a %G_TYPE_ENUM #GValue. |
685 | | * |
686 | | * Returns: enum contents of @value |
687 | | */ |
688 | | gint |
689 | | g_value_get_enum (const GValue *value) |
690 | 0 | { |
691 | 0 | g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0); |
692 | | |
693 | 0 | return value->data[0].v_long; |
694 | 0 | } |
695 | | |
696 | | /** |
697 | | * g_value_set_flags: |
698 | | * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS |
699 | | * @v_flags: flags value to be set |
700 | | * |
701 | | * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags. |
702 | | */ |
703 | | void |
704 | | g_value_set_flags (GValue *value, |
705 | | guint v_flags) |
706 | 0 | { |
707 | 0 | g_return_if_fail (G_VALUE_HOLDS_FLAGS (value)); |
708 | | |
709 | 0 | value->data[0].v_ulong = v_flags; |
710 | 0 | } |
711 | | |
712 | | /** |
713 | | * g_value_get_flags: |
714 | | * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS |
715 | | * |
716 | | * Get the contents of a %G_TYPE_FLAGS #GValue. |
717 | | * |
718 | | * Returns: flags contents of @value |
719 | | */ |
720 | | guint |
721 | | g_value_get_flags (const GValue *value) |
722 | 0 | { |
723 | 0 | g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0); |
724 | | |
725 | 0 | return value->data[0].v_ulong; |
726 | 0 | } |