]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/macro: add macro to iterate variadic args
authorDan Streetman <ddstreet@ieee.org>
Thu, 2 Feb 2023 20:58:10 +0000 (15:58 -0500)
committerDan Streetman <ddstreet@ieee.org>
Thu, 9 Mar 2023 12:11:04 +0000 (07:11 -0500)
src/basic/macro.h

index 63344f8d97362e10cade4fb04be1741b527afb29..2770d01aa910026afa678a71d15b877a1c6fc47f 100644 (file)
@@ -430,4 +430,13 @@ assert_cc(sizeof(dummy_t) == 0);
                 _q && _q > (base) ? &_q[-1] : NULL;      \
         })
 
+/* Iterate through each variadic arg. All must be the same type as 'entry' or must be implicitly
+ * convertable. The iteration variable 'entry' must already be defined. */
+#define VA_ARGS_FOREACH(entry, ...)                                     \
+        _VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__)
+#define _VA_ARGS_FOREACH(entry, _entries_, _current_, ...)         \
+        for (typeof(entry) _entries_[] = { __VA_ARGS__ }, *_current_ = _entries_; \
+             ((long)(_current_ - _entries_) < (long)ELEMENTSOF(_entries_)) && ({ entry = *_current_; true; }); \
+             _current_++)
+
 #include "log.h"