Introduce virCheckFlags for consistent flags checking

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.
This commit is contained in:
Jiri Denemark 2010-04-13 15:48:04 +02:00
parent d2327278fb
commit 070da02fb5

View File

@ -199,4 +199,30 @@
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
*
* To avoid memory leaks this macro has to be used before any non-trivial
* code which could possibly allocate some memory.
*
* 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__ */