nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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 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 {
90 static gboolean initialized = FALSE;
91 static const GTypeValueTable flags_enum_value_table = {
92 value_flags_enum_init, /* value_init */
93 NULL, /* value_free */
94 value_flags_enum_copy_value, /* value_copy */
95 NULL, /* value_peek_pointer */
96 "i", /* collect_format */
97 value_flags_enum_collect_value, /* collect_value */
98 "p", /* lcopy_format */
99 value_flags_enum_lcopy_value, /* lcopy_value */
100 };
101 GTypeInfo info = {
102 0, /* class_size */
103 NULL, /* base_init */
104 NULL, /* base_destroy */
105 NULL, /* class_init */
106 NULL, /* class_destroy */
107 NULL, /* class_data */
108 0, /* instance_size */
109 0, /* n_preallocs */
110 NULL, /* instance_init */
111 &flags_enum_value_table, /* value_table */
112 };
113 static const GTypeFundamentalInfo finfo = {
114 G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE,
115 };
116 GType type;
117  
118 g_return_if_fail (initialized == FALSE);
119 initialized = TRUE;
120  
121 /* G_TYPE_ENUM
122 */
123 info.class_size = sizeof (GEnumClass);
124 type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo,
125 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
126 g_assert (type == G_TYPE_ENUM);
127  
128 /* G_TYPE_FLAGS
129 */
130 info.class_size = sizeof (GFlagsClass);
131 type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo,
132 G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
133 g_assert (type == G_TYPE_FLAGS);
134 }
135  
136 static void
137 value_flags_enum_init (GValue *value)
138 {
139 value->data[0].v_long = 0;
140 }
141  
142 static void
143 value_flags_enum_copy_value (const GValue *src_value,
144 GValue *dest_value)
145 {
146 dest_value->data[0].v_long = src_value->data[0].v_long;
147 }
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 {
155 value->data[0].v_long = collect_values[0].v_int;
156  
157 return NULL;
158 }
159  
160 static gchar*
161 value_flags_enum_lcopy_value (const GValue *value,
162 guint n_collect_values,
163 GTypeCValue *collect_values,
164 guint collect_flags)
165 {
166 gint *int_p = collect_values[0].v_pointer;
167  
168 if (!int_p)
169 return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
170  
171 *int_p = value->data[0].v_long;
172  
173 return NULL;
174 }
175  
176 /**
177 * g_enum_register_static:
178 * @name: A nul-terminated string used as the name of the new type.
179 * @const_static_values: An array of #GEnumValue structs for the possible
180 * enumeration values. The array is terminated by a struct with all
181 * members being 0. GObject keeps a reference to the data, so it cannot
182 * be stack-allocated.
183 *
184 * Registers a new static enumeration type with the name @name.
185 *
186 * It is normally more convenient to let [glib-mkenums][glib-mkenums],
187 * generate a my_enum_get_type() function from a usual C enumeration
188 * definition than to write one yourself using g_enum_register_static().
189 *
190 * Returns: The new type identifier.
191 */
192 GType
193 g_enum_register_static (const gchar *name,
194 const GEnumValue *const_static_values)
195 {
196 GTypeInfo enum_type_info = {
197 sizeof (GEnumClass), /* class_size */
198 NULL, /* base_init */
199 NULL, /* base_finalize */
200 (GClassInitFunc) g_enum_class_init,
201 NULL, /* class_finalize */
202 NULL, /* class_data */
203 0, /* instance_size */
204 0, /* n_preallocs */
205 NULL, /* instance_init */
206 NULL, /* value_table */
207 };
208 GType type;
209  
210 g_return_val_if_fail (name != NULL, 0);
211 g_return_val_if_fail (const_static_values != NULL, 0);
212  
213 enum_type_info.class_data = const_static_values;
214  
215 type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
216  
217 return type;
218 }
219  
220 /**
221 * g_flags_register_static:
222 * @name: A nul-terminated string used as the name of the new type.
223 * @const_static_values: An array of #GFlagsValue structs for the possible
224 * flags values. The array is terminated by a struct with all members being 0.
225 * GObject keeps a reference to the data, so it cannot be stack-allocated.
226 *
227 * Registers a new static flags type with the name @name.
228 *
229 * It is normally more convenient to let [glib-mkenums][glib-mkenums]
230 * generate a my_flags_get_type() function from a usual C enumeration
231 * definition than to write one yourself using g_flags_register_static().
232 *
233 * Returns: The new type identifier.
234 */
235 GType
236 g_flags_register_static (const gchar *name,
237 const GFlagsValue *const_static_values)
238 {
239 GTypeInfo flags_type_info = {
240 sizeof (GFlagsClass), /* class_size */
241 NULL, /* base_init */
242 NULL, /* base_finalize */
243 (GClassInitFunc) g_flags_class_init,
244 NULL, /* class_finalize */
245 NULL, /* class_data */
246 0, /* instance_size */
247 0, /* n_preallocs */
248 NULL, /* instance_init */
249 NULL, /* value_table */
250 };
251 GType type;
252  
253 g_return_val_if_fail (name != NULL, 0);
254 g_return_val_if_fail (const_static_values != NULL, 0);
255  
256 flags_type_info.class_data = const_static_values;
257  
258 type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
259  
260 return type;
261 }
262  
263 /**
264 * g_enum_complete_type_info:
265 * @g_enum_type: the type identifier of the type being completed
266 * @info: (out callee-allocates): the #GTypeInfo struct to be filled in
267 * @const_values: An array of #GEnumValue structs for the possible
268 * enumeration values. The array is terminated by a struct with all
269 * members being 0.
270 *
271 * This function is meant to be called from the `complete_type_info`
272 * function of a #GTypePlugin implementation, as in the following
273 * example:
274 *
275 * |[<!-- language="C" -->
276 * static void
277 * my_enum_complete_type_info (GTypePlugin *plugin,
278 * GType g_type,
279 * GTypeInfo *info,
280 * GTypeValueTable *value_table)
281 * {
282 * static const GEnumValue values[] = {
283 * { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
284 * { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
285 * { 0, NULL, NULL }
286 * };
287 *
288 * g_enum_complete_type_info (type, info, values);
289 * }
290 * ]|
291 */
292 void
293 g_enum_complete_type_info (GType g_enum_type,
294 GTypeInfo *info,
295 const GEnumValue *const_values)
296 {
297 g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
298 g_return_if_fail (info != NULL);
299 g_return_if_fail (const_values != NULL);
300  
301 info->class_size = sizeof (GEnumClass);
302 info->base_init = NULL;
303 info->base_finalize = NULL;
304 info->class_init = (GClassInitFunc) g_enum_class_init;
305 info->class_finalize = NULL;
306 info->class_data = const_values;
307 }
308  
309 /**
310 * g_flags_complete_type_info:
311 * @g_flags_type: the type identifier of the type being completed
312 * @info: (out callee-allocates): the #GTypeInfo struct to be filled in
313 * @const_values: An array of #GFlagsValue structs for the possible
314 * enumeration values. The array is terminated by a struct with all
315 * members being 0.
316 *
317 * This function is meant to be called from the complete_type_info()
318 * function of a #GTypePlugin implementation, see the example for
319 * g_enum_complete_type_info() above.
320 */
321 void
322 g_flags_complete_type_info (GType g_flags_type,
323 GTypeInfo *info,
324 const GFlagsValue *const_values)
325 {
326 g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
327 g_return_if_fail (info != NULL);
328 g_return_if_fail (const_values != NULL);
329  
330 info->class_size = sizeof (GFlagsClass);
331 info->base_init = NULL;
332 info->base_finalize = NULL;
333 info->class_init = (GClassInitFunc) g_flags_class_init;
334 info->class_finalize = NULL;
335 info->class_data = const_values;
336 }
337  
338 static void
339 g_enum_class_init (GEnumClass *class,
340 gpointer class_data)
341 {
342 g_return_if_fail (G_IS_ENUM_CLASS (class));
343  
344 class->minimum = 0;
345 class->maximum = 0;
346 class->n_values = 0;
347 class->values = class_data;
348  
349 if (class->values)
350 {
351 GEnumValue *values;
352  
353 class->minimum = class->values->value;
354 class->maximum = class->values->value;
355 for (values = class->values; values->value_name; values++)
356 {
357 class->minimum = MIN (class->minimum, values->value);
358 class->maximum = MAX (class->maximum, values->value);
359 class->n_values++;
360 }
361 }
362 }
363  
364 static void
365 g_flags_class_init (GFlagsClass *class,
366 gpointer class_data)
367 {
368 g_return_if_fail (G_IS_FLAGS_CLASS (class));
369  
370 class->mask = 0;
371 class->n_values = 0;
372 class->values = class_data;
373  
374 if (class->values)
375 {
376 GFlagsValue *values;
377  
378 for (values = class->values; values->value_name; values++)
379 {
380 class->mask |= values->value;
381 class->n_values++;
382 }
383 }
384 }
385  
386 /**
387 * g_enum_get_value_by_name:
388 * @enum_class: a #GEnumClass
389 * @name: the name to look up
390 *
391 * Looks up a #GEnumValue by name.
392 *
393 * Returns: (transfer none): the #GEnumValue with name @name,
394 * or %NULL if the enumeration doesn't have a member
395 * with that name
396 */
397 GEnumValue*
398 g_enum_get_value_by_name (GEnumClass *enum_class,
399 const gchar *name)
400 {
401 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
402 g_return_val_if_fail (name != NULL, NULL);
403  
404 if (enum_class->n_values)
405 {
406 GEnumValue *enum_value;
407  
408 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
409 if (strcmp (name, enum_value->value_name) == 0)
410 return enum_value;
411 }
412  
413 return NULL;
414 }
415  
416 /**
417 * g_flags_get_value_by_name:
418 * @flags_class: a #GFlagsClass
419 * @name: the name to look up
420 *
421 * Looks up a #GFlagsValue by name.
422 *
423 * Returns: (transfer none): the #GFlagsValue with name @name,
424 * or %NULL if there is no flag with that name
425 */
426 GFlagsValue*
427 g_flags_get_value_by_name (GFlagsClass *flags_class,
428 const gchar *name)
429 {
430 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
431 g_return_val_if_fail (name != NULL, NULL);
432  
433 if (flags_class->n_values)
434 {
435 GFlagsValue *flags_value;
436  
437 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
438 if (strcmp (name, flags_value->value_name) == 0)
439 return flags_value;
440 }
441  
442 return NULL;
443 }
444  
445 /**
446 * g_enum_get_value_by_nick:
447 * @enum_class: a #GEnumClass
448 * @nick: the nickname to look up
449 *
450 * Looks up a #GEnumValue by nickname.
451 *
452 * Returns: (transfer none): the #GEnumValue with nickname @nick,
453 * or %NULL if the enumeration doesn't have a member
454 * with that nickname
455 */
456 GEnumValue*
457 g_enum_get_value_by_nick (GEnumClass *enum_class,
458 const gchar *nick)
459 {
460 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
461 g_return_val_if_fail (nick != NULL, NULL);
462  
463 if (enum_class->n_values)
464 {
465 GEnumValue *enum_value;
466  
467 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
468 if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
469 return enum_value;
470 }
471  
472 return NULL;
473 }
474  
475 /**
476 * g_flags_get_value_by_nick:
477 * @flags_class: a #GFlagsClass
478 * @nick: the nickname to look up
479 *
480 * Looks up a #GFlagsValue by nickname.
481 *
482 * Returns: (transfer none): the #GFlagsValue with nickname @nick,
483 * or %NULL if there is no flag with that nickname
484 */
485 GFlagsValue*
486 g_flags_get_value_by_nick (GFlagsClass *flags_class,
487 const gchar *nick)
488 {
489 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
490 g_return_val_if_fail (nick != NULL, NULL);
491  
492 if (flags_class->n_values)
493 {
494 GFlagsValue *flags_value;
495  
496 for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
497 if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
498 return flags_value;
499 }
500  
501 return NULL;
502 }
503  
504 /**
505 * g_enum_get_value:
506 * @enum_class: a #GEnumClass
507 * @value: the value to look up
508 *
509 * Returns the #GEnumValue for a value.
510 *
511 * Returns: (transfer none): the #GEnumValue for @value, or %NULL
512 * if @value is not a member of the enumeration
513 */
514 GEnumValue*
515 g_enum_get_value (GEnumClass *enum_class,
516 gint value)
517 {
518 g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
519  
520 if (enum_class->n_values)
521 {
522 GEnumValue *enum_value;
523  
524 for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
525 if (enum_value->value == value)
526 return enum_value;
527 }
528  
529 return NULL;
530 }
531  
532 /**
533 * g_flags_get_first_value:
534 * @flags_class: a #GFlagsClass
535 * @value: the value
536 *
537 * Returns the first #GFlagsValue which is set in @value.
538 *
539 * Returns: (transfer none): the first #GFlagsValue which is set in
540 * @value, or %NULL if none is set
541 */
542 GFlagsValue*
543 g_flags_get_first_value (GFlagsClass *flags_class,
544 guint value)
545 {
546 g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
547  
548 if (flags_class->n_values)
549 {
550 GFlagsValue *flags_value;
551  
552 if (value == 0)
553 {
554 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
555 if (flags_value->value == 0)
556 return flags_value;
557 }
558 else
559 {
560 for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
561 if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
562 return flags_value;
563 }
564 }
565  
566 return NULL;
567 }
568  
569 /**
570 * g_value_set_enum:
571 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
572 * @v_enum: enum value to be set
573 *
574 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
575 */
576 void
577 g_value_set_enum (GValue *value,
578 gint v_enum)
579 {
580 g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
581  
582 value->data[0].v_long = v_enum;
583 }
584  
585 /**
586 * g_value_get_enum:
587 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
588 *
589 * Get the contents of a %G_TYPE_ENUM #GValue.
590 *
591 * Returns: enum contents of @value
592 */
593 gint
594 g_value_get_enum (const GValue *value)
595 {
596 g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
597  
598 return value->data[0].v_long;
599 }
600  
601 /**
602 * g_value_set_flags:
603 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
604 * @v_flags: flags value to be set
605 *
606 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
607 */
608 void
609 g_value_set_flags (GValue *value,
610 guint v_flags)
611 {
612 g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
613  
614 value->data[0].v_ulong = v_flags;
615 }
616  
617 /**
618 * g_value_get_flags:
619 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
620 *
621 * Get the contents of a %G_TYPE_FLAGS #GValue.
622 *
623 * Returns: flags contents of @value
624 */
625 guint
626 g_value_get_flags (const GValue *value)
627 {
628 g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
629  
630 return value->data[0].v_ulong;
631 }