diff mbox series

[4/8] ccan/cppmaigc: Add CPPMAGIC_MAP_CNT(...) macro

Message ID 20180620053409.14538-5-alistair@popple.id.au
State Accepted
Headers show
Series Rework option parsing | expand

Commit Message

Alistair Popple June 20, 2018, 5:34 a.m. UTC
It can be useful to have a count to keep track of which argument number in the
list is being expanded.

Signed-off-by: Alistair Popple <alistair@popple.id.au>
---
 ccan/cppmagic/cppmagic.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
diff mbox series

Patch

diff --git a/ccan/cppmagic/cppmagic.h b/ccan/cppmagic/cppmagic.h
index ffd5848..4cfb892 100644
--- a/ccan/cppmagic/cppmagic.h
+++ b/ccan/cppmagic/cppmagic.h
@@ -177,6 +177,25 @@ 
 		()
 
 /**
+ * CPPMAGIC_MAP_CNT - iterate antoher macro across arguments adding a count
+ * @m: name of a two argument macro
+ *
+ * CPPMAGIC_MAP_CNT(@m, @a1, @a2, ..., @an)
+ *      expands to the expansion of
+ *            @m(0, @a1) , @m(1, @a2) , ... , @m(n - 1, @an)
+ */
+#define _CPPMAGIC_MAP_CNT_()		_CPPMAGIC_MAP_CNT
+#define _CPPMAGIC_MAP_CNT(m_, c_, a_, ...)				\
+	m_(c_, a_)							\
+	CPPMAGIC_IFELSE(CPPMAGIC_NONEMPTY(__VA_ARGS__))			\
+	(, CPPMAGIC_DEFER2(_CPPMAGIC_MAP_CNT_)()(m_, CPPMAGIC_INC(c_), __VA_ARGS__)) \
+	()
+#define CPPMAGIC_MAP_CNT(m_, ...)				\
+	CPPMAGIC_IFELSE(CPPMAGIC_NONEMPTY(__VA_ARGS__))		\
+	(CPPMAGIC_EVAL(_CPPMAGIC_MAP_CNT(m_, 0, __VA_ARGS__)))	\
+	()
+
+/**
  * CPPMAGIC_JOIN - separate arguments with given delimiter
  * @d: delimiter
  *