[libvirt] [PATCH v2 1/7] internal: introduce macro helpers to reject exclusive flags

Pavel Hrdina phrdina at redhat.com
Fri Mar 27 10:01:22 UTC 2015


Inspired by commit 7e437ee7 that introduced similar macros for virsh
commands so we don't have to repeat the same code all over.

Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
---
 src/internal.h | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/src/internal.h b/src/internal.h
index 4d473af..eb8d231 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -327,6 +327,50 @@
         }                                                               \
     } while (0)
 
+/* Macros to help dealing with mutually exclusive flags. */
+
+/**
+ * VIR_EXCLUSIVE_FLAGS_RET:
+ *
+ * @FLAG1: First flag to be checked.
+ * @FLAG2: Second flag to be checked.
+ * @RET: Return value.
+ *
+ * Reject mutually exclusive API flags.  The checked flags are compared
+ * with flags variable.
+ *
+ * This helper does an early return and therefore it has to be called
+ * before anything that would require cleanup.
+ */
+# define VIR_EXCLUSIVE_FLAGS_RET(FLAG1, FLAG2, RET)                         \
+    if ((flags & FLAG1) && (flags & FLAG2)) {                               \
+        virReportInvalidArg(ctl,                                            \
+                            _("Flags '%s' and '%s' are mutually exclusive"),\
+                            #FLAG1, #FLAG2);                                \
+        return RET;                                                         \
+    }
+
+/**
+ * VIR_EXCLUSIVE_FLAGS_GOTO:
+ *
+ * @FLAG1: First flag to be checked.
+ * @FLAG2: Second flag to be checked.
+ * @LABEL: Label to jump to.
+ *
+ * Reject mutually exclusive API flags.  The checked flags are compared
+ * with flags variable.
+ *
+ * Returns nothing.  Jumps to a label if unsupported flags were
+ * passed to it.
+ */
+# define VIR_EXCLUSIVE_FLAGS_GOTO(FLAG1, FLAG2, LABEL)                      \
+    if ((flags & FLAG1) && (flags & FLAG2)) {                               \
+        virReportInvalidArg(ctl,                                            \
+                            _("Flags '%s' and '%s' are mutually exclusive"),\
+                            #FLAG1, #FLAG2);                                \
+        goto LABEL;                                                         \
+    }
+
 # define virCheckNonNullArgReturn(argname, retval)  \
     do {                                            \
         if (argname == NULL) {                      \
-- 
2.0.5




More information about the libvir-list mailing list