]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
internal: introduce macro helpers to reject exclusive flags
authorPavel Hrdina <phrdina@redhat.com>
Fri, 20 Mar 2015 14:38:59 +0000 (15:38 +0100)
committerPavel Hrdina <phrdina@redhat.com>
Mon, 4 May 2015 07:20:00 +0000 (09:20 +0200)
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@redhat.com>
src/internal.h

index 4d473afdf98fb84bec22a8b32ada7e8f2d7c9ce0..b47ea60ac6e32c1c91be39ab4d95c6fd747a686b 100644 (file)
         }                                                               \
     } 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)                         \
+    do {                                                                    \
+        if ((flags & FLAG1) && (flags & FLAG2)) {                           \
+            virReportInvalidArg(ctl,                                        \
+                                _("Flags '%s' and '%s' are mutually "       \
+                                  "exclusive"),                             \
+                                #FLAG1, #FLAG2);                            \
+            return RET;                                                     \
+        }                                                                   \
+    } while (0)
+
+/**
+ * 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)                      \
+    do {                                                                    \
+        if ((flags & FLAG1) && (flags & FLAG2)) {                           \
+            virReportInvalidArg(ctl,                                        \
+                                _("Flags '%s' and '%s' are mutually "       \
+                                  "exclusive"),                             \
+                                #FLAG1, #FLAG2);                            \
+            goto LABEL;                                                     \
+        }                                                                   \
+    } while (0)
+
 # define virCheckNonNullArgReturn(argname, retval)  \
     do {                                            \
         if (argname == NULL) {                      \