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

Jiri Denemark jdenemar at redhat.com
Fri Apr 9 14:41:00 UTC 2010


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_SUPPORTED_ANOTHER_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 not supported rather than all flags passed.
---
 src/Makefile.am   |    3 ++-
 src/util/checks.h |   37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletions(-)
 create mode 100644 src/util/checks.h

diff --git a/src/Makefile.am b/src/Makefile.am
index d54e6d0..3db9d10 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -76,7 +76,8 @@ UTIL_SOURCES =							\
 		util/uuid.c util/uuid.h				\
 		util/util.c util/util.h				\
 		util/xml.c util/xml.h				\
-		util/virterror.c util/virterror_internal.h
+		util/virterror.c util/virterror_internal.h	\
+		util/checks.h
 
 EXTRA_DIST += util/threads-pthread.c util/threads-win32.c
 
diff --git a/src/util/checks.h b/src/util/checks.h
new file mode 100644
index 0000000..2333856
--- /dev/null
+++ b/src/util/checks.h
@@ -0,0 +1,37 @@
+/*
+ * Linux block and network stats.
+ *
+ * Copyright (C) 2010 Red Hat, Inc.
+ *
+ * See COPYING.LIB for the License of this software
+ *
+ * Jiri Denemark <jdenemar at redhat.com>
+ */
+
+#ifndef __CHECKS_H__
+# define __CHECKS_H__
+
+# include "virterror_internal.h"
+
+/**
+ * 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 /* __CHECKS_H__ */
-- 
1.7.0.4




More information about the libvir-list mailing list