[libvirt] [libvirt-glib 10/37] Add gvir_config_genum_get_nick helper

Marc-André Lureau marcandre.lureau at gmail.com
Fri Nov 11 14:54:50 UTC 2011


ack

On Thu, Nov 10, 2011 at 9:33 PM, Christophe Fergeau <cfergeau at redhat.com> wrote:
> We will often need to convert from an enum to its string
> representation, add an helper for that to avoid duplicating that
> code.
> ---
>  libvirt-gconfig/libvirt-gconfig-clock.c           |   15 ++++++---------
>  libvirt-gconfig/libvirt-gconfig-helpers-private.h |    1 +
>  libvirt-gconfig/libvirt-gconfig-helpers.c         |   17 +++++++++++++++++
>  3 files changed, 24 insertions(+), 9 deletions(-)
>
> diff --git a/libvirt-gconfig/libvirt-gconfig-clock.c b/libvirt-gconfig/libvirt-gconfig-clock.c
> index dc7932d..3deb725 100644
> --- a/libvirt-gconfig/libvirt-gconfig-clock.c
> +++ b/libvirt-gconfig/libvirt-gconfig-clock.c
> @@ -27,6 +27,7 @@
>  #include <libxml/tree.h>
>
>  #include "libvirt-gconfig/libvirt-gconfig.h"
> +#include "libvirt-gconfig/libvirt-gconfig-helpers-private.h"
>
>  extern gboolean debugFlag;
>
> @@ -84,20 +85,16 @@ GVirConfigClock *gvir_config_clock_new_from_xml(const gchar *xml,
>  void gvir_config_clock_set_offset(GVirConfigClock *klock,
>                                   GVirConfigClockOffset offset)
>  {
> -    GEnumClass *enum_class;
> -    GEnumValue *enum_value;
>     xmlNodePtr node;
> +    const char *offset_str;
>
>     node = gvir_config_object_get_xml_node(GVIR_CONFIG_OBJECT(klock));
>     if (node == NULL)
>         return;
> -    enum_class = g_type_class_ref(GVIR_TYPE_CONFIG_CLOCK_OFFSET);
> -    enum_value = g_enum_get_value(enum_class, offset);
> -    if (enum_value != NULL)
> -        xmlNewProp(node, (xmlChar*)"offset", (xmlChar*)enum_value->value_nick);
> -
> -    g_type_class_unref(enum_class);
> -
> +    offset_str = gvir_config_genum_get_nick(GVIR_TYPE_CONFIG_CLOCK_OFFSET,
> +                                             offset);
> +    if (offset_str != NULL)
> +        xmlNewProp(node, (xmlChar*)"offset", (xmlChar*)offset_str);
>  }
>
>  void gvir_config_clock_set_timezone(GVirConfigClock *klock,
> diff --git a/libvirt-gconfig/libvirt-gconfig-helpers-private.h b/libvirt-gconfig/libvirt-gconfig-helpers-private.h
> index c7a5d6a..59efd24 100644
> --- a/libvirt-gconfig/libvirt-gconfig-helpers-private.h
> +++ b/libvirt-gconfig/libvirt-gconfig-helpers-private.h
> @@ -40,6 +40,7 @@ xmlChar * gvir_config_xml_get_child_element_content (xmlNode    *node,
>                                                      const char *child_name);
>  char *gvir_config_xml_get_child_element_content_glib (xmlNode    *node,
>                                                       const char *child_name);
> +const char *gvir_config_genum_get_nick (GType enum_type, gint value);
>  G_END_DECLS
>
>  #endif /* __LIBVIRT_GCONFIG_HELPERS_PRIVATE_H__ */
> diff --git a/libvirt-gconfig/libvirt-gconfig-helpers.c b/libvirt-gconfig/libvirt-gconfig-helpers.c
> index 2e28429..d069714 100644
> --- a/libvirt-gconfig/libvirt-gconfig-helpers.c
> +++ b/libvirt-gconfig/libvirt-gconfig-helpers.c
> @@ -178,3 +178,20 @@ gvir_config_xml_get_child_element_content_glib (xmlNode    *node,
>
>         return copy;
>  }
> +
> +const char *gvir_config_genum_get_nick (GType enum_type, gint value)
> +{
> +    GEnumClass *enum_class;
> +    GEnumValue *enum_value;
> +
> +    g_return_val_if_fail (G_TYPE_IS_ENUM (enum_type), NULL);
> +
> +    enum_class = g_type_class_ref(enum_type);
> +    enum_value = g_enum_get_value(enum_class, value);
> +    g_type_class_unref(enum_class);
> +
> +    if (enum_value != NULL)
> +        return enum_value->value_nick;
> +
> +    return NULL;
> +}
> --
> 1.7.7
>
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
>



-- 
Marc-André Lureau




More information about the libvir-list mailing list