[libvirt] [dbus PATCH v2 3/5] utils: Introduce functions and macros for translating ENUMs to strings

Pavel Hrdina phrdina at redhat.com
Sat Mar 31 12:00:05 UTC 2018


On Thu, Mar 29, 2018 at 05:41:19PM +0200, Katerina Koukiou wrote:
> The functions were copied from src/util/virutil.* files from
> libvirt project
> 
> Signed-off-by: Katerina Koukiou <kkoukiou at redhat.com>
> ---
>  src/util.c | 27 +++++++++++++++++++++++++++
>  src/util.h | 28 ++++++++++++++++++++++++++++
>  2 files changed, 55 insertions(+)
> 
> diff --git a/src/util.c b/src/util.c
> index d6c27f3..3179dd7 100644
> --- a/src/util.c
> +++ b/src/util.c
> @@ -124,3 +124,30 @@ virtDBusUtilVirDomainListFree(virDomainPtr *domains)
>  
>      g_free(domains);
>  }
> +
> +const gchar *
> +virtDBusUtilEnumToString(const gchar *const*types,

s/*const*types/*const *types/

> +                         guint ntypes,
> +                         gint type)
> +{
> +    if (type < 0 || (unsigned)type >= ntypes)

s/unsigned/guint/

> +        return NULL;
> +
> +    return types[type];
> +}
> +
> +gint
> +virtDBusUtilEnumFromString(const gchar *const*types,

s/*const*types/*const *types/

> +                           guint ntypes,
> +                           const gchar *type)
> +{
> +    guint i;
> +    if (!type)
> +        return -1;
> +
> +    for (i = 0; i < ntypes; i++)
> +        if (strcmp(types[i], type) == 0)

You can use g_str_equal here.

> +            return i;
> +
> +    return -1;
> +}
> diff --git a/src/util.h b/src/util.h
> index 4304bac..61e01c9 100644
> --- a/src/util.h
> +++ b/src/util.h
> @@ -37,3 +37,31 @@ virtDBusUtilVirDomainListFree(virDomainPtr *domains);
>  
>  G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomain, virDomainFree);
>  G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainPtr, virtDBusUtilVirDomainListFree);
> +
> +gint
> +virtDBusUtilEnumFromString(const gchar *const*types,

s/*const*types/*const *types/

> +                           guint ntypes,
> +                           const gchar *type) G_GNUC_PURE;
> +
> +const gchar *
> +virtDBusUtilEnumToString(const gchar *const*types,

s/*const*types/*const *types/

> +                         guint ntypes,
> +                         gint type) G_GNUC_PURE;
> +
> +# define VIRT_DBUS_ENUM_IMPL(name, lastVal, ...) \

Remove the extra space after #.

> +    static const gchar *const name ##TypeList[] = { __VA_ARGS__ }; \
> +    G_STATIC_ASSERT(G_N_ELEMENTS(name ##TypeList) == lastVal); \
> +    const gchar *name ##TypeToString(int type) { \

s/int/gint/

> +        return virtDBusUtilEnumToString(name ##TypeList, \
> +                                        G_N_ELEMENTS(name ##TypeList), \
> +                                        type); \
> +    } \
> +    gint name ##TypeFromString(const gchar *type) { \
> +        return virtDBusUtilEnumFromString(name ##TypeList, \
> +                                          G_N_ELEMENTS(name ##TypeList), \
> +                                          type); \
> +    }
> +
> +# define VIRT_DBUS_ENUM_DECL(name) \

Remove the extra space after #.

> +    const gchar *name ##TypeToString(gint type) G_GNUC_PURE; \
> +    gint name ##TypeFromString(const gchar*type) G_GNUC_PURE;

s/gchar*type/gchar *type/

If you fix these small issues mostly introduced by copying the code

Reviewed-by: Pavel Hrdina <phrdina at redhat.com>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180331/135ac36f/attachment-0001.sig>


More information about the libvir-list mailing list