macros: simplify NM_IN_SET() macro
authorThomas Haller <thaller@redhat.com>
Thu, 11 Feb 2016 16:24:33 +0000 (17:24 +0100)
committerThomas Haller <thaller@redhat.com>
Thu, 11 Feb 2016 16:54:38 +0000 (17:54 +0100)
Also fix NM_IN_SET_SE() to guaranteed return 0 or 1.

libnm-core/tests/test-general.c
shared/nm-macros-internal.h

index 99b96b2..48a8b7c 100644 (file)
@@ -4864,6 +4864,8 @@ test_nm_in_set (void)
        _ASSERT (4,  NM_IN_SET (-1, G( 1), G( 2), G( 3), G(-1)));
 
        _ASSERT (4,  NM_IN_SET (-1, G( 1), G( 2), G( 3), G(-1), G( 5)));
+       _ASSERT (5,  NM_IN_SET (-1, G( 1), G( 2), G( 3), G( 4), G(-1)));
+       _ASSERT (6,  NM_IN_SET (-1, G( 1), G( 2), G( 3), G( 4), G( 5), G( -1)));
 
        _ASSERT (1, !NM_IN_SET_SE (-1, G( 1)));
        _ASSERT (1,  NM_IN_SET_SE (-1, G(-1)));
@@ -4889,6 +4891,7 @@ test_nm_in_set (void)
        _ASSERT (4,  NM_IN_SET_SE (-1, G( 1), G( 2), G( 3), G(-1)));
 
        _ASSERT (5,  NM_IN_SET_SE (-1, G( 1), G( 2), G( 3), G(-1), G( 5)));
+       _ASSERT (6,  NM_IN_SET_SE (-1, G( 1), G( 2), G( 3), G( 4), G( 5), G(-1)));
 #undef G
 #undef N
 #undef _ASSERT
index c178758..c70c6e2 100644 (file)
 
 /********************************************************/
 
-#define _NM_IN_SET_EVAL_1(op, x, y1)                              \
-    ({                                                            \
-        typeof(x) _x = (x);                                       \
-        (   (_x == (y1))                                          \
-        );                                                        \
+#define _NM_IN_SET_EVAL_1(op, _x, y1)                               \
+    (_x == (y1))
+
+#define _NM_IN_SET_EVAL_2(op, _x, y1, y2)                           \
+    (   (_x == (y1))                                                \
+     op (_x == (y2))                                                \
+    )
+
+#define _NM_IN_SET_EVAL_3(op, _x, y1, y2, y3)                       \
+    (   (_x == (y1))                                                \
+     op (_x == (y2))                                                \
+     op (_x == (y3))                                                \
+    )
+
+#define _NM_IN_SET_EVAL_4(op, _x, y1, y2, y3, y4)                   \
+    (   (_x == (y1))                                                \
+     op (_x == (y2))                                                \
+     op (_x == (y3))                                                \
+     op (_x == (y4))                                                \
+    )
+
+#define _NM_IN_SET_EVAL_5(op, _x, y1, y2, y3, y4, y5)               \
+    (   (_x == (y1))                                                \
+     op (_x == (y2))                                                \
+     op (_x == (y3))                                                \
+     op (_x == (y4))                                                \
+     op (_x == (y5))                                                \
+    )
+
+#define _NM_IN_SET_EVAL_6(op, _x, y1, y2, y3, y4, y5, y6)           \
+    (   (_x == (y1))                                                \
+     op (_x == (y2))                                                \
+     op (_x == (y3))                                                \
+     op (_x == (y4))                                                \
+     op (_x == (y5))                                                \
+     op (_x == (y6))                                                \
+    )
+
+#define _NM_IN_SET_EVAL_N2(op, _x, n, ...)        _NM_IN_SET_EVAL_##n(op, _x, __VA_ARGS__)
+#define _NM_IN_SET_EVAL_N(op, x, n, ...)                            \
+    ({                                                              \
+        typeof(x) _x = (x);                                         \
+        !!_NM_IN_SET_EVAL_N2(op, _x, n, __VA_ARGS__);               \
     })
 
-#define _NM_IN_SET_EVAL_2(op, x, y1, y2)                          \
-    ({                                                            \
-        typeof(x) _x = (x);                                       \
-        (   (_x == (y1))                                          \
-         op (_x == (y2))                                          \
-        );                                                        \
-    })
-
-#define _NM_IN_SET_EVAL_3(op, x, y1, y2, y3)                      \
-    ({                                                            \
-        typeof(x) _x = (x);                                       \
-        (   (_x == (y1))                                          \
-         op (_x == (y2))                                          \
-         op (_x == (y3))                                          \
-        );                                                        \
-    })
-
-#define _NM_IN_SET_EVAL_4(op, x, y1, y2, y3, y4)                  \
-    ({                                                            \
-        typeof(x) _x = (x);                                       \
-        (   (_x == (y1))                                          \
-         op (_x == (y2))                                          \
-         op (_x == (y3))                                          \
-         op (_x == (y4))                                          \
-        );                                                        \
-    })
-
-#define _NM_IN_SET_EVAL_5(op, x, y1, y2, y3, y4, y5)              \
-    ({                                                            \
-        typeof(x) _x = (x);                                       \
-        (   (_x == (y1))                                          \
-         op (_x == (y2))                                          \
-         op (_x == (y3))                                          \
-         op (_x == (y4))                                          \
-         op (_x == (y5))                                          \
-        );                                                        \
-    })
-
-#define _NM_IN_SET_EVAL_N2(op, x, n, ...)        _NM_IN_SET_EVAL_##n(op, x, __VA_ARGS__)
-#define _NM_IN_SET_EVAL_N(op, x, n, ...)         _NM_IN_SET_EVAL_N2(op, x, n, __VA_ARGS__)
-
 /* Beware that this does short-circuit evaluation (use "||" instead of "|")
  * which has a possibly unexpected non-function-like behavior.
  * Use NM_IN_SET_SE if you need all arguments to be evaluted. */