summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2019-06-21 10:58:02 +0200
committerDavid Lamparter <equinox@diac24.net>2019-09-03 17:15:17 +0200
commit48373d46f1e82fb0413831fa85304cea5c1db766 (patch)
tree693e765b1726c517afe03c35a363275a2f9e843c
parentMerge pull request #4888 from donaldsharp/rmap_cleanup (diff)
downloadfrr-48373d46f1e82fb0413831fa85304cea5c1db766.tar.xz
frr-48373d46f1e82fb0413831fa85304cea5c1db766.zip
lib: add some macro helpers
Macro soup, now with 50% more macros. Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
-rw-r--r--lib/compiler.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/compiler.h b/lib/compiler.h
index 6700ca9e8..e430925e6 100644
--- a/lib/compiler.h
+++ b/lib/compiler.h
@@ -121,6 +121,49 @@ extern "C" {
#define macro_inline static inline __attribute__((unused))
#define macro_pure static inline __attribute__((unused, pure))
+
+/* variadic macros, use like:
+ * #define V_0() ...
+ * #define V_1(x) ...
+ * #define V(...) MACRO_VARIANT(V, ##__VA_ARGS__)(__VA_ARGS__)
+ */
+#define _MACRO_VARIANT(A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,A10, N, ...) N
+
+#define _CONCAT2(a, b) a ## b
+#define _CONCAT(a, b) _CONCAT2(a,b)
+
+#define MACRO_VARIANT(NAME, ...) \
+ _CONCAT(NAME, _MACRO_VARIANT(0, ##__VA_ARGS__, \
+ _10, _9, _8, _7, _6, _5, _4, _3, _2, _1, _0))
+
+#define NAMECTR(name) _CONCAT(name, __COUNTER__)
+
+/* per-arg repeat macros, use like:
+ * #define PERARG(n) ...n...
+ * #define FOO(...) MACRO_REPEAT(PERARG, ##__VA_ARGS__)
+ */
+
+#define _MACRO_REPEAT_0(NAME)
+#define _MACRO_REPEAT_1(NAME, A1) \
+ NAME(A1)
+#define _MACRO_REPEAT_2(NAME, A1, A2) \
+ NAME(A1) NAME(A2)
+#define _MACRO_REPEAT_3(NAME, A1, A2, A3) \
+ NAME(A1) NAME(A2) NAME(A3)
+#define _MACRO_REPEAT_4(NAME, A1, A2, A3, A4) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4)
+#define _MACRO_REPEAT_5(NAME, A1, A2, A3, A4, A5) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5)
+#define _MACRO_REPEAT_6(NAME, A1, A2, A3, A4, A5, A6) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6)
+#define _MACRO_REPEAT_7(NAME, A1, A2, A3, A4, A5, A6, A7) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6) NAME(A7)
+#define _MACRO_REPEAT_8(NAME, A1, A2, A3, A4, A5, A6, A7, A8) \
+ NAME(A1) NAME(A2) NAME(A3) NAME(A4) NAME(A5) NAME(A6) NAME(A7) NAME(A8)
+
+#define MACRO_REPEAT(NAME, ...) \
+ MACRO_VARIANT(_MACRO_REPEAT, ##__VA_ARGS__)(NAME, ##__VA_ARGS__)
+
/*
* for warnings on macros, put in the macro content like this:
* #define MACRO BLA CPP_WARN("MACRO has been deprecated")