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

Pavel Hrdina phrdina at redhat.com
Fri Mar 20 14:38:59 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 | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 80 insertions(+)

diff --git a/src/internal.h b/src/internal.h
index 4d473af..5885f03 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -327,6 +327,86 @@
         }                                                               \
     } while (0)
 
+/* Macros to help dealing with mutually exclusive flags. */
+
+/**
+ * VIR_EXCLUSIVE_FLAGS_EXPR_RET:
+ *
+ * @NAME1: String containing the name of the flag.
+ * @EXPR1: Expression to validate the flag.
+ * @NAME2: String containing the name of the flag.
+ * @EXPR2: Expression to validate the flag.
+ *
+ * Reject mutually exclusive API flags.  Use the provided expression
+ * to check the flags.
+ *
+ * This helper does an early return and therefore it has to be called
+ * before anything that would require cleanup.
+ */
+# define VIR_EXCLUSIVE_FLAGS_EXPR_RET(NAME1, EXPR1, NAME2, EXPR2)           \
+    if ((EXPR1) && (EXPR2)) {                                               \
+        virReportInvalidArg(ctl,                                            \
+                            _("Flags '%s' and '%s' are mutually exclusive"),\
+                            NAME1, NAME2);                                  \
+        return -1;                                                          \
+    }
+
+/**
+ * VIR_EXCLUSIVE_FLAGS_VAR_RET:
+ *
+ * @FLAG1: First flag to be checked
+ * @FLAG2: Second flag to be checked
+ *
+ * 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)                              \
+    VIR_EXCLUSIVE_FLAGS_EXPR_RET(#FLAG1, flags & FLAG1, #FLAG2, flags & FLAG2)
+
+/**
+ * VIR_EXCLUSIVE_FLAGS_EXPR_GOTO:
+ *
+ * @NAME1: String containing the name of the flag.
+ * @EXPR1: Expression to validate the flag.
+ * @NAME2: String containing the name of the flag.
+ * @EXPR2: Expression to validate the flag.
+ * @LABEL: label to jump to.
+ *
+ * Reject mutually exclusive API flags.  Use the provided expression
+ * to check the flag.
+ *
+ * Returns nothing.  Jumps to a label if unsupported flags were
+ * passed to it.
+ */
+# define VIR_EXCLUSIVE_FLAGS_EXPR_GOTO(NAME1, EXPR1, NAME2, EXPR2, LABEL)   \
+    if ((EXPR1) && (EXPR2)) {                                               \
+        virReportInvalidArg(ctl,                                            \
+                            _("Flags '%s' and '%s' are mutually exclusive"),\
+                            NAME1, NAME2);                                  \
+        goto LABEL;                                                         \
+    }
+
+/**
+ * VIR_EXCLUSIVE_FLAGS_VAR_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)                      \
+    VIR_EXCLUSIVE_FLAGS_EXPR_GOTO(#FLAG1, flags & FLAG1,                    \
+                                  #FLAG2, flags & FLAG2, LABEL)
+
+
 # define virCheckNonNullArgReturn(argname, retval)  \
     do {                                            \
         if (argname == NULL) {                      \
-- 
2.0.5




More information about the libvir-list mailing list