From cabb78068899232c152f4585f19d023e373aa73d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 2 Dec 2013 23:08:25 +0100 Subject: macro: add a macro to test whether a value is in a specified list Introduce IN_SET() macro to nicely check whether a value a is one of a few listed values. This makes writing this: if (a == 1 || a == 7 || a == 8 || a == 9) nicer, by allowing this: if (IN_SET(a, 1, 7, 8, 9)) This is particularly useful for state machine enums. --- src/shared/macro.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/shared') diff --git a/src/shared/macro.h b/src/shared/macro.h index 9f5f51cb13..54b641be23 100644 --- a/src/shared/macro.h +++ b/src/shared/macro.h @@ -285,4 +285,17 @@ do { \ #define SET_FLAG(v, flag, b) \ (v) = (b) ? ((v) | (flag)) : ((v) & ~(flag)) +#define IN_SET(x, ...) ({ \ + typeof(x) _x = (x); \ + unsigned _i; \ + bool _found = false; \ + for (_i = 0; _i < sizeof((typeof(_x)[]) { __VA_ARGS__ })/sizeof(typeof(_x)); _i++) \ + if (((typeof(_x)[]) { __VA_ARGS__ })[_i] == _x) { \ + _found = true; \ + break; \ + } \ + _found; \ + }) + + #include "log.h" -- cgit v1.2.3-54-g00ecf