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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The functions were copied from src/util/virutil.* files from
libvirt project

Signed-off-by: Katerina Koukiou <kkoukiou@xxxxxxxxxx>
---
 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..be65172 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,
+                         guint ntypes,
+                         gint type)
+{
+    if (type < 0 || (guint)type >= ntypes)
+        return NULL;
+
+    return types[type];
+}
+
+gint
+virtDBusUtilEnumFromString(const gchar *const *types,
+                           guint ntypes,
+                           const gchar *type)
+{
+    guint i;
+    if (!type)
+        return -1;
+
+    for (i = 0; i < ntypes; i++)
+        if (g_str_equal(types[i], type) == 0)
+            return i;
+
+    return -1;
+}
diff --git a/src/util.h b/src/util.h
index 4304bac..c9d9cfd 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,
+                           guint ntypes,
+                           const gchar *type) G_GNUC_PURE;
+
+const gchar *
+virtDBusUtilEnumToString(const gchar *const *types,
+                         guint ntypes,
+                         gint type) G_GNUC_PURE;
+
+#define VIRT_DBUS_ENUM_IMPL(name, lastVal, ...) \
+    static const gchar *const name ##TypeList[] = { __VA_ARGS__ }; \
+    G_STATIC_ASSERT(G_N_ELEMENTS(name ##TypeList) == lastVal); \
+    const gchar *name ##TypeToString(gint type) { \
+        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) \
+    const gchar *name ##TypeToString(gint type) G_GNUC_PURE; \
+    gint name ##TypeFromString(const gchar *type) G_GNUC_PURE;
-- 
2.15.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux