Skip to content

Commit cd4f43b

Browse files
ddstreetbluca
authored andcommitted
basic/macro: add macro to iterate variadic args
(cherry picked from commit e179f2d)
1 parent f52d795 commit cd4f43b

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/basic/macro.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,4 +424,13 @@ assert_cc(sizeof(dummy_t) == 0);
424424
_q && _q > (base) ? &_q[-1] : NULL; \
425425
})
426426

427+
/* Iterate through each variadic arg. All must be the same type as 'entry' or must be implicitly
428+
* convertable. The iteration variable 'entry' must already be defined. */
429+
#define VA_ARGS_FOREACH(entry, ...) \
430+
_VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__)
431+
#define _VA_ARGS_FOREACH(entry, _entries_, _current_, ...) \
432+
for (typeof(entry) _entries_[] = { __VA_ARGS__ }, *_current_ = _entries_; \
433+
((long)(_current_ - _entries_) < (long)ELEMENTSOF(_entries_)) && ({ entry = *_current_; true; }); \
434+
_current_++)
435+
427436
#include "log.h"

0 commit comments

Comments
 (0)