Blame gobject/genums.c

Packit ae235b
/* GObject - GLib Type, Object, Parameter and Signal Library
Packit ae235b
 * Copyright (C) 1998-1999, 2000-2001 Tim Janik and Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "genums.h"
Packit ae235b
#include "gtype-private.h"
Packit ae235b
#include "gvalue.h"
Packit ae235b
#include "gvaluecollector.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:enumerations_flags
Packit ae235b
 * @short_description: Enumeration and flags types
Packit ae235b
 * @title: Enumeration and Flag Types
Packit ae235b
 * @see_also:#GParamSpecEnum, #GParamSpecFlags, g_param_spec_enum(),
Packit ae235b
 * g_param_spec_flags()
Packit ae235b
 *
Packit ae235b
 * The GLib type system provides fundamental types for enumeration and
Packit ae235b
 * flags types. (Flags types are like enumerations, but allow their
Packit ae235b
 * values to be combined by bitwise or). A registered enumeration or
Packit ae235b
 * flags type associates a name and a nickname with each allowed
Packit ae235b
 * value, and the methods g_enum_get_value_by_name(),
Packit ae235b
 * g_enum_get_value_by_nick(), g_flags_get_value_by_name() and
Packit ae235b
 * g_flags_get_value_by_nick() can look up values by their name or
Packit ae235b
 * nickname.  When an enumeration or flags type is registered with the
Packit ae235b
 * GLib type system, it can be used as value type for object
Packit ae235b
 * properties, using g_param_spec_enum() or g_param_spec_flags().
Packit ae235b
 *
Packit ae235b
 * GObject ships with a utility called [glib-mkenums][glib-mkenums],
Packit ae235b
 * that can construct suitable type registration functions from C enumeration
Packit ae235b
 * definitions.
Packit ae235b
 *
Packit ae235b
 * Example of how to get a string representation of an enum value:
Packit ae235b
 * |[
Packit ae235b
 * GEnumClass *enum_class;
Packit ae235b
 * GEnumValue *enum_value;
Packit ae235b
 *
Packit ae235b
 * enum_class = g_type_class_ref (MAMAN_TYPE_MY_ENUM);
Packit ae235b
 * enum_value = g_enum_get_value (enum_class, MAMAN_MY_ENUM_FOO);
Packit ae235b
 *
Packit ae235b
 * g_print ("Name: %s\n", enum_value->value_name);
Packit ae235b
 *
Packit ae235b
 * g_type_class_unref (enum_class);
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
Packit ae235b
/* --- prototypes --- */
Packit ae235b
static void	g_enum_class_init		(GEnumClass	*class,
Packit ae235b
						 gpointer	 class_data);
Packit ae235b
static void	g_flags_class_init		(GFlagsClass	*class,
Packit ae235b
						 gpointer	 class_data);
Packit ae235b
static void	value_flags_enum_init		(GValue		*value);
Packit ae235b
static void	value_flags_enum_copy_value	(const GValue	*src_value,
Packit ae235b
						 GValue		*dest_value);
Packit ae235b
static gchar*	value_flags_enum_collect_value  (GValue		*value,
Packit ae235b
						 guint           n_collect_values,
Packit ae235b
						 GTypeCValue    *collect_values,
Packit ae235b
						 guint           collect_flags);
Packit ae235b
static gchar*	value_flags_enum_lcopy_value	(const GValue	*value,
Packit ae235b
						 guint           n_collect_values,
Packit ae235b
						 GTypeCValue    *collect_values,
Packit ae235b
						 guint           collect_flags);
Packit ae235b
Packit ae235b
/* --- functions --- */
Packit ae235b
void
Packit ae235b
_g_enum_types_init (void)
Packit ae235b
{
Packit ae235b
  static gboolean initialized = FALSE;
Packit ae235b
  static const GTypeValueTable flags_enum_value_table = {
Packit ae235b
    value_flags_enum_init,	    /* value_init */
Packit ae235b
    NULL,			    /* value_free */
Packit ae235b
    value_flags_enum_copy_value,    /* value_copy */
Packit ae235b
    NULL,			    /* value_peek_pointer */
Packit ae235b
    "i",			    /* collect_format */
Packit ae235b
    value_flags_enum_collect_value, /* collect_value */
Packit ae235b
    "p",			    /* lcopy_format */
Packit ae235b
    value_flags_enum_lcopy_value,   /* lcopy_value */
Packit ae235b
  };
Packit ae235b
  GTypeInfo info = {
Packit ae235b
    0,                          /* class_size */
Packit ae235b
    NULL,                       /* base_init */
Packit ae235b
    NULL,                       /* base_destroy */
Packit ae235b
    NULL,                       /* class_init */
Packit ae235b
    NULL,                       /* class_destroy */
Packit ae235b
    NULL,                       /* class_data */
Packit ae235b
    0,                          /* instance_size */
Packit ae235b
    0,                          /* n_preallocs */
Packit ae235b
    NULL,                       /* instance_init */
Packit ae235b
    &flags_enum_value_table,    /* value_table */
Packit ae235b
  };
Packit ae235b
  static const GTypeFundamentalInfo finfo = {
Packit ae235b
    G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_DERIVABLE,
Packit ae235b
  };
Packit ae235b
  GType type;
Packit ae235b
  
Packit ae235b
  g_return_if_fail (initialized == FALSE);
Packit ae235b
  initialized = TRUE;
Packit ae235b
  
Packit ae235b
  /* G_TYPE_ENUM
Packit ae235b
   */
Packit ae235b
  info.class_size = sizeof (GEnumClass);
Packit ae235b
  type = g_type_register_fundamental (G_TYPE_ENUM, g_intern_static_string ("GEnum"), &info, &finfo,
Packit ae235b
				      G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
Packit ae235b
  g_assert (type == G_TYPE_ENUM);
Packit ae235b
  
Packit ae235b
  /* G_TYPE_FLAGS
Packit ae235b
   */
Packit ae235b
  info.class_size = sizeof (GFlagsClass);
Packit ae235b
  type = g_type_register_fundamental (G_TYPE_FLAGS, g_intern_static_string ("GFlags"), &info, &finfo,
Packit ae235b
				      G_TYPE_FLAG_ABSTRACT | G_TYPE_FLAG_VALUE_ABSTRACT);
Packit ae235b
  g_assert (type == G_TYPE_FLAGS);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
value_flags_enum_init (GValue *value)
Packit ae235b
{
Packit ae235b
  value->data[0].v_long = 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
value_flags_enum_copy_value (const GValue *src_value,
Packit ae235b
			     GValue	  *dest_value)
Packit ae235b
{
Packit ae235b
  dest_value->data[0].v_long = src_value->data[0].v_long;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar*
Packit ae235b
value_flags_enum_collect_value (GValue      *value,
Packit ae235b
				guint        n_collect_values,
Packit ae235b
				GTypeCValue *collect_values,
Packit ae235b
				guint        collect_flags)
Packit ae235b
{
Packit ae235b
  value->data[0].v_long = collect_values[0].v_int;
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar*
Packit ae235b
value_flags_enum_lcopy_value (const GValue *value,
Packit ae235b
			      guint         n_collect_values,
Packit ae235b
			      GTypeCValue  *collect_values,
Packit ae235b
			      guint         collect_flags)
Packit ae235b
{
Packit ae235b
  gint *int_p = collect_values[0].v_pointer;
Packit ae235b
  
Packit ae235b
  if (!int_p)
Packit ae235b
    return g_strdup_printf ("value location for '%s' passed as NULL", G_VALUE_TYPE_NAME (value));
Packit ae235b
  
Packit ae235b
  *int_p = value->data[0].v_long;
Packit ae235b
  
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_enum_register_static:
Packit ae235b
 * @name: A nul-terminated string used as the name of the new type.
Packit ae235b
 * @const_static_values: An array of #GEnumValue structs for the possible
Packit ae235b
 *  enumeration values. The array is terminated by a struct with all
Packit ae235b
 *  members being 0. GObject keeps a reference to the data, so it cannot
Packit ae235b
 *  be stack-allocated.
Packit ae235b
 *
Packit ae235b
 * Registers a new static enumeration type with the name @name.
Packit ae235b
 *
Packit ae235b
 * It is normally more convenient to let [glib-mkenums][glib-mkenums],
Packit ae235b
 * generate a my_enum_get_type() function from a usual C enumeration
Packit ae235b
 * definition  than to write one yourself using g_enum_register_static().
Packit ae235b
 *
Packit ae235b
 * Returns: The new type identifier.
Packit ae235b
 */
Packit ae235b
GType
Packit ae235b
g_enum_register_static (const gchar	 *name,
Packit ae235b
			const GEnumValue *const_static_values)
Packit ae235b
{
Packit ae235b
  GTypeInfo enum_type_info = {
Packit ae235b
    sizeof (GEnumClass), /* class_size */
Packit ae235b
    NULL,                /* base_init */
Packit ae235b
    NULL,                /* base_finalize */
Packit ae235b
    (GClassInitFunc) g_enum_class_init,
Packit ae235b
    NULL,                /* class_finalize */
Packit ae235b
    NULL,                /* class_data */
Packit ae235b
    0,                   /* instance_size */
Packit ae235b
    0,                   /* n_preallocs */
Packit ae235b
    NULL,                /* instance_init */
Packit ae235b
    NULL,		 /* value_table */
Packit ae235b
  };
Packit ae235b
  GType type;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (name != NULL, 0);
Packit ae235b
  g_return_val_if_fail (const_static_values != NULL, 0);
Packit ae235b
  
Packit ae235b
  enum_type_info.class_data = const_static_values;
Packit ae235b
  
Packit ae235b
  type = g_type_register_static (G_TYPE_ENUM, name, &enum_type_info, 0);
Packit ae235b
  
Packit ae235b
  return type;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_flags_register_static:
Packit ae235b
 * @name: A nul-terminated string used as the name of the new type.
Packit ae235b
 * @const_static_values: An array of #GFlagsValue structs for the possible
Packit ae235b
 *  flags values. The array is terminated by a struct with all members being 0.
Packit ae235b
 *  GObject keeps a reference to the data, so it cannot be stack-allocated.
Packit ae235b
 *
Packit ae235b
 * Registers a new static flags type with the name @name.
Packit ae235b
 *
Packit ae235b
 * It is normally more convenient to let [glib-mkenums][glib-mkenums]
Packit ae235b
 * generate a my_flags_get_type() function from a usual C enumeration
Packit ae235b
 * definition than to write one yourself using g_flags_register_static().
Packit ae235b
 *
Packit ae235b
 * Returns: The new type identifier.
Packit ae235b
 */
Packit ae235b
GType
Packit ae235b
g_flags_register_static (const gchar	   *name,
Packit ae235b
			 const GFlagsValue *const_static_values)
Packit ae235b
{
Packit ae235b
  GTypeInfo flags_type_info = {
Packit ae235b
    sizeof (GFlagsClass), /* class_size */
Packit ae235b
    NULL,                 /* base_init */
Packit ae235b
    NULL,                 /* base_finalize */
Packit ae235b
    (GClassInitFunc) g_flags_class_init,
Packit ae235b
    NULL,                 /* class_finalize */
Packit ae235b
    NULL,                 /* class_data */
Packit ae235b
    0,                    /* instance_size */
Packit ae235b
    0,                    /* n_preallocs */
Packit ae235b
    NULL,                 /* instance_init */
Packit ae235b
    NULL,		  /* value_table */
Packit ae235b
  };
Packit ae235b
  GType type;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (name != NULL, 0);
Packit ae235b
  g_return_val_if_fail (const_static_values != NULL, 0);
Packit ae235b
  
Packit ae235b
  flags_type_info.class_data = const_static_values;
Packit ae235b
  
Packit ae235b
  type = g_type_register_static (G_TYPE_FLAGS, name, &flags_type_info, 0);
Packit ae235b
  
Packit ae235b
  return type;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_enum_complete_type_info:
Packit ae235b
 * @g_enum_type: the type identifier of the type being completed
Packit ae235b
 * @info: (out callee-allocates): the #GTypeInfo struct to be filled in
Packit ae235b
 * @const_values: An array of #GEnumValue structs for the possible
Packit ae235b
 *  enumeration values. The array is terminated by a struct with all
Packit ae235b
 *  members being 0.
Packit ae235b
 *
Packit ae235b
 * This function is meant to be called from the `complete_type_info`
Packit ae235b
 * function of a #GTypePlugin implementation, as in the following
Packit ae235b
 * example:
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 * static void
Packit ae235b
 * my_enum_complete_type_info (GTypePlugin     *plugin,
Packit ae235b
 *                             GType            g_type,
Packit ae235b
 *                             GTypeInfo       *info,
Packit ae235b
 *                             GTypeValueTable *value_table)
Packit ae235b
 * {
Packit ae235b
 *   static const GEnumValue values[] = {
Packit ae235b
 *     { MY_ENUM_FOO, "MY_ENUM_FOO", "foo" },
Packit ae235b
 *     { MY_ENUM_BAR, "MY_ENUM_BAR", "bar" },
Packit ae235b
 *     { 0, NULL, NULL }
Packit ae235b
 *   };
Packit ae235b
 *
Packit ae235b
 *   g_enum_complete_type_info (type, info, values);
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_enum_complete_type_info (GType	     g_enum_type,
Packit ae235b
			   GTypeInfo	    *info,
Packit ae235b
			   const GEnumValue *const_values)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_TYPE_IS_ENUM (g_enum_type));
Packit ae235b
  g_return_if_fail (info != NULL);
Packit ae235b
  g_return_if_fail (const_values != NULL);
Packit ae235b
  
Packit ae235b
  info->class_size = sizeof (GEnumClass);
Packit ae235b
  info->base_init = NULL;
Packit ae235b
  info->base_finalize = NULL;
Packit ae235b
  info->class_init = (GClassInitFunc) g_enum_class_init;
Packit ae235b
  info->class_finalize = NULL;
Packit ae235b
  info->class_data = const_values;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_flags_complete_type_info:
Packit ae235b
 * @g_flags_type: the type identifier of the type being completed
Packit ae235b
 * @info: (out callee-allocates): the #GTypeInfo struct to be filled in
Packit ae235b
 * @const_values: An array of #GFlagsValue structs for the possible
Packit ae235b
 *  enumeration values. The array is terminated by a struct with all
Packit ae235b
 *  members being 0.
Packit ae235b
 *
Packit ae235b
 * This function is meant to be called from the complete_type_info()
Packit ae235b
 * function of a #GTypePlugin implementation, see the example for
Packit ae235b
 * g_enum_complete_type_info() above.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_flags_complete_type_info (GType	       g_flags_type,
Packit ae235b
			    GTypeInfo	      *info,
Packit ae235b
			    const GFlagsValue *const_values)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_TYPE_IS_FLAGS (g_flags_type));
Packit ae235b
  g_return_if_fail (info != NULL);
Packit ae235b
  g_return_if_fail (const_values != NULL);
Packit ae235b
  
Packit ae235b
  info->class_size = sizeof (GFlagsClass);
Packit ae235b
  info->base_init = NULL;
Packit ae235b
  info->base_finalize = NULL;
Packit ae235b
  info->class_init = (GClassInitFunc) g_flags_class_init;
Packit ae235b
  info->class_finalize = NULL;
Packit ae235b
  info->class_data = const_values;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_enum_class_init (GEnumClass *class,
Packit ae235b
		   gpointer    class_data)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_ENUM_CLASS (class));
Packit ae235b
  
Packit ae235b
  class->minimum = 0;
Packit ae235b
  class->maximum = 0;
Packit ae235b
  class->n_values = 0;
Packit ae235b
  class->values = class_data;
Packit ae235b
  
Packit ae235b
  if (class->values)
Packit ae235b
    {
Packit ae235b
      GEnumValue *values;
Packit ae235b
      
Packit ae235b
      class->minimum = class->values->value;
Packit ae235b
      class->maximum = class->values->value;
Packit ae235b
      for (values = class->values; values->value_name; values++)
Packit ae235b
	{
Packit ae235b
	  class->minimum = MIN (class->minimum, values->value);
Packit ae235b
	  class->maximum = MAX (class->maximum, values->value);
Packit ae235b
	  class->n_values++;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_flags_class_init (GFlagsClass *class,
Packit ae235b
		    gpointer	 class_data)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_IS_FLAGS_CLASS (class));
Packit ae235b
  
Packit ae235b
  class->mask = 0;
Packit ae235b
  class->n_values = 0;
Packit ae235b
  class->values = class_data;
Packit ae235b
  
Packit ae235b
  if (class->values)
Packit ae235b
    {
Packit ae235b
      GFlagsValue *values;
Packit ae235b
      
Packit ae235b
      for (values = class->values; values->value_name; values++)
Packit ae235b
	{
Packit ae235b
	  class->mask |= values->value;
Packit ae235b
	  class->n_values++;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_enum_get_value_by_name:
Packit ae235b
 * @enum_class: a #GEnumClass
Packit ae235b
 * @name: the name to look up
Packit ae235b
 *
Packit ae235b
 * Looks up a #GEnumValue by name.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GEnumValue with name @name,
Packit ae235b
 *          or %NULL if the enumeration doesn't have a member
Packit ae235b
 *          with that name
Packit ae235b
 */
Packit ae235b
GEnumValue*
Packit ae235b
g_enum_get_value_by_name (GEnumClass  *enum_class,
Packit ae235b
			  const gchar *name)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
Packit ae235b
  g_return_val_if_fail (name != NULL, NULL);
Packit ae235b
  
Packit ae235b
  if (enum_class->n_values)
Packit ae235b
    {
Packit ae235b
      GEnumValue *enum_value;
Packit ae235b
      
Packit ae235b
      for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
Packit ae235b
	if (strcmp (name, enum_value->value_name) == 0)
Packit ae235b
	  return enum_value;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_flags_get_value_by_name:
Packit ae235b
 * @flags_class: a #GFlagsClass
Packit ae235b
 * @name: the name to look up
Packit ae235b
 *
Packit ae235b
 * Looks up a #GFlagsValue by name.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GFlagsValue with name @name,
Packit ae235b
 *          or %NULL if there is no flag with that name
Packit ae235b
 */
Packit ae235b
GFlagsValue*
Packit ae235b
g_flags_get_value_by_name (GFlagsClass *flags_class,
Packit ae235b
			   const gchar *name)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
Packit ae235b
  g_return_val_if_fail (name != NULL, NULL);
Packit ae235b
  
Packit ae235b
  if (flags_class->n_values)
Packit ae235b
    {
Packit ae235b
      GFlagsValue *flags_value;
Packit ae235b
      
Packit ae235b
      for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
Packit ae235b
	if (strcmp (name, flags_value->value_name) == 0)
Packit ae235b
	  return flags_value;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_enum_get_value_by_nick:
Packit ae235b
 * @enum_class: a #GEnumClass
Packit ae235b
 * @nick: the nickname to look up
Packit ae235b
 *
Packit ae235b
 * Looks up a #GEnumValue by nickname.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GEnumValue with nickname @nick,
Packit ae235b
 *          or %NULL if the enumeration doesn't have a member
Packit ae235b
 *          with that nickname
Packit ae235b
 */
Packit ae235b
GEnumValue*
Packit ae235b
g_enum_get_value_by_nick (GEnumClass  *enum_class,
Packit ae235b
			  const gchar *nick)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
Packit ae235b
  g_return_val_if_fail (nick != NULL, NULL);
Packit ae235b
  
Packit ae235b
  if (enum_class->n_values)
Packit ae235b
    {
Packit ae235b
      GEnumValue *enum_value;
Packit ae235b
      
Packit ae235b
      for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
Packit ae235b
	if (enum_value->value_nick && strcmp (nick, enum_value->value_nick) == 0)
Packit ae235b
	  return enum_value;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_flags_get_value_by_nick:
Packit ae235b
 * @flags_class: a #GFlagsClass
Packit ae235b
 * @nick: the nickname to look up
Packit ae235b
 *
Packit ae235b
 * Looks up a #GFlagsValue by nickname.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GFlagsValue with nickname @nick,
Packit ae235b
 *          or %NULL if there is no flag with that nickname
Packit ae235b
 */
Packit ae235b
GFlagsValue*
Packit ae235b
g_flags_get_value_by_nick (GFlagsClass *flags_class,
Packit ae235b
			   const gchar *nick)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
Packit ae235b
  g_return_val_if_fail (nick != NULL, NULL);
Packit ae235b
  
Packit ae235b
  if (flags_class->n_values)
Packit ae235b
    {
Packit ae235b
      GFlagsValue *flags_value;
Packit ae235b
      
Packit ae235b
      for (flags_value = flags_class->values; flags_value->value_nick; flags_value++)
Packit ae235b
	if (flags_value->value_nick && strcmp (nick, flags_value->value_nick) == 0)
Packit ae235b
	  return flags_value;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_enum_get_value:
Packit ae235b
 * @enum_class: a #GEnumClass
Packit ae235b
 * @value: the value to look up
Packit ae235b
 *
Packit ae235b
 * Returns the #GEnumValue for a value.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the #GEnumValue for @value, or %NULL
Packit ae235b
 *          if @value is not a member of the enumeration
Packit ae235b
 */
Packit ae235b
GEnumValue*
Packit ae235b
g_enum_get_value (GEnumClass *enum_class,
Packit ae235b
		  gint	      value)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_ENUM_CLASS (enum_class), NULL);
Packit ae235b
  
Packit ae235b
  if (enum_class->n_values)
Packit ae235b
    {
Packit ae235b
      GEnumValue *enum_value;
Packit ae235b
      
Packit ae235b
      for (enum_value = enum_class->values; enum_value->value_name; enum_value++)
Packit ae235b
	if (enum_value->value == value)
Packit ae235b
	  return enum_value;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_flags_get_first_value:
Packit ae235b
 * @flags_class: a #GFlagsClass
Packit ae235b
 * @value: the value
Packit ae235b
 *
Packit ae235b
 * Returns the first #GFlagsValue which is set in @value.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer none): the first #GFlagsValue which is set in
Packit ae235b
 *          @value, or %NULL if none is set
Packit ae235b
 */
Packit ae235b
GFlagsValue*
Packit ae235b
g_flags_get_first_value (GFlagsClass *flags_class,
Packit ae235b
			 guint	      value)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
Packit ae235b
  
Packit ae235b
  if (flags_class->n_values)
Packit ae235b
    {
Packit ae235b
      GFlagsValue *flags_value;
Packit ae235b
Packit ae235b
      if (value == 0)
Packit ae235b
        {
Packit ae235b
          for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
Packit ae235b
            if (flags_value->value == 0)
Packit ae235b
              return flags_value;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          for (flags_value = flags_class->values; flags_value->value_name; flags_value++)
Packit ae235b
            if (flags_value->value != 0 && (flags_value->value & value) == flags_value->value)
Packit ae235b
              return flags_value;
Packit ae235b
        }      
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_enum_to_string:
Packit ae235b
 * @g_enum_type: the type identifier of a #GEnumClass type
Packit ae235b
 * @value: the value
Packit ae235b
 *
Packit ae235b
 * Pretty-prints @value in the form of the enum’s name.
Packit ae235b
 *
Packit ae235b
 * This is intended to be used for debugging purposes. The format of the output
Packit ae235b
 * may change in the future.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a newly-allocated text string
Packit ae235b
 *
Packit ae235b
 * Since: 2.54
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_enum_to_string (GType g_enum_type,
Packit ae235b
                  gint  value)
Packit ae235b
{
Packit ae235b
  gchar *result;
Packit ae235b
  GEnumClass *enum_class;
Packit ae235b
  GEnumValue *enum_value;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_TYPE_IS_ENUM (g_enum_type), NULL);
Packit ae235b
Packit ae235b
  enum_class = g_type_class_ref (g_enum_type);
Packit ae235b
Packit ae235b
  /* Already warned */
Packit ae235b
  if (enum_class == NULL)
Packit ae235b
    return g_strdup_printf ("%d", value);
Packit ae235b
Packit ae235b
  enum_value = g_enum_get_value (enum_class, value);
Packit ae235b
Packit ae235b
  if (enum_value == NULL)
Packit ae235b
    result = g_strdup_printf ("%d", value);
Packit ae235b
  else
Packit ae235b
    result = g_strdup (enum_value->value_name);
Packit ae235b
Packit ae235b
  g_type_class_unref (enum_class);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_flags_get_value_string:
Packit ae235b
 * @flags_class: a #GFlagsClass
Packit ae235b
 * @value: the value
Packit ae235b
 *
Packit ae235b
 * Pretty-prints @value in the form of the flag names separated by ` | ` and
Packit ae235b
 * sorted. Any extra bits will be shown at the end as a hexadecimal number.
Packit ae235b
 *
Packit ae235b
 * This is intended to be used for debugging purposes. The format of the output
Packit ae235b
 * may change in the future.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a newly-allocated text string
Packit ae235b
 *
Packit ae235b
 * Since: 2.54
Packit ae235b
 */
Packit ae235b
static gchar *
Packit ae235b
g_flags_get_value_string (GFlagsClass *flags_class,
Packit ae235b
                          guint        value)
Packit ae235b
{
Packit ae235b
  GString *str;
Packit ae235b
  GFlagsValue *flags_value;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_FLAGS_CLASS (flags_class), NULL);
Packit ae235b
Packit ae235b
  str = g_string_new (NULL);
Packit ae235b
Packit ae235b
  while ((str->len == 0 || value != 0) &&
Packit ae235b
         (flags_value = g_flags_get_first_value (flags_class, value)) != NULL)
Packit ae235b
    {
Packit ae235b
      if (str->len > 0)
Packit ae235b
        g_string_append (str, " | ");
Packit ae235b
Packit ae235b
      g_string_append (str, flags_value->value_name);
Packit ae235b
Packit ae235b
      value &= ~flags_value->value;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Show the extra bits */
Packit ae235b
  if (value != 0 || str->len == 0)
Packit ae235b
    {
Packit ae235b
      if (str->len > 0)
Packit ae235b
        g_string_append (str, " | ");
Packit ae235b
Packit ae235b
      g_string_append_printf (str, "0x%x", value);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return g_string_free (str, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_flags_to_string:
Packit ae235b
 * @flags_type: the type identifier of a #GFlagsClass type
Packit ae235b
 * @value: the value
Packit ae235b
 *
Packit ae235b
 * Pretty-prints @value in the form of the flag names separated by ` | ` and
Packit ae235b
 * sorted. Any extra bits will be shown at the end as a hexadecimal number.
Packit ae235b
 *
Packit ae235b
 * This is intended to be used for debugging purposes. The format of the output
Packit ae235b
 * may change in the future.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a newly-allocated text string
Packit ae235b
 *
Packit ae235b
 * Since: 2.54
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_flags_to_string (GType flags_type,
Packit ae235b
                   guint value)
Packit ae235b
{
Packit ae235b
  gchar *result;
Packit ae235b
  GFlagsClass *flags_class;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_TYPE_IS_FLAGS (flags_type), NULL);
Packit ae235b
Packit ae235b
  flags_class = g_type_class_ref (flags_type);
Packit ae235b
Packit ae235b
  /* Already warned */
Packit ae235b
  if (flags_class == NULL)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  result = g_flags_get_value_string (flags_class, value);
Packit ae235b
Packit ae235b
  g_type_class_unref (flags_class);
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_value_set_enum:
Packit ae235b
 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
Packit ae235b
 * @v_enum: enum value to be set
Packit ae235b
 *
Packit ae235b
 * Set the contents of a %G_TYPE_ENUM #GValue to @v_enum.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_value_set_enum (GValue *value,
Packit ae235b
		  gint    v_enum)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_VALUE_HOLDS_ENUM (value));
Packit ae235b
  
Packit ae235b
  value->data[0].v_long = v_enum;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_value_get_enum:
Packit ae235b
 * @value: a valid #GValue whose type is derived from %G_TYPE_ENUM
Packit ae235b
 *
Packit ae235b
 * Get the contents of a %G_TYPE_ENUM #GValue.
Packit ae235b
 *
Packit ae235b
 * Returns: enum contents of @value
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_value_get_enum (const GValue *value)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_VALUE_HOLDS_ENUM (value), 0);
Packit ae235b
  
Packit ae235b
  return value->data[0].v_long;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_value_set_flags:
Packit ae235b
 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
Packit ae235b
 * @v_flags: flags value to be set
Packit ae235b
 *
Packit ae235b
 * Set the contents of a %G_TYPE_FLAGS #GValue to @v_flags.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_value_set_flags (GValue *value,
Packit ae235b
		   guint   v_flags)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (G_VALUE_HOLDS_FLAGS (value));
Packit ae235b
  
Packit ae235b
  value->data[0].v_ulong = v_flags;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_value_get_flags:
Packit ae235b
 * @value: a valid #GValue whose type is derived from %G_TYPE_FLAGS
Packit ae235b
 *
Packit ae235b
 * Get the contents of a %G_TYPE_FLAGS #GValue.
Packit ae235b
 *
Packit ae235b
 * Returns: flags contents of @value
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_value_get_flags (const GValue *value)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_VALUE_HOLDS_FLAGS (value), 0);
Packit ae235b
  
Packit ae235b
  return value->data[0].v_ulong;
Packit ae235b
}