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

Alistair Popple alistair at popple.id.au
Wed Jun 20 15:34:05 AEST 2018


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 at popple.id.au>
---
 ccan/cppmagic/cppmagic.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

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
  *
-- 
2.11.0



More information about the Pdbg mailing list