[libvirt] [PATCH v2 1/2] Introduce virCheckFlags for consistent flags checking

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

 



The idea is that every API implementation in driver which has flags
parameter should first call virCheckFlags() macro to check the function
was called with supported flags:

    virCheckFlags(VIR_SUPPORTED_FLAG_1 |
                  VIR_SUPPORTED_FLAG_2 |
                  VIR_ANOTHER_SUPPORTED_FLAG, -1);

The error massage which is printed when unsupported flags are passed
looks like:

    invalid argument in virFooBar: unsupported flags (0x2)

Where the unsupported flags part only prints those flags which were
passed but are not supported rather than all flags passed.
---
 src/internal.h |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/src/internal.h b/src/internal.h
index 2e73210..12e9a1f 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -191,4 +191,27 @@
     fprintf(stderr, "Unimplemented block at %s:%d\n",			\
             __FILE__, __LINE__);
 
+/**
+ * virCheckFlags:
+ * @supported: an OR'ed set of supported flags
+ * @retval: return value in case unsupported flags were passed
+ *
+ * Returns nothing. Exits the caller function if unsupported flags were
+ * passed to it.
+ */
+# define virCheckFlags(supported, retval)                               \
+    do {                                                                \
+        if ((flags & ~(supported))) {                                   \
+            virReportErrorHelper(NULL,                                  \
+                                 VIR_FROM_THIS,                         \
+                                 VIR_ERR_INVALID_ARG,                   \
+                                 __FILE__,                              \
+                                 __FUNCTION__,                          \
+                                 __LINE__,                              \
+                                 _("%s: unsupported flags (0x%x)"),     \
+                                 __FUNCTION__, flags & ~(supported));   \
+            return retval;                                              \
+        }                                                               \
+    } while (0)
+
 #endif                          /* __VIR_INTERNAL_H__ */
-- 
1.7.0.4

--
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]